@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.
Files changed (42) hide show
  1. package/README.md +117 -0
  2. package/dist/constants.d.ts +25 -0
  3. package/dist/constants.js +34 -0
  4. package/dist/core/block.d.ts +65 -0
  5. package/dist/core/block.js +128 -0
  6. package/dist/core/hash.d.ts +20 -0
  7. package/dist/core/hash.js +51 -0
  8. package/dist/core/keypair.d.ts +24 -0
  9. package/dist/core/keypair.js +97 -0
  10. package/dist/core/opcode.d.ts +3 -0
  11. package/dist/core/opcode.js +2 -0
  12. package/dist/core/script.d.ts +25 -0
  13. package/dist/core/script.js +144 -0
  14. package/dist/core/serializing.d.ts +40 -0
  15. package/dist/core/serializing.js +175 -0
  16. package/dist/core/tx.d.ts +147 -0
  17. package/dist/core/tx.js +252 -0
  18. package/dist/core/witness-rule.d.ts +128 -0
  19. package/dist/core/witness-rule.js +201 -0
  20. package/dist/core/witness.d.ts +24 -0
  21. package/dist/core/witness.js +62 -0
  22. package/dist/index.d.ts +16 -0
  23. package/dist/index.js +15 -0
  24. package/dist/internal/bytes.d.ts +11 -0
  25. package/dist/internal/bytes.js +55 -0
  26. package/dist/internal/hex.d.ts +2 -0
  27. package/dist/internal/hex.js +10 -0
  28. package/dist/rpcclient/index.d.ts +166 -0
  29. package/dist/rpcclient/index.js +539 -0
  30. package/dist/rpcclient/parse.d.ts +16 -0
  31. package/dist/rpcclient/parse.js +48 -0
  32. package/dist/rpcclient/types.d.ts +640 -0
  33. package/dist/rpcclient/types.js +1 -0
  34. package/dist/utils.d.ts +20 -0
  35. package/dist/utils.js +40 -0
  36. package/dist/wallet/index.d.ts +2 -0
  37. package/dist/wallet/index.js +2 -0
  38. package/dist/wallet/nep2.d.ts +19 -0
  39. package/dist/wallet/nep2.js +96 -0
  40. package/dist/wallet/nep6.d.ts +136 -0
  41. package/dist/wallet/nep6.js +178 -0
  42. package/package.json +62 -0
