@alephium/web3 0.0.1-test.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/.editorconfig +13 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +21 -0
  4. package/.gitattributes +1 -0
  5. package/LICENSE +165 -0
  6. package/README.md +136 -0
  7. package/contracts/add.ral +16 -0
  8. package/contracts/greeter.ral +7 -0
  9. package/contracts/greeter_interface.ral +3 -0
  10. package/contracts/greeter_main.ral +9 -0
  11. package/contracts/main.ral +6 -0
  12. package/contracts/sub.ral +9 -0
  13. package/dev/user.conf +29 -0
  14. package/dist/alephium-web3.min.js +3 -0
  15. package/dist/alephium-web3.min.js.LICENSE.txt +27 -0
  16. package/dist/alephium-web3.min.js.map +1 -0
  17. package/dist/scripts/check-versions.d.ts +1 -0
  18. package/dist/scripts/check-versions.js +39 -0
  19. package/dist/scripts/create-project.d.ts +2 -0
  20. package/dist/scripts/create-project.js +124 -0
  21. package/dist/scripts/header.d.ts +0 -0
  22. package/dist/scripts/header.js +18 -0
  23. package/dist/scripts/rename-gitignore.d.ts +1 -0
  24. package/dist/scripts/rename-gitignore.js +24 -0
  25. package/dist/scripts/start-devnet.d.ts +1 -0
  26. package/dist/scripts/start-devnet.js +131 -0
  27. package/dist/scripts/stop-devnet.d.ts +1 -0
  28. package/dist/scripts/stop-devnet.js +32 -0
  29. package/dist/src/api/api-alephium.d.ts +1473 -0
  30. package/dist/src/api/api-alephium.js +1143 -0
  31. package/dist/src/api/api-explorer.d.ts +475 -0
  32. package/dist/src/api/api-explorer.js +457 -0
  33. package/dist/src/api/index.d.ts +10 -0
  34. package/dist/src/api/index.js +55 -0
  35. package/dist/src/constants.d.ts +2 -0
  36. package/dist/src/constants.js +22 -0
  37. package/dist/src/contract/contract.d.ts +182 -0
  38. package/dist/src/contract/contract.js +760 -0
  39. package/dist/src/contract/events.d.ts +11 -0
  40. package/dist/src/contract/events.js +64 -0
  41. package/dist/src/contract/index.d.ts +3 -0
  42. package/dist/src/contract/index.js +32 -0
  43. package/dist/src/contract/ralph.d.ts +12 -0
  44. package/dist/src/contract/ralph.js +362 -0
  45. package/dist/src/index.d.ts +6 -0
  46. package/dist/src/index.js +35 -0
  47. package/dist/src/signer/index.d.ts +2 -0
  48. package/dist/src/signer/index.js +31 -0
  49. package/dist/src/signer/node-wallet.d.ts +13 -0
  50. package/dist/src/signer/node-wallet.js +60 -0
  51. package/dist/src/signer/signer.d.ts +143 -0
  52. package/dist/src/signer/signer.js +184 -0
  53. package/dist/src/test/index.d.ts +7 -0
  54. package/dist/src/test/index.js +41 -0
  55. package/dist/src/test/privatekey-wallet.d.ts +12 -0
  56. package/dist/src/test/privatekey-wallet.js +68 -0
  57. package/dist/src/transaction/index.d.ts +2 -0
  58. package/dist/src/transaction/index.js +31 -0
  59. package/dist/src/transaction/sign-verify.d.ts +2 -0
  60. package/dist/src/transaction/sign-verify.js +58 -0
  61. package/dist/src/transaction/status.d.ts +10 -0
  62. package/dist/src/transaction/status.js +48 -0
  63. package/dist/src/utils/address.d.ts +1 -0
  64. package/dist/src/utils/address.js +42 -0
  65. package/dist/src/utils/bs58.d.ts +4 -0
  66. package/dist/src/utils/bs58.js +28 -0
  67. package/dist/src/utils/djb2.d.ts +1 -0
  68. package/dist/src/utils/djb2.js +27 -0
  69. package/dist/src/utils/index.d.ts +6 -0
  70. package/dist/src/utils/index.js +35 -0
  71. package/dist/src/utils/password-crypto.d.ts +2 -0
  72. package/dist/src/utils/password-crypto.js +69 -0
  73. package/dist/src/utils/subscription.d.ts +24 -0
  74. package/dist/src/utils/subscription.js +52 -0
  75. package/dist/src/utils/utils.d.ts +30 -0
  76. package/dist/src/utils/utils.js +199 -0
  77. package/gitignore +10 -0
  78. package/package.json +127 -0
  79. package/scripts/check-versions.js +45 -0
  80. package/scripts/create-project.ts +136 -0
  81. package/scripts/header.js +17 -0
  82. package/scripts/rename-gitignore.js +24 -0
  83. package/scripts/start-devnet.js +141 -0
  84. package/scripts/stop-devnet.js +32 -0
  85. package/src/api/api-alephium.ts +2430 -0
  86. package/src/api/api-explorer.ts +852 -0
  87. package/src/api/index.ts +35 -0
  88. package/src/constants.ts +20 -0
  89. package/src/contract/contract.ts +1014 -0
  90. package/src/contract/events.ts +75 -0
  91. package/src/contract/index.ts +21 -0
  92. package/src/contract/ralph.test.ts +178 -0
  93. package/src/contract/ralph.ts +362 -0
  94. package/src/fixtures/address.json +36 -0
  95. package/src/fixtures/balance.json +9 -0
  96. package/src/fixtures/self-clique.json +19 -0
  97. package/src/fixtures/transaction.json +13 -0
  98. package/src/fixtures/transactions.json +179 -0
  99. package/src/index.ts +25 -0
  100. package/src/signer/fixtures/genesis.json +26 -0
  101. package/src/signer/fixtures/wallets.json +26 -0
  102. package/src/signer/index.ts +20 -0
  103. package/src/signer/node-wallet.ts +74 -0
  104. package/src/signer/signer.ts +313 -0
  105. package/src/test/index.ts +32 -0
  106. package/src/test/privatekey-wallet.ts +58 -0
  107. package/src/transaction/index.ts +20 -0
  108. package/src/transaction/sign-verify.test.ts +50 -0
  109. package/src/transaction/sign-verify.ts +39 -0
  110. package/src/transaction/status.ts +58 -0
  111. package/src/utils/address.test.ts +47 -0
  112. package/src/utils/address.ts +39 -0
  113. package/src/utils/bs58.ts +26 -0
  114. package/src/utils/djb2.test.ts +35 -0
  115. package/src/utils/djb2.ts +25 -0
  116. package/src/utils/index.ts +24 -0
  117. package/src/utils/password-crypto.test.ts +27 -0
  118. package/src/utils/password-crypto.ts +77 -0
  119. package/src/utils/subscription.ts +72 -0
  120. package/src/utils/utils.test.ts +160 -0
  121. package/src/utils/utils.ts +209 -0
  122. package/templates/base/README.md +34 -0
  123. package/templates/base/package.json +35 -0
  124. package/templates/base/src/greeter.ts +41 -0
  125. package/templates/base/tsconfig.json +19 -0
  126. package/templates/react/README.md +34 -0
  127. package/templates/react/config-overrides.js +18 -0
  128. package/templates/react/package.json +66 -0
  129. package/templates/react/src/App.tsx +42 -0
  130. package/templates/react/src/artifacts/greeter.ral.json +26 -0
  131. package/templates/react/src/artifacts/greeter_main.ral.json +22 -0
  132. package/templates/shared/.eslintrc.json +12 -0
  133. package/templates/shared/scripts/header.js +0 -0
  134. package/test/contract.test.ts +161 -0
  135. package/test/events.test.ts +138 -0
  136. package/test/transaction.test.ts +72 -0
  137. package/webpack.config.js +57 -0
