@alephium/web3 0.0.1-test.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/.gitattributes +1 -0
- package/LICENSE +165 -0
- package/README.md +136 -0
- package/contracts/add.ral +16 -0
- package/contracts/greeter.ral +7 -0
- package/contracts/greeter_interface.ral +3 -0
- package/contracts/greeter_main.ral +9 -0
- package/contracts/main.ral +6 -0
- package/contracts/sub.ral +9 -0
- package/dev/user.conf +29 -0
- package/dist/alephium-web3.min.js +3 -0
- package/dist/alephium-web3.min.js.LICENSE.txt +27 -0
- package/dist/alephium-web3.min.js.map +1 -0
- package/dist/scripts/check-versions.d.ts +1 -0
- package/dist/scripts/check-versions.js +39 -0
- package/dist/scripts/create-project.d.ts +2 -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/src/api/api-alephium.d.ts +1473 -0
- package/dist/src/api/api-alephium.js +1143 -0
- package/dist/src/api/api-explorer.d.ts +475 -0
- package/dist/src/api/api-explorer.js +457 -0
- package/dist/src/api/index.d.ts +10 -0
- package/dist/src/api/index.js +55 -0
- package/dist/src/constants.d.ts +2 -0
- package/dist/src/constants.js +22 -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 +11 -0
- package/dist/src/contract/events.js +64 -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 +6 -0
- package/dist/src/index.js +35 -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/src/transaction/index.d.ts +2 -0
- package/dist/src/transaction/index.js +31 -0
- package/dist/src/transaction/sign-verify.d.ts +2 -0
- package/dist/src/transaction/sign-verify.js +58 -0
- package/dist/src/transaction/status.d.ts +10 -0
- package/dist/src/transaction/status.js +48 -0
- package/dist/src/utils/address.d.ts +1 -0
- package/dist/src/utils/address.js +42 -0
- package/dist/src/utils/bs58.d.ts +4 -0
- package/dist/src/utils/bs58.js +28 -0
- package/dist/src/utils/djb2.d.ts +1 -0
- package/dist/src/utils/djb2.js +27 -0
- package/dist/src/utils/index.d.ts +6 -0
- package/dist/src/utils/index.js +35 -0
- package/dist/src/utils/password-crypto.d.ts +2 -0
- package/dist/src/utils/password-crypto.js +69 -0
- package/dist/src/utils/subscription.d.ts +24 -0
- package/dist/src/utils/subscription.js +52 -0
- package/dist/src/utils/utils.d.ts +30 -0
- package/dist/src/utils/utils.js +199 -0
- package/gitignore +10 -0
- package/package.json +127 -0
- package/scripts/check-versions.js +45 -0
- package/scripts/create-project.ts +136 -0
- package/scripts/header.js +17 -0
- package/scripts/rename-gitignore.js +24 -0
- package/scripts/start-devnet.js +141 -0
- package/scripts/stop-devnet.js +32 -0
- package/src/api/api-alephium.ts +2430 -0
- package/src/api/api-explorer.ts +852 -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 +75 -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 +25 -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/transaction/index.ts +20 -0
- package/src/transaction/sign-verify.test.ts +50 -0
- package/src/transaction/sign-verify.ts +39 -0
- package/src/transaction/status.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/subscription.ts +72 -0
- package/src/utils/utils.test.ts +160 -0
- package/src/utils/utils.ts +209 -0
- package/templates/base/README.md +34 -0
- 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 +138 -0
- package/test/transaction.test.ts +72 -0
- package/webpack.config.js +57 -0
|
@@ -0,0 +1,28 @@
|
|
|
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.bs58 = void 0;
|
|
24
|
+
/** This source is under MIT License and come originally from https://github.com/cryptocoinjs/bs58 **/
|
|
25
|
+
const base_x_1 = __importDefault(require("base-x"));
|
|
26
|
+
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
27
|
+
exports.bs58 = (0, base_x_1.default)(ALPHABET);
|
|
28
|
+
exports.default = exports.bs58;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function djb2(bytes: Uint8Array): number;
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
function djb2(bytes) {
|
|
21
|
+
let hash = 5381;
|
|
22
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
+
hash = (hash << 5) + hash + (bytes[`${i}`] & 0xff);
|
|
24
|
+
}
|
|
25
|
+
return hash;
|
|
26
|
+
}
|
|
27
|
+
exports.default = djb2;
|
|
@@ -0,0 +1,35 @@
|
|
|
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("./address"), exports);
|
|
31
|
+
__exportStar(require("./bs58"), exports);
|
|
32
|
+
__exportStar(require("./djb2"), exports);
|
|
33
|
+
__exportStar(require("./password-crypto"), exports);
|
|
34
|
+
__exportStar(require("./utils"), exports);
|
|
35
|
+
__exportStar(require("./subscription"), exports);
|
|
@@ -0,0 +1,69 @@
|
|
|
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.decrypt = exports.encrypt = void 0;
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
const crypto_1 = require("crypto");
|
|
23
|
+
const saltByteLength = 64;
|
|
24
|
+
const ivByteLength = 64;
|
|
25
|
+
const authTagLength = 16;
|
|
26
|
+
const encrypt = (password, dataRaw) => {
|
|
27
|
+
const data = buffer_1.Buffer.from(dataRaw, 'utf8');
|
|
28
|
+
const salt = (0, crypto_1.randomBytes)(saltByteLength);
|
|
29
|
+
const derivedKey = keyFromPassword(password, salt);
|
|
30
|
+
const iv = (0, crypto_1.randomBytes)(ivByteLength);
|
|
31
|
+
const cipher = createCipher(derivedKey, iv);
|
|
32
|
+
const encrypted = buffer_1.Buffer.concat([cipher.update(data), cipher.final()]);
|
|
33
|
+
const authTag = cipher.getAuthTag();
|
|
34
|
+
const payload = {
|
|
35
|
+
salt: salt.toString('hex'),
|
|
36
|
+
iv: iv.toString('hex'),
|
|
37
|
+
encrypted: buffer_1.Buffer.concat([encrypted, authTag]).toString('hex'),
|
|
38
|
+
version: 1
|
|
39
|
+
};
|
|
40
|
+
return JSON.stringify(payload);
|
|
41
|
+
};
|
|
42
|
+
exports.encrypt = encrypt;
|
|
43
|
+
const decrypt = (password, payloadRaw) => {
|
|
44
|
+
const payload = JSON.parse(payloadRaw);
|
|
45
|
+
const version = payload.version;
|
|
46
|
+
if (version !== 1) {
|
|
47
|
+
throw new Error(`Invalid version: got ${version}, expected: 1`);
|
|
48
|
+
}
|
|
49
|
+
const salt = buffer_1.Buffer.from(payload.salt, 'hex');
|
|
50
|
+
const iv = buffer_1.Buffer.from(payload.iv, 'hex');
|
|
51
|
+
const encrypted = buffer_1.Buffer.from(payload.encrypted, 'hex');
|
|
52
|
+
const derivedKey = keyFromPassword(password, salt);
|
|
53
|
+
const decipher = createDecipher(derivedKey, iv);
|
|
54
|
+
const data = encrypted.slice(0, encrypted.length - authTagLength);
|
|
55
|
+
const authTag = encrypted.slice(encrypted.length - authTagLength, encrypted.length);
|
|
56
|
+
decipher.setAuthTag(authTag);
|
|
57
|
+
const decrypted = buffer_1.Buffer.concat([decipher.update(data), decipher.final()]);
|
|
58
|
+
return decrypted.toString('utf8');
|
|
59
|
+
};
|
|
60
|
+
exports.decrypt = decrypt;
|
|
61
|
+
const createCipher = (key, iv) => {
|
|
62
|
+
return (0, crypto_1.createCipheriv)('aes-256-gcm', key, iv);
|
|
63
|
+
};
|
|
64
|
+
const createDecipher = (key, iv) => {
|
|
65
|
+
return (0, crypto_1.createDecipheriv)('aes-256-gcm', key, iv);
|
|
66
|
+
};
|
|
67
|
+
const keyFromPassword = (password, salt) => {
|
|
68
|
+
return (0, crypto_1.pbkdf2Sync)(password, salt, 10000, 32, 'sha256');
|
|
69
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import EventEmitter from 'eventemitter3';
|
|
2
|
+
import { NodeProvider } from '../api';
|
|
3
|
+
declare type MessageCallback<Message> = (message: Message) => Promise<void>;
|
|
4
|
+
declare type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>;
|
|
5
|
+
export interface SubscribeOptions<Message> {
|
|
6
|
+
provider: NodeProvider;
|
|
7
|
+
pollingInterval: number;
|
|
8
|
+
messageCallback: MessageCallback<Message>;
|
|
9
|
+
errorCallback: ErrorCallback<Message>;
|
|
10
|
+
}
|
|
11
|
+
export declare abstract class Subscription<Message> {
|
|
12
|
+
provider: NodeProvider;
|
|
13
|
+
pollingInterval: number;
|
|
14
|
+
protected messageCallback: MessageCallback<Message>;
|
|
15
|
+
protected errorCallback: ErrorCallback<Message>;
|
|
16
|
+
protected task: ReturnType<typeof setTimeout> | undefined;
|
|
17
|
+
protected eventEmitter: EventEmitter;
|
|
18
|
+
protected cancelled: boolean;
|
|
19
|
+
constructor(options: SubscribeOptions<Message>);
|
|
20
|
+
startPolling(): void;
|
|
21
|
+
unsubscribe(): void;
|
|
22
|
+
abstract polling(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
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.Subscription = void 0;
|
|
24
|
+
const eventemitter3_1 = __importDefault(require("eventemitter3"));
|
|
25
|
+
class Subscription {
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.provider = options.provider;
|
|
28
|
+
this.pollingInterval = options.pollingInterval;
|
|
29
|
+
this.messageCallback = options.messageCallback;
|
|
30
|
+
this.errorCallback = options.errorCallback;
|
|
31
|
+
this.task = undefined;
|
|
32
|
+
this.cancelled = false;
|
|
33
|
+
this.eventEmitter = new eventemitter3_1.default();
|
|
34
|
+
}
|
|
35
|
+
startPolling() {
|
|
36
|
+
this.eventEmitter.on('tick', async () => {
|
|
37
|
+
await this.polling();
|
|
38
|
+
if (!this.cancelled) {
|
|
39
|
+
this.task = setTimeout(() => this.eventEmitter.emit('tick'), this.pollingInterval);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
this.eventEmitter.emit('tick');
|
|
43
|
+
}
|
|
44
|
+
unsubscribe() {
|
|
45
|
+
this.eventEmitter.removeAllListeners();
|
|
46
|
+
this.cancelled = true;
|
|
47
|
+
if (typeof this.task !== 'undefined') {
|
|
48
|
+
clearTimeout(this.task);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.Subscription = Subscription;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ec as EC, SignatureInput } from 'elliptic';
|
|
2
|
+
import * as node from '../api/api-alephium';
|
|
3
|
+
import * as explorer from '../api/api-explorer';
|
|
4
|
+
export declare function convertHttpResponse<T>(response: node.HttpResponse<T, {
|
|
5
|
+
detail: string;
|
|
6
|
+
}> | explorer.HttpResponse<T, {
|
|
7
|
+
detail: string;
|
|
8
|
+
}>): T;
|
|
9
|
+
export declare function signatureEncode(signature: EC.Signature): string;
|
|
10
|
+
export declare const signatureDecode: (ec: EC, signature: string) => SignatureInput;
|
|
11
|
+
export declare function isHexString(input: string): boolean;
|
|
12
|
+
export declare const groupOfAddress: (address: string) => number;
|
|
13
|
+
export declare function contractIdFromAddress(address: string): Uint8Array;
|
|
14
|
+
export declare function tokenIdFromAddress(address: string): Uint8Array;
|
|
15
|
+
export declare function hexToBinUnsafe(hex: string): Uint8Array;
|
|
16
|
+
export declare function binToHex(bin: Uint8Array): string;
|
|
17
|
+
export declare function publicKeyFromPrivateKey(privateKey: string): string;
|
|
18
|
+
export declare function addressFromPublicKey(publicKey: string): string;
|
|
19
|
+
export declare function addressFromContractId(contractId: string): string;
|
|
20
|
+
export declare function contractIdFromTx(txId: string, outputIndex: number): string;
|
|
21
|
+
export declare function subContractId(parentContractId: string, pathInHex: string): string;
|
|
22
|
+
export declare function stringToHex(str: string): string;
|
|
23
|
+
declare type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
24
|
+
export declare type Eq<X, Y> = _Eq<{
|
|
25
|
+
[P in keyof X]: X[P];
|
|
26
|
+
}, {
|
|
27
|
+
[P in keyof Y]: Y[P];
|
|
28
|
+
}>;
|
|
29
|
+
export declare function assertType<T extends true>(): void;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,199 @@
|
|
|
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.assertType = exports.stringToHex = exports.subContractId = exports.contractIdFromTx = exports.addressFromContractId = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.binToHex = exports.hexToBinUnsafe = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isHexString = exports.signatureDecode = exports.signatureEncode = exports.convertHttpResponse = void 0;
|
|
24
|
+
const elliptic_1 = require("elliptic");
|
|
25
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
26
|
+
const blakejs_1 = __importDefault(require("blakejs"));
|
|
27
|
+
const bs58_1 = __importDefault(require("./bs58"));
|
|
28
|
+
const buffer_1 = require("buffer/");
|
|
29
|
+
const constants_1 = require("../constants");
|
|
30
|
+
const djb2_1 = __importDefault(require("./djb2"));
|
|
31
|
+
const ec = new elliptic_1.ec('secp256k1');
|
|
32
|
+
function convertHttpResponse(response) {
|
|
33
|
+
if (response.error) {
|
|
34
|
+
throw new Error(response.error.detail);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return response.data;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.convertHttpResponse = convertHttpResponse;
|
|
41
|
+
function signatureEncode(signature) {
|
|
42
|
+
let sNormalized = signature.s;
|
|
43
|
+
if (ec.n && signature.s.cmp(ec.nh) === 1) {
|
|
44
|
+
sNormalized = ec.n.sub(signature.s);
|
|
45
|
+
}
|
|
46
|
+
const r = signature.r.toString('hex', 66).slice(2);
|
|
47
|
+
const s = sNormalized.toString('hex', 66).slice(2);
|
|
48
|
+
return r + s;
|
|
49
|
+
}
|
|
50
|
+
exports.signatureEncode = signatureEncode;
|
|
51
|
+
// the signature should be in hex string format for 64 bytes
|
|
52
|
+
const signatureDecode = (ec, signature) => {
|
|
53
|
+
if (signature.length !== 128) {
|
|
54
|
+
throw new Error('Invalid signature length');
|
|
55
|
+
}
|
|
56
|
+
const sHex = signature.slice(64, 128);
|
|
57
|
+
const s = new bn_js_1.default(sHex, 'hex');
|
|
58
|
+
if (ec.n && s.cmp(ec.nh) < 1) {
|
|
59
|
+
const decoded = { r: signature.slice(0, 64), s: sHex };
|
|
60
|
+
return decoded;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
throw new Error('The signature is not normalized');
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
exports.signatureDecode = signatureDecode;
|
|
67
|
+
const xorByte = (intValue) => {
|
|
68
|
+
const byte0 = (intValue >> 24) & 0xff;
|
|
69
|
+
const byte1 = (intValue >> 16) & 0xff;
|
|
70
|
+
const byte2 = (intValue >> 8) & 0xff;
|
|
71
|
+
const byte3 = intValue & 0xff;
|
|
72
|
+
return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff;
|
|
73
|
+
};
|
|
74
|
+
function isHexString(input) {
|
|
75
|
+
return input.length % 2 === 0 && /[0-9a-f]*$/.test(input);
|
|
76
|
+
}
|
|
77
|
+
exports.isHexString = isHexString;
|
|
78
|
+
var AddressType;
|
|
79
|
+
(function (AddressType) {
|
|
80
|
+
AddressType[AddressType["P2PKH"] = 0] = "P2PKH";
|
|
81
|
+
AddressType[AddressType["P2MPKH"] = 1] = "P2MPKH";
|
|
82
|
+
AddressType[AddressType["P2SH"] = 2] = "P2SH";
|
|
83
|
+
AddressType[AddressType["P2C"] = 3] = "P2C";
|
|
84
|
+
})(AddressType || (AddressType = {}));
|
|
85
|
+
const groupOfAddress = (address) => {
|
|
86
|
+
const decoded = bs58_1.default.decode(address);
|
|
87
|
+
if (decoded.length == 0)
|
|
88
|
+
throw new Error('Address string is empty');
|
|
89
|
+
const addressType = decoded[0];
|
|
90
|
+
const addressBody = decoded.slice(1);
|
|
91
|
+
if (addressType == AddressType.P2PKH) {
|
|
92
|
+
return groupOfP2pkhAddress(addressBody);
|
|
93
|
+
}
|
|
94
|
+
else if (addressType == AddressType.P2MPKH) {
|
|
95
|
+
return groupOfP2mpkhAddress(addressBody);
|
|
96
|
+
}
|
|
97
|
+
else if (addressType == AddressType.P2SH) {
|
|
98
|
+
return groupOfP2shAddress(addressBody);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
throw new Error(`Invalid asset address type: ${addressType}`);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
exports.groupOfAddress = groupOfAddress;
|
|
105
|
+
const groupOfAddressBytes = (bytes) => {
|
|
106
|
+
const hint = (0, djb2_1.default)(bytes) | 1;
|
|
107
|
+
const hash = xorByte(hint);
|
|
108
|
+
const group = hash % constants_1.TOTAL_NUMBER_OF_GROUPS;
|
|
109
|
+
return group;
|
|
110
|
+
};
|
|
111
|
+
// Pay to public key hash address
|
|
112
|
+
const groupOfP2pkhAddress = (address) => {
|
|
113
|
+
if (address.length != 32) {
|
|
114
|
+
throw new Error(`Invalid p2pkh address length: ${address.length}`);
|
|
115
|
+
}
|
|
116
|
+
return groupOfAddressBytes(address);
|
|
117
|
+
};
|
|
118
|
+
// Pay to multiple public key hash address
|
|
119
|
+
const groupOfP2mpkhAddress = (address) => {
|
|
120
|
+
if ((address.length - 2) % 32 != 0) {
|
|
121
|
+
throw new Error(`Invalid p2mpkh address length: ${address.length}`);
|
|
122
|
+
}
|
|
123
|
+
return groupOfAddressBytes(address.slice(1, 33));
|
|
124
|
+
};
|
|
125
|
+
// Pay to script hash address
|
|
126
|
+
const groupOfP2shAddress = (address) => {
|
|
127
|
+
return groupOfAddressBytes(address);
|
|
128
|
+
};
|
|
129
|
+
function contractIdFromAddress(address) {
|
|
130
|
+
return idFromAddress(address);
|
|
131
|
+
}
|
|
132
|
+
exports.contractIdFromAddress = contractIdFromAddress;
|
|
133
|
+
function tokenIdFromAddress(address) {
|
|
134
|
+
return idFromAddress(address);
|
|
135
|
+
}
|
|
136
|
+
exports.tokenIdFromAddress = tokenIdFromAddress;
|
|
137
|
+
function idFromAddress(address) {
|
|
138
|
+
const decoded = bs58_1.default.decode(address);
|
|
139
|
+
if (decoded.length == 0)
|
|
140
|
+
throw new Error('Address string is empty');
|
|
141
|
+
const addressType = decoded[0];
|
|
142
|
+
const addressBody = decoded.slice(1);
|
|
143
|
+
if (addressType == AddressType.P2C) {
|
|
144
|
+
return addressBody;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
throw new Error(`Invalid contract address type: ${addressType}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function hexToBinUnsafe(hex) {
|
|
151
|
+
return buffer_1.Buffer.from(hex, 'hex');
|
|
152
|
+
}
|
|
153
|
+
exports.hexToBinUnsafe = hexToBinUnsafe;
|
|
154
|
+
function binToHex(bin) {
|
|
155
|
+
return buffer_1.Buffer.from(bin).toString('hex');
|
|
156
|
+
}
|
|
157
|
+
exports.binToHex = binToHex;
|
|
158
|
+
function publicKeyFromPrivateKey(privateKey) {
|
|
159
|
+
const key = ec.keyFromPrivate(privateKey);
|
|
160
|
+
return key.getPublic(true, 'hex');
|
|
161
|
+
}
|
|
162
|
+
exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
|
|
163
|
+
function addressFromPublicKey(publicKey) {
|
|
164
|
+
const addressType = buffer_1.Buffer.from([AddressType.P2PKH]);
|
|
165
|
+
const hash = buffer_1.Buffer.from(blakejs_1.default.blake2b(buffer_1.Buffer.from(publicKey, 'hex'), undefined, 32));
|
|
166
|
+
const bytes = buffer_1.Buffer.concat([addressType, hash]);
|
|
167
|
+
return bs58_1.default.encode(bytes);
|
|
168
|
+
}
|
|
169
|
+
exports.addressFromPublicKey = addressFromPublicKey;
|
|
170
|
+
function addressFromContractId(contractId) {
|
|
171
|
+
const addressType = buffer_1.Buffer.from([AddressType.P2C]);
|
|
172
|
+
const hash = buffer_1.Buffer.from(hexToBinUnsafe(contractId));
|
|
173
|
+
const bytes = buffer_1.Buffer.concat([addressType, hash]);
|
|
174
|
+
return bs58_1.default.encode(bytes);
|
|
175
|
+
}
|
|
176
|
+
exports.addressFromContractId = addressFromContractId;
|
|
177
|
+
function contractIdFromTx(txId, outputIndex) {
|
|
178
|
+
const txIdBin = hexToBinUnsafe(txId);
|
|
179
|
+
const data = buffer_1.Buffer.concat([txIdBin, buffer_1.Buffer.from([outputIndex])]);
|
|
180
|
+
const hash = blakejs_1.default.blake2b(data, undefined, 32);
|
|
181
|
+
return binToHex(hash);
|
|
182
|
+
}
|
|
183
|
+
exports.contractIdFromTx = contractIdFromTx;
|
|
184
|
+
function subContractId(parentContractId, pathInHex) {
|
|
185
|
+
const data = buffer_1.Buffer.concat([hexToBinUnsafe(pathInHex), hexToBinUnsafe(parentContractId)]);
|
|
186
|
+
return binToHex(blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32));
|
|
187
|
+
}
|
|
188
|
+
exports.subContractId = subContractId;
|
|
189
|
+
function stringToHex(str) {
|
|
190
|
+
let hex = '';
|
|
191
|
+
for (let i = 0; i < str.length; i++) {
|
|
192
|
+
hex += '' + str.charCodeAt(i).toString(16);
|
|
193
|
+
}
|
|
194
|
+
return hex;
|
|
195
|
+
}
|
|
196
|
+
exports.stringToHex = stringToHex;
|
|
197
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
|
|
198
|
+
function assertType() { }
|
|
199
|
+
exports.assertType = assertType;
|
package/gitignore
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alephium/web3",
|
|
3
|
+
"version": "0.0.1-test.0",
|
|
4
|
+
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
|
+
"license": "GPL",
|
|
6
|
+
"main": "dist/src/index.js",
|
|
7
|
+
"browser": "dist/alephium-web3.min.js",
|
|
8
|
+
"types": "dist/src/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/src/index.js",
|
|
11
|
+
"./test": "./dist/src/test/index.js"
|
|
12
|
+
},
|
|
13
|
+
"typesVersions": {
|
|
14
|
+
"*": {
|
|
15
|
+
"test": [
|
|
16
|
+
"dist/src/test/"
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git@github.com:alephium/alephium-web3.git"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/alephium/alephium-web3",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/alephium/alephium-web3/issues"
|
|
27
|
+
},
|
|
28
|
+
"author": "Alephium dev <dev@alephium.org>",
|
|
29
|
+
"config": {
|
|
30
|
+
"alephium_version": "token-balance",
|
|
31
|
+
"explorer_backend_version": "1.7.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "rm -rf dist/* && npx tsc --build . && webpack",
|
|
35
|
+
"bundle": "webpack",
|
|
36
|
+
"update-schemas": "npm run update-schema:alephium && npm run update-schema:explorer",
|
|
37
|
+
"update-schema:alephium": "npx swagger-typescript-api -t ./configs -o ./src/api -n api-alephium.ts -p https://raw.githubusercontent.com/alephium/alephium/${npm_package_config_alephium_version}/api/src/main/resources/openapi.json",
|
|
38
|
+
"update-schema:explorer": "npx swagger-typescript-api -t ./configs -o ./src/api -n api-explorer.ts -p https://raw.githubusercontent.com/alephium/explorer-backend/v${npm_package_config_explorer_backend_version}/app/src/main/resources/explorer-backend-openapi.json",
|
|
39
|
+
"check-versions": "node scripts/check-versions.js ${npm_package_config_alephium_version} ${npm_package_config_explorer_backend_version}",
|
|
40
|
+
"dev": "tsnd --respawn lib/index.ts",
|
|
41
|
+
"lint": "eslint . --ext ts",
|
|
42
|
+
"lint:fix": "eslint . --fix --ext ts",
|
|
43
|
+
"test": "jest -i --config ./configs/jest.config.ts",
|
|
44
|
+
"test:client": "jest -i --config ./configs/jest-client.config.ts",
|
|
45
|
+
"test:unit": "jest -i --config ./configs/jest-unit.config.ts",
|
|
46
|
+
"test:watch": "npm run test -- --watch --coverage=false",
|
|
47
|
+
"test:watch:unit": "npm run test:unit -- --watch --coverage=false",
|
|
48
|
+
"test:watch:client": "npm run test:client -- --watch --coverage=false",
|
|
49
|
+
"prepublishOnly": "npm run build",
|
|
50
|
+
"prepack": "node scripts/rename-gitignore.js .gitignore gitignore",
|
|
51
|
+
"postpack": "node scripts/rename-gitignore.js gitignore .gitignore",
|
|
52
|
+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
|
|
53
|
+
"start-devnet": "node scripts/start-devnet.js ${npm_package_config_alephium_version}",
|
|
54
|
+
"restart-devnet": "npm run start-devnet",
|
|
55
|
+
"stop-devnet": "node scripts/stop-devnet.js"
|
|
56
|
+
},
|
|
57
|
+
"type": "commonjs",
|
|
58
|
+
"bin": {
|
|
59
|
+
"alephium": "dist/scripts/create-project.js"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"base-x": "4.0.0",
|
|
63
|
+
"blakejs": "1.2.1",
|
|
64
|
+
"buffer": "^6.0.3",
|
|
65
|
+
"commander": "^9.1.0",
|
|
66
|
+
"cross-fetch": "^3.1.5",
|
|
67
|
+
"crypto-js": "4.1.1",
|
|
68
|
+
"elliptic": "6.5.4",
|
|
69
|
+
"eventemitter3": "^4.0.7",
|
|
70
|
+
"find-up": "^2.1.0",
|
|
71
|
+
"fs-extra": "^10.0.1"
|
|
72
|
+
},
|
|
73
|
+
"devDependencies": {
|
|
74
|
+
"@types/crypto-js": "^4.1.1",
|
|
75
|
+
"@types/elliptic": "^6.4.13",
|
|
76
|
+
"@types/find-up": "^2.1.0",
|
|
77
|
+
"@types/fs-extra": "^9.0.13",
|
|
78
|
+
"@types/jest": "^27.5.1",
|
|
79
|
+
"@types/mock-fs": "^4.13.1",
|
|
80
|
+
"@types/node": "^16.7.8",
|
|
81
|
+
"@types/rewire": "^2.5.28",
|
|
82
|
+
"@typescript-eslint/eslint-plugin": "^4.30.0",
|
|
83
|
+
"@typescript-eslint/parser": "^4.30.0",
|
|
84
|
+
"babel-eslint": "^10.1.0",
|
|
85
|
+
"clean-webpack-plugin": "4.0.0",
|
|
86
|
+
"crypto-browserify": "^3.12.0",
|
|
87
|
+
"eslint": "^7.32.0",
|
|
88
|
+
"eslint-config-prettier": "^8.5.0",
|
|
89
|
+
"eslint-plugin-header": "^3.1.1",
|
|
90
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
91
|
+
"eslint-plugin-react": "^7.29.4",
|
|
92
|
+
"eslint-plugin-security": "1.4.0",
|
|
93
|
+
"html-webpack-plugin": "5.5.0",
|
|
94
|
+
"jest": "^28.1.0",
|
|
95
|
+
"jest-localstorage-mock": "^2.4.18",
|
|
96
|
+
"jest-websocket-mock": "^2.2.1",
|
|
97
|
+
"mock-fs": "^5.1.2",
|
|
98
|
+
"mock-socket": "^9.0.8",
|
|
99
|
+
"prettier": "^2.3.2",
|
|
100
|
+
"process": "^0.11.10",
|
|
101
|
+
"react-app-rewired": "^2.2.1",
|
|
102
|
+
"rewire": "^6.0.0",
|
|
103
|
+
"shelljs": "^0.8.5",
|
|
104
|
+
"stream-browserify": "^3.0.0",
|
|
105
|
+
"swagger-typescript-api": "^9.2.0",
|
|
106
|
+
"terser-webpack-plugin": "^5.3.1",
|
|
107
|
+
"ts-jest": "^28.0.2",
|
|
108
|
+
"ts-node": "^10.7.0",
|
|
109
|
+
"tslib": "^2.3.1",
|
|
110
|
+
"typescript": "^4.4.2",
|
|
111
|
+
"webpack": "^5.72.0",
|
|
112
|
+
"webpack-cli": "^4.9.2"
|
|
113
|
+
},
|
|
114
|
+
"engines": {
|
|
115
|
+
"node": ">=14.0.0",
|
|
116
|
+
"npm": ">=7.0.0"
|
|
117
|
+
},
|
|
118
|
+
"prettier": {
|
|
119
|
+
"printWidth": 120,
|
|
120
|
+
"tabWidth": 2,
|
|
121
|
+
"useTabs": false,
|
|
122
|
+
"semi": false,
|
|
123
|
+
"singleQuote": true,
|
|
124
|
+
"bracketSameLine": false,
|
|
125
|
+
"trailingComma": "none"
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const fetch = require('cross-fetch')
|
|
20
|
+
const process = require('process')
|
|
21
|
+
|
|
22
|
+
async function extractNodeVersionFromExplorer(explorerVersion) {
|
|
23
|
+
const url = `https://raw.githubusercontent.com/alephium/explorer-backend/v${explorerVersion}/project/Dependencies.scala`
|
|
24
|
+
const response = await (await fetch(url)).text()
|
|
25
|
+
const regex = /val common = "[^"]+"/
|
|
26
|
+
const matched = regex.exec(response)[0]
|
|
27
|
+
return matched.split('"')[1]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function main() {
|
|
31
|
+
const nodeVersionConfigured = process.argv[2]
|
|
32
|
+
const explorerVersionConfigured = process.argv[3]
|
|
33
|
+
const nodeVersionExpected = await extractNodeVersionFromExplorer(explorerVersionConfigured)
|
|
34
|
+
|
|
35
|
+
if (nodeVersionExpected != nodeVersionConfigured) {
|
|
36
|
+
console.log(
|
|
37
|
+
`Invalid node version: the configured explorer-backend version (${explorerVersionConfigured}) expects node ${nodeVersionExpected}.`
|
|
38
|
+
)
|
|
39
|
+
console.log(`Instead, the configured node version is ${nodeVersionConfigured}`)
|
|
40
|
+
console.log('Please, check that the configured node and explorer-backend versions in the package.json are correct.')
|
|
41
|
+
process.exit(1)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
main()
|