Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.isValidXAddress = exports.decodeXAddress = exports.xAddressToClassicAddress = exports.encodeXAddress = exports.classicAddressToXAddress = exports.isValidClassicAddress = exports.decodeAccountPublic = exports.encodeAccountPublic = exports.decodeNodePublic = exports.encodeNodePublic = exports.decodeAccountID = exports.encodeAccountID = exports.decodeSeed = exports.encodeSeed = exports.codec = void 0;
  27. const assert = __importStar(require("assert"));
  28. const xrp_codec_1 = require("./xrp-codec");
  29. Object.defineProperty(exports, "codec", { enumerable: true, get: function () { return xrp_codec_1.codec; } });
  30. Object.defineProperty(exports, "encodeSeed", { enumerable: true, get: function () { return xrp_codec_1.encodeSeed; } });
  31. Object.defineProperty(exports, "decodeSeed", { enumerable: true, get: function () { return xrp_codec_1.decodeSeed; } });
  32. Object.defineProperty(exports, "encodeAccountID", { enumerable: true, get: function () { return xrp_codec_1.encodeAccountID; } });
  33. Object.defineProperty(exports, "decodeAccountID", { enumerable: true, get: function () { return xrp_codec_1.decodeAccountID; } });
  34. Object.defineProperty(exports, "encodeNodePublic", { enumerable: true, get: function () { return xrp_codec_1.encodeNodePublic; } });
  35. Object.defineProperty(exports, "decodeNodePublic", { enumerable: true, get: function () { return xrp_codec_1.decodeNodePublic; } });
  36. Object.defineProperty(exports, "encodeAccountPublic", { enumerable: true, get: function () { return xrp_codec_1.encodeAccountPublic; } });
  37. Object.defineProperty(exports, "decodeAccountPublic", { enumerable: true, get: function () { return xrp_codec_1.decodeAccountPublic; } });
  38. Object.defineProperty(exports, "isValidClassicAddress", { enumerable: true, get: function () { return xrp_codec_1.isValidClassicAddress; } });
  39. const PREFIX_BYTES = {
  40. // 5, 68
  41. main: Buffer.from([0x05, 0x44]),
  42. // 4, 147
  43. test: Buffer.from([0x04, 0x93]),
  44. };
  45. const MAX_32_BIT_UNSIGNED_INT = 4294967295;
  46. function classicAddressToXAddress(classicAddress, tag, test) {
  47. const accountId = (0, xrp_codec_1.decodeAccountID)(classicAddress);
  48. return encodeXAddress(accountId, tag, test);
  49. }
  50. exports.classicAddressToXAddress = classicAddressToXAddress;
  51. function encodeXAddress(accountId, tag, test) {
  52. if (accountId.length !== 20) {
  53. // RIPEMD160 is 160 bits = 20 bytes
  54. throw new Error('Account ID must be 20 bytes');
  55. }
  56. if (tag > MAX_32_BIT_UNSIGNED_INT) {
  57. throw new Error('Invalid tag');
  58. }
  59. const theTag = tag || 0;
  60. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Passing null is a common js mistake
  61. const flag = tag === false || tag == null ? 0 : 1;
  62. /* eslint-disable no-bitwise ---
  63. * need to use bitwise operations here */
  64. const bytes = Buffer.concat([
  65. test ? PREFIX_BYTES.test : PREFIX_BYTES.main,
  66. accountId,
  67. Buffer.from([
  68. // 0x00 if no tag, 0x01 if 32-bit tag
  69. flag,
  70. // first byte
  71. theTag & 0xff,
  72. // second byte
  73. (theTag >> 8) & 0xff,
  74. // third byte
  75. (theTag >> 16) & 0xff,
  76. // fourth byte
  77. (theTag >> 24) & 0xff,
  78. 0,
  79. 0,
  80. 0,
  81. // four zero bytes (reserved for 64-bit tags)
  82. 0,
  83. ]),
  84. ]);
  85. /* eslint-enable no-bitwise */
  86. return xrp_codec_1.codec.encodeChecked(bytes);
  87. }
  88. exports.encodeXAddress = encodeXAddress;
  89. function xAddressToClassicAddress(xAddress) {
  90. /* eslint-disable @typescript-eslint/naming-convention --
  91. * TODO 'test' should be something like 'isTest', do this later
  92. */
  93. const { accountId, tag, test } = decodeXAddress(xAddress);
  94. /* eslint-enable @typescript-eslint/naming-convention */
  95. const classicAddress = (0, xrp_codec_1.encodeAccountID)(accountId);
  96. return {
  97. classicAddress,
  98. tag,
  99. test,
  100. };
  101. }
  102. exports.xAddressToClassicAddress = xAddressToClassicAddress;
  103. function decodeXAddress(xAddress) {
  104. const decoded = xrp_codec_1.codec.decodeChecked(xAddress);
  105. /* eslint-disable @typescript-eslint/naming-convention --
  106. * TODO 'test' should be something like 'isTest', do this later
  107. */
  108. const test = isBufferForTestAddress(decoded);
  109. /* eslint-enable @typescript-eslint/naming-convention */
  110. const accountId = decoded.slice(2, 22);
  111. const tag = tagFromBuffer(decoded);
  112. return {
  113. accountId,
  114. tag,
  115. test,
  116. };
  117. }
  118. exports.decodeXAddress = decodeXAddress;
  119. function isBufferForTestAddress(buf) {
  120. const decodedPrefix = buf.slice(0, 2);
  121. if (PREFIX_BYTES.main.equals(decodedPrefix)) {
  122. return false;
  123. }
  124. if (PREFIX_BYTES.test.equals(decodedPrefix)) {
  125. return true;
  126. }
  127. throw new Error('Invalid X-address: bad prefix');
  128. }
  129. function tagFromBuffer(buf) {
  130. const flag = buf[22];
  131. if (flag >= 2) {
  132. // No support for 64-bit tags at this time
  133. throw new Error('Unsupported X-address');
  134. }
  135. if (flag === 1) {
  136. // Little-endian to big-endian
  137. return buf[23] + buf[24] * 0x100 + buf[25] * 0x10000 + buf[26] * 0x1000000;
  138. }
  139. assert.strictEqual(flag, 0, 'flag must be zero to indicate no tag');
  140. assert.ok(Buffer.from('0000000000000000', 'hex').equals(buf.slice(23, 23 + 8)), 'remaining bytes must be zero');
  141. return false;
  142. }
  143. function isValidXAddress(xAddress) {
  144. try {
  145. decodeXAddress(xAddress);
  146. }
  147. catch (_error) {
  148. return false;
  149. }
  150. return true;
  151. }
  152. exports.isValidXAddress = isValidXAddress;
  153. //# sourceMappingURL=index.js.map