@alephium/web3 0.3.0-rc.4 → 0.3.0-rc.7

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.
@@ -43,32 +43,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
43
43
  return (mod && mod.__esModule) ? mod : { "default": mod };
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.verifyHexString = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = void 0;
46
+ exports.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.verifyHexString = exports.SignerProviderWithCachedAccounts = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = void 0;
47
47
  const elliptic_1 = require("elliptic");
48
48
  const api_1 = require("../api");
49
49
  const utils = __importStar(require("../utils"));
50
- const utils_1 = require("../utils");
51
50
  const blakejs_1 = __importDefault(require("blakejs"));
51
+ const tx_builder_1 = require("./tx-builder");
52
52
  const ec = new elliptic_1.ec('secp256k1');
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
- utils_1.assertType;
61
- (0, utils_1.assertType)();
62
- class SignerProviderSimple {
63
- getNodeProvider() {
64
- if (this.nodeProvider === undefined) {
65
- throw Error('The signer does not contain a node provider');
66
- }
67
- return this.nodeProvider;
53
+ class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
54
+ async getSelectedAddress() {
55
+ const account = await this.getSelectedAccount();
56
+ return account.address;
68
57
  }
69
58
  async submitTransaction(params) {
70
59
  const data = { unsignedTx: params.unsignedTx, signature: params.signature };
71
- return this.getNodeProvider().transactions.postTransactionsSubmit(data);
60
+ return this.nodeProvider.transactions.postTransactionsSubmit(data);
72
61
  }
73
62
  async signAndSubmitTransferTx(params) {
74
63
  const signResult = await this.signTransferTx(params);
@@ -92,72 +81,39 @@ class SignerProviderSimple {
92
81
  }
93
82
  async usePublicKey(params) {
94
83
  const { signerAddress, ...restParams } = params;
95
- const selectedAccount = await this.getSelectedAccount();
96
- if (signerAddress !== selectedAccount.address) {
97
- throw new Error('The signer address is not the selected address');
98
- }
99
- else {
100
- return { fromPublicKey: selectedAccount.publicKey, ...restParams };
101
- }
84
+ const publicKey = await this.getPublicKey(signerAddress);
85
+ return { fromPublicKey: publicKey, ...restParams };
102
86
  }
103
87
  async signTransferTx(params) {
104
88
  const response = await this.buildTransferTx(params);
105
89
  const signature = await this.signRaw(params.signerAddress, response.txId);
106
- return { ...response, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
90
+ return { signature, ...response };
107
91
  }
108
92
  async buildTransferTx(params) {
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);
93
+ return super.buildTransferTx(params, await this.getPublicKey(params.signerAddress));
115
94
  }
116
95
  async signDeployContractTx(params) {
117
- const response = await this.buildContractCreationTx(params);
96
+ const response = await this.buildDeployContractTx(params);
118
97
  const signature = await this.signRaw(params.signerAddress, response.txId);
119
- const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress));
120
- return { ...response, contractId, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
121
- }
122
- async buildContractCreationTx(params) {
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);
98
+ return { signature, ...response };
99
+ }
100
+ async buildDeployContractTx(params) {
101
+ return super.buildDeployContractTx(params, await this.getPublicKey(params.signerAddress));
131
102
  }
132
103
  async signExecuteScriptTx(params) {
133
- const response = await this.buildScriptTx(params);
104
+ const response = await this.buildExecuteScriptTx(params);
134
105
  const signature = await this.signRaw(params.signerAddress, response.txId);
135
- return { ...response, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
106
+ return { signature, ...response };
136
107
  }
137
- async buildScriptTx(params) {
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);
108
+ async buildExecuteScriptTx(params) {
109
+ return super.buildExecuteScriptTx(params, await this.getPublicKey(params.signerAddress));
145
110
  }
146
111
  // in general, wallet should show the decoded information to user for confirmation
147
112
  // please overwrite this function for real wallet
148
113
  async signUnsignedTx(params) {
149
- const data = { unsignedTx: params.unsignedTx };
150
- const decoded = await this.getNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data);
151
- const signature = await this.signRaw(params.signerAddress, decoded.unsignedTx.txId);
152
- return {
153
- fromGroup: decoded.fromGroup,
154
- toGroup: decoded.toGroup,
155
- unsignedTx: params.unsignedTx,
156
- txId: decoded.unsignedTx.txId,
157
- signature,
158
- gasAmount: decoded.unsignedTx.gasAmount,
159
- gasPrice: (0, api_1.fromApiNumber256)(decoded.unsignedTx.gasPrice)
160
- };
114
+ const response = await this.buildUnsignedTx(params);
115
+ const signature = await this.signRaw(params.signerAddress, response.txId);
116
+ return { signature, ...response };
161
117
  }
162
118
  async signMessage(params) {
163
119
  const extendedMessage = extendMessage(params.message);
@@ -178,8 +134,48 @@ class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
178
134
  return account;
179
135
  }
180
136
  }
