@dynamic-labs-wallet/tron 1.1.68 → 1.1.70

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 CHANGED
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var browser = require('@dynamic-labs-wallet/browser');
4
- var sdkApiCore = require('@dynamic-labs/sdk-api-core');
5
4
  var sha3 = require('@noble/hashes/sha3');
6
5
  var sha256 = require('@noble/hashes/sha256');
7
6
  var bs58 = require('bs58');
7
+ var sdkApiCore = require('@dynamic-labs/sdk-api-core');
8
8
 
9
9
  function _interopNamespaceDefault(e) {
10
10
  var n = Object.create(null);
@@ -38,7 +38,7 @@ var sdkApiCore__namespace = /*#__PURE__*/_interopNamespaceDefault(sdkApiCore);
38
38
  /**
39
39
  * Length of the encoded (un-prefixed) Base58Check TRON address.
40
40
  * `T...` addresses are always 34 characters.
41
- */ var TRON_ADDRESS_LENGTH = 34;
41
+ */ var TRON_ADDRESS_LENGTH$1 = 34;
42
42
  /**
43
43
  * Derive a TRON Base58Check ("T...") address from a secp256k1 public key.
44
44
  *
@@ -88,7 +88,7 @@ var deriveTronAddress = function(param) {
88
88
  * `0x...` addresses, malformed Base58, and addresses whose checksum was
89
89
  * tampered with.
90
90
  */ var isValidTronAddress = function(address) {
91
- if (typeof address !== 'string' || address.length !== TRON_ADDRESS_LENGTH) {
91
+ if (typeof address !== 'string' || address.length !== TRON_ADDRESS_LENGTH$1) {
92
92
  return false;
93
93
  }
94
94
  var decoded;
@@ -256,6 +256,88 @@ function _instanceof$1(left, right) {
256
256
  }
257
257
  };
258
258
 
259
+ function _define_property$2(obj, key, value) {
260
+ if (key in obj) {
261
+ Object.defineProperty(obj, key, {
262
+ value: value,
263
+ enumerable: true,
264
+ configurable: true,
265
+ writable: true
266
+ });
267
+ } else {
268
+ obj[key] = value;
269
+ }
270
+ return obj;
271
+ }
272
+ function _object_spread$2(target) {
273
+ for(var i = 1; i < arguments.length; i++){
274
+ var source = arguments[i] != null ? arguments[i] : {};
275
+ var ownKeys = Object.keys(source);
276
+ if (typeof Object.getOwnPropertySymbols === "function") {
277
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
278
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
279
+ }));
280
+ }
281
+ ownKeys.forEach(function(key) {
282
+ _define_property$2(target, key, source[key]);
283
+ });
284
+ }
285
+ return target;
286
+ }
287
+ function ownKeys$1(object, enumerableOnly) {
288
+ var keys = Object.keys(object);
289
+ if (Object.getOwnPropertySymbols) {
290
+ var symbols = Object.getOwnPropertySymbols(object);
291
+ keys.push.apply(keys, symbols);
292
+ }
293
+ return keys;
294
+ }
295
+ function _object_spread_props$1(target, source) {
296
+ source = source != null ? source : {};
297
+ if (Object.getOwnPropertyDescriptors) {
298
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
299
+ } else {
300
+ ownKeys$1(Object(source)).forEach(function(key) {
301
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
302
+ });
303
+ }
304
+ return target;
305
+ }
306
+ var HEX_CHAIN_ID = /^0x[0-9a-f]+$/i;
307
+ var normalizeTronChainId = function(chainId) {
308
+ return HEX_CHAIN_ID.test(chainId) ? BigInt(chainId).toString() : chainId;
309
+ };
310
+ var resolveTronTransactionContext = function(param) {
311
+ var context = param.context, chainId = param.chainId, rawDataHex = param.rawDataHex;
312
+ if ((context === null || context === void 0 ? void 0 : context.tronTransaction) !== undefined) {
313
+ return context;
314
+ }
315
+ var base = _object_spread$2({}, context);
316
+ delete base.rawSign;
317
+ if (chainId !== undefined) {
318
+ return _object_spread_props$1(_object_spread$2({}, base), {
319
+ tronTransaction: {
320
+ chainId: normalizeTronChainId(chainId),
321
+ rawDataHex: rawDataHex
322
+ }
323
+ });
324
+ }
325
+ return _object_spread_props$1(_object_spread$2({}, base), {
326
+ rawSign: {
327
+ hashFunction: sdkApiCore.SignMessageRawSignHashFunctionEnum.Sha256,
328
+ payload: rawDataHex
329
+ }
330
+ });
331
+ };
332
+ var resolveTronMessageContext = function(context, message) {
333
+ if ((context === null || context === void 0 ? void 0 : context.tronMessage) !== undefined || (context === null || context === void 0 ? void 0 : context.rawSign) !== undefined) {
334
+ return context;
335
+ }
336
+ return _object_spread_props$1(_object_spread$2({}, context), {
337
+ tronMessage: message
338
+ });
339
+ };
340
+
259
341
  var ERROR_KEYGEN_FAILED = 'Error with keygen';
