@alephium/web3 0.5.0-rc.7 → 0.5.0-rc.9
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +2 -0
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +57 -20
- package/dist/src/api/api-alephium.js +57 -15
- package/dist/src/api/index.d.ts +1 -0
- package/dist/src/api/index.js +1 -0
- package/dist/src/contract/contract.js +6 -2
- package/dist/src/signer/signer.d.ts +29 -30
- package/dist/src/signer/signer.js +34 -25
- package/dist/src/signer/tx-builder.d.ts +2 -7
- package/dist/src/signer/tx-builder.js +10 -7
- package/dist/src/signer/types.d.ts +8 -0
- package/dist/src/transaction/sign-verify.d.ts +3 -2
- package/dist/src/transaction/sign-verify.js +4 -14
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/sign.d.ts +3 -0
- package/dist/src/utils/sign.js +89 -0
- package/dist/src/utils/utils.d.ts +4 -3
- package/dist/src/utils/utils.js +25 -10
- package/package.json +2 -1
- package/src/api/api-alephium.ts +88 -32
- package/src/api/index.ts +2 -0
- package/src/contract/contract.ts +7 -3
- package/src/signer/signer.ts +69 -55
- package/src/signer/tx-builder.ts +13 -7
- package/src/signer/types.ts +10 -2
- package/src/transaction/sign-verify.ts +10 -15
- package/src/utils/index.ts +1 -0
- package/src/utils/sign.ts +66 -0
- package/src/utils/utils.ts +26 -10
|
@@ -0,0 +1,89 @@
|
|
|
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.verifySignature = exports.sign = void 0;
|
|
44
|
+
const elliptic_1 = require("elliptic");
|
|
45
|
+
const __1 = require("..");
|
|
46
|
+
const necc = __importStar(require("@noble/secp256k1"));
|
|
47
|
+
const crypto_1 = require("crypto");
|
|
48
|
+
const ec = new elliptic_1.ec('secp256k1');
|
|
49
|
+
necc.utils.sha256Sync = (...messages) => {
|
|
50
|
+
const sha256 = (0, crypto_1.createHash)('sha256');
|
|
51
|
+
for (const message of messages)
|
|
52
|
+
sha256.update(message);
|
|
53
|
+
return sha256.digest();
|
|
54
|
+
};
|
|
55
|
+
necc.utils.hmacSha256Sync = (key, ...messages) => {
|
|
56
|
+
const hash = (0, crypto_1.createHmac)('sha256', Buffer.from(key));
|
|
57
|
+
messages.forEach((m) => hash.update(m));
|
|
58
|
+
return Uint8Array.from(hash.digest());
|
|
59
|
+
};
|
|
60
|
+
// hash has to be 32 bytes
|
|
61
|
+
function sign(hash, privateKey, _keyType) {
|
|
62
|
+
const keyType = _keyType ?? 'default';
|
|
63
|
+
if (keyType === 'default') {
|
|
64
|
+
const key = ec.keyFromPrivate(privateKey);
|
|
65
|
+
const signature = key.sign(hash);
|
|
66
|
+
return (0, __1.encodeSignature)(signature);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const signature = necc.schnorr.signSync((0, __1.hexToBinUnsafe)(hash), (0, __1.hexToBinUnsafe)(privateKey));
|
|
70
|
+
return (0, __1.binToHex)(signature);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.sign = sign;
|
|
74
|
+
function verifySignature(hash, publicKey, signature, _keyType) {
|
|
75
|
+
const keyType = _keyType ?? 'default';
|
|
76
|
+
try {
|
|
77
|
+
if (keyType === 'default') {
|
|
78
|
+
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
79
|
+
return key.verify(hash, (0, __1.signatureDecode)(ec, signature));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return necc.schnorr.verifySync((0, __1.hexToBinUnsafe)(signature), (0, __1.hexToBinUnsafe)(hash), (0, __1.hexToBinUnsafe)(publicKey));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.verifySignature = verifySignature;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ec as EC, SignatureInput } from 'elliptic';
|
|
2
2
|
import BN from 'bn.js';
|
|
3
|
+
import { KeyType } from '../signer';
|
|
3
4
|
export declare function encodeSignature(signature: EC.Signature | {
|
|
4
5
|
r: BN;
|
|
5
6
|
s: BN;
|
|
@@ -13,9 +14,9 @@ export declare function contractIdFromAddress(address: string): Uint8Array;
|
|
|
13
14
|
export declare function tokenIdFromAddress(address: string): Uint8Array;
|
|
14
15
|
export declare function hexToBinUnsafe(hex: string): Uint8Array;
|
|
15
16
|
export declare function binToHex(bin: Uint8Array): string;
|
|
16
|
-
export declare function groupOfPrivateKey(privateKey: string): number;
|
|
17
|
-
export declare function publicKeyFromPrivateKey(privateKey: string): string;
|
|
18
|
-
export declare function addressFromPublicKey(publicKey: string): string;
|
|
17
|
+
export declare function groupOfPrivateKey(privateKey: string, keyType?: KeyType): number;
|
|
18
|
+
export declare function publicKeyFromPrivateKey(privateKey: string, _keyType?: KeyType): string;
|
|
19
|
+
export declare function addressFromPublicKey(publicKey: string, _keyType?: KeyType): string;
|
|
19
20
|
export declare function addressFromContractId(contractId: string): string;
|
|
20
21
|
export declare function contractIdFromTx(txId: string, outputIndex: number): string;
|
|
21
22
|
export declare function subContractId(parentContractId: string, pathInHex: string, group: number): string;
|
package/dist/src/utils/utils.js
CHANGED
|
@@ -153,20 +153,35 @@ function binToHex(bin) {
|
|
|
153
153
|
return buffer_1.Buffer.from(bin).toString('hex');
|
|
154
154
|
}
|
|
155
155
|
exports.binToHex = binToHex;
|
|
156
|
-
function groupOfPrivateKey(privateKey) {
|
|
157
|
-
return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey)));
|
|
156
|
+
function groupOfPrivateKey(privateKey, keyType) {
|
|
157
|
+
return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey, keyType), keyType));
|
|
158
158
|
}
|
|
159
159
|
exports.groupOfPrivateKey = groupOfPrivateKey;
|
|
160
|
-
function publicKeyFromPrivateKey(privateKey) {
|
|
161
|
-
const
|
|
162
|
-
|
|
160
|
+
function publicKeyFromPrivateKey(privateKey, _keyType) {
|
|
161
|
+
const keyType = _keyType ?? 'default';
|
|
162
|
+
if (keyType === 'default') {
|
|
163
|
+
const key = ec.keyFromPrivate(privateKey);
|
|
164
|
+
return key.getPublic(true, 'hex');
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
return ec.g.mul(new bn_js_1.default(privateKey, 16)).encode('hex', true).slice(2);
|
|
168
|
+
}
|
|
163
169
|
}
|
|
164
170
|
exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
|
|
165
|
-
function addressFromPublicKey(publicKey) {
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
171
|
+
function addressFromPublicKey(publicKey, _keyType) {
|
|
172
|
+
const keyType = _keyType ?? 'default';
|
|
173
|
+
if (keyType === 'default') {
|
|
174
|
+
const addressType = buffer_1.Buffer.from([AddressType.P2PKH]);
|
|
175
|
+
const hash = buffer_1.Buffer.from(blakejs_1.default.blake2b(buffer_1.Buffer.from(publicKey, 'hex'), undefined, 32));
|
|
176
|
+
const bytes = buffer_1.Buffer.concat([addressType, hash]);
|
|
177
|
+
return bs58_1.default.encode(bytes);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const lockupScript = buffer_1.Buffer.from(`0101000000000458144020${publicKey}8685`, 'hex');
|
|
181
|
+
const lockupScriptHash = blakejs_1.default.blake2b(lockupScript, undefined, 32);
|
|
182
|
+
const addressType = buffer_1.Buffer.from([AddressType.P2SH]);
|
|
183
|
+
return bs58_1.default.encode(buffer_1.Buffer.concat([addressType, lockupScriptHash]));
|
|
184
|
+
}
|
|
170
185
|
}
|
|
171
186
|
exports.addressFromPublicKey = addressFromPublicKey;
|
|
172
187
|
function addressFromContractId(contractId) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.5.0-rc.
|
|
3
|
+
"version": "0.5.0-rc.9",
|
|
4
4
|
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
5
|
"license": "GPL",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"type": "commonjs",
|
|
42
42
|
"dependencies": {
|
|
43
|
+
"@noble/secp256k1": "1.7.1",
|
|
43
44
|
"base-x": "4.0.0",
|
|
44
45
|
"blakejs": "1.2.1",
|
|
45
46
|
"buffer": "^6.0.3",
|
package/src/api/api-alephium.ts
CHANGED
|
@@ -202,9 +202,12 @@ export interface BrokerInfo {
|
|
|
202
202
|
}
|
|
203
203
|
|
|
204
204
|
export interface BuildDeployContractTx {
|
|
205
|
-
/** @format
|
|
205
|
+
/** @format hex-string */
|
|
206
206
|
fromPublicKey: string
|
|
207
207
|
|
|
208
|
+
/** @format hex-string */
|
|
209
|
+
fromPublicKeyType?: string
|
|
210
|
+
|
|
208
211
|
/** @format hex-string */
|
|
209
212
|
bytecode: string
|
|
210
213
|
|
|
@@ -247,9 +250,12 @@ export interface BuildDeployContractTxResult {
|
|
|
247
250
|
}
|
|
248
251
|
|
|
249
252
|
export interface BuildExecuteScriptTx {
|
|
250
|
-
/** @format
|
|
253
|
+
/** @format hex-string */
|
|
251
254
|
fromPublicKey: string
|
|
252
255
|
|
|
256
|
+
/** @format hex-string */
|
|
257
|
+
fromPublicKeyType?: string
|
|
258
|
+
|
|
253
259
|
/** @format hex-string */
|
|
254
260
|
bytecode: string
|
|
255
261
|
|
|
@@ -346,8 +352,11 @@ export interface BuildSweepAddressTransactionsResult {
|
|
|
346
352
|
}
|
|
347
353
|
|
|
348
354
|
export interface BuildTransaction {
|
|
349
|
-
/** @format
|
|
355
|
+
/** @format hex-string */
|
|
350
356
|
fromPublicKey: string
|
|
357
|
+
|
|
358
|
+
/** @format hex-string */
|
|
359
|
+
fromPublicKeyType?: string
|
|
351
360
|
destinations: Destination[]
|
|
352
361
|
utxos?: OutputRef[]
|
|
353
362
|
|
|
@@ -695,6 +704,15 @@ export interface MemPooled {
|
|
|
695
704
|
type: string
|
|
696
705
|
}
|
|
697
706
|
|
|
707
|
+
export interface MempoolTransactions {
|
|
708
|
+
/** @format int32 */
|
|
709
|
+
fromGroup: number
|
|
710
|
+
|
|
711
|
+
/** @format int32 */
|
|
712
|
+
toGroup: number
|
|
713
|
+
transactions: TransactionTemplate[]
|
|
714
|
+
}
|
|
715
|
+
|
|
698
716
|
export interface MinerAddresses {
|
|
699
717
|
addresses: string[]
|
|
700
718
|
}
|
|
@@ -1004,15 +1022,6 @@ export interface Unban {
|
|
|
1004
1022
|
type: string
|
|
1005
1023
|
}
|
|
1006
1024
|
|
|
1007
|
-
export interface UnconfirmedTransactions {
|
|
1008
|
-
/** @format int32 */
|
|
1009
|
-
fromGroup: number
|
|
1010
|
-
|
|
1011
|
-
/** @format int32 */
|
|
1012
|
-
toGroup: number
|
|
1013
|
-
unconfirmedTransactions: TransactionTemplate[]
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
1025
|
export interface Unreachable {
|
|
1017
1026
|
peers: string[]
|
|
1018
1027
|
type: string
|
|
@@ -1334,7 +1343,7 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
1334
1343
|
|
|
1335
1344
|
/**
|
|
1336
1345
|
* @title Alephium API
|
|
1337
|
-
* @version 1.7.
|
|
1346
|
+
* @version 1.7.1
|
|
1338
1347
|
* @baseUrl ../
|
|
1339
1348
|
*/
|
|
1340
1349
|
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
@@ -2070,25 +2079,6 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
|
|
|
2070
2079
|
}).then(convertHttpResponse)
|
|
2071
2080
|
}
|
|
2072
2081
|
transactions = {
|
|
2073
|
-
/**
|
|
2074
|
-
* No description
|
|
2075
|
-
*
|
|
2076
|
-
* @tags Transactions
|
|
2077
|
-
* @name GetTransactionsUnconfirmed
|
|
2078
|
-
* @summary List unconfirmed transactions
|
|
2079
|
-
* @request GET:/transactions/unconfirmed
|
|
2080
|
-
*/
|
|
2081
|
-
getTransactionsUnconfirmed: (params: RequestParams = {}) =>
|
|
2082
|
-
this.request<
|
|
2083
|
-
UnconfirmedTransactions[],
|
|
2084
|
-
BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable
|
|
2085
|
-
>({
|
|
2086
|
-
path: `/transactions/unconfirmed`,
|
|
2087
|
-
method: 'GET',
|
|
2088
|
-
format: 'json',
|
|
2089
|
-
...params
|
|
2090
|
-
}).then(convertHttpResponse),
|
|
2091
|
-
|
|
2092
2082
|
/**
|
|
2093
2083
|
* No description
|
|
2094
2084
|
*
|
|
@@ -2211,6 +2201,72 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
|
|
|
2211
2201
|
...params
|
|
2212
2202
|
}).then(convertHttpResponse)
|
|
2213
2203
|
}
|
|
2204
|
+
mempool = {
|
|
2205
|
+
/**
|
|
2206
|
+
* No description
|
|
2207
|
+
*
|
|
2208
|
+
* @tags Mempool
|
|
2209
|
+
* @name GetMempoolTransactions
|
|
2210
|
+
* @summary List mempool transactions
|
|
2211
|
+
* @request GET:/mempool/transactions
|
|
2212
|
+
*/
|
|
2213
|
+
getMempoolTransactions: (params: RequestParams = {}) =>
|
|
2214
|
+
this.request<
|
|
2215
|
+
MempoolTransactions[],
|
|
2216
|
+
BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable
|
|
2217
|
+
>({
|
|
2218
|
+
path: `/mempool/transactions`,
|
|
2219
|
+
method: 'GET',
|
|
2220
|
+
format: 'json',
|
|
2221
|
+
...params
|
|
2222
|
+
}).then(convertHttpResponse),
|
|
2223
|
+
|
|
2224
|
+
/**
|
|
2225
|
+
* No description
|
|
2226
|
+
*
|
|
2227
|
+
* @tags Mempool
|
|
2228
|
+
* @name DeleteMempoolTransactions
|
|
2229
|
+
* @summary Remove all transactions from mempool
|
|
2230
|
+
* @request DELETE:/mempool/transactions
|
|
2231
|
+
*/
|
|
2232
|
+
deleteMempoolTransactions: (params: RequestParams = {}) =>
|
|
2233
|
+
this.request<void, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
|
|
2234
|
+
path: `/mempool/transactions`,
|
|
2235
|
+
method: 'DELETE',
|
|
2236
|
+
...params
|
|
2237
|
+
}).then(convertHttpResponse),
|
|
2238
|
+
|
|
2239
|
+
/**
|
|
2240
|
+
* No description
|
|
2241
|
+
*
|
|
2242
|
+
* @tags Mempool
|
|
2243
|
+
* @name PutMempoolTransactionsRebroadcast
|
|
2244
|
+
* @summary Rebroadcase a mempool transaction to the network
|
|
2245
|
+
* @request PUT:/mempool/transactions/rebroadcast
|
|
2246
|
+
*/
|
|
2247
|
+
putMempoolTransactionsRebroadcast: (query: { txId: string }, params: RequestParams = {}) =>
|
|
2248
|
+
this.request<void, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
|
|
2249
|
+
path: `/mempool/transactions/rebroadcast`,
|
|
2250
|
+
method: 'PUT',
|
|
2251
|
+
query: query,
|
|
2252
|
+
...params
|
|
2253
|
+
}).then(convertHttpResponse),
|
|
2254
|
+
|
|
2255
|
+
/**
|
|
2256
|
+
* No description
|
|
2257
|
+
*
|
|
2258
|
+
* @tags Mempool
|
|
2259
|
+
* @name PutMempoolTransactionsValidate
|
|
2260
|
+
* @summary Validate all mempool transactions and remove invalid ones
|
|
2261
|
+
* @request PUT:/mempool/transactions/validate
|
|
2262
|
+
*/
|
|
2263
|
+
putMempoolTransactionsValidate: (params: RequestParams = {}) =>
|
|
2264
|
+
this.request<void, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
|
|
2265
|
+
path: `/mempool/transactions/validate`,
|
|
2266
|
+
method: 'PUT',
|
|
2267
|
+
...params
|
|
2268
|
+
}).then(convertHttpResponse)
|
|
2269
|
+
}
|
|
2214
2270
|
contracts = {
|
|
2215
2271
|
/**
|
|
2216
2272
|
* No description
|
package/src/api/index.ts
CHANGED
|
@@ -58,6 +58,7 @@ export class NodeProvider {
|
|
|
58
58
|
readonly blockflow: NodeApi<string>['blockflow']
|
|
59
59
|
readonly addresses: NodeApi<string>['addresses']
|
|
60
60
|
readonly transactions: NodeApi<string>['transactions']
|
|
61
|
+
readonly mempool: NodeApi<string>['mempool']
|
|
61
62
|
readonly contracts: NodeApi<string>['contracts']
|
|
62
63
|
readonly multisig: NodeApi<string>['multisig']
|
|
63
64
|
readonly utils: NodeApi<string>['utils']
|
|
@@ -83,6 +84,7 @@ export class NodeProvider {
|
|
|
83
84
|
this.blockflow = { ...nodeApi.blockflow }
|
|
84
85
|
this.addresses = { ...nodeApi.addresses }
|
|
85
86
|
this.transactions = { ...nodeApi.transactions }
|
|
87
|
+
this.mempool = { ...nodeApi.mempool }
|
|
86
88
|
this.contracts = { ...nodeApi.contracts }
|
|
87
89
|
this.multisig = { ...nodeApi.multisig }
|
|
88
90
|
this.utils = { ...nodeApi.utils }
|
package/src/contract/contract.ts
CHANGED
|
@@ -920,8 +920,10 @@ export class Contract extends Artifact {
|
|
|
920
920
|
params: DeployContractParams<P>
|
|
921
921
|
): Promise<SignDeployContractTxParams> {
|
|
922
922
|
const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {})
|
|
923
|
+
const selectedAccount = await signer.getSelectedAccount()
|
|
923
924
|
const signerParams: SignDeployContractTxParams = {
|
|
924
|
-
signerAddress:
|
|
925
|
+
signerAddress: selectedAccount.address,
|
|
926
|
+
signerKeyType: selectedAccount.keyType,
|
|
925
927
|
bytecode: bytecode,
|
|
926
928
|
initialAttoAlphAmount: params?.initialAttoAlphAmount,
|
|
927
929
|
issueTokenAmount: params?.issueTokenAmount,
|
|
@@ -1063,8 +1065,10 @@ export class Script extends Artifact {
|
|
|
1063
1065
|
signer: SignerProvider,
|
|
1064
1066
|
params: ExecuteScriptParams<P>
|
|
1065
1067
|
): Promise<SignExecuteScriptTxParams> {
|
|
1068
|
+
const selectedAccount = await signer.getSelectedAccount()
|
|
1066
1069
|
const signerParams: SignExecuteScriptTxParams = {
|
|
1067
|
-
signerAddress:
|
|
1070
|
+
signerAddress: selectedAccount.address,
|
|
1071
|
+
signerKeyType: selectedAccount.keyType,
|
|
1068
1072
|
bytecode: this.buildByteCodeToDeploy(params.initialFields ?? {}),
|
|
1069
1073
|
attoAlphAmount: params.attoAlphAmount,
|
|
1070
1074
|
tokens: params.tokens,
|
|
@@ -1289,7 +1293,7 @@ export interface DeployContractParams<P extends Fields = Fields> {
|
|
|
1289
1293
|
assertType<
|
|
1290
1294
|
Eq<
|
|
1291
1295
|
Omit<DeployContractParams<undefined>, 'initialFields'>,
|
|
1292
|
-
Omit<SignDeployContractTxParams, 'signerAddress' | 'bytecode'>
|
|
1296
|
+
Omit<SignDeployContractTxParams, 'signerAddress' | 'signerKeyType' | 'bytecode'>
|
|
1293
1297
|
>
|
|
1294
1298
|
>
|
|
1295
1299
|
export type DeployContractResult<T> = SignDeployContractTxResult & { instance: T }
|
package/src/signer/signer.ts
CHANGED
|
@@ -16,7 +16,6 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { ec as EC } from 'elliptic'
|
|
20
19
|
import { ExplorerProvider, fromApiNumber256, fromApiTokens, NodeProvider, toApiNumber256, toApiTokens } from '../api'
|
|
21
20
|
import { node } from '../api'
|
|
22
21
|
import * as utils from '../utils'
|
|
@@ -43,53 +42,70 @@ import {
|
|
|
43
42
|
ExtSignDeployContractTxParams,
|
|
44
43
|
ExtSignExecuteScriptTxParams,
|
|
45
44
|
ExtSignUnsignedTxParams,
|
|
46
|
-
ExtSignMessageParams
|
|
45
|
+
ExtSignMessageParams,
|
|
46
|
+
KeyType
|
|
47
47
|
} from './types'
|
|
48
48
|
import { TransactionBuilder } from './tx-builder'
|
|
49
|
+
import { addressFromPublicKey, groupOfAddress } from '../utils'
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
export abstract class SignerProvider {
|
|
52
|
+
abstract get nodeProvider(): NodeProvider | undefined
|
|
53
|
+
abstract get explorerProvider(): ExplorerProvider | undefined
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
protected abstract unsafeGetSelectedAccount(): Promise<Account>
|
|
56
|
+
async getSelectedAccount(): Promise<Account> {
|
|
57
|
+
const account = await this.unsafeGetSelectedAccount()
|
|
58
|
+
SignerProvider.validateAccount(account)
|
|
59
|
+
return account
|
|
60
|
+
}
|
|
55
61
|
|
|
56
|
-
|
|
62
|
+
static validateAccount(account: Account): void {
|
|
63
|
+
const derivedAddress = addressFromPublicKey(account.publicKey, account.keyType)
|
|
64
|
+
const derivedGroup = groupOfAddress(derivedAddress)
|
|
65
|
+
if (derivedAddress !== account.address || derivedGroup !== account.group) {
|
|
66
|
+
throw Error(`Invalid accounot data: ${JSON.stringify(account)}`)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
57
69
|
|
|
58
|
-
signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
|
|
59
|
-
signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
|
|
60
|
-
signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
|
|
61
|
-
signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
70
|
+
abstract signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
|
|
71
|
+
abstract signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
|
|
72
|
+
abstract signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
|
|
73
|
+
abstract signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
62
74
|
|
|
63
|
-
signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
75
|
+
abstract signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
64
76
|
// The message will be prefixed with 'Alephium Signed Message: ' before signing
|
|
65
77
|
// so that the resulted signature cannot be reused for building transactions.
|
|
66
|
-
signMessage(params: SignMessageParams): Promise<SignMessageResult>
|
|
78
|
+
abstract signMessage(params: SignMessageParams): Promise<SignMessageResult>
|
|
67
79
|
}
|
|
68
80
|
|
|
69
81
|
// Abstraction for interactive signer (e.g. WalletConnect instance, Extension wallet object)
|
|
70
|
-
export
|
|
71
|
-
extends
|
|
72
|
-
|
|
73
|
-
|
|
82
|
+
export abstract class InteractiveSignerProvider<
|
|
83
|
+
EnableOptions extends EnableOptionsBase = EnableOptionsBase
|
|
84
|
+
> extends SignerProvider {
|
|
85
|
+
protected abstract unsafeEnable(opt?: EnableOptions): Promise<Account>
|
|
86
|
+
async enable(opt?: EnableOptions): Promise<Account> {
|
|
87
|
+
const account = await this.unsafeEnable(opt)
|
|
88
|
+
SignerProvider.validateAccount(account)
|
|
89
|
+
return account
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
abstract disconnect(): Promise<void>
|
|
74
93
|
|
|
75
94
|
// Methods inherited from SignerProvider, but require networkId in the params
|
|
76
|
-
signAndSubmitTransferTx(params: ExtSignTransferTxParams): Promise<SignTransferTxResult>
|
|
77
|
-
signAndSubmitDeployContractTx(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
95
|
+
abstract override signAndSubmitTransferTx(params: ExtSignTransferTxParams): Promise<SignTransferTxResult>
|
|
96
|
+
abstract override signAndSubmitDeployContractTx(
|
|
97
|
+
params: ExtSignDeployContractTxParams
|
|
98
|
+
): Promise<SignDeployContractTxResult>
|
|
99
|
+
abstract override signAndSubmitExecuteScriptTx(
|
|
100
|
+
params: ExtSignExecuteScriptTxParams
|
|
101
|
+
): Promise<SignExecuteScriptTxResult>
|
|
102
|
+
abstract override signAndSubmitUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
103
|
+
abstract override signUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
104
|
+
abstract override signMessage(params: ExtSignMessageParams): Promise<SignMessageResult>
|
|
82
105
|
}
|
|
83
106
|
|
|
84
|
-
export abstract class SignerProviderSimple extends
|
|
85
|
-
abstract get
|
|
86
|
-
|
|
87
|
-
abstract getSelectedAccount(): Promise<Account>
|
|
88
|
-
|
|
89
|
-
async getSelectedAddress(): Promise<Address> {
|
|
90
|
-
const account = await this.getSelectedAccount()
|
|
91
|
-
return account.address
|
|
92
|
-
}
|
|
107
|
+
export abstract class SignerProviderSimple extends SignerProvider {
|
|
108
|
+
abstract override get nodeProvider(): NodeProvider
|
|
93
109
|
|
|
94
110
|
async submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult> {
|
|
95
111
|
const data: node.SubmitTransaction = { unsignedTx: params.unsignedTx, signature: params.signature }
|
|
@@ -133,8 +149,11 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
|
|
|
133
149
|
return { signature, ...response }
|
|
134
150
|
}
|
|
135
151
|
|
|
136
|
-
|
|
137
|
-
return
|
|
152
|
+
async buildTransferTx(params: SignTransferTxParams): Promise<Omit<SignTransferTxResult, 'signature'>> {
|
|
153
|
+
return TransactionBuilder.from(this.nodeProvider).buildTransferTx(
|
|
154
|
+
params,
|
|
155
|
+
await this.getPublicKey(params.signerAddress)
|
|
156
|
+
)
|
|
138
157
|
}
|
|
139
158
|
|
|
140
159
|
async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
|
|
@@ -143,10 +162,13 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
|
|
|
143
162
|
return { signature, ...response }
|
|
144
163
|
}
|
|
145
164
|
|
|
146
|
-
|
|
165
|
+
async buildDeployContractTx(
|
|
147
166
|
params: SignDeployContractTxParams
|
|
148
167
|
): Promise<Omit<SignDeployContractTxResult, 'signature'>> {
|
|
149
|
-
return
|
|
168
|
+
return TransactionBuilder.from(this.nodeProvider).buildDeployContractTx(
|
|
169
|
+
params,
|
|
170
|
+
await this.getPublicKey(params.signerAddress)
|
|
171
|
+
)
|
|
150
172
|
}
|
|
151
173
|
|
|
152
174
|
async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
|
|
@@ -155,16 +177,17 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
|
|
|
155
177
|
return { signature, ...response }
|
|
156
178
|
}
|
|
157
179
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
180
|
+
async buildExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<Omit<SignExecuteScriptTxResult, 'signature'>> {
|
|
181
|
+
return TransactionBuilder.from(this.nodeProvider).buildExecuteScriptTx(
|
|
182
|
+
params,
|
|
183
|
+
await this.getPublicKey(params.signerAddress)
|
|
184
|
+
)
|
|
162
185
|
}
|
|
163
186
|
|
|
164
187
|
// in general, wallet should show the decoded information to user for confirmation
|
|
165
188
|
// please overwrite this function for real wallet
|
|
166
189
|
async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
|
|
167
|
-
const response = await this.buildUnsignedTx(params)
|
|
190
|
+
const response = await TransactionBuilder.from(this.nodeProvider).buildUnsignedTx(params)
|
|
168
191
|
const signature = await this.signRaw(params.signerAddress, response.txId)
|
|
169
192
|
return { signature, ...response }
|
|
170
193
|
}
|
|
@@ -180,7 +203,7 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
|
|
|
180
203
|
}
|
|
181
204
|
|
|
182
205
|
export abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
|
|
183
|
-
abstract
|
|
206
|
+
abstract setSelectedAccount(address: string): Promise<void>
|
|
184
207
|
|
|
185
208
|
abstract getAccounts(): Promise<Account[]>
|
|
186
209
|
|
|
@@ -204,7 +227,7 @@ export abstract class SignerProviderWithCachedAccounts<T extends Account> extend
|
|
|
204
227
|
private _selectedAccount: T | undefined = undefined
|
|
205
228
|
protected readonly _accounts = new Map<Address, T>()
|
|
206
229
|
|
|
207
|
-
|
|
230
|
+
protected unsafeGetSelectedAccount(): Promise<T> {
|
|
208
231
|
if (this._selectedAccount === undefined) {
|
|
209
232
|
throw Error('No account is selected yet')
|
|
210
233
|
} else {
|
|
@@ -212,7 +235,7 @@ export abstract class SignerProviderWithCachedAccounts<T extends Account> extend
|
|
|
212
235
|
}
|
|
213
236
|
}
|
|
214
237
|
|
|
215
|
-
|
|
238
|
+
setSelectedAccount(address: string): Promise<void> {
|
|
216
239
|
const accountOpt = this._accounts.get(address)
|
|
217
240
|
if (accountOpt === undefined) {
|
|
218
241
|
throw Error('The address is not in the accounts')
|
|
@@ -236,23 +259,14 @@ export abstract class SignerProviderWithCachedAccounts<T extends Account> extend
|
|
|
236
259
|
}
|
|
237
260
|
}
|
|
238
261
|
|
|
239
|
-
export function verifyHexString(hexString: string, publicKey: string, signature: string): boolean {
|
|
240
|
-
try {
|
|
241
|
-
const key = ec.keyFromPublic(publicKey, 'hex')
|
|
242
|
-
return key.verify(hexString, utils.signatureDecode(ec, signature))
|
|
243
|
-
} catch (error) {
|
|
244
|
-
return false
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
262
|
function extendMessage(message: string): string {
|
|
249
263
|
return 'Alephium Signed Message: ' + message
|
|
250
264
|
}
|
|
251
265
|
|
|
252
|
-
export function verifySignedMessage(message: string, publicKey: string, signature: string): boolean {
|
|
266
|
+
export function verifySignedMessage(message: string, publicKey: string, signature: string, keyType?: KeyType): boolean {
|
|
253
267
|
const extendedMessage = extendMessage(message)
|
|
254
268
|
const messageHash = blake.blake2b(extendedMessage, undefined, 32)
|
|
255
|
-
return
|
|
269
|
+
return utils.verifySignature(utils.binToHex(messageHash), publicKey, signature, keyType)
|
|
256
270
|
}
|
|
257
271
|
|
|
258
272
|
export function toApiDestination(data: Destination): node.Destination {
|