137
+ async getPublicKey(signerAddress) {
138
+ const account = await this.getAccount(signerAddress);
139
+ return account.publicKey;
140
+ }
181
141
  }
182
142
  exports.SignerProviderWithMultipleAccounts = SignerProviderWithMultipleAccounts;
143
+ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccounts {
144
+ constructor() {
145
+ super(...arguments);
146
+ this._selectedAccount = undefined;
147
+ this._accounts = new Map();
148
+ }
149
+ getSelectedAccount() {
150
+ if (this._selectedAccount === undefined) {
151
+ throw Error('No account is selected yet');
152
+ }
153
+ else {
154
+ return Promise.resolve(this._selectedAccount);
155
+ }
156
+ }
157
+ setSelectedAddress(address) {
158
+ const accountOpt = this._accounts.get(address);
159
+ if (accountOpt === undefined) {
160
+ throw Error('The address is not in the accounts');
161
+ }
162
+ else {
163
+ this._selectedAccount = accountOpt;
164
+ return Promise.resolve();
165
+ }
166
+ }
167
+ getAccounts() {
168
+ return Promise.resolve(Array.from(this._accounts.values()));
169
+ }
170
+ async getAccount(address) {
171
+ const account = this._accounts.get(address);
172
+ if (account === undefined) {
173
+ throw Error('The address is not in the accounts');
174
+ }
175
+ return Promise.resolve(account);
176
+ }
177
+ }
178
+ exports.SignerProviderWithCachedAccounts = SignerProviderWithCachedAccounts;
183
179
  function verifyHexString(hexString, publicKey, signature) {
184
180
  try {
185
181
  const key = ec.keyFromPublic(publicKey, 'hex');
@@ -199,7 +195,6 @@ function verifySignedMessage(message, publicKey, signature) {
199
195
  return verifyHexString(utils.binToHex(messageHash), publicKey, signature);
200
196
  }
201
197
  exports.verifySignedMessage = verifySignedMessage;
202
- utils_1.assertType;
203
198
  function toApiDestination(data) {
204
199
  return { ...data, attoAlphAmount: (0, api_1.toApiNumber256)(data.attoAlphAmount), tokens: (0, api_1.toApiTokens)(data.tokens) };
205
200
  }
@@ -0,0 +1,17 @@
1
+ import { NodeProvider } from '../api';
2
+ import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignExecuteScriptTxResult, SignTransferTxParams, SignTransferTxResult, SignUnsignedTxParams, SignUnsignedTxResult } from './types';
3
+ export declare abstract class TransactionBuilder {
4
+ abstract get nodeProvider(): NodeProvider;
5
+ static create(baseUrl: string, apiKey?: string): {
6
+ readonly nodeProvider: NodeProvider;
7
+ buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, "signature">>;
8
+ buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, "signature">>;
9
+ buildExecuteScriptTx(params: SignExecuteScriptTxParams, publicKey: string): Promise<Omit<SignExecuteScriptTxResult, "signature">>;
10
+ buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, "signature">>;
11
+ };
12
+ private static validatePublicKey;
13
+ buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, 'signature'>>;
14
+ buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, 'signature'>>;
15
+ buildExecuteScriptTx(params: SignExecuteScriptTxParams, publicKey: string): Promise<Omit<SignExecuteScriptTxResult, 'signature'>>;
16
+ buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, 'signature'>>;
17
+ }
@@ -0,0 +1,93 @@
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.TransactionBuilder = void 0;
21
+ const __1 = require("..");
22
+ const api_1 = require("../api");
23
+ const utils_1 = require("../utils");
24
+ const signer_1 = require("./signer");
25
+ class TransactionBuilder {
26
+ static create(baseUrl, apiKey) {
27
+ const nodeProvider = new api_1.NodeProvider(baseUrl, apiKey);
28
+ return new (class extends TransactionBuilder {
29
+ get nodeProvider() {
30
+ return nodeProvider;
31
+ }
32
+ })();
33
+ }
34
+ static validatePublicKey(params, publicKey) {
35
+ const address = (0, utils_1.addressFromPublicKey)(publicKey);
36
+ if (address !== params.signerAddress) {
37
+ throw new Error('Unmatched public key');
38
+ }
39
+ }
40
+ async buildTransferTx(params, publicKey) {
41
+ TransactionBuilder.validatePublicKey(params, publicKey);
42
+ const { destinations, gasPrice, ...rest } = params;
43
+ const data = {
44
+ fromPublicKey: publicKey,
45
+ destinations: (0, signer_1.toApiDestinations)(destinations),
46
+ gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
47
+ ...rest
48
+ };
49
+ const response = await this.nodeProvider.transactions.postTransactionsBuild(data);
50
+ return { ...response, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
51
+ }
52
+ async buildDeployContractTx(params, publicKey) {
53
+ TransactionBuilder.validatePublicKey(params, publicKey);
54
+ const { initialAttoAlphAmount, initialTokenAmounts, issueTokenAmount, gasPrice, ...rest } = params;
55
+ const data = {
56
+ fromPublicKey: publicKey,
57
+ initialAttoAlphAmount: (0, api_1.toApiNumber256Optional)(initialAttoAlphAmount),
58
+ initialTokenAmounts: (0, api_1.toApiTokens)(initialTokenAmounts),
59
+ issueTokenAmount: (0, api_1.toApiNumber256Optional)(issueTokenAmount),
60
+ gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
61
+ ...rest
62
+ };
63
+ const response = await this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data);
64
+ const contractId = __1.utils.binToHex(__1.utils.contractIdFromAddress(response.contractAddress));
65
+ return { ...response, contractId, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
66
+ }
67
+ async buildExecuteScriptTx(params, publicKey) {
68
+ TransactionBuilder.validatePublicKey(params, publicKey);
69
+ const { attoAlphAmount, tokens, gasPrice, ...rest } = params;
70
+ const data = {
71
+ fromPublicKey: publicKey,
72
+ attoAlphAmount: (0, api_1.toApiNumber256Optional)(attoAlphAmount),
73
+ tokens: (0, api_1.toApiTokens)(tokens),
74
+ gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
75
+ ...rest
76
+ };
77
+ const response = await this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data);
78
+ return { ...response, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
79
+ }
80
+ async buildUnsignedTx(params) {
81
+ const data = { unsignedTx: params.unsignedTx };
82
+ const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data);
83
+ return {
84
+ fromGroup: decoded.fromGroup,
85
+ toGroup: decoded.toGroup,
86
+ unsignedTx: params.unsignedTx,
87
+ txId: decoded.unsignedTx.txId,
88
+ gasAmount: decoded.unsignedTx.gasAmount,
89
+ gasPrice: (0, api_1.fromApiNumber256)(decoded.unsignedTx.gasPrice)
90
+ };
91
+ }
92
+ }
93
+ exports.TransactionBuilder = TransactionBuilder;
@@ -0,0 +1,121 @@
1
+ import { Number256, Token } from '../api';
2
+ import { node } from '../api';
3
+ export declare type Address = string;
4
+ export declare type OutputRef = node.OutputRef;
5
+ export interface Destination {
6
+ address: string;
7
+ attoAlphAmount: Number256;
8
+ tokens?: Token[];
9
+ lockTime?: number;
10
+ message?: string;
11
+ }
12
+ export interface Account {
13
+ address: string;
14
+ group: number;
15
+ publicKey: string;
16
+ }
17
+ export declare type SignerAddress = {
18
+ signerAddress: string;
19
+ };
20
+ export interface SignTransferTxParams {
21
+ signerAddress: string;
22
+ destinations: Destination[];
23
+ utxos?: OutputRef[];
24
+ gasAmount?: number;
25
+ gasPrice?: Number256;
26
+ }
27
+ export interface SignTransferTxResult {
28
+ fromGroup: number;
29
+ toGroup: number;
30
+ unsignedTx: string;
31
+ txId: string;
32
+ signature: string;
33
+ gasAmount: number;
34
+ gasPrice: Number256;
35
+ }
36
+ export interface SignDeployContractTxParams {
37
+ signerAddress: string;
38
+ bytecode: string;
39
+ initialAttoAlphAmount?: Number256;
40
+ initialTokenAmounts?: Token[];
41
+ issueTokenAmount?: Number256;
42
+ gasAmount?: number;
43
+ gasPrice?: Number256;
44
+ }
45
+ export interface SignDeployContractTxResult {
46
+ fromGroup: number;
47
+ toGroup: number;
48
+ unsignedTx: string;
49
+ txId: string;
50
+ signature: string;
51
+ contractId: string;
52
+ contractAddress: string;
53
+ gasAmount: number;
54
+ gasPrice: Number256;
55
+ }
56
+ export interface SignExecuteScriptTxParams {
57
+ signerAddress: string;
58
+ bytecode: string;
59
+ attoAlphAmount?: Number256;
60
+ tokens?: Token[];
61
+ gasAmount?: number;
62
+ gasPrice?: Number256;
63
+ }
64
+ export interface SignExecuteScriptTxResult {
65
+ fromGroup: number;
66
+ toGroup: number;
67
+ unsignedTx: string;
68
+ txId: string;
69
+ signature: string;
70
+ gasAmount: number;
71
+ gasPrice: Number256;
72
+ }
73
+ export interface SignUnsignedTxParams {
74
+ signerAddress: string;
75
+ unsignedTx: string;
76
+ }
77
+ export interface SignUnsignedTxResult {
78
+ fromGroup: number;
79
+ toGroup: number;
80
+ unsignedTx: string;
81
+ txId: string;
82
+ signature: string;
83
+ gasAmount: number;
84
+ gasPrice: Number256;
85
+ }
86
+ export interface SignMessageParams {
87
+ signerAddress: string;
88
+ message: string;
89
+ }
90
+ export interface SignMessageResult {
91
+ signature: string;
92
+ }
93
+ export interface SubmitTransactionParams {
94
+ unsignedTx: string;
95
+ signature: string;
96
+ }
97
+ export interface SubmissionResult {
98
+ txId: string;
99
+ fromGroup: number;
100
+ toGroup: number;
101
+ }
102
+ export interface EnableOptionsBase {
103
+ chainGroup?: number;
104
+ networkId: string;
105
+ onDisconnected: () => Promise<void>;
106
+ }
107
+ export declare type ExtSignTransferTxParams = SignTransferTxParams & {
108
+ networkId: string;
109
+ };
110
+ export declare type ExtSignDeployContractTxParams = SignDeployContractTxParams & {
111
+ networkId: string;
112
+ };
113
+ export declare type ExtSignExecuteScriptTxParams = SignExecuteScriptTxParams & {
114
+ networkId: string;
115
+ };
116
+ export declare type ExtSignUnsignedTxParams = SignUnsignedTxParams & {
117
+ networkId: string;
118
+ };
119
+ export declare type ExtSignMessageParams = SignMessageParams & {
120
+ networkId: string;
121
+ };
@@ -0,0 +1,30 @@
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
+ const utils_1 = require("../utils");
21
+ utils_1.assertType;
22
+ (0, utils_1.assertType)();
23
+ (0, utils_1.assertType)();
24
+ (0, utils_1.assertType)();
25
+ (0, utils_1.assertType)();
26
+ (0, utils_1.assertType)();
27
+ (0, utils_1.assertType)();
28
+ (0, utils_1.assertType)();
29
+ utils_1.assertType;
30
+ (0, utils_1.assertType)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.3.0-rc.4",
3
+ "version": "0.3.0-rc.7",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -27,8 +27,8 @@
27
27
  },
28
28
  "author": "Alephium dev <dev@alephium.org>",
29
29
  "config": {
30
- "alephium_version": "1.6.0-rc5",
31
- "explorer_backend_version": "1.11.2"
30
+ "alephium_version": "1.6.2",
31
+ "explorer_backend_version": "1.12.0-rc2"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "rm -rf dist/* && npx tsc --build . && webpack",
@@ -71,6 +71,7 @@
71
71
  "jest-websocket-mock": "^2.2.1",
72
72
  "mock-fs": "^5.1.2",
73
73
  "mock-socket": "^9.0.8",
74
+ "path-browserify": "^1.0.1",
74
75
  "prettier": "^2.3.2",
75
76
  "process": "^0.11.10",
76
77
  "rewire": "^6.0.0",
@@ -1332,7 +1332,7 @@ export class HttpClient<SecurityDataType = unknown> {
1332
1332
 
1333
1333
  /**
1334
1334
  * @title Alephium API
1335
- * @version 1.6.0
1335
+ * @version 1.6.2
1336
1336
  * @baseUrl ../
1337
1337
  */
1338
1338
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {