2018-06-13 15:39:39 -04:00
|
|
|
'use strict';
|
2018-06-22 20:30:50 -04:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
2018-06-13 15:39:39 -04:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-06-13 21:10:41 -04:00
|
|
|
// We use this for base 36 maths
|
2018-06-22 20:30:50 -04:00
|
|
|
var bn_js_1 = __importDefault(require("bn.js"));
|
2018-06-17 16:47:28 -04:00
|
|
|
var bytes_1 = require("./bytes");
|
2018-06-13 15:39:39 -04:00
|
|
|
var keccak256_1 = require("./keccak256");
|
2018-06-13 21:10:41 -04:00
|
|
|
var rlp_1 = require("./rlp");
|
2018-09-24 16:07:14 -04:00
|
|
|
var errors = require("../errors");
|
2018-07-30 18:59:52 -04:00
|
|
|
///////////////////////////////
|
2018-06-13 15:39:39 -04:00
|
|
|
function getChecksumAddress(address) {
|
|
|
|
if (typeof (address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
|
|
|
errors.throwError('invalid address', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
|
|
|
}
|
|
|
|
address = address.toLowerCase();
|
|
|
|
var chars = address.substring(2).split('');
|
|
|
|
var hashed = new Uint8Array(40);
|
|
|
|
for (var i_1 = 0; i_1 < 40; i_1++) {
|
|
|
|
hashed[i_1] = chars[i_1].charCodeAt(0);
|
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
hashed = bytes_1.arrayify(keccak256_1.keccak256(hashed));
|
2018-06-13 15:39:39 -04:00
|
|
|
for (var 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('');
|
|
|
|
}
|
|
|
|
// Shims for environments that are missing some required constants and functions
|
|
|
|
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
|
|
|
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
|
|
|
|
var ibanLookup = {};
|
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
ibanLookup[String(i)] = String(i);
|
|
|
|
}
|
|
|
|
for (var 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)
|
|
|
|
var safeDigits = Math.floor(log10(MAX_SAFE_INTEGER));
|
|
|
|
function ibanChecksum(address) {
|
|
|
|
address = address.toUpperCase();
|
|
|
|
address = address.substring(4) + address.substring(0, 2) + '00';
|
|
|
|
var expanded = '';
|
|
|
|
address.split('').forEach(function (c) {
|
|
|
|
expanded += ibanLookup[c];
|
|
|
|
});
|
|
|
|
// Javascript can handle integers safely up to 15 (decimal) digits
|
|
|
|
while (expanded.length >= safeDigits) {
|
|
|
|
var block = expanded.substring(0, safeDigits);
|
|
|
|
expanded = parseInt(block, 10) % 97 + expanded.substring(block.length);
|
|
|
|
}
|
|
|
|
var checksum = String(98 - (parseInt(expanded, 10) % 97));
|
|
|
|
while (checksum.length < 2) {
|
|
|
|
checksum = '0' + checksum;
|
|
|
|
}
|
|
|
|
return checksum;
|
|
|
|
}
|
|
|
|
;
|
2018-06-17 16:32:57 -04:00
|
|
|
function getAddress(address) {
|
2018-06-13 15:39:39 -04:00
|
|
|
var result = null;
|
|
|
|
if (typeof (address) !== 'string') {
|
|
|
|
errors.throwError('invalid address', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
errors.throwError('bad address checksum', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
|
|
|
}
|
|
|
|
// 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)) {
|
|
|
|
errors.throwError('bad icap checksum', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
|
|
|
}
|
2018-06-22 20:30:50 -04:00
|
|
|
result = (new bn_js_1.default.BN(address.substring(4), 36)).toString(16);
|
2018-06-13 15:39:39 -04:00
|
|
|
while (result.length < 40) {
|
|
|
|
result = '0' + result;
|
|
|
|
}
|
|
|
|
result = getChecksumAddress('0x' + result);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errors.throwError('invalid address', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
exports.getAddress = getAddress;
|
2018-06-17 16:32:57 -04:00
|
|
|
function getIcapAddress(address) {
|
2018-06-22 20:30:50 -04:00
|
|
|
var base36 = (new bn_js_1.default.BN(getAddress(address).substring(2), 16)).toString(36).toUpperCase();
|
2018-06-17 16:32:57 -04:00
|
|
|
while (base36.length < 30) {
|
|
|
|
base36 = '0' + base36;
|
|
|
|
}
|
|
|
|
return 'XE' + ibanChecksum('XE00' + base36) + base36;
|
|
|
|
}
|
|
|
|
exports.getIcapAddress = getIcapAddress;
|
2018-06-13 21:10:41 -04:00
|
|
|
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
|
|
|
function getContractAddress(transaction) {
|
|
|
|
if (!transaction.from) {
|
|
|
|
throw new Error('missing from address');
|
|
|
|
}
|
|
|
|
var nonce = transaction.nonce;
|
|
|
|
return getAddress('0x' + keccak256_1.keccak256(rlp_1.encode([
|
|
|
|
getAddress(transaction.from),
|
2018-06-17 16:47:28 -04:00
|
|
|
bytes_1.stripZeros(bytes_1.hexlify(nonce))
|
2018-06-13 21:10:41 -04:00
|
|
|
])).substring(26));
|
|
|
|
}
|
|
|
|
exports.getContractAddress = getContractAddress;
|