github.com/0chain/gosdk@v1.17.11/wasmsdk/sdk.go (about) 1 //go:build js && wasm 2 // +build js,wasm 3 4 package main 5 6 import ( 7 "encoding/hex" 8 "encoding/json" 9 "fmt" 10 "io" 11 "os" 12 13 "github.com/0chain/gosdk/core/encryption" 14 "github.com/0chain/gosdk/core/imageutil" 15 "github.com/0chain/gosdk/core/logger" 16 "github.com/0chain/gosdk/zboxcore/sdk" 17 "github.com/0chain/gosdk/zboxcore/zboxutil" 18 "github.com/0chain/gosdk/zcncore" 19 ) 20 21 var CreateObjectURL func(buf []byte, mimeType string) string 22 23 // initSDKs init sdk with its parameters 24 // - chainID is the chain id 25 // - blockWorker is the block worker url, which is the DNS server used to locate the network nodes 26 // - signatureScheme is the signature scheme used for signing transactions 27 // - minConfirmation is the minimum number of confirmations required for a transaction to be considered final 28 // - minSubmit is the minimum number of times a transaction must be submitted to the network 29 // - confirmationChainLength is the number of blocks to wait for a transaction to be confirmed 30 // - zboxHost is the url of the 0box service 31 // - zboxAppType is the application type of the 0box service 32 // - sharderconsensous is the number of sharders to reach consensus 33 func initSDKs(chainID, blockWorker, signatureScheme string, 34 minConfirmation, minSubmit, confirmationChainLength int, 35 zboxHost, zboxAppType string, sharderconsensous int, isSplit bool) error { 36 37 zboxApiClient.SetRequest(zboxHost, zboxAppType) 38 39 err := sdk.InitStorageSDK("{}", blockWorker, chainID, signatureScheme, nil, 0) 40 if err != nil { 41 fmt.Println("wasm: InitStorageSDK ", err) 42 return err 43 } 44 45 if !isSplit && zcncore.IsSplitWallet() { 46 // split wallet should not be reset back, use the existing 47 isSplit = true 48 } 49 50 fmt.Println("init SDKs, isSplit:", isSplit) 51 err = zcncore.InitZCNSDK(blockWorker, signatureScheme, 52 zcncore.WithChainID(chainID), 53 zcncore.WithMinConfirmation(minConfirmation), 54 zcncore.WithMinSubmit(minSubmit), 55 zcncore.WithConfirmationChainLength(confirmationChainLength), 56 zcncore.WithSharderConsensous(sharderconsensous), 57 zcncore.WithIsSplitWallet(isSplit), 58 ) 59 60 if err != nil { 61 return err 62 } 63 sdk.SetWasm() 64 return nil 65 } 66 67 // getVersion retrieve the sdk version 68 func getVersion() string { 69 return sdk.GetVersion() 70 } 71 72 var sdkLogger *logger.Logger 73 var zcnLogger *logger.Logger 74 var logEnabled = false 75 76 // showLogs enable logging 77 func showLogs() { 78 zcnLogger.SetLevel(logger.DEBUG) 79 sdkLogger.SetLevel(logger.DEBUG) 80 81 zcnLogger.SetLogFile(os.Stdout, true) 82 sdkLogger.SetLogFile(os.Stdout, true) 83 84 logEnabled = true 85 } 86 87 // hideLogs disable logging 88 func hideLogs() { 89 zcnLogger.SetLevel(logger.ERROR) 90 sdkLogger.SetLevel(logger.ERROR) 91 92 zcnLogger.SetLogFile(io.Discard, false) 93 sdkLogger.SetLogFile(io.Discard, false) 94 95 logEnabled = false 96 } 97 98 // isWalletID check if the client id is a valid wallet hash 99 // - clientID is the client id to check 100 func isWalletID(clientID string) bool { 101 if clientID == "" { 102 return false 103 } 104 105 if !isHash(clientID) { 106 return false 107 } 108 109 return true 110 111 } 112 113 const HASH_LENGTH = 32 114 115 func isHash(str string) bool { 116 bytes, err := hex.DecodeString(str) 117 return err == nil && len(bytes) == HASH_LENGTH 118 } 119 120 // getLookupHash retrieve lookup hash with allocation id and path 121 // Lookup hash is generated by hashing the allocation id and path 122 // - allocationID is the allocation id 123 // - path is the path 124 func getLookupHash(allocationID string, path string) string { 125 return encryption.Hash(allocationID + ":" + path) 126 } 127 128 // createThumbnail create thumbnail of an image buffer. It supports 129 // - png 130 // - jpeg 131 // - gif 132 // - bmp 133 // - ccitt 134 // - riff 135 // - tiff 136 // - vector 137 // - vp8 138 // - vp8l 139 // - webp 140 // Paramters: 141 // - buf is the image buffer which carry the image in bytes 142 // - width is the width of the thumbnail 143 // - height is the height of the thumbnail 144 func createThumbnail(buf []byte, width, height int) ([]byte, error) { 145 return imageutil.CreateThumbnail(buf, width, height) 146 } 147 148 // makeSCRestAPICall issue a request to the public API of one of the smart contracts 149 // - scAddress is the smart contract address 150 // - relativePath is the relative path of the endpoint 151 // - paramsJson is the parameters in JSON format. It's a key-value map, and added as query parameters to the request. 152 func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, error) { 153 var params map[string]string 154 err := json.Unmarshal([]byte(paramsJson), ¶ms) 155 if err != nil { 156 sdkLogger.Error(fmt.Sprintf("Error parsing JSON: %v", err)) 157 } 158 b, err := zboxutil.MakeSCRestAPICall(scAddress, relativePath, params, nil) 159 return string(b), err 160 } 161 162 // send Send tokens to a client 163 // - toClientID is the client id to send tokens to 164 // - tokens is the number of tokens to send 165 // - fee is the transaction fee 166 // - desc is the description of the transaction 167 func send(toClientID string, tokens uint64, fee uint64, desc string) (string, error) { 168 return sdk.ExecuteSmartContractSend(toClientID, tokens, fee, desc) 169 }