xrp-codec.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. /**
  3. * Codec class
  4. */
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.isValidClassicAddress = exports.decodeAccountPublic = exports.encodeAccountPublic = exports.encodeNodePublic = exports.decodeNodePublic = exports.decodeAddress = exports.decodeAccountID = exports.encodeAddress = exports.encodeAccountID = exports.decodeSeed = exports.encodeSeed = exports.codec = void 0;
  7. const baseCodec = require("base-x");
  8. const createHash = require("create-hash");
  9. const utils_1 = require("./utils");
  10. class Codec {
  11. constructor(options) {
  12. this.sha256 = options.sha256;
  13. this.alphabet = options.alphabet;
  14. this.codec = baseCodec(this.alphabet);
  15. this.base = this.alphabet.length;
  16. }
  17. /**
  18. * Encoder.
  19. *
  20. * @param bytes - Buffer of data to encode.
  21. * @param opts - Options object including the version bytes and the expected length of the data to encode.
  22. */
  23. encode(bytes, opts) {
  24. const versions = opts.versions;
  25. return this.encodeVersioned(bytes, versions, opts.expectedLength);
  26. }
  27. encodeVersioned(bytes, versions, expectedLength) {
  28. if (expectedLength && bytes.length !== expectedLength) {
  29. throw new Error('unexpected_payload_length: bytes.length does not match expectedLength.' +
  30. ' Ensure that the bytes are a Buffer.');
  31. }
  32. return this.encodeChecked(Buffer.from((0, utils_1.concatArgs)(versions, bytes)));
  33. }
  34. encodeChecked(buffer) {
  35. const check = this.sha256(this.sha256(buffer)).slice(0, 4);
  36. return this.encodeRaw(Buffer.from((0, utils_1.concatArgs)(buffer, check)));
  37. }
  38. encodeRaw(bytes) {
  39. return this.codec.encode(bytes);
  40. }
  41. /**
  42. * Decoder.
  43. *
  44. * @param base58string - Base58Check-encoded string to decode.
  45. * @param opts - Options object including the version byte(s) and the expected length of the data after decoding.
  46. */
  47. /* eslint-disable max-lines-per-function --
  48. * TODO refactor */
  49. decode(base58string, opts) {
  50. const versions = opts.versions;
  51. const types = opts.versionTypes;
  52. const withoutSum = this.decodeChecked(base58string);
  53. if (versions.length > 1 && !opts.expectedLength) {
  54. throw new Error('expectedLength is required because there are >= 2 possible versions');
  55. }
  56. const versionLengthGuess = typeof versions[0] === 'number' ? 1 : versions[0].length;
  57. const payloadLength = opts.expectedLength || withoutSum.length - versionLengthGuess;
  58. const versionBytes = withoutSum.slice(0, -payloadLength);
  59. const payload = withoutSum.slice(-payloadLength);
  60. for (let i = 0; i < versions.length; i++) {
  61. const version = Array.isArray(versions[i])
  62. ? versions[i]
  63. : [versions[i]];
  64. if ((0, utils_1.seqEqual)(versionBytes, version)) {
  65. return {
  66. version,
  67. bytes: payload,
  68. type: types ? types[i] : null,
  69. };
  70. }
  71. }
  72. throw new Error('version_invalid: version bytes do not match any of the provided version(s)');
  73. }
  74. /* eslint-enable max-lines-per-function */
  75. decodeChecked(base58string) {
  76. const buffer = this.decodeRaw(base58string);
  77. if (buffer.length < 5) {
  78. throw new Error('invalid_input_size: decoded data must have length >= 5');
  79. }
  80. if (!this.verifyCheckSum(buffer)) {
  81. throw new Error('checksum_invalid');
  82. }
  83. return buffer.slice(0, -4);
  84. }
  85. decodeRaw(base58string) {
  86. return this.codec.decode(base58string);
  87. }
  88. verifyCheckSum(bytes) {
  89. const computed = this.sha256(this.sha256(bytes.slice(0, -4))).slice(0, 4);
  90. const checksum = bytes.slice(-4);
  91. return (0, utils_1.seqEqual)(computed, checksum);
  92. }
  93. }
  94. /**
  95. * XRP codec
  96. */
  97. // base58 encodings: https://xrpl.org/base58-encodings.html
  98. // Account address (20 bytes)
  99. const ACCOUNT_ID = 0;
  100. // Account public key (33 bytes)
  101. const ACCOUNT_PUBLIC_KEY = 0x23;
  102. // 33; Seed value (for secret keys) (16 bytes)
  103. const FAMILY_SEED = 0x21;
  104. // 28; Validation public key (33 bytes)
  105. const NODE_PUBLIC = 0x1c;
  106. // [1, 225, 75]
  107. const ED25519_SEED = [0x01, 0xe1, 0x4b];
  108. const codecOptions = {
  109. sha256(bytes) {
  110. return createHash('sha256').update(Buffer.from(bytes)).digest();
  111. },
  112. alphabet: 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz',
  113. };
  114. const codecWithXrpAlphabet = new Codec(codecOptions);
  115. exports.codec = codecWithXrpAlphabet;
  116. // entropy is a Buffer of size 16
  117. // type is 'ed25519' or 'secp256k1'
  118. function encodeSeed(entropy, type) {
  119. if (entropy.length !== 16) {
  120. throw new Error('entropy must have length 16');
  121. }
  122. const opts = {
  123. expectedLength: 16,
  124. // for secp256k1, use `FAMILY_SEED`
  125. versions: type === 'ed25519' ? ED25519_SEED : [FAMILY_SEED],
  126. };
  127. // prefixes entropy with version bytes
  128. return codecWithXrpAlphabet.encode(entropy, opts);
  129. }
  130. exports.encodeSeed = encodeSeed;
  131. function decodeSeed(seed, opts = {
  132. versionTypes: ['ed25519', 'secp256k1'],
  133. versions: [ED25519_SEED, FAMILY_SEED],
  134. expectedLength: 16,
  135. }) {
  136. return codecWithXrpAlphabet.decode(seed, opts);
  137. }
  138. exports.decodeSeed = decodeSeed;
  139. function encodeAccountID(bytes) {
  140. const opts = { versions: [ACCOUNT_ID], expectedLength: 20 };
  141. return codecWithXrpAlphabet.encode(bytes, opts);
  142. }
  143. exports.encodeAccountID = encodeAccountID;
  144. /* eslint-disable import/no-unused-modules ---
  145. * unclear why this is aliased but we should keep it in case someone else is
  146. * importing it with the aliased name */
  147. exports.encodeAddress = encodeAccountID;
  148. /* eslint-enable import/no-unused-modules */
  149. function decodeAccountID(accountId) {
  150. const opts = { versions: [ACCOUNT_ID], expectedLength: 20 };
  151. return codecWithXrpAlphabet.decode(accountId, opts).bytes;
  152. }
  153. exports.decodeAccountID = decodeAccountID;
  154. /* eslint-disable import/no-unused-modules ---
  155. * unclear why this is aliased but we should keep it in case someone else is
  156. * importing it with the aliased name */
  157. exports.decodeAddress = decodeAccountID;
  158. /* eslint-enable import/no-unused-modules */
  159. function decodeNodePublic(base58string) {
  160. const opts = { versions: [NODE_PUBLIC], expectedLength: 33 };
  161. return codecWithXrpAlphabet.decode(base58string, opts).bytes;
  162. }
  163. exports.decodeNodePublic = decodeNodePublic;
  164. function encodeNodePublic(bytes) {
  165. const opts = { versions: [NODE_PUBLIC], expectedLength: 33 };
  166. return codecWithXrpAlphabet.encode(bytes, opts);
  167. }
  168. exports.encodeNodePublic = encodeNodePublic;
  169. function encodeAccountPublic(bytes) {
  170. const opts = { versions: [ACCOUNT_PUBLIC_KEY], expectedLength: 33 };
  171. return codecWithXrpAlphabet.encode(bytes, opts);
  172. }
  173. exports.encodeAccountPublic = encodeAccountPublic;
  174. function decodeAccountPublic(base58string) {
  175. const opts = { versions: [ACCOUNT_PUBLIC_KEY], expectedLength: 33 };
  176. return codecWithXrpAlphabet.decode(base58string, opts).bytes;
  177. }
  178. exports.decodeAccountPublic = decodeAccountPublic;
  179. function isValidClassicAddress(address) {
  180. try {
  181. decodeAccountID(address);
  182. }
  183. catch (_error) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. exports.isValidClassicAddress = isValidClassicAddress;
  189. //# sourceMappingURL=xrp-codec.js.map