@alephium/web3 0.9.0-rc.0 → 0.9.0-rc.1
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 +3 -0
- package/dist/alephium-web3.min.js.LICENSE.txt +12 -0
- package/dist/alephium-web3.min.js.map +1 -0
- package/dist/scripts/check-versions.d.ts +1 -0
- package/dist/scripts/check-versions.js +39 -0
- package/dist/scripts/header.d.ts +0 -0
- package/dist/scripts/header.js +18 -0
- package/dist/src/api/api-alephium.d.ts +1717 -0
- package/dist/src/api/api-alephium.js +1284 -0
- package/dist/src/api/api-explorer.d.ts +923 -0
- package/dist/src/api/api-explorer.js +711 -0
- package/dist/src/api/explorer-provider.d.ts +20 -0
- package/dist/src/api/explorer-provider.js +69 -0
- package/dist/src/api/index.d.ts +6 -0
- package/dist/src/api/index.js +52 -0
- package/dist/src/api/node-provider.d.ts +38 -0
- package/dist/src/api/node-provider.js +99 -0
- package/dist/src/api/types.d.ts +38 -0
- package/dist/src/api/types.js +259 -0
- package/dist/src/api/utils.d.ts +12 -0
- package/dist/src/api/utils.js +59 -0
- package/dist/src/constants.d.ts +6 -0
- package/dist/src/constants.js +26 -0
- package/dist/src/contract/contract.d.ts +297 -0
- package/dist/src/contract/contract.js +1097 -0
- package/dist/src/contract/events.d.ts +11 -0
- package/dist/src/contract/events.js +65 -0
- package/dist/src/contract/index.d.ts +3 -0
- package/dist/src/contract/index.js +36 -0
- package/dist/src/contract/ralph.d.ts +13 -0
- package/dist/src/contract/ralph.js +397 -0
- package/dist/src/global.d.ts +7 -0
- package/dist/src/global.js +54 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +58 -0
- package/dist/src/signer/index.d.ts +3 -0
- package/dist/src/signer/index.js +36 -0
- package/dist/src/signer/signer.d.ts +60 -0
- package/dist/src/signer/signer.js +236 -0
- package/dist/src/signer/tx-builder.d.ts +12 -0
- package/dist/src/signer/tx-builder.js +96 -0
- package/dist/src/signer/types.d.ts +115 -0
- package/dist/src/signer/types.js +30 -0
- package/dist/src/token/index.d.ts +1 -0
- package/dist/src/token/index.js +34 -0
- package/dist/src/token/nft.d.ts +10 -0
- package/dist/src/token/nft.js +19 -0
- package/dist/src/transaction/index.d.ts +2 -0
- package/dist/src/transaction/index.js +35 -0
- package/dist/src/transaction/sign-verify.d.ts +3 -0
- package/dist/src/transaction/sign-verify.js +52 -0
- package/dist/src/transaction/status.d.ts +11 -0
- package/dist/src/transaction/status.js +49 -0
- package/dist/src/utils/address.d.ts +1 -0
- package/dist/src/utils/address.js +42 -0
- package/dist/src/utils/bs58.d.ts +5 -0
- package/dist/src/utils/bs58.js +40 -0
- package/dist/src/utils/djb2.d.ts +1 -0
- package/dist/src/utils/djb2.js +27 -0
- package/dist/src/utils/index.d.ts +7 -0
- package/dist/src/utils/index.js +40 -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 +149 -0
- package/dist/src/utils/sign.d.ts +3 -0
- package/dist/src/utils/sign.js +89 -0
- package/dist/src/utils/subscription.d.ts +21 -0
- package/dist/src/utils/subscription.js +51 -0
- package/dist/src/utils/utils.d.ts +40 -0
- package/dist/src/utils/utils.js +244 -0
- package/package.json +1 -1
- package/LICENSE +0 -165
|
@@ -0,0 +1,36 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
31
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
__exportStar(require("./signer"), exports);
|
|
35
|
+
__exportStar(require("./types"), exports);
|
|
36
|
+
__exportStar(require("./tx-builder"), exports);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ExplorerProvider, NodeProvider } from '../api';
|
|
2
|
+
import { node } from '../api';
|
|
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>;
|
|
16
|
+
}
|
|
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>;
|
|
21
|
+
}
|
|
22
|
+
export declare abstract class SignerProviderSimple extends SignerProvider {
|
|
23
|
+
abstract get nodeProvider(): NodeProvider;
|
|
24
|
+
submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult>;
|
|
25
|
+
signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
26
|
+
signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
27
|
+
signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
28
|
+
signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
29
|
+
protected abstract getPublicKey(address: string): Promise<string>;
|
|
30
|
+
private usePublicKey;
|
|
31
|
+
signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
32
|
+
buildTransferTx(params: SignTransferTxParams): Promise<Omit<SignTransferTxResult, 'signature'>>;
|
|
33
|
+
signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
34
|
+
buildDeployContractTx(params: SignDeployContractTxParams): Promise<Omit<SignDeployContractTxResult, 'signature'>>;
|
|
35
|
+
signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
36
|
+
buildExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<Omit<SignExecuteScriptTxResult, 'signature'>>;
|
|
37
|
+
signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
38
|
+
signMessage(params: SignMessageParams): Promise<SignMessageResult>;
|
|
39
|
+
abstract signRaw(signerAddress: string, hexString: string): Promise<string>;
|
|
40
|
+
}
|
|
41
|
+
export declare abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
|
|
42
|
+
abstract setSelectedAccount(address: string): Promise<void>;
|
|
43
|
+
abstract getAccounts(): Promise<Account[]>;
|
|
44
|
+
getAccount(signerAddress: string): Promise<Account>;
|
|
45
|
+
getPublicKey(signerAddress: string): Promise<string>;
|
|
46
|
+
}
|
|
47
|
+
export declare abstract class SignerProviderWithCachedAccounts<T extends Account> extends SignerProviderWithMultipleAccounts {
|
|
48
|
+
private _selectedAccount;
|
|
49
|
+
protected readonly _accounts: Map<string, T>;
|
|
50
|
+
protected unsafeGetSelectedAccount(): Promise<T>;
|
|
51
|
+
setSelectedAccount(address: string): Promise<void>;
|
|
52
|
+
getAccounts(): Promise<T[]>;
|
|
53
|
+
getAccount(address: string): Promise<T>;
|
|
54
|
+
}
|
|
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;
|
|
58
|
+
export declare function toApiDestination(data: Destination): node.Destination;
|
|
59
|
+
export declare function toApiDestinations(data: Destination[]): node.Destination[];
|
|
60
|
+
export declare function fromApiDestination(data: node.Destination): Destination;
|
|
@@ -0,0 +1,236 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
43
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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");
|
|
49
|
+
const api_1 = require("../api");
|
|
50
|
+
const utils = __importStar(require("../utils"));
|
|
51
|
+
const blakejs_1 = __importDefault(require("blakejs"));
|
|
52
|
+
const tx_builder_1 = require("./tx-builder");
|
|
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;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.InteractiveSignerProvider = InteractiveSignerProvider;
|
|
78
|
+
class SignerProviderSimple extends SignerProvider {
|
|
79
|
+
async submitTransaction(params) {
|
|
80
|
+
const data = { unsignedTx: params.unsignedTx, signature: params.signature };
|
|
81
|
+
return this.nodeProvider.transactions.postTransactionsSubmit(data);
|
|
82
|
+
}
|
|
83
|
+
async signAndSubmitTransferTx(params) {
|
|
84
|
+
const signResult = await this.signTransferTx(params);
|
|
85
|
+
await this.submitTransaction(signResult);
|
|
86
|
+
return signResult;
|
|
87
|
+
}
|
|
88
|
+
async signAndSubmitDeployContractTx(params) {
|
|
89
|
+
const signResult = await this.signDeployContractTx(params);
|
|
90
|
+
await this.submitTransaction(signResult);
|
|
91
|
+
return signResult;
|
|
92
|
+
}
|
|
93
|
+
async signAndSubmitExecuteScriptTx(params) {
|
|
94
|
+
const signResult = await this.signExecuteScriptTx(params);
|
|
95
|
+
await this.submitTransaction(signResult);
|
|
96
|
+
return signResult;
|
|
97
|
+
}
|
|
98
|
+
async signAndSubmitUnsignedTx(params) {
|
|
99
|
+
const signResult = await this.signUnsignedTx(params);
|
|
100
|
+
await this.submitTransaction(signResult);
|
|
101
|
+
return signResult;
|
|
102
|
+
}
|
|
103
|
+
async usePublicKey(params) {
|
|
104
|
+
const { signerAddress, ...restParams } = params;
|
|
105
|
+
const publicKey = await this.getPublicKey(signerAddress);
|
|
106
|
+
return { fromPublicKey: publicKey, ...restParams };
|
|
107
|
+
}
|
|
108
|
+
async signTransferTx(params) {
|
|
109
|
+
const response = await this.buildTransferTx(params);
|
|
110
|
+
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
111
|
+
return { signature, ...response };
|
|
112
|
+
}
|
|
113
|
+
async buildTransferTx(params) {
|
|
114
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildTransferTx(params, await this.getPublicKey(params.signerAddress));
|
|
115
|
+
}
|
|
116
|
+
async signDeployContractTx(params) {
|
|
117
|
+
const response = await this.buildDeployContractTx(params);
|
|
118
|
+
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
119
|
+
return { signature, ...response };
|
|
120
|
+
}
|
|
121
|
+
async buildDeployContractTx(params) {
|
|
122
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildDeployContractTx(params, await this.getPublicKey(params.signerAddress));
|
|
123
|
+
}
|
|
124
|
+
async signExecuteScriptTx(params) {
|
|
125
|
+
const response = await this.buildExecuteScriptTx(params);
|
|
126
|
+
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
127
|
+
return { signature, ...response };
|
|
128
|
+
}
|
|
129
|
+
async buildExecuteScriptTx(params) {
|
|
130
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildExecuteScriptTx(params, await this.getPublicKey(params.signerAddress));
|
|
131
|
+
}
|
|
132
|
+
// in general, wallet should show the decoded information to user for confirmation
|
|
133
|
+
// please overwrite this function for real wallet
|
|
134
|
+
async signUnsignedTx(params) {
|
|
135
|
+
const response = await tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildUnsignedTx(params);
|
|
136
|
+
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
137
|
+
return { signature, ...response };
|
|
138
|
+
}
|
|
139
|
+
async signMessage(params) {
|
|
140
|
+
const messageHash = hashMessage(params.message, params.messageHasher);
|
|
141
|
+
const signature = await this.signRaw(params.signerAddress, messageHash);
|
|
142
|
+
return { signature: signature };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.SignerProviderSimple = SignerProviderSimple;
|
|
146
|
+
class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
|
|
147
|
+
async getAccount(signerAddress) {
|
|
148
|
+
const accounts = await this.getAccounts();
|
|
149
|
+
const account = accounts.find((a) => a.address === signerAddress);
|
|
150
|
+
if (typeof account === 'undefined') {
|
|
151
|
+
throw new Error('Unmatched signerAddress');
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
return account;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async getPublicKey(signerAddress) {
|
|
158
|
+
const account = await this.getAccount(signerAddress);
|
|
159
|
+
return account.publicKey;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
exports.SignerProviderWithMultipleAccounts = SignerProviderWithMultipleAccounts;
|
|
163
|
+
class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccounts {
|
|
164
|
+
constructor() {
|
|
165
|
+
super(...arguments);
|
|
166
|
+
this._selectedAccount = undefined;
|
|
167
|
+
this._accounts = new Map();
|
|
168
|
+
}
|
|
169
|
+
unsafeGetSelectedAccount() {
|
|
170
|
+
if (this._selectedAccount === undefined) {
|
|
171
|
+
throw Error('No account is selected yet');
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
return Promise.resolve(this._selectedAccount);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
setSelectedAccount(address) {
|
|
178
|
+
const accountOpt = this._accounts.get(address);
|
|
179
|
+
if (accountOpt === undefined) {
|
|
180
|
+
throw Error('The address is not in the accounts');
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
this._selectedAccount = accountOpt;
|
|
184
|
+
return Promise.resolve();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
getAccounts() {
|
|
188
|
+
return Promise.resolve(Array.from(this._accounts.values()));
|
|
189
|
+
}
|
|
190
|
+
async getAccount(address) {
|
|
191
|
+
const account = this._accounts.get(address);
|
|
192
|
+
if (account === undefined) {
|
|
193
|
+
throw Error('The address is not in the accounts');
|
|
194
|
+
}
|
|
195
|
+
return Promise.resolve(account);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.SignerProviderWithCachedAccounts = SignerProviderWithCachedAccounts;
|
|
199
|
+
function extendMessage(message) {
|
|
200
|
+
return 'Alephium Signed Message: ' + message;
|
|
201
|
+
}
|
|
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);
|
|
223
|
+
}
|
|
224
|
+
exports.verifySignedMessage = verifySignedMessage;
|
|
225
|
+
function toApiDestination(data) {
|
|
226
|
+
return { ...data, attoAlphAmount: (0, api_1.toApiNumber256)(data.attoAlphAmount), tokens: (0, api_1.toApiTokens)(data.tokens) };
|
|
227
|
+
}
|
|
228
|
+
exports.toApiDestination = toApiDestination;
|
|
229
|
+
function toApiDestinations(data) {
|
|
230
|
+
return data.map(toApiDestination);
|
|
231
|
+
}
|
|
232
|
+
exports.toApiDestinations = toApiDestinations;
|
|
233
|
+
function fromApiDestination(data) {
|
|
234
|
+
return { ...data, attoAlphAmount: (0, api_1.fromApiNumber256)(data.attoAlphAmount), tokens: (0, api_1.fromApiTokens)(data.tokens) };
|
|
235
|
+
}
|
|
236
|
+
exports.fromApiDestination = fromApiDestination;
|
|
@@ -0,0 +1,12 @@
|
|
|
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 from(nodeProvider: NodeProvider): TransactionBuilder;
|
|
6
|
+
static from(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): TransactionBuilder;
|
|
7
|
+
private static validatePublicKey;
|
|
8
|
+
buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, 'signature'>>;
|
|
9
|
+
buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, 'signature'>>;
|
|
10
|
+
buildExecuteScriptTx(params: SignExecuteScriptTxParams, publicKey: string): Promise<Omit<SignExecuteScriptTxResult, 'signature'>>;
|
|
11
|
+
buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, 'signature'>>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
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 from(param0, param1, customFetch) {
|
|
27
|
+
const nodeProvider = typeof param0 === 'string' ? new api_1.NodeProvider(param0, param1, customFetch) : param0;
|
|
28
|
+
return new (class extends TransactionBuilder {
|
|
29
|
+
get nodeProvider() {
|
|
30
|
+
return nodeProvider;
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
33
|
+
}
|
|
34
|
+
static validatePublicKey(params, publicKey, keyType) {
|
|
35
|
+
const address = (0, utils_1.addressFromPublicKey)(publicKey, keyType);
|
|
36
|
+
if (address !== params.signerAddress) {
|
|
37
|
+
throw new Error('Unmatched public key');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async buildTransferTx(params, publicKey) {
|
|
41
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType);
|
|
42
|
+
const { destinations, gasPrice, ...rest } = params;
|
|
43
|
+
const data = {
|
|
44
|
+
fromPublicKey: publicKey,
|
|
45
|
+
fromPublicKeyType: params.signerKeyType,
|
|
46
|
+
destinations: (0, signer_1.toApiDestinations)(destinations),
|
|
47
|
+
gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
|
|
48
|
+
...rest
|
|
49
|
+
};
|
|
50
|
+
const response = await this.nodeProvider.transactions.postTransactionsBuild(data);
|
|
51
|
+
return { ...response, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
52
|
+
}
|
|
53
|
+
async buildDeployContractTx(params, publicKey) {
|
|
54
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType);
|
|
55
|
+
const { initialAttoAlphAmount, initialTokenAmounts, issueTokenAmount, gasPrice, ...rest } = params;
|
|
56
|
+
const data = {
|
|
57
|
+
fromPublicKey: publicKey,
|
|
58
|
+
fromPublicKeyType: params.signerKeyType,
|
|
59
|
+
initialAttoAlphAmount: (0, api_1.toApiNumber256Optional)(initialAttoAlphAmount),
|
|
60
|
+
initialTokenAmounts: (0, api_1.toApiTokens)(initialTokenAmounts),
|
|
61
|
+
issueTokenAmount: (0, api_1.toApiNumber256Optional)(issueTokenAmount),
|
|
62
|
+
gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
|
|
63
|
+
...rest
|
|
64
|
+
};
|
|
65
|
+
const response = await this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data);
|
|
66
|
+
const contractId = __1.utils.binToHex(__1.utils.contractIdFromAddress(response.contractAddress));
|
|
67
|
+
return { ...response, groupIndex: response.fromGroup, contractId, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
68
|
+
}
|
|
69
|
+
async buildExecuteScriptTx(params, publicKey) {
|
|
70
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType);
|
|
71
|
+
const { attoAlphAmount, tokens, gasPrice, ...rest } = params;
|
|
72
|
+
const data = {
|
|
73
|
+
fromPublicKey: publicKey,
|
|
74
|
+
fromPublicKeyType: params.signerKeyType,
|
|
75
|
+
attoAlphAmount: (0, api_1.toApiNumber256Optional)(attoAlphAmount),
|
|
76
|
+
tokens: (0, api_1.toApiTokens)(tokens),
|
|
77
|
+
gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
|
|
78
|
+
...rest
|
|
79
|
+
};
|
|
80
|
+
const response = await this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data);
|
|
81
|
+
return { ...response, groupIndex: response.fromGroup, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
82
|
+
}
|
|
83
|
+
async buildUnsignedTx(params) {
|
|
84
|
+
const data = { unsignedTx: params.unsignedTx };
|
|
85
|
+
const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data);
|
|
86
|
+
return {
|
|
87
|
+
fromGroup: decoded.fromGroup,
|
|
88
|
+
toGroup: decoded.toGroup,
|
|
89
|
+
unsignedTx: params.unsignedTx,
|
|
90
|
+
txId: decoded.unsignedTx.txId,
|
|
91
|
+
gasAmount: decoded.unsignedTx.gasAmount,
|
|
92
|
+
gasPrice: (0, api_1.fromApiNumber256)(decoded.unsignedTx.gasPrice)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.TransactionBuilder = TransactionBuilder;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Number256, Token } from '../api';
|
|
2
|
+
import { node } from '../api';
|
|
3
|
+
export type Address = string;
|
|
4
|
+
export 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 type KeyType = 'default' | 'bip340-schnorr';
|
|
13
|
+
export interface Account {
|
|
14
|
+
keyType: KeyType;
|
|
15
|
+
address: string;
|
|
16
|
+
group: number;
|
|
17
|
+
publicKey: string;
|
|
18
|
+
}
|
|
19
|
+
export type SignerAddress = {
|
|
20
|
+
signerAddress: string;
|
|
21
|
+
signerKeyType?: KeyType;
|
|
22
|
+
};
|
|
23
|
+
export interface SignTransferTxParams {
|
|
24
|
+
signerAddress: string;
|
|
25
|
+
signerKeyType?: KeyType;
|
|
26
|
+
destinations: Destination[];
|
|
27
|
+
utxos?: OutputRef[];
|
|
28
|
+
gasAmount?: number;
|
|
29
|
+
gasPrice?: Number256;
|
|
30
|
+
}
|
|
31
|
+
export interface SignTransferTxResult {
|
|
32
|
+
fromGroup: number;
|
|
33
|
+
toGroup: number;
|
|
34
|
+
unsignedTx: string;
|
|
35
|
+
txId: string;
|
|
36
|
+
signature: string;
|
|
37
|
+
gasAmount: number;
|
|
38
|
+
gasPrice: Number256;
|
|
39
|
+
}
|
|
40
|
+
export interface SignDeployContractTxParams {
|
|
41
|
+
signerAddress: string;
|
|
42
|
+
signerKeyType?: KeyType;
|
|
43
|
+
bytecode: string;
|
|
44
|
+
initialAttoAlphAmount?: Number256;
|
|
45
|
+
initialTokenAmounts?: Token[];
|
|
46
|
+
issueTokenAmount?: Number256;
|
|
47
|
+
gasAmount?: number;
|
|
48
|
+
gasPrice?: Number256;
|
|
49
|
+
}
|
|
50
|
+
export interface SignDeployContractTxResult {
|
|
51
|
+
groupIndex: number;
|
|
52
|
+
unsignedTx: string;
|
|
53
|
+
txId: string;
|
|
54
|
+
signature: string;
|
|
55
|
+
contractId: string;
|
|
56
|
+
contractAddress: string;
|
|
57
|
+
gasAmount: number;
|
|
58
|
+
gasPrice: Number256;
|
|
59
|
+
}
|
|
60
|
+
export interface SignExecuteScriptTxParams {
|
|
61
|
+
signerAddress: string;
|
|
62
|
+
signerKeyType?: KeyType;
|
|
63
|
+
bytecode: string;
|
|
64
|
+
attoAlphAmount?: Number256;
|
|
65
|
+
tokens?: Token[];
|
|
66
|
+
gasAmount?: number;
|
|
67
|
+
gasPrice?: Number256;
|
|
68
|
+
}
|
|
69
|
+
export interface SignExecuteScriptTxResult {
|
|
70
|
+
groupIndex: number;
|
|
71
|
+
unsignedTx: string;
|
|
72
|
+
txId: string;
|
|
73
|
+
signature: string;
|
|
74
|
+
gasAmount: number;
|
|
75
|
+
gasPrice: Number256;
|
|
76
|
+
}
|
|
77
|
+
export interface SignUnsignedTxParams {
|
|
78
|
+
signerAddress: string;
|
|
79
|
+
signerKeyType?: KeyType;
|
|
80
|
+
unsignedTx: string;
|
|
81
|
+
}
|
|
82
|
+
export interface SignUnsignedTxResult {
|
|
83
|
+
fromGroup: number;
|
|
84
|
+
toGroup: number;
|
|
85
|
+
unsignedTx: string;
|
|
86
|
+
txId: string;
|
|
87
|
+
signature: string;
|
|
88
|
+
gasAmount: number;
|
|
89
|
+
gasPrice: Number256;
|
|
90
|
+
}
|
|
91
|
+
export type MessageHasher = 'alephium' | 'sha256' | 'blake2b' | 'identity';
|
|
92
|
+
export interface SignMessageParams {
|
|
93
|
+
signerAddress: string;
|
|
94
|
+
signerKeyType?: KeyType;
|
|
95
|
+
message: string;
|
|
96
|
+
messageHasher: MessageHasher;
|
|
97
|
+
}
|
|
98
|
+
export interface SignMessageResult {
|
|
99
|
+
signature: string;
|
|
100
|
+
}
|
|
101
|
+
export interface SubmitTransactionParams {
|
|
102
|
+
unsignedTx: string;
|
|
103
|
+
signature: string;
|
|
104
|
+
}
|
|
105
|
+
export interface SubmissionResult {
|
|
106
|
+
txId: string;
|
|
107
|
+
fromGroup: number;
|
|
108
|
+
toGroup: number;
|
|
109
|
+
}
|
|
110
|
+
export interface EnableOptionsBase {
|
|
111
|
+
chainGroup?: number;
|
|
112
|
+
keyType?: KeyType;
|
|
113
|
+
networkId?: string;
|
|
114
|
+
onDisconnected: () => Promise<void> | void;
|
|
115
|
+
}
|
|
@@ -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)();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './nft';
|
|
@@ -0,0 +1,34 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
31
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
__exportStar(require("./nft"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
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 });
|