@lifi/perps-sdk-provider-lighter 8.2.1 → 10.0.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/README.md +14 -10
- package/dist/cjs/LighterProvider.d.ts +5 -10
- package/dist/cjs/LighterProvider.d.ts.map +1 -1
- package/dist/cjs/LighterProvider.js +31 -26
- package/dist/cjs/LighterProvider.js.map +1 -1
- package/dist/cjs/constants.d.ts +5 -8
- package/dist/cjs/constants.d.ts.map +1 -1
- package/dist/cjs/constants.js +15 -12
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/index.d.ts +3 -4
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +6 -5
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/signers/LighterSigner.d.ts +5 -7
- package/dist/cjs/signers/LighterSigner.d.ts.map +1 -1
- package/dist/cjs/signers/LighterSigner.js +30 -17
- package/dist/cjs/signers/LighterSigner.js.map +1 -1
- package/dist/cjs/signers/generated/wasmExecRuntime.d.ts +2 -0
- package/dist/cjs/signers/generated/wasmExecRuntime.d.ts.map +1 -0
- package/dist/cjs/signers/generated/wasmExecRuntime.js +569 -0
- package/dist/cjs/signers/generated/wasmExecRuntime.js.map +1 -0
- package/dist/cjs/signers/signActions.d.ts +2 -2
- package/dist/cjs/signers/signActions.d.ts.map +1 -1
- package/dist/cjs/signers/signActions.js +4 -10
- package/dist/cjs/signers/signActions.js.map +1 -1
- package/dist/cjs/signers/wasmBinaryUrl.js +11 -0
- package/dist/cjs/signers/wasmLoader.d.ts +1 -6
- package/dist/cjs/signers/wasmLoader.d.ts.map +1 -1
- package/dist/cjs/signers/wasmLoader.js +15 -69
- package/dist/cjs/signers/wasmLoader.js.map +1 -1
- package/dist/cjs/types/account.d.ts +4 -1
- package/dist/cjs/types/account.d.ts.map +1 -1
- package/dist/cjs/types/action.d.ts +4 -1
- package/dist/cjs/types/action.d.ts.map +1 -1
- package/dist/cjs/types/action.js.map +1 -1
- package/dist/cjs/utils/assetCollateral.d.ts +2 -1
- package/dist/cjs/utils/assetCollateral.d.ts.map +1 -1
- package/dist/cjs/utils/assetCollateral.js +1 -1
- package/dist/cjs/utils/assetCollateral.js.map +1 -1
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +3 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/withdrawableBalances.d.ts +4 -0
- package/dist/cjs/utils/withdrawableBalances.d.ts.map +1 -0
- package/dist/cjs/utils/withdrawableBalances.js +21 -0
- package/dist/cjs/utils/withdrawableBalances.js.map +1 -0
- package/dist/esm/LighterProvider.d.ts +53 -56
- package/dist/esm/LighterProvider.d.ts.map +1 -1
- package/dist/esm/LighterProvider.js +74 -49
- package/dist/esm/LighterProvider.js.map +1 -1
- package/dist/esm/constants.d.ts +25 -36
- package/dist/esm/constants.d.ts.map +1 -1
- package/dist/esm/constants.js +31 -18
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/index.d.ts +6 -7
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/signers/LighterSigner.d.ts +10 -18
- package/dist/esm/signers/LighterSigner.d.ts.map +1 -1
- package/dist/esm/signers/LighterSigner.js +32 -25
- package/dist/esm/signers/LighterSigner.js.map +1 -1
- package/dist/esm/signers/generated/wasmExecRuntime.d.ts +8 -0
- package/dist/esm/signers/generated/wasmExecRuntime.d.ts.map +1 -0
- package/dist/esm/signers/generated/wasmExecRuntime.js +574 -0
- package/dist/esm/signers/generated/wasmExecRuntime.js.map +1 -0
- package/dist/esm/signers/signActions.d.ts +3 -5
- package/dist/esm/signers/signActions.d.ts.map +1 -1
- package/dist/esm/signers/signActions.js +4 -10
- package/dist/esm/signers/signActions.js.map +1 -1
- package/dist/esm/signers/wasmBinaryUrl.js +10 -0
- package/dist/esm/signers/wasmLoader.d.ts +7 -31
- package/dist/esm/signers/wasmLoader.d.ts.map +1 -1
- package/dist/esm/signers/wasmLoader.js +32 -108
- package/dist/esm/signers/wasmLoader.js.map +1 -1
- package/dist/esm/types/account.d.ts +17 -6
- package/dist/esm/types/account.d.ts.map +1 -1
- package/dist/esm/types/action.d.ts +14 -2
- package/dist/esm/types/action.d.ts.map +1 -1
- package/dist/esm/types/action.js.map +1 -1
- package/dist/esm/utils/assetCollateral.d.ts +3 -2
- package/dist/esm/utils/assetCollateral.d.ts.map +1 -1
- package/dist/esm/utils/assetCollateral.js +5 -5
- package/dist/esm/utils/assetCollateral.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/withdrawableBalances.d.ts +12 -0
- package/dist/esm/utils/withdrawableBalances.d.ts.map +1 -0
- package/dist/esm/utils/withdrawableBalances.js +25 -0
- package/dist/esm/utils/withdrawableBalances.js.map +1 -0
- package/dist/types/LighterProvider.d.ts +53 -56
- package/dist/types/LighterProvider.d.ts.map +1 -1
- package/dist/types/constants.d.ts +25 -36
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/index.d.ts +6 -7
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/signers/LighterSigner.d.ts +10 -18
- package/dist/types/signers/LighterSigner.d.ts.map +1 -1
- package/dist/types/signers/generated/wasmExecRuntime.d.ts +8 -0
- package/dist/types/signers/generated/wasmExecRuntime.d.ts.map +1 -0
- package/dist/types/signers/signActions.d.ts +3 -5
- package/dist/types/signers/signActions.d.ts.map +1 -1
- package/dist/types/signers/wasmLoader.d.ts +7 -31
- package/dist/types/signers/wasmLoader.d.ts.map +1 -1
- package/dist/types/types/account.d.ts +17 -6
- package/dist/types/types/account.d.ts.map +1 -1
- package/dist/types/types/action.d.ts +14 -2
- package/dist/types/types/action.d.ts.map +1 -1
- package/dist/types/utils/assetCollateral.d.ts +3 -2
- package/dist/types/utils/assetCollateral.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/withdrawableBalances.d.ts +12 -0
- package/dist/types/utils/withdrawableBalances.d.ts.map +1 -0
- package/package.json +3 -6
- package/src/LighterProvider.ts +112 -101
- package/src/constants.ts +35 -45
- package/src/index.ts +13 -10
- package/src/signers/LighterSigner.ts +47 -51
- package/src/signers/generated/wasmExecRuntime.ts +575 -0
- package/src/signers/signActions.ts +12 -30
- package/src/signers/wasmBinaryUrl.d.ts +7 -0
- package/src/signers/wasmLoader.ts +33 -152
- package/src/types/account.ts +18 -6
- package/src/types/action.ts +14 -2
- package/src/utils/assetCollateral.ts +8 -6
- package/src/utils/index.ts +1 -0
- package/src/utils/withdrawableBalances.ts +31 -0
- package/dist/cjs/signers/index.d.ts +0 -11
- package/dist/cjs/signers/index.d.ts.map +0 -1
- package/dist/cjs/signers/index.js +0 -18
- package/dist/cjs/signers/index.js.map +0 -1
- package/dist/esm/signers/index.d.ts +0 -11
- package/dist/esm/signers/index.d.ts.map +0 -1
- package/dist/esm/signers/index.js +0 -7
- package/dist/esm/signers/index.js.map +0 -1
- package/dist/types/signers/index.d.ts +0 -11
- package/dist/types/signers/index.d.ts.map +0 -1
- package/dist/wasm/wasm_exec.js +0 -561
- package/src/signers/index.ts +0 -35
- package/wasm/lighter-signer.wasm +0 -0
- package/wasm/wasm_exec.js +0 -561
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
// Generated by scripts/generate-wasm-exec.mjs from wasm/wasm_exec.js.
|
|
2
|
+
// Do not edit: regenerate after vendoring a new Go-built signer runtime.
|
|
3
|
+
|
|
4
|
+
const WASM_EXEC_LINES: readonly string[] = [
|
|
5
|
+
"// Copyright 2018 The Go Authors. All rights reserved.",
|
|
6
|
+
"// Use of this source code is governed by a BSD-style",
|
|
7
|
+
"// license that can be found in the LICENSE file.",
|
|
8
|
+
"",
|
|
9
|
+
"\"use strict\";",
|
|
10
|
+
"",
|
|
11
|
+
"(() => {",
|
|
12
|
+
"\tconst enosys = () => {",
|
|
13
|
+
"\t\tconst err = new Error(\"not implemented\");",
|
|
14
|
+
"\t\terr.code = \"ENOSYS\";",
|
|
15
|
+
"\t\treturn err;",
|
|
16
|
+
"\t};",
|
|
17
|
+
"",
|
|
18
|
+
"\tif (!globalThis.fs) {",
|
|
19
|
+
"\t\tlet outputBuf = \"\";",
|
|
20
|
+
"\t\tglobalThis.fs = {",
|
|
21
|
+
"\t\t\tconstants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused",
|
|
22
|
+
"\t\t\twriteSync(fd, buf) {",
|
|
23
|
+
"\t\t\t\toutputBuf += decoder.decode(buf);",
|
|
24
|
+
"\t\t\t\tconst nl = outputBuf.lastIndexOf(\"\\n\");",
|
|
25
|
+
"\t\t\t\tif (nl != -1) {",
|
|
26
|
+
"\t\t\t\t\tconsole.log(outputBuf.substring(0, nl));",
|
|
27
|
+
"\t\t\t\t\toutputBuf = outputBuf.substring(nl + 1);",
|
|
28
|
+
"\t\t\t\t}",
|
|
29
|
+
"\t\t\t\treturn buf.length;",
|
|
30
|
+
"\t\t\t},",
|
|
31
|
+
"\t\t\twrite(fd, buf, offset, length, position, callback) {",
|
|
32
|
+
"\t\t\t\tif (offset !== 0 || length !== buf.length || position !== null) {",
|
|
33
|
+
"\t\t\t\t\tcallback(enosys());",
|
|
34
|
+
"\t\t\t\t\treturn;",
|
|
35
|
+
"\t\t\t\t}",
|
|
36
|
+
"\t\t\t\tconst n = this.writeSync(fd, buf);",
|
|
37
|
+
"\t\t\t\tcallback(null, n);",
|
|
38
|
+
"\t\t\t},",
|
|
39
|
+
"\t\t\tchmod(path, mode, callback) { callback(enosys()); },",
|
|
40
|
+
"\t\t\tchown(path, uid, gid, callback) { callback(enosys()); },",
|
|
41
|
+
"\t\t\tclose(fd, callback) { callback(enosys()); },",
|
|
42
|
+
"\t\t\tfchmod(fd, mode, callback) { callback(enosys()); },",
|
|
43
|
+
"\t\t\tfchown(fd, uid, gid, callback) { callback(enosys()); },",
|
|
44
|
+
"\t\t\tfstat(fd, callback) { callback(enosys()); },",
|
|
45
|
+
"\t\t\tfsync(fd, callback) { callback(null); },",
|
|
46
|
+
"\t\t\tftruncate(fd, length, callback) { callback(enosys()); },",
|
|
47
|
+
"\t\t\tlchown(path, uid, gid, callback) { callback(enosys()); },",
|
|
48
|
+
"\t\t\tlink(path, link, callback) { callback(enosys()); },",
|
|
49
|
+
"\t\t\tlstat(path, callback) { callback(enosys()); },",
|
|
50
|
+
"\t\t\tmkdir(path, perm, callback) { callback(enosys()); },",
|
|
51
|
+
"\t\t\topen(path, flags, mode, callback) { callback(enosys()); },",
|
|
52
|
+
"\t\t\tread(fd, buffer, offset, length, position, callback) { callback(enosys()); },",
|
|
53
|
+
"\t\t\treaddir(path, callback) { callback(enosys()); },",
|
|
54
|
+
"\t\t\treadlink(path, callback) { callback(enosys()); },",
|
|
55
|
+
"\t\t\trename(from, to, callback) { callback(enosys()); },",
|
|
56
|
+
"\t\t\trmdir(path, callback) { callback(enosys()); },",
|
|
57
|
+
"\t\t\tstat(path, callback) { callback(enosys()); },",
|
|
58
|
+
"\t\t\tsymlink(path, link, callback) { callback(enosys()); },",
|
|
59
|
+
"\t\t\ttruncate(path, length, callback) { callback(enosys()); },",
|
|
60
|
+
"\t\t\tunlink(path, callback) { callback(enosys()); },",
|
|
61
|
+
"\t\t\tutimes(path, atime, mtime, callback) { callback(enosys()); },",
|
|
62
|
+
"\t\t};",
|
|
63
|
+
"\t}",
|
|
64
|
+
"",
|
|
65
|
+
"\tif (!globalThis.process) {",
|
|
66
|
+
"\t\tglobalThis.process = {",
|
|
67
|
+
"\t\t\tgetuid() { return -1; },",
|
|
68
|
+
"\t\t\tgetgid() { return -1; },",
|
|
69
|
+
"\t\t\tgeteuid() { return -1; },",
|
|
70
|
+
"\t\t\tgetegid() { return -1; },",
|
|
71
|
+
"\t\t\tgetgroups() { throw enosys(); },",
|
|
72
|
+
"\t\t\tpid: -1,",
|
|
73
|
+
"\t\t\tppid: -1,",
|
|
74
|
+
"\t\t\tumask() { throw enosys(); },",
|
|
75
|
+
"\t\t\tcwd() { throw enosys(); },",
|
|
76
|
+
"\t\t\tchdir() { throw enosys(); },",
|
|
77
|
+
"\t\t}",
|
|
78
|
+
"\t}",
|
|
79
|
+
"",
|
|
80
|
+
"\tif (!globalThis.crypto) {",
|
|
81
|
+
"\t\tthrow new Error(\"globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)\");",
|
|
82
|
+
"\t}",
|
|
83
|
+
"",
|
|
84
|
+
"\tif (!globalThis.performance) {",
|
|
85
|
+
"\t\tthrow new Error(\"globalThis.performance is not available, polyfill required (performance.now only)\");",
|
|
86
|
+
"\t}",
|
|
87
|
+
"",
|
|
88
|
+
"\tif (!globalThis.TextEncoder) {",
|
|
89
|
+
"\t\tthrow new Error(\"globalThis.TextEncoder is not available, polyfill required\");",
|
|
90
|
+
"\t}",
|
|
91
|
+
"",
|
|
92
|
+
"\tif (!globalThis.TextDecoder) {",
|
|
93
|
+
"\t\tthrow new Error(\"globalThis.TextDecoder is not available, polyfill required\");",
|
|
94
|
+
"\t}",
|
|
95
|
+
"",
|
|
96
|
+
"\tconst encoder = new TextEncoder(\"utf-8\");",
|
|
97
|
+
"\tconst decoder = new TextDecoder(\"utf-8\");",
|
|
98
|
+
"",
|
|
99
|
+
"\tglobalThis.Go = class {",
|
|
100
|
+
"\t\tconstructor() {",
|
|
101
|
+
"\t\t\tthis.argv = [\"js\"];",
|
|
102
|
+
"\t\t\tthis.env = {};",
|
|
103
|
+
"\t\t\tthis.exit = (code) => {",
|
|
104
|
+
"\t\t\t\tif (code !== 0) {",
|
|
105
|
+
"\t\t\t\t\tconsole.warn(\"exit code:\", code);",
|
|
106
|
+
"\t\t\t\t}",
|
|
107
|
+
"\t\t\t};",
|
|
108
|
+
"\t\t\tthis._exitPromise = new Promise((resolve) => {",
|
|
109
|
+
"\t\t\t\tthis._resolveExitPromise = resolve;",
|
|
110
|
+
"\t\t\t});",
|
|
111
|
+
"\t\t\tthis._pendingEvent = null;",
|
|
112
|
+
"\t\t\tthis._scheduledTimeouts = new Map();",
|
|
113
|
+
"\t\t\tthis._nextCallbackTimeoutID = 1;",
|
|
114
|
+
"",
|
|
115
|
+
"\t\t\tconst setInt64 = (addr, v) => {",
|
|
116
|
+
"\t\t\t\tthis.mem.setUint32(addr + 0, v, true);",
|
|
117
|
+
"\t\t\t\tthis.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);",
|
|
118
|
+
"\t\t\t}",
|
|
119
|
+
"",
|
|
120
|
+
"\t\t\tconst setInt32 = (addr, v) => {",
|
|
121
|
+
"\t\t\t\tthis.mem.setUint32(addr + 0, v, true);",
|
|
122
|
+
"\t\t\t}",
|
|
123
|
+
"",
|
|
124
|
+
"\t\t\tconst getInt64 = (addr) => {",
|
|
125
|
+
"\t\t\t\tconst low = this.mem.getUint32(addr + 0, true);",
|
|
126
|
+
"\t\t\t\tconst high = this.mem.getInt32(addr + 4, true);",
|
|
127
|
+
"\t\t\t\treturn low + high * 4294967296;",
|
|
128
|
+
"\t\t\t}",
|
|
129
|
+
"",
|
|
130
|
+
"\t\t\tconst loadValue = (addr) => {",
|
|
131
|
+
"\t\t\t\tconst f = this.mem.getFloat64(addr, true);",
|
|
132
|
+
"\t\t\t\tif (f === 0) {",
|
|
133
|
+
"\t\t\t\t\treturn undefined;",
|
|
134
|
+
"\t\t\t\t}",
|
|
135
|
+
"\t\t\t\tif (!isNaN(f)) {",
|
|
136
|
+
"\t\t\t\t\treturn f;",
|
|
137
|
+
"\t\t\t\t}",
|
|
138
|
+
"",
|
|
139
|
+
"\t\t\t\tconst id = this.mem.getUint32(addr, true);",
|
|
140
|
+
"\t\t\t\treturn this._values[id];",
|
|
141
|
+
"\t\t\t}",
|
|
142
|
+
"",
|
|
143
|
+
"\t\t\tconst storeValue = (addr, v) => {",
|
|
144
|
+
"\t\t\t\tconst nanHead = 0x7FF80000;",
|
|
145
|
+
"",
|
|
146
|
+
"\t\t\t\tif (typeof v === \"number\" && v !== 0) {",
|
|
147
|
+
"\t\t\t\t\tif (isNaN(v)) {",
|
|
148
|
+
"\t\t\t\t\t\tthis.mem.setUint32(addr + 4, nanHead, true);",
|
|
149
|
+
"\t\t\t\t\t\tthis.mem.setUint32(addr, 0, true);",
|
|
150
|
+
"\t\t\t\t\t\treturn;",
|
|
151
|
+
"\t\t\t\t\t}",
|
|
152
|
+
"\t\t\t\t\tthis.mem.setFloat64(addr, v, true);",
|
|
153
|
+
"\t\t\t\t\treturn;",
|
|
154
|
+
"\t\t\t\t}",
|
|
155
|
+
"",
|
|
156
|
+
"\t\t\t\tif (v === undefined) {",
|
|
157
|
+
"\t\t\t\t\tthis.mem.setFloat64(addr, 0, true);",
|
|
158
|
+
"\t\t\t\t\treturn;",
|
|
159
|
+
"\t\t\t\t}",
|
|
160
|
+
"",
|
|
161
|
+
"\t\t\t\tlet id = this._ids.get(v);",
|
|
162
|
+
"\t\t\t\tif (id === undefined) {",
|
|
163
|
+
"\t\t\t\t\tid = this._idPool.pop();",
|
|
164
|
+
"\t\t\t\t\tif (id === undefined) {",
|
|
165
|
+
"\t\t\t\t\t\tid = this._values.length;",
|
|
166
|
+
"\t\t\t\t\t}",
|
|
167
|
+
"\t\t\t\t\tthis._values[id] = v;",
|
|
168
|
+
"\t\t\t\t\tthis._goRefCounts[id] = 0;",
|
|
169
|
+
"\t\t\t\t\tthis._ids.set(v, id);",
|
|
170
|
+
"\t\t\t\t}",
|
|
171
|
+
"\t\t\t\tthis._goRefCounts[id]++;",
|
|
172
|
+
"\t\t\t\tlet typeFlag = 0;",
|
|
173
|
+
"\t\t\t\tswitch (typeof v) {",
|
|
174
|
+
"\t\t\t\t\tcase \"object\":",
|
|
175
|
+
"\t\t\t\t\t\tif (v !== null) {",
|
|
176
|
+
"\t\t\t\t\t\t\ttypeFlag = 1;",
|
|
177
|
+
"\t\t\t\t\t\t}",
|
|
178
|
+
"\t\t\t\t\t\tbreak;",
|
|
179
|
+
"\t\t\t\t\tcase \"string\":",
|
|
180
|
+
"\t\t\t\t\t\ttypeFlag = 2;",
|
|
181
|
+
"\t\t\t\t\t\tbreak;",
|
|
182
|
+
"\t\t\t\t\tcase \"symbol\":",
|
|
183
|
+
"\t\t\t\t\t\ttypeFlag = 3;",
|
|
184
|
+
"\t\t\t\t\t\tbreak;",
|
|
185
|
+
"\t\t\t\t\tcase \"function\":",
|
|
186
|
+
"\t\t\t\t\t\ttypeFlag = 4;",
|
|
187
|
+
"\t\t\t\t\t\tbreak;",
|
|
188
|
+
"\t\t\t\t}",
|
|
189
|
+
"\t\t\t\tthis.mem.setUint32(addr + 4, nanHead | typeFlag, true);",
|
|
190
|
+
"\t\t\t\tthis.mem.setUint32(addr, id, true);",
|
|
191
|
+
"\t\t\t}",
|
|
192
|
+
"",
|
|
193
|
+
"\t\t\tconst loadSlice = (addr) => {",
|
|
194
|
+
"\t\t\t\tconst array = getInt64(addr + 0);",
|
|
195
|
+
"\t\t\t\tconst len = getInt64(addr + 8);",
|
|
196
|
+
"\t\t\t\treturn new Uint8Array(this._inst.exports.mem.buffer, array, len);",
|
|
197
|
+
"\t\t\t}",
|
|
198
|
+
"",
|
|
199
|
+
"\t\t\tconst loadSliceOfValues = (addr) => {",
|
|
200
|
+
"\t\t\t\tconst array = getInt64(addr + 0);",
|
|
201
|
+
"\t\t\t\tconst len = getInt64(addr + 8);",
|
|
202
|
+
"\t\t\t\tconst a = new Array(len);",
|
|
203
|
+
"\t\t\t\tfor (let i = 0; i < len; i++) {",
|
|
204
|
+
"\t\t\t\t\ta[i] = loadValue(array + i * 8);",
|
|
205
|
+
"\t\t\t\t}",
|
|
206
|
+
"\t\t\t\treturn a;",
|
|
207
|
+
"\t\t\t}",
|
|
208
|
+
"",
|
|
209
|
+
"\t\t\tconst loadString = (addr) => {",
|
|
210
|
+
"\t\t\t\tconst saddr = getInt64(addr + 0);",
|
|
211
|
+
"\t\t\t\tconst len = getInt64(addr + 8);",
|
|
212
|
+
"\t\t\t\treturn decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));",
|
|
213
|
+
"\t\t\t}",
|
|
214
|
+
"",
|
|
215
|
+
"\t\t\tconst timeOrigin = Date.now() - performance.now();",
|
|
216
|
+
"\t\t\tthis.importObject = {",
|
|
217
|
+
"\t\t\t\t_gotest: {",
|
|
218
|
+
"\t\t\t\t\tadd: (a, b) => a + b,",
|
|
219
|
+
"\t\t\t\t},",
|
|
220
|
+
"\t\t\t\tgojs: {",
|
|
221
|
+
"\t\t\t\t\t// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)",
|
|
222
|
+
"\t\t\t\t\t// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported",
|
|
223
|
+
"\t\t\t\t\t// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).",
|
|
224
|
+
"\t\t\t\t\t// This changes the SP, thus we have to update the SP used by the imported function.",
|
|
225
|
+
"",
|
|
226
|
+
"\t\t\t\t\t// func wasmExit(code int32)",
|
|
227
|
+
"\t\t\t\t\t\"runtime.wasmExit\": (sp) => {",
|
|
228
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
229
|
+
"\t\t\t\t\t\tconst code = this.mem.getInt32(sp + 8, true);",
|
|
230
|
+
"\t\t\t\t\t\tthis.exited = true;",
|
|
231
|
+
"\t\t\t\t\t\tdelete this._inst;",
|
|
232
|
+
"\t\t\t\t\t\tdelete this._values;",
|
|
233
|
+
"\t\t\t\t\t\tdelete this._goRefCounts;",
|
|
234
|
+
"\t\t\t\t\t\tdelete this._ids;",
|
|
235
|
+
"\t\t\t\t\t\tdelete this._idPool;",
|
|
236
|
+
"\t\t\t\t\t\tthis.exit(code);",
|
|
237
|
+
"\t\t\t\t\t},",
|
|
238
|
+
"",
|
|
239
|
+
"\t\t\t\t\t// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)",
|
|
240
|
+
"\t\t\t\t\t\"runtime.wasmWrite\": (sp) => {",
|
|
241
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
242
|
+
"\t\t\t\t\t\tconst fd = getInt64(sp + 8);",
|
|
243
|
+
"\t\t\t\t\t\tconst p = getInt64(sp + 16);",
|
|
244
|
+
"\t\t\t\t\t\tconst n = this.mem.getInt32(sp + 24, true);",
|
|
245
|
+
"\t\t\t\t\t\tfs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));",
|
|
246
|
+
"\t\t\t\t\t},",
|
|
247
|
+
"",
|
|
248
|
+
"\t\t\t\t\t// func resetMemoryDataView()",
|
|
249
|
+
"\t\t\t\t\t\"runtime.resetMemoryDataView\": (sp) => {",
|
|
250
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
251
|
+
"\t\t\t\t\t\tthis.mem = new DataView(this._inst.exports.mem.buffer);",
|
|
252
|
+
"\t\t\t\t\t},",
|
|
253
|
+
"",
|
|
254
|
+
"\t\t\t\t\t// func nanotime1() int64",
|
|
255
|
+
"\t\t\t\t\t\"runtime.nanotime1\": (sp) => {",
|
|
256
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
257
|
+
"\t\t\t\t\t\tsetInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);",
|
|
258
|
+
"\t\t\t\t\t},",
|
|
259
|
+
"",
|
|
260
|
+
"\t\t\t\t\t// func walltime() (sec int64, nsec int32)",
|
|
261
|
+
"\t\t\t\t\t\"runtime.walltime\": (sp) => {",
|
|
262
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
263
|
+
"\t\t\t\t\t\tconst msec = (new Date).getTime();",
|
|
264
|
+
"\t\t\t\t\t\tsetInt64(sp + 8, msec / 1000);",
|
|
265
|
+
"\t\t\t\t\t\tthis.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);",
|
|
266
|
+
"\t\t\t\t\t},",
|
|
267
|
+
"",
|
|
268
|
+
"\t\t\t\t\t// func scheduleTimeoutEvent(delay int64) int32",
|
|
269
|
+
"\t\t\t\t\t\"runtime.scheduleTimeoutEvent\": (sp) => {",
|
|
270
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
271
|
+
"\t\t\t\t\t\tconst id = this._nextCallbackTimeoutID;",
|
|
272
|
+
"\t\t\t\t\t\tthis._nextCallbackTimeoutID++;",
|
|
273
|
+
"\t\t\t\t\t\tthis._scheduledTimeouts.set(id, setTimeout(",
|
|
274
|
+
"\t\t\t\t\t\t\t() => {",
|
|
275
|
+
"\t\t\t\t\t\t\t\tthis._resume();",
|
|
276
|
+
"\t\t\t\t\t\t\t\twhile (this._scheduledTimeouts.has(id)) {",
|
|
277
|
+
"\t\t\t\t\t\t\t\t\t// for some reason Go failed to register the timeout event, log and try again",
|
|
278
|
+
"\t\t\t\t\t\t\t\t\t// (temporary workaround for https://github.com/golang/go/issues/28975)",
|
|
279
|
+
"\t\t\t\t\t\t\t\t\tconsole.warn(\"scheduleTimeoutEvent: missed timeout event\");",
|
|
280
|
+
"\t\t\t\t\t\t\t\t\tthis._resume();",
|
|
281
|
+
"\t\t\t\t\t\t\t\t}",
|
|
282
|
+
"\t\t\t\t\t\t\t},",
|
|
283
|
+
"\t\t\t\t\t\t\tgetInt64(sp + 8),",
|
|
284
|
+
"\t\t\t\t\t\t));",
|
|
285
|
+
"\t\t\t\t\t\tthis.mem.setInt32(sp + 16, id, true);",
|
|
286
|
+
"\t\t\t\t\t},",
|
|
287
|
+
"",
|
|
288
|
+
"\t\t\t\t\t// func clearTimeoutEvent(id int32)",
|
|
289
|
+
"\t\t\t\t\t\"runtime.clearTimeoutEvent\": (sp) => {",
|
|
290
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
291
|
+
"\t\t\t\t\t\tconst id = this.mem.getInt32(sp + 8, true);",
|
|
292
|
+
"\t\t\t\t\t\tclearTimeout(this._scheduledTimeouts.get(id));",
|
|
293
|
+
"\t\t\t\t\t\tthis._scheduledTimeouts.delete(id);",
|
|
294
|
+
"\t\t\t\t\t},",
|
|
295
|
+
"",
|
|
296
|
+
"\t\t\t\t\t// func getRandomData(r []byte)",
|
|
297
|
+
"\t\t\t\t\t\"runtime.getRandomData\": (sp) => {",
|
|
298
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
299
|
+
"\t\t\t\t\t\tcrypto.getRandomValues(loadSlice(sp + 8));",
|
|
300
|
+
"\t\t\t\t\t},",
|
|
301
|
+
"",
|
|
302
|
+
"\t\t\t\t\t// func finalizeRef(v ref)",
|
|
303
|
+
"\t\t\t\t\t\"syscall/js.finalizeRef\": (sp) => {",
|
|
304
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
305
|
+
"\t\t\t\t\t\tconst id = this.mem.getUint32(sp + 8, true);",
|
|
306
|
+
"\t\t\t\t\t\tthis._goRefCounts[id]--;",
|
|
307
|
+
"\t\t\t\t\t\tif (this._goRefCounts[id] === 0) {",
|
|
308
|
+
"\t\t\t\t\t\t\tconst v = this._values[id];",
|
|
309
|
+
"\t\t\t\t\t\t\tthis._values[id] = null;",
|
|
310
|
+
"\t\t\t\t\t\t\tthis._ids.delete(v);",
|
|
311
|
+
"\t\t\t\t\t\t\tthis._idPool.push(id);",
|
|
312
|
+
"\t\t\t\t\t\t}",
|
|
313
|
+
"\t\t\t\t\t},",
|
|
314
|
+
"",
|
|
315
|
+
"\t\t\t\t\t// func stringVal(value string) ref",
|
|
316
|
+
"\t\t\t\t\t\"syscall/js.stringVal\": (sp) => {",
|
|
317
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
318
|
+
"\t\t\t\t\t\tstoreValue(sp + 24, loadString(sp + 8));",
|
|
319
|
+
"\t\t\t\t\t},",
|
|
320
|
+
"",
|
|
321
|
+
"\t\t\t\t\t// func valueGet(v ref, p string) ref",
|
|
322
|
+
"\t\t\t\t\t\"syscall/js.valueGet\": (sp) => {",
|
|
323
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
324
|
+
"\t\t\t\t\t\tconst result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));",
|
|
325
|
+
"\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
|
|
326
|
+
"\t\t\t\t\t\tstoreValue(sp + 32, result);",
|
|
327
|
+
"\t\t\t\t\t},",
|
|
328
|
+
"",
|
|
329
|
+
"\t\t\t\t\t// func valueSet(v ref, p string, x ref)",
|
|
330
|
+
"\t\t\t\t\t\"syscall/js.valueSet\": (sp) => {",
|
|
331
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
332
|
+
"\t\t\t\t\t\tReflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));",
|
|
333
|
+
"\t\t\t\t\t},",
|
|
334
|
+
"",
|
|
335
|
+
"\t\t\t\t\t// func valueDelete(v ref, p string)",
|
|
336
|
+
"\t\t\t\t\t\"syscall/js.valueDelete\": (sp) => {",
|
|
337
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
338
|
+
"\t\t\t\t\t\tReflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));",
|
|
339
|
+
"\t\t\t\t\t},",
|
|
340
|
+
"",
|
|
341
|
+
"\t\t\t\t\t// func valueIndex(v ref, i int) ref",
|
|
342
|
+
"\t\t\t\t\t\"syscall/js.valueIndex\": (sp) => {",
|
|
343
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
344
|
+
"\t\t\t\t\t\tstoreValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));",
|
|
345
|
+
"\t\t\t\t\t},",
|
|
346
|
+
"",
|
|
347
|
+
"\t\t\t\t\t// valueSetIndex(v ref, i int, x ref)",
|
|
348
|
+
"\t\t\t\t\t\"syscall/js.valueSetIndex\": (sp) => {",
|
|
349
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
350
|
+
"\t\t\t\t\t\tReflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));",
|
|
351
|
+
"\t\t\t\t\t},",
|
|
352
|
+
"",
|
|
353
|
+
"\t\t\t\t\t// func valueCall(v ref, m string, args []ref) (ref, bool)",
|
|
354
|
+
"\t\t\t\t\t\"syscall/js.valueCall\": (sp) => {",
|
|
355
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
356
|
+
"\t\t\t\t\t\ttry {",
|
|
357
|
+
"\t\t\t\t\t\t\tconst v = loadValue(sp + 8);",
|
|
358
|
+
"\t\t\t\t\t\t\tconst m = Reflect.get(v, loadString(sp + 16));",
|
|
359
|
+
"\t\t\t\t\t\t\tconst args = loadSliceOfValues(sp + 32);",
|
|
360
|
+
"\t\t\t\t\t\t\tconst result = Reflect.apply(m, v, args);",
|
|
361
|
+
"\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
|
|
362
|
+
"\t\t\t\t\t\t\tstoreValue(sp + 56, result);",
|
|
363
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 64, 1);",
|
|
364
|
+
"\t\t\t\t\t\t} catch (err) {",
|
|
365
|
+
"\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
|
|
366
|
+
"\t\t\t\t\t\t\tstoreValue(sp + 56, err);",
|
|
367
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 64, 0);",
|
|
368
|
+
"\t\t\t\t\t\t}",
|
|
369
|
+
"\t\t\t\t\t},",
|
|
370
|
+
"",
|
|
371
|
+
"\t\t\t\t\t// func valueInvoke(v ref, args []ref) (ref, bool)",
|
|
372
|
+
"\t\t\t\t\t\"syscall/js.valueInvoke\": (sp) => {",
|
|
373
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
374
|
+
"\t\t\t\t\t\ttry {",
|
|
375
|
+
"\t\t\t\t\t\t\tconst v = loadValue(sp + 8);",
|
|
376
|
+
"\t\t\t\t\t\t\tconst args = loadSliceOfValues(sp + 16);",
|
|
377
|
+
"\t\t\t\t\t\t\tconst result = Reflect.apply(v, undefined, args);",
|
|
378
|
+
"\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
|
|
379
|
+
"\t\t\t\t\t\t\tstoreValue(sp + 40, result);",
|
|
380
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
|
|
381
|
+
"\t\t\t\t\t\t} catch (err) {",
|
|
382
|
+
"\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
|
|
383
|
+
"\t\t\t\t\t\t\tstoreValue(sp + 40, err);",
|
|
384
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
|
|
385
|
+
"\t\t\t\t\t\t}",
|
|
386
|
+
"\t\t\t\t\t},",
|
|
387
|
+
"",
|
|
388
|
+
"\t\t\t\t\t// func valueNew(v ref, args []ref) (ref, bool)",
|
|
389
|
+
"\t\t\t\t\t\"syscall/js.valueNew\": (sp) => {",
|
|
390
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
391
|
+
"\t\t\t\t\t\ttry {",
|
|
392
|
+
"\t\t\t\t\t\t\tconst v = loadValue(sp + 8);",
|
|
393
|
+
"\t\t\t\t\t\t\tconst args = loadSliceOfValues(sp + 16);",
|
|
394
|
+
"\t\t\t\t\t\t\tconst result = Reflect.construct(v, args);",
|
|
395
|
+
"\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
|
|
396
|
+
"\t\t\t\t\t\t\tstoreValue(sp + 40, result);",
|
|
397
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
|
|
398
|
+
"\t\t\t\t\t\t} catch (err) {",
|
|
399
|
+
"\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
|
|
400
|
+
"\t\t\t\t\t\t\tstoreValue(sp + 40, err);",
|
|
401
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
|
|
402
|
+
"\t\t\t\t\t\t}",
|
|
403
|
+
"\t\t\t\t\t},",
|
|
404
|
+
"",
|
|
405
|
+
"\t\t\t\t\t// func valueLength(v ref) int",
|
|
406
|
+
"\t\t\t\t\t\"syscall/js.valueLength\": (sp) => {",
|
|
407
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
408
|
+
"\t\t\t\t\t\tsetInt64(sp + 16, parseInt(loadValue(sp + 8).length));",
|
|
409
|
+
"\t\t\t\t\t},",
|
|
410
|
+
"",
|
|
411
|
+
"\t\t\t\t\t// valuePrepareString(v ref) (ref, int)",
|
|
412
|
+
"\t\t\t\t\t\"syscall/js.valuePrepareString\": (sp) => {",
|
|
413
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
414
|
+
"\t\t\t\t\t\tconst str = encoder.encode(String(loadValue(sp + 8)));",
|
|
415
|
+
"\t\t\t\t\t\tstoreValue(sp + 16, str);",
|
|
416
|
+
"\t\t\t\t\t\tsetInt64(sp + 24, str.length);",
|
|
417
|
+
"\t\t\t\t\t},",
|
|
418
|
+
"",
|
|
419
|
+
"\t\t\t\t\t// valueLoadString(v ref, b []byte)",
|
|
420
|
+
"\t\t\t\t\t\"syscall/js.valueLoadString\": (sp) => {",
|
|
421
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
422
|
+
"\t\t\t\t\t\tconst str = loadValue(sp + 8);",
|
|
423
|
+
"\t\t\t\t\t\tloadSlice(sp + 16).set(str);",
|
|
424
|
+
"\t\t\t\t\t},",
|
|
425
|
+
"",
|
|
426
|
+
"\t\t\t\t\t// func valueInstanceOf(v ref, t ref) bool",
|
|
427
|
+
"\t\t\t\t\t\"syscall/js.valueInstanceOf\": (sp) => {",
|
|
428
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
429
|
+
"\t\t\t\t\t\tthis.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);",
|
|
430
|
+
"\t\t\t\t\t},",
|
|
431
|
+
"",
|
|
432
|
+
"\t\t\t\t\t// func copyBytesToGo(dst []byte, src ref) (int, bool)",
|
|
433
|
+
"\t\t\t\t\t\"syscall/js.copyBytesToGo\": (sp) => {",
|
|
434
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
435
|
+
"\t\t\t\t\t\tconst dst = loadSlice(sp + 8);",
|
|
436
|
+
"\t\t\t\t\t\tconst src = loadValue(sp + 32);",
|
|
437
|
+
"\t\t\t\t\t\tif (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {",
|
|
438
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
|
|
439
|
+
"\t\t\t\t\t\t\treturn;",
|
|
440
|
+
"\t\t\t\t\t\t}",
|
|
441
|
+
"\t\t\t\t\t\tconst toCopy = src.subarray(0, dst.length);",
|
|
442
|
+
"\t\t\t\t\t\tdst.set(toCopy);",
|
|
443
|
+
"\t\t\t\t\t\tsetInt64(sp + 40, toCopy.length);",
|
|
444
|
+
"\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
|
|
445
|
+
"\t\t\t\t\t},",
|
|
446
|
+
"",
|
|
447
|
+
"\t\t\t\t\t// func copyBytesToJS(dst ref, src []byte) (int, bool)",
|
|
448
|
+
"\t\t\t\t\t\"syscall/js.copyBytesToJS\": (sp) => {",
|
|
449
|
+
"\t\t\t\t\t\tsp >>>= 0;",
|
|
450
|
+
"\t\t\t\t\t\tconst dst = loadValue(sp + 8);",
|
|
451
|
+
"\t\t\t\t\t\tconst src = loadSlice(sp + 16);",
|
|
452
|
+
"\t\t\t\t\t\tif (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {",
|
|
453
|
+
"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
|
|
454
|
+
"\t\t\t\t\t\t\treturn;",
|
|
455
|
+
"\t\t\t\t\t\t}",
|
|
456
|
+
"\t\t\t\t\t\tconst toCopy = src.subarray(0, dst.length);",
|
|
457
|
+
"\t\t\t\t\t\tdst.set(toCopy);",
|
|
458
|
+
"\t\t\t\t\t\tsetInt64(sp + 40, toCopy.length);",
|
|
459
|
+
"\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
|
|
460
|
+
"\t\t\t\t\t},",
|
|
461
|
+
"",
|
|
462
|
+
"\t\t\t\t\t\"debug\": (value) => {",
|
|
463
|
+
"\t\t\t\t\t\tconsole.log(value);",
|
|
464
|
+
"\t\t\t\t\t},",
|
|
465
|
+
"\t\t\t\t}",
|
|
466
|
+
"\t\t\t};",
|
|
467
|
+
"\t\t}",
|
|
468
|
+
"",
|
|
469
|
+
"\t\tasync run(instance) {",
|
|
470
|
+
"\t\t\tif (!(instance instanceof WebAssembly.Instance)) {",
|
|
471
|
+
"\t\t\t\tthrow new Error(\"Go.run: WebAssembly.Instance expected\");",
|
|
472
|
+
"\t\t\t}",
|
|
473
|
+
"\t\t\tthis._inst = instance;",
|
|
474
|
+
"\t\t\tthis.mem = new DataView(this._inst.exports.mem.buffer);",
|
|
475
|
+
"\t\t\tthis._values = [ // JS values that Go currently has references to, indexed by reference id",
|
|
476
|
+
"\t\t\t\tNaN,",
|
|
477
|
+
"\t\t\t\t0,",
|
|
478
|
+
"\t\t\t\tnull,",
|
|
479
|
+
"\t\t\t\ttrue,",
|
|
480
|
+
"\t\t\t\tfalse,",
|
|
481
|
+
"\t\t\t\tglobalThis,",
|
|
482
|
+
"\t\t\t\tthis,",
|
|
483
|
+
"\t\t\t];",
|
|
484
|
+
"\t\t\tthis._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id",
|
|
485
|
+
"\t\t\tthis._ids = new Map([ // mapping from JS values to reference ids",
|
|
486
|
+
"\t\t\t\t[0, 1],",
|
|
487
|
+
"\t\t\t\t[null, 2],",
|
|
488
|
+
"\t\t\t\t[true, 3],",
|
|
489
|
+
"\t\t\t\t[false, 4],",
|
|
490
|
+
"\t\t\t\t[globalThis, 5],",
|
|
491
|
+
"\t\t\t\t[this, 6],",
|
|
492
|
+
"\t\t\t]);",
|
|
493
|
+
"\t\t\tthis._idPool = []; // unused ids that have been garbage collected",
|
|
494
|
+
"\t\t\tthis.exited = false; // whether the Go program has exited",
|
|
495
|
+
"",
|
|
496
|
+
"\t\t\t// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.",
|
|
497
|
+
"\t\t\tlet offset = 4096;",
|
|
498
|
+
"",
|
|
499
|
+
"\t\t\tconst strPtr = (str) => {",
|
|
500
|
+
"\t\t\t\tconst ptr = offset;",
|
|
501
|
+
"\t\t\t\tconst bytes = encoder.encode(str + \"\\0\");",
|
|
502
|
+
"\t\t\t\tnew Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);",
|
|
503
|
+
"\t\t\t\toffset += bytes.length;",
|
|
504
|
+
"\t\t\t\tif (offset % 8 !== 0) {",
|
|
505
|
+
"\t\t\t\t\toffset += 8 - (offset % 8);",
|
|
506
|
+
"\t\t\t\t}",
|
|
507
|
+
"\t\t\t\treturn ptr;",
|
|
508
|
+
"\t\t\t};",
|
|
509
|
+
"",
|
|
510
|
+
"\t\t\tconst argc = this.argv.length;",
|
|
511
|
+
"",
|
|
512
|
+
"\t\t\tconst argvPtrs = [];",
|
|
513
|
+
"\t\t\tthis.argv.forEach((arg) => {",
|
|
514
|
+
"\t\t\t\targvPtrs.push(strPtr(arg));",
|
|
515
|
+
"\t\t\t});",
|
|
516
|
+
"\t\t\targvPtrs.push(0);",
|
|
517
|
+
"",
|
|
518
|
+
"\t\t\tconst keys = Object.keys(this.env).sort();",
|
|
519
|
+
"\t\t\tkeys.forEach((key) => {",
|
|
520
|
+
"\t\t\t\targvPtrs.push(strPtr(`${key}=${this.env[key]}`));",
|
|
521
|
+
"\t\t\t});",
|
|
522
|
+
"\t\t\targvPtrs.push(0);",
|
|
523
|
+
"",
|
|
524
|
+
"\t\t\tconst argv = offset;",
|
|
525
|
+
"\t\t\targvPtrs.forEach((ptr) => {",
|
|
526
|
+
"\t\t\t\tthis.mem.setUint32(offset, ptr, true);",
|
|
527
|
+
"\t\t\t\tthis.mem.setUint32(offset + 4, 0, true);",
|
|
528
|
+
"\t\t\t\toffset += 8;",
|
|
529
|
+
"\t\t\t});",
|
|
530
|
+
"",
|
|
531
|
+
"\t\t\t// The linker guarantees global data starts from at least wasmMinDataAddr.",
|
|
532
|
+
"\t\t\t// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.",
|
|
533
|
+
"\t\t\tconst wasmMinDataAddr = 4096 + 8192;",
|
|
534
|
+
"\t\t\tif (offset >= wasmMinDataAddr) {",
|
|
535
|
+
"\t\t\t\tthrow new Error(\"total length of command line and environment variables exceeds limit\");",
|
|
536
|
+
"\t\t\t}",
|
|
537
|
+
"",
|
|
538
|
+
"\t\t\tthis._inst.exports.run(argc, argv);",
|
|
539
|
+
"\t\t\tif (this.exited) {",
|
|
540
|
+
"\t\t\t\tthis._resolveExitPromise();",
|
|
541
|
+
"\t\t\t}",
|
|
542
|
+
"\t\t\tawait this._exitPromise;",
|
|
543
|
+
"\t\t}",
|
|
544
|
+
"",
|
|
545
|
+
"\t\t_resume() {",
|
|
546
|
+
"\t\t\tif (this.exited) {",
|
|
547
|
+
"\t\t\t\tthrow new Error(\"Go program has already exited\");",
|
|
548
|
+
"\t\t\t}",
|
|
549
|
+
"\t\t\tthis._inst.exports.resume();",
|
|
550
|
+
"\t\t\tif (this.exited) {",
|
|
551
|
+
"\t\t\t\tthis._resolveExitPromise();",
|
|
552
|
+
"\t\t\t}",
|
|
553
|
+
"\t\t}",
|
|
554
|
+
"",
|
|
555
|
+
"\t\t_makeFuncWrapper(id) {",
|
|
556
|
+
"\t\t\tconst go = this;",
|
|
557
|
+
"\t\t\treturn function () {",
|
|
558
|
+
"\t\t\t\tconst event = { id: id, this: this, args: arguments };",
|
|
559
|
+
"\t\t\t\tgo._pendingEvent = event;",
|
|
560
|
+
"\t\t\t\tgo._resume();",
|
|
561
|
+
"\t\t\t\treturn event.result;",
|
|
562
|
+
"\t\t\t};",
|
|
563
|
+
"\t\t}",
|
|
564
|
+
"\t}",
|
|
565
|
+
"})();",
|
|
566
|
+
"",
|
|
567
|
+
]
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Go's canonical `wasm_exec.js`, verbatim, as opaque runtime text. Evaluated by
|
|
571
|
+
* the WASM loader to install `globalThis.Go`.
|
|
572
|
+
*
|
|
573
|
+
* @internal
|
|
574
|
+
*/
|
|
575
|
+
export const WASM_EXEC_JS: string = WASM_EXEC_LINES.join('\n')
|
|
@@ -21,18 +21,16 @@ import type { LighterSigner } from './LighterSigner.js'
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Per-batch dependencies the Lighter `signActions` implementation needs:
|
|
24
|
-
*
|
|
24
|
+
* the provider instance's WASM signer, its API-key store keyed on L1 address,
|
|
25
25
|
* a REST client for the token-authenticated venue mutations that execute
|
|
26
26
|
* client-side, and an `accountIndex` resolver for the REGISTER_API_KEY
|
|
27
27
|
* hybrid flow.
|
|
28
28
|
*
|
|
29
|
-
* `signer` and `keyStore` are optional because only the `WASM_BLOB` arm signs
|
|
30
|
-
* with them — `EVM_TX` broadcasts through the end-user wallet in `ctx`.
|
|
31
29
|
* @internal
|
|
32
30
|
*/
|
|
33
31
|
export interface LighterSignActionsDeps {
|
|
34
|
-
signer
|
|
35
|
-
keyStore
|
|
32
|
+
signer: LighterSigner
|
|
33
|
+
keyStore: LighterKeyStore
|
|
36
34
|
/** REST client bound to the user's Lighter base URL, used for the direct
|
|
37
35
|
* `changeAccountTier` / `referral/use` POSTs so their auth token never
|
|
38
36
|
* transits the LI.FI backend. */
|
|
@@ -46,12 +44,6 @@ export interface LighterSignActionsDeps {
|
|
|
46
44
|
resolveAccountIndex(address: Address): Promise<number>
|
|
47
45
|
}
|
|
48
46
|
|
|
49
|
-
/** {@link LighterSignActionsDeps} narrowed to the arm that signs wasm blobs. */
|
|
50
|
-
type LighterWasmSignActionsDeps = LighterSignActionsDeps & {
|
|
51
|
-
signer: LighterSigner
|
|
52
|
-
keyStore: LighterKeyStore
|
|
53
|
-
}
|
|
54
|
-
|
|
55
47
|
/**
|
|
56
48
|
* `wasmSignParams.kind` values for the Lighter mutations that authenticate with
|
|
57
49
|
* a short-lived auth token instead of a wasm-signed transaction. Both execute
|
|
@@ -86,35 +78,25 @@ export async function signWasmBlobActions(
|
|
|
86
78
|
steps: WasmBlobActionStep[],
|
|
87
79
|
ctx: SignActionsContext | undefined
|
|
88
80
|
): Promise<WasmBlobSignedActionStep[]> {
|
|
89
|
-
const { signer, keyStore } = deps
|
|
90
|
-
if (signer === undefined || keyStore === undefined) {
|
|
91
|
-
throw new PerpsError(
|
|
92
|
-
PerpsErrorCode.SDKError,
|
|
93
|
-
'lighterProvider.signActions requires `signer` and `keyStore` to be ' +
|
|
94
|
-
'configured at construction.'
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
const wasmDeps: LighterWasmSignActionsDeps = { ...deps, signer, keyStore }
|
|
98
|
-
|
|
99
81
|
const signed: WasmBlobSignedActionStep[] = []
|
|
100
82
|
for (const step of steps) {
|
|
101
83
|
if (step.action === ActionType.REGISTER_API_KEY) {
|
|
102
|
-
signed.push(await signRegisterApiKey(
|
|
84
|
+
signed.push(await signRegisterApiKey(deps, address, step, ctx))
|
|
103
85
|
} else if (step.action === ActionType.APPROVE_INTEGRATOR) {
|
|
104
|
-
signed.push(await signApproveIntegrator(
|
|
86
|
+
signed.push(await signApproveIntegrator(deps, address, step, ctx))
|
|
105
87
|
} else if (
|
|
106
88
|
TOKEN_AUTH_MUTATION_KINDS.has(step.wasmSignParams.kind as string)
|
|
107
89
|
) {
|
|
108
|
-
await executeTokenAuthMutation(
|
|
90
|
+
await executeTokenAuthMutation(deps, address, step)
|
|
109
91
|
} else {
|
|
110
|
-
signed.push(await signStandardWasmAction(
|
|
92
|
+
signed.push(await signStandardWasmAction(deps, address, step))
|
|
111
93
|
}
|
|
112
94
|
}
|
|
113
95
|
return signed
|
|
114
96
|
}
|
|
115
97
|
|
|
116
98
|
async function signStandardWasmAction(
|
|
117
|
-
deps:
|
|
99
|
+
deps: LighterSignActionsDeps,
|
|
118
100
|
address: Address,
|
|
119
101
|
step: WasmBlobActionStep
|
|
120
102
|
): Promise<WasmBlobSignedActionStep> {
|
|
@@ -144,7 +126,7 @@ async function signStandardWasmAction(
|
|
|
144
126
|
* user's consent to rotate keys.
|
|
145
127
|
*/
|
|
146
128
|
async function signRegisterApiKey(
|
|
147
|
-
deps:
|
|
129
|
+
deps: LighterSignActionsDeps,
|
|
148
130
|
address: Address,
|
|
149
131
|
step: WasmBlobActionStep,
|
|
150
132
|
ctx: SignActionsContext | undefined
|
|
@@ -230,7 +212,7 @@ async function signRegisterApiKey(
|
|
|
230
212
|
* different L1 address with non-zero fee caps.
|
|
231
213
|
*/
|
|
232
214
|
async function signApproveIntegrator(
|
|
233
|
-
deps:
|
|
215
|
+
deps: LighterSignActionsDeps,
|
|
234
216
|
address: Address,
|
|
235
217
|
step: WasmBlobActionStep,
|
|
236
218
|
ctx: SignActionsContext | undefined
|
|
@@ -285,7 +267,7 @@ async function signApproveIntegrator(
|
|
|
285
267
|
* `code`/`message` verbatim as an {@link PerpsErrorCode.ExchangeRejected}.
|
|
286
268
|
*/
|
|
287
269
|
async function executeTokenAuthMutation(
|
|
288
|
-
deps:
|
|
270
|
+
deps: LighterSignActionsDeps,
|
|
289
271
|
address: Address,
|
|
290
272
|
step: WasmBlobActionStep
|
|
291
273
|
): Promise<void> {
|
|
@@ -364,7 +346,7 @@ function buildTokenAuthMutationRequest(
|
|
|
364
346
|
}
|
|
365
347
|
|
|
366
348
|
async function requireApiKey(
|
|
367
|
-
deps:
|
|
349
|
+
deps: LighterSignActionsDeps,
|
|
368
350
|
address: Address
|
|
369
351
|
): Promise<LighterApiKey> {
|
|
370
352
|
const apiKey = await deps.keyStore.get(address)
|