@alephium/web3 0.0.3
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/.gitattributes +1 -0
- package/LICENSE +165 -0
- package/README.md +136 -0
- package/contracts/add.ral +12 -0
- package/contracts/greeter-main.ral +8 -0
- package/contracts/greeter.ral +5 -0
- package/contracts/main.ral +8 -0
- package/contracts/sub.ral +9 -0
- package/dev/user.conf +24 -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/api/api-alephium.d.ts +1292 -0
- package/dist/api/api-alephium.js +761 -0
- package/dist/api/api-explorer.d.ts +350 -0
- package/dist/api/api-explorer.js +297 -0
- package/dist/cli/create-project.d.ts +2 -0
- package/dist/cli/create-project.js +87 -0
- package/dist/lib/address.d.ts +1 -0
- package/dist/lib/address.js +42 -0
- package/dist/lib/bs58.d.ts +4 -0
- package/dist/lib/bs58.js +26 -0
- package/dist/lib/clique.d.ts +23 -0
- package/dist/lib/clique.js +149 -0
- package/dist/lib/constants.d.ts +2 -0
- package/dist/lib/constants.js +22 -0
- package/dist/lib/contract.d.ts +152 -0
- package/dist/lib/contract.js +711 -0
- package/dist/lib/djb2.d.ts +1 -0
- package/dist/lib/djb2.js +27 -0
- package/dist/lib/explorer.d.ts +8 -0
- package/dist/lib/explorer.js +46 -0
- package/dist/lib/index.d.ts +12 -0
- package/dist/lib/index.js +60 -0
- package/dist/lib/node.d.ts +10 -0
- package/dist/lib/node.js +64 -0
- package/dist/lib/numbers.d.ts +7 -0
- package/dist/lib/numbers.js +128 -0
- package/dist/lib/password-crypto.d.ts +2 -0
- package/dist/lib/password-crypto.js +68 -0
- package/dist/lib/signer.d.ts +17 -0
- package/dist/lib/signer.js +70 -0
- package/dist/lib/storage-browser.d.ts +9 -0
- package/dist/lib/storage-browser.js +52 -0
- package/dist/lib/storage-node.d.ts +9 -0
- package/dist/lib/storage-node.js +65 -0
- package/dist/lib/utils.d.ts +11 -0
- package/dist/lib/utils.js +135 -0
- package/dist/lib/wallet.d.ts +30 -0
- package/dist/lib/wallet.js +142 -0
- package/gitignore +9 -0
- package/package.json +113 -0
- package/scripts/check-versions.js +45 -0
- package/scripts/rename-gitignore.js +24 -0
- package/scripts/start-devnet.js +141 -0
- package/scripts/stop-devnet.js +32 -0
- package/templates/README.md +34 -0
- package/templates/package.json +29 -0
- package/webpack.config.js +57 -0
|
@@ -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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
20
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
const fs_1 = __importDefault(require("fs"));
|
|
24
|
+
class NodeStorage {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.remove = (name) => {
|
|
27
|
+
fs_1.default.unlinkSync(this.walletsUrl + '/' + name + '.dat');
|
|
28
|
+
};
|
|
29
|
+
this.load = (name) => {
|
|
30
|
+
let buffer;
|
|
31
|
+
try {
|
|
32
|
+
buffer = fs_1.default.readFileSync(this.walletsUrl + '/' + name + '.dat');
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
throw new Error(`Unable to load wallet ${name}: ${error}`);
|
|
36
|
+
}
|
|
37
|
+
return JSON.parse(buffer.toString());
|
|
38
|
+
};
|
|
39
|
+
this.save = (name, json) => {
|
|
40
|
+
const str = JSON.stringify(json);
|
|
41
|
+
const data = new Uint8Array(Buffer.from(str));
|
|
42
|
+
fs_1.default.writeFileSync(this.walletsUrl.toString() + '/' + name + '.dat', data);
|
|
43
|
+
};
|
|
44
|
+
this.list = () => {
|
|
45
|
+
const xs = [];
|
|
46
|
+
try {
|
|
47
|
+
const files = fs_1.default.readdirSync(this.walletsUrl);
|
|
48
|
+
files.forEach(function (file) {
|
|
49
|
+
if (file.endsWith('.dat')) {
|
|
50
|
+
xs.push(file.substring(0, file.length - 4));
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
return xs;
|
|
58
|
+
};
|
|
59
|
+
this.walletsUrl = process.env.HOME + '/.alephium-wallet-apps';
|
|
60
|
+
if (!fs_1.default.existsSync(this.walletsUrl)) {
|
|
61
|
+
fs_1.default.mkdirSync(this.walletsUrl);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.default = NodeStorage;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as EC from 'elliptic';
|
|
2
|
+
import NodeStorage from './storage-node';
|
|
3
|
+
import BrowserStorage from './storage-browser';
|
|
4
|
+
export declare const signatureEncode: (ec: EC.ec, signature: EC.ec.Signature) => string;
|
|
5
|
+
export declare const signatureDecode: (ec: EC.ec, signature: string) => {
|
|
6
|
+
r: string;
|
|
7
|
+
s: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const getStorage: () => BrowserStorage | NodeStorage;
|
|
10
|
+
export declare const groupOfAddress: (address: string) => number;
|
|
11
|
+
export declare function tokenIdFromAddress(address: string): string;
|
|
@@ -0,0 +1,135 @@
|
|
|
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.tokenIdFromAddress = exports.groupOfAddress = exports.getStorage = exports.signatureDecode = exports.signatureEncode = void 0;
|
|
24
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
25
|
+
const bs58_1 = __importDefault(require("./bs58"));
|
|
26
|
+
const storage_node_1 = __importDefault(require("./storage-node"));
|
|
27
|
+
const storage_browser_1 = __importDefault(require("./storage-browser"));
|
|
28
|
+
const constants_1 = require("./constants");
|
|
29
|
+
const djb2_1 = __importDefault(require("./djb2"));
|
|
30
|
+
const signatureEncode = (ec, signature) => {
|
|
31
|
+
let sNormalized = signature.s;
|
|
32
|
+
if (ec.n && signature.s.cmp(ec.nh) === 1) {
|
|
33
|
+
sNormalized = ec.n.sub(signature.s);
|
|
34
|
+
}
|
|
35
|
+
const r = signature.r.toArrayLike(Buffer, 'be', 33).slice(1);
|
|
36
|
+
const s = sNormalized.toArrayLike(Buffer, 'be', 33).slice(1);
|
|
37
|
+
const xs = new Uint8Array(r.byteLength + s.byteLength);
|
|
38
|
+
xs.set(new Uint8Array(r), 0);
|
|
39
|
+
xs.set(new Uint8Array(s), r.byteLength);
|
|
40
|
+
return Buffer.from(xs).toString('hex');
|
|
41
|
+
};
|
|
42
|
+
exports.signatureEncode = signatureEncode;
|
|
43
|
+
// the signature should be in hex string format for 64 bytes
|
|
44
|
+
const signatureDecode = (ec, signature) => {
|
|
45
|
+
if (signature.length !== 128) {
|
|
46
|
+
throw new Error('Invalid signature length');
|
|
47
|
+
}
|
|
48
|
+
const sHex = signature.slice(64, 128);
|
|
49
|
+
const s = new bn_js_1.default(sHex, 'hex');
|
|
50
|
+
if (ec.n && s.cmp(ec.nh) < 1) {
|
|
51
|
+
const decoded = { r: signature.slice(0, 64), s: sHex };
|
|
52
|
+
return decoded;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
throw new Error('The signature is not normalized');
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
exports.signatureDecode = signatureDecode;
|
|
59
|
+
const getStorage = () => {
|
|
60
|
+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
61
|
+
return isBrowser ? new storage_browser_1.default() : new storage_node_1.default();
|
|
62
|
+
};
|
|
63
|
+
exports.getStorage = getStorage;
|
|
64
|
+
const xorByte = (intValue) => {
|
|
65
|
+
const byte0 = (intValue >> 24) & 0xff;
|
|
66
|
+
const byte1 = (intValue >> 16) & 0xff;
|
|
67
|
+
const byte2 = (intValue >> 8) & 0xff;
|
|
68
|
+
const byte3 = intValue & 0xff;
|
|
69
|
+
return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff;
|
|
70
|
+
};
|
|
71
|
+
var AddressType;
|
|
72
|
+
(function (AddressType) {
|
|
73
|
+
AddressType[AddressType["P2PKH"] = 0] = "P2PKH";
|
|
74
|
+
AddressType[AddressType["P2MPKH"] = 1] = "P2MPKH";
|
|
75
|
+
AddressType[AddressType["P2SH"] = 2] = "P2SH";
|
|
76
|
+
AddressType[AddressType["P2C"] = 3] = "P2C";
|
|
77
|
+
})(AddressType || (AddressType = {}));
|
|
78
|
+
const groupOfAddress = (address) => {
|
|
79
|
+
const decoded = bs58_1.default.decode(address);
|
|
80
|
+
if (decoded.length == 0)
|
|
81
|
+
throw new Error('Address string is empty');
|
|
82
|
+
const addressType = decoded[0];
|
|
83
|
+
const addressBody = decoded.slice(1);
|
|
84
|
+
if (addressType == AddressType.P2PKH) {
|
|
85
|
+
return groupOfP2pkhAddress(addressBody);
|
|
86
|
+
}
|
|
87
|
+
else if (addressType == AddressType.P2MPKH) {
|
|
88
|
+
return groupOfP2mpkhAddress(addressBody);
|
|
89
|
+
}
|
|
90
|
+
else if (addressType == AddressType.P2SH) {
|
|
91
|
+
return groupOfP2shAddress(addressBody);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
throw new Error(`Invalid asset address type: ${addressType}`);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
exports.groupOfAddress = groupOfAddress;
|
|
98
|
+
const groupOfAddressBytes = (bytes) => {
|
|
99
|
+
const hint = (0, djb2_1.default)(bytes) | 1;
|
|
100
|
+
const hash = xorByte(hint);
|
|
101
|
+
const group = hash % constants_1.TOTAL_NUMBER_OF_GROUPS;
|
|
102
|
+
return group;
|
|
103
|
+
};
|
|
104
|
+
// Pay to public key hash address
|
|
105
|
+
const groupOfP2pkhAddress = (address) => {
|
|
106
|
+
if (address.length != 32) {
|
|
107
|
+
throw new Error(`Invalid p2pkh address length: ${address.length}`);
|
|
108
|
+
}
|
|
109
|
+
return groupOfAddressBytes(address);
|
|
110
|
+
};
|
|
111
|
+
// Pay to multiple public key hash address
|
|
112
|
+
const groupOfP2mpkhAddress = (address) => {
|
|
113
|
+
if ((address.length - 2) % 32 != 0) {
|
|
114
|
+
throw new Error(`Invalid p2mpkh address length: ${address.length}`);
|
|
115
|
+
}
|
|
116
|
+
return groupOfAddressBytes(address.slice(1, 33));
|
|
117
|
+
};
|
|
118
|
+
// Pay to script hash address
|
|
119
|
+
const groupOfP2shAddress = (address) => {
|
|
120
|
+
return groupOfAddressBytes(address);
|
|
121
|
+
};
|
|
122
|
+
function tokenIdFromAddress(address) {
|
|
123
|
+
const decoded = bs58_1.default.decode(address);
|
|
124
|
+
if (decoded.length == 0)
|
|
125
|
+
throw new Error('Address string is empty');
|
|
126
|
+
const addressType = decoded[0];
|
|
127
|
+
const addressBody = decoded.slice(1);
|
|
128
|
+
if (addressType == AddressType.P2C) {
|
|
129
|
+
return Buffer.from(addressBody).toString('hex');
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
throw new Error(`Invalid contract address type: ${addressType}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.tokenIdFromAddress = tokenIdFromAddress;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
declare type WalletProps = {
|
|
3
|
+
address: string;
|
|
4
|
+
publicKey: string;
|
|
5
|
+
privateKey: string;
|
|
6
|
+
seed: Buffer;
|
|
7
|
+
mnemonic: string;
|
|
8
|
+
};
|
|
9
|
+
export declare class Wallet {
|
|
10
|
+
readonly address: string;
|
|
11
|
+
readonly publicKey: string;
|
|
12
|
+
readonly privateKey: string;
|
|
13
|
+
readonly seed: Buffer;
|
|
14
|
+
readonly mnemonic: string;
|
|
15
|
+
constructor({ address, publicKey, privateKey, seed, mnemonic }: WalletProps);
|
|
16
|
+
encrypt: (password: string) => string;
|
|
17
|
+
}
|
|
18
|
+
export declare const getPath: (addressIndex?: number | undefined) => string;
|
|
19
|
+
export declare const getWalletFromMnemonic: (mnemonic: string) => Wallet;
|
|
20
|
+
export declare type AddressAndKeys = {
|
|
21
|
+
address: string;
|
|
22
|
+
publicKey: string;
|
|
23
|
+
privateKey: string;
|
|
24
|
+
addressIndex: number;
|
|
25
|
+
};
|
|
26
|
+
export declare const deriveNewAddressData: (seed: Buffer, forGroup?: number | undefined, addressIndex?: number | undefined, skipAddressIndexes?: number[]) => AddressAndKeys;
|
|
27
|
+
export declare const walletGenerate: () => Wallet;
|
|
28
|
+
export declare const walletImport: (mnemonic: string) => Wallet;
|
|
29
|
+
export declare const walletOpen: (password: string, encryptedWallet: string) => Wallet;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,142 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
27
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
28
|
+
}) : function(o, v) {
|
|
29
|
+
o["default"] = v;
|
|
30
|
+
});
|
|
31
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
32
|
+
if (mod && mod.__esModule) return mod;
|
|
33
|
+
var result = {};
|
|
34
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
35
|
+
__setModuleDefault(result, mod);
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.walletOpen = exports.walletImport = exports.walletGenerate = exports.deriveNewAddressData = exports.getWalletFromMnemonic = exports.getPath = exports.Wallet = void 0;
|
|
43
|
+
const bip32 = __importStar(require("bip32"));
|
|
44
|
+
const bip39 = __importStar(require("bip39"));
|
|
45
|
+
const blakejs_1 = __importDefault(require("blakejs"));
|
|
46
|
+
const bs58_1 = __importDefault(require("./bs58"));
|
|
47
|
+
const password_crypto_1 = require("./password-crypto");
|
|
48
|
+
const constants_1 = require("./constants");
|
|
49
|
+
const address_1 = require("./address");
|
|
50
|
+
class StoredState {
|
|
51
|
+
constructor({ mnemonic }) {
|
|
52
|
+
this.version = 1;
|
|
53
|
+
this.mnemonic = mnemonic;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
class Wallet {
|
|
57
|
+
constructor({ address, publicKey, privateKey, seed, mnemonic }) {
|
|
58
|
+
this.encrypt = (password) => {
|
|
59
|
+
const storedState = new StoredState({
|
|
60
|
+
mnemonic: this.mnemonic
|
|
61
|
+
});
|
|
62
|
+
return (0, password_crypto_1.encrypt)(password, JSON.stringify(storedState));
|
|
63
|
+
};
|
|
64
|
+
this.address = address;
|
|
65
|
+
this.publicKey = publicKey;
|
|
66
|
+
this.privateKey = privateKey;
|
|
67
|
+
this.seed = seed;
|
|
68
|
+
this.mnemonic = mnemonic;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.Wallet = Wallet;
|
|
72
|
+
const getPath = (addressIndex) => {
|
|
73
|
+
if (addressIndex !== undefined &&
|
|
74
|
+
(addressIndex < 0 || !Number.isInteger(addressIndex) || addressIndex.toString().includes('e'))) {
|
|
75
|
+
throw new Error('Invalid address index path level');
|
|
76
|
+
}
|
|
77
|
+
// Being explicit: we always use coinType 1234 no matter the network.
|
|
78
|
+
const coinType = "1234'";
|
|
79
|
+
return `m/44'/${coinType}/0'/0/${addressIndex || '0'}`;
|
|
80
|
+
};
|
|
81
|
+
exports.getPath = getPath;
|
|
82
|
+
const getWalletFromMnemonic = (mnemonic) => {
|
|
83
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
84
|
+
const { address, publicKey, privateKey } = deriveAddressAndKeys(seed);
|
|
85
|
+
return new Wallet({ seed, address, publicKey, privateKey, mnemonic });
|
|
86
|
+
};
|
|
87
|
+
exports.getWalletFromMnemonic = getWalletFromMnemonic;
|
|
88
|
+
const deriveAddressAndKeys = (seed, addressIndex) => {
|
|
89
|
+
const masterKey = bip32.fromSeed(seed);
|
|
90
|
+
const keyPair = masterKey.derivePath((0, exports.getPath)(addressIndex));
|
|
91
|
+
if (!keyPair.privateKey)
|
|
92
|
+
throw new Error('Missing private key');
|
|
93
|
+
const publicKey = keyPair.publicKey.toString('hex');
|
|
94
|
+
const privateKey = keyPair.privateKey.toString('hex');
|
|
95
|
+
const hash = blakejs_1.default.blake2b(Buffer.from(publicKey, 'hex'), undefined, 32);
|
|
96
|
+
const pkhash = Buffer.from(hash);
|
|
97
|
+
const type = Buffer.from([0]);
|
|
98
|
+
const bytes = Buffer.concat([type, pkhash]);
|
|
99
|
+
const address = bs58_1.default.encode(bytes);
|
|
100
|
+
return { address, publicKey, privateKey, addressIndex: addressIndex || 0 };
|
|
101
|
+
};
|
|
102
|
+
const findNextAvailableAddressIndex = (startIndex, skipIndexes = []) => {
|
|
103
|
+
let nextAvailableAddressIndex = startIndex;
|
|
104
|
+
do {
|
|
105
|
+
nextAvailableAddressIndex++;
|
|
106
|
+
} while (skipIndexes.includes(nextAvailableAddressIndex));
|
|
107
|
+
return nextAvailableAddressIndex;
|
|
108
|
+
};
|
|
109
|
+
const deriveNewAddressData = (seed, forGroup, addressIndex, skipAddressIndexes = []) => {
|
|
110
|
+
if (forGroup !== undefined && (forGroup >= constants_1.TOTAL_NUMBER_OF_GROUPS || forGroup < 0 || !Number.isInteger(forGroup))) {
|
|
111
|
+
throw new Error('Invalid group number');
|
|
112
|
+
}
|
|
113
|
+
const initialAddressIndex = addressIndex || 0;
|
|
114
|
+
let nextAddressIndex = skipAddressIndexes.includes(initialAddressIndex)
|
|
115
|
+
? findNextAvailableAddressIndex(initialAddressIndex, skipAddressIndexes)
|
|
116
|
+
: initialAddressIndex;
|
|
117
|
+
let newAddressData = deriveAddressAndKeys(seed, nextAddressIndex);
|
|
118
|
+
while (forGroup !== undefined && (0, address_1.addressToGroup)(newAddressData.address, constants_1.TOTAL_NUMBER_OF_GROUPS) !== forGroup) {
|
|
119
|
+
nextAddressIndex = findNextAvailableAddressIndex(newAddressData.addressIndex, skipAddressIndexes);
|
|
120
|
+
newAddressData = deriveAddressAndKeys(seed, nextAddressIndex);
|
|
121
|
+
}
|
|
122
|
+
return newAddressData;
|
|
123
|
+
};
|
|
124
|
+
exports.deriveNewAddressData = deriveNewAddressData;
|
|
125
|
+
const walletGenerate = () => {
|
|
126
|
+
const mnemonic = bip39.generateMnemonic(256);
|
|
127
|
+
return (0, exports.getWalletFromMnemonic)(mnemonic);
|
|
128
|
+
};
|
|
129
|
+
exports.walletGenerate = walletGenerate;
|
|
130
|
+
const walletImport = (mnemonic) => {
|
|
131
|
+
if (!bip39.validateMnemonic(mnemonic)) {
|
|
132
|
+
throw new Error('Invalid seed phrase');
|
|
133
|
+
}
|
|
134
|
+
return (0, exports.getWalletFromMnemonic)(mnemonic);
|
|
135
|
+
};
|
|
136
|
+
exports.walletImport = walletImport;
|
|
137
|
+
const walletOpen = (password, encryptedWallet) => {
|
|
138
|
+
const dataDecrypted = (0, password_crypto_1.decrypt)(password, encryptedWallet);
|
|
139
|
+
const config = JSON.parse(dataDecrypted);
|
|
140
|
+
return (0, exports.getWalletFromMnemonic)(config.mnemonic);
|
|
141
|
+
};
|
|
142
|
+
exports.walletOpen = walletOpen;
|
package/gitignore
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alephium/web3",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
|
+
"license": "GPL",
|
|
6
|
+
"main": "dist/lib/index.js",
|
|
7
|
+
"browser": "dist/alephium-web3.min.js",
|
|
8
|
+
"types": "dist/lib/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/lib/index.js",
|
|
11
|
+
"./api/alephium": "./dist/api/api-alephium.js",
|
|
12
|
+
"./api/explorer": "./dist/api/api-explorer.js"
|
|
13
|
+
},
|
|
14
|
+
"typesVersions": {
|
|
15
|
+
"*": {
|
|
16
|
+
"api/alephium": [
|
|
17
|
+
"dist/api/api-alephium"
|
|
18
|
+
],
|
|
19
|
+
"api/explorer": [
|
|
20
|
+
"dist/api/api-explorer"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"type": "commonjs",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git@github.com:alephium/alephium-web3.git"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/alephium/alephium-web3",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/alephium/alephium-web3/issues"
|
|
32
|
+
},
|
|
33
|
+
"author": "Alephium dev <dev@alephium.org>",
|
|
34
|
+
"config": {
|
|
35
|
+
"alephium_version": "1.3.0-rc9",
|
|
36
|
+
"explorer_backend_version": "1.6.0-rc8"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "rm -rf dist && npx tsc --build . && webpack",
|
|
40
|
+
"update-schemas": "npm run update-schema:alephium && npm run update-schema:explorer",
|
|
41
|
+
"update-schema:alephium": "npx swagger-typescript-api -t ./api -o ./api -n api-alephium.ts -p https://raw.githubusercontent.com/alephium/alephium/v${npm_package_config_alephium_version}/api/src/main/resources/openapi.json",
|
|
42
|
+
"update-schema:explorer": "npx swagger-typescript-api -t ./api -o ./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",
|
|
43
|
+
"check-versions": "node scripts/check-versions.js ${npm_package_config_alephium_version} ${npm_package_config_explorer_backend_version}",
|
|
44
|
+
"dev": "tsnd --respawn lib/index.ts",
|
|
45
|
+
"lint": "eslint . --ext .ts",
|
|
46
|
+
"lint:fix": "eslint . --fix --ext .ts",
|
|
47
|
+
"jest": "jest --useStderr --silent=false --verbose=true --config jestconfig.json",
|
|
48
|
+
"test": "npm run build && npm run jest",
|
|
49
|
+
"test:watch": "npm run build && npm run jest -- --watch",
|
|
50
|
+
"prepublishOnly": "npm run build",
|
|
51
|
+
"prepack": "node scripts/rename-gitignore.js .gitignore gitignore",
|
|
52
|
+
"postpack": "node scripts/rename-gitignore.js gitignore .gitignore",
|
|
53
|
+
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
|
|
54
|
+
"devnet:start": "node scripts/start-devnet.js ${npm_package_config_alephium_version}",
|
|
55
|
+
"devnet:restart": "npm run devnet:start",
|
|
56
|
+
"devnet:stop": "node scripts/stop-devnet.js"
|
|
57
|
+
},
|
|
58
|
+
"bin": {
|
|
59
|
+
"alephium": "dist/cli/create-project.js"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"base-x": "^4.0.0",
|
|
63
|
+
"bcfg": "~0.1.6",
|
|
64
|
+
"bip32": "^2.0.6",
|
|
65
|
+
"bip39": "^3.0.4",
|
|
66
|
+
"blakejs": "^1.1.1",
|
|
67
|
+
"chalk": "^4.1.2",
|
|
68
|
+
"cross-fetch": "^3.1.5",
|
|
69
|
+
"crypto-js": "^4.1.1",
|
|
70
|
+
"elliptic": "^6.5.4",
|
|
71
|
+
"find-up": "^2.1.0",
|
|
72
|
+
"fs-extra": "^10.0.1"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@types/crypto-js": "^4.1.1",
|
|
76
|
+
"@types/elliptic": "^6.4.13",
|
|
77
|
+
"@types/find-up": "^2.1.0",
|
|
78
|
+
"@types/fs-extra": "^9.0.13",
|
|
79
|
+
"@types/jest": "^27.0.1",
|
|
80
|
+
"@types/mock-fs": "^4.13.1",
|
|
81
|
+
"@types/node": "^16.7.8",
|
|
82
|
+
"@types/rewire": "^2.5.28",
|
|
83
|
+
"@typescript-eslint/eslint-plugin": "^4.30.0",
|
|
84
|
+
"@typescript-eslint/parser": "^4.30.0",
|
|
85
|
+
"babel-eslint": "^10.1.0",
|
|
86
|
+
"buffer": "^6.0.3",
|
|
87
|
+
"crypto-browserify": "^3.12.0",
|
|
88
|
+
"eslint": "^7.32.0",
|
|
89
|
+
"eslint-config-prettier": "^8.3.0",
|
|
90
|
+
"eslint-plugin-header": "^3.1.1",
|
|
91
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
92
|
+
"jest": "^27.1.0",
|
|
93
|
+
"jest-localstorage-mock": "^2.4.18",
|
|
94
|
+
"jest-websocket-mock": "^2.2.1",
|
|
95
|
+
"mock-fs": "^5.1.2",
|
|
96
|
+
"mock-socket": "^9.0.8",
|
|
97
|
+
"prettier": "^2.3.2",
|
|
98
|
+
"rewire": "^6.0.0",
|
|
99
|
+
"shelljs": "^0.8.5",
|
|
100
|
+
"stream-browserify": "^3.0.0",
|
|
101
|
+
"swagger-typescript-api": "^9.2.0",
|
|
102
|
+
"ts-jest": "^27.0.5",
|
|
103
|
+
"ts-node": "^10.2.1",
|
|
104
|
+
"tslib": "^2.3.1",
|
|
105
|
+
"typescript": "^4.4.2",
|
|
106
|
+
"webpack": "^5.72.0",
|
|
107
|
+
"webpack-cli": "^4.9.2"
|
|
108
|
+
},
|
|
109
|
+
"engines": {
|
|
110
|
+
"node": ">=14.0.0",
|
|
111
|
+
"npm": ">=7.0.0"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -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()
|
|
@@ -0,0 +1,24 @@
|
|
|
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 { rename } = require('fs')
|
|
20
|
+
|
|
21
|
+
rename(process.argv[2], process.argv[3], function (error) {
|
|
22
|
+
if (error) console.log(error)
|
|
23
|
+
console.log(`Renamed ${process.argv[2]} to ${process.argv[3]}.`)
|
|
24
|
+
})
|