@getpara/web-sdk 1.4.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1735 -3
- package/dist/index.js.br +0 -0
- package/dist/index.js.gz +0 -0
- package/dist/workers/worker.js +993 -2
- package/dist/workers/worker.js.br +0 -0
- package/dist/workers/worker.js.gz +0 -0
- package/package.json +4 -4
package/dist/workers/worker.js
CHANGED
|
@@ -1,3 +1,994 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
|
|
3
|
-
`);return n!=-1&&(console.log(o.substring(0,n)),o=o.substring(n+1)),s.length},write(r,s,n,a,g,u){if(n!==0||a!==s.length||g!==null){u(t());return}let d=this.writeSync(r,s);u(null,d)},chmod(r,s,n){n(t())},chown(r,s,n,a){a(t())},close(r,s){s(t())},fchmod(r,s,n){n(t())},fchown(r,s,n,a){a(t())},fstat(r,s){s(t())},fsync(r,s){s(null)},ftruncate(r,s,n){n(t())},lchown(r,s,n,a){a(t())},link(r,s,n){n(t())},lstat(r,s){s(t())},mkdir(r,s,n){n(t())},open(r,s,n,a){a(t())},read(r,s,n,a,g,u){u(t())},readdir(r,s){s(t())},readlink(r,s){s(t())},rename(r,s,n){n(t())},rmdir(r,s){s(t())},stat(r,s){s(t())},symlink(r,s,n){n(t())},truncate(r,s,n){n(t())},unlink(r,s){s(t())},utimes(r,s,n,a){a(t())}}}if(globalThis.process||(globalThis.process={getuid(){return-1},getgid(){return-1},geteuid(){return-1},getegid(){return-1},getgroups(){throw t()},pid:-1,ppid:-1,umask(){throw t()},cwd(){throw t()},chdir(){throw t()}}),!globalThis.crypto)throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");if(globalThis.performance||(globalThis.performance=Date),!globalThis.TextEncoder)throw new Error("globalThis.TextEncoder is not available, polyfill required");if(!globalThis.TextDecoder)throw new Error("globalThis.TextDecoder is not available, polyfill required");let c=new TextEncoder("utf-8"),l=new TextDecoder("utf-8");globalThis.Go=class{constructor(){this.argv=["js"],this.env={},this.exit=e=>{e!==0&&console.warn("exit code:",e)},this._exitPromise=new Promise(e=>{this._resolveExitPromise=e}),this._pendingEvent=null,this._scheduledTimeouts=new Map,this._nextCallbackTimeoutID=1;let o=(e,i)=>{this.mem.setUint32(e+0,i,!0),this.mem.setUint32(e+4,Math.floor(i/4294967296),!0)},r=(e,i)=>{this.mem.setUint32(e+0,i,!0)},s=e=>{let i=this.mem.getUint32(e+0,!0),f=this.mem.getInt32(e+4,!0);return i+f*4294967296},n=e=>{let i=this.mem.getFloat64(e,!0);if(i===0)return;if(!isNaN(i))return i;let f=this.mem.getUint32(e,!0);return this._values[f]},a=(e,i)=>{if(typeof i=="number"&&i!==0){if(isNaN(i)){this.mem.setUint32(e+4,2146959360,!0),this.mem.setUint32(e,0,!0);return}this.mem.setFloat64(e,i,!0);return}if(i===void 0){this.mem.setFloat64(e,0,!0);return}let m=this._ids.get(i);m===void 0&&(m=this._idPool.pop(),m===void 0&&(m=this._values.length),this._values[m]=i,this._goRefCounts[m]=0,this._ids.set(i,m)),this._goRefCounts[m]++;let w=0;switch(typeof i){case"object":i!==null&&(w=1);break;case"string":w=2;break;case"symbol":w=3;break;case"function":w=4;break}this.mem.setUint32(e+4,2146959360|w,!0),this.mem.setUint32(e,m,!0)},g=e=>{let i=s(e+0),f=s(e+8);return new Uint8Array(this._inst.exports.mem.buffer,i,f)},u=e=>{let i=s(e+0),f=s(e+8),m=new Array(f);for(let w=0;w<f;w++)m[w]=n(i+w*8);return m},d=e=>{let i=s(e+0),f=s(e+8);return l.decode(new DataView(this._inst.exports.mem.buffer,i,f))},h=Date.now()-performance.now();this.importObject={_gotest:{add:(e,i)=>e+i},gojs:{"runtime.wasmExit":e=>{e>>>=0;let i=this.mem.getInt32(e+8,!0);this.exited=!0,delete this._inst,delete this._values,delete this._goRefCounts,delete this._ids,delete this._idPool,this.exit(i)},"runtime.wasmWrite":e=>{e>>>=0;let i=s(e+8),f=s(e+16),m=this.mem.getInt32(e+24,!0);fs.writeSync(i,new Uint8Array(this._inst.exports.mem.buffer,f,m))},"runtime.resetMemoryDataView":e=>{e>>>=0,this.mem=new DataView(this._inst.exports.mem.buffer)},"runtime.nanotime1":e=>{e>>>=0,o(e+8,(h+performance.now())*1e6)},"runtime.walltime":e=>{e>>>=0;let i=new Date().getTime();o(e+8,i/1e3),this.mem.setInt32(e+16,i%1e3*1e6,!0)},"runtime.scheduleTimeoutEvent":e=>{e>>>=0;let i=this._nextCallbackTimeoutID;this._nextCallbackTimeoutID++,this._scheduledTimeouts.set(i,setTimeout(()=>{for(this._resume();this._scheduledTimeouts.has(i);)console.warn("scheduleTimeoutEvent: missed timeout event"),this._resume()},s(e+8))),this.mem.setInt32(e+16,i,!0)},"runtime.clearTimeoutEvent":e=>{e>>>=0;let i=this.mem.getInt32(e+8,!0);clearTimeout(this._scheduledTimeouts.get(i)),this._scheduledTimeouts.delete(i)},"runtime.getRandomData":e=>{e>>>=0,crypto.getRandomValues(g(e+8))},"syscall/js.finalizeRef":e=>{e>>>=0;let i=this.mem.getUint32(e+8,!0);if(this._goRefCounts[i]--,this._goRefCounts[i]===0){let f=this._values[i];this._values[i]=null,this._ids.delete(f),this._idPool.push(i)}},"syscall/js.stringVal":e=>{e>>>=0,a(e+24,d(e+8))},"syscall/js.valueGet":e=>{e>>>=0;let i=Reflect.get(n(e+8),d(e+16));e=this._inst.exports.getsp()>>>0,a(e+32,i)},"syscall/js.valueSet":e=>{e>>>=0,Reflect.set(n(e+8),d(e+16),n(e+32))},"syscall/js.valueDelete":e=>{e>>>=0,Reflect.deleteProperty(n(e+8),d(e+16))},"syscall/js.valueIndex":e=>{e>>>=0,a(e+24,Reflect.get(n(e+8),s(e+16)))},"syscall/js.valueSetIndex":e=>{e>>>=0,Reflect.set(n(e+8),s(e+16),n(e+24))},"syscall/js.valueCall":e=>{e>>>=0;try{let i=n(e+8),f=Reflect.get(i,d(e+16)),m=u(e+32),w=Reflect.apply(f,i,m);e=this._inst.exports.getsp()>>>0,a(e+56,w),this.mem.setUint8(e+64,1)}catch(i){e=this._inst.exports.getsp()>>>0,a(e+56,i),this.mem.setUint8(e+64,0)}},"syscall/js.valueInvoke":e=>{e>>>=0;try{let i=n(e+8),f=u(e+16),m=Reflect.apply(i,void 0,f);e=this._inst.exports.getsp()>>>0,a(e+40,m),this.mem.setUint8(e+48,1)}catch(i){e=this._inst.exports.getsp()>>>0,a(e+40,i),this.mem.setUint8(e+48,0)}},"syscall/js.valueNew":e=>{e>>>=0;try{let i=n(e+8),f=u(e+16),m=Reflect.construct(i,f);e=this._inst.exports.getsp()>>>0,a(e+40,m),this.mem.setUint8(e+48,1)}catch(i){e=this._inst.exports.getsp()>>>0,a(e+40,i),this.mem.setUint8(e+48,0)}},"syscall/js.valueLength":e=>{e>>>=0,o(e+16,parseInt(n(e+8).length))},"syscall/js.valuePrepareString":e=>{e>>>=0;let i=c.encode(String(n(e+8)));a(e+16,i),o(e+24,i.length)},"syscall/js.valueLoadString":e=>{e>>>=0;let i=n(e+8);g(e+16).set(i)},"syscall/js.valueInstanceOf":e=>{e>>>=0,this.mem.setUint8(e+24,n(e+8)instanceof n(e+16)?1:0)},"syscall/js.copyBytesToGo":e=>{e>>>=0;let i=g(e+8),f=n(e+32);if(!(f instanceof Uint8Array||f instanceof Uint8ClampedArray)){this.mem.setUint8(e+48,0);return}let m=f.subarray(0,i.length);i.set(m),o(e+40,m.length),this.mem.setUint8(e+48,1)},"syscall/js.copyBytesToJS":e=>{e>>>=0;let i=n(e+8),f=g(e+16);if(!(i instanceof Uint8Array||i instanceof Uint8ClampedArray)){this.mem.setUint8(e+48,0);return}let m=f.subarray(0,i.length);i.set(m),o(e+40,m.length),this.mem.setUint8(e+48,1)},debug:e=>{console.log(e)}}}}async run(o){if(!(o instanceof WebAssembly.Instance))throw new Error("Go.run: WebAssembly.Instance expected");this._inst=o,this.mem=new DataView(this._inst.exports.mem.buffer),this._values=[NaN,0,null,!0,!1,globalThis,this],this._goRefCounts=new Array(this._values.length).fill(1/0),this._ids=new Map([[0,1],[null,2],[!0,3],[!1,4],[globalThis,5],[this,6]]),this._idPool=[],this.exited=!1;let r=4096,s=h=>{let e=r,i=c.encode(h+"\0");return new Uint8Array(this.mem.buffer,r,i.length).set(i),r+=i.length,r%8!==0&&(r+=8-r%8),e},n=this.argv.length,a=[];this.argv.forEach(h=>{a.push(s(h))}),a.push(0),Object.keys(this.env).sort().forEach(h=>{a.push(s(`${h}=${this.env[h]}`))}),a.push(0);let u=r;if(a.forEach(h=>{this.mem.setUint32(r,h,!0),this.mem.setUint32(r+4,0,!0),r+=8}),r>=12288)throw new Error("total length of command line and environment variables exceeds limit");this._inst.exports.run(n,u),this.exited&&this._resolveExitPromise(),await this._exitPromise}_resume(){if(this.exited)throw new Error("Go program has already exited");this._inst.exports.resume(),this.exited&&this._resolveExitPromise()}_makeFuncWrapper(o){let r=this;return function(){let s={id:o,this:this,args:arguments};return r._pendingEvent=s,r._resume(),s.result}}}})();import{getBaseMPCNetworkUrl as y,WalletScheme as b,WalletType as S}from"@getpara/core-sdk";var D=(t,c,l)=>`{"ServerUrl":"${t}", "WalletId": "${c}", "Id":"${l}", "Ids":["USER","CAPSULE"], "Threshold":1}`,p=(t,c,l)=>`{"walletId": "${t}", "id":"${c}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${l}}`;async function L(t,c,l,o){let{data:r}=await t.mpcComputationClient.post("/wallets",{userId:c,walletId:l,protocolId:o});return r}async function O(t,c,l,o,r,s){let{data:n}=await t.mpcComputationClient.post(`/wallets/${l}/messages/sign`,{userId:c,protocolId:o,message:r,signer:s});return n}async function C(t,c,l,o,r,s,n){let{data:a}=await t.mpcComputationClient.post(`/wallets/${l}/transactions/send`,{userId:c,protocolId:o,transaction:r,signer:s,chainId:n});return a}async function T(t,c){let{walletId:l,protocolId:o}=await t.client.createWallet(c,{scheme:b.ED25519,type:S.SOLANA}),r=y(t.env,!t.disableWebSockets);try{return{signer:await new Promise((n,a)=>globalThis.ed25519CreateAccount(r,l,o,(g,u)=>{g&&a(g),n(u)})),walletId:l}}catch{throw new Error(`error creating account of type SOLANA with userId ${c} and walletId ${l}`)}}async function E(t,c,l){let{walletId:o,protocolId:r}=await t.client.createPregenWallet({pregenIdentifier:c,pregenIdentifierType:l,scheme:b.ED25519,type:S.SOLANA}),s=y(t.env,!t.disableWebSockets);try{return{signer:await new Promise((a,g)=>globalThis.ed25519CreateAccount(s,o,r,(u,d)=>{u&&g(u),a(d)})),walletId:o}}catch{throw new Error(`error creating account of type SOLANA with walletId ${o}`)}}async function I(t,c,l,o,r){let{protocolId:s}=await t.client.preSignMessage(l,o,r,b.ED25519);try{return{signature:await new Promise((a,g)=>globalThis.ed25519Sign(c,s,r,(u,d)=>{u&&g(u),a(d)}))}}catch{throw new Error(`error signing for account of type SOLANA with userId ${l} and walletId ${o}`)}}async function P(t,c,l,o){let{walletId:r,protocolId:s}=await t.client.createWallet(c,{useTwoSigners:!0,scheme:t.useDKLS?b.DKLS:b.CGGMP,type:l,cosmosPrefix:l===S.COSMOS?t.cosmosPrefix:void 0});if(t.offloadMPCComputationURL&&!t.useDKLS)return{signer:(await L(t,c,r,s)).signer,walletId:r};let n=y(t.env,!t.disableWebSockets),a=t.useDKLS?p(r,"USER",t.disableWebSockets):D(n,r,"USER"),g=t.useDKLS?globalThis.dklsCreateAccount:globalThis.createAccountV2;try{return{signer:await new Promise((d,h)=>g(a,n,s,o,()=>{},(e,i)=>{e&&h(e),d(i)})),walletId:r}}catch{throw new Error(`error creating account of type ${l} with userId ${c} and walletId ${r}`)}}async function _(t,c,l,o,r,s){let{walletId:n,protocolId:a}=await t.client.createPregenWallet({pregenIdentifier:l,pregenIdentifierType:o,type:r,cosmosPrefix:r===S.COSMOS?t.cosmosPrefix:void 0}),g=y(t.env,!t.disableWebSockets),u=p(n,"USER",t.disableWebSockets);try{return{signer:await new Promise((h,e)=>globalThis.dklsCreateAccount(u,g,a,s,()=>{},(i,f)=>{i&&e(i),h(f)})),walletId:n}}catch{throw new Error(`error creating account of type ${r} with walletId ${n}`)}}async function k(t,c,l,o,r,s){let{protocolId:n,pendingTransactionId:a}=await t.client.preSignMessage(o,l,r,null,s);if(a)return{pendingTransactionId:a};if(t.offloadMPCComputationURL&&!t.useDKLS)return O(t,o,l,n,r,c);let g=y(t.env,!t.disableWebSockets),u=t.useDKLS?globalThis.dklsSignMessage:globalThis.signMessage,d=JSON.parse(c);!d.disableWebSockets!=!t.disableWebSockets&&(d.disableWebSockets=t.disableWebSockets),c=JSON.stringify(d);try{return await new Promise((h,e)=>u(c,g,r,n,(i,f)=>{i&&e(i),h({signature:f})}))}catch{throw new Error(`error signing for account with userId ${o} and walletId ${l}`)}}async function v(t,c,l,o,r,s){let{data:{protocolId:n,pendingTransactionId:a}}=await t.client.signTransaction(o,l,{transaction:r,chainId:s});if(a)return{pendingTransactionId:a};if(t.offloadMPCComputationURL&&!t.useDKLS)return C(t,o,l,n,r,c,s);let g=y(t.env,!t.disableWebSockets),u=t.useDKLS?globalThis.dklsSendTransaction:globalThis.sendTransaction,d=JSON.parse(c);!d.disableWebSockets!=!t.disableWebSockets&&(d.disableWebSockets=t.disableWebSockets),c=JSON.stringify(d);try{return await new Promise((h,e)=>u(c,g,r,s,n,(i,f)=>{i&&e(i),h({signature:f})}))}catch{throw new Error(`error signing transaction for account with userId ${o} and walletId ${l}`)}}async function U(t,c,l,o,r,s){let{data:{protocolId:n,pendingTransactionId:a}}=await t.client.sendTransaction(o,l,{transaction:r,chainId:s});if(a)return{pendingTransactionId:a};if(t.offloadMPCComputationURL&&!t.useDKLS)return C(t,o,l,n,r,c,s);let g=y(t.env,!t.disableWebSockets),u=t.useDKLS?globalThis.dklsSendTransaction:globalThis.sendTransaction,d=JSON.parse(c);!d.disableWebSockets!=!t.disableWebSockets&&(d.disableWebSockets=t.disableWebSockets),c=JSON.stringify(d);try{return await new Promise((h,e)=>u(c,g,r,s,n,(i,f)=>{i&&e(i),h({signature:f})}))}catch{throw new Error(`error signing transaction to send for account with userId ${o} and walletId ${l}`)}}async function R(t,c,l,o,r,s,n){let{data:{protocolId:a}}=await t.client.refreshKeys(o,l,r,s,n),g=y(t.env,!t.disableWebSockets),u=t.useDKLS?globalThis.dklsRefresh:globalThis.refresh,d=JSON.parse(c);!d.disableWebSockets!=!t.disableWebSockets&&(d.disableWebSockets=t.disableWebSockets),c=JSON.stringify(d);try{return await new Promise((h,e)=>u(c,g,a,(i,f)=>{i&&e(i),h({protocolId:a,signer:f})}))}catch{throw new Error(`error refreshing keys for account with userId ${o} and walletId ${l}`)}}async function W(t,c,l,o){let r=await t.client.getParaShare(o,l);if(!r)return"";try{return await new Promise((s,n)=>globalThis.getPrivateKey(c,r,(a,g)=>{a&&n(a),s(g)}))}catch{throw new Error(`error getting private key for account with userId ${o} and walletId ${l}`)}}import{getPortalBaseURL as M,initClient as K,mpcComputationClient as $,paraVersion as j,WalletType as A}from"@getpara/core-sdk";async function x(t,c){if(typeof self>"u")return;let l=new self.Go,o=c;if(!o){if(process.env.DISABLE_WASM_FETCH==="true")throw new Error("fetching wasm file is disabled");o=await(await fetch(`${M(t)}/static/js/main.wasm`,{mode:"cors"})).arrayBuffer()}let r=await WebAssembly.instantiate(o,l.importObject);l.run(r.instance)}async function G(t,c){let{functionType:l,params:o,returnObject:r}=c;switch(l){case"KEYGEN":{let{userId:s,secretKey:n,type:a=A.EVM}=o;return await P(t,s,a,n)}case"SIGN_TRANSACTION":{let{share:s,walletId:n,userId:a,tx:g,chainId:u}=o;return v(t,s,n,a,g,u)}case"SEND_TRANSACTION":{let{share:s,walletId:n,userId:a,tx:g,chainId:u}=o;return U(t,s,n,a,g,u)}case"SIGN_MESSAGE":{let{share:s,walletId:n,userId:a,message:g,cosmosSignDoc:u}=o;return k(t,s,n,a,g,u)}case"REFRESH":{let{share:s,walletId:n,userId:a,oldPartnerId:g,newPartnerId:u,keyShareProtocolId:d}=o,{protocolId:h,signer:e}=await R(t,s,n,a,g,u,d);return r?{protocolId:h,signer:e}:e}case"PREKEYGEN":{let{email:s,partnerId:n,secretKey:a,type:g=A.EVM}=o,{pregenIdentifier:u,pregenIdentifierType:d}=o;return s!=="null"&&s!=="undefined"&&s!==""&&s!=null&&(u=s,d="EMAIL"),await _(t,n,u,d,g,a)}case"GET_PRIVATE_KEY":{let{share:s,walletId:n,userId:a}=o;return await W(t,s,n,a)}case"ED25519_KEYGEN":{let{userId:s}=o;return T(t,s)}case"ED25519_SIGN":{let{share:s,walletId:n,userId:a,base64Bytes:g}=o;return I(t,s,a,n,g)}case"ED25519_PREKEYGEN":{let{email:s}=o,{pregenIdentifier:n,pregenIdentifierType:a}=o;return s!=="null"&&s!=="undefined"&&s!==""&&s!=null&&(n=s,a="EMAIL"),E(t,n,a)}default:throw new Error(`functionType: ${l} not supported`)}}async function Q(t,c,l){let{env:o,apiKey:r,cosmosPrefix:s="cosmos",offloadMPCComputationURL:n,disableWorkers:a,sessionCookie:g,useDKLS:u,disableWebSockets:d,wasmOverride:h}=t.data;if(!o)return!0;let e={env:o,apiKey:r,cosmosPrefix:s,client:K({env:o,version:j,apiKey:r,useFetchAdapter:l,retrieveSessionCookie:()=>g}),offloadMPCComputationURL:n,mpcComputationClient:n?$.initClient(n,!!a):void 0,useDKLS:u,disableWebSockets:!!d,wasmOverride:h};(!e.offloadMPCComputationURL||e.useDKLS)&&await x(e,h);let i=await G(e,t.data);return c(i),!1}export{Q as handleMessage};
|
|
2
|
+
|
|
3
|
+
// src/wasm/wasm_exec.js
|
|
4
|
+
(() => {
|
|
5
|
+
const enosys = () => {
|
|
6
|
+
const err = new Error("not implemented");
|
|
7
|
+
err.code = "ENOSYS";
|
|
8
|
+
return err;
|
|
9
|
+
};
|
|
10
|
+
if (!globalThis.fs) {
|
|
11
|
+
let outputBuf = "";
|
|
12
|
+
globalThis.fs = {
|
|
13
|
+
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 },
|
|
14
|
+
// unused
|
|
15
|
+
writeSync(fd, buf) {
|
|
16
|
+
outputBuf += decoder.decode(buf);
|
|
17
|
+
const nl = outputBuf.lastIndexOf("\n");
|
|
18
|
+
if (nl != -1) {
|
|
19
|
+
console.log(outputBuf.substring(0, nl));
|
|
20
|
+
outputBuf = outputBuf.substring(nl + 1);
|
|
21
|
+
}
|
|
22
|
+
return buf.length;
|
|
23
|
+
},
|
|
24
|
+
write(fd, buf, offset, length, position, callback) {
|
|
25
|
+
if (offset !== 0 || length !== buf.length || position !== null) {
|
|
26
|
+
callback(enosys());
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const n = this.writeSync(fd, buf);
|
|
30
|
+
callback(null, n);
|
|
31
|
+
},
|
|
32
|
+
chmod(path, mode, callback) {
|
|
33
|
+
callback(enosys());
|
|
34
|
+
},
|
|
35
|
+
chown(path, uid, gid, callback) {
|
|
36
|
+
callback(enosys());
|
|
37
|
+
},
|
|
38
|
+
close(fd, callback) {
|
|
39
|
+
callback(enosys());
|
|
40
|
+
},
|
|
41
|
+
fchmod(fd, mode, callback) {
|
|
42
|
+
callback(enosys());
|
|
43
|
+
},
|
|
44
|
+
fchown(fd, uid, gid, callback) {
|
|
45
|
+
callback(enosys());
|
|
46
|
+
},
|
|
47
|
+
fstat(fd, callback) {
|
|
48
|
+
callback(enosys());
|
|
49
|
+
},
|
|
50
|
+
fsync(fd, callback) {
|
|
51
|
+
callback(null);
|
|
52
|
+
},
|
|
53
|
+
ftruncate(fd, length, callback) {
|
|
54
|
+
callback(enosys());
|
|
55
|
+
},
|
|
56
|
+
lchown(path, uid, gid, callback) {
|
|
57
|
+
callback(enosys());
|
|
58
|
+
},
|
|
59
|
+
link(path, link, callback) {
|
|
60
|
+
callback(enosys());
|
|
61
|
+
},
|
|
62
|
+
lstat(path, callback) {
|
|
63
|
+
callback(enosys());
|
|
64
|
+
},
|
|
65
|
+
mkdir(path, perm, callback) {
|
|
66
|
+
callback(enosys());
|
|
67
|
+
},
|
|
68
|
+
open(path, flags, mode, callback) {
|
|
69
|
+
callback(enosys());
|
|
70
|
+
},
|
|
71
|
+
read(fd, buffer, offset, length, position, callback) {
|
|
72
|
+
callback(enosys());
|
|
73
|
+
},
|
|
74
|
+
readdir(path, callback) {
|
|
75
|
+
callback(enosys());
|
|
76
|
+
},
|
|
77
|
+
readlink(path, callback) {
|
|
78
|
+
callback(enosys());
|
|
79
|
+
},
|
|
80
|
+
rename(from, to, callback) {
|
|
81
|
+
callback(enosys());
|
|
82
|
+
},
|
|
83
|
+
rmdir(path, callback) {
|
|
84
|
+
callback(enosys());
|
|
85
|
+
},
|
|
86
|
+
stat(path, callback) {
|
|
87
|
+
callback(enosys());
|
|
88
|
+
},
|
|
89
|
+
symlink(path, link, callback) {
|
|
90
|
+
callback(enosys());
|
|
91
|
+
},
|
|
92
|
+
truncate(path, length, callback) {
|
|
93
|
+
callback(enosys());
|
|
94
|
+
},
|
|
95
|
+
unlink(path, callback) {
|
|
96
|
+
callback(enosys());
|
|
97
|
+
},
|
|
98
|
+
utimes(path, atime, mtime, callback) {
|
|
99
|
+
callback(enosys());
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (!globalThis.process) {
|
|
104
|
+
globalThis.process = {
|
|
105
|
+
getuid() {
|
|
106
|
+
return -1;
|
|
107
|
+
},
|
|
108
|
+
getgid() {
|
|
109
|
+
return -1;
|
|
110
|
+
},
|
|
111
|
+
geteuid() {
|
|
112
|
+
return -1;
|
|
113
|
+
},
|
|
114
|
+
getegid() {
|
|
115
|
+
return -1;
|
|
116
|
+
},
|
|
117
|
+
getgroups() {
|
|
118
|
+
throw enosys();
|
|
119
|
+
},
|
|
120
|
+
pid: -1,
|
|
121
|
+
ppid: -1,
|
|
122
|
+
umask() {
|
|
123
|
+
throw enosys();
|
|
124
|
+
},
|
|
125
|
+
cwd() {
|
|
126
|
+
throw enosys();
|
|
127
|
+
},
|
|
128
|
+
chdir() {
|
|
129
|
+
throw enosys();
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (!globalThis.crypto) {
|
|
134
|
+
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
|
|
135
|
+
}
|
|
136
|
+
if (!globalThis.performance) {
|
|
137
|
+
globalThis.performance = Date;
|
|
138
|
+
}
|
|
139
|
+
if (!globalThis.TextEncoder) {
|
|
140
|
+
throw new Error("globalThis.TextEncoder is not available, polyfill required");
|
|
141
|
+
}
|
|
142
|
+
if (!globalThis.TextDecoder) {
|
|
143
|
+
throw new Error("globalThis.TextDecoder is not available, polyfill required");
|
|
144
|
+
}
|
|
145
|
+
const encoder = new TextEncoder("utf-8");
|
|
146
|
+
const decoder = new TextDecoder("utf-8");
|
|
147
|
+
globalThis.Go = class {
|
|
148
|
+
constructor() {
|
|
149
|
+
this.argv = ["js"];
|
|
150
|
+
this.env = {};
|
|
151
|
+
this.exit = (code) => {
|
|
152
|
+
if (code !== 0) {
|
|
153
|
+
console.warn("exit code:", code);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
this._exitPromise = new Promise((resolve) => {
|
|
157
|
+
this._resolveExitPromise = resolve;
|
|
158
|
+
});
|
|
159
|
+
this._pendingEvent = null;
|
|
160
|
+
this._scheduledTimeouts = /* @__PURE__ */ new Map();
|
|
161
|
+
this._nextCallbackTimeoutID = 1;
|
|
162
|
+
const setInt64 = (addr, v) => {
|
|
163
|
+
this.mem.setUint32(addr + 0, v, true);
|
|
164
|
+
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
|
165
|
+
};
|
|
166
|
+
const setInt32 = (addr, v) => {
|
|
167
|
+
this.mem.setUint32(addr + 0, v, true);
|
|
168
|
+
};
|
|
169
|
+
const getInt64 = (addr) => {
|
|
170
|
+
const low = this.mem.getUint32(addr + 0, true);
|
|
171
|
+
const high = this.mem.getInt32(addr + 4, true);
|
|
172
|
+
return low + high * 4294967296;
|
|
173
|
+
};
|
|
174
|
+
const loadValue = (addr) => {
|
|
175
|
+
const f = this.mem.getFloat64(addr, true);
|
|
176
|
+
if (f === 0) {
|
|
177
|
+
return void 0;
|
|
178
|
+
}
|
|
179
|
+
if (!isNaN(f)) {
|
|
180
|
+
return f;
|
|
181
|
+
}
|
|
182
|
+
const id = this.mem.getUint32(addr, true);
|
|
183
|
+
return this._values[id];
|
|
184
|
+
};
|
|
185
|
+
const storeValue = (addr, v) => {
|
|
186
|
+
const nanHead = 2146959360;
|
|
187
|
+
if (typeof v === "number" && v !== 0) {
|
|
188
|
+
if (isNaN(v)) {
|
|
189
|
+
this.mem.setUint32(addr + 4, nanHead, true);
|
|
190
|
+
this.mem.setUint32(addr, 0, true);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
this.mem.setFloat64(addr, v, true);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (v === void 0) {
|
|
197
|
+
this.mem.setFloat64(addr, 0, true);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
let id = this._ids.get(v);
|
|
201
|
+
if (id === void 0) {
|
|
202
|
+
id = this._idPool.pop();
|
|
203
|
+
if (id === void 0) {
|
|
204
|
+
id = this._values.length;
|
|
205
|
+
}
|
|
206
|
+
this._values[id] = v;
|
|
207
|
+
this._goRefCounts[id] = 0;
|
|
208
|
+
this._ids.set(v, id);
|
|
209
|
+
}
|
|
210
|
+
this._goRefCounts[id]++;
|
|
211
|
+
let typeFlag = 0;
|
|
212
|
+
switch (typeof v) {
|
|
213
|
+
case "object":
|
|
214
|
+
if (v !== null) {
|
|
215
|
+
typeFlag = 1;
|
|
216
|
+
}
|
|
217
|
+
break;
|
|
218
|
+
case "string":
|
|
219
|
+
typeFlag = 2;
|
|
220
|
+
break;
|
|
221
|
+
case "symbol":
|
|
222
|
+
typeFlag = 3;
|
|
223
|
+
break;
|
|
224
|
+
case "function":
|
|
225
|
+
typeFlag = 4;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
|
|
229
|
+
this.mem.setUint32(addr, id, true);
|
|
230
|
+
};
|
|
231
|
+
const loadSlice = (addr) => {
|
|
232
|
+
const array = getInt64(addr + 0);
|
|
233
|
+
const len = getInt64(addr + 8);
|
|
234
|
+
return new Uint8Array(this._inst.exports.mem.buffer, array, len);
|
|
235
|
+
};
|
|
236
|
+
const loadSliceOfValues = (addr) => {
|
|
237
|
+
const array = getInt64(addr + 0);
|
|
238
|
+
const len = getInt64(addr + 8);
|
|
239
|
+
const a = new Array(len);
|
|
240
|
+
for (let i = 0; i < len; i++) {
|
|
241
|
+
a[i] = loadValue(array + i * 8);
|
|
242
|
+
}
|
|
243
|
+
return a;
|
|
244
|
+
};
|
|
245
|
+
const loadString = (addr) => {
|
|
246
|
+
const saddr = getInt64(addr + 0);
|
|
247
|
+
const len = getInt64(addr + 8);
|
|
248
|
+
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
|
|
249
|
+
};
|
|
250
|
+
const timeOrigin = Date.now() - performance.now();
|
|
251
|
+
this.importObject = {
|
|
252
|
+
_gotest: {
|
|
253
|
+
add: (a, b) => a + b
|
|
254
|
+
},
|
|
255
|
+
gojs: {
|
|
256
|
+
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
|
|
257
|
+
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
|
|
258
|
+
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
|
|
259
|
+
// This changes the SP, thus we have to update the SP used by the imported function.
|
|
260
|
+
// func wasmExit(code int32)
|
|
261
|
+
"runtime.wasmExit": (sp) => {
|
|
262
|
+
sp >>>= 0;
|
|
263
|
+
const code = this.mem.getInt32(sp + 8, true);
|
|
264
|
+
this.exited = true;
|
|
265
|
+
delete this._inst;
|
|
266
|
+
delete this._values;
|
|
267
|
+
delete this._goRefCounts;
|
|
268
|
+
delete this._ids;
|
|
269
|
+
delete this._idPool;
|
|
270
|
+
this.exit(code);
|
|
271
|
+
},
|
|
272
|
+
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
|
|
273
|
+
"runtime.wasmWrite": (sp) => {
|
|
274
|
+
sp >>>= 0;
|
|
275
|
+
const fd = getInt64(sp + 8);
|
|
276
|
+
const p = getInt64(sp + 16);
|
|
277
|
+
const n = this.mem.getInt32(sp + 24, true);
|
|
278
|
+
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
|
|
279
|
+
},
|
|
280
|
+
// func resetMemoryDataView()
|
|
281
|
+
"runtime.resetMemoryDataView": (sp) => {
|
|
282
|
+
sp >>>= 0;
|
|
283
|
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
|
284
|
+
},
|
|
285
|
+
// func nanotime1() int64
|
|
286
|
+
"runtime.nanotime1": (sp) => {
|
|
287
|
+
sp >>>= 0;
|
|
288
|
+
setInt64(sp + 8, (timeOrigin + performance.now()) * 1e6);
|
|
289
|
+
},
|
|
290
|
+
// func walltime() (sec int64, nsec int32)
|
|
291
|
+
"runtime.walltime": (sp) => {
|
|
292
|
+
sp >>>= 0;
|
|
293
|
+
const msec = (/* @__PURE__ */ new Date()).getTime();
|
|
294
|
+
setInt64(sp + 8, msec / 1e3);
|
|
295
|
+
this.mem.setInt32(sp + 16, msec % 1e3 * 1e6, true);
|
|
296
|
+
},
|
|
297
|
+
// func scheduleTimeoutEvent(delay int64) int32
|
|
298
|
+
"runtime.scheduleTimeoutEvent": (sp) => {
|
|
299
|
+
sp >>>= 0;
|
|
300
|
+
const id = this._nextCallbackTimeoutID;
|
|
301
|
+
this._nextCallbackTimeoutID++;
|
|
302
|
+
this._scheduledTimeouts.set(id, setTimeout(
|
|
303
|
+
() => {
|
|
304
|
+
this._resume();
|
|
305
|
+
while (this._scheduledTimeouts.has(id)) {
|
|
306
|
+
console.warn("scheduleTimeoutEvent: missed timeout event");
|
|
307
|
+
this._resume();
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
getInt64(sp + 8)
|
|
311
|
+
));
|
|
312
|
+
this.mem.setInt32(sp + 16, id, true);
|
|
313
|
+
},
|
|
314
|
+
// func clearTimeoutEvent(id int32)
|
|
315
|
+
"runtime.clearTimeoutEvent": (sp) => {
|
|
316
|
+
sp >>>= 0;
|
|
317
|
+
const id = this.mem.getInt32(sp + 8, true);
|
|
318
|
+
clearTimeout(this._scheduledTimeouts.get(id));
|
|
319
|
+
this._scheduledTimeouts.delete(id);
|
|
320
|
+
},
|
|
321
|
+
// func getRandomData(r []byte)
|
|
322
|
+
"runtime.getRandomData": (sp) => {
|
|
323
|
+
sp >>>= 0;
|
|
324
|
+
crypto.getRandomValues(loadSlice(sp + 8));
|
|
325
|
+
},
|
|
326
|
+
// func finalizeRef(v ref)
|
|
327
|
+
"syscall/js.finalizeRef": (sp) => {
|
|
328
|
+
sp >>>= 0;
|
|
329
|
+
const id = this.mem.getUint32(sp + 8, true);
|
|
330
|
+
this._goRefCounts[id]--;
|
|
331
|
+
if (this._goRefCounts[id] === 0) {
|
|
332
|
+
const v = this._values[id];
|
|
333
|
+
this._values[id] = null;
|
|
334
|
+
this._ids.delete(v);
|
|
335
|
+
this._idPool.push(id);
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
// func stringVal(value string) ref
|
|
339
|
+
"syscall/js.stringVal": (sp) => {
|
|
340
|
+
sp >>>= 0;
|
|
341
|
+
storeValue(sp + 24, loadString(sp + 8));
|
|
342
|
+
},
|
|
343
|
+
// func valueGet(v ref, p string) ref
|
|
344
|
+
"syscall/js.valueGet": (sp) => {
|
|
345
|
+
sp >>>= 0;
|
|
346
|
+
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
|
|
347
|
+
sp = this._inst.exports.getsp() >>> 0;
|
|
348
|
+
storeValue(sp + 32, result);
|
|
349
|
+
},
|
|
350
|
+
// func valueSet(v ref, p string, x ref)
|
|
351
|
+
"syscall/js.valueSet": (sp) => {
|
|
352
|
+
sp >>>= 0;
|
|
353
|
+
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
|
354
|
+
},
|
|
355
|
+
// func valueDelete(v ref, p string)
|
|
356
|
+
"syscall/js.valueDelete": (sp) => {
|
|
357
|
+
sp >>>= 0;
|
|
358
|
+
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
|
|
359
|
+
},
|
|
360
|
+
// func valueIndex(v ref, i int) ref
|
|
361
|
+
"syscall/js.valueIndex": (sp) => {
|
|
362
|
+
sp >>>= 0;
|
|
363
|
+
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
|
364
|
+
},
|
|
365
|
+
// valueSetIndex(v ref, i int, x ref)
|
|
366
|
+
"syscall/js.valueSetIndex": (sp) => {
|
|
367
|
+
sp >>>= 0;
|
|
368
|
+
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
|
369
|
+
},
|
|
370
|
+
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
|
371
|
+
"syscall/js.valueCall": (sp) => {
|
|
372
|
+
sp >>>= 0;
|
|
373
|
+
try {
|
|
374
|
+
const v = loadValue(sp + 8);
|
|
375
|
+
const m = Reflect.get(v, loadString(sp + 16));
|
|
376
|
+
const args = loadSliceOfValues(sp + 32);
|
|
377
|
+
const result = Reflect.apply(m, v, args);
|
|
378
|
+
sp = this._inst.exports.getsp() >>> 0;
|
|
379
|
+
storeValue(sp + 56, result);
|
|
380
|
+
this.mem.setUint8(sp + 64, 1);
|
|
381
|
+
} catch (err) {
|
|
382
|
+
sp = this._inst.exports.getsp() >>> 0;
|
|
383
|
+
storeValue(sp + 56, err);
|
|
384
|
+
this.mem.setUint8(sp + 64, 0);
|
|
385
|
+
}
|
|
386
|
+
},
|
|
387
|
+
// func valueInvoke(v ref, args []ref) (ref, bool)
|
|
388
|
+
"syscall/js.valueInvoke": (sp) => {
|
|
389
|
+
sp >>>= 0;
|
|
390
|
+
try {
|
|
391
|
+
const v = loadValue(sp + 8);
|
|
392
|
+
const args = loadSliceOfValues(sp + 16);
|
|
393
|
+
const result = Reflect.apply(v, void 0, args);
|
|
394
|
+
sp = this._inst.exports.getsp() >>> 0;
|
|
395
|
+
storeValue(sp + 40, result);
|
|
396
|
+
this.mem.setUint8(sp + 48, 1);
|
|
397
|
+
} catch (err) {
|
|
398
|
+
sp = this._inst.exports.getsp() >>> 0;
|
|
399
|
+
storeValue(sp + 40, err);
|
|
400
|
+
this.mem.setUint8(sp + 48, 0);
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
// func valueNew(v ref, args []ref) (ref, bool)
|
|
404
|
+
"syscall/js.valueNew": (sp) => {
|
|
405
|
+
sp >>>= 0;
|
|
406
|
+
try {
|
|
407
|
+
const v = loadValue(sp + 8);
|
|
408
|
+
const args = loadSliceOfValues(sp + 16);
|
|
409
|
+
const result = Reflect.construct(v, args);
|
|
410
|
+
sp = this._inst.exports.getsp() >>> 0;
|
|
411
|
+
storeValue(sp + 40, result);
|
|
412
|
+
this.mem.setUint8(sp + 48, 1);
|
|
413
|
+
} catch (err) {
|
|
414
|
+
sp = this._inst.exports.getsp() >>> 0;
|
|
415
|
+
storeValue(sp + 40, err);
|
|
416
|
+
this.mem.setUint8(sp + 48, 0);
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
// func valueLength(v ref) int
|
|
420
|
+
"syscall/js.valueLength": (sp) => {
|
|
421
|
+
sp >>>= 0;
|
|
422
|
+
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
|
423
|
+
},
|
|
424
|
+
// valuePrepareString(v ref) (ref, int)
|
|
425
|
+
"syscall/js.valuePrepareString": (sp) => {
|
|
426
|
+
sp >>>= 0;
|
|
427
|
+
const str = encoder.encode(String(loadValue(sp + 8)));
|
|
428
|
+
storeValue(sp + 16, str);
|
|
429
|
+
setInt64(sp + 24, str.length);
|
|
430
|
+
},
|
|
431
|
+
// valueLoadString(v ref, b []byte)
|
|
432
|
+
"syscall/js.valueLoadString": (sp) => {
|
|
433
|
+
sp >>>= 0;
|
|
434
|
+
const str = loadValue(sp + 8);
|
|
435
|
+
loadSlice(sp + 16).set(str);
|
|
436
|
+
},
|
|
437
|
+
// func valueInstanceOf(v ref, t ref) bool
|
|
438
|
+
"syscall/js.valueInstanceOf": (sp) => {
|
|
439
|
+
sp >>>= 0;
|
|
440
|
+
this.mem.setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0);
|
|
441
|
+
},
|
|
442
|
+
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
|
443
|
+
"syscall/js.copyBytesToGo": (sp) => {
|
|
444
|
+
sp >>>= 0;
|
|
445
|
+
const dst = loadSlice(sp + 8);
|
|
446
|
+
const src = loadValue(sp + 32);
|
|
447
|
+
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
|
448
|
+
this.mem.setUint8(sp + 48, 0);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const toCopy = src.subarray(0, dst.length);
|
|
452
|
+
dst.set(toCopy);
|
|
453
|
+
setInt64(sp + 40, toCopy.length);
|
|
454
|
+
this.mem.setUint8(sp + 48, 1);
|
|
455
|
+
},
|
|
456
|
+
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
|
457
|
+
"syscall/js.copyBytesToJS": (sp) => {
|
|
458
|
+
sp >>>= 0;
|
|
459
|
+
const dst = loadValue(sp + 8);
|
|
460
|
+
const src = loadSlice(sp + 16);
|
|
461
|
+
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
|
462
|
+
this.mem.setUint8(sp + 48, 0);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const toCopy = src.subarray(0, dst.length);
|
|
466
|
+
dst.set(toCopy);
|
|
467
|
+
setInt64(sp + 40, toCopy.length);
|
|
468
|
+
this.mem.setUint8(sp + 48, 1);
|
|
469
|
+
},
|
|
470
|
+
"debug": (value) => {
|
|
471
|
+
console.log(value);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
async run(instance) {
|
|
477
|
+
if (!(instance instanceof WebAssembly.Instance)) {
|
|
478
|
+
throw new Error("Go.run: WebAssembly.Instance expected");
|
|
479
|
+
}
|
|
480
|
+
this._inst = instance;
|
|
481
|
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
|
482
|
+
this._values = [
|
|
483
|
+
// JS values that Go currently has references to, indexed by reference id
|
|
484
|
+
NaN,
|
|
485
|
+
0,
|
|
486
|
+
null,
|
|
487
|
+
true,
|
|
488
|
+
false,
|
|
489
|
+
globalThis,
|
|
490
|
+
this
|
|
491
|
+
];
|
|
492
|
+
this._goRefCounts = new Array(this._values.length).fill(Infinity);
|
|
493
|
+
this._ids = /* @__PURE__ */ new Map([
|
|
494
|
+
// mapping from JS values to reference ids
|
|
495
|
+
[0, 1],
|
|
496
|
+
[null, 2],
|
|
497
|
+
[true, 3],
|
|
498
|
+
[false, 4],
|
|
499
|
+
[globalThis, 5],
|
|
500
|
+
[this, 6]
|
|
501
|
+
]);
|
|
502
|
+
this._idPool = [];
|
|
503
|
+
this.exited = false;
|
|
504
|
+
let offset = 4096;
|
|
505
|
+
const strPtr = (str) => {
|
|
506
|
+
const ptr = offset;
|
|
507
|
+
const bytes = encoder.encode(str + "\0");
|
|
508
|
+
new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
|
|
509
|
+
offset += bytes.length;
|
|
510
|
+
if (offset % 8 !== 0) {
|
|
511
|
+
offset += 8 - offset % 8;
|
|
512
|
+
}
|
|
513
|
+
return ptr;
|
|
514
|
+
};
|
|
515
|
+
const argc = this.argv.length;
|
|
516
|
+
const argvPtrs = [];
|
|
517
|
+
this.argv.forEach((arg) => {
|
|
518
|
+
argvPtrs.push(strPtr(arg));
|
|
519
|
+
});
|
|
520
|
+
argvPtrs.push(0);
|
|
521
|
+
const keys = Object.keys(this.env).sort();
|
|
522
|
+
keys.forEach((key) => {
|
|
523
|
+
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
|
|
524
|
+
});
|
|
525
|
+
argvPtrs.push(0);
|
|
526
|
+
const argv = offset;
|
|
527
|
+
argvPtrs.forEach((ptr) => {
|
|
528
|
+
this.mem.setUint32(offset, ptr, true);
|
|
529
|
+
this.mem.setUint32(offset + 4, 0, true);
|
|
530
|
+
offset += 8;
|
|
531
|
+
});
|
|
532
|
+
const wasmMinDataAddr = 4096 + 8192;
|
|
533
|
+
if (offset >= wasmMinDataAddr) {
|
|
534
|
+
throw new Error("total length of command line and environment variables exceeds limit");
|
|
535
|
+
}
|
|
536
|
+
this._inst.exports.run(argc, argv);
|
|
537
|
+
if (this.exited) {
|
|
538
|
+
this._resolveExitPromise();
|
|
539
|
+
}
|
|
540
|
+
await this._exitPromise;
|
|
541
|
+
}
|
|
542
|
+
_resume() {
|
|
543
|
+
if (this.exited) {
|
|
544
|
+
throw new Error("Go program has already exited");
|
|
545
|
+
}
|
|
546
|
+
this._inst.exports.resume();
|
|
547
|
+
if (this.exited) {
|
|
548
|
+
this._resolveExitPromise();
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
_makeFuncWrapper(id) {
|
|
552
|
+
const go = this;
|
|
553
|
+
return function() {
|
|
554
|
+
const event = { id, this: this, args: arguments };
|
|
555
|
+
go._pendingEvent = event;
|
|
556
|
+
go._resume();
|
|
557
|
+
return event.result;
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
})();
|
|
562
|
+
|
|
563
|
+
// src/workers/walletUtils.ts
|
|
564
|
+
import { getBaseMPCNetworkUrl, WalletScheme, WalletType } from "@getpara/core-sdk";
|
|
565
|
+
var configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
|
|
566
|
+
var configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
|
|
567
|
+
async function keygenRequest(ctx, userId, walletId, protocolId) {
|
|
568
|
+
const { data } = await ctx.mpcComputationClient.post("/wallets", {
|
|
569
|
+
userId,
|
|
570
|
+
walletId,
|
|
571
|
+
protocolId
|
|
572
|
+
});
|
|
573
|
+
return data;
|
|
574
|
+
}
|
|
575
|
+
async function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
|
|
576
|
+
const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
|
|
577
|
+
userId,
|
|
578
|
+
protocolId,
|
|
579
|
+
message,
|
|
580
|
+
signer
|
|
581
|
+
});
|
|
582
|
+
return data;
|
|
583
|
+
}
|
|
584
|
+
async function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
|
|
585
|
+
const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
|
|
586
|
+
userId,
|
|
587
|
+
protocolId,
|
|
588
|
+
transaction,
|
|
589
|
+
signer,
|
|
590
|
+
chainId
|
|
591
|
+
});
|
|
592
|
+
return data;
|
|
593
|
+
}
|
|
594
|
+
async function ed25519Keygen(ctx, userId) {
|
|
595
|
+
const { walletId, protocolId } = await ctx.client.createWallet(userId, {
|
|
596
|
+
scheme: WalletScheme.ED25519,
|
|
597
|
+
type: WalletType.SOLANA
|
|
598
|
+
});
|
|
599
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
600
|
+
try {
|
|
601
|
+
const newSigner = await new Promise(
|
|
602
|
+
(resolve, reject) => globalThis.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
603
|
+
if (err) {
|
|
604
|
+
reject(err);
|
|
605
|
+
}
|
|
606
|
+
resolve(result);
|
|
607
|
+
})
|
|
608
|
+
);
|
|
609
|
+
return { signer: newSigner, walletId };
|
|
610
|
+
} catch (e) {
|
|
611
|
+
throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
async function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
|
|
615
|
+
const { walletId, protocolId } = await ctx.client.createPregenWallet({
|
|
616
|
+
pregenIdentifier,
|
|
617
|
+
pregenIdentifierType,
|
|
618
|
+
scheme: WalletScheme.ED25519,
|
|
619
|
+
type: WalletType.SOLANA
|
|
620
|
+
});
|
|
621
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
622
|
+
try {
|
|
623
|
+
const newSigner = await new Promise(
|
|
624
|
+
(resolve, reject) => globalThis.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
625
|
+
if (err) {
|
|
626
|
+
reject(err);
|
|
627
|
+
}
|
|
628
|
+
resolve(result);
|
|
629
|
+
})
|
|
630
|
+
);
|
|
631
|
+
return { signer: newSigner, walletId };
|
|
632
|
+
} catch (e) {
|
|
633
|
+
throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
async function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
|
|
637
|
+
const { protocolId } = await ctx.client.preSignMessage(userId, walletId, base64Bytes, WalletScheme.ED25519);
|
|
638
|
+
try {
|
|
639
|
+
const base64Sig = await new Promise(
|
|
640
|
+
(resolve, reject) => globalThis.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
|
|
641
|
+
if (err) {
|
|
642
|
+
reject(err);
|
|
643
|
+
}
|
|
644
|
+
resolve(result);
|
|
645
|
+
})
|
|
646
|
+
);
|
|
647
|
+
return { signature: base64Sig };
|
|
648
|
+
} catch (e) {
|
|
649
|
+
throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
async function keygen(ctx, userId, type, secretKey) {
|
|
653
|
+
const { walletId, protocolId } = await ctx.client.createWallet(userId, {
|
|
654
|
+
useTwoSigners: true,
|
|
655
|
+
scheme: ctx.useDKLS ? WalletScheme.DKLS : WalletScheme.CGGMP,
|
|
656
|
+
type,
|
|
657
|
+
cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
|
|
658
|
+
});
|
|
659
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
660
|
+
return {
|
|
661
|
+
signer: (await keygenRequest(ctx, userId, walletId, protocolId)).signer,
|
|
662
|
+
walletId
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
666
|
+
const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
|
|
667
|
+
const createAccountFn = ctx.useDKLS ? globalThis.dklsCreateAccount : globalThis.createAccountV2;
|
|
668
|
+
try {
|
|
669
|
+
const newSigner = await new Promise(
|
|
670
|
+
(resolve, reject) => createAccountFn(
|
|
671
|
+
signerConfigUser,
|
|
672
|
+
serverUrl,
|
|
673
|
+
protocolId,
|
|
674
|
+
secretKey,
|
|
675
|
+
() => {
|
|
676
|
+
},
|
|
677
|
+
// no-op for deprecated callback to update progress percentage
|
|
678
|
+
(err, result) => {
|
|
679
|
+
if (err) {
|
|
680
|
+
reject(err);
|
|
681
|
+
}
|
|
682
|
+
resolve(result);
|
|
683
|
+
}
|
|
684
|
+
)
|
|
685
|
+
);
|
|
686
|
+
return { signer: newSigner, walletId };
|
|
687
|
+
} catch (e) {
|
|
688
|
+
throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
async function preKeygen(ctx, _partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
|
|
692
|
+
const { walletId, protocolId } = await ctx.client.createPregenWallet({
|
|
693
|
+
pregenIdentifier,
|
|
694
|
+
pregenIdentifierType,
|
|
695
|
+
type,
|
|
696
|
+
cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
|
|
697
|
+
});
|
|
698
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
699
|
+
const signerConfigUser = configDKLSBase(walletId, "USER", ctx.disableWebSockets);
|
|
700
|
+
try {
|
|
701
|
+
const newSigner = await new Promise(
|
|
702
|
+
(resolve, reject) => globalThis.dklsCreateAccount(
|
|
703
|
+
signerConfigUser,
|
|
704
|
+
serverUrl,
|
|
705
|
+
protocolId,
|
|
706
|
+
secretKey,
|
|
707
|
+
() => {
|
|
708
|
+
},
|
|
709
|
+
// no-op for deprecated callback to update progress percentage
|
|
710
|
+
(err, result) => {
|
|
711
|
+
if (err) {
|
|
712
|
+
reject(err);
|
|
713
|
+
}
|
|
714
|
+
resolve(result);
|
|
715
|
+
}
|
|
716
|
+
)
|
|
717
|
+
);
|
|
718
|
+
return { signer: newSigner, walletId };
|
|
719
|
+
} catch (e) {
|
|
720
|
+
throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
async function signMessage(ctx, share, walletId, userId, message, cosmosSignDoc) {
|
|
724
|
+
const { protocolId, pendingTransactionId } = await ctx.client.preSignMessage(
|
|
725
|
+
userId,
|
|
726
|
+
walletId,
|
|
727
|
+
message,
|
|
728
|
+
null,
|
|
729
|
+
cosmosSignDoc
|
|
730
|
+
);
|
|
731
|
+
if (pendingTransactionId) {
|
|
732
|
+
return { pendingTransactionId };
|
|
733
|
+
}
|
|
734
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
735
|
+
return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
|
|
736
|
+
}
|
|
737
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
738
|
+
const signMessageFn = ctx.useDKLS ? globalThis.dklsSignMessage : globalThis.signMessage;
|
|
739
|
+
const parsedShare = JSON.parse(share);
|
|
740
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
741
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
742
|
+
}
|
|
743
|
+
share = JSON.stringify(parsedShare);
|
|
744
|
+
try {
|
|
745
|
+
return await new Promise(
|
|
746
|
+
(resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
|
|
747
|
+
if (err) {
|
|
748
|
+
reject(err);
|
|
749
|
+
}
|
|
750
|
+
resolve({ signature: result });
|
|
751
|
+
})
|
|
752
|
+
);
|
|
753
|
+
} catch (e) {
|
|
754
|
+
throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
async function signTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
758
|
+
const {
|
|
759
|
+
data: { protocolId, pendingTransactionId }
|
|
760
|
+
} = await ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
|
|
761
|
+
if (pendingTransactionId) {
|
|
762
|
+
return { pendingTransactionId };
|
|
763
|
+
}
|
|
764
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
765
|
+
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
766
|
+
}
|
|
767
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
768
|
+
const signTransactionFn = ctx.useDKLS ? globalThis.dklsSendTransaction : globalThis.sendTransaction;
|
|
769
|
+
const parsedShare = JSON.parse(share);
|
|
770
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
771
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
772
|
+
}
|
|
773
|
+
share = JSON.stringify(parsedShare);
|
|
774
|
+
try {
|
|
775
|
+
return await new Promise(
|
|
776
|
+
(resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
777
|
+
if (err) {
|
|
778
|
+
reject(err);
|
|
779
|
+
}
|
|
780
|
+
resolve({ signature: result });
|
|
781
|
+
})
|
|
782
|
+
);
|
|
783
|
+
} catch (e) {
|
|
784
|
+
throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
async function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
788
|
+
const {
|
|
789
|
+
data: { protocolId, pendingTransactionId }
|
|
790
|
+
} = await ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
|
|
791
|
+
if (pendingTransactionId) {
|
|
792
|
+
return { pendingTransactionId };
|
|
793
|
+
}
|
|
794
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
795
|
+
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
796
|
+
}
|
|
797
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
798
|
+
const sendTransactionFn = ctx.useDKLS ? globalThis.dklsSendTransaction : globalThis.sendTransaction;
|
|
799
|
+
const parsedShare = JSON.parse(share);
|
|
800
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
801
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
802
|
+
}
|
|
803
|
+
share = JSON.stringify(parsedShare);
|
|
804
|
+
try {
|
|
805
|
+
return await new Promise(
|
|
806
|
+
(resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
807
|
+
if (err) {
|
|
808
|
+
reject(err);
|
|
809
|
+
}
|
|
810
|
+
resolve({ signature: result });
|
|
811
|
+
})
|
|
812
|
+
);
|
|
813
|
+
} catch (e) {
|
|
814
|
+
throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
async function refresh(ctx, share, walletId, userId, oldPartnerId, newPartnerId, keyShareProtocolId) {
|
|
818
|
+
const {
|
|
819
|
+
data: { protocolId }
|
|
820
|
+
} = await ctx.client.refreshKeys(userId, walletId, oldPartnerId, newPartnerId, keyShareProtocolId);
|
|
821
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
822
|
+
const refreshFn = ctx.useDKLS ? globalThis.dklsRefresh : globalThis.refresh;
|
|
823
|
+
const parsedShare = JSON.parse(share);
|
|
824
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
825
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
826
|
+
}
|
|
827
|
+
share = JSON.stringify(parsedShare);
|
|
828
|
+
try {
|
|
829
|
+
return await new Promise(
|
|
830
|
+
(resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
|
|
831
|
+
if (err) {
|
|
832
|
+
reject(err);
|
|
833
|
+
}
|
|
834
|
+
resolve({ protocolId, signer: result });
|
|
835
|
+
})
|
|
836
|
+
);
|
|
837
|
+
} catch (e) {
|
|
838
|
+
throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
async function getPrivateKey(ctx, share, walletId, userId) {
|
|
842
|
+
const paraShare = await ctx.client.getParaShare(userId, walletId);
|
|
843
|
+
if (!paraShare) {
|
|
844
|
+
return "";
|
|
845
|
+
}
|
|
846
|
+
try {
|
|
847
|
+
return await new Promise(
|
|
848
|
+
(resolve, reject) => globalThis.getPrivateKey(share, paraShare, (err, result) => {
|
|
849
|
+
if (err) {
|
|
850
|
+
reject(err);
|
|
851
|
+
}
|
|
852
|
+
resolve(result);
|
|
853
|
+
})
|
|
854
|
+
);
|
|
855
|
+
} catch (e) {
|
|
856
|
+
throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// src/workers/worker.ts
|
|
861
|
+
import {
|
|
862
|
+
getPortalBaseURL,
|
|
863
|
+
initClient,
|
|
864
|
+
mpcComputationClient,
|
|
865
|
+
paraVersion,
|
|
866
|
+
WalletType as WalletType2
|
|
867
|
+
} from "@getpara/core-sdk";
|
|
868
|
+
async function loadWasm(ctx, wasmOverride) {
|
|
869
|
+
if (typeof self === "undefined") {
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const goWasm = new self.Go();
|
|
873
|
+
let wasmArrayBuffer = wasmOverride;
|
|
874
|
+
if (!wasmArrayBuffer) {
|
|
875
|
+
if (process.env.DISABLE_WASM_FETCH === "true") {
|
|
876
|
+
throw new Error("fetching wasm file is disabled");
|
|
877
|
+
}
|
|
878
|
+
const fetchedWasm = await fetch(`${getPortalBaseURL(ctx)}/static/js/main.wasm`, { mode: "cors" });
|
|
879
|
+
wasmArrayBuffer = await fetchedWasm.arrayBuffer();
|
|
880
|
+
}
|
|
881
|
+
const newRes = await WebAssembly.instantiate(wasmArrayBuffer, goWasm.importObject);
|
|
882
|
+
goWasm.run(newRes.instance);
|
|
883
|
+
}
|
|
884
|
+
async function executeMessage(ctx, message) {
|
|
885
|
+
const { functionType, params, returnObject } = message;
|
|
886
|
+
switch (functionType) {
|
|
887
|
+
case "KEYGEN": {
|
|
888
|
+
const { userId, secretKey, type = WalletType2.EVM } = params;
|
|
889
|
+
const keygenRes = await keygen(ctx, userId, type, secretKey);
|
|
890
|
+
return keygenRes;
|
|
891
|
+
}
|
|
892
|
+
case "SIGN_TRANSACTION": {
|
|
893
|
+
const { share, walletId, userId, tx, chainId } = params;
|
|
894
|
+
return signTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
895
|
+
}
|
|
896
|
+
case "SEND_TRANSACTION": {
|
|
897
|
+
const { share, walletId, userId, tx, chainId } = params;
|
|
898
|
+
return sendTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
899
|
+
}
|
|
900
|
+
case "SIGN_MESSAGE": {
|
|
901
|
+
const { share, walletId, userId, message: message2, cosmosSignDoc } = params;
|
|
902
|
+
return signMessage(ctx, share, walletId, userId, message2, cosmosSignDoc);
|
|
903
|
+
}
|
|
904
|
+
case "REFRESH": {
|
|
905
|
+
const { share, walletId, userId, oldPartnerId, newPartnerId, keyShareProtocolId } = params;
|
|
906
|
+
const { protocolId, signer } = await refresh(
|
|
907
|
+
ctx,
|
|
908
|
+
share,
|
|
909
|
+
walletId,
|
|
910
|
+
userId,
|
|
911
|
+
oldPartnerId,
|
|
912
|
+
newPartnerId,
|
|
913
|
+
keyShareProtocolId
|
|
914
|
+
);
|
|
915
|
+
return returnObject ? { protocolId, signer } : signer;
|
|
916
|
+
}
|
|
917
|
+
case "PREKEYGEN": {
|
|
918
|
+
const { email, partnerId, secretKey, type = WalletType2.EVM } = params;
|
|
919
|
+
let { pregenIdentifier, pregenIdentifierType } = params;
|
|
920
|
+
if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
|
|
921
|
+
pregenIdentifier = email;
|
|
922
|
+
pregenIdentifierType = "EMAIL";
|
|
923
|
+
}
|
|
924
|
+
const keygenRes = await preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
|
|
925
|
+
return keygenRes;
|
|
926
|
+
}
|
|
927
|
+
case "GET_PRIVATE_KEY": {
|
|
928
|
+
const { share, walletId, userId } = params;
|
|
929
|
+
return await getPrivateKey(ctx, share, walletId, userId);
|
|
930
|
+
}
|
|
931
|
+
case "ED25519_KEYGEN": {
|
|
932
|
+
const { userId } = params;
|
|
933
|
+
return ed25519Keygen(ctx, userId);
|
|
934
|
+
}
|
|
935
|
+
case "ED25519_SIGN": {
|
|
936
|
+
const { share, walletId, userId, base64Bytes } = params;
|
|
937
|
+
return ed25519Sign(ctx, share, userId, walletId, base64Bytes);
|
|
938
|
+
}
|
|
939
|
+
case "ED25519_PREKEYGEN": {
|
|
940
|
+
const { email } = params;
|
|
941
|
+
let { pregenIdentifier, pregenIdentifierType } = params;
|
|
942
|
+
if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
|
|
943
|
+
pregenIdentifier = email;
|
|
944
|
+
pregenIdentifierType = "EMAIL";
|
|
945
|
+
}
|
|
946
|
+
return ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
|
|
947
|
+
}
|
|
948
|
+
default: {
|
|
949
|
+
throw new Error(`functionType: ${functionType} not supported`);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
async function handleMessage(e, postMessage, useFetchAdapter) {
|
|
954
|
+
const {
|
|
955
|
+
env,
|
|
956
|
+
apiKey,
|
|
957
|
+
cosmosPrefix = "cosmos",
|
|
958
|
+
offloadMPCComputationURL,
|
|
959
|
+
disableWorkers,
|
|
960
|
+
sessionCookie,
|
|
961
|
+
useDKLS,
|
|
962
|
+
disableWebSockets,
|
|
963
|
+
wasmOverride
|
|
964
|
+
} = e.data;
|
|
965
|
+
if (!env) {
|
|
966
|
+
return true;
|
|
967
|
+
}
|
|
968
|
+
const ctx = {
|
|
969
|
+
env,
|
|
970
|
+
apiKey,
|
|
971
|
+
cosmosPrefix,
|
|
972
|
+
client: initClient({
|
|
973
|
+
env,
|
|
974
|
+
version: paraVersion,
|
|
975
|
+
apiKey,
|
|
976
|
+
useFetchAdapter,
|
|
977
|
+
retrieveSessionCookie: () => sessionCookie
|
|
978
|
+
}),
|
|
979
|
+
offloadMPCComputationURL,
|
|
980
|
+
mpcComputationClient: offloadMPCComputationURL ? mpcComputationClient.initClient(offloadMPCComputationURL, !!disableWorkers) : void 0,
|
|
981
|
+
useDKLS,
|
|
982
|
+
disableWebSockets: !!disableWebSockets,
|
|
983
|
+
wasmOverride
|
|
984
|
+
};
|
|
985
|
+
if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
|
|
986
|
+
await loadWasm(ctx, wasmOverride);
|
|
987
|
+
}
|
|
988
|
+
const result = await executeMessage(ctx, e.data);
|
|
989
|
+
postMessage(result);
|
|
990
|
+
return false;
|
|
991
|
+
}
|
|
992
|
+
export {
|
|
993
|
+
handleMessage
|
|
994
|
+
};
|