@r3e/neo-js-sdk 0.3.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 +117 -0
- package/dist/constants.d.ts +25 -0
- package/dist/constants.js +34 -0
- package/dist/core/block.d.ts +65 -0
- package/dist/core/block.js +128 -0
- package/dist/core/hash.d.ts +20 -0
- package/dist/core/hash.js +51 -0
- package/dist/core/keypair.d.ts +24 -0
- package/dist/core/keypair.js +97 -0
- package/dist/core/opcode.d.ts +3 -0
- package/dist/core/opcode.js +2 -0
- package/dist/core/script.d.ts +25 -0
- package/dist/core/script.js +144 -0
- package/dist/core/serializing.d.ts +40 -0
- package/dist/core/serializing.js +175 -0
- package/dist/core/tx.d.ts +147 -0
- package/dist/core/tx.js +252 -0
- package/dist/core/witness-rule.d.ts +128 -0
- package/dist/core/witness-rule.js +201 -0
- package/dist/core/witness.d.ts +24 -0
- package/dist/core/witness.js +62 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +15 -0
- package/dist/internal/bytes.d.ts +11 -0
- package/dist/internal/bytes.js +55 -0
- package/dist/internal/hex.d.ts +2 -0
- package/dist/internal/hex.js +10 -0
- package/dist/rpcclient/index.d.ts +166 -0
- package/dist/rpcclient/index.js +539 -0
- package/dist/rpcclient/parse.d.ts +16 -0
- package/dist/rpcclient/parse.js +48 -0
- package/dist/rpcclient/types.d.ts +640 -0
- package/dist/rpcclient/types.js +1 -0
- package/dist/utils.d.ts +20 -0
- package/dist/utils.js +40 -0
- package/dist/wallet/index.d.ts +2 -0
- package/dist/wallet/index.js +2 -0
- package/dist/wallet/nep2.d.ts +19 -0
- package/dist/wallet/nep2.js +96 -0
- package/dist/wallet/nep6.d.ts +136 -0
- package/dist/wallet/nep6.js +178 -0
- package/package.json +62 -0
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import { bytesToBase64, hexToBytes } from "../internal/bytes.js";
|
|
2
|
+
import { H256 } from "../core/hash.js";
|
|
3
|
+
import { Tx } from "../core/tx.js";
|
|
4
|
+
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
5
|
+
export var RpcCode;
|
|
6
|
+
(function (RpcCode) {
|
|
7
|
+
RpcCode[RpcCode["InvalidRequest"] = -32600] = "InvalidRequest";
|
|
8
|
+
RpcCode[RpcCode["MethodNotFound"] = -32601] = "MethodNotFound";
|
|
9
|
+
RpcCode[RpcCode["InvalidParams"] = -32602] = "InvalidParams";
|
|
10
|
+
RpcCode[RpcCode["InternalError"] = -32603] = "InternalError";
|
|
11
|
+
RpcCode[RpcCode["BadRequest"] = -32700] = "BadRequest";
|
|
12
|
+
RpcCode[RpcCode["UnknownBlock"] = -101] = "UnknownBlock";
|
|
13
|
+
RpcCode[RpcCode["UnknownContract"] = -102] = "UnknownContract";
|
|
14
|
+
RpcCode[RpcCode["UnknownTransaction"] = -103] = "UnknownTransaction";
|
|
15
|
+
RpcCode[RpcCode["UnknownStorageItem"] = -104] = "UnknownStorageItem";
|
|
16
|
+
RpcCode[RpcCode["UnknownScriptContainer"] = -105] = "UnknownScriptContainer";
|
|
17
|
+
RpcCode[RpcCode["UnknownStateRoot"] = -106] = "UnknownStateRoot";
|
|
18
|
+
RpcCode[RpcCode["UnknownSession"] = -107] = "UnknownSession";
|
|
19
|
+
RpcCode[RpcCode["UnknownIterator"] = -108] = "UnknownIterator";
|
|
20
|
+
RpcCode[RpcCode["UnknownHeight"] = -109] = "UnknownHeight";
|
|
21
|
+
RpcCode[RpcCode["InsufficientFundsWallet"] = -300] = "InsufficientFundsWallet";
|
|
22
|
+
RpcCode[RpcCode["WalletFeeLimit"] = -301] = "WalletFeeLimit";
|
|
23
|
+
RpcCode[RpcCode["NoOpenedWallet"] = -302] = "NoOpenedWallet";
|
|
24
|
+
RpcCode[RpcCode["WalletNotFound"] = -303] = "WalletNotFound";
|
|
25
|
+
RpcCode[RpcCode["WalletNotSupported"] = -304] = "WalletNotSupported";
|
|
26
|
+
RpcCode[RpcCode["UnknownAccount"] = -305] = "UnknownAccount";
|
|
27
|
+
RpcCode[RpcCode["VerificationFailed"] = -500] = "VerificationFailed";
|
|
28
|
+
RpcCode[RpcCode["AlreadyExists"] = -501] = "AlreadyExists";
|
|
29
|
+
RpcCode[RpcCode["MempoolCapacityReached"] = -502] = "MempoolCapacityReached";
|
|
30
|
+
RpcCode[RpcCode["AlreadyInPool"] = -503] = "AlreadyInPool";
|
|
31
|
+
RpcCode[RpcCode["InsufficientNetworkFee"] = -504] = "InsufficientNetworkFee";
|
|
32
|
+
RpcCode[RpcCode["PolicyFailed"] = -505] = "PolicyFailed";
|
|
33
|
+
RpcCode[RpcCode["InvalidScript"] = -506] = "InvalidScript";
|
|
34
|
+
RpcCode[RpcCode["InvalidAttribute"] = -507] = "InvalidAttribute";
|
|
35
|
+
RpcCode[RpcCode["InvalidSignature"] = -508] = "InvalidSignature";
|
|
36
|
+
RpcCode[RpcCode["InvalidSize"] = -509] = "InvalidSize";
|
|
37
|
+
RpcCode[RpcCode["ExpiredTransaction"] = -510] = "ExpiredTransaction";
|
|
38
|
+
RpcCode[RpcCode["InsufficientFunds"] = -511] = "InsufficientFunds";
|
|
39
|
+
RpcCode[RpcCode["InvalidContractVerification"] = -512] = "InvalidContractVerification";
|
|
40
|
+
RpcCode[RpcCode["AccessDenied"] = -600] = "AccessDenied";
|
|
41
|
+
RpcCode[RpcCode["SessionsDisabled"] = -601] = "SessionsDisabled";
|
|
42
|
+
RpcCode[RpcCode["OracleDisabled"] = -602] = "OracleDisabled";
|
|
43
|
+
RpcCode[RpcCode["OracleRequestFinished"] = -603] = "OracleRequestFinished";
|
|
44
|
+
RpcCode[RpcCode["OracleRequestNotFound"] = -604] = "OracleRequestNotFound";
|
|
45
|
+
RpcCode[RpcCode["OracleNotDesignatedNode"] = -605] = "OracleNotDesignatedNode";
|
|
46
|
+
RpcCode[RpcCode["UnsupportedState"] = -606] = "UnsupportedState";
|
|
47
|
+
RpcCode[RpcCode["InvalidProof"] = -607] = "InvalidProof";
|
|
48
|
+
RpcCode[RpcCode["ExecutionFailed"] = -608] = "ExecutionFailed";
|
|
49
|
+
})(RpcCode || (RpcCode = {}));
|
|
50
|
+
export class JsonRpcError extends Error {
|
|
51
|
+
code;
|
|
52
|
+
constructor(code, message) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.code = code;
|
|
55
|
+
this.name = "JsonRpcError";
|
|
56
|
+
}
|
|
57
|
+
toString() {
|
|
58
|
+
return `JsonRpcError{code:${this.code},message:${this.message}}`;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function defaultTransport(url, request, timeoutMs) {
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
64
|
+
try {
|
|
65
|
+
const response = await fetch(url, {
|
|
66
|
+
method: "POST",
|
|
67
|
+
headers: {
|
|
68
|
+
"Content-Type": "application/json",
|
|
69
|
+
},
|
|
70
|
+
body: JSON.stringify(request),
|
|
71
|
+
signal: controller.signal,
|
|
72
|
+
});
|
|
73
|
+
return (await response.json());
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
clearTimeout(timeout);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function normalizeEndpoints(endpoints) {
|
|
80
|
+
return Array.isArray(endpoints) ? endpoints : [endpoints];
|
|
81
|
+
}
|
|
82
|
+
function serializeHash(value) {
|
|
83
|
+
return typeof value === "string" ? value : value.toString();
|
|
84
|
+
}
|
|
85
|
+
function encodeBinary(value) {
|
|
86
|
+
if (value instanceof Tx) {
|
|
87
|
+
return bytesToBase64(value.toBytes());
|
|
88
|
+
}
|
|
89
|
+
if (value instanceof Uint8Array) {
|
|
90
|
+
return bytesToBase64(value);
|
|
91
|
+
}
|
|
92
|
+
if (typeof value === "string") {
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === "object" && value !== null && "toBase64" in value && typeof value.toBase64 === "function") {
|
|
96
|
+
return value.toBase64();
|
|
97
|
+
}
|
|
98
|
+
throw new Error("Unsupported binary payload");
|
|
99
|
+
}
|
|
100
|
+
function encodeStorageKey(value) {
|
|
101
|
+
if (value instanceof Uint8Array) {
|
|
102
|
+
return bytesToBase64(value);
|
|
103
|
+
}
|
|
104
|
+
return bytesToBase64(hexToBytes(value));
|
|
105
|
+
}
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
107
|
+
function serializeToJson(item) {
|
|
108
|
+
if (typeof item === "object" && item !== null) {
|
|
109
|
+
if (typeof item.toJSON === "function") {
|
|
110
|
+
return item.toJSON();
|
|
111
|
+
}
|
|
112
|
+
if (typeof item.toJson === "function") {
|
|
113
|
+
return item.toJson();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return item;
|
|
117
|
+
}
|
|
118
|
+
function serializeSigners(signers = []) {
|
|
119
|
+
return signers.map(serializeToJson);
|
|
120
|
+
}
|
|
121
|
+
function serializeInvokeArgs(args) {
|
|
122
|
+
return args.map(serializeToJson);
|
|
123
|
+
}
|
|
124
|
+
function serializeTransferValue(value) {
|
|
125
|
+
return typeof value === "string" ? value : BigInt(value).toString();
|
|
126
|
+
}
|
|
127
|
+
export function mainnetEndpoints() {
|
|
128
|
+
return [
|
|
129
|
+
"http://seed1.neo.org:10332",
|
|
130
|
+
"http://seed2.neo.org:10332",
|
|
131
|
+
"http://seed3.neo.org:10332",
|
|
132
|
+
"http://seed4.neo.org:10332",
|
|
133
|
+
"http://seed5.neo.org:10332",
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
export function testnetEndpoints() {
|
|
137
|
+
return [
|
|
138
|
+
"http://seed1t5.neo.org:20332",
|
|
139
|
+
"http://seed2t5.neo.org:20332",
|
|
140
|
+
"http://seed3t5.neo.org:20332",
|
|
141
|
+
"http://seed4t5.neo.org:20332",
|
|
142
|
+
"http://seed5t5.neo.org:20332",
|
|
143
|
+
];
|
|
144
|
+
}
|
|
145
|
+
export class InvokeParameters {
|
|
146
|
+
parameters = [];
|
|
147
|
+
addAny() {
|
|
148
|
+
this.parameters.push({ type: "Any" });
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
addBool(value) {
|
|
152
|
+
this.parameters.push({ type: "Boolean", value });
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
addInteger(value) {
|
|
156
|
+
this.parameters.push({ type: "Integer", value: BigInt(value).toString() });
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
addHash160(value) {
|
|
160
|
+
this.parameters.push({ type: "Hash160", value: serializeHash(value) });
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
addHash256(value) {
|
|
164
|
+
this.parameters.push({ type: "Hash256", value: serializeHash(value) });
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
addPublicKey(value) {
|
|
168
|
+
this.parameters.push({ type: "PublicKey", value: value.toString() });
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
addString(value) {
|
|
172
|
+
this.parameters.push({ type: "String", value });
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
addSignature(value) {
|
|
176
|
+
const encoded = typeof value === "string" ? value : bytesToBase64(value);
|
|
177
|
+
this.parameters.push({ type: "Signature", value: encoded });
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
addByteArray(value) {
|
|
181
|
+
const encoded = typeof value === "string" ? value : bytesToBase64(value);
|
|
182
|
+
this.parameters.push({ type: "ByteArray", value: encoded });
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
185
|
+
toJSON() {
|
|
186
|
+
return [...this.parameters];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export class JsonRpc {
|
|
190
|
+
endpoints;
|
|
191
|
+
timeoutMs;
|
|
192
|
+
transport;
|
|
193
|
+
endpointStrategy;
|
|
194
|
+
retryTransportErrors;
|
|
195
|
+
nextId = 1;
|
|
196
|
+
nextEndpointIndex = 0;
|
|
197
|
+
constructor(endpoints, options = {}) {
|
|
198
|
+
this.endpoints = normalizeEndpoints(endpoints);
|
|
199
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
200
|
+
this.transport = options.transport ?? defaultTransport;
|
|
201
|
+
this.endpointStrategy = options.endpointStrategy ?? "first";
|
|
202
|
+
this.retryTransportErrors = options.retryTransportErrors ?? false;
|
|
203
|
+
}
|
|
204
|
+
async send(method, params = []) {
|
|
205
|
+
if (this.endpoints.length === 0) {
|
|
206
|
+
throw new JsonRpcError(RpcCode.BadRequest, "no RPC endpoints configured");
|
|
207
|
+
}
|
|
208
|
+
const request = {
|
|
209
|
+
jsonrpc: "2.0",
|
|
210
|
+
id: this.nextId,
|
|
211
|
+
method,
|
|
212
|
+
params,
|
|
213
|
+
};
|
|
214
|
+
this.nextId += 1;
|
|
215
|
+
const startIndex = this.endpointStrategy === "round-robin" ? this.nextEndpointIndex : 0;
|
|
216
|
+
if (this.endpointStrategy === "round-robin") {
|
|
217
|
+
this.nextEndpointIndex = (this.nextEndpointIndex + 1) % this.endpoints.length;
|
|
218
|
+
}
|
|
219
|
+
const maxAttempts = this.retryTransportErrors ? this.endpoints.length : 1;
|
|
220
|
+
let lastTransportError = null;
|
|
221
|
+
for (let offset = 0; offset < maxAttempts; offset += 1) {
|
|
222
|
+
const endpointIndex = (startIndex + offset) % this.endpoints.length;
|
|
223
|
+
const endpoint = this.endpoints[endpointIndex];
|
|
224
|
+
try {
|
|
225
|
+
const response = await this.transport(endpoint, request, this.timeoutMs);
|
|
226
|
+
if ("error" in response && response.error) {
|
|
227
|
+
const code = response.error?.code ?? RpcCode.InternalError;
|
|
228
|
+
const message = response.error?.message ?? "unknown RPC error";
|
|
229
|
+
throw new JsonRpcError(code, message);
|
|
230
|
+
}
|
|
231
|
+
if (!("result" in response)) {
|
|
232
|
+
throw new JsonRpcError(RpcCode.InvalidRequest, "response missing result");
|
|
233
|
+
}
|
|
234
|
+
return response.result;
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
if (error instanceof JsonRpcError) {
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
lastTransportError = new JsonRpcError(RpcCode.InternalError, error instanceof Error ? error.message : String(error));
|
|
241
|
+
if (!this.retryTransportErrors) {
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
throw lastTransportError ?? new JsonRpcError(RpcCode.InternalError, "RPC request failed");
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
export class RpcClient {
|
|
250
|
+
jsonrpc;
|
|
251
|
+
constructor(endpoints = [], options = {}) {
|
|
252
|
+
this.jsonrpc = options.jsonrpc ?? new JsonRpc(endpoints, options);
|
|
253
|
+
}
|
|
254
|
+
async send(method, params = []) {
|
|
255
|
+
return this.jsonrpc.send(method, params);
|
|
256
|
+
}
|
|
257
|
+
callNoParams(method) {
|
|
258
|
+
return this.send(method);
|
|
259
|
+
}
|
|
260
|
+
callSingleParam(method, param) {
|
|
261
|
+
return this.send(method, [param]);
|
|
262
|
+
}
|
|
263
|
+
callHashParam(method, value) {
|
|
264
|
+
return this.callSingleParam(method, serializeHash(value));
|
|
265
|
+
}
|
|
266
|
+
callWithBooleanArg(method, param, flag = 0) {
|
|
267
|
+
return this.send(method, [param, flag]);
|
|
268
|
+
}
|
|
269
|
+
callEncodedPayload(method, payload) {
|
|
270
|
+
return this.callSingleParam(method, encodeBinary(payload));
|
|
271
|
+
}
|
|
272
|
+
callAccountTimeRange(method, account, startTime, endTime) {
|
|
273
|
+
const params = [account];
|
|
274
|
+
if (startTime !== undefined || endTime !== undefined) {
|
|
275
|
+
params.push(startTime ?? "");
|
|
276
|
+
}
|
|
277
|
+
if (endTime !== undefined) {
|
|
278
|
+
params.push(endTime);
|
|
279
|
+
}
|
|
280
|
+
return this.send(method, params);
|
|
281
|
+
}
|
|
282
|
+
callStorageLookup(method, scriptHash, key) {
|
|
283
|
+
return this.send(method, [serializeHash(scriptHash), encodeStorageKey(key)]);
|
|
284
|
+
}
|
|
285
|
+
serializeInvokeInput(args) {
|
|
286
|
+
if (args === undefined) {
|
|
287
|
+
return [];
|
|
288
|
+
}
|
|
289
|
+
return Array.isArray(args) ? serializeInvokeArgs(args) : args.toJSON();
|
|
290
|
+
}
|
|
291
|
+
serializeSignerInput(signers) {
|
|
292
|
+
return serializeSigners(signers ?? []);
|
|
293
|
+
}
|
|
294
|
+
getBestBlockHash() {
|
|
295
|
+
return this.callNoParams("getbestblockhash");
|
|
296
|
+
}
|
|
297
|
+
getBlock(input) {
|
|
298
|
+
return this.callWithBooleanArg("getblock", input.indexOrHash, input.verbose ?? 0);
|
|
299
|
+
}
|
|
300
|
+
getBlockCount() {
|
|
301
|
+
return this.callNoParams("getblockcount");
|
|
302
|
+
}
|
|
303
|
+
getBlockHeaderCount() {
|
|
304
|
+
return this.callNoParams("getblockheadercount");
|
|
305
|
+
}
|
|
306
|
+
getBlockHash(input) {
|
|
307
|
+
return this.callSingleParam("getblockhash", input.blockIndex);
|
|
308
|
+
}
|
|
309
|
+
getBlockHeader(input) {
|
|
310
|
+
const blockHash = input.blockHash instanceof H256 ? input.blockHash.toString() : (input.blockHash ?? null);
|
|
311
|
+
const height = input.height ?? null;
|
|
312
|
+
const indexOrHash = input.indexOrHash ?? null;
|
|
313
|
+
const resolvedVerbose = input.verbose ?? 0;
|
|
314
|
+
const providedLocators = [blockHash, height, indexOrHash].filter((value) => value !== null);
|
|
315
|
+
if (providedLocators.length === 0) {
|
|
316
|
+
return Promise.reject(new JsonRpcError(RpcCode.InvalidParams, "indexOrHash, blockHash, or height is required"));
|
|
317
|
+
}
|
|
318
|
+
if (providedLocators.length > 1) {
|
|
319
|
+
return Promise.reject(new JsonRpcError(RpcCode.InvalidParams, "only one block locator may be set"));
|
|
320
|
+
}
|
|
321
|
+
return this.callWithBooleanArg("getblockheader", blockHash ?? height ?? indexOrHash, resolvedVerbose);
|
|
322
|
+
}
|
|
323
|
+
getApplicationLog(input) {
|
|
324
|
+
const params = [serializeHash(input.hash)];
|
|
325
|
+
if (input.trigger !== undefined) {
|
|
326
|
+
params.push(input.trigger);
|
|
327
|
+
}
|
|
328
|
+
return this.send("getapplicationlog", params);
|
|
329
|
+
}
|
|
330
|
+
getCandidates() {
|
|
331
|
+
return this.callNoParams("getcandidates");
|
|
332
|
+
}
|
|
333
|
+
getCommittee() {
|
|
334
|
+
return this.callNoParams("getcommittee");
|
|
335
|
+
}
|
|
336
|
+
getConnectionCount() {
|
|
337
|
+
return this.callNoParams("getconnectioncount");
|
|
338
|
+
}
|
|
339
|
+
getContractState(input) {
|
|
340
|
+
return this.callHashParam("getcontractstate", input.scriptHash);
|
|
341
|
+
}
|
|
342
|
+
getNativeContracts() {
|
|
343
|
+
return this.callNoParams("getnativecontracts");
|
|
344
|
+
}
|
|
345
|
+
getNep11Balances(input) {
|
|
346
|
+
return this.callSingleParam("getnep11balances", input.account);
|
|
347
|
+
}
|
|
348
|
+
getNep11Properties(input) {
|
|
349
|
+
return this.send("getnep11properties", [serializeHash(input.contractHash), input.tokenId]);
|
|
350
|
+
}
|
|
351
|
+
getNep11Transfers(input) {
|
|
352
|
+
return this.callAccountTimeRange("getnep11transfers", input.account, input.startTime, input.endTime);
|
|
353
|
+
}
|
|
354
|
+
getNep17Balances(input) {
|
|
355
|
+
return this.callSingleParam("getnep17balances", input.account);
|
|
356
|
+
}
|
|
357
|
+
getNep17Transfers(input) {
|
|
358
|
+
return this.callAccountTimeRange("getnep17transfers", input.account, input.startTime, input.endTime);
|
|
359
|
+
}
|
|
360
|
+
getNextBlockValidators() {
|
|
361
|
+
return this.callNoParams("getnextblockvalidators");
|
|
362
|
+
}
|
|
363
|
+
getPeers() {
|
|
364
|
+
return this.callNoParams("getpeers");
|
|
365
|
+
}
|
|
366
|
+
getRawMemPool(includeUnverified = 0) {
|
|
367
|
+
return this.callSingleParam("getrawmempool", includeUnverified);
|
|
368
|
+
}
|
|
369
|
+
getRawTransaction(input) {
|
|
370
|
+
return this.callWithBooleanArg("getrawtransaction", serializeHash(input.hash), input.verbose ?? 0);
|
|
371
|
+
}
|
|
372
|
+
getStateHeight() {
|
|
373
|
+
return this.callNoParams("getstateheight");
|
|
374
|
+
}
|
|
375
|
+
getStateRoot(input) {
|
|
376
|
+
return this.callSingleParam("getstateroot", input.index);
|
|
377
|
+
}
|
|
378
|
+
getProof(input) {
|
|
379
|
+
return this.send("getproof", [
|
|
380
|
+
serializeHash(input.rootHash),
|
|
381
|
+
serializeHash(input.contractHash),
|
|
382
|
+
encodeStorageKey(input.key),
|
|
383
|
+
]);
|
|
384
|
+
}
|
|
385
|
+
verifyProof(input) {
|
|
386
|
+
return this.send("verifyproof", [serializeHash(input.rootHash), encodeStorageKey(input.proof)]);
|
|
387
|
+
}
|
|
388
|
+
getState(input) {
|
|
389
|
+
return this.send("getstate", [
|
|
390
|
+
serializeHash(input.rootHash),
|
|
391
|
+
serializeHash(input.contractHash),
|
|
392
|
+
encodeStorageKey(input.key),
|
|
393
|
+
]);
|
|
394
|
+
}
|
|
395
|
+
getStorage(input) {
|
|
396
|
+
return this.callStorageLookup("getstorage", input.scriptHash, input.key);
|
|
397
|
+
}
|
|
398
|
+
findStorage(input) {
|
|
399
|
+
return this.send("findstorage", [
|
|
400
|
+
serializeHash(input.scriptHash),
|
|
401
|
+
encodeStorageKey(input.prefix),
|
|
402
|
+
input.start ?? 0,
|
|
403
|
+
]);
|
|
404
|
+
}
|
|
405
|
+
findStates(input) {
|
|
406
|
+
const params = [
|
|
407
|
+
serializeHash(input.rootHash),
|
|
408
|
+
serializeHash(input.contractHash),
|
|
409
|
+
encodeStorageKey(input.prefix),
|
|
410
|
+
];
|
|
411
|
+
if (input.from !== undefined || input.count !== undefined) {
|
|
412
|
+
params.push(input.from === undefined ? "" : encodeStorageKey(input.from));
|
|
413
|
+
}
|
|
414
|
+
if (input.count !== undefined) {
|
|
415
|
+
params.push(input.count);
|
|
416
|
+
}
|
|
417
|
+
return this.send("findstates", params);
|
|
418
|
+
}
|
|
419
|
+
getTransactionHeight(input) {
|
|
420
|
+
return this.callHashParam("gettransactionheight", input.hash);
|
|
421
|
+
}
|
|
422
|
+
getUnclaimedGas(input) {
|
|
423
|
+
return this.callSingleParam("getunclaimedgas", input.address);
|
|
424
|
+
}
|
|
425
|
+
getUnspents(input) {
|
|
426
|
+
return this.callSingleParam("getunspents", input.address);
|
|
427
|
+
}
|
|
428
|
+
getVersion() {
|
|
429
|
+
return this.callNoParams("getversion");
|
|
430
|
+
}
|
|
431
|
+
openWallet(input) {
|
|
432
|
+
return this.send("openwallet", [input.path, input.password]);
|
|
433
|
+
}
|
|
434
|
+
closeWallet() {
|
|
435
|
+
return this.callNoParams("closewallet");
|
|
436
|
+
}
|
|
437
|
+
dumpPrivKey(input) {
|
|
438
|
+
return this.callSingleParam("dumpprivkey", input.address);
|
|
439
|
+
}
|
|
440
|
+
getNewAddress() {
|
|
441
|
+
return this.callNoParams("getnewaddress");
|
|
442
|
+
}
|
|
443
|
+
getWalletBalance(input) {
|
|
444
|
+
return this.callHashParam("getwalletbalance", input.assetId);
|
|
445
|
+
}
|
|
446
|
+
getWalletUnclaimedGas() {
|
|
447
|
+
return this.callNoParams("getwalletunclaimedgas");
|
|
448
|
+
}
|
|
449
|
+
importPrivKey(input) {
|
|
450
|
+
return this.callSingleParam("importprivkey", input.wif);
|
|
451
|
+
}
|
|
452
|
+
listAddress() {
|
|
453
|
+
return this.callNoParams("listaddress");
|
|
454
|
+
}
|
|
455
|
+
invokeFunction(input) {
|
|
456
|
+
const serializedArgs = this.serializeInvokeInput(input.args);
|
|
457
|
+
return this.send("invokefunction", [
|
|
458
|
+
serializeHash(input.contractHash),
|
|
459
|
+
input.method,
|
|
460
|
+
serializedArgs,
|
|
461
|
+
this.serializeSignerInput(input.signers),
|
|
462
|
+
]);
|
|
463
|
+
}
|
|
464
|
+
invokeContractVerify(input) {
|
|
465
|
+
const serializedArgs = this.serializeInvokeInput(input.args);
|
|
466
|
+
return this.send("invokecontractverify", [
|
|
467
|
+
serializeHash(input.contractHash),
|
|
468
|
+
serializedArgs,
|
|
469
|
+
this.serializeSignerInput(input.signers),
|
|
470
|
+
]);
|
|
471
|
+
}
|
|
472
|
+
invokeScript(input) {
|
|
473
|
+
return this.send("invokescript", [encodeBinary(input.script), this.serializeSignerInput(input.signers)]);
|
|
474
|
+
}
|
|
475
|
+
traverseIterator(input) {
|
|
476
|
+
return this.send("traverseiterator", [input.sessionId, input.iteratorId, input.count]);
|
|
477
|
+
}
|
|
478
|
+
terminateSession(input) {
|
|
479
|
+
return this.callSingleParam("terminatesession", input.sessionId);
|
|
480
|
+
}
|
|
481
|
+
listPlugins() {
|
|
482
|
+
return this.callNoParams("listplugins");
|
|
483
|
+
}
|
|
484
|
+
sendRawTransaction(input) {
|
|
485
|
+
return this.callEncodedPayload("sendrawtransaction", input.tx);
|
|
486
|
+
}
|
|
487
|
+
sendFrom(input) {
|
|
488
|
+
const params = [
|
|
489
|
+
serializeHash(input.assetId),
|
|
490
|
+
input.from,
|
|
491
|
+
input.to,
|
|
492
|
+
serializeTransferValue(input.amount),
|
|
493
|
+
];
|
|
494
|
+
const signers = (input.signers ?? []).map((signer) => serializeHash(signer));
|
|
495
|
+
if (signers.length > 0) {
|
|
496
|
+
params.push(signers);
|
|
497
|
+
}
|
|
498
|
+
return this.send("sendfrom", params);
|
|
499
|
+
}
|
|
500
|
+
sendMany(input) {
|
|
501
|
+
const serializedTransfers = input.transfers.map((transfer) => ({
|
|
502
|
+
asset: serializeHash(transfer.asset),
|
|
503
|
+
value: serializeTransferValue(transfer.value),
|
|
504
|
+
address: transfer.address,
|
|
505
|
+
}));
|
|
506
|
+
const params = [];
|
|
507
|
+
if (input.from !== undefined) {
|
|
508
|
+
params.push(input.from);
|
|
509
|
+
}
|
|
510
|
+
params.push(serializedTransfers);
|
|
511
|
+
const signers = (input.signers ?? []).map((signer) => serializeHash(signer));
|
|
512
|
+
if (signers.length > 0) {
|
|
513
|
+
params.push(signers);
|
|
514
|
+
}
|
|
515
|
+
return this.send("sendmany", params);
|
|
516
|
+
}
|
|
517
|
+
sendToAddress(input) {
|
|
518
|
+
return this.send("sendtoaddress", [serializeHash(input.assetId), input.to, serializeTransferValue(input.amount)]);
|
|
519
|
+
}
|
|
520
|
+
submitBlock(input) {
|
|
521
|
+
return this.callEncodedPayload("submitblock", input.block);
|
|
522
|
+
}
|
|
523
|
+
validateAddress(input) {
|
|
524
|
+
return this.callSingleParam("validateaddress", input.address);
|
|
525
|
+
}
|
|
526
|
+
cancelTransaction(input) {
|
|
527
|
+
const params = [
|
|
528
|
+
serializeHash(input.txHash),
|
|
529
|
+
(input.signers ?? []).map((signer) => serializeHash(signer)),
|
|
530
|
+
];
|
|
531
|
+
if (input.extraFee !== undefined) {
|
|
532
|
+
params.push(typeof input.extraFee === "string" ? input.extraFee : BigInt(input.extraFee).toString());
|
|
533
|
+
}
|
|
534
|
+
return this.send("canceltransaction", params);
|
|
535
|
+
}
|
|
536
|
+
calculateNetworkFee(input) {
|
|
537
|
+
return this.callEncodedPayload("calculatenetworkfee", input.tx);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { InvokeResult, RpcArrayStackItemJson, RpcMapStackEntryJson, RpcStackItemJson } from "./types.js";
|
|
2
|
+
export type StackItemParser<T = unknown> = (item: RpcStackItemJson) => T;
|
|
3
|
+
export type InvokeResultParser<T = unknown[]> = (result: InvokeResult) => T;
|
|
4
|
+
export declare function parseStackItemBoolean(item: RpcStackItemJson): boolean;
|
|
5
|
+
export declare function parseStackItemInteger(item: RpcStackItemJson): bigint;
|
|
6
|
+
export declare function parseStackItemBytes(item: RpcStackItemJson): Uint8Array;
|
|
7
|
+
export declare function parseStackItemUtf8(item: RpcStackItemJson): string;
|
|
8
|
+
export declare function parseStackItemArray(item: RpcStackItemJson): RpcArrayStackItemJson["value"];
|
|
9
|
+
export declare function parseStackItemStruct(item: RpcStackItemJson): RpcArrayStackItemJson["value"];
|
|
10
|
+
export declare function parseStackItemMap(item: RpcStackItemJson): RpcMapStackEntryJson[];
|
|
11
|
+
export declare function buildStackParser<TParsers extends readonly StackItemParser[]>(...parsers: TParsers): InvokeResultParser<{
|
|
12
|
+
[K in keyof TParsers]: TParsers[K] extends StackItemParser<infer TResult> ? TResult : never;
|
|
13
|
+
}>;
|
|
14
|
+
export declare function parseInvokeStack<TParsers extends readonly StackItemParser[]>(result: InvokeResult, ...parsers: TParsers): {
|
|
15
|
+
[K in keyof TParsers]: TParsers[K] extends StackItemParser<infer TResult> ? TResult : never;
|
|
16
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { base64ToBytes } from "../internal/bytes.js";
|
|
2
|
+
function assertType(item, expected) {
|
|
3
|
+
if (item.type !== expected) {
|
|
4
|
+
throw new Error(`Expected stack item type ${expected} but got ${item.type}`);
|
|
5
|
+
}
|
|
6
|
+
return item;
|
|
7
|
+
}
|
|
8
|
+
export function parseStackItemBoolean(item) {
|
|
9
|
+
return assertType(item, "Boolean").value;
|
|
10
|
+
}
|
|
11
|
+
export function parseStackItemInteger(item) {
|
|
12
|
+
const value = assertType(item, "Integer").value;
|
|
13
|
+
return BigInt(value);
|
|
14
|
+
}
|
|
15
|
+
export function parseStackItemBytes(item) {
|
|
16
|
+
const typed = item.type === "ByteString"
|
|
17
|
+
? item
|
|
18
|
+
: item.type === "Buffer"
|
|
19
|
+
? item
|
|
20
|
+
: null;
|
|
21
|
+
if (typed === null) {
|
|
22
|
+
throw new Error(`Expected stack item type ByteString or Buffer but got ${item.type}`);
|
|
23
|
+
}
|
|
24
|
+
return base64ToBytes(typed.value);
|
|
25
|
+
}
|
|
26
|
+
export function parseStackItemUtf8(item) {
|
|
27
|
+
return new TextDecoder().decode(parseStackItemBytes(item));
|
|
28
|
+
}
|
|
29
|
+
export function parseStackItemArray(item) {
|
|
30
|
+
return assertType(item, "Array").value;
|
|
31
|
+
}
|
|
32
|
+
export function parseStackItemStruct(item) {
|
|
33
|
+
return assertType(item, "Struct").value;
|
|
34
|
+
}
|
|
35
|
+
export function parseStackItemMap(item) {
|
|
36
|
+
return assertType(item, "Map").value;
|
|
37
|
+
}
|
|
38
|
+
export function buildStackParser(...parsers) {
|
|
39
|
+
return (result) => {
|
|
40
|
+
if (result.stack.length !== parsers.length) {
|
|
41
|
+
throw new Error(`Wrong number of items to parse! Expected ${parsers.length} but got ${result.stack.length}!`);
|
|
42
|
+
}
|
|
43
|
+
return result.stack.map((item, index) => parsers[index](item));
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function parseInvokeStack(result, ...parsers) {
|
|
47
|
+
return buildStackParser(...parsers)(result);
|
|
48
|
+
}
|