@alephium/web3 0.9.0-rc.0 → 0.9.0-rc.2

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 (73) hide show
  1. package/dist/alephium-web3.min.js +3 -0
  2. package/dist/alephium-web3.min.js.LICENSE.txt +12 -0
  3. package/dist/alephium-web3.min.js.map +1 -0
  4. package/dist/scripts/check-versions.d.ts +1 -0
  5. package/dist/scripts/check-versions.js +39 -0
  6. package/dist/scripts/header.d.ts +0 -0
  7. package/dist/scripts/header.js +18 -0
  8. package/dist/src/api/api-alephium.d.ts +1717 -0
  9. package/dist/src/api/api-alephium.js +1284 -0
  10. package/dist/src/api/api-explorer.d.ts +923 -0
  11. package/dist/src/api/api-explorer.js +711 -0
  12. package/dist/src/api/explorer-provider.d.ts +20 -0
  13. package/dist/src/api/explorer-provider.js +69 -0
  14. package/dist/src/api/index.d.ts +6 -0
  15. package/dist/src/api/index.js +52 -0
  16. package/dist/src/api/node-provider.d.ts +38 -0
  17. package/dist/src/api/node-provider.js +99 -0
  18. package/dist/src/api/types.d.ts +38 -0
  19. package/dist/src/api/types.js +259 -0
  20. package/dist/src/api/utils.d.ts +12 -0
  21. package/dist/src/api/utils.js +59 -0
  22. package/dist/src/constants.d.ts +6 -0
  23. package/dist/src/constants.js +26 -0
  24. package/dist/src/contract/contract.d.ts +297 -0
  25. package/dist/src/contract/contract.js +1097 -0
  26. package/dist/src/contract/events.d.ts +11 -0
  27. package/dist/src/contract/events.js +65 -0
  28. package/dist/src/contract/index.d.ts +3 -0
  29. package/dist/src/contract/index.js +36 -0
  30. package/dist/src/contract/ralph.d.ts +13 -0
  31. package/dist/src/contract/ralph.js +397 -0
  32. package/dist/src/global.d.ts +7 -0
  33. package/dist/src/global.js +54 -0
  34. package/dist/src/index.d.ts +9 -0
  35. package/dist/src/index.js +58 -0
  36. package/dist/src/signer/index.d.ts +3 -0
  37. package/dist/src/signer/index.js +36 -0
  38. package/dist/src/signer/signer.d.ts +60 -0
  39. package/dist/src/signer/signer.js +236 -0
  40. package/dist/src/signer/tx-builder.d.ts +12 -0
  41. package/dist/src/signer/tx-builder.js +96 -0
  42. package/dist/src/signer/types.d.ts +115 -0
  43. package/dist/src/signer/types.js +30 -0
  44. package/dist/src/token/index.d.ts +1 -0
  45. package/dist/src/token/index.js +34 -0
  46. package/dist/src/token/nft.d.ts +10 -0
  47. package/dist/src/token/nft.js +19 -0
  48. package/dist/src/transaction/index.d.ts +2 -0
  49. package/dist/src/transaction/index.js +35 -0
  50. package/dist/src/transaction/sign-verify.d.ts +3 -0
  51. package/dist/src/transaction/sign-verify.js +52 -0
  52. package/dist/src/transaction/status.d.ts +11 -0
  53. package/dist/src/transaction/status.js +49 -0
  54. package/dist/src/utils/address.d.ts +1 -0
  55. package/dist/src/utils/address.js +42 -0
  56. package/dist/src/utils/bs58.d.ts +5 -0
  57. package/dist/src/utils/bs58.js +40 -0
  58. package/dist/src/utils/djb2.d.ts +1 -0
  59. package/dist/src/utils/djb2.js +27 -0
  60. package/dist/src/utils/index.d.ts +7 -0
  61. package/dist/src/utils/index.js +40 -0
  62. package/dist/src/utils/number.d.ts +18 -0
  63. package/dist/src/utils/number.fixture.d.ts +12 -0
  64. package/dist/src/utils/number.fixture.js +189 -0
  65. package/dist/src/utils/number.js +149 -0
  66. package/dist/src/utils/sign.d.ts +3 -0
  67. package/dist/src/utils/sign.js +89 -0
  68. package/dist/src/utils/subscription.d.ts +21 -0
  69. package/dist/src/utils/subscription.js +51 -0
  70. package/dist/src/utils/utils.d.ts +40 -0
  71. package/dist/src/utils/utils.js +244 -0
  72. package/package.json +1 -1
  73. package/LICENSE +0 -165
