2022-09-05 16:57:11 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.getIcapAddress = exports.getAddress = void 0;
|
2022-09-15 22:58:45 -04:00
|
|
|
const index_js_1 = require("../crypto/index.js");
|
|
|
|
const index_js_2 = require("../utils/index.js");
|
2022-09-05 16:57:11 -04:00
|
|
|
const BN_0 = BigInt(0);
|
|
|
|
const BN_36 = BigInt(36);
|
|
|
|
function getChecksumAddress(address) {
|
|
|
|
// if (!isHexString(address, 20)) {
|
|
|
|
// logger.throwArgumentError("invalid address", "address", address);
|
|
|
|
// }
|
|
|
|
address = address.toLowerCase();
|
|
|
|
const chars = address.substring(2).split("");
|
|
|
|
const expanded = new Uint8Array(40);
|
|
|
|
for (let i = 0; i < 40; i++) {
|
|
|
|
expanded[i] = chars[i].charCodeAt(0);
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
const hashed = (0, index_js_2.getBytes)((0, index_js_1.keccak256)(expanded));
|
2022-09-05 16:57:11 -04:00
|
|
|
for (let i = 0; i < 40; i += 2) {
|
|
|
|
if ((hashed[i >> 1] >> 4) >= 8) {
|
|
|
|
chars[i] = chars[i].toUpperCase();
|
|
|
|
}
|
|
|
|
if ((hashed[i >> 1] & 0x0f) >= 8) {
|
|
|
|
chars[i + 1] = chars[i + 1].toUpperCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "0x" + chars.join("");
|
|
|
|
}
|
|
|
|
// See: https://en.wikipedia.org/wiki/International_Bank_Account_Number
|
|
|
|
// Create lookup table
|
|
|
|
const ibanLookup = {};
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
ibanLookup[String(i)] = String(i);
|
|
|
|
}
|
|
|
|
for (let i = 0; i < 26; i++) {
|
|
|
|
ibanLookup[String.fromCharCode(65 + i)] = String(10 + i);
|
|
|
|
}
|
|
|
|
// How many decimal digits can we process? (for 64-bit float, this is 15)
|
|
|
|
// i.e. Math.floor(Math.log10(Number.MAX_SAFE_INTEGER));
|
|
|
|
const safeDigits = 15;
|
|
|
|
function ibanChecksum(address) {
|
|
|
|
address = address.toUpperCase();
|
|
|
|
address = address.substring(4) + address.substring(0, 2) + "00";
|
|
|
|
let expanded = address.split("").map((c) => { return ibanLookup[c]; }).join("");
|
|
|
|
// Javascript can handle integers safely up to 15 (decimal) digits
|
|
|
|
while (expanded.length >= safeDigits) {
|
|
|
|
let block = expanded.substring(0, safeDigits);
|
|
|
|
expanded = parseInt(block, 10) % 97 + expanded.substring(block.length);
|
|
|
|
}
|
|
|
|
let checksum = String(98 - (parseInt(expanded, 10) % 97));
|
|
|
|
while (checksum.length < 2) {
|
|
|
|
checksum = "0" + checksum;
|
|
|
|
}
|
|
|
|
return checksum;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
const Base36 = (function () {
|
|
|
|
;
|
|
|
|
const result = {};
|
|
|
|
for (let i = 0; i < 36; i++) {
|
|
|
|
const key = "0123456789abcdefghijklmnopqrstuvwxyz"[i];
|
|
|
|
result[key] = BigInt(i);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
})();
|
|
|
|
function fromBase36(value) {
|
|
|
|
value = value.toLowerCase();
|
|
|
|
let result = BN_0;
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
result = result * BN_36 + Base36[value[i]];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2022-12-02 21:27:06 -05:00
|
|
|
/**
|
|
|
|
* Returns a normalized and checksumed address for %%address%%.
|
|
|
|
* This accepts non-checksum addresses, checksum addresses and
|
|
|
|
* [[getIcapAddress]] formats.
|
|
|
|
*
|
|
|
|
* The checksum in Ethereum uses the capitalization (upper-case
|
|
|
|
* vs lower-case) of the characters within an address to encode
|
|
|
|
* its checksum, which offers, on average, a checksum of 15-bits.
|
|
|
|
*
|
|
|
|
* If %%address%% contains both upper-case and lower-case, it is
|
|
|
|
* assumed to already be a checksum address and its checksum is
|
|
|
|
* validated, and if the address fails its expected checksum an
|
|
|
|
* error is thrown.
|
|
|
|
*
|
|
|
|
* If you wish the checksum of %%address%% to be ignore, it should
|
|
|
|
* be converted to lower-case (i.e. ``.toLowercase()``) before
|
|
|
|
* being passed in. This should be a very rare situation though,
|
|
|
|
* that you wish to bypass the safegaurds in place to protect
|
|
|
|
* against an address that has been incorrectly copied from another
|
|
|
|
* source.
|
2022-12-09 18:24:58 -05:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* // Adds the checksum (via upper-casing specific letters)
|
|
|
|
* getAddress("0x8ba1f109551bd432803012645ac136ddd64dba72")
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Converts ICAP address and adds checksum
|
|
|
|
* getAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36");
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Throws an error if an address contains mixed case,
|
|
|
|
* // but the checksum fails
|
|
|
|
* getAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBA72")
|
|
|
|
* //_error:
|
2022-12-02 21:27:06 -05:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
function getAddress(address) {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, index_js_2.assertArgument)(typeof (address) === "string", "invalid address", "address", address);
|
2022-09-05 16:57:11 -04:00
|
|
|
if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) {
|
|
|
|
// Missing the 0x prefix
|
2023-02-18 22:18:42 -05:00
|
|
|
if (!address.startsWith("0x")) {
|
2022-09-05 16:57:11 -04:00
|
|
|
address = "0x" + address;
|
|
|
|
}
|
|
|
|
const result = getChecksumAddress(address);
|
|
|
|
// It is a checksummed address with a bad checksum
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, index_js_2.assertArgument)(!address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) || result === address, "bad address checksum", "address", address);
|
2022-09-05 16:57:11 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// Maybe ICAP? (we only support direct mode)
|
|
|
|
if (address.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/)) {
|
|
|
|
// It is an ICAP address with a bad checksum
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, index_js_2.assertArgument)(address.substring(2, 4) === ibanChecksum(address), "bad icap checksum", "address", address);
|
2022-09-05 16:57:11 -04:00
|
|
|
let result = fromBase36(address.substring(4)).toString(16);
|
|
|
|
while (result.length < 40) {
|
|
|
|
result = "0" + result;
|
|
|
|
}
|
|
|
|
return getChecksumAddress("0x" + result);
|
|
|
|
}
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, index_js_2.assertArgument)(false, "invalid address", "address", address);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
exports.getAddress = getAddress;
|
2022-12-02 21:27:06 -05:00
|
|
|
/**
|
|
|
|
* The [ICAP Address format](link-icap) format is an early checksum
|
|
|
|
* format which attempts to be compatible with the banking
|
2023-03-03 18:25:07 -07:00
|
|
|
* industry [IBAN format](link-wiki-iban) for bank accounts.
|
2022-12-02 21:27:06 -05:00
|
|
|
*
|
|
|
|
* It is no longer common or a recommended format.
|
2022-12-09 18:24:58 -05:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* getIcapAddress("0x8ba1f109551bd432803012645ac136ddd64dba72");
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36");
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Throws an error if the ICAP checksum is wrong
|
|
|
|
* getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK37");
|
|
|
|
* //_error:
|
2022-12-02 21:27:06 -05:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
function getIcapAddress(address) {
|
|
|
|
//let base36 = _base16To36(getAddress(address).substring(2)).toUpperCase();
|
|
|
|
let base36 = BigInt(getAddress(address)).toString(36).toUpperCase();
|
|
|
|
while (base36.length < 30) {
|
|
|
|
base36 = "0" + base36;
|
|
|
|
}
|
|
|
|
return "XE" + ibanChecksum("XE00" + base36) + base36;
|
|
|
|
}
|
|
|
|
exports.getIcapAddress = getIcapAddress;
|
|
|
|
//# sourceMappingURL=address.js.map
|