@dynamic-labs-wallet/tron 1.1.69 → 1.1.71

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
@@ -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,7 +256,7 @@ function _instanceof$1(left, right) {
256
256
  }
257
257
  };
258
258
 
259
- function _define_property$1(obj, key, value) {
259
+ function _define_property$2(obj, key, value) {
260
260
  if (key in obj) {
261
261
  Object.defineProperty(obj, key, {
262
262
  value: value,
@@ -269,7 +269,7 @@ function _define_property$1(obj, key, value) {
269
269
  }
270
270
  return obj;
271
271
  }
272
- function _object_spread$1(target) {
272
+ function _object_spread$2(target) {
273
273
  for(var i = 1; i < arguments.length; i++){
274
274
  var source = arguments[i] != null ? arguments[i] : {};
275
275
  var ownKeys = Object.keys(source);
@@ -279,7 +279,7 @@ function _object_spread$1(target) {
279
279
  }));
280
280
  }
281
281
  ownKeys.forEach(function(key) {
282
- _define_property$1(target, key, source[key]);
282
+ _define_property$2(target, key, source[key]);
283
283
  });
284
284
  }
285
285
  return target;
@@ -312,17 +312,17 @@ var resolveTronTransactionContext = function(param) {
312
312
  if ((context === null || context === void 0 ? void 0 : context.tronTransaction) !== undefined) {
313
313
  return context;
314
314
  }
315
- var base = _object_spread$1({}, context);
315
+ var base = _object_spread$2({}, context);
316
316
  delete base.rawSign;
317
317
  if (chainId !== undefined) {
318
- return _object_spread_props$1(_object_spread$1({}, base), {
318
+ return _object_spread_props$1(_object_spread$2({}, base), {
319
319
  tronTransaction: {
320
320
  chainId: normalizeTronChainId(chainId),
321
321
  rawDataHex: rawDataHex
322
322
  }
323
323
  });
324
324
  }
325
- return _object_spread_props$1(_object_spread$1({}, base), {
325
+ return _object_spread_props$1(_object_spread$2({}, base), {
326
326
  rawSign: {
327
327
  hashFunction: sdkApiCore.SignMessageRawSignHashFunctionEnum.Sha256,
328
328
  payload: rawDataHex
@@ -333,7 +333,7 @@ var resolveTronMessageContext = function(context, message) {
333
333
  if ((context === null || context === void 0 ? void 0 : context.tronMessage) !== undefined || (context === null || context === void 0 ? void 0 : context.rawSign) !== undefined) {
334
334
  return context;
335
335
  }
336
- return _object_spread_props$1(_object_spread$1({}, context), {
336
+ return _object_spread_props$1(_object_spread$2({}, context), {
337
337
  tronMessage: message
338
338
  });
339
339
  };
@@ -413,7 +413,7 @@ function _create_class(Constructor, protoProps, staticProps) {
413
413
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
414
414
  return Constructor;
415
415
  }
416
- function _define_property(obj, key, value) {
416
+ function _define_property$1(obj, key, value) {
417
417
  if (key in obj) {
418
418
  Object.defineProperty(obj, key, {
419
419
  value: value,
@@ -458,7 +458,7 @@ function _iterable_to_array(iter) {
458
458
  function _non_iterable_spread() {
459
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.");
460
460
  }
461
- function _object_spread(target) {
461
+ function _object_spread$1(target) {
462
462
  for(var i = 1; i < arguments.length; i++){
463
463
  var source = arguments[i] != null ? arguments[i] : {};
464
464
  var ownKeys = Object.keys(source);
@@ -468,7 +468,7 @@ function _object_spread(target) {
468
468
  }));
469
469
  }
470
470
  ownKeys.forEach(function(key) {
471
- _define_property(target, key, source[key]);
471
+ _define_property$1(target, key, source[key]);
472
472
  });
473
473
  }
474
474
  return target;
@@ -648,7 +648,7 @@ function _ts_generator(thisArg, body) {
648
648
  function DynamicTronWalletClient() {
649
649
  _class_call_check(this, DynamicTronWalletClient);
650
650
  var _this;
651
- _this = _call_super(this, DynamicTronWalletClient, arguments), _define_property(_this, "chainName", 'TRON');
651
+ _this = _call_super(this, DynamicTronWalletClient, arguments), _define_property$1(_this, "chainName", 'TRON');
652
652
  return _this;
653
653
  }
654
654
  _create_class(DynamicTronWalletClient, [
@@ -935,7 +935,7 @@ function _ts_generator(thisArg, body) {
935
935
  serializedSig = serializeECDSASignature(signatureEcdsa, _this.logger);
936
936
  hexSig = serializedSig.slice(2);
937
937
  existingSignatures = (_parsedTransaction_signature = parsedTransaction.signature) !== null && _parsedTransaction_signature !== void 0 ? _parsedTransaction_signature : [];
938
- signed = _object_spread_props(_object_spread({}, parsedTransaction), {
938
+ signed = _object_spread_props(_object_spread$1({}, parsedTransaction), {
939
939
  txID: (_parsedTransaction_txID = parsedTransaction.txID) !== null && _parsedTransaction_txID !== void 0 ? _parsedTransaction_txID : txid.slice(2),
940
940
  signature: _to_consumable_array(existingSignatures).concat([
941
941
  hexSig
@@ -1225,6 +1225,416 @@ function _ts_generator(thisArg, body) {
1225
1225
  return DynamicTronWalletClient;
1226
1226
  }(browser.DynamicWalletClient);
1227
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
+
1228
1638
  exports.sdkApiCore = sdkApiCore__namespace;
1229
1639
  exports.DynamicTronWalletClient = DynamicTronWalletClient;
1230
1640
  exports.TRON_ADDRESS_PREFIX = TRON_ADDRESS_PREFIX;
@@ -1235,6 +1645,8 @@ exports.formatTronMessage = formatTronMessage;
1235
1645
  exports.getTronTxId = getTronTxId;
1236
1646
  exports.getTronTxIdHex = getTronTxIdHex;
1237
1647
  exports.isValidTronAddress = isValidTronAddress;
1648
+ exports.prepareTronTransactionSigningOperation = prepareTronTransactionSigningOperation;
1649
+ exports.resumeTronTransactionSigningIntent = resumeTronTransactionSigningIntent;
1238
1650
  exports.serializeECDSASignature = serializeECDSASignature;
1239
1651
  exports.toTronMessageBytes = toTronMessageBytes;
1240
1652
  exports.tronAddressToHex = tronAddressToHex;
package/index.esm.js CHANGED
@@ -19,7 +19,7 @@ export { sdkApiCore };
19
19
  /**
20
20
  * Length of the encoded (un-prefixed) Base58Check TRON address.
21
21
  * `T...` addresses are always 34 characters.
22
- */ var TRON_ADDRESS_LENGTH = 34;
22
+ */ var TRON_ADDRESS_LENGTH$1 = 34;
23
23
  /**
24
24
  * Derive a TRON Base58Check ("T...") address from a secp256k1 public key.
25
25
  *
@@ -69,7 +69,7 @@ var deriveTronAddress = function(param) {
69
69
  * `0x...` addresses, malformed Base58, and addresses whose checksum was
70
70
  * tampered with.
71
71
  */ var isValidTronAddress = function(address) {
72
- if (typeof address !== 'string' || address.length !== TRON_ADDRESS_LENGTH) {
72
+ if (typeof address !== 'string' || address.length !== TRON_ADDRESS_LENGTH$1) {
73
73
  return false;
74
74
  }
75
75
  var decoded;
@@ -237,7 +237,7 @@ function _instanceof$1(left, right) {
237
237
  }
238
238
  };
239
239
 
240
- function _define_property$1(obj, key, value) {
240
+ function _define_property$2(obj, key, value) {
241
241
  if (key in obj) {
242
242
  Object.defineProperty(obj, key, {
243
243
  value: value,
@@ -250,7 +250,7 @@ function _define_property$1(obj, key, value) {
250
250
  }
251
251
  return obj;
252
252
  }
253
- function _object_spread$1(target) {
253
+ function _object_spread$2(target) {
254
254
  for(var i = 1; i < arguments.length; i++){
255
255
  var source = arguments[i] != null ? arguments[i] : {};
256
256
  var ownKeys = Object.keys(source);
@@ -260,7 +260,7 @@ function _object_spread$1(target) {
260
260
  }));
261
261
  }
262
262
  ownKeys.forEach(function(key) {
263
- _define_property$1(target, key, source[key]);
263
+ _define_property$2(target, key, source[key]);
264
264
  });
265
265
  }
266
266
  return target;
@@ -293,17 +293,17 @@ var resolveTronTransactionContext = function(param) {
293
293
  if ((context === null || context === void 0 ? void 0 : context.tronTransaction) !== undefined) {
294
294
  return context;
295
295
  }
296
- var base = _object_spread$1({}, context);
296
+ var base = _object_spread$2({}, context);
297
297
  delete base.rawSign;
298
298
  if (chainId !== undefined) {
299
- return _object_spread_props$1(_object_spread$1({}, base), {
299
+ return _object_spread_props$1(_object_spread$2({}, base), {
300
300
  tronTransaction: {
301
301
  chainId: normalizeTronChainId(chainId),
302
302
  rawDataHex: rawDataHex
303
303
  }
304
304
  });
305
305
  }
306
- return _object_spread_props$1(_object_spread$1({}, base), {
306
+ return _object_spread_props$1(_object_spread$2({}, base), {
307
307
  rawSign: {
308
308
  hashFunction: SignMessageRawSignHashFunctionEnum.Sha256,
309
309
  payload: rawDataHex
@@ -314,7 +314,7 @@ var resolveTronMessageContext = function(context, message) {
314
314
  if ((context === null || context === void 0 ? void 0 : context.tronMessage) !== undefined || (context === null || context === void 0 ? void 0 : context.rawSign) !== undefined) {
315
315
  return context;
316
316
  }
317
- return _object_spread_props$1(_object_spread$1({}, context), {
317
+ return _object_spread_props$1(_object_spread$2({}, context), {
318
318
  tronMessage: message
319
319
  });
320
320
  };
@@ -394,7 +394,7 @@ function _create_class(Constructor, protoProps, staticProps) {
394
394
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
395
395
  return Constructor;
396
396
  }
397
- function _define_property(obj, key, value) {
397
+ function _define_property$1(obj, key, value) {
398
398
  if (key in obj) {
399
399
  Object.defineProperty(obj, key, {
400
400
  value: value,
@@ -439,7 +439,7 @@ function _iterable_to_array(iter) {
439
439
  function _non_iterable_spread() {
440
440
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
441
441
  }
442
- function _object_spread(target) {
442
+ function _object_spread$1(target) {
443
443
  for(var i = 1; i < arguments.length; i++){
444
444
  var source = arguments[i] != null ? arguments[i] : {};
445
445
  var ownKeys = Object.keys(source);
@@ -449,7 +449,7 @@ function _object_spread(target) {
449
449
  }));
450
450
  }
451
451
  ownKeys.forEach(function(key) {
452
- _define_property(target, key, source[key]);
452
+ _define_property$1(target, key, source[key]);
453
453
  });
454
454
  }
455
455
  return target;
@@ -629,7 +629,7 @@ function _ts_generator(thisArg, body) {
629
629
  function DynamicTronWalletClient() {
630
630
  _class_call_check(this, DynamicTronWalletClient);
631
631
  var _this;
632
- _this = _call_super(this, DynamicTronWalletClient, arguments), _define_property(_this, "chainName", 'TRON');
632
+ _this = _call_super(this, DynamicTronWalletClient, arguments), _define_property$1(_this, "chainName", 'TRON');
633
633
  return _this;
634
634
  }
635
635
  _create_class(DynamicTronWalletClient, [
@@ -916,7 +916,7 @@ function _ts_generator(thisArg, body) {
916
916
  serializedSig = serializeECDSASignature(signatureEcdsa, _this.logger);
917
917
  hexSig = serializedSig.slice(2);
918
918
  existingSignatures = (_parsedTransaction_signature = parsedTransaction.signature) !== null && _parsedTransaction_signature !== void 0 ? _parsedTransaction_signature : [];
919
- signed = _object_spread_props(_object_spread({}, parsedTransaction), {
919
+ signed = _object_spread_props(_object_spread$1({}, parsedTransaction), {
920
920
  txID: (_parsedTransaction_txID = parsedTransaction.txID) !== null && _parsedTransaction_txID !== void 0 ? _parsedTransaction_txID : txid.slice(2),
921
921
  signature: _to_consumable_array(existingSignatures).concat([
922
922
  hexSig
@@ -1206,4 +1206,414 @@ function _ts_generator(thisArg, body) {
1206
1206
  return DynamicTronWalletClient;
1207
1207
  }(DynamicWalletClient);
1208
1208
 
1209
- export { DynamicTronWalletClient, TRON_ADDRESS_PREFIX, TRON_SIGN_MESSAGE_PREFIX, decodeTronAddress, deriveTronAddress, formatTronMessage, getTronTxId, getTronTxIdHex, isValidTronAddress, serializeECDSASignature, toTronMessageBytes, tronAddressToHex };
1209
+ var TRON_ADDRESS_LENGTH = 21;
1210
+ var CHECKSUM_LENGTH = 4;
1211
+ var MAX_RAW_DATA_BYTES = 102400;
1212
+ var MAX_VARINT_BYTES = 10;
1213
+ var TRANSFER_CONTRACT = 1n;
1214
+ var TRIGGER_SMART_CONTRACT = 31n;
1215
+ var TYPE_URLS = {
1216
+ TransferContract: 'type.googleapis.com/protocol.TransferContract',
1217
+ TriggerSmartContract: 'type.googleapis.com/protocol.TriggerSmartContract'
1218
+ };
1219
+ var CONTRACT_TYPES = {
1220
+ TransferContract: TRANSFER_CONTRACT,
1221
+ TriggerSmartContract: TRIGGER_SMART_CONTRACT
1222
+ };
1223
+ var readVarint = function(bytes, start) {
1224
+ var value = 0n;
1225
+ for(var index = 0; index < MAX_VARINT_BYTES; index += 1){
1226
+ var byte = bytes[start + index];
1227
+ if (byte === undefined) return undefined;
1228
+ value |= BigInt(byte & 0x7f) << BigInt(7 * index);
1229
+ if ((byte & 0x80) === 0) return {
1230
+ next: start + index + 1,
1231
+ value: value
1232
+ };
1233
+ }
1234
+ return undefined;
1235
+ };
1236
+ // Only varint and length-delimited fields: every field this parser accepts is
1237
+ // one of the two, so any other wire type is rejected rather than skipped.
1238
+ var readField = function(bytes, position) {
1239
+ var key = readVarint(bytes, position);
1240
+ if (!key) return undefined;
1241
+ var number = Number(key.value >> 3n);
1242
+ var wireType = Number(key.value & 7n);
1243
+ if (number === 0) return undefined;
1244
+ if (wireType === 0) {
1245
+ var varint = readVarint(bytes, key.next);
1246
+ return varint && {
1247
+ field: {
1248
+ number: number,
1249
+ varint: varint.value
1250
+ },
1251
+ next: varint.next
1252
+ };
1253
+ }
1254
+ if (wireType === 2) {
1255
+ var length = readVarint(bytes, key.next);
1256
+ if (!length || length.value > BigInt(bytes.length - length.next)) return undefined;
1257
+ var end = length.next + Number(length.value);
1258
+ return {
1259
+ field: {
1260
+ bytes: bytes.subarray(length.next, end),
1261
+ number: number
1262
+ },
1263
+ next: end
1264
+ };
1265
+ }
1266
+ return undefined;
1267
+ };
1268
+ var readFields = function(bytes) {
1269
+ var fields = [];
1270
+ var position = 0;
1271
+ while(position < bytes.length){
1272
+ var read = readField(bytes, position);
1273
+ if (!read) return undefined;
1274
+ fields.push(read.field);
1275
+ position = read.next;
1276
+ }
1277
+ return fields;
1278
+ };
1279
+ // A field outside `allowed`, of the wrong kind or repeated would be signed but
1280
+ // not described by the intent, so the whole message is rejected.
1281
+ var readStrict = function(bytes, allowed) {
1282
+ var fields = readFields(bytes);
1283
+ if (!fields) return undefined;
1284
+ var byNumber = new Map();
1285
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1286
+ try {
1287
+ for(var _iterator = fields[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1288
+ var field = _step.value;
1289
+ var kind = allowed[field.number];
1290
+ if (kind === undefined || byNumber.has(field.number)) return undefined;
1291
+ if (kind === 'bytes' ? !field.bytes : field.varint === undefined) return undefined;
1292
+ byNumber.set(field.number, field);
1293
+ }
1294
+ } catch (err) {
1295
+ _didIteratorError = true;
1296
+ _iteratorError = err;
1297
+ } finally{
1298
+ try {
1299
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1300
+ _iterator.return();
1301
+ }
1302
+ } finally{
1303
+ if (_didIteratorError) {
1304
+ throw _iteratorError;
1305
+ }
1306
+ }
1307
+ }
1308
+ return byNumber;
1309
+ };
1310
+ var concat = function(parts) {
1311
+ var result = new Uint8Array(parts.reduce(function(total, part) {
1312
+ return total + part.length;
1313
+ }, 0));
1314
+ var offset = 0;
1315
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1316
+ try {
1317
+ for(var _iterator = parts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1318
+ var part = _step.value;
1319
+ result.set(part, offset);
1320
+ offset += part.length;
1321
+ }
1322
+ } catch (err) {
1323
+ _didIteratorError = true;
1324
+ _iteratorError = err;
1325
+ } finally{
1326
+ try {
1327
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1328
+ _iterator.return();
1329
+ }
1330
+ } finally{
1331
+ if (_didIteratorError) {
1332
+ throw _iteratorError;
1333
+ }
1334
+ }
1335
+ }
1336
+ return result;
1337
+ };
1338
+ var EMPTY = new Uint8Array(0);
1339
+ var checksumOf = function(payload) {
1340
+ return sha256(sha256(payload)).subarray(0, CHECKSUM_LENGTH);
1341
+ };
1342
+ var encodeAddress = function(bytes) {
1343
+ if ((bytes === null || bytes === void 0 ? void 0 : bytes.length) !== TRON_ADDRESS_LENGTH || bytes[0] !== TRON_ADDRESS_PREFIX) return undefined;
1344
+ return bs58.encode(concat([
1345
+ bytes,
1346
+ checksumOf(bytes)
1347
+ ]));
1348
+ };
1349
+ var decodeAddressBytes = function(address) {
1350
+ var decoded = bs58.decode(address);
1351
+ var payload = decoded.subarray(0, TRON_ADDRESS_LENGTH);
1352
+ var checksum = decoded.subarray(TRON_ADDRESS_LENGTH);
1353
+ if (decoded.length !== TRON_ADDRESS_LENGTH + CHECKSUM_LENGTH || payload[0] !== TRON_ADDRESS_PREFIX || Buffer.compare(checksum, checksumOf(payload)) !== 0) {
1354
+ throw new Error('Invalid TRON address');
1355
+ }
1356
+ return payload;
1357
+ };
1358
+ var toHex = function(bytes) {
1359
+ return "0x".concat(Buffer.from(bytes !== null && bytes !== void 0 ? bytes : []).toString('hex'));
1360
+ };
1361
+ var hexBytes = function(hex) {
1362
+ return Uint8Array.from(Buffer.from(hex.replace(/^0x/, ''), 'hex'));
1363
+ };
1364
+ /**
1365
+ * Reads a TRON transaction's `raw_data` into the fields a signing intent
1366
+ * describes. Returns `undefined` for anything it cannot describe completely:
1367
+ * unknown, repeated or mistyped fields at any level, anything but one
1368
+ * TransferContract or TriggerSmartContract, a malformed address.
1369
+ */ var parseTronRawData = function(rawData) {
1370
+ var _raw_get, _contract_get, _contract_get1, _parameter_get, _parameter_get1, _value_get, _value_get1, _value_get2, _raw_get1, _value_get3;
1371
+ if (rawData.length === 0 || rawData.length > MAX_RAW_DATA_BYTES) return undefined;
1372
+ var raw = readStrict(rawData, {
1373
+ 1: 'bytes',
1374
+ 3: 'varint',
1375
+ 4: 'bytes',
1376
+ 8: 'varint',
1377
+ 11: 'bytes',
1378
+ 14: 'varint',
1379
+ 18: 'varint'
1380
+ });
1381
+ var contractBytes = raw === null || raw === void 0 ? void 0 : (_raw_get = raw.get(11)) === null || _raw_get === void 0 ? void 0 : _raw_get.bytes;
1382
+ if (!raw || !contractBytes) return undefined;
1383
+ var contract = readStrict(contractBytes, {
1384
+ 1: 'varint',
1385
+ 2: 'bytes'
1386
+ });
1387
+ var type = contract === null || contract === void 0 ? void 0 : (_contract_get = contract.get(1)) === null || _contract_get === void 0 ? void 0 : _contract_get.varint;
1388
+ var parameterBytes = contract === null || contract === void 0 ? void 0 : (_contract_get1 = contract.get(2)) === null || _contract_get1 === void 0 ? void 0 : _contract_get1.bytes;
1389
+ if (type === undefined || !parameterBytes) return undefined;
1390
+ var parameter = readStrict(parameterBytes, {
1391
+ 1: 'bytes',
1392
+ 2: 'bytes'
1393
+ });
1394
+ var typeUrl = parameter === null || parameter === void 0 ? void 0 : (_parameter_get = parameter.get(1)) === null || _parameter_get === void 0 ? void 0 : _parameter_get.bytes;
1395
+ var valueBytes = parameter === null || parameter === void 0 ? void 0 : (_parameter_get1 = parameter.get(2)) === null || _parameter_get1 === void 0 ? void 0 : _parameter_get1.bytes;
1396
+ if (!typeUrl || !valueBytes) return undefined;
1397
+ var contractType = Object.keys(TYPE_URLS).find(function(name) {
1398
+ return CONTRACT_TYPES[name] === type && TYPE_URLS[name] === Buffer.from(typeUrl).toString('utf8');
1399
+ });
1400
+ if (!contractType) return undefined;
1401
+ var isTransfer = contractType === 'TransferContract';
1402
+ var value = readStrict(valueBytes, isTransfer ? {
1403
+ 1: 'bytes',
1404
+ 2: 'bytes',
1405
+ 3: 'varint'
1406
+ } : {
1407
+ 1: 'bytes',
1408
+ 2: 'bytes',
1409
+ 3: 'varint',
1410
+ 4: 'bytes'
1411
+ });
1412
+ 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);
1413
+ 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);
1414
+ if (!value || !from || !to) return undefined;
1415
+ var _raw_get_varint, _value_get_varint;
1416
+ return {
1417
+ contractType: contractType,
1418
+ data: isTransfer ? '0x' : toHex((_value_get2 = value.get(4)) === null || _value_get2 === void 0 ? void 0 : _value_get2.bytes),
1419
+ 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(),
1420
+ from: from,
1421
+ to: to,
1422
+ 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()
1423
+ };
1424
+ };
1425
+ var varint = function(value) {
1426
+ var bytes = [];
1427
+ var rest = value;
1428
+ while(rest >= 0x80n){
1429
+ bytes.push(Number(rest & 0x7fn) | 0x80);
1430
+ rest >>= 7n;
1431
+ }
1432
+ bytes.push(Number(rest));
1433
+ return Uint8Array.from(bytes);
1434
+ };
1435
+ var bytesField = function(number, value) {
1436
+ return concat([
1437
+ varint(BigInt(number << 3 | 2)),
1438
+ varint(BigInt(value.length)),
1439
+ value
1440
+ ]);
1441
+ };
1442
+ // proto3 leaves a zero or empty field out.
1443
+ var optionalVarintField = function(number, value) {
1444
+ return value === 0n ? EMPTY : concat([
1445
+ varint(BigInt(number << 3)),
1446
+ varint(value)
1447
+ ]);
1448
+ };
1449
+ var optionalBytesField = function(number, value) {
1450
+ return value.length === 0 ? EMPTY : bytesField(number, value);
1451
+ };
1452
+ var encodeContract = function(body) {
1453
+ var owner = bytesField(1, decodeAddressBytes(body.from));
1454
+ var target = bytesField(2, decodeAddressBytes(body.to));
1455
+ var value = optionalVarintField(3, BigInt(body.value));
1456
+ var contractValue = body.contractType === 'TransferContract' ? concat([
1457
+ owner,
1458
+ target,
1459
+ value
1460
+ ]) : concat([
1461
+ owner,
1462
+ target,
1463
+ value,
1464
+ optionalBytesField(4, hexBytes(body.data))
1465
+ ]);
1466
+ var typeUrl = Uint8Array.from(Buffer.from(TYPE_URLS[body.contractType]));
1467
+ return concat([
1468
+ optionalVarintField(1, CONTRACT_TYPES[body.contractType]),
1469
+ bytesField(2, concat([
1470
+ bytesField(1, typeUrl),
1471
+ bytesField(2, contractValue)
1472
+ ]))
1473
+ ]);
1474
+ };
1475
+ /**
1476
+ * Serializes a transaction's `raw_data` from the described body and a block
1477
+ * reference, with the fields in field-number order as a TRON node does.
1478
+ */ var encodeTronRawData = function(body, reference) {
1479
+ return concat([
1480
+ bytesField(1, hexBytes(reference.refBlockBytes)),
1481
+ bytesField(4, hexBytes(reference.refBlockHash)),
1482
+ optionalVarintField(8, BigInt(reference.expiration)),
1483
+ bytesField(11, encodeContract(body)),
1484
+ optionalVarintField(14, BigInt(reference.timestamp)),
1485
+ optionalVarintField(18, BigInt(body.feeLimit))
1486
+ ]);
1487
+ };
1488
+
1489
+ function _define_property(obj, key, value) {
1490
+ if (key in obj) {
1491
+ Object.defineProperty(obj, key, {
1492
+ value: value,
1493
+ enumerable: true,
1494
+ configurable: true,
1495
+ writable: true
1496
+ });
1497
+ } else {
1498
+ obj[key] = value;
1499
+ }
1500
+ return obj;
1501
+ }
1502
+ function _object_spread(target) {
1503
+ for(var i = 1; i < arguments.length; i++){
1504
+ var source = arguments[i] != null ? arguments[i] : {};
1505
+ var ownKeys = Object.keys(source);
1506
+ if (typeof Object.getOwnPropertySymbols === "function") {
1507
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
1508
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
1509
+ }));
1510
+ }
1511
+ ownKeys.forEach(function(key) {
1512
+ _define_property(target, key, source[key]);
1513
+ });
1514
+ }
1515
+ return target;
1516
+ }
1517
+ var isHexBytes = function(value) {
1518
+ return /^(?:[0-9a-fA-F]{2})+$/.test(value);
1519
+ };
1520
+ var DECIMAL_CHAIN_ID = /^(0|[1-9]\d*)$/;
1521
+ // A hexadecimal id is accepted as the TRON client does, and described in decimal.
1522
+ var assertNetworkChainId = function(chainId) {
1523
+ var networkChainId = chainId && normalizeTronChainId(chainId);
1524
+ if (!networkChainId || !DECIMAL_CHAIN_ID.test(networkChainId)) {
1525
+ throw new Error('TRON signing intent requires a network chainId');
1526
+ }
1527
+ return networkChainId;
1528
+ };
1529
+ // The TRON client takes the transaction as JSON holding `raw_data_hex`; the bare hex is accepted too.
1530
+ var rawDataHexOf = function(transaction) {
1531
+ var trimmed = transaction.trim();
1532
+ if (!trimmed.startsWith('{')) return trimmed.replace(/^0x/, '');
1533
+ try {
1534
+ var _JSON_parse = JSON.parse(trimmed), rawDataHex = _JSON_parse.raw_data_hex;
1535
+ if (typeof rawDataHex === 'string') return rawDataHex.replace(/^0x/, '');
1536
+ } catch (e) {
1537
+ // Not JSON: rejected below as not hex.
1538
+ }
1539
+ return trimmed;
1540
+ };
1541
+ /**
1542
+ * Describes a TRON transaction for a signing-quorum intent from its `raw_data`
1543
+ * hex, bare or inside the transaction JSON the TRON client signs. The reference block, expiration and timestamp are not part of the
1544
+ * description: they may be refreshed when the approved intent is resumed.
1545
+ */ var prepareTronTransactionSigningOperation = function(param) {
1546
+ var transaction = param.transaction, accountAddress = param.accountAddress, chainId = param.chainId;
1547
+ var networkChainId = assertNetworkChainId(chainId);
1548
+ var rawDataHex = rawDataHexOf(transaction);
1549
+ if (!isHexBytes(rawDataHex)) throw new Error('TRON signing intent requires a hex raw_data');
1550
+ var body = parseTronRawData(Uint8Array.from(Buffer.from(rawDataHex, 'hex')));
1551
+ if (!body) throw new Error('TRON signing intent supports one TRX transfer or smart-contract call');
1552
+ if (body.from !== accountAddress) throw new Error('TRON transaction owner must be the signing account');
1553
+ return {
1554
+ operation: _object_spread({
1555
+ kind: 'transaction',
1556
+ chainName: 'TRON',
1557
+ chainId: networkChainId
1558
+ }, body),
1559
+ lifetime: {
1560
+ refreshable: 'expiration'
1561
+ }
1562
+ };
1563
+ };
1564
+ var signedTronOperation = function(intent) {
1565
+ var _intent_lifetime;
1566
+ var operation = intent.operation;
1567
+ if (operation.kind !== 'transaction' || operation.chainName !== 'TRON' || ((_intent_lifetime = intent.lifetime) === null || _intent_lifetime === void 0 ? void 0 : _intent_lifetime.refreshable) !== 'expiration') {
1568
+ throw new Error('Stored signing intent is not a TRON transaction');
1569
+ }
1570
+ return operation;
1571
+ };
1572
+ var isPositiveSafeInteger = function(value) {
1573
+ return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
1574
+ };
1575
+ var parseBlockReference = function(lifetimeValue) {
1576
+ var parsed;
1577
+ try {
1578
+ parsed = lifetimeValue ? JSON.parse(lifetimeValue) : undefined;
1579
+ } catch (e) {
1580
+ parsed = undefined;
1581
+ }
1582
+ var _ref = parsed !== null && parsed !== void 0 ? parsed : {}, expiration = _ref.expiration, refBlockBytes = _ref.refBlockBytes, refBlockHash = _ref.refBlockHash, timestamp = _ref.timestamp;
1583
+ 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)) {
1584
+ throw new Error('TRON transaction intent requires a block reference');
1585
+ }
1586
+ return {
1587
+ expiration: expiration,
1588
+ refBlockBytes: refBlockBytes,
1589
+ refBlockHash: refBlockHash,
1590
+ timestamp: timestamp
1591
+ };
1592
+ };
1593
+ var operationBody = function(operation) {
1594
+ return {
1595
+ contractType: operation.contractType,
1596
+ data: operation.data,
1597
+ feeLimit: operation.feeLimit,
1598
+ from: operation.from,
1599
+ to: operation.to,
1600
+ value: operation.value
1601
+ };
1602
+ };
1603
+ /**
1604
+ * Rebuilds the `raw_data` hex of an approved TRON transaction intent. The
1605
+ * signed intent carries no block reference, so the caller supplies a fresh one
1606
+ * as `lifetimeValue`, a JSON object with `refBlockBytes` and `refBlockHash` as
1607
+ * hex and `expiration` and `timestamp` in milliseconds.
1608
+ */ var resumeTronTransactionSigningIntent = function(param) {
1609
+ var intent = param.intent, lifetimeValue = param.lifetimeValue;
1610
+ var operation = signedTronOperation(intent);
1611
+ var rawData = encodeTronRawData(operationBody(operation), parseBlockReference(lifetimeValue));
1612
+ var rebuilt = parseTronRawData(rawData);
1613
+ if (JSON.stringify(rebuilt) !== JSON.stringify(operationBody(operation))) {
1614
+ throw new Error('Stored TRON transaction intent is malformed');
1615
+ }
1616
+ return Buffer.from(rawData).toString('hex');
1617
+ };
1618
+
1619
+ export { DynamicTronWalletClient, TRON_ADDRESS_PREFIX, TRON_SIGN_MESSAGE_PREFIX, decodeTronAddress, deriveTronAddress, formatTronMessage, getTronTxId, getTronTxIdHex, isValidTronAddress, prepareTronTransactionSigningOperation, resumeTronTransactionSigningIntent, serializeECDSASignature, toTronMessageBytes, tronAddressToHex };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@dynamic-labs-wallet/tron",
3
- "version": "1.1.69",
3
+ "version": "1.1.71",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "dependencies": {
7
- "@dynamic-labs-wallet/browser": "1.1.69",
7
+ "@dynamic-labs-wallet/browser": "1.1.71",
8
8
  "@dynamic-labs/sdk-api-core": "0.167.0",
9
9
  "@noble/hashes": "1.7.1",
10
10
  "bs58": "^6.0.0"
package/src/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { DynamicTronWalletClient } from './client/index.js';
2
2
  export { TRON_ADDRESS_PREFIX, deriveTronAddress, isValidTronAddress, decodeTronAddress, tronAddressToHex, TRON_SIGN_MESSAGE_PREFIX, toTronMessageBytes, formatTronMessage, getTronTxId, getTronTxIdHex, serializeECDSASignature, } from './utils/index.js';
3
+ export { prepareTronTransactionSigningOperation, resumeTronTransactionSigningIntent } from './signingIntent.js';
3
4
  export type { TronTransaction } from './types.js';
4
5
  export * as sdkApiCore from '@dynamic-labs/sdk-api-core';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,wBAAwB,EACxB,kBAAkB,EAClB,iBAAiB,EACjB,WAAW,EACX,cAAc,EACd,uBAAuB,GACxB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,KAAK,UAAU,MAAM,4BAA4B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,wBAAwB,EACxB,kBAAkB,EAClB,iBAAiB,EACjB,WAAW,EACX,cAAc,EACd,uBAAuB,GACxB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,sCAAsC,EAAE,kCAAkC,EAAE,MAAM,oBAAoB,CAAC;AAChH,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,KAAK,UAAU,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,22 @@
1
+ import type { PreparedTronTransactionSigningIntent, SigningIntent } from '@dynamic-labs-wallet/browser';
2
+ /**
3
+ * Describes a TRON transaction for a signing-quorum intent from its `raw_data`
4
+ * hex, bare or inside the transaction JSON the TRON client signs. The reference block, expiration and timestamp are not part of the
5
+ * description: they may be refreshed when the approved intent is resumed.
6
+ */
7
+ export declare const prepareTronTransactionSigningOperation: ({ transaction, accountAddress, chainId, }: {
8
+ transaction: string;
9
+ accountAddress: string;
10
+ chainId: string | undefined;
11
+ }) => PreparedTronTransactionSigningIntent;
12
+ /**
13
+ * Rebuilds the `raw_data` hex of an approved TRON transaction intent. The
14
+ * signed intent carries no block reference, so the caller supplies a fresh one
15
+ * as `lifetimeValue`, a JSON object with `refBlockBytes` and `refBlockHash` as
16
+ * hex and `expiration` and `timestamp` in milliseconds.
17
+ */
18
+ export declare const resumeTronTransactionSigningIntent: ({ intent, lifetimeValue, }: {
19
+ intent: SigningIntent;
20
+ lifetimeValue: string | undefined;
21
+ }) => string;
22
+ //# sourceMappingURL=signingIntent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signingIntent.d.ts","sourceRoot":"","sources":["../../packages/src/signingIntent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oCAAoC,EACpC,aAAa,EAEd,MAAM,8BAA8B,CAAC;AA8BtC;;;;GAIG;AACH,eAAO,MAAM,sCAAsC,8CAIhD;IACD,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;CAC7B,KAAG,oCAaH,CAAC;AA+CF;;;;;GAKG;AACH,eAAO,MAAM,kCAAkC,+BAG5C;IACD,MAAM,EAAE,aAAa,CAAC;IACtB,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC,KAAG,MASH,CAAC"}
@@ -0,0 +1,34 @@
1
+ declare const TYPE_URLS: {
2
+ readonly TransferContract: "type.googleapis.com/protocol.TransferContract";
3
+ readonly TriggerSmartContract: "type.googleapis.com/protocol.TriggerSmartContract";
4
+ };
5
+ export type TronContractType = keyof typeof TYPE_URLS;
6
+ /** What a TRON transaction signing intent describes. The reference block, expiration and timestamp may be refreshed. */
7
+ export type TronRawDataBody = {
8
+ contractType: TronContractType;
9
+ data: string;
10
+ feeLimit: string;
11
+ from: string;
12
+ to: string;
13
+ value: string;
14
+ };
15
+ export type TronBlockReference = {
16
+ expiration: number;
17
+ refBlockBytes: string;
18
+ refBlockHash: string;
19
+ timestamp: number;
20
+ };
21
+ /**
22
+ * Reads a TRON transaction's `raw_data` into the fields a signing intent
23
+ * describes. Returns `undefined` for anything it cannot describe completely:
24
+ * unknown, repeated or mistyped fields at any level, anything but one
25
+ * TransferContract or TriggerSmartContract, a malformed address.
26
+ */
27
+ export declare const parseTronRawData: (rawData: Uint8Array) => TronRawDataBody | undefined;
28
+ /**
29
+ * Serializes a transaction's `raw_data` from the described body and a block
30
+ * reference, with the fields in field-number order as a TRON node does.
31
+ */
32
+ export declare const encodeTronRawData: (body: TronRawDataBody, reference: TronBlockReference) => Uint8Array;
33
+ export {};
34
+ //# sourceMappingURL=tronRawData.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tronRawData.d.ts","sourceRoot":"","sources":["../../packages/src/tronRawData.ts"],"names":[],"mappings":"AAYA,QAAA,MAAM,SAAS;;;CAGL,CAAC;AAUX,MAAM,MAAM,gBAAgB,GAAG,MAAM,OAAO,SAAS,CAAC;AAEtD,wHAAwH;AACxH,MAAM,MAAM,eAAe,GAAG;IAC5B,YAAY,EAAE,gBAAgB,CAAC;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAmGF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,YAAa,UAAU,KAAG,eAAe,GAAG,SA8CxE,CAAC;AAsCF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,SAAU,eAAe,aAAa,kBAAkB,KAAG,UAQrF,CAAC"}