@alephium/web3 0.0.3 → 0.1.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +13 -0
- package/.eslintignore +2 -0
- package/.eslintrc.json +21 -0
- package/README.md +2 -2
- package/contracts/add.ral +7 -3
- package/contracts/greeter.ral +3 -1
- package/contracts/greeter_interface.ral +3 -0
- package/contracts/greeter_main.ral +9 -0
- package/contracts/main.ral +3 -5
- package/dev/user.conf +8 -4
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/scripts/check-versions.d.ts +1 -0
- package/dist/scripts/check-versions.js +39 -0
- package/dist/{cli → scripts}/create-project.d.ts +0 -0
- package/dist/scripts/create-project.js +124 -0
- package/dist/scripts/header.d.ts +0 -0
- package/dist/scripts/header.js +18 -0
- package/dist/scripts/rename-gitignore.d.ts +1 -0
- package/dist/scripts/rename-gitignore.js +24 -0
- package/dist/scripts/start-devnet.d.ts +1 -0
- package/dist/scripts/start-devnet.js +131 -0
- package/dist/scripts/stop-devnet.d.ts +1 -0
- package/dist/scripts/stop-devnet.js +32 -0
- package/dist/{api → src/api}/api-alephium.d.ts +287 -168
- package/dist/{api → src/api}/api-alephium.js +497 -115
- package/dist/{api → src/api}/api-explorer.d.ts +117 -19
- package/dist/{api → src/api}/api-explorer.js +206 -46
- package/dist/src/api/index.d.ts +10 -0
- package/dist/src/api/index.js +55 -0
- package/dist/{lib → src}/constants.d.ts +0 -0
- package/dist/{lib → src}/constants.js +0 -0
- package/dist/src/contract/contract.d.ts +182 -0
- package/dist/src/contract/contract.js +760 -0
- package/dist/src/contract/events.d.ts +29 -0
- package/dist/src/contract/events.js +77 -0
- package/dist/src/contract/index.d.ts +3 -0
- package/dist/src/contract/index.js +32 -0
- package/dist/src/contract/ralph.d.ts +12 -0
- package/dist/src/contract/ralph.js +362 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +34 -0
- package/dist/src/signer/index.d.ts +2 -0
- package/dist/src/signer/index.js +31 -0
- package/dist/src/signer/node-wallet.d.ts +13 -0
- package/dist/src/signer/node-wallet.js +60 -0
- package/dist/src/signer/signer.d.ts +143 -0
- package/dist/src/signer/signer.js +184 -0
- package/dist/src/test/index.d.ts +7 -0
- package/dist/src/test/index.js +41 -0
- package/dist/src/test/privatekey-wallet.d.ts +12 -0
- package/dist/src/test/privatekey-wallet.js +68 -0
- package/dist/{lib → src/utils}/address.d.ts +0 -0
- package/dist/{lib → src/utils}/address.js +1 -1
- package/dist/{lib → src/utils}/bs58.d.ts +2 -2
- package/dist/{lib → src/utils}/bs58.js +3 -1
- package/dist/{lib → src/utils}/djb2.d.ts +0 -0
- package/dist/{lib → src/utils}/djb2.js +1 -1
- package/dist/src/utils/index.d.ts +6 -0
- package/dist/src/utils/index.js +35 -0
- package/dist/{lib → src/utils}/password-crypto.d.ts +0 -0
- package/dist/{lib → src/utils}/password-crypto.js +8 -7
- package/dist/src/utils/transaction.d.ts +2 -0
- package/dist/src/utils/transaction.js +58 -0
- package/dist/src/utils/utils.d.ts +30 -0
- package/dist/{lib → src/utils}/utils.js +83 -19
- package/gitignore +5 -4
- package/package.json +55 -41
- package/scripts/create-project.ts +136 -0
- package/scripts/header.js +17 -0
- package/scripts/start-devnet.js +3 -3
- package/src/api/api-alephium.ts +2323 -0
- package/src/api/api-explorer.ts +808 -0
- package/src/api/index.ts +35 -0
- package/src/constants.ts +20 -0
- package/src/contract/contract.ts +1014 -0
- package/src/contract/events.ts +102 -0
- package/src/contract/index.ts +21 -0
- package/src/contract/ralph.test.ts +178 -0
- package/src/contract/ralph.ts +362 -0
- package/src/fixtures/address.json +36 -0
- package/src/fixtures/balance.json +9 -0
- package/src/fixtures/self-clique.json +19 -0
- package/src/fixtures/transaction.json +13 -0
- package/src/fixtures/transactions.json +179 -0
- package/src/index.ts +24 -0
- package/src/signer/fixtures/genesis.json +26 -0
- package/src/signer/fixtures/wallets.json +26 -0
- package/src/signer/index.ts +20 -0
- package/src/signer/node-wallet.ts +74 -0
- package/src/signer/signer.ts +313 -0
- package/src/test/index.ts +32 -0
- package/src/test/privatekey-wallet.ts +58 -0
- package/src/utils/address.test.ts +47 -0
- package/src/utils/address.ts +39 -0
- package/src/utils/bs58.ts +26 -0
- package/src/utils/djb2.test.ts +35 -0
- package/src/utils/djb2.ts +25 -0
- package/src/utils/index.ts +24 -0
- package/src/utils/password-crypto.test.ts +27 -0
- package/src/utils/password-crypto.ts +77 -0
- package/src/utils/transaction.test.ts +50 -0
- package/src/utils/transaction.ts +39 -0
- package/src/utils/utils.test.ts +160 -0
- package/src/utils/utils.ts +209 -0
- package/templates/{README.md → base/README.md} +4 -4
- package/templates/base/package.json +35 -0
- package/templates/base/src/greeter.ts +41 -0
- package/templates/base/tsconfig.json +19 -0
- package/templates/react/README.md +34 -0
- package/templates/react/config-overrides.js +18 -0
- package/templates/react/package.json +66 -0
- package/templates/react/src/App.tsx +42 -0
- package/templates/react/src/artifacts/greeter.ral.json +26 -0
- package/templates/react/src/artifacts/greeter_main.ral.json +22 -0
- package/templates/shared/.eslintrc.json +12 -0
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +161 -0
- package/test/events.test.ts +139 -0
- package/webpack.config.js +1 -1
- package/contracts/greeter-main.ral +0 -8
- package/dist/cli/create-project.js +0 -87
- package/dist/lib/clique.d.ts +0 -23
- package/dist/lib/clique.js +0 -149
- package/dist/lib/contract.d.ts +0 -152
- package/dist/lib/contract.js +0 -711
- package/dist/lib/explorer.d.ts +0 -8
- package/dist/lib/explorer.js +0 -46
- package/dist/lib/index.d.ts +0 -12
- package/dist/lib/index.js +0 -60
- package/dist/lib/node.d.ts +0 -10
- package/dist/lib/node.js +0 -64
- package/dist/lib/numbers.d.ts +0 -7
- package/dist/lib/numbers.js +0 -128
- package/dist/lib/signer.d.ts +0 -17
- package/dist/lib/signer.js +0 -70
- package/dist/lib/storage-browser.d.ts +0 -9
- package/dist/lib/storage-browser.js +0 -52
- package/dist/lib/storage-node.d.ts +0 -9
- package/dist/lib/storage-node.js +0 -65
- package/dist/lib/utils.d.ts +0 -11
- package/dist/lib/wallet.d.ts +0 -30
- package/dist/lib/wallet.js +0 -142
- package/templates/package.json +0 -29
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { NodeProvider } from '../api';
|
|
2
|
+
import { node } from '../api';
|
|
3
|
+
import { Token } from '../api/api-alephium';
|
|
4
|
+
export interface SignResult {
|
|
5
|
+
fromGroup: number;
|
|
6
|
+
toGroup: number;
|
|
7
|
+
unsignedTx: string;
|
|
8
|
+
txId: string;
|
|
9
|
+
signature: string;
|
|
10
|
+
}
|
|
11
|
+
export interface Account {
|
|
12
|
+
address: string;
|
|
13
|
+
group: number;
|
|
14
|
+
publicKey: string;
|
|
15
|
+
}
|
|
16
|
+
export declare type SubmitTx = {
|
|
17
|
+
submitTx?: boolean;
|
|
18
|
+
};
|
|
19
|
+
export declare type SignerAddress = {
|
|
20
|
+
signerAddress: string;
|
|
21
|
+
};
|
|
22
|
+
export declare type GetAccountsParams = undefined;
|
|
23
|
+
export declare type GetAccountsResult = Account[];
|
|
24
|
+
export interface SignTransferTxParams {
|
|
25
|
+
signerAddress: string;
|
|
26
|
+
destinations: node.Destination[];
|
|
27
|
+
utxos?: node.OutputRef[];
|
|
28
|
+
gasAmount?: number;
|
|
29
|
+
gasPrice?: string;
|
|
30
|
+
submitTx?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface SignTransferTxResult {
|
|
33
|
+
fromGroup: number;
|
|
34
|
+
toGroup: number;
|
|
35
|
+
unsignedTx: string;
|
|
36
|
+
txId: string;
|
|
37
|
+
signature: string;
|
|
38
|
+
}
|
|
39
|
+
export interface SignDeployContractTxParams {
|
|
40
|
+
signerAddress: string;
|
|
41
|
+
bytecode: string;
|
|
42
|
+
initialAlphAmount?: string;
|
|
43
|
+
initialTokenAmounts?: Token[];
|
|
44
|
+
issueTokenAmount?: string;
|
|
45
|
+
gasAmount?: number;
|
|
46
|
+
gasPrice?: string;
|
|
47
|
+
submitTx?: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface SignDeployContractTxResult {
|
|
50
|
+
fromGroup: number;
|
|
51
|
+
toGroup: number;
|
|
52
|
+
unsignedTx: string;
|
|
53
|
+
txId: string;
|
|
54
|
+
signature: string;
|
|
55
|
+
contractId: string;
|
|
56
|
+
contractAddress: string;
|
|
57
|
+
}
|
|
58
|
+
export interface SignExecuteScriptTxParams {
|
|
59
|
+
signerAddress: string;
|
|
60
|
+
bytecode: string;
|
|
61
|
+
alphAmount?: string;
|
|
62
|
+
tokens?: node.Token[];
|
|
63
|
+
gasAmount?: number;
|
|
64
|
+
gasPrice?: string;
|
|
65
|
+
submitTx?: boolean;
|
|
66
|
+
}
|
|
67
|
+
export interface SignExecuteScriptTxResult {
|
|
68
|
+
fromGroup: number;
|
|
69
|
+
toGroup: number;
|
|
70
|
+
unsignedTx: string;
|
|
71
|
+
txId: string;
|
|
72
|
+
signature: string;
|
|
73
|
+
}
|
|
74
|
+
export interface SignUnsignedTxParams {
|
|
75
|
+
signerAddress: string;
|
|
76
|
+
unsignedTx: string;
|
|
77
|
+
submitTx?: boolean;
|
|
78
|
+
}
|
|
79
|
+
export interface SignUnsignedTxResult {
|
|
80
|
+
fromGroup: number;
|
|
81
|
+
toGroup: number;
|
|
82
|
+
unsignedTx: string;
|
|
83
|
+
txId: string;
|
|
84
|
+
signature: string;
|
|
85
|
+
}
|
|
86
|
+
export interface SignHexStringParams {
|
|
87
|
+
signerAddress: string;
|
|
88
|
+
hexString: string;
|
|
89
|
+
}
|
|
90
|
+
export interface SignHexStringResult {
|
|
91
|
+
signature: string;
|
|
92
|
+
}
|
|
93
|
+
export interface SignMessageParams {
|
|
94
|
+
signerAddress: string;
|
|
95
|
+
message: string;
|
|
96
|
+
}
|
|
97
|
+
export interface SignMessageResult {
|
|
98
|
+
signature: string;
|
|
99
|
+
}
|
|
100
|
+
export interface SignerProvider {
|
|
101
|
+
getAccounts(): Promise<Account[]>;
|
|
102
|
+
signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
103
|
+
signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
104
|
+
signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
105
|
+
signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
106
|
+
signHexString(params: SignHexStringParams): Promise<SignHexStringResult>;
|
|
107
|
+
signMessage(params: SignMessageParams): Promise<SignMessageResult>;
|
|
108
|
+
}
|
|
109
|
+
export declare abstract class SignerWithNodeProvider implements SignerProvider {
|
|
110
|
+
readonly provider: NodeProvider;
|
|
111
|
+
alwaysSubmitTx: boolean;
|
|
112
|
+
abstract getAccounts(): Promise<Account[]>;
|
|
113
|
+
getAccount(signerAddress: string): Promise<Account>;
|
|
114
|
+
constructor(provider: NodeProvider, alwaysSubmitTx: boolean);
|
|
115
|
+
private defaultSignerAddress;
|
|
116
|
+
submitTransaction(unsignedTx: string, txHash: string, signerAddress?: string): Promise<SubmissionResult>;
|
|
117
|
+
private shouldSubmitTx;
|
|
118
|
+
private usePublicKey;
|
|
119
|
+
signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
120
|
+
buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult>;
|
|
121
|
+
signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
122
|
+
buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult>;
|
|
123
|
+
signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
124
|
+
buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult>;
|
|
125
|
+
signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
126
|
+
protected handleSign(response: {
|
|
127
|
+
fromGroup: number;
|
|
128
|
+
toGroup: number;
|
|
129
|
+
signerAddress: string;
|
|
130
|
+
unsignedTx: string;
|
|
131
|
+
txId: string;
|
|
132
|
+
}, submitTx: boolean): Promise<SignResult>;
|
|
133
|
+
signHexString(params: SignHexStringParams): Promise<SignHexStringResult>;
|
|
134
|
+
signMessage(params: SignMessageParams): Promise<SignMessageResult>;
|
|
135
|
+
abstract signRaw(signerAddress: string, hexString: string): Promise<string>;
|
|
136
|
+
}
|
|
137
|
+
export interface SubmissionResult {
|
|
138
|
+
txId: string;
|
|
139
|
+
fromGroup: number;
|
|
140
|
+
toGroup: number;
|
|
141
|
+
}
|
|
142
|
+
export declare function verifyHexString(hexString: string, publicKey: string, signature: string): boolean;
|
|
143
|
+
export declare function verifySignedMessage(message: string, publicKey: string, signature: string): boolean;
|
|
@@ -0,0 +1,184 @@
|
|
|
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.verifySignedMessage = exports.verifyHexString = exports.SignerWithNodeProvider = void 0;
|
|
43
|
+
const elliptic_1 = require("elliptic");
|
|
44
|
+
const utils = __importStar(require("../utils"));
|
|
45
|
+
const utils_1 = require("../utils");
|
|
46
|
+
const blakejs_1 = __importDefault(require("blakejs"));
|
|
47
|
+
const ec = new elliptic_1.ec('secp256k1');
|
|
48
|
+
(0, utils_1.assertType)();
|
|
49
|
+
(0, utils_1.assertType)();
|
|
50
|
+
(0, utils_1.assertType)();
|
|
51
|
+
(0, utils_1.assertType)();
|
|
52
|
+
(0, utils_1.assertType)();
|
|
53
|
+
(0, utils_1.assertType)();
|
|
54
|
+
(0, utils_1.assertType)();
|
|
55
|
+
(0, utils_1.assertType)();
|
|
56
|
+
(0, utils_1.assertType)();
|
|
57
|
+
(0, utils_1.assertType)();
|
|
58
|
+
(0, utils_1.assertType)();
|
|
59
|
+
(0, utils_1.assertType)();
|
|
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;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async defaultSignerAddress() {
|
|
76
|
+
return (await this.getAccounts())[0].address;
|
|
77
|
+
}
|
|
78
|
+
async submitTransaction(unsignedTx, txHash, signerAddress) {
|
|
79
|
+
const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress();
|
|
80
|
+
const signature = await this.signRaw(address, txHash);
|
|
81
|
+
const params = { unsignedTx: unsignedTx, signature: signature };
|
|
82
|
+
return this.provider.transactions.postTransactionsSubmit(params);
|
|
83
|
+
}
|
|
84
|
+
shouldSubmitTx(params) {
|
|
85
|
+
return this.alwaysSubmitTx || (params.submitTx ? params.submitTx : true);
|
|
86
|
+
}
|
|
87
|
+
async usePublicKey(params) {
|
|
88
|
+
const { signerAddress, ...restParams } = params;
|
|
89
|
+
const allAccounts = await this.getAccounts();
|
|
90
|
+
const signerAccount = allAccounts.find((account) => account.address === signerAddress);
|
|
91
|
+
if (typeof signerAccount === 'undefined') {
|
|
92
|
+
throw new Error('Unknown signer address');
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return { fromPublicKey: signerAccount.publicKey, ...restParams };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async signTransferTx(params) {
|
|
99
|
+
const response = await this.buildTransferTx(params);
|
|
100
|
+
return this.handleSign({ signerAddress: params.signerAddress, ...response }, this.shouldSubmitTx(params));
|
|
101
|
+
}
|
|
102
|
+
async buildTransferTx(params) {
|
|
103
|
+
return this.provider.transactions.postTransactionsBuild(await this.usePublicKey(params));
|
|
104
|
+
}
|
|
105
|
+
async signDeployContractTx(params) {
|
|
106
|
+
const response = await this.buildContractCreationTx(params);
|
|
107
|
+
const result = await this.handleSign({ signerAddress: params.signerAddress, ...response }, this.shouldSubmitTx(params));
|
|
108
|
+
const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress));
|
|
109
|
+
return { ...result, contractId: contractId, contractAddress: response.contractAddress };
|
|
110
|
+
}
|
|
111
|
+
async buildContractCreationTx(params) {
|
|
112
|
+
return this.provider.contracts.postContractsUnsignedTxDeployContract(await this.usePublicKey(params));
|
|
113
|
+
}
|
|
114
|
+
async signExecuteScriptTx(params) {
|
|
115
|
+
const response = await this.buildScriptTx(params);
|
|
116
|
+
return this.handleSign({ signerAddress: params.signerAddress, ...response }, this.shouldSubmitTx(params));
|
|
117
|
+
}
|
|
118
|
+
async buildScriptTx(params) {
|
|
119
|
+
return this.provider.contracts.postContractsUnsignedTxExecuteScript(await this.usePublicKey(params));
|
|
120
|
+
}
|
|
121
|
+
// in general, wallet should show the decoded information to user for confirmation
|
|
122
|
+
// please overwrite this function for real wallet
|
|
123
|
+
async signUnsignedTx(params) {
|
|
124
|
+
const data = { unsignedTx: params.unsignedTx };
|
|
125
|
+
const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx(data);
|
|
126
|
+
return this.handleSign({
|
|
127
|
+
fromGroup: decoded.fromGroup,
|
|
128
|
+
toGroup: decoded.toGroup,
|
|
129
|
+
signerAddress: params.signerAddress,
|
|
130
|
+
unsignedTx: params.unsignedTx,
|
|
131
|
+
txId: decoded.unsignedTx.txId
|
|
132
|
+
}, params.submitTx ? params.submitTx : true // we don't consider `alwaysSubmitTx` as the tx might needs multiple signatures
|
|
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
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
async signHexString(params) {
|
|
155
|
+
const signature = await this.signRaw(params.signerAddress, params.hexString);
|
|
156
|
+
return { signature: signature };
|
|
157
|
+
}
|
|
158
|
+
async signMessage(params) {
|
|
159
|
+
const extendedMessage = extendMessage(params.message);
|
|
160
|
+
const messageHash = blakejs_1.default.blake2b(extendedMessage, undefined, 32);
|
|
161
|
+
const signature = await this.signRaw(params.signerAddress, utils.binToHex(messageHash));
|
|
162
|
+
return { signature: signature };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.SignerWithNodeProvider = SignerWithNodeProvider;
|
|
166
|
+
function verifyHexString(hexString, publicKey, signature) {
|
|
167
|
+
try {
|
|
168
|
+
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
169
|
+
return key.verify(hexString, utils.signatureDecode(ec, signature));
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.verifyHexString = verifyHexString;
|
|
176
|
+
function extendMessage(message) {
|
|
177
|
+
return 'Alephium Signed Message: ' + message;
|
|
178
|
+
}
|
|
179
|
+
function verifySignedMessage(message, publicKey, signature) {
|
|
180
|
+
const extendedMessage = extendMessage(message);
|
|
181
|
+
const messageHash = blakejs_1.default.blake2b(extendedMessage, undefined, 32);
|
|
182
|
+
return verifyHexString(utils.binToHex(messageHash), publicKey, signature);
|
|
183
|
+
}
|
|
184
|
+
exports.verifySignedMessage = verifySignedMessage;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { NodeProvider } from '../api';
|
|
2
|
+
import { NodeWallet } from '../signer';
|
|
3
|
+
export declare const testWalletName = "alephium-web3-test-only-wallet";
|
|
4
|
+
export declare const testAddress = "1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH";
|
|
5
|
+
export declare const testPassword = "alph";
|
|
6
|
+
export declare function testWallet(provider: NodeProvider): Promise<NodeWallet>;
|
|
7
|
+
export * from './privatekey-wallet';
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
exports.testWallet = exports.testPassword = exports.testAddress = exports.testWalletName = void 0;
|
|
31
|
+
const signer_1 = require("../signer");
|
|
32
|
+
exports.testWalletName = 'alephium-web3-test-only-wallet';
|
|
33
|
+
exports.testAddress = '1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH';
|
|
34
|
+
exports.testPassword = 'alph';
|
|
35
|
+
async function testWallet(provider) {
|
|
36
|
+
const wallet = new signer_1.NodeWallet(provider, exports.testWalletName);
|
|
37
|
+
await wallet.unlock(exports.testPassword);
|
|
38
|
+
return wallet;
|
|
39
|
+
}
|
|
40
|
+
exports.testWallet = testWallet;
|
|
41
|
+
__exportStar(require("./privatekey-wallet"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NodeProvider } from '../api';
|
|
2
|
+
import { Account, SignerWithNodeProvider } from '../signer';
|
|
3
|
+
export declare class PrivateKeyWallet extends SignerWithNodeProvider {
|
|
4
|
+
readonly privateKey: string;
|
|
5
|
+
readonly publicKey: string;
|
|
6
|
+
readonly address: string;
|
|
7
|
+
readonly group: number;
|
|
8
|
+
constructor(provider: NodeProvider, privateKey: string, alwaysSubmitTx?: boolean);
|
|
9
|
+
static Random(provider: NodeProvider, alwaysSubmitTx?: boolean): PrivateKeyWallet;
|
|
10
|
+
getAccounts(): Promise<Account[]>;
|
|
11
|
+
signRaw(signerAddress: string, hexString: string): Promise<string>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.PrivateKeyWallet = void 0;
|
|
40
|
+
const elliptic_1 = require("elliptic");
|
|
41
|
+
const signer_1 = require("../signer");
|
|
42
|
+
const utils = __importStar(require("../utils"));
|
|
43
|
+
const ec = new elliptic_1.ec('secp256k1');
|
|
44
|
+
class PrivateKeyWallet extends signer_1.SignerWithNodeProvider {
|
|
45
|
+
constructor(provider, privateKey, alwaysSubmitTx = true) {
|
|
46
|
+
super(provider, alwaysSubmitTx);
|
|
47
|
+
this.privateKey = privateKey;
|
|
48
|
+
this.publicKey = utils.publicKeyFromPrivateKey(privateKey);
|
|
49
|
+
this.address = utils.addressFromPublicKey(this.publicKey);
|
|
50
|
+
this.group = utils.groupOfAddress(this.address);
|
|
51
|
+
}
|
|
52
|
+
static Random(provider, alwaysSubmitTx = true) {
|
|
53
|
+
const keyPair = ec.genKeyPair();
|
|
54
|
+
return new PrivateKeyWallet(provider, keyPair.getPrivate().toString('hex'), alwaysSubmitTx);
|
|
55
|
+
}
|
|
56
|
+
async getAccounts() {
|
|
57
|
+
return [{ address: this.address, publicKey: this.publicKey, group: this.group }];
|
|
58
|
+
}
|
|
59
|
+
async signRaw(signerAddress, hexString) {
|
|
60
|
+
if (signerAddress !== this.address) {
|
|
61
|
+
throw Error('Unmatched signer address');
|
|
62
|
+
}
|
|
63
|
+
const key = ec.keyFromPrivate(this.privateKey);
|
|
64
|
+
const signature = key.sign(hexString);
|
|
65
|
+
return utils.signatureEncode(signature);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.PrivateKeyWallet = PrivateKeyWallet;
|
|
File without changes
|
|
@@ -22,7 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
exports.addressToGroup = void 0;
|
|
24
24
|
const bs58_1 = __importDefault(require("./bs58"));
|
|
25
|
-
const djb2_1 = __importDefault(require("
|
|
25
|
+
const djb2_1 = __importDefault(require("./djb2"));
|
|
26
26
|
function addressToGroup(address, totalNumberOfGroups) {
|
|
27
27
|
const bytes = bs58_1.default.decode(address).slice(1);
|
|
28
28
|
const value = (0, djb2_1.default)(bytes) | 1;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** This source is under MIT License and come originally from https://github.com/cryptocoinjs/bs58 **/
|
|
2
2
|
import basex from 'base-x';
|
|
3
|
-
declare const
|
|
4
|
-
export default
|
|
3
|
+
export declare const bs58: basex.BaseConverter;
|
|
4
|
+
export default bs58;
|
|
@@ -20,7 +20,9 @@ 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
24
|
/** This source is under MIT License and come originally from https://github.com/cryptocoinjs/bs58 **/
|
|
24
25
|
const base_x_1 = __importDefault(require("base-x"));
|
|
25
26
|
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
26
|
-
exports.
|
|
27
|
+
exports.bs58 = (0, base_x_1.default)(ALPHABET);
|
|
28
|
+
exports.default = exports.bs58;
|
|
File without changes
|
|
@@ -20,7 +20,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
20
20
|
function djb2(bytes) {
|
|
21
21
|
let hash = 5381;
|
|
22
22
|
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
-
hash = (hash << 5) + hash + (bytes[i] & 0xff);
|
|
23
|
+
hash = (hash << 5) + hash + (bytes[`${i}`] & 0xff);
|
|
24
24
|
}
|
|
25
25
|
return hash;
|
|
26
26
|
}
|
|
@@ -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("./transaction"), exports);
|
|
35
|
+
__exportStar(require("./utils"), exports);
|
|
File without changes
|
|
@@ -18,22 +18,23 @@ 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.decrypt = exports.encrypt = void 0;
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
21
22
|
const crypto_1 = require("crypto");
|
|
22
23
|
const saltByteLength = 64;
|
|
23
24
|
const ivByteLength = 64;
|
|
24
25
|
const authTagLength = 16;
|
|
25
26
|
const encrypt = (password, dataRaw) => {
|
|
26
|
-
const data = Buffer.from(dataRaw, 'utf8');
|
|
27
|
+
const data = buffer_1.Buffer.from(dataRaw, 'utf8');
|
|
27
28
|
const salt = (0, crypto_1.randomBytes)(saltByteLength);
|
|
28
29
|
const derivedKey = keyFromPassword(password, salt);
|
|
29
30
|
const iv = (0, crypto_1.randomBytes)(ivByteLength);
|
|
30
31
|
const cipher = createCipher(derivedKey, iv);
|
|
31
|
-
const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
|
|
32
|
+
const encrypted = buffer_1.Buffer.concat([cipher.update(data), cipher.final()]);
|
|
32
33
|
const authTag = cipher.getAuthTag();
|
|
33
34
|
const payload = {
|
|
34
35
|
salt: salt.toString('hex'),
|
|
35
36
|
iv: iv.toString('hex'),
|
|
36
|
-
encrypted: Buffer.concat([encrypted, authTag]).toString('hex'),
|
|
37
|
+
encrypted: buffer_1.Buffer.concat([encrypted, authTag]).toString('hex'),
|
|
37
38
|
version: 1
|
|
38
39
|
};
|
|
39
40
|
return JSON.stringify(payload);
|
|
@@ -45,15 +46,15 @@ const decrypt = (password, payloadRaw) => {
|
|
|
45
46
|
if (version !== 1) {
|
|
46
47
|
throw new Error(`Invalid version: got ${version}, expected: 1`);
|
|
47
48
|
}
|
|
48
|
-
const salt = Buffer.from(payload.salt, 'hex');
|
|
49
|
-
const iv = Buffer.from(payload.iv, 'hex');
|
|
50
|
-
const encrypted = Buffer.from(payload.encrypted, 'hex');
|
|
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');
|
|
51
52
|
const derivedKey = keyFromPassword(password, salt);
|
|
52
53
|
const decipher = createDecipher(derivedKey, iv);
|
|
53
54
|
const data = encrypted.slice(0, encrypted.length - authTagLength);
|
|
54
55
|
const authTag = encrypted.slice(encrypted.length - authTagLength, encrypted.length);
|
|
55
56
|
decipher.setAuthTag(authTag);
|
|
56
|
-
const decrypted = Buffer.concat([decipher.update(data), decipher.final()]);
|
|
57
|
+
const decrypted = buffer_1.Buffer.concat([decipher.update(data), decipher.final()]);
|
|
57
58
|
return decrypted.toString('utf8');
|
|
58
59
|
};
|
|
59
60
|
exports.decrypt = decrypt;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
4
|
+
This file is part of the alephium project.
|
|
5
|
+
|
|
6
|
+
The library is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
The library is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
20
|
+
if (k2 === undefined) k2 = k;
|
|
21
|
+
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.transactionVerifySignature = exports.transactionSign = void 0;
|
|
40
|
+
const elliptic_1 = require("elliptic");
|
|
41
|
+
const utils = __importStar(require("./utils"));
|
|
42
|
+
const ec = new elliptic_1.ec('secp256k1');
|
|
43
|
+
function transactionSign(txHash, privateKey) {
|
|
44
|
+
const keyPair = ec.keyFromPrivate(privateKey);
|
|
45
|
+
const signature = keyPair.sign(txHash);
|
|
46
|
+
return utils.signatureEncode(signature);
|
|
47
|
+
}
|
|
48
|
+
exports.transactionSign = transactionSign;
|
|
49
|
+
function transactionVerifySignature(txHash, publicKey, signature) {
|
|
50
|
+
try {
|
|
51
|
+
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
52
|
+
return key.verify(txHash, utils.signatureDecode(ec, signature));
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.transactionVerifySignature = transactionVerifySignature;
|