@alephium/web3 0.2.0-test.0 → 0.2.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/.eslintignore +2 -2
- package/README.md +2 -135
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +152 -24
- package/dist/src/api/api-alephium.js +163 -82
- package/dist/src/api/api-explorer.d.ts +192 -49
- package/dist/src/api/api-explorer.js +195 -34
- package/dist/src/api/index.d.ts +40 -5
- package/dist/src/api/index.js +115 -7
- package/dist/src/api/types.d.ts +23 -0
- package/dist/src/api/types.js +235 -0
- package/dist/src/api/utils.d.ts +6 -0
- package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
- package/dist/src/contract/contract.d.ts +127 -80
- package/dist/src/contract/contract.js +425 -467
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/events.js +2 -1
- package/dist/src/contract/index.js +5 -1
- package/dist/src/contract/ralph.d.ts +5 -4
- package/dist/src/contract/ralph.js +27 -1
- package/dist/src/global.d.ts +7 -0
- package/dist/src/global.js +54 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +23 -1
- package/dist/src/signer/index.d.ts +0 -1
- package/dist/src/signer/index.js +5 -2
- package/dist/src/signer/signer.d.ts +59 -60
- package/dist/src/signer/signer.js +99 -69
- package/dist/src/transaction/index.d.ts +0 -1
- package/dist/src/transaction/index.js +5 -2
- package/dist/src/transaction/status.d.ts +2 -1
- package/dist/src/transaction/status.js +2 -1
- package/dist/src/utils/bs58.d.ts +1 -0
- package/dist/src/utils/bs58.js +13 -1
- package/dist/src/utils/index.d.ts +0 -1
- package/dist/src/utils/index.js +5 -2
- package/dist/src/utils/subscription.d.ts +0 -3
- package/dist/src/utils/subscription.js +0 -1
- package/dist/src/utils/utils.d.ts +6 -11
- package/dist/src/utils/utils.js +22 -26
- package/jest-config.json +11 -0
- package/package.json +11 -47
- package/src/api/api-alephium.ts +219 -33
- package/src/api/api-explorer.ts +275 -52
- package/src/api/index.ts +140 -6
- package/src/api/types.ts +229 -0
- package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
- package/src/contract/contract.ts +663 -581
- package/src/contract/events.ts +8 -7
- package/src/contract/ralph.ts +29 -4
- package/src/global.ts +56 -0
- package/src/index.ts +7 -0
- package/src/signer/index.ts +0 -1
- package/src/signer/signer.ts +165 -134
- package/src/transaction/index.ts +0 -1
- package/src/transaction/status.ts +6 -3
- package/src/utils/bs58.ts +11 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +1 -5
- package/src/utils/utils.ts +15 -23
- package/webpack.config.js +3 -0
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -16
- package/contracts/greeter/greeter.ral +0 -7
- package/contracts/greeter/greeter_interface.ral +0 -3
- package/contracts/greeter_main.ral +0 -9
- package/contracts/main.ral +0 -6
- package/contracts/sub/sub.ral +0 -9
- package/dev/user.conf +0 -29
- package/dist/scripts/create-project.d.ts +0 -2
- package/dist/scripts/create-project.js +0 -124
- package/dist/scripts/rename-gitignore.d.ts +0 -1
- package/dist/scripts/start-devnet.d.ts +0 -1
- package/dist/scripts/start-devnet.js +0 -131
- package/dist/scripts/stop-devnet.d.ts +0 -1
- package/dist/scripts/stop-devnet.js +0 -32
- package/dist/src/signer/node-wallet.d.ts +0 -13
- package/dist/src/signer/node-wallet.js +0 -60
- package/dist/src/test/index.d.ts +0 -7
- package/dist/src/test/index.js +0 -41
- package/dist/src/test/privatekey-wallet.d.ts +0 -12
- package/dist/src/test/privatekey-wallet.js +0 -68
- package/dist/src/transaction/sign-verify.d.ts +0 -2
- package/dist/src/transaction/sign-verify.js +0 -58
- package/dist/src/utils/password-crypto.d.ts +0 -2
- package/dist/src/utils/password-crypto.js +0 -69
- package/gitignore +0 -10
- package/scripts/create-project.ts +0 -136
- package/scripts/start-devnet.js +0 -141
- package/scripts/stop-devnet.js +0 -32
- package/src/contract/ralph.test.ts +0 -178
- package/src/fixtures/address.json +0 -36
- package/src/fixtures/balance.json +0 -9
- package/src/fixtures/self-clique.json +0 -19
- package/src/fixtures/transaction.json +0 -13
- package/src/fixtures/transactions.json +0 -179
- package/src/signer/fixtures/genesis.json +0 -26
- package/src/signer/fixtures/wallets.json +0 -26
- package/src/signer/node-wallet.ts +0 -74
- package/src/test/index.ts +0 -32
- package/src/test/privatekey-wallet.ts +0 -58
- package/src/transaction/sign-verify.test.ts +0 -50
- package/src/transaction/sign-verify.ts +0 -39
- package/src/utils/address.test.ts +0 -47
- package/src/utils/djb2.test.ts +0 -35
- package/src/utils/password-crypto.test.ts +0 -27
- package/src/utils/password-crypto.ts +0 -77
- package/src/utils/utils.test.ts +0 -161
- package/templates/base/README.md +0 -34
- package/templates/base/package.json +0 -35
- package/templates/base/src/greeter.ts +0 -41
- package/templates/base/tsconfig.json +0 -19
- package/templates/react/README.md +0 -34
- package/templates/react/config-overrides.js +0 -18
- package/templates/react/package.json +0 -66
- package/templates/react/src/App.tsx +0 -42
- package/templates/react/src/artifacts/greeter.ral.json +0 -26
- package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
- package/templates/shared/.eslintrc.json +0 -12
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +0 -178
- package/test/events.test.ts +0 -138
- package/test/transaction.test.ts +0 -72
|
@@ -18,7 +18,11 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
*/
|
|
19
19
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
20
20
|
if (k2 === undefined) k2 = k;
|
|
21
|
-
Object.
|
|
21
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
22
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
23
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(o, k2, desc);
|
|
22
26
|
}) : (function(o, m, k, k2) {
|
|
23
27
|
if (k2 === undefined) k2 = k;
|
|
24
28
|
o[k2] = m[k];
|
|
@@ -39,8 +43,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
43
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
44
|
};
|
|
41
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.verifySignedMessage = exports.verifyHexString = exports.
|
|
46
|
+
exports.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.verifyHexString = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = void 0;
|
|
43
47
|
const elliptic_1 = require("elliptic");
|
|
48
|
+
const api_1 = require("../api");
|
|
44
49
|
const utils = __importStar(require("../utils"));
|
|
45
50
|
const utils_1 = require("../utils");
|
|
46
51
|
const blakejs_1 = __importDefault(require("blakejs"));
|
|
@@ -52,109 +57,108 @@ const ec = new elliptic_1.ec('secp256k1');
|
|
|
52
57
|
(0, utils_1.assertType)();
|
|
53
58
|
(0, utils_1.assertType)();
|
|
54
59
|
(0, utils_1.assertType)();
|
|
60
|
+
utils_1.assertType;
|
|
55
61
|
(0, utils_1.assertType)();
|
|
56
|
-
|
|
57
|
-
(
|
|
58
|
-
(
|
|
59
|
-
(
|
|
60
|
-
class SignerWithNodeProvider {
|
|
61
|
-
constructor(provider, alwaysSubmitTx) {
|
|
62
|
-
this.provider = provider;
|
|
63
|
-
this.alwaysSubmitTx = alwaysSubmitTx;
|
|
64
|
-
}
|
|
65
|
-
async getAccount(signerAddress) {
|
|
66
|
-
const accounts = await this.getAccounts();
|
|
67
|
-
const account = accounts.find((a) => a.address === signerAddress);
|
|
68
|
-
if (typeof account === 'undefined') {
|
|
69
|
-
throw new Error('Unmatched signerAddress');
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
return account;
|
|
62
|
+
class SignerProviderSimple {
|
|
63
|
+
getNodeProvider() {
|
|
64
|
+
if (this.nodeProvider === undefined) {
|
|
65
|
+
throw Error('The signer does not contain a node provider');
|
|
73
66
|
}
|
|
67
|
+
return this.nodeProvider;
|
|
68
|
+
}
|
|
69
|
+
async submitTransaction(params) {
|
|
70
|
+
const data = { unsignedTx: params.unsignedTx, signature: params.signature };
|
|
71
|
+
return this.getNodeProvider().transactions.postTransactionsSubmit(data);
|
|
74
72
|
}
|
|
75
|
-
async
|
|
76
|
-
|
|
73
|
+
async signAndSubmitTransferTx(params) {
|
|
74
|
+
const signResult = await this.signTransferTx(params);
|
|
75
|
+
await this.submitTransaction(signResult);
|
|
76
|
+
return signResult;
|
|
77
77
|
}
|
|
78
|
-
async
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return this.provider.transactions.postTransactionsSubmit(params);
|
|
78
|
+
async signAndSubmitDeployContractTx(params) {
|
|
79
|
+
const signResult = await this.signDeployContractTx(params);
|
|
80
|
+
await this.submitTransaction(signResult);
|
|
81
|
+
return signResult;
|
|
83
82
|
}
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
async signAndSubmitExecuteScriptTx(params) {
|
|
84
|
+
const signResult = await this.signExecuteScriptTx(params);
|
|
85
|
+
await this.submitTransaction(signResult);
|
|
86
|
+
return signResult;
|
|
87
|
+
}
|
|
88
|
+
async signAndSubmitUnsignedTx(params) {
|
|
89
|
+
const signResult = await this.signUnsignedTx(params);
|
|
90
|
+
await this.submitTransaction(signResult);
|
|
91
|
+
return signResult;
|
|
86
92
|
}
|
|
87
93
|
async usePublicKey(params) {
|
|
88
94
|
const { signerAddress, ...restParams } = params;
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
throw new Error('Unknown signer address');
|
|
95
|
+
const selectedAccount = await this.getSelectedAccount();
|
|
96
|
+
if (signerAddress !== selectedAccount.address) {
|
|
97
|
+
throw new Error('The signer address is not the selected address');
|
|
93
98
|
}
|
|
94
99
|
else {
|
|
95
|
-
return { fromPublicKey:
|
|
100
|
+
return { fromPublicKey: selectedAccount.publicKey, ...restParams };
|
|
96
101
|
}
|
|
97
102
|
}
|
|
98
103
|
async signTransferTx(params) {
|
|
99
104
|
const response = await this.buildTransferTx(params);
|
|
100
|
-
|
|
105
|
+
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
106
|
+
return { ...response, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
101
107
|
}
|
|
102
108
|
async buildTransferTx(params) {
|
|
103
|
-
|
|
109
|
+
const data = {
|
|
110
|
+
...(await this.usePublicKey(params)),
|
|
111
|
+
destinations: toApiDestinations(params.destinations),
|
|
112
|
+
gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
|
|
113
|
+
};
|
|
114
|
+
return this.getNodeProvider().transactions.postTransactionsBuild(data);
|
|
104
115
|
}
|
|
105
116
|
async signDeployContractTx(params) {
|
|
106
117
|
const response = await this.buildContractCreationTx(params);
|
|
107
|
-
const
|
|
118
|
+
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
108
119
|
const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress));
|
|
109
|
-
return { ...
|
|
120
|
+
return { ...response, contractId, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
110
121
|
}
|
|
111
122
|
async buildContractCreationTx(params) {
|
|
112
|
-
|
|
123
|
+
const data = {
|
|
124
|
+
...(await this.usePublicKey(params)),
|
|
125
|
+
initialAttoAlphAmount: (0, api_1.toApiNumber256Optional)(params.initialAttoAlphAmount),
|
|
126
|
+
initialTokenAmounts: (0, api_1.toApiTokens)(params.initialTokenAmounts),
|
|
127
|
+
issueTokenAmount: (0, api_1.toApiNumber256Optional)(params.issueTokenAmount),
|
|
128
|
+
gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
|
|
129
|
+
};
|
|
130
|
+
return this.getNodeProvider().contracts.postContractsUnsignedTxDeployContract(data);
|
|
113
131
|
}
|
|
114
132
|
async signExecuteScriptTx(params) {
|
|
115
133
|
const response = await this.buildScriptTx(params);
|
|
116
|
-
|
|
134
|
+
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
135
|
+
return { ...response, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
117
136
|
}
|
|
118
137
|
async buildScriptTx(params) {
|
|
119
|
-
|
|
138
|
+
const data = {
|
|
139
|
+
...(await this.usePublicKey(params)),
|
|
140
|
+
attoAlphAmount: (0, api_1.toApiNumber256Optional)(params.attoAlphAmount),
|
|
141
|
+
tokens: (0, api_1.toApiTokens)(params.tokens),
|
|
142
|
+
gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
|
|
143
|
+
};
|
|
144
|
+
return this.getNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data);
|
|
120
145
|
}
|
|
121
146
|
// in general, wallet should show the decoded information to user for confirmation
|
|
122
147
|
// please overwrite this function for real wallet
|
|
123
148
|
async signUnsignedTx(params) {
|
|
124
149
|
const data = { unsignedTx: params.unsignedTx };
|
|
125
|
-
const decoded = await this.
|
|
126
|
-
|
|
150
|
+
const decoded = await this.getNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data);
|
|
151
|
+
const signature = await this.signRaw(params.signerAddress, decoded.unsignedTx.txId);
|
|
152
|
+
return {
|
|
127
153
|
fromGroup: decoded.fromGroup,
|
|
128
154
|
toGroup: decoded.toGroup,
|
|
129
|
-
signerAddress: params.signerAddress,
|
|
130
155
|
unsignedTx: params.unsignedTx,
|
|
131
|
-
txId: decoded.unsignedTx.txId
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
async handleSign(response, submitTx) {
|
|
136
|
-
// sign the tx
|
|
137
|
-
const signature = await this.signRaw(response.signerAddress, response.txId);
|
|
138
|
-
// submit the tx if required
|
|
139
|
-
if (submitTx) {
|
|
140
|
-
await this.provider.transactions.postTransactionsSubmit({
|
|
141
|
-
unsignedTx: response.unsignedTx,
|
|
142
|
-
signature: signature
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
// return the signature back to the provider
|
|
146
|
-
return {
|
|
147
|
-
fromGroup: response.fromGroup,
|
|
148
|
-
toGroup: response.toGroup,
|
|
149
|
-
unsignedTx: response.unsignedTx,
|
|
150
|
-
txId: response.txId,
|
|
151
|
-
signature: signature
|
|
156
|
+
txId: decoded.unsignedTx.txId,
|
|
157
|
+
signature,
|
|
158
|
+
gasAmount: decoded.unsignedTx.gasAmount,
|
|
159
|
+
gasPrice: (0, api_1.fromApiNumber256)(decoded.unsignedTx.gasPrice)
|
|
152
160
|
};
|
|
153
161
|
}
|
|
154
|
-
async signHexString(params) {
|
|
155
|
-
const signature = await this.signRaw(params.signerAddress, params.hexString);
|
|
156
|
-
return { signature: signature };
|
|
157
|
-
}
|
|
158
162
|
async signMessage(params) {
|
|
159
163
|
const extendedMessage = extendMessage(params.message);
|
|
160
164
|
const messageHash = blakejs_1.default.blake2b(extendedMessage, undefined, 32);
|
|
@@ -162,7 +166,20 @@ class SignerWithNodeProvider {
|
|
|
162
166
|
return { signature: signature };
|
|
163
167
|
}
|
|
164
168
|
}
|
|
165
|
-
exports.
|
|
169
|
+
exports.SignerProviderSimple = SignerProviderSimple;
|
|
170
|
+
class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
|
|
171
|
+
async getAccount(signerAddress) {
|
|
172
|
+
const accounts = await this.getAccounts();
|
|
173
|
+
const account = accounts.find((a) => a.address === signerAddress);
|
|
174
|
+
if (typeof account === 'undefined') {
|
|
175
|
+
throw new Error('Unmatched signerAddress');
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
return account;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
exports.SignerProviderWithMultipleAccounts = SignerProviderWithMultipleAccounts;
|
|
166
183
|
function verifyHexString(hexString, publicKey, signature) {
|
|
167
184
|
try {
|
|
168
185
|
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
@@ -182,3 +199,16 @@ function verifySignedMessage(message, publicKey, signature) {
|
|
|
182
199
|
return verifyHexString(utils.binToHex(messageHash), publicKey, signature);
|
|
183
200
|
}
|
|
184
201
|
exports.verifySignedMessage = verifySignedMessage;
|
|
202
|
+
utils_1.assertType;
|
|
203
|
+
function toApiDestination(data) {
|
|
204
|
+
return { ...data, attoAlphAmount: (0, api_1.toApiNumber256)(data.attoAlphAmount), tokens: (0, api_1.toApiTokens)(data.tokens) };
|
|
205
|
+
}
|
|
206
|
+
exports.toApiDestination = toApiDestination;
|
|
207
|
+
function toApiDestinations(data) {
|
|
208
|
+
return data.map(toApiDestination);
|
|
209
|
+
}
|
|
210
|
+
exports.toApiDestinations = toApiDestinations;
|
|
211
|
+
function fromApiDestination(data) {
|
|
212
|
+
return { ...data, attoAlphAmount: (0, api_1.fromApiNumber256)(data.attoAlphAmount), tokens: (0, api_1.fromApiTokens)(data.tokens) };
|
|
213
|
+
}
|
|
214
|
+
exports.fromApiDestination = fromApiDestination;
|
|
@@ -18,7 +18,11 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
*/
|
|
19
19
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
20
20
|
if (k2 === undefined) k2 = k;
|
|
21
|
-
Object.
|
|
21
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
22
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
23
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(o, k2, desc);
|
|
22
26
|
}) : (function(o, m, k, k2) {
|
|
23
27
|
if (k2 === undefined) k2 = k;
|
|
24
28
|
o[k2] = m[k];
|
|
@@ -27,5 +31,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
27
31
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
28
32
|
};
|
|
29
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
__exportStar(require("./sign-verify"), exports);
|
|
31
34
|
__exportStar(require("./status"), exports);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { node } from '../api';
|
|
2
2
|
import { Subscription, SubscribeOptions } from '../utils';
|
|
3
|
+
export declare type TxStatus = node.TxStatus;
|
|
3
4
|
export declare class TxStatusSubscription extends Subscription<TxStatus> {
|
|
4
5
|
readonly txId: string;
|
|
5
6
|
readonly fromGroup?: number;
|
|
@@ -18,6 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
*/
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
20
|
exports.subscribeToTxStatus = exports.TxStatusSubscription = void 0;
|
|
21
|
+
const __1 = require("..");
|
|
21
22
|
const utils_1 = require("../utils");
|
|
22
23
|
class TxStatusSubscription extends utils_1.Subscription {
|
|
23
24
|
constructor(options, txId, fromGroup, toGroup) {
|
|
@@ -29,7 +30,7 @@ class TxStatusSubscription extends utils_1.Subscription {
|
|
|
29
30
|
}
|
|
30
31
|
async polling() {
|
|
31
32
|
try {
|
|
32
|
-
const txStatus = await
|
|
33
|
+
const txStatus = await __1.web3.getCurrentNodeProvider().transactions.getTransactionsStatus({
|
|
33
34
|
txId: this.txId,
|
|
34
35
|
fromGroup: this.fromGroup,
|
|
35
36
|
toGroup: this.toGroup
|
package/dist/src/utils/bs58.d.ts
CHANGED
package/dist/src/utils/bs58.js
CHANGED
|
@@ -20,9 +20,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
20
20
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.bs58 = void 0;
|
|
23
|
+
exports.isBase58 = exports.bs58 = void 0;
|
|
24
24
|
/** This source is under MIT License and come originally from https://github.com/cryptocoinjs/bs58 **/
|
|
25
25
|
const base_x_1 = __importDefault(require("base-x"));
|
|
26
26
|
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
27
27
|
exports.bs58 = (0, base_x_1.default)(ALPHABET);
|
|
28
|
+
function isBase58(s) {
|
|
29
|
+
if (s === '' || s.trim() === '') {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
return exports.bs58.encode(exports.bs58.decode(s)) === s;
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.isBase58 = isBase58;
|
|
28
40
|
exports.default = exports.bs58;
|
package/dist/src/utils/index.js
CHANGED
|
@@ -18,7 +18,11 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
*/
|
|
19
19
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
20
20
|
if (k2 === undefined) k2 = k;
|
|
21
|
-
Object.
|
|
21
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
22
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
23
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(o, k2, desc);
|
|
22
26
|
}) : (function(o, m, k, k2) {
|
|
23
27
|
if (k2 === undefined) k2 = k;
|
|
24
28
|
o[k2] = m[k];
|
|
@@ -30,6 +34,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
30
34
|
__exportStar(require("./address"), exports);
|
|
31
35
|
__exportStar(require("./bs58"), exports);
|
|
32
36
|
__exportStar(require("./djb2"), exports);
|
|
33
|
-
__exportStar(require("./password-crypto"), exports);
|
|
34
37
|
__exportStar(require("./utils"), exports);
|
|
35
38
|
__exportStar(require("./subscription"), exports);
|
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import EventEmitter from 'eventemitter3';
|
|
2
|
-
import { NodeProvider } from '../api';
|
|
3
2
|
declare type MessageCallback<Message> = (message: Message) => Promise<void>;
|
|
4
3
|
declare type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>;
|
|
5
4
|
export interface SubscribeOptions<Message> {
|
|
6
|
-
provider: NodeProvider;
|
|
7
5
|
pollingInterval: number;
|
|
8
6
|
messageCallback: MessageCallback<Message>;
|
|
9
7
|
errorCallback: ErrorCallback<Message>;
|
|
10
8
|
}
|
|
11
9
|
export declare abstract class Subscription<Message> {
|
|
12
|
-
provider: NodeProvider;
|
|
13
10
|
pollingInterval: number;
|
|
14
11
|
protected messageCallback: MessageCallback<Message>;
|
|
15
12
|
protected errorCallback: ErrorCallback<Message>;
|
|
@@ -24,7 +24,6 @@ exports.Subscription = void 0;
|
|
|
24
24
|
const eventemitter3_1 = __importDefault(require("eventemitter3"));
|
|
25
25
|
class Subscription {
|
|
26
26
|
constructor(options) {
|
|
27
|
-
this.provider = options.provider;
|
|
28
27
|
this.pollingInterval = options.pollingInterval;
|
|
29
28
|
this.messageCallback = options.messageCallback;
|
|
30
29
|
this.errorCallback = options.errorCallback;
|
|
@@ -1,27 +1,22 @@
|
|
|
1
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
2
|
export declare function signatureEncode(signature: EC.Signature): string;
|
|
10
|
-
export declare
|
|
3
|
+
export declare function signatureDecode(ec: EC, signature: string): SignatureInput;
|
|
4
|
+
export declare function xorByte(intValue: number): number;
|
|
11
5
|
export declare function isHexString(input: string): boolean;
|
|
12
|
-
export declare
|
|
6
|
+
export declare function groupOfAddress(address: string): number;
|
|
13
7
|
export declare function contractIdFromAddress(address: string): Uint8Array;
|
|
14
8
|
export declare function tokenIdFromAddress(address: string): Uint8Array;
|
|
15
9
|
export declare function hexToBinUnsafe(hex: string): Uint8Array;
|
|
16
10
|
export declare function binToHex(bin: Uint8Array): string;
|
|
11
|
+
export declare function groupOfPrivateKey(privateKey: string): number;
|
|
17
12
|
export declare function publicKeyFromPrivateKey(privateKey: string): string;
|
|
18
13
|
export declare function addressFromPublicKey(publicKey: string): string;
|
|
19
14
|
export declare function addressFromContractId(contractId: string): string;
|
|
20
15
|
export declare function contractIdFromTx(txId: string, outputIndex: number): string;
|
|
21
16
|
export declare function subContractId(parentContractId: string, pathInHex: string): string;
|
|
22
17
|
export declare function stringToHex(str: string): string;
|
|
23
|
-
export declare function hexToString(str:
|
|
24
|
-
export declare function timeout(ms: number): Promise<
|
|
18
|
+
export declare function hexToString(str: string): string;
|
|
19
|
+
export declare function timeout(ms: number): Promise<void>;
|
|
25
20
|
declare type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
26
21
|
export declare type Eq<X, Y> = _Eq<{
|
|
27
22
|
[P in keyof X]: X[P];
|
package/dist/src/utils/utils.js
CHANGED
|
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
20
20
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.assertType = exports.timeout = exports.hexToString = 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.
|
|
23
|
+
exports.assertType = exports.timeout = exports.hexToString = exports.stringToHex = exports.subContractId = exports.contractIdFromTx = exports.addressFromContractId = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.groupOfPrivateKey = exports.binToHex = exports.hexToBinUnsafe = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isHexString = exports.xorByte = exports.signatureDecode = exports.signatureEncode = void 0;
|
|
24
24
|
const elliptic_1 = require("elliptic");
|
|
25
25
|
const bn_js_1 = __importDefault(require("bn.js"));
|
|
26
26
|
const blakejs_1 = __importDefault(require("blakejs"));
|
|
@@ -29,15 +29,6 @@ const buffer_1 = require("buffer/");
|
|
|
29
29
|
const constants_1 = require("../constants");
|
|
30
30
|
const djb2_1 = __importDefault(require("./djb2"));
|
|
31
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
32
|
function signatureEncode(signature) {
|
|
42
33
|
let sNormalized = signature.s;
|
|
43
34
|
if (ec.n && signature.s.cmp(ec.nh) === 1) {
|
|
@@ -49,7 +40,7 @@ function signatureEncode(signature) {
|
|
|
49
40
|
}
|
|
50
41
|
exports.signatureEncode = signatureEncode;
|
|
51
42
|
// the signature should be in hex string format for 64 bytes
|
|
52
|
-
|
|
43
|
+
function signatureDecode(ec, signature) {
|
|
53
44
|
if (signature.length !== 128) {
|
|
54
45
|
throw new Error('Invalid signature length');
|
|
55
46
|
}
|
|
@@ -62,15 +53,16 @@ const signatureDecode = (ec, signature) => {
|
|
|
62
53
|
else {
|
|
63
54
|
throw new Error('The signature is not normalized');
|
|
64
55
|
}
|
|
65
|
-
}
|
|
56
|
+
}
|
|
66
57
|
exports.signatureDecode = signatureDecode;
|
|
67
|
-
|
|
58
|
+
function xorByte(intValue) {
|
|
68
59
|
const byte0 = (intValue >> 24) & 0xff;
|
|
69
60
|
const byte1 = (intValue >> 16) & 0xff;
|
|
70
61
|
const byte2 = (intValue >> 8) & 0xff;
|
|
71
62
|
const byte3 = intValue & 0xff;
|
|
72
63
|
return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff;
|
|
73
|
-
}
|
|
64
|
+
}
|
|
65
|
+
exports.xorByte = xorByte;
|
|
74
66
|
function isHexString(input) {
|
|
75
67
|
return input.length % 2 === 0 && /[0-9a-f]*$/.test(input);
|
|
76
68
|
}
|
|
@@ -82,7 +74,7 @@ var AddressType;
|
|
|
82
74
|
AddressType[AddressType["P2SH"] = 2] = "P2SH";
|
|
83
75
|
AddressType[AddressType["P2C"] = 3] = "P2C";
|
|
84
76
|
})(AddressType || (AddressType = {}));
|
|
85
|
-
|
|
77
|
+
function groupOfAddress(address) {
|
|
86
78
|
const decoded = bs58_1.default.decode(address);
|
|
87
79
|
if (decoded.length == 0)
|
|
88
80
|
throw new Error('Address string is empty');
|
|
@@ -100,32 +92,32 @@ const groupOfAddress = (address) => {
|
|
|
100
92
|
else {
|
|
101
93
|
throw new Error(`Invalid asset address type: ${addressType}`);
|
|
102
94
|
}
|
|
103
|
-
}
|
|
95
|
+
}
|
|
104
96
|
exports.groupOfAddress = groupOfAddress;
|
|
105
|
-
|
|
97
|
+
function groupOfAddressBytes(bytes) {
|
|
106
98
|
const hint = (0, djb2_1.default)(bytes) | 1;
|
|
107
99
|
const hash = xorByte(hint);
|
|
108
100
|
const group = hash % constants_1.TOTAL_NUMBER_OF_GROUPS;
|
|
109
101
|
return group;
|
|
110
|
-
}
|
|
102
|
+
}
|
|
111
103
|
// Pay to public key hash address
|
|
112
|
-
|
|
104
|
+
function groupOfP2pkhAddress(address) {
|
|
113
105
|
if (address.length != 32) {
|
|
114
106
|
throw new Error(`Invalid p2pkh address length: ${address.length}`);
|
|
115
107
|
}
|
|
116
108
|
return groupOfAddressBytes(address);
|
|
117
|
-
}
|
|
109
|
+
}
|
|
118
110
|
// Pay to multiple public key hash address
|
|
119
|
-
|
|
111
|
+
function groupOfP2mpkhAddress(address) {
|
|
120
112
|
if ((address.length - 2) % 32 != 0) {
|
|
121
113
|
throw new Error(`Invalid p2mpkh address length: ${address.length}`);
|
|
122
114
|
}
|
|
123
115
|
return groupOfAddressBytes(address.slice(1, 33));
|
|
124
|
-
}
|
|
116
|
+
}
|
|
125
117
|
// Pay to script hash address
|
|
126
|
-
|
|
118
|
+
function groupOfP2shAddress(address) {
|
|
127
119
|
return groupOfAddressBytes(address);
|
|
128
|
-
}
|
|
120
|
+
}
|
|
129
121
|
function contractIdFromAddress(address) {
|
|
130
122
|
return idFromAddress(address);
|
|
131
123
|
}
|
|
@@ -155,6 +147,10 @@ function binToHex(bin) {
|
|
|
155
147
|
return buffer_1.Buffer.from(bin).toString('hex');
|
|
156
148
|
}
|
|
157
149
|
exports.binToHex = binToHex;
|
|
150
|
+
function groupOfPrivateKey(privateKey) {
|
|
151
|
+
return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey)));
|
|
152
|
+
}
|
|
153
|
+
exports.groupOfPrivateKey = groupOfPrivateKey;
|
|
158
154
|
function publicKeyFromPrivateKey(privateKey) {
|
|
159
155
|
const key = ec.keyFromPrivate(privateKey);
|
|
160
156
|
return key.getPublic(true, 'hex');
|
|
@@ -182,7 +178,7 @@ function contractIdFromTx(txId, outputIndex) {
|
|
|
182
178
|
}
|
|
183
179
|
exports.contractIdFromTx = contractIdFromTx;
|
|
184
180
|
function subContractId(parentContractId, pathInHex) {
|
|
185
|
-
const data = buffer_1.Buffer.concat([hexToBinUnsafe(
|
|
181
|
+
const data = buffer_1.Buffer.concat([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)]);
|
|
186
182
|
return binToHex(blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32));
|
|
187
183
|
}
|
|
188
184
|
exports.subContractId = subContractId;
|
|
@@ -195,7 +191,7 @@ function stringToHex(str) {
|
|
|
195
191
|
}
|
|
196
192
|
exports.stringToHex = stringToHex;
|
|
197
193
|
function hexToString(str) {
|
|
198
|
-
return buffer_1.Buffer.from(str
|
|
194
|
+
return buffer_1.Buffer.from(str, 'hex').toString();
|
|
199
195
|
}
|
|
200
196
|
exports.hexToString = hexToString;
|
|
201
197
|
function timeout(ms) {
|
package/jest-config.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"testPathIgnorePatterns": [".*/node_modules/"],
|
|
3
|
+
"transform": {
|
|
4
|
+
"^.+\\.(t|j)sx?$": "ts-jest"
|
|
5
|
+
},
|
|
6
|
+
"testMatch": ["**/*.test.ts"],
|
|
7
|
+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
|
|
8
|
+
"collectCoverage": true,
|
|
9
|
+
"coverageDirectory": "./coverage/",
|
|
10
|
+
"collectCoverageFrom": ["src/**/*.ts"]
|
|
11
|
+
}
|