@@ -0,0 +1,11 @@
1
+ import { node } from '../api';
2
+ import { Subscription, SubscribeOptions } from '../utils';
3
+ export declare class EventSubscription extends Subscription<node.ContractEvent> {
4
+ readonly contractAddress: string;
5
+ private fromCount;
6
+ constructor(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number);
7
+ startPolling(): void;
8
+ currentEventCount(): number;
9
+ polling(): Promise<void>;
10
+ }
11
+ export declare function subscribeToEvents(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number): EventSubscription;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.subscribeToEvents = exports.EventSubscription = void 0;
21
+ const __1 = require("..");
22
+ const utils_1 = require("../utils");
23
+ class EventSubscription extends utils_1.Subscription {
24
+ constructor(options, contractAddress, fromCount) {
25
+ super(options);
26
+ this.contractAddress = contractAddress;
27
+ this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount;
28
+ this.startPolling();
29
+ }
30
+ startPolling() {
31
+ this.eventEmitter.on('tick', async () => {
32
+ await this.polling();
33
+ });
34
+ this.eventEmitter.emit('tick');
35
+ }
36
+ currentEventCount() {
37
+ return this.fromCount;
38
+ }
39
+ async polling() {
40
+ try {
41
+ const events = await __1.web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, {
42
+ start: this.fromCount
43
+ });
44
+ if (this.cancelled) {
45
+ return;
46
+ }
47
+ if (this.fromCount === events.nextStart) {
48
+ this.task = setTimeout(() => this.eventEmitter.emit('tick'), this.pollingInterval);
49
+ return;
50
+ }
51
+ const promises = events.events.map((event) => this.messageCallback(event));
52
+ await Promise.all(promises);
53
+ this.fromCount = events.nextStart;
54
+ await this.polling();
55
+ }
56
+ catch (err) {
57
+ await this.errorCallback(err, this);
58
+ }
59
+ }
60
+ }
61
+ exports.EventSubscription = EventSubscription;
62
+ function subscribeToEvents(options, contractAddress, fromCount) {
63
+ return new EventSubscription(options, contractAddress, fromCount);
64
+ }
65
+ exports.subscribeToEvents = subscribeToEvents;
@@ -0,0 +1,3 @@
1
+ export * from './ralph';
2
+ export * from './contract';
3
+ export * from './events';
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ var desc = Object.getOwnPropertyDescriptor(m, k);
22
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
+ desc = { enumerable: true, get: function() { return m[k]; } };
24
+ }
25
+ Object.defineProperty(o, k2, desc);
26
+ }) : (function(o, m, k, k2) {
27
+ if (k2 === undefined) k2 = k;
28
+ o[k2] = m[k];
29
+ }));
30
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
31
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ __exportStar(require("./ralph"), exports);
35
+ __exportStar(require("./contract"), exports);
36
+ __exportStar(require("./events"), exports);
@@ -0,0 +1,13 @@
1
+ import { Val } from '../api';
2
+ import { Fields, FieldsSig } from './contract';
3
+ export declare function encodeBool(bool: boolean): Uint8Array;
4
+ export declare function encodeI256(i256: bigint): Uint8Array;
5
+ export declare function encodeU256(u256: bigint): Uint8Array;
6
+ export declare function encodeByteVec(bytes: string): Uint8Array;
7
+ export declare function encodeAddress(address: string): Uint8Array;
8
+ export declare function encodeScriptFieldAsString(tpe: string, value: Val): string;
9
+ export declare function encodeScriptField(tpe: string, value: Val): Uint8Array;
10
+ export declare function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig): string;
11
+ export declare function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig): string;
12
+ export declare function encodeContractField(tpe: string, value: Val): Uint8Array[];
13
+ export declare function buildDebugBytecode(bytecode: string, bytecodePatch: string): string;
@@ -0,0 +1,397 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ var _a;
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.buildScriptByteCode = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodeAddress = exports.encodeByteVec = exports.encodeU256 = exports.encodeI256 = exports.encodeBool = void 0;
22
+ const buffer_1 = require("buffer/");
23
+ const utils_1 = require("../utils");
24
+ const bigIntZero = BigInt(0);
25
+ class UnSigned {
26
+ }
27
+ UnSigned.oneByteBound = BigInt(0x40);
28
+ UnSigned.twoByteBound = UnSigned.oneByteBound << BigInt(8);
29
+ UnSigned.fourByteBound = UnSigned.oneByteBound << BigInt(8 * 3);
30
+ UnSigned.u256UpperBound = BigInt(1) << BigInt(256);
31
+ class Signed {
32
+ }
33
+ _a = Signed;
34
+ Signed.oneByteBound = BigInt(0x20);
35
+ Signed.twoByteBound = Signed.oneByteBound << BigInt(8);
36
+ Signed.fourByteBound = Signed.oneByteBound << BigInt(8 * 3);
37
+ Signed.i256UpperBound = BigInt(1) << BigInt(255);
38
+ Signed.i256LowerBound = -_a.i256UpperBound;
39
+ class CompactInt {
40
+ }
41
+ CompactInt.oneBytePrefix = 0x00;
42
+ CompactInt.oneByteNegPrefix = 0xc0;
43
+ CompactInt.twoBytePrefix = 0x40;
44
+ CompactInt.twoByteNegPrefix = 0x80;
45
+ CompactInt.fourBytePrefix = 0x80;
46
+ CompactInt.fourByteNegPrefix = 0x40;
47
+ CompactInt.multiBytePrefix = 0xc0;
48
+ function encodeBool(bool) {
49
+ return bool ? Uint8Array.from([1]) : Uint8Array.from([0]);
50
+ }
51
+ exports.encodeBool = encodeBool;
52
+ function encodeI256(i256) {
53
+ if (i256 >= bigIntZero) {
54
+ return encodeI256Positive(i256);
55
+ }
56
+ else {
57
+ return encodeI256Negative(i256);
58
+ }
59
+ }
60
+ exports.encodeI256 = encodeI256;
61
+ // n should be positive
62
+ function toByteArray(n, signed, notBit) {
63
+ let hex = n.toString(16);
64
+ if (hex.length % 2 === 1) {
65
+ hex = '0' + hex;
66
+ }
67
+ else if (signed && hex[0] >= '8') {
68
+ hex = '00' + hex; // add the byte for sign
69
+ }
70
+ const byteLength = hex.length / 2;
71
+ const bytes = new Uint8Array(byteLength + 1);
72
+ for (let index = 0; index < byteLength; index++) {
73
+ const offset = index * 2;
74
+ const byte = parseInt(hex.slice(offset, offset + 2), 16);
75
+ bytes[`${index + 1}`] = notBit ? ~byte : byte;
76
+ }
77
+ const header = byteLength - 4 + CompactInt.multiBytePrefix;
78
+ bytes[0] = header;
79
+ return bytes;
80
+ }
81
+ function encodeI256Positive(i256) {
82
+ if (i256 < Signed.oneByteBound) {
83
+ return new Uint8Array([Number(i256) + CompactInt.oneBytePrefix]);
84
+ }
85
+ else if (i256 < Signed.twoByteBound) {
86
+ const num = Number(i256);
87
+ return new Uint8Array([(num >> 8) + CompactInt.twoBytePrefix, num & 0xff]);
88
+ }
89
+ else if (i256 < Signed.fourByteBound) {
90
+ const num = Number(i256);
91
+ return new Uint8Array([(num >> 24) + CompactInt.fourBytePrefix, (num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff]);
92
+ }
93
+ else if (i256 < Signed.i256UpperBound) {
94
+ return toByteArray(i256, true, false);
95
+ }
96
+ else {
97
+ throw Error(`Too large number for i256: ${i256}`);
98
+ }
99
+ }
100
+ function encodeI256Negative(i256) {
101
+ if (i256 >= -Signed.oneByteBound) {
102
+ const num = Number(i256);
103
+ return new Uint8Array([(num ^ CompactInt.oneByteNegPrefix) & 0xff]);
104
+ }
105
+ else if (i256 >= -Signed.twoByteBound) {
106
+ const num = Number(i256);
107
+ return new Uint8Array([((num >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, num & 0xff]);
108
+ }
109
+ else if (i256 >= -Signed.fourByteBound) {
110
+ const num = Number(i256);
111
+ return new Uint8Array([
112
+ ((num >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
113
+ (num >> 16) & 0xff,
114
+ (num >> 8) & 0xff,
115
+ num & 0xff
116
+ ]);
117
+ }
118
+ else if (i256 >= Signed.i256LowerBound) {
119
+ return toByteArray(~i256, true, true);
120
+ }
121
+ else {
122
+ throw Error(`Too small number for i256: ${i256}`);
123
+ }
124
+ }
125
+ function encodeU256(u256) {
126
+ if (u256 < bigIntZero) {
127
+ throw Error(`Negative number for U256: ${u256}`);
128
+ }
129
+ else if (u256 < UnSigned.oneByteBound) {
130
+ return new Uint8Array([Number(u256) + CompactInt.oneBytePrefix]);
131
+ }
132
+ else if (u256 < UnSigned.twoByteBound) {
133
+ const num = Number(u256);
134
+ return new Uint8Array([((num >> 8) & 0xff) + CompactInt.twoBytePrefix, num & 0xff]);
135
+ }
136
+ else if (u256 < UnSigned.fourByteBound) {
137
+ const num = Number(u256);
138
+ return new Uint8Array([
139
+ ((num >> 24) & 0xff) + CompactInt.fourBytePrefix,
140
+ (num >> 16) & 0xff,
141
+ (num >> 8) & 0xff,
142
+ num & 0xff
143
+ ]);
144
+ }
145
+ else if (u256 < UnSigned.u256UpperBound) {
146
+ return toByteArray(u256, false, false);
147
+ }
148
+ else {
149
+ throw Error(`Too large number for U256: ${u256}`);
150
+ }
151
+ }
152
+ exports.encodeU256 = encodeU256;
153
+ function encodeByteVec(bytes) {
154
+ if (!(0, utils_1.isHexString)(bytes)) {
155
+ throw Error(`Given value ${bytes} is not a valid hex string`);
156
+ }
157
+ const buffer0 = buffer_1.Buffer.from(bytes, 'hex');
158
+ const buffer1 = buffer_1.Buffer.from(encodeI256(BigInt(buffer0.length)));
159
+ return buffer_1.Buffer.concat([buffer1, buffer0]);
160
+ }
161
+ exports.encodeByteVec = encodeByteVec;
162
+ function encodeAddress(address) {
163
+ return utils_1.bs58.decode(address);
164
+ }
165
+ exports.encodeAddress = encodeAddress;
166
+ function invalidScriptField(tpe, value) {
167
+ return Error(`Invalid script field ${value} for type ${tpe}`);
168
+ }
169
+ var Instruction;
170
+ (function (Instruction) {
171
+ Instruction[Instruction["trueConst"] = 3] = "trueConst";
172
+ Instruction[Instruction["falseConst"] = 4] = "falseConst";
173
+ Instruction[Instruction["i256Const0"] = 5] = "i256Const0";
174
+ Instruction[Instruction["i256Const1"] = 6] = "i256Const1";
175
+ Instruction[Instruction["i256Const2"] = 7] = "i256Const2";
176
+ Instruction[Instruction["i256Const3"] = 8] = "i256Const3";
177
+ Instruction[Instruction["i256Const4"] = 9] = "i256Const4";
178
+ Instruction[Instruction["i256Const5"] = 10] = "i256Const5";
179
+ Instruction[Instruction["i256ConstN1"] = 11] = "i256ConstN1";
180
+ Instruction[Instruction["u256Const0"] = 12] = "u256Const0";
181
+ Instruction[Instruction["u256Const1"] = 13] = "u256Const1";
182
+ Instruction[Instruction["u256Const2"] = 14] = "u256Const2";
183
+ Instruction[Instruction["u256Const3"] = 15] = "u256Const3";
184
+ Instruction[Instruction["u256Const4"] = 16] = "u256Const4";
185
+ Instruction[Instruction["u256Const5"] = 17] = "u256Const5";
186
+ Instruction[Instruction["i256Const"] = 18] = "i256Const";
187
+ Instruction[Instruction["u256Const"] = 19] = "u256Const";
188
+ Instruction[Instruction["bytesConst"] = 20] = "bytesConst";
189
+ Instruction[Instruction["addressConst"] = 21] = "addressConst";
190
+ })(Instruction || (Instruction = {}));
191
+ // TODO: optimize
192
+ function encodeScriptFieldI256(value) {
193
+ return new Uint8Array([Instruction.i256Const, ...encodeI256(value)]);
194
+ }
195
+ // TODO: optimize
196
+ function encodeScriptFieldU256(value) {
197
+ return new Uint8Array([Instruction.u256Const, ...encodeU256(value)]);
198
+ }
199
+ function encodeScriptFieldAsString(tpe, value) {
200
+ return buffer_1.Buffer.from(encodeScriptField(tpe, value)).toString('hex');
201
+ }
202
+ exports.encodeScriptFieldAsString = encodeScriptFieldAsString;
203
+ function encodeScriptField(tpe, value) {
204
+ switch (tpe) {
205
+ case 'Bool':
206
+ if (typeof value === 'boolean') {
207
+ const byte = value ? Instruction.trueConst : Instruction.falseConst;
208
+ return new Uint8Array([byte]);
209
+ }
210
+ break;
211
+ case 'I256':
212
+ if (typeof value === 'number' && Number.isInteger(value)) {
213
+ return encodeScriptFieldI256(BigInt(value));
214
+ }
215
+ else if (typeof value === 'bigint') {
216
+ return encodeScriptFieldI256(value);
217
+ }
218
+ break;
219
+ case 'U256':
220
+ if (typeof value === 'number' && Number.isInteger(value)) {
221
+ return encodeScriptFieldU256(BigInt(value));
222
+ }
223
+ else if (typeof value === 'bigint') {
224
+ return encodeScriptFieldU256(value);
225
+ }
226
+ break;
227
+ case 'ByteVec':
228
+ if (typeof value === 'string') {
229
+ return new Uint8Array([Instruction.bytesConst, ...encodeByteVec(value)]);
230
+ }
231
+ break;
232
+ case 'Address':
233
+ if (typeof value === 'string') {
234
+ return new Uint8Array([Instruction.addressConst, ...encodeAddress(value)]);
235
+ }
236
+ break;
237
+ }
238
+ throw invalidScriptField(tpe, value);
239
+ }
240
+ exports.encodeScriptField = encodeScriptField;
241
+ const scriptFieldRegex = /\{([0-9]*)\}/g;
242
+ function buildScriptByteCode(bytecodeTemplate, fields, fieldsSig) {
243
+ return bytecodeTemplate.replace(scriptFieldRegex, (_, fieldIndex) => {
244
+ const fieldName = fieldsSig.names[`${fieldIndex}`];
245
+ const fieldType = fieldsSig.types[`${fieldIndex}`];
246
+ if (fieldName in fields) {
247
+ const fieldValue = fields[`${fieldName}`];
248
+ return encodeScriptFieldAsString(fieldType, fieldValue);
249
+ }
250
+ else {
251
+ throw new Error(`The value of field ${fieldName} is not provided`);
252
+ }
253
+ });
254
+ }
255
+ exports.buildScriptByteCode = buildScriptByteCode;
256
+ function encodeFields(fields, fieldsSig, mutable) {
257
+ const fieldIndexes = fieldsSig.isMutable
258
+ .map((_, index) => index)
259
+ .filter((index) => fieldsSig.isMutable[`${index}`] === mutable);
260
+ const fieldsEncoded = fieldIndexes.flatMap((fieldIndex) => {
261
+ const fieldName = fieldsSig.names[`${fieldIndex}`];
262
+ const fieldType = fieldsSig.types[`${fieldIndex}`];
263
+ if (fieldName in fields) {
264
+ const fieldValue = fields[`${fieldName}`];
265
+ return encodeContractField(fieldType, fieldValue);
266
+ }
267
+ else {
268
+ throw new Error(`The value of field ${fieldName} is not provided`);
269
+ }
270
+ });
271
+ const fieldsLength = buffer_1.Buffer.from(encodeI256(BigInt(fieldsEncoded.length))).toString('hex');
272
+ return fieldsLength + fieldsEncoded.map((f) => buffer_1.Buffer.from(f).toString('hex')).join('');
273
+ }
274
+ function buildContractByteCode(bytecode, fields, fieldsSig) {
275
+ const encodedImmFields = encodeFields(fields, fieldsSig, false);
276
+ const encodedMutFields = encodeFields(fields, fieldsSig, true);
277
+ return bytecode + encodedImmFields + encodedMutFields;
278
+ }
279
+ exports.buildContractByteCode = buildContractByteCode;
280
+ var ApiValType;
281
+ (function (ApiValType) {
282
+ ApiValType[ApiValType["Bool"] = 0] = "Bool";
283
+ ApiValType[ApiValType["I256"] = 1] = "I256";
284
+ ApiValType[ApiValType["U256"] = 2] = "U256";
285
+ ApiValType[ApiValType["ByteVec"] = 3] = "ByteVec";
286
+ ApiValType[ApiValType["Address"] = 4] = "Address";
287
+ })(ApiValType || (ApiValType = {}));
288
+ function encodeContractFieldI256(value) {
289
+ return new Uint8Array([ApiValType.I256, ...encodeI256(value)]);
290
+ }
291
+ function encodeContractFieldU256(value) {
292
+ return new Uint8Array([ApiValType.U256, ...encodeU256(value)]);
293
+ }
294
+ function encodeContractFieldArray(tpe, val) {
295
+ if (!Array.isArray(val)) {
296
+ throw new Error(`Expected array, got ${val}`);
297
+ }
298
+ const semiColonIndex = tpe.lastIndexOf(';');
299
+ if (semiColonIndex == -1) {
300
+ throw new Error(`Invalid Array type: ${tpe}`);
301
+ }
302
+ const subType = tpe.slice(1, semiColonIndex);
303
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
304
+ if (val.length != dim) {
305
+ throw new Error(`Invalid val dimension: ${val}`);
306
+ }
307
+ else {
308
+ return val.flatMap((v) => encodeContractField(subType, v));
309
+ }
310
+ }
311
+ function encodeContractField(tpe, value) {
312
+ switch (tpe) {
313
+ case 'Bool':
314
+ if (typeof value === 'boolean') {
315
+ const byte = value ? 1 : 0;
316
+ return [new Uint8Array([ApiValType.Bool, byte])];
317
+ }
318
+ break;
319
+ case 'I256':
320
+ if (typeof value === 'number' && Number.isInteger(value)) {
321
+ return [encodeContractFieldI256(BigInt(value))];
322
+ }
323
+ else if (typeof value === 'bigint') {
324
+ return [encodeContractFieldI256(value)];
325
+ }
326
+ break;
327
+ case 'U256':
328
+ if (typeof value === 'number' && Number.isInteger(value)) {
329
+ return [encodeContractFieldU256(BigInt(value))];
330
+ }
331
+ else if (typeof value === 'bigint') {
332
+ return [encodeContractFieldU256(value)];
333
+ }
334
+ break;
335
+ case 'ByteVec':
336
+ if (typeof value === 'string') {
337
+ return [new Uint8Array([ApiValType.ByteVec, ...encodeByteVec(value)])];
338
+ }
339
+ break;
340
+ case 'Address':
341
+ if (typeof value === 'string') {
342
+ return [new Uint8Array([ApiValType.Address, ...encodeAddress(value)])];
343
+ }
344
+ break;
345
+ default:
346
+ // Array type
347
+ return encodeContractFieldArray(tpe, value);
348
+ }
349
+ throw invalidVal(tpe, value);
350
+ }
351
+ exports.encodeContractField = encodeContractField;
352
+ function invalidVal(tpe, value) {
353
+ return Error(`Invalid API value ${value} for type ${tpe}`);
354
+ }
355
+ function buildDebugBytecode(bytecode, bytecodePatch) {
356
+ if (bytecodePatch === '') {
357
+ return bytecode;
358
+ }
359
+ const pattern = /[=+-][0-9a-f]*/g;
360
+ let result = '';
361
+ let index = 0;
362
+ for (const parts of bytecodePatch.matchAll(pattern)) {
363
+ const part = parts[0];
364
+ const diffType = part[0];
365
+ if (diffType === '=') {
366
+ const length = parseInt(part.substring(1));
367
+ result = result + bytecode.slice(index, index + length);
368
+ index = index + length;
369
+ }
370
+ else if (diffType === '+') {
371
+ result = result + part.substring(1);
372
+ }
373
+ else {
374
+ const length = parseInt(part.substring(1));
375
+ index = index + length;
376
+ }
377
+ }
378
+ return result;
379
+ }
380
+ exports.buildDebugBytecode = buildDebugBytecode;
381
+ // export function buildContractByteCode(
382
+ // compiled: node.TemplateContractByteCode,
383
+ // templateVariables: TemplateVariables
384
+ // ): string {
385
+ // const methodsBuilt = compiled.methodsByteCode.map((template) => buildByteCode(template, templateVariables))
386
+ // let count = 0
387
+ // const methodIndexes = methodsBuilt.map((hex) => {
388
+ // count += hex.length / 2
389
+ // return count
390
+ // })
391
+ // return (
392
+ // binToHex(encodeI256(BigInt(compiled.filedLength))) +
393
+ // binToHex(encodeI256(BigInt(methodIndexes.length))) +
394
+ // methodIndexes.map((index) => binToHex(encodeI256(BigInt(index)))).join('') +
395
+ // methodsBuilt.join('')
396
+ // )
397
+ // }
@@ -0,0 +1,7 @@
1
+ import { ExplorerProvider, NodeProvider } from './api';
2
+ export declare function setCurrentNodeProvider(provider: NodeProvider): void;
3
+ export declare function setCurrentNodeProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void;
4
+ export declare function getCurrentNodeProvider(): NodeProvider;
5
+ export declare function setCurrentExplorerProvider(provider: ExplorerProvider): void;
6
+ export declare function setCurrentExplorerProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void;
7
+ export declare function getCurrentExplorerProvider(): ExplorerProvider | undefined;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.getCurrentExplorerProvider = exports.setCurrentExplorerProvider = exports.getCurrentNodeProvider = exports.setCurrentNodeProvider = void 0;
21
+ const api_1 = require("./api");
22
+ let _currentNodeProvider = undefined;
23
+ function setCurrentNodeProvider(provider, apiKey, customFetch) {
24
+ if (typeof provider == 'string') {
25
+ _currentNodeProvider = new api_1.NodeProvider(provider, apiKey, customFetch);
26
+ }
27
+ else {
28
+ _currentNodeProvider = provider;
29
+ }
30
+ }
31
+ exports.setCurrentNodeProvider = setCurrentNodeProvider;
32
+ function getCurrentNodeProvider() {
33
+ if (typeof _currentNodeProvider === 'undefined') {
34
+ throw Error('No node provider is set.');
35
+ }
36
+ return _currentNodeProvider;
37
+ }
38
+ exports.getCurrentNodeProvider = getCurrentNodeProvider;
39
+ let _currentExplorerProvider = undefined;
40
+ function setCurrentExplorerProvider(provider, apiKey, customFetch) {
41
+ if (typeof provider == 'string') {
42
+ _currentExplorerProvider = new api_1.ExplorerProvider(provider, apiKey, customFetch);
43
+ }
44
+ else {
45
+ _currentExplorerProvider = provider;
46
+ }
47
+ }
48
+ exports.setCurrentExplorerProvider = setCurrentExplorerProvider;
49
+ // Different from `NodeProvider`, this may return `undefined`
50
+ // as ExplorerProvider is not necessary for all applications
51
+ function getCurrentExplorerProvider() {
52
+ return _currentExplorerProvider;
53
+ }
54
+ exports.getCurrentExplorerProvider = getCurrentExplorerProvider;
@@ -0,0 +1,9 @@
1
+ export * from './api';
2
+ export * from './contract';
3
+ export * from './signer';
4
+ export * from './utils';
5
+ export * from './transaction';
6
+ export * from './token';
7
+ export * from './constants';
8
+ export * as web3 from './global';
9
+ export * as utils from './utils';
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ var desc = Object.getOwnPropertyDescriptor(m, k);
22
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
+ desc = { enumerable: true, get: function() { return m[k]; } };
24
+ }
25
+ Object.defineProperty(o, k2, desc);
26
+ }) : (function(o, m, k, k2) {
27
+ if (k2 === undefined) k2 = k;
28
+ o[k2] = m[k];
29
+ }));
30
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
31
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
32
+ }) : function(o, v) {
33
+ o["default"] = v;
34
+ });
35
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
+ };
38
+ var __importStar = (this && this.__importStar) || function (mod) {
39
+ if (mod && mod.__esModule) return mod;
40
+ var result = {};
41
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
42
+ __setModuleDefault(result, mod);
43
+ return result;
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.utils = exports.web3 = void 0;
47
+ BigInt.prototype['toJSON'] = function () {
48
+ return this.toString();
49
+ };
50
+ __exportStar(require("./api"), exports);
51
+ __exportStar(require("./contract"), exports);
52
+ __exportStar(require("./signer"), exports);
53
+ __exportStar(require("./utils"), exports);
54
+ __exportStar(require("./transaction"), exports);
55
+ __exportStar(require("./token"), exports);
56
+ __exportStar(require("./constants"), exports);
57
+ exports.web3 = __importStar(require("./global"));
58
+ exports.utils = __importStar(require("./utils"));
@@ -0,0 +1,3 @@
1
+ export * from './signer';
2
+ export * from './types';
3
+ export * from './tx-builder';