@@ -0,0 +1,144 @@
1
+ import { createHash } from "node:crypto";
2
+ import { bytesToHex, concatBytes, toUint8Array, utf8ToBytes } from "../internal/bytes.js";
3
+ import { H160, H256 } from "./hash.js";
4
+ import { serialize } from "./serializing.js";
5
+ import { PublicKey } from "./keypair.js";
6
+ import { OpCode } from "./opcode.js";
7
+ export var CallFlags;
8
+ (function (CallFlags) {
9
+ CallFlags[CallFlags["NONE"] = 0] = "NONE";
10
+ CallFlags[CallFlags["ReadStates"] = 1] = "ReadStates";
11
+ CallFlags[CallFlags["WriteStates"] = 2] = "WriteStates";
12
+ CallFlags[CallFlags["AllowCall"] = 4] = "AllowCall";
13
+ CallFlags[CallFlags["AllowNotify"] = 8] = "AllowNotify";
14
+ CallFlags[CallFlags["States"] = 3] = "States";
15
+ CallFlags[CallFlags["ReadOnly"] = 5] = "ReadOnly";
16
+ CallFlags[CallFlags["All"] = 15] = "All";
17
+ })(CallFlags || (CallFlags = {}));
18
+ export function syscallCode(syscallName) {
19
+ const hash = createHash("sha256").update(syscallName).digest();
20
+ return hash.readUInt32LE(0);
21
+ }
22
+ function bigintToFixedLE(value) {
23
+ if (value >= -1n && value <= 16n) {
24
+ return new Uint8Array();
25
+ }
26
+ const widths = [8, 16, 32, 64, 128, 256];
27
+ for (const bits of widths) {
28
+ const min = -(1n << BigInt(bits - 1));
29
+ const max = (1n << BigInt(bits - 1)) - 1n;
30
+ if (value < min || value > max) {
31
+ continue;
32
+ }
33
+ const mod = 1n << BigInt(bits);
34
+ let unsigned = value < 0n ? mod + value : value;
35
+ const bytes = new Uint8Array(bits / 8);
36
+ for (let index = 0; index < bytes.length; index += 1) {
37
+ bytes[index] = Number(unsigned & 0xffn);
38
+ unsigned >>= 8n;
39
+ }
40
+ return bytes;
41
+ }
42
+ throw new Error(`push too large integer: ${value.toString()}`);
43
+ }
44
+ export class ScriptBuilder {
45
+ script = [];
46
+ emit(opcode, operand = new Uint8Array()) {
47
+ this.script.push(opcode, ...operand);
48
+ return this;
49
+ }
50
+ emitPush(item) {
51
+ if (item === null || item === undefined) {
52
+ return this.emit(OpCode.PUSHNULL);
53
+ }
54
+ if (typeof item === "boolean") {
55
+ return this.emit(item ? OpCode.PUSHT : OpCode.PUSHF);
56
+ }
57
+ if (typeof item === "number" || typeof item === "bigint") {
58
+ return this.emitPushInt(BigInt(item));
59
+ }
60
+ if (typeof item === "string") {
61
+ return this.emitPushBytes(utf8ToBytes(item));
62
+ }
63
+ if (Array.isArray(item)) {
64
+ return this.emitPushArray(item);
65
+ }
66
+ if (item instanceof Uint8Array || item instanceof ArrayBuffer) {
67
+ return this.emitPushBytes(toUint8Array(item));
68
+ }
69
+ if (item instanceof H160 || item instanceof H256 || item instanceof PublicKey) {
70
+ return this.emitPushBytes(serialize(item));
71
+ }
72
+ if (typeof item === "object" && item && "marshalTo" in item) {
73
+ return this.emitPushBytes(serialize(item));
74
+ }
75
+ throw new Error(`Unsupported push item: ${String(item)}`);
76
+ }
77
+ emitPushInt(value) {
78
+ if (value >= -1n && value <= 16n) {
79
+ return this.emit((OpCode.PUSHM1 + Number(value) + 1));
80
+ }
81
+ const payload = bigintToFixedLE(value);
82
+ switch (payload.length) {
83
+ case 1:
84
+ return this.emit(OpCode.PUSHINT8, payload);
85
+ case 2:
86
+ return this.emit(OpCode.PUSHINT16, payload);
87
+ case 4:
88
+ return this.emit(OpCode.PUSHINT32, payload);
89
+ case 8:
90
+ return this.emit(OpCode.PUSHINT64, payload);
91
+ case 16:
92
+ return this.emit(OpCode.PUSHINT128, payload);
93
+ case 32:
94
+ return this.emit(OpCode.PUSHINT256, payload);
95
+ default:
96
+ throw new Error(`Unsupported integer width: ${payload.length}`);
97
+ }
98
+ }
99
+ emitPushBytes(value) {
100
+ const length = value.length;
101
+ if (length < 0x100) {
102
+ return this.emit(OpCode.PUSHDATA1, concatBytes(Uint8Array.of(length), value));
103
+ }
104
+ if (length < 0x10000) {
105
+ return this.emit(OpCode.PUSHDATA2, concatBytes(Uint8Array.of(length & 0xff, (length >> 8) & 0xff), value));
106
+ }
107
+ if (length < 0x100000000) {
108
+ return this.emit(OpCode.PUSHDATA4, concatBytes(Uint8Array.of(length & 0xff, (length >> 8) & 0xff, (length >> 16) & 0xff, (length >> 24) & 0xff), value));
109
+ }
110
+ throw new Error(`push too large bytes: ${length}`);
111
+ }
112
+ emitPushArray(items) {
113
+ if (items.length === 0) {
114
+ return this.emit(OpCode.NEWARRAY0);
115
+ }
116
+ for (const item of [...items].reverse()) {
117
+ this.emitPush(item);
118
+ }
119
+ this.emitPushInt(BigInt(items.length));
120
+ return this.emit(OpCode.PACK);
121
+ }
122
+ emitContractCall(contractHash, method, callFlags = CallFlags.NONE, args = []) {
123
+ this.emitPushArray(args);
124
+ this.emitPushInt(BigInt(callFlags));
125
+ this.emitPush(method);
126
+ this.emitPush(typeof contractHash === "string" ? new H160(contractHash) : contractHash);
127
+ return this.emitSyscall("System.Contract.Call");
128
+ }
129
+ emitSyscall(syscall, args = []) {
130
+ for (const arg of [...args].reverse()) {
131
+ this.emitPush(arg);
132
+ }
133
+ this.emit(OpCode.SYSCALL);
134
+ const code = typeof syscall === "number" ? syscall : syscallCode(syscall);
135
+ this.script.push(code & 0xff, (code >> 8) & 0xff, (code >> 16) & 0xff, (code >> 24) & 0xff);
136
+ return this;
137
+ }
138
+ toBytes() {
139
+ return Uint8Array.from(this.script);
140
+ }
141
+ toHex() {
142
+ return bytesToHex(this.toBytes());
143
+ }
144
+ }
@@ -0,0 +1,40 @@
1
+ import { BytesLike } from "../internal/bytes.js";
2
+ type Marshaled = {
3
+ marshalTo(writer: BinaryWriter): void;
4
+ };
5
+ type Unmarshalable<T> = {
6
+ unmarshalFrom(reader: BinaryReader): T;
7
+ };
8
+ export declare class BinaryWriter {
9
+ private readonly chunks;
10
+ write(data: BytesLike): void;
11
+ writeVarBytes(data: BytesLike): void;
12
+ writeVarString(value: string): void;
13
+ writeBool(value: boolean): void;
14
+ writeUInt8(value: number): void;
15
+ writeUInt16LE(value: number): void;
16
+ writeUInt32LE(value: number): void;
17
+ writeUInt64LE(value: bigint | number): void;
18
+ writeVarInt(value: bigint | number): void;
19
+ writeMultiple(items: Marshaled[]): void;
20
+ toBytes(): Uint8Array;
21
+ }
22
+ export declare class BinaryReader {
23
+ private readonly data;
24
+ private index;
25
+ constructor(data: Uint8Array);
26
+ remaining(): number;
27
+ read(length: number): Uint8Array;
28
+ readVarInt(): bigint;
29
+ readVarBytes(): Uint8Array;
30
+ readVarString(): string;
31
+ readBool(): boolean;
32
+ readUInt8(): number;
33
+ readUInt16LE(): number;
34
+ readUInt32LE(): number;
35
+ readUInt64LE(): bigint;
36
+ readMultiple<T>(type: Unmarshalable<T>): T[];
37
+ }
38
+ export declare function serialize(value: Marshaled): Uint8Array;
39
+ export declare function deserialize<T>(data: BytesLike, type: Unmarshalable<T>): T;
40
+ export {};
@@ -0,0 +1,175 @@
1
+ import { concatBytes, toUint8Array } from "../internal/bytes.js";
2
+ export class BinaryWriter {
3
+ chunks = [];
4
+ write(data) {
5
+ this.chunks.push(toUint8Array(data));
6
+ }
7
+ writeVarBytes(data) {
8
+ const bytes = toUint8Array(data);
9
+ this.writeVarInt(bytes.length);
10
+ this.write(bytes);
11
+ }
12
+ writeVarString(value) {
13
+ this.writeVarBytes(new TextEncoder().encode(value));
14
+ }
15
+ writeBool(value) {
16
+ this.writeUInt8(value ? 1 : 0);
17
+ }
18
+ writeUInt8(value) {
19
+ if (!Number.isInteger(value) || value < 0 || value > 0xff) {
20
+ throw new Error("uint8 must be between 0 and 255");
21
+ }
22
+ this.write([value]);
23
+ }
24
+ writeUInt16LE(value) {
25
+ if (!Number.isInteger(value) || value < 0 || value > 0xffff) {
26
+ throw new Error("uint16 must be between 0 and 65535");
27
+ }
28
+ const out = new Uint8Array(2);
29
+ out[0] = value & 0xff;
30
+ out[1] = (value >> 8) & 0xff;
31
+ this.write(out);
32
+ }
33
+ writeUInt32LE(value) {
34
+ if (!Number.isInteger(value) || value < 0 || value > 0xffffffff) {
35
+ throw new Error("uint32 must be between 0 and 4294967295");
36
+ }
37
+ const out = new Uint8Array(4);
38
+ out[0] = value & 0xff;
39
+ out[1] = (value >> 8) & 0xff;
40
+ out[2] = (value >> 16) & 0xff;
41
+ out[3] = (value >> 24) & 0xff;
42
+ this.write(out);
43
+ }
44
+ writeUInt64LE(value) {
45
+ const big = BigInt(value);
46
+ if (big < 0n || big > 0xffffffffffffffffn) {
47
+ throw new Error("uint64 must be between 0 and 2^64-1");
48
+ }
49
+ const out = new Uint8Array(8);
50
+ let remaining = big;
51
+ for (let index = 0; index < 8; index += 1) {
52
+ out[index] = Number(remaining & 0xffn);
53
+ remaining >>= 8n;
54
+ }
55
+ this.write(out);
56
+ }
57
+ writeVarInt(value) {
58
+ const big = BigInt(value);
59
+ if (big < 0n) {
60
+ throw new Error("var int cannot be negative");
61
+ }
62
+ if (big < 0xfdn) {
63
+ this.writeUInt8(Number(big));
64
+ return;
65
+ }
66
+ if (big <= 0xffffn) {
67
+ this.writeUInt8(0xfd);
68
+ this.writeUInt16LE(Number(big));
69
+ return;
70
+ }
71
+ if (big <= 0xffffffffn) {
72
+ this.writeUInt8(0xfe);
73
+ this.writeUInt32LE(Number(big));
74
+ return;
75
+ }
76
+ if (big <= 0xffffffffffffffffn) {
77
+ this.writeUInt8(0xff);
78
+ this.writeUInt64LE(big);
79
+ return;
80
+ }
81
+ throw new Error("var int exceeds 8 bytes");
82
+ }
83
+ writeMultiple(items) {
84
+ this.writeVarInt(items.length);
85
+ for (const item of items) {
86
+ item.marshalTo(this);
87
+ }
88
+ }
89
+ toBytes() {
90
+ return concatBytes(...this.chunks);
91
+ }
92
+ }
93
+ export class BinaryReader {
94
+ data;
95
+ index = 0;
96
+ constructor(data) {
97
+ this.data = data;
98
+ }
99
+ remaining() {
100
+ return this.data.length - this.index;
101
+ }
102
+ read(length) {
103
+ if (this.index + length > this.data.length) {
104
+ throw new Error(`unexpected EOF: expected ${length} bytes, ${this.remaining()} available`);
105
+ }
106
+ const out = this.data.slice(this.index, this.index + length);
107
+ this.index += length;
108
+ return out;
109
+ }
110
+ readVarInt() {
111
+ const tag = this.readUInt8();
112
+ if (tag < 0xfd) {
113
+ return BigInt(tag);
114
+ }
115
+ if (tag === 0xfd) {
116
+ return BigInt(this.readUInt16LE());
117
+ }
118
+ if (tag === 0xfe) {
119
+ return BigInt(this.readUInt32LE());
120
+ }
121
+ return this.readUInt64LE();
122
+ }
123
+ readVarBytes() {
124
+ return this.read(Number(this.readVarInt()));
125
+ }
126
+ readVarString() {
127
+ return new TextDecoder().decode(this.readVarBytes());
128
+ }
129
+ readBool() {
130
+ const value = this.readUInt8();
131
+ if (value !== 0 && value !== 1) {
132
+ throw new Error("serialized bool must be 0 or 1");
133
+ }
134
+ return value === 1;
135
+ }
136
+ readUInt8() {
137
+ return this.read(1)[0];
138
+ }
139
+ readUInt16LE() {
140
+ const bytes = this.read(2);
141
+ return bytes[0] | (bytes[1] << 8);
142
+ }
143
+ readUInt32LE() {
144
+ const bytes = this.read(4);
145
+ return bytes[0] + bytes[1] * 0x100 + bytes[2] * 0x10000 + bytes[3] * 0x1000000;
146
+ }
147
+ readUInt64LE() {
148
+ const bytes = this.read(8);
149
+ let result = 0n;
150
+ for (let index = 7; index >= 0; index -= 1) {
151
+ result = (result << 8n) | BigInt(bytes[index]);
152
+ }
153
+ return result;
154
+ }
155
+ readMultiple(type) {
156
+ const count = Number(this.readVarInt());
157
+ if (count > this.remaining()) {
158
+ throw new Error(`readMultiple: count ${count} exceeds remaining ${this.remaining()} bytes`);
159
+ }
160
+ const out = [];
161
+ for (let index = 0; index < count; index += 1) {
162
+ out.push(type.unmarshalFrom(this));
163
+ }
164
+ return out;
165
+ }
166
+ }
167
+ export function serialize(value) {
168
+ const writer = new BinaryWriter();
169
+ value.marshalTo(writer);
170
+ return writer.toBytes();
171
+ }
172
+ export function deserialize(data, type) {
173
+ const reader = new BinaryReader(toUint8Array(data));
174
+ return type.unmarshalFrom(reader);
175
+ }
@@ -0,0 +1,147 @@
1
+ import { H160, H256 } from "./hash.js";
2
+ import { PublicKey } from "./keypair.js";
3
+ import { BinaryReader, BinaryWriter } from "./serializing.js";
4
+ import { Witness, WitnessScope } from "./witness.js";
5
+ import { WitnessRule, type WitnessRuleJson } from "./witness-rule.js";
6
+ export interface SignerJson {
7
+ account: string;
8
+ scopes: string;
9
+ allowedcontracts: string[];
10
+ allowedgroups: string[];
11
+ rules: WitnessRuleJson[];
12
+ }
13
+ export interface BaseTxAttributeJson {
14
+ type: string;
15
+ }
16
+ export interface HighPriorityAttributeJson {
17
+ type: "HighPriority";
18
+ }
19
+ export interface OracleResponseAttributeJson {
20
+ type: "OracleResponse";
21
+ id: string;
22
+ code: string;
23
+ result: string;
24
+ }
25
+ export interface NotValidBeforeAttributeJson {
26
+ type: "NotValidBefore";
27
+ height: number;
28
+ }
29
+ export interface ConflictsAttributeJson {
30
+ type: "Conflicts";
31
+ hash: string;
32
+ }
33
+ export interface NotaryAssistedAttributeJson {
34
+ type: "NotaryAssisted";
35
+ nkeys: number;
36
+ }
37
+ export type TxAttributeJson = HighPriorityAttributeJson | OracleResponseAttributeJson | NotValidBeforeAttributeJson | ConflictsAttributeJson | NotaryAssistedAttributeJson;
38
+ export declare class Signer {
39
+ readonly account: H160;
40
+ readonly scopes: WitnessScope;
41
+ readonly allowedContracts: H160[];
42
+ readonly allowedGroups: PublicKey[];
43
+ readonly rules: WitnessRule[];
44
+ constructor({ account, scopes, allowedContracts, allowedGroups, rules, }: {
45
+ account: H160 | string;
46
+ scopes: WitnessScope;
47
+ allowedContracts?: (H160 | string)[];
48
+ allowedGroups?: (PublicKey | string)[];
49
+ rules?: WitnessRule[];
50
+ });
51
+ toJSON(): SignerJson;
52
+ marshalTo(writer: BinaryWriter): void;
53
+ static unmarshalFrom(reader: BinaryReader): Signer;
54
+ }
55
+ export declare enum TxAttributeType {
56
+ HighPriority = 1,
57
+ OracleResponse = 17,
58
+ NotValidBefore = 32,
59
+ Conflicts = 33,
60
+ NotaryAssisted = 34
61
+ }
62
+ export declare enum OracleResponseCode {
63
+ Success = 0,
64
+ ProtocolNotSupported = 16,
65
+ ConsensusUnreachable = 18,
66
+ NotFound = 20,
67
+ Timeout = 22,
68
+ Forbidden = 24,
69
+ ResponseTooLarge = 26,
70
+ InsufficientFunds = 28,
71
+ ContentTypeNotSupported = 31,
72
+ Error = 255
73
+ }
74
+ export declare class TxAttribute {
75
+ readonly type: TxAttributeType;
76
+ constructor(type: TxAttributeType);
77
+ marshalTo(writer: BinaryWriter): void;
78
+ static unmarshalFrom(reader: BinaryReader): TxAttribute;
79
+ toJSON(): TxAttributeJson;
80
+ }
81
+ export declare class HighPriorityAttribute extends TxAttribute {
82
+ constructor();
83
+ toJSON(): HighPriorityAttributeJson;
84
+ }
85
+ export declare class OracleResponseAttribute extends TxAttribute {
86
+ readonly id: bigint;
87
+ readonly code: OracleResponseCode;
88
+ readonly result: Uint8Array;
89
+ constructor(id: bigint, code: OracleResponseCode, result: Uint8Array);
90
+ marshalTo(writer: BinaryWriter): void;
91
+ toJSON(): OracleResponseAttributeJson;
92
+ }
93
+ export declare class NotValidBeforeAttribute extends TxAttribute {
94
+ readonly height: number;
95
+ constructor(height: number);
96
+ marshalTo(writer: BinaryWriter): void;
97
+ toJSON(): NotValidBeforeAttributeJson;
98
+ }
99
+ export declare class ConflictsAttribute extends TxAttribute {
100
+ readonly hash: H256;
101
+ constructor(hash: H256);
102
+ marshalTo(writer: BinaryWriter): void;
103
+ toJSON(): ConflictsAttributeJson;
104
+ }
105
+ export declare class NotaryAssistedAttribute extends TxAttribute {
106
+ readonly nKeys: number;
107
+ constructor(nKeys: number);
108
+ marshalTo(writer: BinaryWriter): void;
109
+ toJSON(): NotaryAssistedAttributeJson;
110
+ }
111
+ export declare class Tx {
112
+ readonly version = 0;
113
+ nonce: number;
114
+ systemFee: bigint;
115
+ networkFee: bigint;
116
+ validUntilBlock: number;
117
+ script: Uint8Array;
118
+ signers: Signer[];
119
+ attributes: TxAttribute[];
120
+ witnesses: Witness[];
121
+ constructor({ nonce, systemFee, networkFee, validUntilBlock, script, signers, witnesses, attributes, }: {
122
+ nonce: number;
123
+ systemFee: bigint | number;
124
+ networkFee: bigint | number;
125
+ validUntilBlock: number;
126
+ script: Uint8Array;
127
+ signers?: Signer[];
128
+ witnesses?: Witness[];
129
+ attributes?: TxAttribute[];
130
+ });
131
+ getSignData(networkId: number): Uint8Array;
132
+ marshalUnsignedTo(writer: BinaryWriter): void;
133
+ marshalTo(writer: BinaryWriter): void;
134
+ static unmarshalFrom(reader: BinaryReader): Tx;
135
+ toBytes(): Uint8Array;
136
+ toJSON(): {
137
+ version: number;
138
+ nonce: number;
139
+ sysfee: string;
140
+ netfee: string;
141
+ validuntilblock: number;
142
+ script: string;
143
+ signers: SignerJson[];
144
+ attributes: TxAttributeJson[];
145
+ witnesses: ReturnType<Witness["toJSON"]>[];
146
+ };
147
+ }