@alephium/web3 0.0.3 → 0.1.0-rc.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/.editorconfig +13 -0
- package/.eslintignore +2 -0
- package/.eslintrc.json +21 -0
- package/README.md +2 -2
- package/contracts/add.ral +7 -3
- package/contracts/greeter.ral +3 -1
- package/contracts/greeter_interface.ral +3 -0
- package/contracts/greeter_main.ral +9 -0
- package/contracts/main.ral +3 -5
- package/dev/user.conf +8 -4
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/scripts/check-versions.d.ts +1 -0
- package/dist/scripts/check-versions.js +39 -0
- package/dist/{cli → scripts}/create-project.d.ts +0 -0
- package/dist/scripts/create-project.js +124 -0
- package/dist/scripts/header.d.ts +0 -0
- package/dist/scripts/header.js +18 -0
- package/dist/scripts/rename-gitignore.d.ts +1 -0
- package/dist/scripts/rename-gitignore.js +24 -0
- package/dist/scripts/start-devnet.d.ts +1 -0
- package/dist/scripts/start-devnet.js +131 -0
- package/dist/scripts/stop-devnet.d.ts +1 -0
- package/dist/scripts/stop-devnet.js +32 -0
- package/dist/{api → src/api}/api-alephium.d.ts +287 -168
- package/dist/{api → src/api}/api-alephium.js +497 -115
- package/dist/{api → src/api}/api-explorer.d.ts +117 -19
- package/dist/{api → src/api}/api-explorer.js +206 -46
- package/dist/src/api/index.d.ts +10 -0
- package/dist/src/api/index.js +55 -0
- package/dist/{lib → src}/constants.d.ts +0 -0
- package/dist/{lib → src}/constants.js +0 -0
- package/dist/src/contract/contract.d.ts +182 -0
- package/dist/src/contract/contract.js +760 -0
- package/dist/src/contract/events.d.ts +29 -0
- package/dist/src/contract/events.js +77 -0
- package/dist/src/contract/index.d.ts +3 -0
- package/dist/src/contract/index.js +32 -0
- package/dist/src/contract/ralph.d.ts +12 -0
- package/dist/src/contract/ralph.js +362 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +34 -0
- package/dist/src/signer/index.d.ts +2 -0
- package/dist/src/signer/index.js +31 -0
- package/dist/src/signer/node-wallet.d.ts +13 -0
- package/dist/src/signer/node-wallet.js +60 -0
- package/dist/src/signer/signer.d.ts +143 -0
- package/dist/src/signer/signer.js +184 -0
- package/dist/src/test/index.d.ts +7 -0
- package/dist/src/test/index.js +41 -0
- package/dist/src/test/privatekey-wallet.d.ts +12 -0
- package/dist/src/test/privatekey-wallet.js +68 -0
- package/dist/{lib → src/utils}/address.d.ts +0 -0
- package/dist/{lib → src/utils}/address.js +1 -1
- package/dist/{lib → src/utils}/bs58.d.ts +2 -2
- package/dist/{lib → src/utils}/bs58.js +3 -1
- package/dist/{lib → src/utils}/djb2.d.ts +0 -0
- package/dist/{lib → src/utils}/djb2.js +1 -1
- package/dist/src/utils/index.d.ts +6 -0
- package/dist/src/utils/index.js +35 -0
- package/dist/{lib → src/utils}/password-crypto.d.ts +0 -0
- package/dist/{lib → src/utils}/password-crypto.js +8 -7
- package/dist/src/utils/transaction.d.ts +2 -0
- package/dist/src/utils/transaction.js +58 -0
- package/dist/src/utils/utils.d.ts +30 -0
- package/dist/{lib → src/utils}/utils.js +83 -19
- package/gitignore +5 -4
- package/package.json +56 -40
- package/scripts/create-project.ts +136 -0
- package/scripts/header.js +17 -0
- package/scripts/start-devnet.js +3 -3
- package/src/api/api-alephium.ts +2323 -0
- package/src/api/api-explorer.ts +808 -0
- package/src/api/index.ts +35 -0
- package/src/constants.ts +20 -0
- package/src/contract/contract.ts +1014 -0
- package/src/contract/events.ts +102 -0
- package/src/contract/index.ts +21 -0
- package/src/contract/ralph.test.ts +178 -0
- package/src/contract/ralph.ts +362 -0
- package/src/fixtures/address.json +36 -0
- package/src/fixtures/balance.json +9 -0
- package/src/fixtures/self-clique.json +19 -0
- package/src/fixtures/transaction.json +13 -0
- package/src/fixtures/transactions.json +179 -0
- package/src/index.ts +24 -0
- package/src/signer/fixtures/genesis.json +26 -0
- package/src/signer/fixtures/wallets.json +26 -0
- package/src/signer/index.ts +20 -0
- package/src/signer/node-wallet.ts +74 -0
- package/src/signer/signer.ts +313 -0
- package/src/test/index.ts +32 -0
- package/src/test/privatekey-wallet.ts +58 -0
- package/src/utils/address.test.ts +47 -0
- package/src/utils/address.ts +39 -0
- package/src/utils/bs58.ts +26 -0
- package/src/utils/djb2.test.ts +35 -0
- package/src/utils/djb2.ts +25 -0
- package/src/utils/index.ts +24 -0
- package/src/utils/password-crypto.test.ts +27 -0
- package/src/utils/password-crypto.ts +77 -0
- package/src/utils/transaction.test.ts +50 -0
- package/src/utils/transaction.ts +39 -0
- package/src/utils/utils.test.ts +160 -0
- package/src/utils/utils.ts +209 -0
- package/templates/{README.md → base/README.md} +4 -4
- package/templates/base/package.json +35 -0
- package/templates/base/src/greeter.ts +41 -0
- package/templates/base/tsconfig.json +19 -0
- package/templates/react/README.md +34 -0
- package/templates/react/config-overrides.js +18 -0
- package/templates/react/package.json +66 -0
- package/templates/react/src/App.tsx +42 -0
- package/templates/react/src/artifacts/greeter.ral.json +26 -0
- package/templates/react/src/artifacts/greeter_main.ral.json +22 -0
- package/templates/shared/.eslintrc.json +12 -0
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +161 -0
- package/test/events.test.ts +139 -0
- package/webpack.config.js +1 -1
- package/contracts/greeter-main.ral +0 -8
- package/dist/cli/create-project.js +0 -87
- package/dist/lib/clique.d.ts +0 -23
- package/dist/lib/clique.js +0 -149
- package/dist/lib/contract.d.ts +0 -152
- package/dist/lib/contract.js +0 -711
- package/dist/lib/explorer.d.ts +0 -8
- package/dist/lib/explorer.js +0 -46
- package/dist/lib/index.d.ts +0 -12
- package/dist/lib/index.js +0 -60
- package/dist/lib/node.d.ts +0 -10
- package/dist/lib/node.js +0 -64
- package/dist/lib/numbers.d.ts +0 -7
- package/dist/lib/numbers.js +0 -128
- package/dist/lib/signer.d.ts +0 -17
- package/dist/lib/signer.js +0 -70
- package/dist/lib/storage-browser.d.ts +0 -9
- package/dist/lib/storage-browser.js +0 -52
- package/dist/lib/storage-node.d.ts +0 -9
- package/dist/lib/storage-node.js +0 -65
- package/dist/lib/utils.d.ts +0 -11
- package/dist/lib/wallet.d.ts +0 -30
- package/dist/lib/wallet.js +0 -142
- package/templates/package.json +0 -29
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { node } from '../api';
|
|
2
|
+
import { NodeProvider } from '../api';
|
|
3
|
+
declare type EventCallback = (event: node.ContractEvent) => Promise<void>;
|
|
4
|
+
declare type ErrorCallback = (error: any, subscription: Subscription) => Promise<void>;
|
|
5
|
+
export interface SubscribeOptions {
|
|
6
|
+
provider: NodeProvider;
|
|
7
|
+
contractAddress: string;
|
|
8
|
+
fromCount?: number;
|
|
9
|
+
pollingInterval: number;
|
|
10
|
+
eventCallback: EventCallback;
|
|
11
|
+
errorCallback: ErrorCallback;
|
|
12
|
+
}
|
|
13
|
+
export declare class Subscription {
|
|
14
|
+
provider: NodeProvider;
|
|
15
|
+
readonly contractAddress: string;
|
|
16
|
+
pollingInterval: number;
|
|
17
|
+
private fromCount;
|
|
18
|
+
private eventCallback;
|
|
19
|
+
private errorCallback;
|
|
20
|
+
private task;
|
|
21
|
+
private cancelled;
|
|
22
|
+
private eventEmitter;
|
|
23
|
+
constructor(options: SubscribeOptions);
|
|
24
|
+
unsubscribe(): void;
|
|
25
|
+
currentEventCount(): number;
|
|
26
|
+
private fetchEvents;
|
|
27
|
+
}
|
|
28
|
+
export declare function subscribe(options: SubscribeOptions): Subscription;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
20
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.subscribe = exports.Subscription = void 0;
|
|
24
|
+
const eventemitter3_1 = __importDefault(require("eventemitter3"));
|
|
25
|
+
class Subscription {
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.provider = options.provider;
|
|
28
|
+
this.contractAddress = options.contractAddress;
|
|
29
|
+
this.fromCount = typeof options.fromCount === 'undefined' ? 0 : options.fromCount;
|
|
30
|
+
this.pollingInterval = options.pollingInterval;
|
|
31
|
+
this.eventCallback = options.eventCallback;
|
|
32
|
+
this.errorCallback = options.errorCallback;
|
|
33
|
+
this.task = undefined;
|
|
34
|
+
this.cancelled = false;
|
|
35
|
+
this.eventEmitter = new eventemitter3_1.default();
|
|
36
|
+
this.eventEmitter.on('tick', async () => {
|
|
37
|
+
await this.fetchEvents();
|
|
38
|
+
});
|
|
39
|
+
this.eventEmitter.emit('tick');
|
|
40
|
+
}
|
|
41
|
+
unsubscribe() {
|
|
42
|
+
this.eventEmitter.removeAllListeners();
|
|
43
|
+
this.cancelled = true;
|
|
44
|
+
if (typeof this.task !== 'undefined') {
|
|
45
|
+
clearTimeout(this.task);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
currentEventCount() {
|
|
49
|
+
return this.fromCount;
|
|
50
|
+
}
|
|
51
|
+
async fetchEvents() {
|
|
52
|
+
try {
|
|
53
|
+
const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
|
|
54
|
+
start: this.fromCount
|
|
55
|
+
});
|
|
56
|
+
if (this.cancelled) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (this.fromCount === events.nextStart) {
|
|
60
|
+
this.task = setTimeout(() => this.eventEmitter.emit('tick'), this.pollingInterval);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const promises = events.events.map((event) => this.eventCallback(event));
|
|
64
|
+
await Promise.all(promises);
|
|
65
|
+
this.fromCount = events.nextStart;
|
|
66
|
+
await this.fetchEvents();
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
await this.errorCallback(err, this);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.Subscription = Subscription;
|
|
74
|
+
function subscribe(options) {
|
|
75
|
+
return new Subscription(options);
|
|
76
|
+
}
|
|
77
|
+
exports.subscribe = subscribe;
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
22
|
+
}) : (function(o, m, k, k2) {
|
|
23
|
+
if (k2 === undefined) k2 = k;
|
|
24
|
+
o[k2] = m[k];
|
|
25
|
+
}));
|
|
26
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
27
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
__exportStar(require("./ralph"), exports);
|
|
31
|
+
__exportStar(require("./contract"), exports);
|
|
32
|
+
__exportStar(require("./events"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { node } from '../api';
|
|
2
|
+
import { Fields, Val } 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: node.FieldsSig): string;
|
|
11
|
+
export declare function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: node.FieldsSig): string;
|
|
12
|
+
export declare function encodeContractField(tpe: string, value: Val): Uint8Array[];
|
|
@@ -0,0 +1,362 @@
|
|
|
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.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 buildContractByteCode(bytecode, fields, fieldsSig) {
|
|
257
|
+
const fieldsEncoded = fieldsSig.names.flatMap((fieldName, fieldIndex) => {
|
|
258
|
+
const fieldType = fieldsSig.types[`${fieldIndex}`];
|
|
259
|
+
if (fieldName in fields) {
|
|
260
|
+
const fieldValue = fields[`${fieldName}`];
|
|
261
|
+
return encodeContractField(fieldType, fieldValue);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
throw new Error(`The value of field ${fieldName} is not provided`);
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
const fieldsLength = buffer_1.Buffer.from(encodeI256(BigInt(fieldsEncoded.length))).toString('hex');
|
|
268
|
+
return bytecode + fieldsLength + fieldsEncoded.map((f) => buffer_1.Buffer.from(f).toString('hex')).join('');
|
|
269
|
+
}
|
|
270
|
+
exports.buildContractByteCode = buildContractByteCode;
|
|
271
|
+
var ApiValType;
|
|
272
|
+
(function (ApiValType) {
|
|
273
|
+
ApiValType[ApiValType["Bool"] = 0] = "Bool";
|
|
274
|
+
ApiValType[ApiValType["I256"] = 1] = "I256";
|
|
275
|
+
ApiValType[ApiValType["U256"] = 2] = "U256";
|
|
276
|
+
ApiValType[ApiValType["ByteVec"] = 3] = "ByteVec";
|
|
277
|
+
ApiValType[ApiValType["Address"] = 4] = "Address";
|
|
278
|
+
})(ApiValType || (ApiValType = {}));
|
|
279
|
+
function encodeContractFieldI256(value) {
|
|
280
|
+
return new Uint8Array([ApiValType.I256, ...encodeI256(value)]);
|
|
281
|
+
}
|
|
282
|
+
function encodeContractFieldU256(value) {
|
|
283
|
+
return new Uint8Array([ApiValType.U256, ...encodeU256(value)]);
|
|
284
|
+
}
|
|
285
|
+
function encodeContractFieldArray(tpe, val) {
|
|
286
|
+
if (!Array.isArray(val)) {
|
|
287
|
+
throw new Error(`Expected array, got ${val}`);
|
|
288
|
+
}
|
|
289
|
+
const semiColonIndex = tpe.lastIndexOf(';');
|
|
290
|
+
if (semiColonIndex == -1) {
|
|
291
|
+
throw new Error(`Invalid Array type: ${tpe}`);
|
|
292
|
+
}
|
|
293
|
+
const subType = tpe.slice(1, semiColonIndex);
|
|
294
|
+
const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
|
|
295
|
+
if (val.length != dim) {
|
|
296
|
+
throw new Error(`Invalid val dimension: ${val}`);
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
return val.flatMap((v) => encodeContractField(subType, v));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function encodeContractField(tpe, value) {
|
|
303
|
+
switch (tpe) {
|
|
304
|
+
case 'Bool':
|
|
305
|
+
if (typeof value === 'boolean') {
|
|
306
|
+
const byte = value ? 1 : 0;
|
|
307
|
+
return [new Uint8Array([ApiValType.Bool, byte])];
|
|
308
|
+
}
|
|
309
|
+
break;
|
|
310
|
+
case 'I256':
|
|
311
|
+
if (typeof value === 'number' && Number.isInteger(value)) {
|
|
312
|
+
return [encodeContractFieldI256(BigInt(value))];
|
|
313
|
+
}
|
|
314
|
+
else if (typeof value === 'bigint') {
|
|
315
|
+
return [encodeContractFieldI256(value)];
|
|
316
|
+
}
|
|
317
|
+
break;
|
|
318
|
+
case 'U256':
|
|
319
|
+
if (typeof value === 'number' && Number.isInteger(value)) {
|
|
320
|
+
return [encodeContractFieldU256(BigInt(value))];
|
|
321
|
+
}
|
|
322
|
+
else if (typeof value === 'bigint') {
|
|
323
|
+
return [encodeContractFieldU256(value)];
|
|
324
|
+
}
|
|
325
|
+
break;
|
|
326
|
+
case 'ByteVec':
|
|
327
|
+
if (typeof value === 'string') {
|
|
328
|
+
return [new Uint8Array([ApiValType.ByteVec, ...encodeByteVec(value)])];
|
|
329
|
+
}
|
|
330
|
+
break;
|
|
331
|
+
case 'Address':
|
|
332
|
+
if (typeof value === 'string') {
|
|
333
|
+
return [new Uint8Array([ApiValType.Address, ...encodeAddress(value)])];
|
|
334
|
+
}
|
|
335
|
+
break;
|
|
336
|
+
default:
|
|
337
|
+
// Array type
|
|
338
|
+
return encodeContractFieldArray(tpe, value);
|
|
339
|
+
}
|
|
340
|
+
throw invalidVal(tpe, value);
|
|
341
|
+
}
|
|
342
|
+
exports.encodeContractField = encodeContractField;
|
|
343
|
+
function invalidVal(tpe, value) {
|
|
344
|
+
return Error(`Invalid API value ${value} for type ${tpe}`);
|
|
345
|
+
}
|
|
346
|
+
// export function buildContractByteCode(
|
|
347
|
+
// compiled: node.TemplateContractByteCode,
|
|
348
|
+
// templateVariables: TemplateVariables
|
|
349
|
+
// ): string {
|
|
350
|
+
// const methodsBuilt = compiled.methodsByteCode.map((template) => buildByteCode(template, templateVariables))
|
|
351
|
+
// let count = 0
|
|
352
|
+
// const methodIndexes = methodsBuilt.map((hex) => {
|
|
353
|
+
// count += hex.length / 2
|
|
354
|
+
// return count
|
|
355
|
+
// })
|
|
356
|
+
// return (
|
|
357
|
+
// binToHex(encodeI256(BigInt(compiled.filedLength))) +
|
|
358
|
+
// binToHex(encodeI256(BigInt(methodIndexes.length))) +
|
|
359
|
+
// methodIndexes.map((index) => binToHex(encodeI256(BigInt(index)))).join('') +
|
|
360
|
+
// methodsBuilt.join('')
|
|
361
|
+
// )
|
|
362
|
+
// }
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
22
|
+
}) : (function(o, m, k, k2) {
|
|
23
|
+
if (k2 === undefined) k2 = k;
|
|
24
|
+
o[k2] = m[k];
|
|
25
|
+
}));
|
|
26
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
27
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
__exportStar(require("./api"), exports);
|
|
31
|
+
__exportStar(require("./contract"), exports);
|
|
32
|
+
__exportStar(require("./signer"), exports);
|
|
33
|
+
__exportStar(require("./utils"), exports);
|
|
34
|
+
__exportStar(require("./constants"), exports);
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
22
|
+
}) : (function(o, m, k, k2) {
|
|
23
|
+
if (k2 === undefined) k2 = k;
|
|
24
|
+
o[k2] = m[k];
|
|
25
|
+
}));
|
|
26
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
27
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
__exportStar(require("./signer"), exports);
|
|
31
|
+
__exportStar(require("./node-wallet"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NodeProvider } from '../api';
|
|
2
|
+
import { Account, SignerWithNodeProvider } from '../signer';
|
|
3
|
+
export declare class NodeWallet extends SignerWithNodeProvider {
|
|
4
|
+
walletName: string;
|
|
5
|
+
accounts: Account[] | undefined;
|
|
6
|
+
constructor(provider: NodeProvider, walletName: string, alwaysSubmitTx?: boolean);
|
|
7
|
+
getAccounts(): Promise<Account[]>;
|
|
8
|
+
private getAllAccounts;
|
|
9
|
+
static FromCliqueClient(provider: NodeProvider, walletName: string, alwaysSubmitTx?: boolean): Promise<NodeWallet>;
|
|
10
|
+
signRaw(signerAddress: string, hexString: string): Promise<string>;
|
|
11
|
+
unlock(password: string): Promise<void>;
|
|
12
|
+
lock(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
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.NodeWallet = void 0;
|
|
21
|
+
const signer_1 = require("../signer");
|
|
22
|
+
class NodeWallet extends signer_1.SignerWithNodeProvider {
|
|
23
|
+
constructor(provider, walletName, alwaysSubmitTx = true) {
|
|
24
|
+
super(provider, alwaysSubmitTx);
|
|
25
|
+
this.walletName = walletName;
|
|
26
|
+
}
|
|
27
|
+
async getAccounts() {
|
|
28
|
+
if (typeof this.accounts === 'undefined') {
|
|
29
|
+
this.accounts = await this.getAllAccounts();
|
|
30
|
+
}
|
|
31
|
+
return this.accounts;
|
|
32
|
+
}
|
|
33
|
+
async getAllAccounts() {
|
|
34
|
+
const walletAddresses = await this.provider.wallets.getWalletsWalletNameAddresses(this.walletName);
|
|
35
|
+
const accounts = walletAddresses.addresses.map((acc) => ({
|
|
36
|
+
publicKey: acc.publicKey,
|
|
37
|
+
address: acc.address,
|
|
38
|
+
group: acc.group
|
|
39
|
+
}));
|
|
40
|
+
return accounts;
|
|
41
|
+
}
|
|
42
|
+
static async FromCliqueClient(provider, walletName, alwaysSubmitTx = true) {
|
|
43
|
+
return new NodeWallet(provider, walletName, alwaysSubmitTx);
|
|
44
|
+
}
|
|
45
|
+
async signRaw(signerAddress, hexString) {
|
|
46
|
+
const currentActiveAddressResponse = await this.provider.wallets.getWalletsWalletNameAddresses(this.walletName);
|
|
47
|
+
const { activeAddress } = currentActiveAddressResponse;
|
|
48
|
+
await this.provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: signerAddress });
|
|
49
|
+
const { signature } = await this.provider.wallets.postWalletsWalletNameSign(this.walletName, { data: hexString });
|
|
50
|
+
await this.provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: activeAddress }); // set the address that's active back to previous state
|
|
51
|
+
return signature;
|
|
52
|
+
}
|
|
53
|
+
async unlock(password) {
|
|
54
|
+
return await this.provider.wallets.postWalletsWalletNameUnlock(this.walletName, { password });
|
|
55
|
+
}
|
|
56
|
+
async lock() {
|
|
57
|
+
return await this.provider.wallets.postWalletsWalletNameLock(this.walletName);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.NodeWallet = NodeWallet;
|