@alephium/web3 0.5.0-rc.2 → 0.5.0-rc.21
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 +113 -26
- package/dist/src/api/api-alephium.js +62 -18
- package/dist/src/api/api-explorer.d.ts +222 -52
- package/dist/src/api/api-explorer.js +17 -15
- package/dist/src/api/explorer-provider.d.ts +18 -0
- package/dist/src/api/explorer-provider.js +65 -0
- package/dist/src/api/index.d.ts +2 -41
- package/dist/src/api/index.js +6 -116
- package/dist/src/api/node-provider.d.ts +21 -0
- package/dist/src/api/node-provider.js +68 -0
- package/dist/src/api/types.d.ts +10 -2
- package/dist/src/api/types.js +23 -3
- package/dist/src/contract/contract.d.ts +15 -7
- package/dist/src/contract/contract.js +62 -42
- package/dist/src/signer/signer.d.ts +25 -30
- package/dist/src/signer/signer.js +57 -30
- 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 +12 -16
- 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 +2 -0
- package/dist/src/utils/index.js +2 -0
- package/dist/src/utils/number.d.ts +18 -0
- package/dist/src/utils/number.fixture.d.ts +12 -0
- package/dist/src/utils/number.fixture.js +189 -0
- package/dist/src/utils/number.js +148 -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 +7 -5
- package/src/api/api-alephium.ts +260 -207
- package/src/api/api-explorer.ts +327 -126
- package/src/api/explorer-provider.ts +78 -0
- package/src/api/index.ts +2 -146
- package/src/api/node-provider.ts +84 -0
- package/src/api/types.ts +36 -3
- package/src/contract/contract.ts +80 -49
- package/src/signer/signer.ts +87 -66
- package/src/signer/tx-builder.ts +13 -7
- package/src/signer/types.ts +22 -11
- package/src/transaction/sign-verify.ts +10 -15
- package/src/utils/index.ts +2 -0
- package/src/utils/number.fixture.ts +187 -0
- package/src/utils/number.ts +162 -0
- package/src/utils/sign.ts +66 -0
- package/src/utils/utils.ts +26 -10
|
@@ -1,32 +1,26 @@
|
|
|
1
1
|
import { ExplorerProvider, NodeProvider } from '../api';
|
|
2
2
|
import { node } from '../api';
|
|
3
|
-
import { Account,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
get
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
import { Account, EnableOptionsBase, Destination, SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignExecuteScriptTxResult, SignMessageParams, SignMessageResult, SignTransferTxParams, SignTransferTxResult, SignUnsignedTxParams, SignUnsignedTxResult, SubmissionResult, SubmitTransactionParams, KeyType, MessageHasher } from './types';
|
|
4
|
+
export declare abstract class SignerProvider {
|
|
5
|
+
abstract get nodeProvider(): NodeProvider | undefined;
|
|
6
|
+
abstract get explorerProvider(): ExplorerProvider | undefined;
|
|
7
|
+
protected abstract unsafeGetSelectedAccount(): Promise<Account>;
|
|
8
|
+
getSelectedAccount(): Promise<Account>;
|
|
9
|
+
static validateAccount(account: Account): void;
|
|
10
|
+
abstract signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
11
|
+
abstract signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
12
|
+
abstract signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
13
|
+
abstract signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
14
|
+
abstract signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
15
|
+
abstract signMessage(params: SignMessageParams): Promise<SignMessageResult>;
|
|
15
16
|
}
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
signAndSubmitDeployContractTx(params: ExtSignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
21
|
-
signAndSubmitExecuteScriptTx(params: ExtSignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
22
|
-
signAndSubmitUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
23
|
-
signUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
24
|
-
signMessage(params: ExtSignMessageParams): Promise<SignMessageResult>;
|
|
17
|
+
export declare abstract class InteractiveSignerProvider<EnableOptions extends EnableOptionsBase = EnableOptionsBase> extends SignerProvider {
|
|
18
|
+
protected abstract unsafeEnable(opt?: EnableOptions): Promise<Account>;
|
|
19
|
+
enable(opt?: EnableOptions): Promise<Account>;
|
|
20
|
+
abstract disconnect(): Promise<void>;
|
|
25
21
|
}
|
|
26
|
-
export declare abstract class SignerProviderSimple extends
|
|
27
|
-
abstract get
|
|
28
|
-
abstract getSelectedAccount(): Promise<Account>;
|
|
29
|
-
getSelectedAddress(): Promise<Address>;
|
|
22
|
+
export declare abstract class SignerProviderSimple extends SignerProvider {
|
|
23
|
+
abstract get nodeProvider(): NodeProvider;
|
|
30
24
|
submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult>;
|
|
31
25
|
signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
32
26
|
signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
@@ -45,7 +39,7 @@ export declare abstract class SignerProviderSimple extends TransactionBuilder im
|
|
|
45
39
|
abstract signRaw(signerAddress: string, hexString: string): Promise<string>;
|
|
46
40
|
}
|
|
47
41
|
export declare abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
|
|
48
|
-
abstract
|
|
42
|
+
abstract setSelectedAccount(address: string): Promise<void>;
|
|
49
43
|
abstract getAccounts(): Promise<Account[]>;
|
|
50
44
|
getAccount(signerAddress: string): Promise<Account>;
|
|
51
45
|
getPublicKey(signerAddress: string): Promise<string>;
|
|
@@ -53,13 +47,14 @@ export declare abstract class SignerProviderWithMultipleAccounts extends SignerP
|
|
|
53
47
|
export declare abstract class SignerProviderWithCachedAccounts<T extends Account> extends SignerProviderWithMultipleAccounts {
|
|
54
48
|
private _selectedAccount;
|
|
55
49
|
protected readonly _accounts: Map<string, T>;
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
protected unsafeGetSelectedAccount(): Promise<T>;
|
|
51
|
+
setSelectedAccount(address: string): Promise<void>;
|
|
58
52
|
getAccounts(): Promise<T[]>;
|
|
59
53
|
getAccount(address: string): Promise<T>;
|
|
60
54
|
}
|
|
61
|
-
export declare function
|
|
62
|
-
export declare function
|
|
55
|
+
export declare function extendMessage(message: string): string;
|
|
56
|
+
export declare function hashMessage(message: string, hasher: MessageHasher): string;
|
|
57
|
+
export declare function verifySignedMessage(message: string, messageHasher: MessageHasher, publicKey: string, signature: string, keyType?: KeyType): boolean;
|
|
63
58
|
export declare function toApiDestination(data: Destination): node.Destination;
|
|
64
59
|
export declare function toApiDestinations(data: Destination[]): node.Destination[];
|
|
65
60
|
export declare function fromApiDestination(data: node.Destination): Destination;
|
|
@@ -43,18 +43,39 @@ 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.
|
|
47
|
-
const
|
|
46
|
+
exports.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.hashMessage = exports.extendMessage = exports.SignerProviderWithCachedAccounts = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = exports.InteractiveSignerProvider = exports.SignerProvider = void 0;
|
|
47
|
+
const buffer_1 = require("buffer/");
|
|
48
|
+
const crypto_1 = require("crypto");
|
|
48
49
|
const api_1 = require("../api");
|
|
49
50
|
const utils = __importStar(require("../utils"));
|
|
50
51
|
const blakejs_1 = __importDefault(require("blakejs"));
|
|
51
52
|
const tx_builder_1 = require("./tx-builder");
|
|
52
|
-
const
|
|
53
|
-
class
|
|
54
|
-
async
|
|
55
|
-
const account = await this.
|
|
56
|
-
|
|
53
|
+
const utils_1 = require("../utils");
|
|
54
|
+
class SignerProvider {
|
|
55
|
+
async getSelectedAccount() {
|
|
56
|
+
const account = await this.unsafeGetSelectedAccount();
|
|
57
|
+
SignerProvider.validateAccount(account);
|
|
58
|
+
return account;
|
|
59
|
+
}
|
|
60
|
+
static validateAccount(account) {
|
|
61
|
+
const derivedAddress = (0, utils_1.addressFromPublicKey)(account.publicKey, account.keyType);
|
|
62
|
+
const derivedGroup = (0, utils_1.groupOfAddress)(derivedAddress);
|
|
63
|
+
if (derivedAddress !== account.address || derivedGroup !== account.group) {
|
|
64
|
+
throw Error(`Invalid accounot data: ${JSON.stringify(account)}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.SignerProvider = SignerProvider;
|
|
69
|
+
// Abstraction for interactive signer (e.g. WalletConnect instance, Extension wallet object)
|
|
70
|
+
class InteractiveSignerProvider extends SignerProvider {
|
|
71
|
+
async enable(opt) {
|
|
72
|
+
const account = await this.unsafeEnable(opt);
|
|
73
|
+
SignerProvider.validateAccount(account);
|
|
74
|
+
return account;
|
|
57
75
|
}
|
|
76
|
+
}
|
|
77
|
+
exports.InteractiveSignerProvider = InteractiveSignerProvider;
|
|
78
|
+
class SignerProviderSimple extends SignerProvider {
|
|
58
79
|
async submitTransaction(params) {
|
|
59
80
|
const data = { unsignedTx: params.unsignedTx, signature: params.signature };
|
|
60
81
|
return this.nodeProvider.transactions.postTransactionsSubmit(data);
|
|
@@ -90,7 +111,7 @@ class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
|
|
|
90
111
|
return { signature, ...response };
|
|
91
112
|
}
|
|
92
113
|
async buildTransferTx(params) {
|
|
93
|
-
return
|
|
114
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildTransferTx(params, await this.getPublicKey(params.signerAddress));
|
|
94
115
|
}
|
|
95
116
|
async signDeployContractTx(params) {
|
|
96
117
|
const response = await this.buildDeployContractTx(params);
|
|
@@ -98,7 +119,7 @@ class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
|
|
|
98
119
|
return { signature, ...response };
|
|
99
120
|
}
|
|
100
121
|
async buildDeployContractTx(params) {
|
|
101
|
-
return
|
|
122
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildDeployContractTx(params, await this.getPublicKey(params.signerAddress));
|
|
102
123
|
}
|
|
103
124
|
async signExecuteScriptTx(params) {
|
|
104
125
|
const response = await this.buildExecuteScriptTx(params);
|
|
@@ -106,19 +127,18 @@ class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
|
|
|
106
127
|
return { signature, ...response };
|
|
107
128
|
}
|
|
108
129
|
async buildExecuteScriptTx(params) {
|
|
109
|
-
return
|
|
130
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildExecuteScriptTx(params, await this.getPublicKey(params.signerAddress));
|
|
110
131
|
}
|
|
111
132
|
// in general, wallet should show the decoded information to user for confirmation
|
|
112
133
|
// please overwrite this function for real wallet
|
|
113
134
|
async signUnsignedTx(params) {
|
|
114
|
-
const response = await this.buildUnsignedTx(params);
|
|
135
|
+
const response = await tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildUnsignedTx(params);
|
|
115
136
|
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
116
137
|
return { signature, ...response };
|
|
117
138
|
}
|
|
118
139
|
async signMessage(params) {
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
const signature = await this.signRaw(params.signerAddress, utils.binToHex(messageHash));
|
|
140
|
+
const messageHash = hashMessage(params.message, params.messageHasher);
|
|
141
|
+
const signature = await this.signRaw(params.signerAddress, messageHash);
|
|
122
142
|
return { signature: signature };
|
|
123
143
|
}
|
|
124
144
|
}
|
|
@@ -146,7 +166,7 @@ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccount
|
|
|
146
166
|
this._selectedAccount = undefined;
|
|
147
167
|
this._accounts = new Map();
|
|
148
168
|
}
|
|
149
|
-
|
|
169
|
+
unsafeGetSelectedAccount() {
|
|
150
170
|
if (this._selectedAccount === undefined) {
|
|
151
171
|
throw Error('No account is selected yet');
|
|
152
172
|
}
|
|
@@ -154,7 +174,7 @@ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccount
|
|
|
154
174
|
return Promise.resolve(this._selectedAccount);
|
|
155
175
|
}
|
|
156
176
|
}
|
|
157
|
-
|
|
177
|
+
setSelectedAccount(address) {
|
|
158
178
|
const accountOpt = this._accounts.get(address);
|
|
159
179
|
if (accountOpt === undefined) {
|
|
160
180
|
throw Error('The address is not in the accounts');
|
|
@@ -176,23 +196,30 @@ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccount
|
|
|
176
196
|
}
|
|
177
197
|
}
|
|
178
198
|
exports.SignerProviderWithCachedAccounts = SignerProviderWithCachedAccounts;
|
|
179
|
-
function verifyHexString(hexString, publicKey, signature) {
|
|
180
|
-
try {
|
|
181
|
-
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
182
|
-
return key.verify(hexString, utils.signatureDecode(ec, signature));
|
|
183
|
-
}
|
|
184
|
-
catch (error) {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
exports.verifyHexString = verifyHexString;
|
|
189
199
|
function extendMessage(message) {
|
|
190
200
|
return 'Alephium Signed Message: ' + message;
|
|
191
201
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
202
|
+
exports.extendMessage = extendMessage;
|
|
203
|
+
function hashMessage(message, hasher) {
|
|
204
|
+
switch (hasher) {
|
|
205
|
+
case 'alephium':
|
|
206
|
+
return utils.binToHex(blakejs_1.default.blake2b(extendMessage(message), undefined, 32));
|
|
207
|
+
case 'sha256':
|
|
208
|
+
const sha256 = (0, crypto_1.createHash)('sha256');
|
|
209
|
+
sha256.update(buffer_1.Buffer.from(message));
|
|
210
|
+
return utils.binToHex(sha256.digest());
|
|
211
|
+
case 'blake2b':
|
|
212
|
+
return utils.binToHex(blakejs_1.default.blake2b(message, undefined, 32));
|
|
213
|
+
case 'identity':
|
|
214
|
+
return message;
|
|
215
|
+
default:
|
|
216
|
+
throw Error(`Invalid message hasher: ${hasher}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
exports.hashMessage = hashMessage;
|
|
220
|
+
function verifySignedMessage(message, messageHasher, publicKey, signature, keyType) {
|
|
221
|
+
const messageHash = hashMessage(message, messageHasher);
|
|
222
|
+
return utils.verifySignature(messageHash, publicKey, signature, keyType);
|
|
196
223
|
}
|
|
197
224
|
exports.verifySignedMessage = verifySignedMessage;
|
|
198
225
|
function toApiDestination(data) {
|
|
@@ -2,13 +2,8 @@ import { NodeProvider } from '../api';
|
|
|
2
2
|
import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignExecuteScriptTxResult, SignTransferTxParams, SignTransferTxResult, SignUnsignedTxParams, SignUnsignedTxResult } from './types';
|
|
3
3
|
export declare abstract class TransactionBuilder {
|
|
4
4
|
abstract get nodeProvider(): NodeProvider;
|
|
5
|
-
static
|
|
6
|
-
|
|
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
|
-
};
|
|
5
|
+
static from(nodeProvider: NodeProvider): TransactionBuilder;
|
|
6
|
+
static from(baseUrl: string, apiKey?: string): TransactionBuilder;
|
|
12
7
|
private static validatePublicKey;
|
|
13
8
|
buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, 'signature'>>;
|
|
14
9
|
buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, 'signature'>>;
|
|
@@ -23,25 +23,26 @@ const api_1 = require("../api");
|
|
|
23
23
|
const utils_1 = require("../utils");
|
|
24
24
|
const signer_1 = require("./signer");
|
|
25
25
|
class TransactionBuilder {
|
|
26
|
-
static
|
|
27
|
-
const nodeProvider = new api_1.NodeProvider(
|
|
26
|
+
static from(param0, param1) {
|
|
27
|
+
const nodeProvider = typeof param0 === 'string' ? new api_1.NodeProvider(param0, param1) : param0;
|
|
28
28
|
return new (class extends TransactionBuilder {
|
|
29
29
|
get nodeProvider() {
|
|
30
30
|
return nodeProvider;
|
|
31
31
|
}
|
|
32
32
|
})();
|
|
33
33
|
}
|
|
34
|
-
static validatePublicKey(params, publicKey) {
|
|
35
|
-
const address = (0, utils_1.addressFromPublicKey)(publicKey);
|
|
34
|
+
static validatePublicKey(params, publicKey, keyType) {
|
|
35
|
+
const address = (0, utils_1.addressFromPublicKey)(publicKey, keyType);
|
|
36
36
|
if (address !== params.signerAddress) {
|
|
37
37
|
throw new Error('Unmatched public key');
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
async buildTransferTx(params, publicKey) {
|
|
41
|
-
TransactionBuilder.validatePublicKey(params, publicKey);
|
|
41
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType);
|
|
42
42
|
const { destinations, gasPrice, ...rest } = params;
|
|
43
43
|
const data = {
|
|
44
44
|
fromPublicKey: publicKey,
|
|
45
|
+
fromPublicKeyType: params.signerKeyType,
|
|
45
46
|
destinations: (0, signer_1.toApiDestinations)(destinations),
|
|
46
47
|
gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
|
|
47
48
|
...rest
|
|
@@ -50,10 +51,11 @@ class TransactionBuilder {
|
|
|
50
51
|
return { ...response, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
51
52
|
}
|
|
52
53
|
async buildDeployContractTx(params, publicKey) {
|
|
53
|
-
TransactionBuilder.validatePublicKey(params, publicKey);
|
|
54
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType);
|
|
54
55
|
const { initialAttoAlphAmount, initialTokenAmounts, issueTokenAmount, gasPrice, ...rest } = params;
|
|
55
56
|
const data = {
|
|
56
57
|
fromPublicKey: publicKey,
|
|
58
|
+
fromPublicKeyType: params.signerKeyType,
|
|
57
59
|
initialAttoAlphAmount: (0, api_1.toApiNumber256Optional)(initialAttoAlphAmount),
|
|
58
60
|
initialTokenAmounts: (0, api_1.toApiTokens)(initialTokenAmounts),
|
|
59
61
|
issueTokenAmount: (0, api_1.toApiNumber256Optional)(issueTokenAmount),
|
|
@@ -65,10 +67,11 @@ class TransactionBuilder {
|
|
|
65
67
|
return { ...response, groupIndex: response.fromGroup, contractId, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
66
68
|
}
|
|
67
69
|
async buildExecuteScriptTx(params, publicKey) {
|
|
68
|
-
TransactionBuilder.validatePublicKey(params, publicKey);
|
|
70
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType);
|
|
69
71
|
const { attoAlphAmount, tokens, gasPrice, ...rest } = params;
|
|
70
72
|
const data = {
|
|
71
73
|
fromPublicKey: publicKey,
|
|
74
|
+
fromPublicKeyType: params.signerKeyType,
|
|
72
75
|
attoAlphAmount: (0, api_1.toApiNumber256Optional)(attoAlphAmount),
|
|
73
76
|
tokens: (0, api_1.toApiTokens)(tokens),
|
|
74
77
|
gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
|
|
@@ -9,16 +9,20 @@ export interface Destination {
|
|
|
9
9
|
lockTime?: number;
|
|
10
10
|
message?: string;
|
|
11
11
|
}
|
|
12
|
+
export declare type KeyType = 'default' | 'bip340-schnorr';
|
|
12
13
|
export interface Account {
|
|
14
|
+
keyType: KeyType;
|
|
13
15
|
address: string;
|
|
14
16
|
group: number;
|
|
15
17
|
publicKey: string;
|
|
16
18
|
}
|
|
17
19
|
export declare type SignerAddress = {
|
|
18
20
|
signerAddress: string;
|
|
21
|
+
signerKeyType?: KeyType;
|
|
19
22
|
};
|
|
20
23
|
export interface SignTransferTxParams {
|
|
21
24
|
signerAddress: string;
|
|
25
|
+
signerKeyType?: KeyType;
|
|
22
26
|
destinations: Destination[];
|
|
23
27
|
utxos?: OutputRef[];
|
|
24
28
|
gasAmount?: number;
|
|
@@ -35,6 +39,7 @@ export interface SignTransferTxResult {
|
|
|
35
39
|
}
|
|
36
40
|
export interface SignDeployContractTxParams {
|
|
37
41
|
signerAddress: string;
|
|
42
|
+
signerKeyType?: KeyType;
|
|
38
43
|
bytecode: string;
|
|
39
44
|
initialAttoAlphAmount?: Number256;
|
|
40
45
|
initialTokenAmounts?: Token[];
|
|
@@ -54,6 +59,7 @@ export interface SignDeployContractTxResult {
|
|
|
54
59
|
}
|
|
55
60
|
export interface SignExecuteScriptTxParams {
|
|
56
61
|
signerAddress: string;
|
|
62
|
+
signerKeyType?: KeyType;
|
|
57
63
|
bytecode: string;
|
|
58
64
|
attoAlphAmount?: Number256;
|
|
59
65
|
tokens?: Token[];
|
|
@@ -70,6 +76,7 @@ export interface SignExecuteScriptTxResult {
|
|
|
70
76
|
}
|
|
71
77
|
export interface SignUnsignedTxParams {
|
|
72
78
|
signerAddress: string;
|
|
79
|
+
signerKeyType?: KeyType;
|
|
73
80
|
unsignedTx: string;
|
|
74
81
|
}
|
|
75
82
|
export interface SignUnsignedTxResult {
|
|
@@ -81,9 +88,12 @@ export interface SignUnsignedTxResult {
|
|
|
81
88
|
gasAmount: number;
|
|
82
89
|
gasPrice: Number256;
|
|
83
90
|
}
|
|
91
|
+
export declare type MessageHasher = 'alephium' | 'sha256' | 'blake2b' | 'identity';
|
|
84
92
|
export interface SignMessageParams {
|
|
85
93
|
signerAddress: string;
|
|
94
|
+
signerKeyType?: KeyType;
|
|
86
95
|
message: string;
|
|
96
|
+
messageHasher: MessageHasher;
|
|
87
97
|
}
|
|
88
98
|
export interface SignMessageResult {
|
|
89
99
|
signature: string;
|
|
@@ -99,21 +109,7 @@ export interface SubmissionResult {
|
|
|
99
109
|
}
|
|
100
110
|
export interface EnableOptionsBase {
|
|
101
111
|
chainGroup?: number;
|
|
112
|
+
keyType?: KeyType;
|
|
102
113
|
networkId: string;
|
|
103
|
-
onDisconnected: () => Promise<void
|
|
114
|
+
onDisconnected: () => Promise<void> | void;
|
|
104
115
|
}
|
|
105
|
-
export declare type ExtSignTransferTxParams = SignTransferTxParams & {
|
|
106
|
-
networkId: string;
|
|
107
|
-
};
|
|
108
|
-
export declare type ExtSignDeployContractTxParams = SignDeployContractTxParams & {
|
|
109
|
-
networkId: string;
|
|
110
|
-
};
|
|
111
|
-
export declare type ExtSignExecuteScriptTxParams = SignExecuteScriptTxParams & {
|
|
112
|
-
networkId: string;
|
|
113
|
-
};
|
|
114
|
-
export declare type ExtSignUnsignedTxParams = SignUnsignedTxParams & {
|
|
115
|
-
networkId: string;
|
|
116
|
-
};
|
|
117
|
-
export declare type ExtSignMessageParams = SignMessageParams & {
|
|
118
|
-
networkId: string;
|
|
119
|
-
};
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
1
|
+
import { KeyType } from '../signer';
|
|
2
|
+
export declare function transactionSign(txId: string, privateKey: string, keyType?: KeyType): string;
|
|
3
|
+
export declare function transactionVerifySignature(txId: string, publicKey: string, signature: string, keyType?: KeyType): boolean;
|
|
@@ -42,21 +42,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
42
42
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
43
|
exports.transactionVerifySignature = exports.transactionSign = void 0;
|
|
44
44
|
const utils = __importStar(require("../utils"));
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function transactionSign(txHash, privateKey) {
|
|
48
|
-
const keyPair = ec.keyFromPrivate(privateKey);
|
|
49
|
-
const signature = keyPair.sign(txHash);
|
|
50
|
-
return utils.encodeSignature(signature);
|
|
45
|
+
function transactionSign(txId, privateKey, keyType) {
|
|
46
|
+
return utils.sign(txId, privateKey, keyType);
|
|
51
47
|
}
|
|
52
48
|
exports.transactionSign = transactionSign;
|
|
53
|
-
function transactionVerifySignature(
|
|
54
|
-
|
|
55
|
-
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
56
|
-
return key.verify(txHash, utils.signatureDecode(ec, signature));
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
49
|
+
function transactionVerifySignature(txId, publicKey, signature, keyType) {
|
|
50
|
+
return utils.verifySignature(txId, publicKey, signature, keyType);
|
|
61
51
|
}
|
|
62
52
|
exports.transactionVerifySignature = transactionVerifySignature;
|
package/dist/src/utils/index.js
CHANGED
|
@@ -36,3 +36,5 @@ __exportStar(require("./bs58"), exports);
|
|
|
36
36
|
__exportStar(require("./djb2"), exports);
|
|
37
37
|
__exportStar(require("./utils"), exports);
|
|
38
38
|
__exportStar(require("./subscription"), exports);
|
|
39
|
+
__exportStar(require("./sign"), exports);
|
|
40
|
+
__exportStar(require("./number"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Number256 } from '..';
|
|
2
|
+
export declare const isNumeric: (numToCheck: any) => boolean;
|
|
3
|
+
export interface IPrettifyNumberConfig {
|
|
4
|
+
minDecimalPlaces: number;
|
|
5
|
+
maxDecimalPlaces: number;
|
|
6
|
+
/** significant digits to show in decimals while respecting decimal places */
|
|
7
|
+
minDecimalSignificantDigits: number;
|
|
8
|
+
/** special case for zero, e.g. we may want to display $0.00 or 0.0 ALPH */
|
|
9
|
+
decimalPlacesWhenZero: number;
|
|
10
|
+
}
|
|
11
|
+
export declare const prettifyNumberConfig: Record<string, IPrettifyNumberConfig>;
|
|
12
|
+
export declare function prettifyAttoAlphAmount(amount: Number256): string | undefined;
|
|
13
|
+
export declare function prettifyTokenAmount(amount: Number256, decimals: number): string | undefined;
|
|
14
|
+
export declare function prettifyExactAmount(amount: Number256, decimals: number): string | undefined;
|
|
15
|
+
export declare function prettifyNumber(amount: Number256, decimals: number, config: IPrettifyNumberConfig): string | undefined;
|
|
16
|
+
export declare function convertAmountWithDecimals(amount: string | number, decimals: number): bigint | undefined;
|
|
17
|
+
export declare function convertAlphAmount(amount: string | number): bigint | undefined;
|
|
18
|
+
export declare function number256ToBigint(number: Number256): bigint;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tests1 = exports.tests = void 0;
|
|
4
|
+
/*
|
|
5
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
6
|
+
This file is part of the alephium project.
|
|
7
|
+
|
|
8
|
+
The library is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
The library is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
*/
|
|
21
|
+
exports.tests = [
|
|
22
|
+
{
|
|
23
|
+
raw: 123456n,
|
|
24
|
+
decimal: 5,
|
|
25
|
+
exact: '1.23456',
|
|
26
|
+
alphFormat: '1.23',
|
|
27
|
+
tokenFormat: '1.2346'
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
raw: 12345612n,
|
|
31
|
+
decimal: 2,
|
|
32
|
+
exact: '123,456.12',
|
|
33
|
+
alphFormat: '123,456.12',
|
|
34
|
+
tokenFormat: '123,456.12'
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
raw: 123456123456n,
|
|
38
|
+
decimal: 6,
|
|
39
|
+
exact: '123,456.123456',
|
|
40
|
+
alphFormat: '123,456.12',
|
|
41
|
+
tokenFormat: '123,456.1235'
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
raw: 12n,
|
|
45
|
+
decimal: 2,
|
|
46
|
+
exact: '0.12',
|
|
47
|
+
alphFormat: '0.12',
|
|
48
|
+
tokenFormat: '0.12'
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
raw: 123456n,
|
|
52
|
+
decimal: 6,
|
|
53
|
+
exact: '0.123456',
|
|
54
|
+
alphFormat: '0.12',
|
|
55
|
+
tokenFormat: '0.1235'
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
raw: 123456n,
|
|
59
|
+
decimal: 7,
|
|
60
|
+
exact: '0.0123456',
|
|
61
|
+
alphFormat: '0.012',
|
|
62
|
+
tokenFormat: '0.0123'
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
raw: 123456n,
|
|
66
|
+
decimal: 8,
|
|
67
|
+
exact: '0.00123456',
|
|
68
|
+
alphFormat: '0.0012',
|
|
69
|
+
tokenFormat: '0.0012'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
raw: 123456n,
|
|
73
|
+
decimal: 9,
|
|
74
|
+
exact: '0.000123456',
|
|
75
|
+
alphFormat: '0.00012',
|
|
76
|
+
tokenFormat: '0.00012'
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
raw: 123456n,
|
|
80
|
+
decimal: 11,
|
|
81
|
+
exact: '0.00000123456',
|
|
82
|
+
alphFormat: '0.0000012',
|
|
83
|
+
tokenFormat: '0.0000012'
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
raw: -123456n,
|
|
87
|
+
decimal: 11,
|
|
88
|
+
exact: '-0.00000123456',
|
|
89
|
+
alphFormat: '-0.0000012',
|
|
90
|
+
tokenFormat: '-0.0000012'
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
raw: 8923088n,
|
|
94
|
+
decimal: 10,
|
|
95
|
+
exact: '0.0008923088',
|
|
96
|
+
alphFormat: '0.00089',
|
|
97
|
+
tokenFormat: '0.00089'
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
raw: 885n,
|
|
101
|
+
decimal: 6,
|
|
102
|
+
exact: '0.000885',
|
|
103
|
+
alphFormat: '0.00089',
|
|
104
|
+
tokenFormat: '0.00089'
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
raw: 100000000000n,
|
|
108
|
+
decimal: 18,
|
|
109
|
+
exact: '0.0000001',
|
|
110
|
+
alphFormat: '0.0000001',
|
|
111
|
+
tokenFormat: '0.0000001'
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
raw: 1504000000000000000n,
|
|
115
|
+
decimal: 18,
|
|
116
|
+
exact: '1.504',
|
|
117
|
+
alphFormat: '1.50',
|
|
118
|
+
tokenFormat: '1.504'
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
raw: 1505000000000000000n,
|
|
122
|
+
decimal: 18,
|
|
123
|
+
exact: '1.505',
|
|
124
|
+
alphFormat: '1.51',
|
|
125
|
+
tokenFormat: '1.505'
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
raw: 1500050000000000000n,
|
|
129
|
+
decimal: 18,
|
|
130
|
+
exact: '1.50005',
|
|
131
|
+
alphFormat: '1.50',
|
|
132
|
+
tokenFormat: '1.5001'
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
raw: 100n,
|
|
136
|
+
decimal: 0,
|
|
137
|
+
exact: '100',
|
|
138
|
+
alphFormat: '100.00',
|
|
139
|
+
tokenFormat: '100.0'
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
raw: 123456789n,
|
|
143
|
+
decimal: 0,
|
|
144
|
+
exact: '123,456,789',
|
|
145
|
+
alphFormat: '123,456,789.00',
|
|
146
|
+
tokenFormat: '123,456,789.0'
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
raw: -123456789n,
|
|
150
|
+
decimal: 0,
|
|
151
|
+
exact: '-123,456,789',
|
|
152
|
+
alphFormat: '-123,456,789.00',
|
|
153
|
+
tokenFormat: '-123,456,789.0'
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
raw: 0n,
|
|
157
|
+
decimal: 0,
|
|
158
|
+
exact: '0',
|
|
159
|
+
alphFormat: '0.00',
|
|
160
|
+
tokenFormat: '0.0'
|
|
161
|
+
}
|
|
162
|
+
];
|
|
163
|
+
exports.tests1 = [
|
|
164
|
+
{
|
|
165
|
+
raw: '0',
|
|
166
|
+
decimals: 18,
|
|
167
|
+
amount: 0n
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
raw: '1.23',
|
|
171
|
+
decimals: 2,
|
|
172
|
+
amount: 123n
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
raw: '1',
|
|
176
|
+
decimals: 5,
|
|
177
|
+
amount: 100000n
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
raw: '1',
|
|
181
|
+
decimals: 18,
|
|
182
|
+
amount: 1000000000000000000n
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
raw: '1.23456789',
|
|
186
|
+
decimals: 18,
|
|
187
|
+
amount: 1234567890000000000n
|
|
188
|
+
}
|
|
189
|
+
];
|