address.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getIcapAddress = exports.getAddress = void 0;
  4. const index_js_1 = require("../crypto/index.js");
  5. const index_js_2 = require("../utils/index.js");
  6. const BN_0 = BigInt(0);
  7. const BN_36 = BigInt(36);
  8. function getChecksumAddress(address) {
  9. // if (!isHexString(address, 20)) {
  10. // logger.throwArgumentError("invalid address", "address", address);
  11. // }
  12. address = address.toLowerCase();
  13. const chars = address.substring(2).split("");
  14. const expanded = new Uint8Array(40);
  15. for (let i = 0; i < 40; i++) {
  16. expanded[i] = chars[i].charCodeAt(0);
  17. }
  18. const hashed = (0, index_js_2.getBytes)((0, index_js_1.keccak256)(expanded));
  19. for (let i = 0; i < 40; i += 2) {
  20. if ((hashed[i >> 1] >> 4) >= 8) {
  21. chars[i] = chars[i].toUpperCase();
  22. }
  23. if ((hashed[i >> 1] & 0x0f) >= 8) {
  24. chars[i + 1] = chars[i + 1].toUpperCase();
  25. }
  26. }
  27. return "0x" + chars.join("");
  28. }
  29. // See: https://en.wikipedia.org/wiki/International_Bank_Account_Number
  30. // Create lookup table
  31. const ibanLookup = {};
  32. for (let i = 0; i < 10; i++) {
  33. ibanLookup[String(i)] = String(i);
  34. }
  35. for (let i = 0; i < 26; i++) {
  36. ibanLookup[String.fromCharCode(65 + i)] = String(10 + i);
  37. }
  38. // How many decimal digits can we process? (for 64-bit float, this is 15)
  39. // i.e. Math.floor(Math.log10(Number.MAX_SAFE_INTEGER));
  40. const safeDigits = 15;
  41. function ibanChecksum(address) {
  42. address = address.toUpperCase();
  43. address = address.substring(4) + address.substring(0, 2) + "00";
  44. let expanded = address.split("").map((c) => { return ibanLookup[c]; }).join("");
  45. // Javascript can handle integers safely up to 15 (decimal) digits
  46. while (expanded.length >= safeDigits) {
  47. let block = expanded.substring(0, safeDigits);
  48. expanded = parseInt(block, 10) % 97 + expanded.substring(block.length);
  49. }
  50. let checksum = String(98 - (parseInt(expanded, 10) % 97));
  51. while (checksum.length < 2) {
  52. checksum = "0" + checksum;
  53. }
  54. return checksum;
  55. }
  56. ;
  57. const Base36 = (function () {
  58. ;
  59. const result = {};
  60. for (let i = 0; i < 36; i++) {
  61. const key = "0123456789abcdefghijklmnopqrstuvwxyz"[i];
  62. result[key] = BigInt(i);
  63. }
  64. return result;
  65. })();
  66. function fromBase36(value) {
  67. value = value.toLowerCase();
  68. let result = BN_0;
  69. for (let i = 0; i < value.length; i++) {
  70. result = result * BN_36 + Base36[value[i]];
  71. }
  72. return result;
  73. }
  74. /**
  75. * Returns a normalized and checksumed address for %%address%%.
  76. * This accepts non-checksum addresses, checksum addresses and
  77. * [[getIcapAddress]] formats.
  78. *
  79. * The checksum in Ethereum uses the capitalization (upper-case
  80. * vs lower-case) of the characters within an address to encode
  81. * its checksum, which offers, on average, a checksum of 15-bits.
  82. *
  83. * If %%address%% contains both upper-case and lower-case, it is
  84. * assumed to already be a checksum address and its checksum is
  85. * validated, and if the address fails its expected checksum an
  86. * error is thrown.
  87. *
  88. * If you wish the checksum of %%address%% to be ignore, it should
  89. * be converted to lower-case (i.e. ``.toLowercase()``) before
  90. * being passed in. This should be a very rare situation though,
  91. * that you wish to bypass the safegaurds in place to protect
  92. * against an address that has been incorrectly copied from another
  93. * source.
  94. *
  95. * @example:
  96. * // Adds the checksum (via upper-casing specific letters)
  97. * getAddress("0x8ba1f109551bd432803012645ac136ddd64dba72")
  98. * //_result:
  99. *
  100. * // Converts ICAP address and adds checksum
  101. * getAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36");
  102. * //_result:
  103. *
  104. * // Throws an error if an address contains mixed case,
  105. * // but the checksum fails
  106. * getAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBA72")
  107. * //_error:
  108. */
  109. function getAddress(address) {
  110. (0, index_js_2.assertArgument)(typeof (address) === "string", "invalid address", "address", address);
  111. if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) {
  112. // Missing the 0x prefix
  113. if (!address.startsWith("0x")) {
  114. address = "0x" + address;
  115. }
  116. const result = getChecksumAddress(address);
  117. // It is a checksummed address with a bad checksum
  118. (0, index_js_2.assertArgument)(!address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) || result === address, "bad address checksum", "address", address);
  119. return result;
  120. }
  121. // Maybe ICAP? (we only support direct mode)
  122. if (address.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/)) {
  123. // It is an ICAP address with a bad checksum
  124. (0, index_js_2.assertArgument)(address.substring(2, 4) === ibanChecksum(address), "bad icap checksum", "address", address);
  125. let result = fromBase36(address.substring(4)).toString(16);
  126. while (result.length < 40) {
  127. result = "0" + result;
  128. }
  129. return getChecksumAddress("0x" + result);
  130. }
  131. (0, index_js_2.assertArgument)(false, "invalid address", "address", address);
  132. }
  133. exports.getAddress = getAddress;
  134. /**
  135. * The [ICAP Address format](link-icap) format is an early checksum
  136. * format which attempts to be compatible with the banking
  137. * industry [IBAN format](link-wiki-iban) for bank accounts.
  138. *
  139. * It is no longer common or a recommended format.
  140. *
  141. * @example:
  142. * getIcapAddress("0x8ba1f109551bd432803012645ac136ddd64dba72");
  143. * //_result:
  144. *
  145. * getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36");
  146. * //_result:
  147. *
  148. * // Throws an error if the ICAP checksum is wrong
  149. * getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK37");
  150. * //_error:
  151. */
  152. function getIcapAddress(address) {
  153. //let base36 = _base16To36(getAddress(address).substring(2)).toUpperCase();
  154. let base36 = BigInt(getAddress(address)).toString(36).toUpperCase();
  155. while (base36.length < 30) {
  156. base36 = "0" + base36;
  157. }
  158. return "XE" + ibanChecksum("XE00" + base36) + base36;
  159. }
  160. exports.getIcapAddress = getIcapAddress;
  161. //# sourceMappingURL=address.js.map