2017-02-24 22:57:46 +03:00
|
|
|
|
|
|
|
var BN = require('bn.js');
|
|
|
|
|
2017-04-05 00:22:53 +03:00
|
|
|
var convert = require('./convert');
|
|
|
|
var throwError = require('./throw-error');
|
|
|
|
var keccak256 = require('./keccak256');
|
2017-02-24 22:57:46 +03:00
|
|
|
|
|
|
|
function getChecksumAddress(address) {
|
|
|
|
if (typeof(address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
2017-04-05 00:22:53 +03:00
|
|
|
throwError('invalid address', {input: address});
|
2017-02-24 22:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
address = address.toLowerCase();
|
|
|
|
|
|
|
|
var hashed = address.substring(2).split('');
|
|
|
|
for (var i = 0; i < hashed.length; i++) {
|
|
|
|
hashed[i] = hashed[i].charCodeAt(0);
|
|
|
|
}
|
|
|
|
hashed = convert.arrayify(keccak256(hashed));
|
|
|
|
|
|
|
|
address = address.substring(2).split('');
|
|
|
|
for (var i = 0; i < 40; i += 2) {
|
|
|
|
if ((hashed[i >> 1] >> 4) >= 8) {
|
|
|
|
address[i] = address[i].toUpperCase();
|
|
|
|
}
|
|
|
|
if ((hashed[i >> 1] & 0x0f) >= 8) {
|
|
|
|
address[i + 1] = address[i + 1].toUpperCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '0x' + address.join('');
|
|
|
|
}
|
|
|
|
|
2017-11-09 04:19:41 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-24 22:57:46 +03:00
|
|
|
// See: https://en.wikipedia.org/wiki/International_Bank_Account_Number
|
|
|
|
var ibanChecksum = (function() {
|
|
|
|
|
|
|
|
// 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)
|
2017-11-09 04:19:41 +03:00
|
|
|
var safeDigits = Math.floor(log10(MAX_SAFE_INTEGER));
|
2017-02-24 22:57:46 +03:00
|
|
|
|
|
|
|
return function(address) {
|
|
|
|
address = address.toUpperCase();
|
|
|
|
address = address.substring(4) + address.substring(0, 2) + '00';
|
|
|
|
|
|
|
|
var expanded = address.split('');
|
|
|
|
for (var i = 0; i < expanded.length; i++) {
|
|
|
|
expanded[i] = ibanLookup[expanded[i]];
|
|
|
|
}
|
|
|
|
expanded = expanded.join('');
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
function getAddress(address, icapFormat) {
|
|
|
|
var result = null;
|
|
|
|
|
2017-04-05 00:22:53 +03:00
|
|
|
if (typeof(address) !== 'string') {
|
|
|
|
throwError('invalid address', {input: address});
|
|
|
|
}
|
2017-02-24 22:57:46 +03: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) {
|
2017-04-05 00:22:53 +03:00
|
|
|
throwError('invalid address checksum', { input: address, expected: result });
|
2017-02-24 22:57:46 +03: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)) {
|
2017-04-05 00:22:53 +03:00
|
|
|
throwError('invalid address icap checksum', { input: address });
|
2017-02-24 22:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = (new BN(address.substring(4), 36)).toString(16);
|
|
|
|
while (result.length < 40) { result = '0' + result; }
|
|
|
|
result = getChecksumAddress('0x' + result);
|
|
|
|
|
|
|
|
} else {
|
2017-04-05 00:22:53 +03:00
|
|
|
throwError('invalid address', { input: address });
|
2017-02-24 22:57:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (icapFormat) {
|
|
|
|
var base36 = (new BN(result.substring(2), 16)).toString(36).toUpperCase();
|
|
|
|
while (base36.length < 30) { base36 = '0' + base36; }
|
|
|
|
return 'XE' + ibanChecksum('XE00' + base36) + base36;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getAddress: getAddress,
|
|
|
|
}
|