@alephium/web3 0.3.0-rc.3 → 0.3.0-rc.5

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.
@@ -1,100 +1,10 @@
1
- import { ExplorerProvider, NodeProvider, Number256, Token } from '../api';
1
+ import { ExplorerProvider, NodeProvider } from '../api';
2
2
  import { node } from '../api';
3
- export declare type OutputRef = node.OutputRef;
4
- export interface Account {
5
- address: string;
6
- group: number;
7
- publicKey: string;
8
- }
9
- export declare type SignerAddress = {
10
- signerAddress: string;
11
- };
12
- export interface SignTransferTxParams {
13
- signerAddress: string;
14
- destinations: Destination[];
15
- utxos?: OutputRef[];
16
- gasAmount?: number;
17
- gasPrice?: Number256;
18
- }
19
- export interface SignTransferTxResult {
20
- fromGroup: number;
21
- toGroup: number;
22
- unsignedTx: string;
23
- txId: string;
24
- signature: string;
25
- gasAmount: number;
26
- gasPrice: Number256;
27
- }
28
- export interface SignDeployContractTxParams {
29
- signerAddress: string;
30
- bytecode: string;
31
- initialAttoAlphAmount?: Number256;
32
- initialTokenAmounts?: Token[];
33
- issueTokenAmount?: Number256;
34
- gasAmount?: number;
35
- gasPrice?: Number256;
36
- }
37
- export interface SignDeployContractTxResult {
38
- fromGroup: number;
39
- toGroup: number;
40
- unsignedTx: string;
41
- txId: string;
42
- signature: string;
43
- contractId: string;
44
- contractAddress: string;
45
- gasAmount: number;
46
- gasPrice: Number256;
47
- }
48
- export interface SignExecuteScriptTxParams {
49
- signerAddress: string;
50
- bytecode: string;
51
- attoAlphAmount?: Number256;
52
- tokens?: Token[];
53
- gasAmount?: number;
54
- gasPrice?: Number256;
55
- }
56
- export interface SignExecuteScriptTxResult {
57
- fromGroup: number;
58
- toGroup: number;
59
- unsignedTx: string;
60
- txId: string;
61
- signature: string;
62
- gasAmount: number;
63
- gasPrice: Number256;
64
- }
65
- export interface SignUnsignedTxParams {
66
- signerAddress: string;
67
- unsignedTx: string;
68
- }
69
- export interface SignUnsignedTxResult {
70
- fromGroup: number;
71
- toGroup: number;
72
- unsignedTx: string;
73
- txId: string;
74
- signature: string;
75
- gasAmount: number;
76
- gasPrice: Number256;
77
- }
78
- export interface SignMessageParams {
79
- signerAddress: string;
80
- message: string;
81
- }
82
- export interface SignMessageResult {
83
- signature: string;
84
- }
85
- export interface SubmitTransactionParams {
86
- unsignedTx: string;
87
- signature: string;
88
- }
89
- export interface SubmissionResult {
90
- txId: string;
91
- fromGroup: number;
92
- toGroup: number;
93
- }
3
+ import { Account, Address, EnableOptionsBase, Destination, SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignExecuteScriptTxResult, SignMessageParams, SignMessageResult, SignTransferTxParams, SignTransferTxResult, SignUnsignedTxParams, SignUnsignedTxResult, SubmissionResult, SubmitTransactionParams } from './types';
94
4
  export interface SignerProvider {
95
5
  get nodeProvider(): NodeProvider | undefined;
96
6
  get explorerProvider(): ExplorerProvider | undefined;
97
- getSelectedAccount(): Promise<Account>;
7
+ getSelectedAddress(): Promise<Address>;
98
8
  signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
99
9
  signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
100
10
  signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
@@ -102,16 +12,22 @@ export interface SignerProvider {
102
12
  signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
103
13
  signMessage(params: SignMessageParams): Promise<SignMessageResult>;
104
14
  }
15
+ export interface InteractiveSignerProvider<EnableOptions extends EnableOptionsBase = EnableOptionsBase> extends SignerProvider {
16
+ enable(opt?: EnableOptions): Promise<void>;
17
+ disconnect(): Promise<void>;
18
+ }
105
19
  export declare abstract class SignerProviderSimple implements SignerProvider {
106
20
  abstract get nodeProvider(): NodeProvider | undefined;
107
21
  abstract get explorerProvider(): ExplorerProvider | undefined;
108
22
  abstract getSelectedAccount(): Promise<Account>;
23
+ getSelectedAddress(): Promise<Address>;
109
24
  private getNodeProvider;
110
25
  submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult>;
111
26
  signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
112
27
  signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
113
28
  signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
114
29
  signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
30
+ protected abstract getPublicKey(address: string): Promise<string>;
115
31
  private usePublicKey;
116
32
  signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
117
33
  buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult>;
@@ -124,19 +40,21 @@ export declare abstract class SignerProviderSimple implements SignerProvider {
124
40
  abstract signRaw(signerAddress: string, hexString: string): Promise<string>;
125
41
  }
126
42
  export declare abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
43
+ abstract setSelectedAddress(address: string): Promise<void>;
127
44
  abstract getAccounts(): Promise<Account[]>;
128
45
  getAccount(signerAddress: string): Promise<Account>;
129
- abstract setSelectedAccount(address: string): Promise<void>;
46
+ getPublicKey(signerAddress: string): Promise<string>;
47
+ }
48
+ export declare abstract class SignerProviderWithCachedAccounts<T extends Account> extends SignerProviderWithMultipleAccounts {
49
+ private _selectedAccount;
50
+ protected readonly _accounts: Map<string, T>;
51
+ getSelectedAccount(): Promise<T>;
52
+ setSelectedAddress(address: string): Promise<void>;
53
+ getAccounts(): Promise<T[]>;
54
+ getAccount(address: string): Promise<T>;
130
55
  }
131
56
  export declare function verifyHexString(hexString: string, publicKey: string, signature: string): boolean;
132
57
  export declare function verifySignedMessage(message: string, publicKey: string, signature: string): boolean;
133
- export interface Destination {
134
- address: string;
135
- attoAlphAmount: Number256;
136
- tokens?: Token[];
137
- lockTime?: number;
138
- message?: string;
139
- }
140
58
  export declare function toApiDestination(data: Destination): node.Destination;
141
59
  export declare function toApiDestinations(data: Destination[]): node.Destination[];
142
60
  export declare function fromApiDestination(data: node.Destination): Destination;
@@ -43,23 +43,17 @@ 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"));
52
51
  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
52
  class SignerProviderSimple {
53
+ async getSelectedAddress() {
54
+ const account = await this.getSelectedAccount();
55
+ return account.address;
56
+ }
63
57
  getNodeProvider() {
64
58
  if (this.nodeProvider === undefined) {
65
59
  throw Error('The signer does not contain a node provider');
@@ -92,13 +86,8 @@ class SignerProviderSimple {
92
86
  }
93
87
  async usePublicKey(params) {
94
88
  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
- }
89
+ const publicKey = await this.getPublicKey(signerAddress);
90
+ return { fromPublicKey: publicKey, ...restParams };
102
91
  }
103
92
  async signTransferTx(params) {
104
93
  const response = await this.buildTransferTx(params);
@@ -178,8 +167,48 @@ class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
178
167
  return account;
179
168
  }
180
169
  }
170
+ async getPublicKey(signerAddress) {
171
+ const account = await this.getAccount(signerAddress);
172
+ return account.publicKey;
173
+ }
181
174
  }
182
175
  exports.SignerProviderWithMultipleAccounts = SignerProviderWithMultipleAccounts;
176
+ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccounts {
177
+ constructor() {
178
+ super(...arguments);
179
+ this._selectedAccount = undefined;
180
+ this._accounts = new Map();
181
+ }
182
+ getSelectedAccount() {
183
+ if (this._selectedAccount === undefined) {
184
+ throw Error('No account is selected yet');
185
+ }
186
+ else {
187
+ return Promise.resolve(this._selectedAccount);
188
+ }
189
+ }
190
+ setSelectedAddress(address) {
191
+ const accountOpt = this._accounts.get(address);
192
+ if (accountOpt === undefined) {
193
+ throw Error('The address is not in the accounts');
194
+ }
195
+ else {
196
+ this._selectedAccount = accountOpt;
197
+ return Promise.resolve();
198
+ }
199
+ }
200
+ getAccounts() {
201
+ return Promise.resolve(Array.from(this._accounts.values()));
202
+ }
203
+ async getAccount(address) {
204
+ const account = this._accounts.get(address);
205
+ if (account === undefined) {
206
+ throw Error('The address is not in the accounts');
207
+ }
208
+ return Promise.resolve(account);
209
+ }
210
+ }
211
+ exports.SignerProviderWithCachedAccounts = SignerProviderWithCachedAccounts;
183
212
  function verifyHexString(hexString, publicKey, signature) {
184
213
  try {
185
214
  const key = ec.keyFromPublic(publicKey, 'hex');
@@ -199,7 +228,6 @@ function verifySignedMessage(message, publicKey, signature) {
199
228
  return verifyHexString(utils.binToHex(messageHash), publicKey, signature);
200
229
  }
201
230
  exports.verifySignedMessage = verifySignedMessage;
202
- utils_1.assertType;
203
231
  function toApiDestination(data) {
204
232
  return { ...data, attoAlphAmount: (0, api_1.toApiNumber256)(data.attoAlphAmount), tokens: (0, api_1.toApiTokens)(data.tokens) };
205
233
  }
@@ -0,0 +1,109 @@
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
+ onDisconnected: () => Promise<void>;
105
+ onNetworkChanged: (network: {
106
+ networkName: string;
107
+ networkId: number;
108
+ }) => Promise<void>;
109
+ }
@@ -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)();
@@ -1 +1,2 @@
1
1
  export * from './status';
2
+ export * from './sign-verify';
@@ -32,3 +32,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
34
  __exportStar(require("./status"), exports);
35
+ __exportStar(require("./sign-verify"), 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,62 @@
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
+ var desc = Object.getOwnPropertyDescriptor(m, k);
22
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
+ desc = { enumerable: true, get: function() { return m[k]; } };
24
+ }
25
+ Object.defineProperty(o, k2, desc);
26
+ }) : (function(o, m, k, k2) {
27
+ if (k2 === undefined) k2 = k;
28
+ o[k2] = m[k];
29
+ }));
30
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
31
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
32
+ }) : function(o, v) {
33
+ o["default"] = v;
34
+ });
35
+ var __importStar = (this && this.__importStar) || function (mod) {
36
+ if (mod && mod.__esModule) return mod;
37
+ var result = {};
38
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
39
+ __setModuleDefault(result, mod);
40
+ return result;
41
+ };
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.transactionVerifySignature = exports.transactionSign = void 0;
44
+ const utils = __importStar(require("../utils"));
45
+ const elliptic_1 = require("elliptic");
46
+ const ec = new elliptic_1.ec('secp256k1');
47
+ function transactionSign(txHash, privateKey) {
48
+ const keyPair = ec.keyFromPrivate(privateKey);
49
+ const signature = keyPair.sign(txHash);
50
+ return utils.encodeSignature(signature);
51
+ }
52
+ exports.transactionSign = transactionSign;
53
+ function transactionVerifySignature(txHash, publicKey, signature) {
54
+ try {
55
+ const key = ec.keyFromPublic(publicKey, 'hex');
56
+ return key.verify(txHash, utils.signatureDecode(ec, signature));
57
+ }
58
+ catch (error) {
59
+ return false;
60
+ }
61
+ }
62
+ exports.transactionVerifySignature = transactionVerifySignature;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.3.0-rc.3",
3
+ "version": "0.3.0-rc.5",
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,7 +27,7 @@
27
27
  },
28
28
  "author": "Alephium dev <dev@alephium.org>",
29
29
  "config": {
30
- "alephium_version": "1.6.0-rc5",
30
+ "alephium_version": "1.6.1",
31
31
  "explorer_backend_version": "1.11.2"
32
32
  },
33
33
  "scripts": {
@@ -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.1
1336
1336
  * @baseUrl ../
1337
1337
  */
1338
1338
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {