address.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.TRON_BIP39_PATH_INDEX_0 = exports.TRON_BIP39_PATH_PREFIX = exports.ADDRESS_PREFIX_REGEX = exports.ADDRESS_PREFIX_BYTE = exports.ADDRESS_PREFIX = exports.ADDRESS_SIZE = void 0;
  4. exports.fromHex = fromHex;
  5. exports.toHex = toHex;
  6. exports.toChecksumAddress = toChecksumAddress;
  7. exports.isChecksumAddress = isChecksumAddress;
  8. exports.fromPrivateKey = fromPrivateKey;
  9. exports.isAddress = isAddress;
  10. const code_js_1 = require("./code.js");
  11. const crypto_js_1 = require("./crypto.js");
  12. const validations_js_1 = require("./validations.js");
  13. const ethersUtils_js_1 = require("./ethersUtils.js");
  14. exports.ADDRESS_SIZE = 34;
  15. exports.ADDRESS_PREFIX = '41';
  16. exports.ADDRESS_PREFIX_BYTE = 0x41;
  17. exports.ADDRESS_PREFIX_REGEX = /^(41)/;
  18. exports.TRON_BIP39_PATH_PREFIX = "m/44'/195'";
  19. exports.TRON_BIP39_PATH_INDEX_0 = exports.TRON_BIP39_PATH_PREFIX + "/0'/0/0";
  20. function fromHex(address) {
  21. if (!(0, validations_js_1.isHex)(address))
  22. return address;
  23. return (0, crypto_js_1.getBase58CheckAddress)((0, code_js_1.hexStr2byteArray)(address.replace(/^0x/, exports.ADDRESS_PREFIX)));
  24. }
  25. function toHex(address) {
  26. if ((0, validations_js_1.isHex)(address))
  27. return address.toLowerCase().replace(/^0x/, exports.ADDRESS_PREFIX);
  28. return (0, code_js_1.byteArray2hexStr)((0, crypto_js_1.decodeBase58Address)(address)).toLowerCase();
  29. }
  30. function getChecksumAddress(address) {
  31. address = address.toLowerCase();
  32. const chars = address.substring(2).split('');
  33. const expanded = new Uint8Array(40);
  34. for (let i = 0; i < 40; i++) {
  35. expanded[i] = chars[i].charCodeAt(0);
  36. }
  37. const hashed = (0, code_js_1.hexStr2byteArray)((0, ethersUtils_js_1.keccak256)(expanded).slice(2));
  38. for (let i = 0; i < 40; i += 2) {
  39. if ((hashed[i >> 1] >> 4) >= 8) {
  40. chars[i] = chars[i].toUpperCase();
  41. }
  42. if ((hashed[i >> 1] & 0x0f) >= 8) {
  43. chars[i + 1] = chars[i + 1].toUpperCase();
  44. }
  45. }
  46. return exports.ADDRESS_PREFIX + chars.join('');
  47. }
  48. function toChecksumAddress(address) {
  49. if (!isAddress(address))
  50. throw new Error(`'${address}' is not a valid address string`);
  51. return getChecksumAddress(toHex(address));
  52. }
  53. function isChecksumAddress(address) {
  54. if (!(0, validations_js_1.isHex)(address) || address.length !== 42)
  55. return false;
  56. try {
  57. return toChecksumAddress(address) === address;
  58. }
  59. catch {
  60. return false;
  61. }
  62. }
  63. function fromPrivateKey(privateKey, strict = false) {
  64. try {
  65. return (0, crypto_js_1.pkToAddress)(privateKey, strict);
  66. }
  67. catch {
  68. return false;
  69. }
  70. }
  71. function isAddress(address) {
  72. if (!address || !(0, validations_js_1.isString)(address))
  73. return false;
  74. // Convert HEX to Base58
  75. if (address.length === 42) {
  76. try {
  77. // it throws an error if the address starts with 0x
  78. return isAddress((0, crypto_js_1.getBase58CheckAddress)((0, code_js_1.hexStr2byteArray)(address)));
  79. }
  80. catch (err) {
  81. return false;
  82. }
  83. }
  84. try {
  85. return (0, crypto_js_1.isAddressValid)(address);
  86. }
  87. catch (err) {
  88. return false;
  89. }
  90. }
  91. //# sourceMappingURL=address.js.map