2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2020-01-06 19:00:55 -05:00
|
|
|
import { arrayify, concat, hexDataLength, hexDataSlice, isHexString, stripZeros } from "@ethersproject/bytes";
|
2020-11-16 23:07:24 -05:00
|
|
|
import { BigNumber, _base16To36, _base36To16 } from "@ethersproject/bignumber";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { keccak256 } from "@ethersproject/keccak256";
|
|
|
|
import { encode } from "@ethersproject/rlp";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-14 18:48:48 -04:00
|
|
|
function getChecksumAddress(address) {
|
2019-08-25 02:39:20 -04:00
|
|
|
if (!isHexString(address, 20)) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid address", "address", address);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
address = address.toLowerCase();
|
2019-11-20 18:57:38 +09:00
|
|
|
const chars = address.substring(2).split("");
|
|
|
|
const expanded = new Uint8Array(40);
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < 40; i++) {
|
2019-11-20 18:57:38 +09:00
|
|
|
expanded[i] = chars[i].charCodeAt(0);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
const hashed = arrayify(keccak256(expanded));
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < 40; i += 2) {
|
2019-05-14 18:48:48 -04:00
|
|
|
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("");
|
|
|
|
}
|
|
|
|
// Shims for environments that are missing some required constants and functions
|
2019-08-25 02:39:20 -04:00
|
|
|
const MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
2019-05-14 18:48:48 -04:00
|
|
|
function log10(x) {
|
|
|
|
if (Math.log10) {
|
|
|
|
return Math.log10(x);
|
|
|
|
}
|
|
|
|
return Math.log(x) / Math.LN10;
|
|
|
|
}
|
|
|
|
// See: https://en.wikipedia.org/wiki/International_Bank_Account_Number
|
|
|
|
// Create lookup table
|
2019-11-20 18:57:38 +09:00
|
|
|
const ibanLookup = {};
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < 10; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
ibanLookup[String(i)] = String(i);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < 26; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
ibanLookup[String.fromCharCode(65 + i)] = String(10 + i);
|
|
|
|
}
|
|
|
|
// How many decimal digits can we process? (for 64-bit float, this is 15)
|
2019-11-20 18:57:38 +09:00
|
|
|
const safeDigits = Math.floor(log10(MAX_SAFE_INTEGER));
|
2019-05-14 18:48:48 -04:00
|
|
|
function ibanChecksum(address) {
|
|
|
|
address = address.toUpperCase();
|
|
|
|
address = address.substring(4) + address.substring(0, 2) + "00";
|
2019-11-20 18:57:38 +09:00
|
|
|
let expanded = address.split("").map((c) => { return ibanLookup[c]; }).join("");
|
2019-05-14 18:48:48 -04:00
|
|
|
// Javascript can handle integers safely up to 15 (decimal) digits
|
|
|
|
while (expanded.length >= safeDigits) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let block = expanded.substring(0, safeDigits);
|
2019-05-14 18:48:48 -04:00
|
|
|
expanded = parseInt(block, 10) % 97 + expanded.substring(block.length);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let checksum = String(98 - (parseInt(expanded, 10) % 97));
|
2019-05-14 18:48:48 -04:00
|
|
|
while (checksum.length < 2) {
|
|
|
|
checksum = "0" + checksum;
|
|
|
|
}
|
|
|
|
return checksum;
|
|
|
|
}
|
|
|
|
;
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getAddress(address) {
|
|
|
|
let result = null;
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (address) !== "string") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid address", "address", address);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) {
|
|
|
|
// Missing the 0x prefix
|
|
|
|
if (address.substring(0, 2) !== "0x") {
|
|
|
|
address = "0x" + address;
|
|
|
|
}
|
|
|
|
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) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("bad address checksum", "address", address);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
// Maybe ICAP? (we only support direct mode)
|
|
|
|
}
|
|
|
|
else 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)) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("bad icap checksum", "address", address);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-11-16 23:07:24 -05:00
|
|
|
result = _base36To16(address.substring(4));
|
2019-05-14 18:48:48 -04:00
|
|
|
while (result.length < 40) {
|
|
|
|
result = "0" + result;
|
|
|
|
}
|
|
|
|
result = getChecksumAddress("0x" + result);
|
|
|
|
}
|
|
|
|
else {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid address", "address", address);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function isAddress(address) {
|
2019-05-14 18:48:48 -04:00
|
|
|
try {
|
|
|
|
getAddress(address);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getIcapAddress(address) {
|
2020-11-16 23:07:24 -05:00
|
|
|
let base36 = _base16To36(getAddress(address).substring(2)).toUpperCase();
|
2019-05-14 18:48:48 -04:00
|
|
|
while (base36.length < 30) {
|
|
|
|
base36 = "0" + base36;
|
|
|
|
}
|
|
|
|
return "XE" + ibanChecksum("XE00" + base36) + base36;
|
|
|
|
}
|
|
|
|
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getContractAddress(transaction) {
|
|
|
|
let from = null;
|
2019-05-14 18:48:48 -04:00
|
|
|
try {
|
|
|
|
from = getAddress(transaction.from);
|
|
|
|
}
|
|
|
|
catch (error) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("missing from address", "transaction", transaction);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
const nonce = stripZeros(arrayify(BigNumber.from(transaction.nonce).toHexString()));
|
2019-08-25 02:39:20 -04:00
|
|
|
return getAddress(hexDataSlice(keccak256(encode([from, nonce])), 12));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-06 19:00:55 -05:00
|
|
|
export function getCreate2Address(from, salt, initCodeHash) {
|
|
|
|
if (hexDataLength(salt) !== 32) {
|
|
|
|
logger.throwArgumentError("salt must be 32 bytes", "salt", salt);
|
|
|
|
}
|
|
|
|
if (hexDataLength(initCodeHash) !== 32) {
|
|
|
|
logger.throwArgumentError("initCodeHash must be 32 bytes", "initCodeHash", initCodeHash);
|
|
|
|
}
|
|
|
|
return getAddress(hexDataSlice(keccak256(concat(["0xff", getAddress(from), salt, initCodeHash])), 12));
|
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=index.js.map
|