129 lines
4.0 KiB
TypeScript
129 lines
4.0 KiB
TypeScript
|
|
import { arrayify } from "@ethersproject/bytes";
|
|
import { keccak256 } from "@ethersproject/crypto";
|
|
|
|
import { logger } from "./logger.js";
|
|
|
|
|
|
const BN_0 = BigInt(0);
|
|
const BN_36 = BigInt(36);
|
|
|
|
function getChecksumAddress(address: string): string {
|
|
// 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);
|
|
}
|
|
|
|
const hashed = arrayify(keccak256(expanded));
|
|
|
|
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: { [character: string]: string } = { };
|
|
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: string): string {
|
|
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: Record<string, bigint> = { };
|
|
for (let i = 0; i < 36; i++) {
|
|
const key = "0123456789abcdefghijklmnopqrstuvwxyz"[i];
|
|
result[key] = BigInt(i);
|
|
}
|
|
return result;
|
|
})();
|
|
|
|
function fromBase36(value: string): bigint {
|
|
value = value.toLowerCase();
|
|
|
|
let result = BN_0;
|
|
for (let i = 0; i < value.length; i++) {
|
|
result = result * BN_36 + Base36[value[i]];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function getAddress(address: string): string {
|
|
|
|
if (typeof(address) !== "string") {
|
|
logger.throwArgumentError("invalid address", "address", address);
|
|
}
|
|
|
|
if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) {
|
|
|
|
// Missing the 0x prefix
|
|
if (address.substring(0, 2) !== "0x") { address = "0x" + address; }
|
|
|
|
const result = getChecksumAddress(address);
|
|
|
|
// It is a checksummed address with a bad checksum
|
|
if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) && result !== address) {
|
|
logger.throwArgumentError("bad address checksum", "address", address);
|
|
}
|
|
|
|
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
|
|
if (address.substring(2, 4) !== ibanChecksum(address)) {
|
|
logger.throwArgumentError("bad icap checksum", "address", address);
|
|
}
|
|
|
|
let result = fromBase36(address.substring(4)).toString(16);
|
|
while (result.length < 40) { result = "0" + result; }
|
|
return getChecksumAddress("0x" + result);
|
|
}
|
|
|
|
return logger.throwArgumentError("invalid address", "address", address);
|
|
}
|
|
|
|
export function getIcapAddress(address: string): string {
|
|
//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;
|
|
}
|