@dynamic-labs-wallet/tron 0.0.0 → 1.0.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/index.cjs +1155 -0
- package/index.d.ts +1 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +1123 -0
- package/package.json +35 -1
- package/src/client/client.d.ts +149 -0
- package/src/client/client.d.ts.map +1 -0
- package/src/client/constants.d.ts +40 -0
- package/src/client/constants.d.ts.map +1 -0
- package/src/client/index.d.ts +2 -0
- package/src/client/index.d.ts.map +1 -0
- package/src/index.d.ts +5 -0
- package/src/index.d.ts.map +1 -0
- package/src/types.d.ts +39 -0
- package/src/types.d.ts.map +1 -0
- package/src/utils/deriveTronAddress/deriveTronAddress.d.ts +30 -0
- package/src/utils/deriveTronAddress/deriveTronAddress.d.ts.map +1 -0
- package/src/utils/deriveTronAddress/index.d.ts +2 -0
- package/src/utils/deriveTronAddress/index.d.ts.map +1 -0
- package/src/utils/formatTronMessage/formatTronMessage.d.ts +37 -0
- package/src/utils/formatTronMessage/formatTronMessage.d.ts.map +1 -0
- package/src/utils/formatTronMessage/index.d.ts +2 -0
- package/src/utils/formatTronMessage/index.d.ts.map +1 -0
- package/src/utils/getTronTxId/getTronTxId.d.ts +17 -0
- package/src/utils/getTronTxId/getTronTxId.d.ts.map +1 -0
- package/src/utils/getTronTxId/index.d.ts +2 -0
- package/src/utils/getTronTxId/index.d.ts.map +1 -0
- package/src/utils/index.d.ts +5 -0
- package/src/utils/index.d.ts.map +1 -0
- package/src/utils/serializeECDSASignature/index.d.ts +2 -0
- package/src/utils/serializeECDSASignature/index.d.ts.map +1 -0
- package/src/utils/serializeECDSASignature/serializeECDSASignature.d.ts +13 -0
- package/src/utils/serializeECDSASignature/serializeECDSASignature.d.ts.map +1 -0
package/index.cjs
ADDED
|
@@ -0,0 +1,1155 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var browser = require('@dynamic-labs-wallet/browser');
|
|
4
|
+
var sha3 = require('@noble/hashes/sha3');
|
|
5
|
+
var sha256 = require('@noble/hashes/sha256');
|
|
6
|
+
var bs58 = require('bs58');
|
|
7
|
+
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
|
|
8
|
+
|
|
9
|
+
function _interopNamespaceDefault(e) {
|
|
10
|
+
var n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return e[k]; }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
n.default = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var sdkApiCore__namespace = /*#__PURE__*/_interopNamespaceDefault(sdkApiCore);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Mainnet address prefix byte for TRON addresses. Shasta and Nile testnets
|
|
30
|
+
* use the same prefix; the network is disambiguated at the node/relay layer
|
|
31
|
+
* (TRON does not embed a network byte into the address).
|
|
32
|
+
*
|
|
33
|
+
* @see https://github.com/tronprotocol/tips/blob/master/tip-21.md
|
|
34
|
+
*/ var TRON_ADDRESS_PREFIX = 0x41;
|
|
35
|
+
/**
|
|
36
|
+
* Length of the Base58Check checksum appended to a TRON address payload.
|
|
37
|
+
*/ var TRON_CHECKSUM_LENGTH = 4;
|
|
38
|
+
/**
|
|
39
|
+
* Length of the encoded (un-prefixed) Base58Check TRON address.
|
|
40
|
+
* `T...` addresses are always 34 characters.
|
|
41
|
+
*/ var TRON_ADDRESS_LENGTH = 34;
|
|
42
|
+
/**
|
|
43
|
+
* Derive a TRON Base58Check ("T...") address from a secp256k1 public key.
|
|
44
|
+
*
|
|
45
|
+
* TRON addresses are the keccak256 hash of the uncompressed public key
|
|
46
|
+
* (with the leading 0x04 byte stripped), taking the last 20 bytes, prefixed
|
|
47
|
+
* with 0x41, then encoded as Base58Check (SHA256d checksum).
|
|
48
|
+
*
|
|
49
|
+
* Mainnet, Shasta, and Nile share the same address format — the network is
|
|
50
|
+
* resolved by the RPC endpoint, not the address itself.
|
|
51
|
+
*
|
|
52
|
+
* @see https://github.com/tronprotocol/tips/blob/master/tip-21.md
|
|
53
|
+
*/ var hexToBytes$1 = function(hex) {
|
|
54
|
+
var buf = Buffer.from(hex, 'hex');
|
|
55
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
56
|
+
};
|
|
57
|
+
var deriveTronAddress = function(param) {
|
|
58
|
+
var publicKeyHex = param.publicKeyHex;
|
|
59
|
+
var cleanHex = publicKeyHex.startsWith('0x') ? publicKeyHex.slice(2) : publicKeyHex;
|
|
60
|
+
var publicKeyBytes = hexToBytes$1(cleanHex);
|
|
61
|
+
// Allow either 65-byte uncompressed (0x04 || X || Y) or 64-byte X||Y form.
|
|
62
|
+
var xy;
|
|
63
|
+
if (publicKeyBytes.length === 65) {
|
|
64
|
+
if (publicKeyBytes[0] !== 0x04) {
|
|
65
|
+
throw new Error("Invalid uncompressed public key prefix: expected 0x04, got 0x".concat(publicKeyBytes[0].toString(16)));
|
|
66
|
+
}
|
|
67
|
+
xy = publicKeyBytes.subarray(1);
|
|
68
|
+
} else if (publicKeyBytes.length === 64) {
|
|
69
|
+
xy = publicKeyBytes;
|
|
70
|
+
} else {
|
|
71
|
+
throw new Error("Invalid public key length: ".concat(publicKeyBytes.length, ", expected 64 (X||Y) or 65 (uncompressed)"));
|
|
72
|
+
}
|
|
73
|
+
var hashed = sha3.keccak_256(xy);
|
|
74
|
+
var lastTwentyBytes = hashed.slice(-20);
|
|
75
|
+
var payload = new Uint8Array(21);
|
|
76
|
+
payload[0] = TRON_ADDRESS_PREFIX;
|
|
77
|
+
payload.set(lastTwentyBytes, 1);
|
|
78
|
+
var checksum = sha256.sha256(sha256.sha256(payload)).slice(0, TRON_CHECKSUM_LENGTH);
|
|
79
|
+
var addressBytes = new Uint8Array(payload.length + TRON_CHECKSUM_LENGTH);
|
|
80
|
+
addressBytes.set(payload, 0);
|
|
81
|
+
addressBytes.set(checksum, payload.length);
|
|
82
|
+
return bs58.encode(addressBytes);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Validate a TRON Base58Check address.
|
|
86
|
+
*
|
|
87
|
+
* Performs the full decode + checksum + prefix check, so it rejects EVM
|
|
88
|
+
* `0x...` addresses, malformed Base58, and addresses whose checksum was
|
|
89
|
+
* tampered with.
|
|
90
|
+
*/ var isValidTronAddress = function(address) {
|
|
91
|
+
if (typeof address !== 'string' || address.length !== TRON_ADDRESS_LENGTH) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
var decoded;
|
|
95
|
+
try {
|
|
96
|
+
decoded = bs58.decode(address);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (decoded.length !== 25 || decoded[0] !== TRON_ADDRESS_PREFIX) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
var payload = decoded.subarray(0, 21);
|
|
104
|
+
var expectedChecksum = decoded.subarray(21);
|
|
105
|
+
var actualChecksum = sha256.sha256(sha256.sha256(payload)).slice(0, TRON_CHECKSUM_LENGTH);
|
|
106
|
+
for(var i = 0; i < TRON_CHECKSUM_LENGTH; i++){
|
|
107
|
+
if (expectedChecksum[i] !== actualChecksum[i]) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Decode a TRON Base58Check address to its 21-byte payload (prefix + 20-byte
|
|
115
|
+
* hash). Throws if the checksum doesn't validate.
|
|
116
|
+
*/ var decodeTronAddress = function(address) {
|
|
117
|
+
if (!isValidTronAddress(address)) {
|
|
118
|
+
throw new Error('Invalid TRON address');
|
|
119
|
+
}
|
|
120
|
+
return bs58.decode(address).subarray(0, 21);
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Convert a TRON Base58Check address to its 0x41-prefixed hex form (used in
|
|
124
|
+
* raw transactions).
|
|
125
|
+
*/ var tronAddressToHex = function(address) {
|
|
126
|
+
var payload = decodeTronAddress(address);
|
|
127
|
+
return "0x".concat(Buffer.from(payload).toString('hex'));
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
function _instanceof$2(left, right) {
|
|
131
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
132
|
+
return !!right[Symbol.hasInstance](left);
|
|
133
|
+
} else {
|
|
134
|
+
return left instanceof right;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* TIP-191 personal-sign prefix used by TRON for signed messages.
|
|
139
|
+
*
|
|
140
|
+
* The hash that gets signed is:
|
|
141
|
+
* keccak256("\x19TRON Signed Message:\n" || len(message) || message)
|
|
142
|
+
*
|
|
143
|
+
* where `message` is the raw bytes the user agreed to sign.
|
|
144
|
+
*
|
|
145
|
+
* @see https://github.com/tronprotocol/tips/blob/master/tip-191.md
|
|
146
|
+
*/ var TRON_SIGN_MESSAGE_PREFIX = '\x19TRON Signed Message:\n';
|
|
147
|
+
var bufferToUint8Array = function(buf) {
|
|
148
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
149
|
+
};
|
|
150
|
+
var stringToBytes = function(s) {
|
|
151
|
+
return bufferToUint8Array(Buffer.from(s, 'utf8'));
|
|
152
|
+
};
|
|
153
|
+
var hexToBytes = function(hex) {
|
|
154
|
+
var clean = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
155
|
+
return bufferToUint8Array(Buffer.from(clean, 'hex'));
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Convert a user-supplied message into the bytes that TIP-191 expects to be
|
|
159
|
+
* hashed. Mirrors the shape of `formatTempoMessage` so the wallet client can
|
|
160
|
+
* treat both chains uniformly.
|
|
161
|
+
*
|
|
162
|
+
* - `string` starting with `0x` → treated as hex bytes
|
|
163
|
+
* - `string` otherwise → treated as utf-8 bytes
|
|
164
|
+
* - `Uint8Array` → passed through
|
|
165
|
+
* - `{ raw: ... }` → mirrors viem's `{ raw }` escape hatch for already-hashed payloads
|
|
166
|
+
*/ var toTronMessageBytes = function(message) {
|
|
167
|
+
if (_instanceof$2(message, Uint8Array)) return message;
|
|
168
|
+
if (typeof message === 'string') {
|
|
169
|
+
return message.startsWith('0x') ? hexToBytes(message) : stringToBytes(message);
|
|
170
|
+
}
|
|
171
|
+
if (typeof message.raw === 'string') return hexToBytes(message.raw);
|
|
172
|
+
return message.raw;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Apply the TIP-191 prefix and return the keccak256 hash that should be
|
|
176
|
+
* signed by the MPC ECDSA flow.
|
|
177
|
+
*
|
|
178
|
+
* Returns a `Uint8Array` (32 bytes). The wallet client converts this into a
|
|
179
|
+
* `MessageHash` (`isFormatted: true`) before handing it to `sign(...)` so
|
|
180
|
+
* the forward-MPC path sees a pre-hashed payload.
|
|
181
|
+
*/ var formatTronMessage = function(message, logger) {
|
|
182
|
+
try {
|
|
183
|
+
var bytes = toTronMessageBytes(message);
|
|
184
|
+
var prefixBytes = stringToBytes("".concat(TRON_SIGN_MESSAGE_PREFIX).concat(bytes.length));
|
|
185
|
+
var combined = new Uint8Array(prefixBytes.length + bytes.length);
|
|
186
|
+
combined.set(prefixBytes, 0);
|
|
187
|
+
combined.set(bytes, prefixBytes.length);
|
|
188
|
+
return sha3.keccak_256(combined);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
logger.error('[DynamicTronWalletClient]: Error formatting TRON message:', error);
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* TRON transactions are signed over `sha256(raw_data_hex_bytes)`, where
|
|
197
|
+
* `raw_data_hex` is the protobuf-encoded `Transaction.raw` produced by the
|
|
198
|
+
* TRON full node when the unsigned transaction is created.
|
|
199
|
+
*
|
|
200
|
+
* The MPC client never reconstructs the protobuf itself — the caller passes
|
|
201
|
+
* the already-serialized `raw_data_hex` and we hash it deterministically here.
|
|
202
|
+
*
|
|
203
|
+
* @see https://developers.tron.network/docs/transactions
|
|
204
|
+
*/ var getTronTxId = function(rawDataHex) {
|
|
205
|
+
var clean = rawDataHex.startsWith('0x') ? rawDataHex.slice(2) : rawDataHex;
|
|
206
|
+
if (clean.length === 0 || clean.length % 2 !== 0 || /[^0-9a-fA-F]/.test(clean)) {
|
|
207
|
+
throw new Error('Invalid raw_data_hex: must be a non-empty hex string');
|
|
208
|
+
}
|
|
209
|
+
var buf = Buffer.from(clean, 'hex');
|
|
210
|
+
return sha256.sha256(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Convenience wrapper that returns the txID as a 0x-prefixed hex string
|
|
214
|
+
* (matching the form returned by the TRON full node `/wallet/gettransactioninfobyid`).
|
|
215
|
+
*/ var getTronTxIdHex = function(rawDataHex) {
|
|
216
|
+
var hash = getTronTxId(rawDataHex);
|
|
217
|
+
return "0x".concat(Buffer.from(hash).toString('hex'));
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
function _instanceof$1(left, right) {
|
|
221
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
222
|
+
return !!right[Symbol.hasInstance](left);
|
|
223
|
+
} else {
|
|
224
|
+
return left instanceof right;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Serialize an MPC-produced ECDSA signature into the 65-byte
|
|
229
|
+
* `r || s || v` hex form that TRON full nodes expect on
|
|
230
|
+
* `Transaction.signature[0]` and TIP-191 signed messages.
|
|
231
|
+
*
|
|
232
|
+
* TRON canonicalises v to 0 / 1 on the wire (recovery id), while the MPC
|
|
233
|
+
* relay returns v as 27 / 28 (Ethereum legacy convention). We strip the +27
|
|
234
|
+
* offset here so the output is directly usable by TRON tooling without
|
|
235
|
+
* the consumer having to remember the convention.
|
|
236
|
+
*/ var serializeECDSASignature = function(signature, logger) {
|
|
237
|
+
try {
|
|
238
|
+
var r = Buffer.from(signature.r).toString('hex');
|
|
239
|
+
var s = Buffer.from(signature.s).toString('hex');
|
|
240
|
+
if (r.length !== 64 || s.length !== 64) {
|
|
241
|
+
throw new Error("Invalid ECDSA r/s length: r=".concat(r.length, ", s=").concat(s.length, " (expected 64 hex chars each)"));
|
|
242
|
+
}
|
|
243
|
+
var vNum = Number(signature.v);
|
|
244
|
+
if (!Number.isFinite(vNum)) {
|
|
245
|
+
throw new TypeError("Invalid ECDSA v: ".concat(signature.v));
|
|
246
|
+
}
|
|
247
|
+
var recoveryId = vNum >= 27 ? vNum - 27 : vNum;
|
|
248
|
+
if (recoveryId !== 0 && recoveryId !== 1) {
|
|
249
|
+
throw new Error("Invalid ECDSA recovery id derived from v=".concat(vNum, ": ").concat(recoveryId));
|
|
250
|
+
}
|
|
251
|
+
var vHex = recoveryId.toString(16).padStart(2, '0');
|
|
252
|
+
return "0x".concat(r).concat(s).concat(vHex);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
logger.error('[DynamicTronWalletClient]: Error serializing ECDSA signature:', _instanceof$1(error, Error) ? error.message : String(error));
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
var ERROR_KEYGEN_FAILED = 'Error with keygen';
|
|
260
|
+
var ERROR_CREATE_WALLET_ACCOUNT = 'Error creating TRON wallet account';
|
|
261
|
+
var ERROR_IMPORT_PRIVATE_KEY = 'Error importing private key';
|
|
262
|
+
var ERROR_EXPORT_PRIVATE_KEY = 'Error exporting private key';
|
|
263
|
+
var ERROR_SIGN_MESSAGE = 'Error signing message';
|
|
264
|
+
var ERROR_SIGN_TRANSACTION = 'Error signing transaction';
|
|
265
|
+
var ERROR_ACCOUNT_ADDRESS_REQUIRED = 'Account address is required';
|
|
266
|
+
var ERROR_RAW_DATA_HEX_REQUIRED = 'Transaction raw_data_hex is required';
|
|
267
|
+
var ERROR_INVALID_RAW_PUBLIC_KEY = 'Invalid raw public key instance';
|
|
268
|
+
|
|
269
|
+
function _array_like_to_array(arr, len) {
|
|
270
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
271
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
272
|
+
return arr2;
|
|
273
|
+
}
|
|
274
|
+
function _array_without_holes(arr) {
|
|
275
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
276
|
+
}
|
|
277
|
+
function _assert_this_initialized(self) {
|
|
278
|
+
if (self === void 0) {
|
|
279
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
280
|
+
}
|
|
281
|
+
return self;
|
|
282
|
+
}
|
|
283
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
284
|
+
try {
|
|
285
|
+
var info = gen[key](arg);
|
|
286
|
+
var value = info.value;
|
|
287
|
+
} catch (error) {
|
|
288
|
+
reject(error);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (info.done) {
|
|
292
|
+
resolve(value);
|
|
293
|
+
} else {
|
|
294
|
+
Promise.resolve(value).then(_next, _throw);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function _async_to_generator(fn) {
|
|
298
|
+
return function() {
|
|
299
|
+
var self = this, args = arguments;
|
|
300
|
+
return new Promise(function(resolve, reject) {
|
|
301
|
+
var gen = fn.apply(self, args);
|
|
302
|
+
function _next(value) {
|
|
303
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
304
|
+
}
|
|
305
|
+
function _throw(err) {
|
|
306
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
307
|
+
}
|
|
308
|
+
_next(undefined);
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function _call_super(_this, derived, args) {
|
|
313
|
+
derived = _get_prototype_of(derived);
|
|
314
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
315
|
+
}
|
|
316
|
+
function _class_call_check(instance, Constructor) {
|
|
317
|
+
if (!(instance instanceof Constructor)) {
|
|
318
|
+
throw new TypeError("Cannot call a class as a function");
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function _defineProperties(target, props) {
|
|
322
|
+
for(var i = 0; i < props.length; i++){
|
|
323
|
+
var descriptor = props[i];
|
|
324
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
325
|
+
descriptor.configurable = true;
|
|
326
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
327
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
331
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
332
|
+
return Constructor;
|
|
333
|
+
}
|
|
334
|
+
function _define_property(obj, key, value) {
|
|
335
|
+
if (key in obj) {
|
|
336
|
+
Object.defineProperty(obj, key, {
|
|
337
|
+
value: value,
|
|
338
|
+
enumerable: true,
|
|
339
|
+
configurable: true,
|
|
340
|
+
writable: true
|
|
341
|
+
});
|
|
342
|
+
} else {
|
|
343
|
+
obj[key] = value;
|
|
344
|
+
}
|
|
345
|
+
return obj;
|
|
346
|
+
}
|
|
347
|
+
function _get_prototype_of(o) {
|
|
348
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
349
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
350
|
+
};
|
|
351
|
+
return _get_prototype_of(o);
|
|
352
|
+
}
|
|
353
|
+
function _inherits(subClass, superClass) {
|
|
354
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
355
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
356
|
+
}
|
|
357
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
358
|
+
constructor: {
|
|
359
|
+
value: subClass,
|
|
360
|
+
writable: true,
|
|
361
|
+
configurable: true
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
365
|
+
}
|
|
366
|
+
function _instanceof(left, right) {
|
|
367
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
368
|
+
return !!right[Symbol.hasInstance](left);
|
|
369
|
+
} else {
|
|
370
|
+
return left instanceof right;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function _iterable_to_array(iter) {
|
|
374
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
375
|
+
}
|
|
376
|
+
function _non_iterable_spread() {
|
|
377
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
378
|
+
}
|
|
379
|
+
function _object_spread(target) {
|
|
380
|
+
for(var i = 1; i < arguments.length; i++){
|
|
381
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
382
|
+
var ownKeys = Object.keys(source);
|
|
383
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
384
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
385
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
386
|
+
}));
|
|
387
|
+
}
|
|
388
|
+
ownKeys.forEach(function(key) {
|
|
389
|
+
_define_property(target, key, source[key]);
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
return target;
|
|
393
|
+
}
|
|
394
|
+
function ownKeys(object, enumerableOnly) {
|
|
395
|
+
var keys = Object.keys(object);
|
|
396
|
+
if (Object.getOwnPropertySymbols) {
|
|
397
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
398
|
+
keys.push.apply(keys, symbols);
|
|
399
|
+
}
|
|
400
|
+
return keys;
|
|
401
|
+
}
|
|
402
|
+
function _object_spread_props(target, source) {
|
|
403
|
+
source = source != null ? source : {};
|
|
404
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
405
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
406
|
+
} else {
|
|
407
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
408
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
return target;
|
|
412
|
+
}
|
|
413
|
+
function _possible_constructor_return(self, call) {
|
|
414
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) {
|
|
415
|
+
return call;
|
|
416
|
+
}
|
|
417
|
+
return _assert_this_initialized(self);
|
|
418
|
+
}
|
|
419
|
+
function _set_prototype_of(o, p) {
|
|
420
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
421
|
+
o.__proto__ = p;
|
|
422
|
+
return o;
|
|
423
|
+
};
|
|
424
|
+
return _set_prototype_of(o, p);
|
|
425
|
+
}
|
|
426
|
+
function _to_consumable_array(arr) {
|
|
427
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
428
|
+
}
|
|
429
|
+
function _type_of(obj) {
|
|
430
|
+
"@swc/helpers - typeof";
|
|
431
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
432
|
+
}
|
|
433
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
434
|
+
if (!o) return;
|
|
435
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
436
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
437
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
438
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
439
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
440
|
+
}
|
|
441
|
+
function _is_native_reflect_construct() {
|
|
442
|
+
try {
|
|
443
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
444
|
+
} catch (_) {}
|
|
445
|
+
return (_is_native_reflect_construct = function() {
|
|
446
|
+
return !!result;
|
|
447
|
+
})();
|
|
448
|
+
}
|
|
449
|
+
function _ts_generator(thisArg, body) {
|
|
450
|
+
var f, y, t, g, _ = {
|
|
451
|
+
label: 0,
|
|
452
|
+
sent: function() {
|
|
453
|
+
if (t[0] & 1) throw t[1];
|
|
454
|
+
return t[1];
|
|
455
|
+
},
|
|
456
|
+
trys: [],
|
|
457
|
+
ops: []
|
|
458
|
+
};
|
|
459
|
+
return g = {
|
|
460
|
+
next: verb(0),
|
|
461
|
+
"throw": verb(1),
|
|
462
|
+
"return": verb(2)
|
|
463
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
464
|
+
return this;
|
|
465
|
+
}), g;
|
|
466
|
+
function verb(n) {
|
|
467
|
+
return function(v) {
|
|
468
|
+
return step([
|
|
469
|
+
n,
|
|
470
|
+
v
|
|
471
|
+
]);
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
function step(op) {
|
|
475
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
476
|
+
while(_)try {
|
|
477
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
478
|
+
if (y = 0, t) op = [
|
|
479
|
+
op[0] & 2,
|
|
480
|
+
t.value
|
|
481
|
+
];
|
|
482
|
+
switch(op[0]){
|
|
483
|
+
case 0:
|
|
484
|
+
case 1:
|
|
485
|
+
t = op;
|
|
486
|
+
break;
|
|
487
|
+
case 4:
|
|
488
|
+
_.label++;
|
|
489
|
+
return {
|
|
490
|
+
value: op[1],
|
|
491
|
+
done: false
|
|
492
|
+
};
|
|
493
|
+
case 5:
|
|
494
|
+
_.label++;
|
|
495
|
+
y = op[1];
|
|
496
|
+
op = [
|
|
497
|
+
0
|
|
498
|
+
];
|
|
499
|
+
continue;
|
|
500
|
+
case 7:
|
|
501
|
+
op = _.ops.pop();
|
|
502
|
+
_.trys.pop();
|
|
503
|
+
continue;
|
|
504
|
+
default:
|
|
505
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
506
|
+
_ = 0;
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
510
|
+
_.label = op[1];
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
514
|
+
_.label = t[1];
|
|
515
|
+
t = op;
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
if (t && _.label < t[2]) {
|
|
519
|
+
_.label = t[2];
|
|
520
|
+
_.ops.push(op);
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
if (t[2]) _.ops.pop();
|
|
524
|
+
_.trys.pop();
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
op = body.call(thisArg, _);
|
|
528
|
+
} catch (e) {
|
|
529
|
+
op = [
|
|
530
|
+
6,
|
|
531
|
+
e
|
|
532
|
+
];
|
|
533
|
+
y = 0;
|
|
534
|
+
} finally{
|
|
535
|
+
f = t = 0;
|
|
536
|
+
}
|
|
537
|
+
if (op[0] & 5) throw op[1];
|
|
538
|
+
return {
|
|
539
|
+
value: op[0] ? op[1] : void 0,
|
|
540
|
+
done: true
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Wallet client for TRON (SLIP-44 coin type 195, secp256k1 / ECDSA).
|
|
546
|
+
*
|
|
547
|
+
* V1 surface (matches the locked Wave C-1 handoff):
|
|
548
|
+
* - create / signMessageV2 / signTransaction / importPrivateKey / exportPrivateKey
|
|
549
|
+
* - No legacy V1 message signing (`signMessage` returns a TIP-191 result;
|
|
550
|
+
* consumers should call `signMessageV2`).
|
|
551
|
+
* - Networks (Mainnet / Shasta / Nile) share the same address format and
|
|
552
|
+
* keyshare namespace; the network is selected at the broadcast layer
|
|
553
|
+
* via the raw_data the caller assembles against the full node of their
|
|
554
|
+
* choice. See `TRON_NETWORKS` in `./constants`.
|
|
555
|
+
*
|
|
556
|
+
* Address derivation: keccak256 of the uncompressed secp256k1 public key
|
|
557
|
+
* (minus the 0x04 prefix), last 20 bytes, prefixed with 0x41, encoded as
|
|
558
|
+
* Base58Check (`T...`).
|
|
559
|
+
*
|
|
560
|
+
* Transaction signing: SHA-256 of `raw_data_hex` is the digest, sent through
|
|
561
|
+
* the forward-MPC path as a pre-hashed (`isFormatted: true`) payload.
|
|
562
|
+
*
|
|
563
|
+
* Message signing (TIP-191): keccak256 of `\x19TRON Signed Message:\n<len><msg>`.
|
|
564
|
+
*/ var DynamicTronWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
565
|
+
_inherits(DynamicTronWalletClient, DynamicWalletClient);
|
|
566
|
+
function DynamicTronWalletClient(param, internalOptions) {
|
|
567
|
+
var environmentId = param.environmentId, authToken = param.authToken, backupServiceAuthToken = param.backupServiceAuthToken, baseApiUrl = param.baseApiUrl, baseMPCRelayApiUrl = param.baseMPCRelayApiUrl, storageKey = param.storageKey, debug = param.debug, featureFlags = param.featureFlags, _param_authMode = param.authMode, authMode = _param_authMode === void 0 ? browser.AuthMode.HEADER : _param_authMode, sdkVersion = param.sdkVersion, forwardMPCClient = param.forwardMPCClient, logger = param.logger;
|
|
568
|
+
_class_call_check(this, DynamicTronWalletClient);
|
|
569
|
+
var _this;
|
|
570
|
+
_this = _call_super(this, DynamicTronWalletClient, [
|
|
571
|
+
{
|
|
572
|
+
environmentId: environmentId,
|
|
573
|
+
authToken: authToken,
|
|
574
|
+
backupServiceAuthToken: backupServiceAuthToken,
|
|
575
|
+
baseApiUrl: baseApiUrl,
|
|
576
|
+
baseMPCRelayApiUrl: baseMPCRelayApiUrl,
|
|
577
|
+
storageKey: storageKey,
|
|
578
|
+
debug: debug,
|
|
579
|
+
featureFlags: featureFlags,
|
|
580
|
+
authMode: authMode,
|
|
581
|
+
sdkVersion: sdkVersion,
|
|
582
|
+
forwardMPCClient: forwardMPCClient,
|
|
583
|
+
logger: logger
|
|
584
|
+
},
|
|
585
|
+
internalOptions
|
|
586
|
+
]), _define_property(_this, "chainName", 'TRON');
|
|
587
|
+
return _this;
|
|
588
|
+
}
|
|
589
|
+
_create_class(DynamicTronWalletClient, [
|
|
590
|
+
{
|
|
591
|
+
key: "createWalletAccount",
|
|
592
|
+
value: /**
|
|
593
|
+
* Create a new TRON wallet account using MPC key generation.
|
|
594
|
+
*/ function createWalletAccount(param) {
|
|
595
|
+
var thresholdSignatureScheme = param.thresholdSignatureScheme, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, onError = param.onError, signedSessionId = param.signedSessionId; param.traceContext;
|
|
596
|
+
var _this = this;
|
|
597
|
+
return _async_to_generator(function() {
|
|
598
|
+
var ceremonyCompleteResolver, ceremonyCompletePromise, _ref, rawPublicKey, clientKeyShares, _this_deriveAccountAddress, accountAddress, publicKeyHex, error;
|
|
599
|
+
return _ts_generator(this, function(_state) {
|
|
600
|
+
switch(_state.label){
|
|
601
|
+
case 0:
|
|
602
|
+
_state.trys.push([
|
|
603
|
+
0,
|
|
604
|
+
5,
|
|
605
|
+
,
|
|
606
|
+
6
|
|
607
|
+
]);
|
|
608
|
+
ceremonyCompletePromise = new Promise(function(resolve) {
|
|
609
|
+
ceremonyCompleteResolver = resolve;
|
|
610
|
+
});
|
|
611
|
+
return [
|
|
612
|
+
4,
|
|
613
|
+
_this.keyGen({
|
|
614
|
+
chainName: _this.chainName,
|
|
615
|
+
thresholdSignatureScheme: thresholdSignatureScheme,
|
|
616
|
+
onError: function(error) {
|
|
617
|
+
_this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
618
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
619
|
+
},
|
|
620
|
+
onCeremonyComplete: function(accountAddress, walletId) {
|
|
621
|
+
var chainConfig = browser.getMPCChainConfig(_this.chainName);
|
|
622
|
+
_this.initializeWalletMapEntry({
|
|
623
|
+
accountAddress: accountAddress,
|
|
624
|
+
walletId: walletId,
|
|
625
|
+
chainName: _this.chainName,
|
|
626
|
+
thresholdSignatureScheme: thresholdSignatureScheme,
|
|
627
|
+
derivationPath: JSON.stringify(Object.fromEntries(chainConfig.derivationPath.map(function(value, index) {
|
|
628
|
+
return [
|
|
629
|
+
index,
|
|
630
|
+
value
|
|
631
|
+
];
|
|
632
|
+
})))
|
|
633
|
+
});
|
|
634
|
+
ceremonyCompleteResolver(undefined);
|
|
635
|
+
},
|
|
636
|
+
password: password,
|
|
637
|
+
signedSessionId: signedSessionId
|
|
638
|
+
})
|
|
639
|
+
];
|
|
640
|
+
case 1:
|
|
641
|
+
_ref = _state.sent(), rawPublicKey = _ref.rawPublicKey, clientKeyShares = _ref.clientKeyShares;
|
|
642
|
+
return [
|
|
643
|
+
4,
|
|
644
|
+
ceremonyCompletePromise
|
|
645
|
+
];
|
|
646
|
+
case 2:
|
|
647
|
+
_state.sent();
|
|
648
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
649
|
+
throw new Error(ERROR_KEYGEN_FAILED);
|
|
650
|
+
}
|
|
651
|
+
_this.checkRawPublicKeyInstance(rawPublicKey);
|
|
652
|
+
_this_deriveAccountAddress = _this.deriveAccountAddress({
|
|
653
|
+
rawPublicKey: rawPublicKey
|
|
654
|
+
}), accountAddress = _this_deriveAccountAddress.accountAddress, publicKeyHex = _this_deriveAccountAddress.publicKeyHex;
|
|
655
|
+
return [
|
|
656
|
+
4,
|
|
657
|
+
_this.setClientKeySharesToStorage({
|
|
658
|
+
accountAddress: accountAddress,
|
|
659
|
+
clientKeyShares: clientKeyShares
|
|
660
|
+
})
|
|
661
|
+
];
|
|
662
|
+
case 3:
|
|
663
|
+
_state.sent();
|
|
664
|
+
return [
|
|
665
|
+
4,
|
|
666
|
+
_this.storeEncryptedBackupByWallet({
|
|
667
|
+
accountAddress: accountAddress,
|
|
668
|
+
clientKeyShares: clientKeyShares,
|
|
669
|
+
password: password,
|
|
670
|
+
signedSessionId: signedSessionId
|
|
671
|
+
})
|
|
672
|
+
];
|
|
673
|
+
case 4:
|
|
674
|
+
_state.sent();
|
|
675
|
+
return [
|
|
676
|
+
2,
|
|
677
|
+
{
|
|
678
|
+
accountAddress: accountAddress,
|
|
679
|
+
rawPublicKey: rawPublicKey,
|
|
680
|
+
publicKeyHex: publicKeyHex
|
|
681
|
+
}
|
|
682
|
+
];
|
|
683
|
+
case 5:
|
|
684
|
+
error = _state.sent();
|
|
685
|
+
if (_instanceof(error, Error) && error.message === browser.ERROR_PASSWORD_MISMATCH) {
|
|
686
|
+
throw error;
|
|
687
|
+
}
|
|
688
|
+
_this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
689
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
|
|
690
|
+
case 6:
|
|
691
|
+
return [
|
|
692
|
+
2
|
|
693
|
+
];
|
|
694
|
+
}
|
|
695
|
+
});
|
|
696
|
+
})();
|
|
697
|
+
}
|
|
698
|
+
},
|
|
699
|
+
{
|
|
700
|
+
key: "signMessage",
|
|
701
|
+
value: /**
|
|
702
|
+
* TIP-191 personal-sign for TRON. This is the `signMessageV2` surface
|
|
703
|
+
* referenced in the rollout — there is no V1 legacy message-sign path.
|
|
704
|
+
*
|
|
705
|
+
* The TIP-191 prefix + keccak256 hashing happens client-side; the resulting
|
|
706
|
+
* 32-byte digest is sent to the forward-MPC relay as `isFormatted: true`
|
|
707
|
+
* (matches the EVM/TEMPO forward-MPC contract).
|
|
708
|
+
*/ function signMessage(param) {
|
|
709
|
+
var message = param.message, accountAddress = param.accountAddress, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken, elevatedAccessToken = param.elevatedAccessToken, context = param.context, onError = param.onError, traceContext = param.traceContext;
|
|
710
|
+
var _this = this;
|
|
711
|
+
return _async_to_generator(function() {
|
|
712
|
+
var digest, signatureEcdsa, error;
|
|
713
|
+
return _ts_generator(this, function(_state) {
|
|
714
|
+
switch(_state.label){
|
|
715
|
+
case 0:
|
|
716
|
+
if (!accountAddress) {
|
|
717
|
+
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
718
|
+
}
|
|
719
|
+
return [
|
|
720
|
+
4,
|
|
721
|
+
_this.verifyPassword({
|
|
722
|
+
accountAddress: accountAddress,
|
|
723
|
+
password: password,
|
|
724
|
+
signedSessionId: signedSessionId
|
|
725
|
+
})
|
|
726
|
+
];
|
|
727
|
+
case 1:
|
|
728
|
+
_state.sent();
|
|
729
|
+
_state.label = 2;
|
|
730
|
+
case 2:
|
|
731
|
+
_state.trys.push([
|
|
732
|
+
2,
|
|
733
|
+
4,
|
|
734
|
+
,
|
|
735
|
+
5
|
|
736
|
+
]);
|
|
737
|
+
digest = formatTronMessage(message, _this.logger);
|
|
738
|
+
return [
|
|
739
|
+
4,
|
|
740
|
+
_this.sign({
|
|
741
|
+
message: digest,
|
|
742
|
+
accountAddress: accountAddress,
|
|
743
|
+
chainName: _this.chainName,
|
|
744
|
+
password: password,
|
|
745
|
+
signedSessionId: signedSessionId,
|
|
746
|
+
isFormatted: true,
|
|
747
|
+
mfaToken: mfaToken,
|
|
748
|
+
elevatedAccessToken: elevatedAccessToken,
|
|
749
|
+
context: context,
|
|
750
|
+
onError: onError,
|
|
751
|
+
traceContext: traceContext
|
|
752
|
+
})
|
|
753
|
+
];
|
|
754
|
+
case 3:
|
|
755
|
+
signatureEcdsa = _state.sent();
|
|
756
|
+
return [
|
|
757
|
+
2,
|
|
758
|
+
serializeECDSASignature(signatureEcdsa, _this.logger)
|
|
759
|
+
];
|
|
760
|
+
case 4:
|
|
761
|
+
error = _state.sent();
|
|
762
|
+
_this.logger.error(ERROR_SIGN_MESSAGE, error);
|
|
763
|
+
throw new Error(ERROR_SIGN_MESSAGE);
|
|
764
|
+
case 5:
|
|
765
|
+
return [
|
|
766
|
+
2
|
|
767
|
+
];
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
})();
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
{
|
|
774
|
+
key: "signTransaction",
|
|
775
|
+
value: /**
|
|
776
|
+
* Sign a TRON transaction.
|
|
777
|
+
*
|
|
778
|
+
* The caller supplies the unsigned `raw_data_hex` produced by a TRON full
|
|
779
|
+
* node (e.g. `/wallet/createtransaction` or `/wallet/triggersmartcontract`),
|
|
780
|
+
* either as a parsed `TronTransaction` object or as the JSON string that
|
|
781
|
+
* came back from the full node. The MPC signer hashes those bytes with
|
|
782
|
+
* SHA-256 (the TRON `txID`) and signs the digest.
|
|
783
|
+
*
|
|
784
|
+
* Returns the entire signed transaction (with the new signature appended to
|
|
785
|
+
* `transaction.signature[]`) as a JSON string, ready to be passed to the
|
|
786
|
+
* full-node `/wallet/broadcasttransaction` endpoint. Returning a string
|
|
787
|
+
* (rather than the object) matches the SDK's iframe `signTransaction`
|
|
788
|
+
* contract, which only carries strings over message transport.
|
|
789
|
+
*/ function signTransaction(param) {
|
|
790
|
+
var senderAddress = param.senderAddress, transaction = param.transaction, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken, elevatedAccessToken = param.elevatedAccessToken, context = param.context, onError = param.onError, traceContext = param.traceContext;
|
|
791
|
+
var _this = this;
|
|
792
|
+
return _async_to_generator(function() {
|
|
793
|
+
var rawParsed, parsedTransaction, digestBytes, txid, resolvedContext, signatureEcdsa, serializedSig, hexSig, _parsedTransaction_signature, existingSignatures, _parsedTransaction_txID, signed, error;
|
|
794
|
+
return _ts_generator(this, function(_state) {
|
|
795
|
+
switch(_state.label){
|
|
796
|
+
case 0:
|
|
797
|
+
if (!senderAddress) {
|
|
798
|
+
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
799
|
+
}
|
|
800
|
+
rawParsed = transaction;
|
|
801
|
+
if (typeof transaction === 'string') {
|
|
802
|
+
try {
|
|
803
|
+
rawParsed = JSON.parse(transaction);
|
|
804
|
+
} catch (e) {
|
|
805
|
+
throw new Error(ERROR_RAW_DATA_HEX_REQUIRED);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
if (!rawParsed || (typeof rawParsed === "undefined" ? "undefined" : _type_of(rawParsed)) !== 'object' || typeof rawParsed.raw_data_hex !== 'string' || rawParsed.raw_data_hex.length === 0) {
|
|
809
|
+
throw new Error(ERROR_RAW_DATA_HEX_REQUIRED);
|
|
810
|
+
}
|
|
811
|
+
parsedTransaction = rawParsed;
|
|
812
|
+
return [
|
|
813
|
+
4,
|
|
814
|
+
_this.verifyPassword({
|
|
815
|
+
accountAddress: senderAddress,
|
|
816
|
+
password: password,
|
|
817
|
+
signedSessionId: signedSessionId
|
|
818
|
+
})
|
|
819
|
+
];
|
|
820
|
+
case 1:
|
|
821
|
+
_state.sent();
|
|
822
|
+
_state.label = 2;
|
|
823
|
+
case 2:
|
|
824
|
+
_state.trys.push([
|
|
825
|
+
2,
|
|
826
|
+
4,
|
|
827
|
+
,
|
|
828
|
+
5
|
|
829
|
+
]);
|
|
830
|
+
digestBytes = getTronTxId(parsedTransaction.raw_data_hex);
|
|
831
|
+
txid = getTronTxIdHex(parsedTransaction.raw_data_hex);
|
|
832
|
+
resolvedContext = _object_spread_props(_object_spread({}, context), {
|
|
833
|
+
tronTransaction: {
|
|
834
|
+
rawDataHex: parsedTransaction.raw_data_hex,
|
|
835
|
+
txid: txid
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
return [
|
|
839
|
+
4,
|
|
840
|
+
_this.sign({
|
|
841
|
+
message: digestBytes,
|
|
842
|
+
accountAddress: senderAddress,
|
|
843
|
+
chainName: _this.chainName,
|
|
844
|
+
password: password,
|
|
845
|
+
signedSessionId: signedSessionId,
|
|
846
|
+
isFormatted: true,
|
|
847
|
+
mfaToken: mfaToken,
|
|
848
|
+
elevatedAccessToken: elevatedAccessToken,
|
|
849
|
+
context: resolvedContext,
|
|
850
|
+
onError: onError,
|
|
851
|
+
traceContext: traceContext
|
|
852
|
+
})
|
|
853
|
+
];
|
|
854
|
+
case 3:
|
|
855
|
+
signatureEcdsa = _state.sent();
|
|
856
|
+
serializedSig = serializeECDSASignature(signatureEcdsa, _this.logger);
|
|
857
|
+
hexSig = serializedSig.slice(2);
|
|
858
|
+
existingSignatures = (_parsedTransaction_signature = parsedTransaction.signature) !== null && _parsedTransaction_signature !== void 0 ? _parsedTransaction_signature : [];
|
|
859
|
+
signed = _object_spread_props(_object_spread({}, parsedTransaction), {
|
|
860
|
+
txID: (_parsedTransaction_txID = parsedTransaction.txID) !== null && _parsedTransaction_txID !== void 0 ? _parsedTransaction_txID : txid.slice(2),
|
|
861
|
+
signature: _to_consumable_array(existingSignatures).concat([
|
|
862
|
+
hexSig
|
|
863
|
+
])
|
|
864
|
+
});
|
|
865
|
+
return [
|
|
866
|
+
2,
|
|
867
|
+
JSON.stringify(signed)
|
|
868
|
+
];
|
|
869
|
+
case 4:
|
|
870
|
+
error = _state.sent();
|
|
871
|
+
_this.logger.error(ERROR_SIGN_TRANSACTION, error);
|
|
872
|
+
throw new Error(ERROR_SIGN_TRANSACTION);
|
|
873
|
+
case 5:
|
|
874
|
+
return [
|
|
875
|
+
2
|
|
876
|
+
];
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
})();
|
|
880
|
+
}
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
/**
|
|
884
|
+
* Derive account address from ECDSA public key.
|
|
885
|
+
*
|
|
886
|
+
* TRON addresses are the keccak256 hash of the uncompressed pubkey (minus
|
|
887
|
+
* the 0x04 prefix), last 20 bytes, prefixed with 0x41, then Base58Check
|
|
888
|
+
* encoded.
|
|
889
|
+
*/ key: "deriveAccountAddress",
|
|
890
|
+
value: function deriveAccountAddress(param) {
|
|
891
|
+
var rawPublicKey = param.rawPublicKey;
|
|
892
|
+
var publicKeyHex = rawPublicKey.pubKeyAsHex();
|
|
893
|
+
var serializedUncompressed = rawPublicKey.serializeUncompressed();
|
|
894
|
+
var uncompressedHex = Buffer.from(serializedUncompressed).toString('hex');
|
|
895
|
+
var accountAddress = deriveTronAddress({
|
|
896
|
+
publicKeyHex: uncompressedHex
|
|
897
|
+
});
|
|
898
|
+
return {
|
|
899
|
+
accountAddress: accountAddress,
|
|
900
|
+
publicKeyHex: publicKeyHex
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
},
|
|
904
|
+
{
|
|
905
|
+
key: "exportPrivateKey",
|
|
906
|
+
value: /**
|
|
907
|
+
* Export private key from MPC key shares.
|
|
908
|
+
*
|
|
909
|
+
* TRON uses the raw hex (no encoded prefix) for its private key format,
|
|
910
|
+
* matching what tronweb / TronLink expose. We return it as 0x-prefixed
|
|
911
|
+
* so it round-trips cleanly through `importPrivateKey`.
|
|
912
|
+
*/ function exportPrivateKey(param) {
|
|
913
|
+
var accountAddress = param.accountAddress, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken, elevatedAccessToken = param.elevatedAccessToken, traceContext = param.traceContext;
|
|
914
|
+
var _this = this;
|
|
915
|
+
return _async_to_generator(function() {
|
|
916
|
+
var derivedPrivateKey, hex, error;
|
|
917
|
+
return _ts_generator(this, function(_state) {
|
|
918
|
+
switch(_state.label){
|
|
919
|
+
case 0:
|
|
920
|
+
_state.trys.push([
|
|
921
|
+
0,
|
|
922
|
+
2,
|
|
923
|
+
,
|
|
924
|
+
3
|
|
925
|
+
]);
|
|
926
|
+
return [
|
|
927
|
+
4,
|
|
928
|
+
_this.exportKey({
|
|
929
|
+
accountAddress: accountAddress,
|
|
930
|
+
chainName: _this.chainName,
|
|
931
|
+
password: password,
|
|
932
|
+
signedSessionId: signedSessionId,
|
|
933
|
+
mfaToken: mfaToken,
|
|
934
|
+
elevatedAccessToken: elevatedAccessToken,
|
|
935
|
+
traceContext: traceContext
|
|
936
|
+
})
|
|
937
|
+
];
|
|
938
|
+
case 1:
|
|
939
|
+
derivedPrivateKey = _state.sent().derivedPrivateKey;
|
|
940
|
+
if (!derivedPrivateKey) {
|
|
941
|
+
throw new Error('Derived private key is undefined');
|
|
942
|
+
}
|
|
943
|
+
hex = derivedPrivateKey.startsWith('0x') ? derivedPrivateKey.slice(2) : derivedPrivateKey;
|
|
944
|
+
return [
|
|
945
|
+
2,
|
|
946
|
+
"0x".concat(hex)
|
|
947
|
+
];
|
|
948
|
+
case 2:
|
|
949
|
+
error = _state.sent();
|
|
950
|
+
_this.logger.error(ERROR_EXPORT_PRIVATE_KEY, error);
|
|
951
|
+
throw new Error(ERROR_EXPORT_PRIVATE_KEY);
|
|
952
|
+
case 3:
|
|
953
|
+
return [
|
|
954
|
+
2
|
|
955
|
+
];
|
|
956
|
+
}
|
|
957
|
+
});
|
|
958
|
+
})();
|
|
959
|
+
}
|
|
960
|
+
},
|
|
961
|
+
{
|
|
962
|
+
key: "offlineExportPrivateKey",
|
|
963
|
+
value: /**
|
|
964
|
+
* Offline export of private key from key shares.
|
|
965
|
+
*/ function offlineExportPrivateKey(param) {
|
|
966
|
+
var keyShares = param.keyShares, derivationPath = param.derivationPath;
|
|
967
|
+
var _this = this;
|
|
968
|
+
return _async_to_generator(function() {
|
|
969
|
+
var derivedPrivateKey;
|
|
970
|
+
return _ts_generator(this, function(_state) {
|
|
971
|
+
switch(_state.label){
|
|
972
|
+
case 0:
|
|
973
|
+
return [
|
|
974
|
+
4,
|
|
975
|
+
_this.offlineExportKey({
|
|
976
|
+
chainName: _this.chainName,
|
|
977
|
+
keyShares: keyShares,
|
|
978
|
+
derivationPath: derivationPath
|
|
979
|
+
})
|
|
980
|
+
];
|
|
981
|
+
case 1:
|
|
982
|
+
derivedPrivateKey = _state.sent().derivedPrivateKey;
|
|
983
|
+
return [
|
|
984
|
+
2,
|
|
985
|
+
{
|
|
986
|
+
derivedPrivateKey: derivedPrivateKey
|
|
987
|
+
}
|
|
988
|
+
];
|
|
989
|
+
}
|
|
990
|
+
});
|
|
991
|
+
})();
|
|
992
|
+
}
|
|
993
|
+
},
|
|
994
|
+
{
|
|
995
|
+
key: "importPrivateKey",
|
|
996
|
+
value: /**
|
|
997
|
+
* Import an existing TRON private key into MPC.
|
|
998
|
+
*/ function importPrivateKey(param) {
|
|
999
|
+
var privateKey = param.privateKey, chainName = param.chainName, thresholdSignatureScheme = param.thresholdSignatureScheme, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, onError = param.onError, signedSessionId = param.signedSessionId, publicAddressCheck = param.publicAddressCheck, legacyWalletId = param.legacyWalletId;
|
|
1000
|
+
var _this = this;
|
|
1001
|
+
return _async_to_generator(function() {
|
|
1002
|
+
var ceremonyCompleteResolver, ceremonyCompletePromise, formattedPrivateKey, _ref, rawPublicKey, clientKeyShares, _this_deriveAccountAddress, accountAddress, publicKeyHex, error;
|
|
1003
|
+
return _ts_generator(this, function(_state) {
|
|
1004
|
+
switch(_state.label){
|
|
1005
|
+
case 0:
|
|
1006
|
+
_state.trys.push([
|
|
1007
|
+
0,
|
|
1008
|
+
5,
|
|
1009
|
+
,
|
|
1010
|
+
6
|
|
1011
|
+
]);
|
|
1012
|
+
if (publicAddressCheck && !isValidTronAddress(publicAddressCheck)) {
|
|
1013
|
+
throw new Error('Invalid TRON address provided for publicAddressCheck');
|
|
1014
|
+
}
|
|
1015
|
+
ceremonyCompletePromise = new Promise(function(resolve) {
|
|
1016
|
+
ceremonyCompleteResolver = resolve;
|
|
1017
|
+
});
|
|
1018
|
+
formattedPrivateKey = privateKey.startsWith('0x') ? privateKey.slice(2) : privateKey;
|
|
1019
|
+
return [
|
|
1020
|
+
4,
|
|
1021
|
+
_this.importRawPrivateKey({
|
|
1022
|
+
chainName: chainName,
|
|
1023
|
+
privateKey: formattedPrivateKey,
|
|
1024
|
+
thresholdSignatureScheme: thresholdSignatureScheme,
|
|
1025
|
+
onCeremonyComplete: function(accountAddress, walletId) {
|
|
1026
|
+
_this.initializeWalletMapEntry({
|
|
1027
|
+
accountAddress: accountAddress,
|
|
1028
|
+
walletId: walletId,
|
|
1029
|
+
chainName: _this.chainName,
|
|
1030
|
+
thresholdSignatureScheme: thresholdSignatureScheme
|
|
1031
|
+
});
|
|
1032
|
+
ceremonyCompleteResolver(undefined);
|
|
1033
|
+
},
|
|
1034
|
+
onError: onError,
|
|
1035
|
+
legacyWalletId: legacyWalletId,
|
|
1036
|
+
password: password,
|
|
1037
|
+
signedSessionId: signedSessionId
|
|
1038
|
+
})
|
|
1039
|
+
];
|
|
1040
|
+
case 1:
|
|
1041
|
+
_ref = _state.sent(), rawPublicKey = _ref.rawPublicKey, clientKeyShares = _ref.clientKeyShares;
|
|
1042
|
+
return [
|
|
1043
|
+
4,
|
|
1044
|
+
ceremonyCompletePromise
|
|
1045
|
+
];
|
|
1046
|
+
case 2:
|
|
1047
|
+
_state.sent();
|
|
1048
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
1049
|
+
throw new Error(ERROR_IMPORT_PRIVATE_KEY);
|
|
1050
|
+
}
|
|
1051
|
+
_this.checkRawPublicKeyInstance(rawPublicKey);
|
|
1052
|
+
_this_deriveAccountAddress = _this.deriveAccountAddress({
|
|
1053
|
+
rawPublicKey: rawPublicKey
|
|
1054
|
+
}), accountAddress = _this_deriveAccountAddress.accountAddress, publicKeyHex = _this_deriveAccountAddress.publicKeyHex;
|
|
1055
|
+
if (publicAddressCheck && accountAddress !== publicAddressCheck) {
|
|
1056
|
+
throw new Error('Public address mismatch between derived and provided publicAddressCheck');
|
|
1057
|
+
}
|
|
1058
|
+
return [
|
|
1059
|
+
4,
|
|
1060
|
+
_this.setClientKeySharesToStorage({
|
|
1061
|
+
accountAddress: accountAddress,
|
|
1062
|
+
clientKeyShares: clientKeyShares
|
|
1063
|
+
})
|
|
1064
|
+
];
|
|
1065
|
+
case 3:
|
|
1066
|
+
_state.sent();
|
|
1067
|
+
return [
|
|
1068
|
+
4,
|
|
1069
|
+
_this.storeEncryptedBackupByWallet({
|
|
1070
|
+
accountAddress: accountAddress,
|
|
1071
|
+
clientKeyShares: clientKeyShares,
|
|
1072
|
+
password: password,
|
|
1073
|
+
signedSessionId: signedSessionId
|
|
1074
|
+
})
|
|
1075
|
+
];
|
|
1076
|
+
case 4:
|
|
1077
|
+
_state.sent();
|
|
1078
|
+
return [
|
|
1079
|
+
2,
|
|
1080
|
+
{
|
|
1081
|
+
accountAddress: accountAddress,
|
|
1082
|
+
rawPublicKey: rawPublicKey,
|
|
1083
|
+
publicKeyHex: publicKeyHex
|
|
1084
|
+
}
|
|
1085
|
+
];
|
|
1086
|
+
case 5:
|
|
1087
|
+
error = _state.sent();
|
|
1088
|
+
_this.logger.error(ERROR_IMPORT_PRIVATE_KEY, error);
|
|
1089
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
1090
|
+
throw error;
|
|
1091
|
+
case 6:
|
|
1092
|
+
return [
|
|
1093
|
+
2
|
|
1094
|
+
];
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
1097
|
+
})();
|
|
1098
|
+
}
|
|
1099
|
+
},
|
|
1100
|
+
{
|
|
1101
|
+
key: "getTronWallets",
|
|
1102
|
+
value: /**
|
|
1103
|
+
* Get all TRON wallets known to this client.
|
|
1104
|
+
*/ function getTronWallets() {
|
|
1105
|
+
var _this = this;
|
|
1106
|
+
return _async_to_generator(function() {
|
|
1107
|
+
var wallets;
|
|
1108
|
+
return _ts_generator(this, function(_state) {
|
|
1109
|
+
switch(_state.label){
|
|
1110
|
+
case 0:
|
|
1111
|
+
return [
|
|
1112
|
+
4,
|
|
1113
|
+
_this.getWallets()
|
|
1114
|
+
];
|
|
1115
|
+
case 1:
|
|
1116
|
+
wallets = _state.sent();
|
|
1117
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1118
|
+
return [
|
|
1119
|
+
2,
|
|
1120
|
+
wallets.filter(function(wallet) {
|
|
1121
|
+
return wallet.chainName === 'TRON';
|
|
1122
|
+
})
|
|
1123
|
+
];
|
|
1124
|
+
}
|
|
1125
|
+
});
|
|
1126
|
+
})();
|
|
1127
|
+
}
|
|
1128
|
+
},
|
|
1129
|
+
{
|
|
1130
|
+
key: "checkRawPublicKeyInstance",
|
|
1131
|
+
value: /**
|
|
1132
|
+
* Check if the raw public key is a valid ECDSA public key instance.
|
|
1133
|
+
*/ function checkRawPublicKeyInstance(rawPublicKey) {
|
|
1134
|
+
if (!rawPublicKey || (typeof rawPublicKey === "undefined" ? "undefined" : _type_of(rawPublicKey)) !== 'object' || !('serializeUncompressed' in rawPublicKey) || !('pubKeyAsHex' in rawPublicKey)) {
|
|
1135
|
+
throw new Error(ERROR_INVALID_RAW_PUBLIC_KEY);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
]);
|
|
1140
|
+
return DynamicTronWalletClient;
|
|
1141
|
+
}(browser.DynamicWalletClient);
|
|
1142
|
+
|
|
1143
|
+
exports.sdkApiCore = sdkApiCore__namespace;
|
|
1144
|
+
exports.DynamicTronWalletClient = DynamicTronWalletClient;
|
|
1145
|
+
exports.TRON_ADDRESS_PREFIX = TRON_ADDRESS_PREFIX;
|
|
1146
|
+
exports.TRON_SIGN_MESSAGE_PREFIX = TRON_SIGN_MESSAGE_PREFIX;
|
|
1147
|
+
exports.decodeTronAddress = decodeTronAddress;
|
|
1148
|
+
exports.deriveTronAddress = deriveTronAddress;
|
|
1149
|
+
exports.formatTronMessage = formatTronMessage;
|
|
1150
|
+
exports.getTronTxId = getTronTxId;
|
|
1151
|
+
exports.getTronTxIdHex = getTronTxIdHex;
|
|
1152
|
+
exports.isValidTronAddress = isValidTronAddress;
|
|
1153
|
+
exports.serializeECDSASignature = serializeECDSASignature;
|
|
1154
|
+
exports.toTronMessageBytes = toTronMessageBytes;
|
|
1155
|
+
exports.tronAddressToHex = tronAddressToHex;
|