260
342
  var ERROR_CREATE_WALLET_ACCOUNT = 'Error creating TRON wallet account';
261
343
  var ERROR_IMPORT_PRIVATE_KEY = 'Error importing private key';
@@ -331,7 +413,7 @@ function _create_class(Constructor, protoProps, staticProps) {
331
413
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
332
414
  return Constructor;
333
415
  }
334
- function _define_property(obj, key, value) {
416
+ function _define_property$1(obj, key, value) {
335
417
  if (key in obj) {
336
418
  Object.defineProperty(obj, key, {
337
419
  value: value,
@@ -376,7 +458,7 @@ function _iterable_to_array(iter) {
376
458
  function _non_iterable_spread() {
377
459
  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
460
  }
379
- function _object_spread(target) {
461
+ function _object_spread$1(target) {
380
462
  for(var i = 1; i < arguments.length; i++){
381
463
  var source = arguments[i] != null ? arguments[i] : {};
382
464
  var ownKeys = Object.keys(source);
@@ -386,7 +468,7 @@ function _object_spread(target) {
386
468
  }));
387
469
  }
388
470
  ownKeys.forEach(function(key) {
389
- _define_property(target, key, source[key]);
471
+ _define_property$1(target, key, source[key]);
390
472
  });
391
473
  }
392
474
  return target;
@@ -566,7 +648,7 @@ function _ts_generator(thisArg, body) {
566
648
  function DynamicTronWalletClient() {
567
649
  _class_call_check(this, DynamicTronWalletClient);
568
650
  var _this;
569
- _this = _call_super(this, DynamicTronWalletClient, arguments), _define_property(_this, "chainName", 'TRON');
651
+ _this = _call_super(this, DynamicTronWalletClient, arguments), _define_property$1(_this, "chainName", 'TRON');
570
652
  return _this;
571
653
  }
572
654
  _create_class(DynamicTronWalletClient, [
@@ -733,7 +815,7 @@ function _ts_generator(thisArg, body) {
733
815
  isFormatted: true,
734
816
  mfaToken: mfaToken,
735
817
  elevatedAccessToken: elevatedAccessToken,
736
- context: context,
818
+ context: resolveTronMessageContext(context, message),
737
819
  approvalQuorum: approvalQuorum,
738
820
  onError: onError,
739
821
  traceContext: traceContext
@@ -784,7 +866,7 @@ function _ts_generator(thisArg, body) {
784
866
  * (rather than the object) matches the SDK's iframe `signTransaction`
785
867
  * contract, which only carries strings over message transport.
786
868
  */ function signTransaction(param) {
787
- 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, approvalQuorum = param.approvalQuorum, onError = param.onError, traceContext = param.traceContext;
869
+ var senderAddress = param.senderAddress, transaction = param.transaction, chainId = param.chainId, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken, elevatedAccessToken = param.elevatedAccessToken, context = param.context, approvalQuorum = param.approvalQuorum, onError = param.onError, traceContext = param.traceContext;
788
870
  var _this = this;
789
871
  return _async_to_generator(function() {
790
872
  var rawParsed, parsedTransaction, digestBytes, txid, resolvedContext, signatureEcdsa, serializedSig, hexSig, _parsedTransaction_signature, existingSignatures, _parsedTransaction_txID, signed, error;
@@ -826,11 +908,10 @@ function _ts_generator(thisArg, body) {
826
908
  ]);
827
909
  digestBytes = getTronTxId(parsedTransaction.raw_data_hex);
828
910
  txid = getTronTxIdHex(parsedTransaction.raw_data_hex);
829
- resolvedContext = _object_spread_props(_object_spread({}, context), {
830
- rawSign: {
831
- hashFunction: sdkApiCore.SignMessageRawSignHashFunctionEnum.Sha256,
832
- payload: parsedTransaction.raw_data_hex
833
- }
911
+ resolvedContext = resolveTronTransactionContext({
912
+ context: context,
913
+ chainId: chainId,
914
+ rawDataHex: parsedTransaction.raw_data_hex
834
915
  });
835
916
  return [
836
917
  4,
@@ -854,7 +935,7 @@ function _ts_generator(thisArg, body) {
854
935
  serializedSig = serializeECDSASignature(signatureEcdsa, _this.logger);
855
936
  hexSig = serializedSig.slice(2);
856
937
  existingSignatures = (_parsedTransaction_signature = parsedTransaction.signature) !== null && _parsedTransaction_signature !== void 0 ? _parsedTransaction_signature : [];
857
- signed = _object_spread_props(_object_spread({}, parsedTransaction), {
938
+ signed = _object_spread_props(_object_spread$1({}, parsedTransaction), {
858
939
  txID: (_parsedTransaction_txID = parsedTransaction.txID) !== null && _parsedTransaction_txID !== void 0 ? _parsedTransaction_txID : txid.slice(2),
859
940
  signature: _to_consumable_array(existingSignatures).concat([
860
941
  hexSig
@@ -1144,6 +1225,416 @@ function _ts_generator(thisArg, body) {
1144
1225
  return DynamicTronWalletClient;
1145
1226
  }(browser.DynamicWalletClient);
1146
1227
 
1228
+ var TRON_ADDRESS_LENGTH = 21;
1229
+ var CHECKSUM_LENGTH = 4;
1230
+ var MAX_RAW_DATA_BYTES = 102400;
1231
+ var MAX_VARINT_BYTES = 10;
1232
+ var TRANSFER_CONTRACT = 1n;
1233
+ var TRIGGER_SMART_CONTRACT = 31n;
1234
+ var TYPE_URLS = {
1235
+ TransferContract: 'type.googleapis.com/protocol.TransferContract',
1236
+ TriggerSmartContract: 'type.googleapis.com/protocol.TriggerSmartContract'
1237
+ };
1238
+ var CONTRACT_TYPES = {
1239
+ TransferContract: TRANSFER_CONTRACT,
1240
+ TriggerSmartContract: TRIGGER_SMART_CONTRACT
1241
+ };
1242
+ var readVarint = function(bytes, start) {
1243
+ var value = 0n;
1244
+ for(var index = 0; index < MAX_VARINT_BYTES; index += 1){
1245
+ var byte = bytes[start + index];
1246
+ if (byte === undefined) return undefined;
1247
+ value |= BigInt(byte & 0x7f) << BigInt(7 * index);
1248
+ if ((byte & 0x80) === 0) return {
1249
+ next: start + index + 1,
1250
+ value: value
1251
+ };
1252
+ }
1253
+ return undefined;
1254
+ };
1255
+ // Only varint and length-delimited fields: every field this parser accepts is
1256
+ // one of the two, so any other wire type is rejected rather than skipped.
1257
+ var readField = function(bytes, position) {
1258
+ var key = readVarint(bytes, position);
1259
+ if (!key) return undefined;
1260
+ var number = Number(key.value >> 3n);
1261
+ var wireType = Number(key.value & 7n);
1262
+ if (number === 0) return undefined;
1263
+ if (wireType === 0) {
1264
+ var varint = readVarint(bytes, key.next);
1265
+ return varint && {
1266
+ field: {
1267
+ number: number,
1268
+ varint: varint.value
1269
+ },
1270
+ next: varint.next
1271
+ };
1272
+ }
1273
+ if (wireType === 2) {
1274
+ var length = readVarint(bytes, key.next);
1275
+ if (!length || length.value > BigInt(bytes.length - length.next)) return undefined;
1276
+ var end = length.next + Number(length.value);
1277
+ return {
1278
+ field: {
1279
+ bytes: bytes.subarray(length.next, end),
1280
+ number: number
1281
+ },
1282
+ next: end
1283
+ };
1284
+ }
1285
+ return undefined;
1286
+ };
1287
+ var readFields = function(bytes) {
1288
+ var fields = [];
1289
+ var position = 0;
1290
+ while(position < bytes.length){
1291
+ var read = readField(bytes, position);
1292
+ if (!read) return undefined;
1293
+ fields.push(read.field);
1294
+ position = read.next;
1295
+ }
1296
+ return fields;
1297
+ };
1298
+ // A field outside `allowed`, of the wrong kind or repeated would be signed but
1299
+ // not described by the intent, so the whole message is rejected.
1300
+ var readStrict = function(bytes, allowed) {
1301
+ var fields = readFields(bytes);
1302
+ if (!fields) return undefined;
1303
+ var byNumber = new Map();
1304
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1305
+ try {
1306
+ for(var _iterator = fields[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1307
+ var field = _step.value;
1308
+ var kind = allowed[field.number];
1309
+ if (kind === undefined || byNumber.has(field.number)) return undefined;
1310
+ if (kind === 'bytes' ? !field.bytes : field.varint === undefined) return undefined;
1311
+ byNumber.set(field.number, field);
1312
+ }
1313
+ } catch (err) {
1314
+ _didIteratorError = true;
1315
+ _iteratorError = err;
1316
+ } finally{
1317
+ try {
1318
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1319
+ _iterator.return();
1320
+ }
1321
+ } finally{
1322
+ if (_didIteratorError) {
1323
+ throw _iteratorError;
1324
+ }
1325
+ }
1326
+ }
1327
+ return byNumber;
1328
+ };
1329
+ var concat = function(parts) {
1330
+ var result = new Uint8Array(parts.reduce(function(total, part) {
1331
+ return total + part.length;
1332
+ }, 0));
1333
+ var offset = 0;
1334
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1335
+ try {
1336
+ for(var _iterator = parts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1337
+ var part = _step.value;
1338
+ result.set(part, offset);
1339
+ offset += part.length;
1340
+ }
1341
+ } catch (err) {
1342
+ _didIteratorError = true;
1343
+ _iteratorError = err;
1344
+ } finally{
1345
+ try {
1346
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1347
+ _iterator.return();
1348
+ }
1349
+ } finally{
1350
+ if (_didIteratorError) {
1351
+ throw _iteratorError;
1352
+ }
1353
+ }
1354
+ }
1355
+ return result;
1356
+ };
1357
+ var EMPTY = new Uint8Array(0);
1358
+ var checksumOf = function(payload) {
1359
+ return sha256.sha256(sha256.sha256(payload)).subarray(0, CHECKSUM_LENGTH);
1360
+ };
1361
+ var encodeAddress = function(bytes) {
1362
+ if ((bytes === null || bytes === void 0 ? void 0 : bytes.length) !== TRON_ADDRESS_LENGTH || bytes[0] !== TRON_ADDRESS_PREFIX) return undefined;
1363
+ return bs58.encode(concat([
1364
+ bytes,
1365
+ checksumOf(bytes)
1366
+ ]));
1367
+ };
1368
+ var decodeAddressBytes = function(address) {
1369
+ var decoded = bs58.decode(address);
1370
+ var payload = decoded.subarray(0, TRON_ADDRESS_LENGTH);
1371
+ var checksum = decoded.subarray(TRON_ADDRESS_LENGTH);
1372
+ if (decoded.length !== TRON_ADDRESS_LENGTH + CHECKSUM_LENGTH || payload[0] !== TRON_ADDRESS_PREFIX || Buffer.compare(checksum, checksumOf(payload)) !== 0) {
1373
+ throw new Error('Invalid TRON address');
1374
+ }
1375
+ return payload;
1376
+ };
1377
+ var toHex = function(bytes) {
1378
+ return "0x".concat(Buffer.from(bytes !== null && bytes !== void 0 ? bytes : []).toString('hex'));
1379
+ };
1380
+ var hexBytes = function(hex) {
1381
+ return Uint8Array.from(Buffer.from(hex.replace(/^0x/, ''), 'hex'));
1382
+ };
1383
+ /**
1384
+ * Reads a TRON transaction's `raw_data` into the fields a signing intent
1385
+ * describes. Returns `undefined` for anything it cannot describe completely:
1386
+ * unknown, repeated or mistyped fields at any level, anything but one
1387
+ * TransferContract or TriggerSmartContract, a malformed address.
1388
+ */ var parseTronRawData = function(rawData) {
1389
+ var _raw_get, _contract_get, _contract_get1, _parameter_get, _parameter_get1, _value_get, _value_get1, _value_get2, _raw_get1, _value_get3;
1390
+ if (rawData.length === 0 || rawData.length > MAX_RAW_DATA_BYTES) return undefined;
1391
+ var raw = readStrict(rawData, {
1392
+ 1: 'bytes',
1393
+ 3: 'varint',
1394
+ 4: 'bytes',
1395
+ 8: 'varint',
1396
+ 11: 'bytes',
1397
+ 14: 'varint',
1398
+ 18: 'varint'
1399
+ });
1400
+ var contractBytes = raw === null || raw === void 0 ? void 0 : (_raw_get = raw.get(11)) === null || _raw_get === void 0 ? void 0 : _raw_get.bytes;
1401
+ if (!raw || !contractBytes) return undefined;
1402
+ var contract = readStrict(contractBytes, {
1403
+ 1: 'varint',
1404
+ 2: 'bytes'
1405
+ });
1406
+ var type = contract === null || contract === void 0 ? void 0 : (_contract_get = contract.get(1)) === null || _contract_get === void 0 ? void 0 : _contract_get.varint;
1407
+ var parameterBytes = contract === null || contract === void 0 ? void 0 : (_contract_get1 = contract.get(2)) === null || _contract_get1 === void 0 ? void 0 : _contract_get1.bytes;
1408
+ if (type === undefined || !parameterBytes) return undefined;
1409
+ var parameter = readStrict(parameterBytes, {
1410
+ 1: 'bytes',
1411
+ 2: 'bytes'
1412
+ });
1413
+ var typeUrl = parameter === null || parameter === void 0 ? void 0 : (_parameter_get = parameter.get(1)) === null || _parameter_get === void 0 ? void 0 : _parameter_get.bytes;
1414
+ var valueBytes = parameter === null || parameter === void 0 ? void 0 : (_parameter_get1 = parameter.get(2)) === null || _parameter_get1 === void 0 ? void 0 : _parameter_get1.bytes;
1415
+ if (!typeUrl || !valueBytes) return undefined;
1416
+ var contractType = Object.keys(TYPE_URLS).find(function(name) {
1417
+ return CONTRACT_TYPES[name] === type && TYPE_URLS[name] === Buffer.from(typeUrl).toString('utf8');
1418
+ });
1419
+ if (!contractType) return undefined;
1420
+ var isTransfer = contractType === 'TransferContract';
1421
+ var value = readStrict(valueBytes, isTransfer ? {
1422
+ 1: 'bytes',
1423
+ 2: 'bytes',
1424
+ 3: 'varint'
1425
+ } : {
1426
+ 1: 'bytes',
1427
+ 2: 'bytes',
1428
+ 3: 'varint',
1429
+ 4: 'bytes'
1430
+ });
1431
+ var from = encodeAddress(value === null || value === void 0 ? void 0 : (_value_get = value.get(1)) === null || _value_get === void 0 ? void 0 : _value_get.bytes);
1432
+ var to = encodeAddress(value === null || value === void 0 ? void 0 : (_value_get1 = value.get(2)) === null || _value_get1 === void 0 ? void 0 : _value_get1.bytes);
1433
+ if (!value || !from || !to) return undefined;
1434
+ var _raw_get_varint, _value_get_varint;
1435
+ return {
1436
+ contractType: contractType,
1437
+ data: isTransfer ? '0x' : toHex((_value_get2 = value.get(4)) === null || _value_get2 === void 0 ? void 0 : _value_get2.bytes),
1438
+ feeLimit: ((_raw_get_varint = (_raw_get1 = raw.get(18)) === null || _raw_get1 === void 0 ? void 0 : _raw_get1.varint) !== null && _raw_get_varint !== void 0 ? _raw_get_varint : 0n).toString(),
1439
+ from: from,
1440
+ to: to,
1441
+ value: ((_value_get_varint = (_value_get3 = value.get(3)) === null || _value_get3 === void 0 ? void 0 : _value_get3.varint) !== null && _value_get_varint !== void 0 ? _value_get_varint : 0n).toString()
1442
+ };
1443
+ };
1444
+ var varint = function(value) {
1445
+ var bytes = [];
1446
+ var rest = value;
1447
+ while(rest >= 0x80n){
1448
+ bytes.push(Number(rest & 0x7fn) | 0x80);
1449
+ rest >>= 7n;
1450
+ }
1451
+ bytes.push(Number(rest));
1452
+ return Uint8Array.from(bytes);
1453
+ };
1454
+ var bytesField = function(number, value) {
1455
+ return concat([
1456
+ varint(BigInt(number << 3 | 2)),
1457
+ varint(BigInt(value.length)),
1458
+ value
1459
+ ]);
1460
+ };
1461
+ // proto3 leaves a zero or empty field out.
1462
+ var optionalVarintField = function(number, value) {
1463
+ return value === 0n ? EMPTY : concat([
1464
+ varint(BigInt(number << 3)),
1465
+ varint(value)
1466
+ ]);
1467
+ };
1468
+ var optionalBytesField = function(number, value) {
1469
+ return value.length === 0 ? EMPTY : bytesField(number, value);
1470
+ };
1471
+ var encodeContract = function(body) {
1472
+ var owner = bytesField(1, decodeAddressBytes(body.from));
1473
+ var target = bytesField(2, decodeAddressBytes(body.to));
1474
+ var value = optionalVarintField(3, BigInt(body.value));
1475
+ var contractValue = body.contractType === 'TransferContract' ? concat([
1476
+ owner,
1477
+ target,
1478
+ value
1479
+ ]) : concat([
1480
+ owner,
1481
+ target,
1482
+ value,
1483
+ optionalBytesField(4, hexBytes(body.data))
1484
+ ]);
1485
+ var typeUrl = Uint8Array.from(Buffer.from(TYPE_URLS[body.contractType]));
1486
+ return concat([
1487
+ optionalVarintField(1, CONTRACT_TYPES[body.contractType]),
1488
+ bytesField(2, concat([
1489
+ bytesField(1, typeUrl),
1490
+ bytesField(2, contractValue)
1491
+ ]))
1492
+ ]);
1493
+ };
1494
+ /**
1495
+ * Serializes a transaction's `raw_data` from the described body and a block
1496
+ * reference, with the fields in field-number order as a TRON node does.
1497
+ */ var encodeTronRawData = function(body, reference) {
1498
+ return concat([
1499
+ bytesField(1, hexBytes(reference.refBlockBytes)),
1500
+ bytesField(4, hexBytes(reference.refBlockHash)),
1501
+ optionalVarintField(8, BigInt(reference.expiration)),
1502
+ bytesField(11, encodeContract(body)),
1503
+ optionalVarintField(14, BigInt(reference.timestamp)),
1504
+ optionalVarintField(18, BigInt(body.feeLimit))
1505
+ ]);
1506
+ };
1507
+
1508
+ function _define_property(obj, key, value) {
1509
+ if (key in obj) {
1510
+ Object.defineProperty(obj, key, {
1511
+ value: value,
1512
+ enumerable: true,
1513
+ configurable: true,
1514
+ writable: true
1515
+ });
1516
+ } else {
1517
+ obj[key] = value;
1518
+ }
1519
+ return obj;
1520
+ }
1521
+ function _object_spread(target) {
1522
+ for(var i = 1; i < arguments.length; i++){
1523
+ var source = arguments[i] != null ? arguments[i] : {};
1524
+ var ownKeys = Object.keys(source);
1525
+ if (typeof Object.getOwnPropertySymbols === "function") {
1526
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
1527
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
1528
+ }));
1529
+ }
1530
+ ownKeys.forEach(function(key) {
1531
+ _define_property(target, key, source[key]);
1532
+ });
1533
+ }
1534
+ return target;
1535
+ }
1536
+ var isHexBytes = function(value) {
1537
+ return /^(?:[0-9a-fA-F]{2})+$/.test(value);
1538
+ };
1539
+ var DECIMAL_CHAIN_ID = /^(0|[1-9]\d*)$/;
1540
+ // A hexadecimal id is accepted as the TRON client does, and described in decimal.
1541
+ var assertNetworkChainId = function(chainId) {
1542
+ var networkChainId = chainId && normalizeTronChainId(chainId);
1543
+ if (!networkChainId || !DECIMAL_CHAIN_ID.test(networkChainId)) {
1544
+ throw new Error('TRON signing intent requires a network chainId');
1545
+ }
1546
+ return networkChainId;
1547
+ };
1548
+ // The TRON client takes the transaction as JSON holding `raw_data_hex`; the bare hex is accepted too.
1549
+ var rawDataHexOf = function(transaction) {
1550
+ var trimmed = transaction.trim();
1551
+ if (!trimmed.startsWith('{')) return trimmed.replace(/^0x/, '');
1552
+ try {
1553
+ var _JSON_parse = JSON.parse(trimmed), rawDataHex = _JSON_parse.raw_data_hex;
1554
+ if (typeof rawDataHex === 'string') return rawDataHex.replace(/^0x/, '');
1555
+ } catch (e) {
1556
+ // Not JSON: rejected below as not hex.
1557
+ }
1558
+ return trimmed;
1559
+ };
1560
+ /**
1561
+ * Describes a TRON transaction for a signing-quorum intent from its `raw_data`
1562
+ * hex, bare or inside the transaction JSON the TRON client signs. The reference block, expiration and timestamp are not part of the
1563
+ * description: they may be refreshed when the approved intent is resumed.
1564
+ */ var prepareTronTransactionSigningOperation = function(param) {
1565
+ var transaction = param.transaction, accountAddress = param.accountAddress, chainId = param.chainId;
1566
+ var networkChainId = assertNetworkChainId(chainId);
1567
+ var rawDataHex = rawDataHexOf(transaction);
1568
+ if (!isHexBytes(rawDataHex)) throw new Error('TRON signing intent requires a hex raw_data');
1569
+ var body = parseTronRawData(Uint8Array.from(Buffer.from(rawDataHex, 'hex')));
1570
+ if (!body) throw new Error('TRON signing intent supports one TRX transfer or smart-contract call');
1571
+ if (body.from !== accountAddress) throw new Error('TRON transaction owner must be the signing account');
1572
+ return {
1573
+ operation: _object_spread({
1574
+ kind: 'transaction',
1575
+ chainName: 'TRON',
1576
+ chainId: networkChainId
1577
+ }, body),
1578
+ lifetime: {
1579
+ refreshable: 'expiration'
1580
+ }
1581
+ };
1582
+ };
1583
+ var signedTronOperation = function(intent) {
1584
+ var _intent_lifetime;
1585
+ var operation = intent.operation;
1586
+ if (operation.kind !== 'transaction' || operation.chainName !== 'TRON' || ((_intent_lifetime = intent.lifetime) === null || _intent_lifetime === void 0 ? void 0 : _intent_lifetime.refreshable) !== 'expiration') {
1587
+ throw new Error('Stored signing intent is not a TRON transaction');
1588
+ }
1589
+ return operation;
1590
+ };
1591
+ var isPositiveSafeInteger = function(value) {
1592
+ return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
1593
+ };
1594
+ var parseBlockReference = function(lifetimeValue) {
1595
+ var parsed;
1596
+ try {
1597
+ parsed = lifetimeValue ? JSON.parse(lifetimeValue) : undefined;
1598
+ } catch (e) {
1599
+ parsed = undefined;
1600
+ }
1601
+ var _ref = parsed !== null && parsed !== void 0 ? parsed : {}, expiration = _ref.expiration, refBlockBytes = _ref.refBlockBytes, refBlockHash = _ref.refBlockHash, timestamp = _ref.timestamp;
1602
+ if (typeof refBlockBytes !== 'string' || !/^(?:[0-9a-fA-F]{2}){2}$/.test(refBlockBytes) || typeof refBlockHash !== 'string' || !/^(?:[0-9a-fA-F]{2}){8}$/.test(refBlockHash) || !isPositiveSafeInteger(expiration) || !isPositiveSafeInteger(timestamp)) {
1603
+ throw new Error('TRON transaction intent requires a block reference');
1604
+ }
1605
+ return {
1606
+ expiration: expiration,
1607
+ refBlockBytes: refBlockBytes,
1608
+ refBlockHash: refBlockHash,
1609
+ timestamp: timestamp
1610
+ };
1611
+ };
1612
+ var operationBody = function(operation) {
1613
+ return {
1614
+ contractType: operation.contractType,
1615
+ data: operation.data,
1616
+ feeLimit: operation.feeLimit,
1617
+ from: operation.from,
1618
+ to: operation.to,
1619
+ value: operation.value
1620
+ };
1621
+ };
1622
+ /**
1623
+ * Rebuilds the `raw_data` hex of an approved TRON transaction intent. The
1624
+ * signed intent carries no block reference, so the caller supplies a fresh one
1625
+ * as `lifetimeValue`, a JSON object with `refBlockBytes` and `refBlockHash` as
1626
+ * hex and `expiration` and `timestamp` in milliseconds.
1627
+ */ var resumeTronTransactionSigningIntent = function(param) {
1628
+ var intent = param.intent, lifetimeValue = param.lifetimeValue;
1629
+ var operation = signedTronOperation(intent);
1630
+ var rawData = encodeTronRawData(operationBody(operation), parseBlockReference(lifetimeValue));
1631
+ var rebuilt = parseTronRawData(rawData);
1632
+ if (JSON.stringify(rebuilt) !== JSON.stringify(operationBody(operation))) {
1633
+ throw new Error('Stored TRON transaction intent is malformed');
1634
+ }
1635
+ return Buffer.from(rawData).toString('hex');
1636
+ };
1637
+
1147
1638
  exports.sdkApiCore = sdkApiCore__namespace;
1148
1639
  exports.DynamicTronWalletClient = DynamicTronWalletClient;
1149
1640
  exports.TRON_ADDRESS_PREFIX = TRON_ADDRESS_PREFIX;
@@ -1154,6 +1645,8 @@ exports.formatTronMessage = formatTronMessage;
1154
1645
  exports.getTronTxId = getTronTxId;
1155
1646
  exports.getTronTxIdHex = getTronTxIdHex;
1156
1647
  exports.isValidTronAddress = isValidTronAddress;
1648
+ exports.prepareTronTransactionSigningOperation = prepareTronTransactionSigningOperation;
1649
+ exports.resumeTronTransactionSigningIntent = resumeTronTransactionSigningIntent;
1157
1650
  exports.serializeECDSASignature = serializeECDSASignature;
1158
1651
  exports.toTronMessageBytes = toTronMessageBytes;
1159
1652
  exports.tronAddressToHex = tronAddressToHex;