From c9b07b18a3c473b827adc387d49d247adcbbfcd8 Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Fri, 28 Sep 2018 14:17:21 +0200 Subject: [PATCH 1/3] Add Blake2B support to crypto utils (taken from https://github.com/emilbayes/blake2b) --- src/crypto/blake2b.js | 276 ++++++++++++++++++++++++++++++++++++++++++ src/crypto/utils.js | 13 ++ 2 files changed, 289 insertions(+) create mode 100644 src/crypto/blake2b.js diff --git a/src/crypto/blake2b.js b/src/crypto/blake2b.js new file mode 100644 index 00000000..2b29a24f --- /dev/null +++ b/src/crypto/blake2b.js @@ -0,0 +1,276 @@ +'use strict'; + +/** + * Credits to https://github.com/emilbayes/blake2b + * + * Copyright (c) 2017, Emil Bay github@tixz.dk + * + * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +// 64-bit unsigned addition +// Sets v[a,a+1] += v[b,b+1] +// v should be a Uint32Array +function ADD64AA (v, a, b) { + var o0 = v[a] + v[b] + var o1 = v[a + 1] + v[b + 1] + if (o0 >= 0x100000000) { + o1++ + } + v[a] = o0 + v[a + 1] = o1 +} + +// 64-bit unsigned addition +// Sets v[a,a+1] += b +// b0 is the low 32 bits of b, b1 represents the high 32 bits +function ADD64AC (v, a, b0, b1) { + var o0 = v[a] + b0 + if (b0 < 0) { + o0 += 0x100000000 + } + var o1 = v[a + 1] + b1 + if (o0 >= 0x100000000) { + o1++ + } + v[a] = o0 + v[a + 1] = o1 +} + +// Little-endian byte access +function B2B_GET32 (arr, i) { + return (arr[i] ^ + (arr[i + 1] << 8) ^ + (arr[i + 2] << 16) ^ + (arr[i + 3] << 24)) +} + +// G Mixing function +// The ROTRs are inlined for speed +function B2B_G (a, b, c, d, ix, iy) { + var x0 = m[ix] + var x1 = m[ix + 1] + var y0 = m[iy] + var y1 = m[iy + 1] + + ADD64AA(v, a, b) // v[a,a+1] += v[b,b+1] ... in JS we must store a uint64 as two uint32s + ADD64AC(v, a, x0, x1) // v[a, a+1] += x ... x0 is the low 32 bits of x, x1 is the high 32 bits + + // v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated to the right by 32 bits + var xor0 = v[d] ^ v[a] + var xor1 = v[d + 1] ^ v[a + 1] + v[d] = xor1 + v[d + 1] = xor0 + + ADD64AA(v, c, d) + + // v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 24 bits + xor0 = v[b] ^ v[c] + xor1 = v[b + 1] ^ v[c + 1] + v[b] = (xor0 >>> 24) ^ (xor1 << 8) + v[b + 1] = (xor1 >>> 24) ^ (xor0 << 8) + + ADD64AA(v, a, b) + ADD64AC(v, a, y0, y1) + + // v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated right by 16 bits + xor0 = v[d] ^ v[a] + xor1 = v[d + 1] ^ v[a + 1] + v[d] = (xor0 >>> 16) ^ (xor1 << 16) + v[d + 1] = (xor1 >>> 16) ^ (xor0 << 16) + + ADD64AA(v, c, d) + + // v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 63 bits + xor0 = v[b] ^ v[c] + xor1 = v[b + 1] ^ v[c + 1] + v[b] = (xor1 >>> 31) ^ (xor0 << 1) + v[b + 1] = (xor0 >>> 31) ^ (xor1 << 1) +} + +// Initialization Vector +var BLAKE2B_IV32 = new Uint32Array([ + 0xF3BCC908, 0x6A09E667, 0x84CAA73B, 0xBB67AE85, + 0xFE94F82B, 0x3C6EF372, 0x5F1D36F1, 0xA54FF53A, + 0xADE682D1, 0x510E527F, 0x2B3E6C1F, 0x9B05688C, + 0xFB41BD6B, 0x1F83D9AB, 0x137E2179, 0x5BE0CD19 +]) + +var SIGMA8 = [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, + 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, + 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, + 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, + 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9, + 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11, + 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10, + 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5, + 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 +] + +// These are offsets into a uint64 buffer. +// Multiply them all by 2 to make them offsets into a uint32 buffer, +// because this is Javascript and we don't have uint64s +var SIGMA82 = new Uint8Array(SIGMA8.map(function (x) { return x * 2 })) + +// Compression function. 'last' flag indicates last block. +// Note we're representing 16 uint64s as 32 uint32s +var v = new Uint32Array(32) +var m = new Uint32Array(32) +function blake2bCompress (ctx, last) { + var i = 0 + + // init work variables + for (i = 0; i < 16; i++) { + v[i] = ctx.h[i] + v[i + 16] = BLAKE2B_IV32[i] + } + + // low 64 bits of offset + v[24] = v[24] ^ ctx.t + v[25] = v[25] ^ (ctx.t / 0x100000000) + // high 64 bits not supported, offset may not be higher than 2**53-1 + + // last block flag set ? + if (last) { + v[28] = ~v[28] + v[29] = ~v[29] + } + + // get little-endian words + for (i = 0; i < 32; i++) { + m[i] = B2B_GET32(ctx.b, 4 * i) + } + + // twelve rounds of mixing + for (i = 0; i < 12; i++) { + B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]) + B2B_G(2, 10, 18, 26, SIGMA82[i * 16 + 2], SIGMA82[i * 16 + 3]) + B2B_G(4, 12, 20, 28, SIGMA82[i * 16 + 4], SIGMA82[i * 16 + 5]) + B2B_G(6, 14, 22, 30, SIGMA82[i * 16 + 6], SIGMA82[i * 16 + 7]) + B2B_G(0, 10, 20, 30, SIGMA82[i * 16 + 8], SIGMA82[i * 16 + 9]) + B2B_G(2, 12, 22, 24, SIGMA82[i * 16 + 10], SIGMA82[i * 16 + 11]) + B2B_G(4, 14, 16, 26, SIGMA82[i * 16 + 12], SIGMA82[i * 16 + 13]) + B2B_G(6, 8, 18, 28, SIGMA82[i * 16 + 14], SIGMA82[i * 16 + 15]) + } + + for (i = 0; i < 16; i++) { + ctx.h[i] = ctx.h[i] ^ v[i] ^ v[i + 16] + } +} + +// reusable parameter_block +var parameter_block = new Uint8Array([ + 0, 0, 0, 0, // 0: outlen, keylen, fanout, depth + 0, 0, 0, 0, // 4: leaf length, sequential mode + 0, 0, 0, 0, // 8: node offset + 0, 0, 0, 0, // 12: node offset + 0, 0, 0, 0, // 16: node depth, inner length, rfu + 0, 0, 0, 0, // 20: rfu + 0, 0, 0, 0, // 24: rfu + 0, 0, 0, 0, // 28: rfu + 0, 0, 0, 0, // 32: salt + 0, 0, 0, 0, // 36: salt + 0, 0, 0, 0, // 40: salt + 0, 0, 0, 0, // 44: salt + 0, 0, 0, 0, // 48: personal + 0, 0, 0, 0, // 52: personal + 0, 0, 0, 0, // 56: personal + 0, 0, 0, 0 // 60: personal +]) + +// Creates a BLAKE2b hashing context +// Requires an output length between 1 and 64 bytes +// Takes an optional Uint8Array key +function Blake2b (outlen, key, salt, personal) { + // zero out parameter_block before usage + parameter_block.fill(0) + // state, 'param block' + + this.b = new Uint8Array(128) + this.h = new Uint32Array(16) + this.t = 0 // input count + this.c = 0 // pointer within buffer + this.outlen = outlen // output length in bytes + + parameter_block[0] = outlen + if (key) parameter_block[1] = key.length + parameter_block[2] = 1 // fanout + parameter_block[3] = 1 // depth + + if (salt) parameter_block.set(salt, 32) + if (personal) parameter_block.set(personal, 48) + + // initialize hash state + for (var i = 0; i < 16; i++) { + this.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameter_block, i * 4) + } + + // key the hash, if applicable + if (key) { + blake2bUpdate(this, key) + // at the end + this.c = 128 + } +} + +Blake2b.prototype.update = function (input) { + blake2bUpdate(this, input) + return this +} + +Blake2b.prototype.digest = function (out) { + var buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(this.outlen) : out + blake2bFinal(this, buf) + if (out === 'hex') return hexSlice(buf) + return buf +} + +Blake2b.prototype.final = Blake2b.prototype.digest + +// Updates a BLAKE2b streaming hash +// Requires hash context and Uint8Array (byte array) +function blake2bUpdate (ctx, input) { + for (var i = 0; i < input.length; i++) { + if (ctx.c === 128) { // buffer full ? + ctx.t += ctx.c // add counters + blake2bCompress(ctx, false) // compress (not last) + ctx.c = 0 // counter to zero + } + ctx.b[ctx.c++] = input[i] + } +} + +// Completes a BLAKE2b streaming hash +// Returns a Uint8Array containing the message digest +function blake2bFinal (ctx, out) { + ctx.t += ctx.c // mark last block offset + + while (ctx.c < 128) { // fill up with zeros + ctx.b[ctx.c++] = 0 + } + blake2bCompress(ctx, true) // final block flag = 1 + + for (var i = 0; i < ctx.outlen; i++) { + out[i] = ctx.h[i >> 2] >> (8 * (i & 3)) + } + return out +} + +function hexSlice (buf) { + var str = '' + for (var i = 0; i < buf.length; i++) str += toHex(buf[i]) + return str +} + +function toHex (n) { + if (n < 16) return '0' + n.toString(16) + return n.toString(16) +} + +module.exports = Blake2b; \ No newline at end of file diff --git a/src/crypto/utils.js b/src/crypto/utils.js index 0d824c99..5f4f3e15 100644 --- a/src/crypto/utils.js +++ b/src/crypto/utils.js @@ -1,6 +1,7 @@ var jsSHA = require('jssha/src/sha256'); var Blake256 = require('./blake256'); var keccak256 = require('./sha3')['keccak256']; +var Blake2B = require('./blake2b'); function numberToHex (number) { var hex = Math.round(number).toString(16); @@ -10,6 +11,15 @@ function numberToHex (number) { return hex; } +function hexToUint8(hexString) { + var arr = []; + for (var i = 0; i < hexString.length; i += 2) { + arr.push(parseInt(hexString.substr(i, 2), 16)); + } + + return new Uint8Array(arr); +} + module.exports = { toHex: function (arrayOfBytes) { var hex = ''; @@ -32,6 +42,9 @@ module.exports = { blake256Checksum: function (payload) { return this.blake256(this.blake256(payload)).substr(0, 8); }, + blake2b: function (hexString, outlen) { + return new Blake2B(outlen).update(hexToUint8(hexString)).digest('hex'); + }, keccak256: function (hexString) { return keccak256(hexString); } From 0be6ed136ce0c0596b66385b3f7204b578ca06c8 Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Fri, 28 Sep 2018 14:18:31 +0200 Subject: [PATCH 2/3] Add support for validating NANO/XRB addresses --- src/currencies.js | 9 +++++++++ src/nano_validator.js | 27 +++++++++++++++++++++++++++ test/wallet_address_validator.js | 20 ++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/nano_validator.js diff --git a/src/currencies.js b/src/currencies.js index c8483af1..6b824046 100644 --- a/src/currencies.js +++ b/src/currencies.js @@ -1,6 +1,7 @@ var XRPValidator = require('./ripple_validator'); var ETHValidator = require('./ethereum_validator'); var BTCValidator = require('./bitcoin_validator'); +var NANOValidator = require('./nano_validator'); // defines P2PKH and P2SH address types for standard (prod) and testnet networks var CURRENCIES = [{ @@ -192,6 +193,14 @@ var CURRENCIES = [{ name: 'bankex', symbol: 'bkx', validator: ETHValidator +},{ + name: 'nano', + symbol: 'nano', + validator: NANOValidator, +},{ + name: 'raiblocks', + symbol: 'xrb', + validator: NANOValidator, }]; diff --git a/src/nano_validator.js b/src/nano_validator.js new file mode 100644 index 00000000..8960ab4e --- /dev/null +++ b/src/nano_validator.js @@ -0,0 +1,27 @@ +var cryptoUtils = require('./crypto/utils'); +var baseX = require('base-x'); + +var ALLOWED_CHARS = '13456789abcdefghijkmnopqrstuwxyz'; + +var codec = baseX(ALLOWED_CHARS); +// https://github.com/nanocurrency/raiblocks/wiki/Accounts,-Keys,-Seeds,-and-Wallet-Identifiers +var regexp = new RegExp('^(xrb|nano)_([' + ALLOWED_CHARS + ']{60})$'); + +module.exports = { + isValidAddress: function (address) { + if (regexp.test(address)) { + return this.verifyChecksum(address); + } + + return false; + }, + + verifyChecksum: function (address) { + var bytes = codec.decode(regexp.exec(address)[2]).slice(-37); + // https://github.com/nanocurrency/raiblocks/blob/master/rai/lib/numbers.cpp#L73 + var computedChecksum = cryptoUtils.blake2b(cryptoUtils.toHex(bytes.slice(0, -5)), 5); + var checksum = cryptoUtils.toHex(bytes.slice(-5).reverse()); + + return computedChecksum === checksum + } +}; diff --git a/test/wallet_address_validator.js b/test/wallet_address_validator.js index 27eefa65..52de9d0e 100644 --- a/test/wallet_address_validator.js +++ b/test/wallet_address_validator.js @@ -354,6 +354,19 @@ describe('WAValidator.validate()', function () { valid('0xf40d80FCfa5cdEa0bB1E570c2D52132ac9bC6aEC', 'bankex', 'testnet'); valid('0x8A7395f281EeCf2B471B689E87Cf4C7fa8bb957d', 'BKX', 'testnet'); }); + + it('should return true for correct nano addresses', function () { + valid('xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3', 'nano'); + valid('xrb_13ezf4od79h1tgj9aiu4djzcmmguendtjfuhwfukhuucboua8cpoihmh8byo', 'nano'); + valid('xrb_35jjmmmh81kydepzeuf9oec8hzkay7msr6yxagzxpcht7thwa5bus5tomgz9', 'nano'); + valid('xrb_1111111111111111111111111111111111111111111111111111hifc8npp', 'nano'); + valid('xrb_1ipx847tk8o46pwxt5qjdbncjqcbwcc1rrmqnkztrfjy5k7z4imsrata9est', 'nano'); + valid('xrb_3wm37qz19zhei7nzscjcopbrbnnachs4p1gnwo5oroi3qonw6inwgoeuufdp', 'nano'); + valid('xrb_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4', 'nano'); + valid('xrb_1f5e4w33ndqbkx4bw5jtp13kp5xghebfxcmw9hdt1f7goid1s4373w6tjmgu', 'nano'); + valid('xrb_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano'); + valid('nano_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano'); + }); }); describe('invalid results', function () { @@ -545,5 +558,12 @@ describe('WAValidator.validate()', function () { //invalid('t2YNzUUx8mWBCRYPRezvA363EYXyEpHokyi', 'komodo', 'testnet'); }); + it('should return false for incorrect nano addresses', function () { + commonTests('nano'); + invalid('xrb_1f5e4w33ndqbkx4bw5jtp13kp5xghebfxcmw9hdt1f7goid1s4373w6tjdgu', 'nano'); + invalid('nano_1f5e4w33ndqbkx4bw5jtp13kp5xghebfxcmw9hdt1f7goid1s4373w6tjdgu', 'nano'); + invalid('xrb_1111111112111111111111111111111111111111111111111111hifc8npp', 'nano'); + invalid('nano_111111111111111111111111111111111111111111111111111hifc8npp', 'nano'); + }); }); }); From 899cec934c122a6d5b55adf3419ae3ceffa01290 Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Fri, 28 Sep 2018 14:29:21 +0200 Subject: [PATCH 3/3] Update README about NANO addition --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ef22e425..bdca7da5 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ npm install wallet-address-validator * Megacoin/MEC, `'megacoin'` or `'MEC'` * Namecoin/NMC, `'namecoin'` or `'NMC'` +* Nano/NANO, `'nano'` or `'NANO'` * NEO/NEO, `'NEO'` or `'NEO'` * NeoGas/GAS, `'neogas'` or `'GAS'` @@ -74,6 +75,7 @@ npm install wallet-address-validator * Qtum/QTUM, `'qtum'` or `'QTUM'` +* Raiblocks/XRB, `'raiblocks'` or `'XRB'` * Ripple/XRP, `'ripple'` or `'XRP'` * Snowgem/SNG, `'snowgem'` or `'SNG'`