@@ -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
+ initialAttoAlphAmount?: 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
+ attoAlphAmount?: 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;
@@ -0,0 +1,2 @@
1
+ export * from './sign-verify';
2
+ export * from './status';
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
22
+ }) : (function(o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ o[k2] = m[k];
25
+ }));
26
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
27
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ __exportStar(require("./sign-verify"), exports);
31
+ __exportStar(require("./status"), exports);
@@ -0,0 +1,2 @@
1
+ export declare function transactionSign(txHash: string, privateKey: string): string;
2
+ export declare function transactionVerifySignature(txHash: string, publicKey: string, signature: string): boolean;
@@ -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;
@@ -0,0 +1,10 @@
1
+ import { TxStatus } from '../api/api-alephium';
2
+ import { Subscription, SubscribeOptions } from '../utils';
3
+ export declare class TxStatusSubscription extends Subscription<TxStatus> {
4
+ readonly txId: string;
5
+ readonly fromGroup?: number;
6
+ readonly toGroup?: number;
7
+ constructor(options: SubscribeOptions<TxStatus>, txId: string, fromGroup?: number, toGroup?: number);
8
+ polling(): Promise<void>;
9
+ }
10
+ export declare function subscribeToTxStatus(options: SubscribeOptions<TxStatus>, txId: string, fromGroup?: number, toGroup?: number): TxStatusSubscription;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.subscribeToTxStatus = exports.TxStatusSubscription = void 0;
21
+ const utils_1 = require("../utils");
22
+ class TxStatusSubscription extends utils_1.Subscription {
23
+ constructor(options, txId, fromGroup, toGroup) {
24
+ super(options);
25
+ this.txId = txId;
26
+ this.fromGroup = fromGroup;
27
+ this.toGroup = toGroup;
28
+ this.startPolling();
29
+ }
30
+ async polling() {
31
+ try {
32
+ const txStatus = await this.provider.transactions.getTransactionsStatus({
33
+ txId: this.txId,
34
+ fromGroup: this.fromGroup,
35
+ toGroup: this.toGroup
36
+ });
37
+ await this.messageCallback(txStatus);
38
+ }
39
+ catch (err) {
40
+ await this.errorCallback(err, this);
41
+ }
42
+ }
43
+ }
44
+ exports.TxStatusSubscription = TxStatusSubscription;
45
+ function subscribeToTxStatus(options, txId, fromGroup, toGroup) {
46
+ return new TxStatusSubscription(options, txId, fromGroup, toGroup);
47
+ }
48
+ exports.subscribeToTxStatus = subscribeToTxStatus;
@@ -0,0 +1 @@
1
+ export declare function addressToGroup(address: string, totalNumberOfGroups: number): number;
@@ -0,0 +1,42 @@
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.addressToGroup = void 0;
24
+ const bs58_1 = __importDefault(require("./bs58"));
25
+ const djb2_1 = __importDefault(require("./djb2"));
26
+ function addressToGroup(address, totalNumberOfGroups) {
27
+ const bytes = bs58_1.default.decode(address).slice(1);
28
+ const value = (0, djb2_1.default)(bytes) | 1;
29
+ const hash = toPosInt(xorByte(value));
30
+ const group = hash % totalNumberOfGroups;
31
+ return group;
32
+ }
33
+ exports.addressToGroup = addressToGroup;
34
+ function xorByte(value) {
35
+ const byte0 = value >> 24;
36
+ const byte1 = value >> 16;
37
+ const byte2 = value >> 8;
38
+ return byte0 ^ byte1 ^ byte2 ^ value;
39
+ }
40
+ function toPosInt(byte) {
41
+ return byte & 0xff;
42
+ }
@@ -0,0 +1,4 @@
1
+ /** This source is under MIT License and come originally from https://github.com/cryptocoinjs/bs58 **/
2
+ import basex from 'base-x';
3
+ export declare const bs58: basex.BaseConverter;
4
+ export default bs58;