github.com/0chain/gosdk@v1.17.11/wasmsdk/jsbridge/zcnworker.js.tpl (about) 1 importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.21.5/misc/wasm/wasm_exec.js','https://cdn.jsdelivr.net/gh/herumi/bls-wasm@v1.1.1/browser/bls.js'); 2 3 const go = new Go(); 4 go.argv = {{.ArgsToJS}} 5 go.env = {{.EnvToJS}} 6 const bls = self.bls 7 bls.init(bls.BN254).then(()=>{}) 8 9 async function getWasmModule() { 10 const cache = await caches.open('wasm-cache'); 11 let response = await cache.match("{{.CachePath}}"); 12 if(!response?.ok) { 13 response = await fetch("{{.Path}}").then(res => res).catch(err => err); 14 if (!response?.ok) { 15 response = await fetch("{{.FallbackPath}}").then(res => res).catch(err => err); 16 } 17 if (!response.ok) { 18 throw new Error(`Failed to fetch WASM: ${response.statusText}`); 19 } 20 } 21 const bytes = await response.arrayBuffer(); 22 return WebAssembly.instantiate(bytes, go.importObject); 23 } 24 25 getWasmModule().then(result => { 26 go.run(result.instance); 27 }).catch(error => { 28 console.error("Failed to load WASM:", error); 29 }); 30 31 function hexStringToByte(str) { 32 if (!str) return new Uint8Array() 33 34 const a = [] 35 for (let i = 0, len = str.length; i < len; i += 2) { 36 a.push(parseInt(str.substr(i, 2), 16)) 37 } 38 39 return new Uint8Array(a) 40 } 41 42 self.__zcn_worker_wasm__ = { 43 sign: async (hash, secretKey) => { 44 if (!secretKey){ 45 const errMsg = 'err: wasm blsSign function requires a secret key' 46 console.warn(errMsg) 47 throw new Error(errMsg) 48 } 49 const bytes = hexStringToByte(hash) 50 const sk = bls.deserializeHexStrToSecretKey(secretKey) 51 const sig = sk.sign(bytes) 52 53 if (!sig) { 54 const errMsg = 'err: wasm blsSign function failed to sign transaction' 55 console.warn(errMsg) 56 throw new Error(errMsg) 57 } 58 59 return sig.serializeToHexStr() 60 }, 61 initProxyKeys: initProxyKeys, 62 verify: blsVerify, 63 verifyWith: blsVerifyWith, 64 addSignature: blsAddSignature 65 } 66 67 async function initProxyKeys(publicKey, privateKey) { 68 const pubKey = bls.deserializeHexStrToPublicKey(publicKey) 69 const privKey = bls.deserializeHexStrToSecretKey(privateKey) 70 bls.publicKey = pubKey 71 bls.secretKey = privKey 72 } 73 74 async function blsVerify(signature, hash) { 75 const bytes = hexStringToByte(hash) 76 const sig = bls.deserializeHexStrToSignature(signature) 77 return jsProxy.publicKey.verify(sig, bytes) 78 } 79 80 async function blsVerifyWith(pk, signature, hash) { 81 const publicKey = bls.deserializeHexStrToPublicKey(pk) 82 const bytes = hexStringToByte(hash) 83 const sig = bls.deserializeHexStrToSignature(signature) 84 return publicKey.verify(sig, bytes) 85 } 86 87 async function blsAddSignature(secretKey, signature, hash) { 88 const privateKey = bls.deserializeHexStrToSecretKey(secretKey) 89 const sig = bls.deserializeHexStrToSignature(signature) 90 var sig2 = privateKey.sign(hexStringToByte(hash)) 91 if (!sig2) { 92 const errMsg = 93 'err: wasm blsAddSignature function failed to sign transaction' 94 console.warn(errMsg) 95 throw new Error(errMsg) 96 } 97 98 sig.add(sig2) 99 100 return sig.serializeToHexStr() 101 }