utils: start using bstr.

This commit is contained in:
Christopher Jeffrey 2017-10-30 21:34:34 -07:00
parent 41925d495c
commit d4685e6e6c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
18 changed files with 15 additions and 627 deletions

View File

@ -123,8 +123,6 @@ bcoin.witness = require('./script/witness');
// Utils
bcoin.utils = require('./utils');
bcoin.base32 = require('./utils/base32');
bcoin.base58 = require('./utils/base58');
bcoin.co = require('./utils/co');
bcoin.lock = require('./utils/lock');
bcoin.util = require('./utils/util');

View File

@ -158,8 +158,6 @@ bcoin.define('witness', './script/witness');
// Utils
bcoin.define('utils', './utils');
bcoin.define('base32', './utils/base32');
bcoin.define('base58', './utils/base58');
bcoin.define('co', './utils/co');
bcoin.define('lock', './utils/lock');
bcoin.define('util', './utils/util');

View File

@ -14,7 +14,7 @@ const secp256k1 = require('bcrypto/lib/secp256k1');
const Network = require('../protocol/network');
const StaticWriter = require('bbuf/lib/staticwriter');
const BufferReader = require('bbuf/lib/reader');
const base58 = require('../utils/base58');
const base58 = require('bstr/lib/base58');
const encoding = require('bbuf/lib/encoding');
const common = require('./common');
const Mnemonic = require('./mnemonic');

View File

@ -13,7 +13,7 @@ const secp256k1 = require('bcrypto/lib/secp256k1');
const Network = require('../protocol/network');
const StaticWriter = require('bbuf/lib/staticwriter');
const BufferReader = require('bbuf/lib/reader');
const base58 = require('../utils/base58');
const base58 = require('bstr/lib/base58');
const encoding = require('bbuf/lib/encoding');
const common = require('./common');

View File

@ -19,7 +19,7 @@ const ccmp = require('bcrypto/lib/ccmp');
const packets = require('./packets');
const secp256k1 = require('bcrypto/lib/secp256k1');
const StaticWriter = require('bbuf/lib/staticwriter');
const base58 = require('../utils/base58');
const base58 = require('bstr/lib/base58');
const encoding = require('bbuf/lib/encoding');
const IP = require('../utils/ip');
const dns = require('./dns');

View File

@ -11,7 +11,7 @@ const assert = require('assert');
const path = require('path');
const {Server} = require('bweb');
const util = require('../utils/util');
const base58 = require('../utils/base58');
const base58 = require('bstr/lib/base58');
const BloomFilter = require('bfilter/lib/bloom');
const TX = require('../primitives/tx');
const Outpoint = require('../primitives/outpoint');

View File

@ -14,7 +14,6 @@ const Network = require('../protocol/network');
const Logger = require('./logger');
const WorkerPool = require('../workers/workerpool');
const secp256k1 = require('bcrypto/lib/secp256k1');
const native = require('../native');
const Config = require('./config');
/**
@ -189,16 +188,6 @@ Node.prototype.handlePreopen = async function handlePreopen() {
Node.prototype.handleOpen = async function handleOpen() {
this.startTime = util.now();
if (!secp256k1.binding) {
this.logger.warning('Warning: secp256k1-node was not built.');
this.logger.warning('Verification will be slow.');
}
if (!native.binding) {
this.logger.warning('Warning: bcoin-native was not built.');
this.logger.warning('Hashing will be slow.');
}
if (!this.workers.enabled) {
this.logger.warning('Warning: worker pool is disabled.');
this.logger.warning('Verification will be slow.');

View File

@ -13,8 +13,8 @@ const encoding = require('bbuf/lib/encoding');
const digest = require('bcrypto/lib/digest');
const BufferReader = require('bbuf/lib/reader');
const StaticWriter = require('bbuf/lib/staticwriter');
const base58 = require('../utils/base58');
const bech32 = require('../utils/bech32');
const base58 = require('bstr/lib/base58');
const bech32 = require('bstr/lib/bech32');
/**
* Represents an address.

View File

@ -13,7 +13,7 @@ const digest = require('bcrypto/lib/digest');
const Network = require('../protocol/network');
const BufferReader = require('bbuf/lib/reader');
const StaticWriter = require('bbuf/lib/staticwriter');
const base58 = require('../utils/base58');
const base58 = require('bstr/lib/base58');
const Script = require('../script/script');
const Address = require('./address');
const Output = require('./output');

View File

@ -1,163 +0,0 @@
/*!
* base58.js - base58 for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
/**
* @module utils/base58
*/
const assert = require('assert');
const native = require('../native').binding;
/*
* Base58
*/
const base58 = ''
+ '123456789'
+ 'ABCDEFGHJKLMNPQRSTUVWXYZ'
+ 'abcdefghijkmnopqrstuvwxyz';
const unbase58 = {};
for (let i = 0; i < base58.length; i++)
unbase58[base58[i]] = i;
/**
* Encode a base58 string.
* @see https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp
* @param {Buffer} data
* @returns {Base58String}
*/
exports.encode = function encode(data) {
assert(Buffer.isBuffer(data));
let zeroes = 0;
let i = 0;
for (; i < data.length; i++) {
if (data[i] !== 0)
break;
zeroes++;
}
const b58 = Buffer.allocUnsafe(((data.length * 138 / 100) | 0) + 1);
b58.fill(0);
let length = 0;
for (; i < data.length; i++) {
let carry = data[i];
let j = 0;
for (let k = b58.length - 1; k >= 0; k--, j++) {
if (carry === 0 && j >= length)
break;
carry += 256 * b58[k];
b58[k] = carry % 58;
carry = carry / 58 | 0;
}
assert(carry === 0);
length = j;
}
i = b58.length - length;
while (i < b58.length && b58[i] === 0)
i++;
let str = '';
for (let j = 0; j < zeroes; j++)
str += '1';
for (; i < b58.length; i++)
str += base58[b58[i]];
return str;
};
if (native)
exports.encode = native.toBase58;
/**
* Decode a base58 string.
* @see https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp
* @param {Base58String} str
* @returns {Buffer}
* @throws on non-base58 character.
*/
exports.decode = function decode(str) {
assert(typeof str === 'string');
let zeroes = 0;
let i = 0;
for (; i < str.length; i++) {
if (str[i] !== '1')
break;
zeroes++;
}
const b256 = Buffer.allocUnsafe(((str.length * 733) / 1000 | 0) + 1);
b256.fill(0);
let length = 0;
for (; i < str.length; i++) {
const ch = unbase58[str[i]];
if (ch == null)
throw new Error('Non-base58 character.');
let carry = ch;
let j = 0;
for (let k = b256.length - 1; k >= 0; k--, j++) {
if (carry === 0 && j >= length)
break;
carry += 58 * b256[k];
b256[k] = carry % 256;
carry = carry / 256 | 0;
}
assert(carry === 0);
length = j;
}
i = 0;
while (i < b256.length && b256[i] === 0)
i++;
const out = Buffer.allocUnsafe(zeroes + (b256.length - i));
let j;
for (j = 0; j < zeroes; j++)
out[j] = 0;
while (i < b256.length)
out[j++] = b256[i++];
return out;
};
if (native)
exports.decode = native.fromBase58;
/**
* Test whether a string is base58 (note that you
* may get a false positive on a hex string).
* @param {String?} str
* @returns {Boolean}
*/
exports.isBase58 = function isBase58(str) {
return typeof str === 'string' && /^[1-9A-Za-z]+$/.test(str);
};

View File

@ -1,363 +0,0 @@
/*!
* bech32.js - bech32 for bcoin
* Copyright (c) 2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*
* Parts of this software are based on "bech32".
* https://github.com/sipa/bech32
*
* Copyright (c) 2017 Pieter Wuille
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
const assert = require('assert');
const native = require('../native').binding;
/**
* @module utils/bech32
*/
const POOL65 = Buffer.allocUnsafe(65);
const CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
const TABLE = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
];
/**
* Update checksum.
* @ignore
* @param {Number} chk
* @returns {Number}
*/
function polymod(pre) {
const b = pre >>> 25;
return ((pre & 0x1ffffff) << 5)
^ (-((b >> 0) & 1) & 0x3b6a57b2)
^ (-((b >> 1) & 1) & 0x26508e6d)
^ (-((b >> 2) & 1) & 0x1ea119fa)
^ (-((b >> 3) & 1) & 0x3d4233dd)
^ (-((b >> 4) & 1) & 0x2a1462b3);
}
/**
* Encode hrp and data as a bech32 string.
* @ignore
* @param {String} hrp
* @param {Buffer} data
* @returns {String}
*/
function serialize(hrp, data) {
let chk = 1;
let i;
for (i = 0; i < hrp.length; i++) {
const ch = hrp.charCodeAt(i);
if ((ch >> 5) === 0)
throw new Error('Invalid bech32 character.');
chk = polymod(chk) ^ (ch >> 5);
}
if (i + 7 + data.length > 90)
throw new Error('Invalid bech32 data length.');
chk = polymod(chk);
let str = '';
for (let i = 0; i < hrp.length; i++) {
const ch = hrp.charCodeAt(i);
chk = polymod(chk) ^ (ch & 0x1f);
str += hrp[i];
}
str += '1';
for (let i = 0; i < data.length; i++) {
const ch = data[i];
if ((ch >> 5) !== 0)
throw new Error('Invalid bech32 value.');
chk = polymod(chk) ^ ch;
str += CHARSET[ch];
}
for (let i = 0; i < 6; i++)
chk = polymod(chk);
chk ^= 1;
for (let i = 0; i < 6; i++)
str += CHARSET[(chk >>> ((5 - i) * 5)) & 0x1f];
return str;
}
/**
* Decode a bech32 string.
* @param {String} str
* @returns {Array} [hrp, data]
*/
function deserialize(str) {
let dlen = 0;
if (str.length < 8 || str.length > 90)
throw new Error('Invalid bech32 string length.');
while (dlen < str.length && str[(str.length - 1) - dlen] !== '1')
dlen++;
const hlen = str.length - (1 + dlen);
if (hlen < 1 || dlen < 6)
throw new Error('Invalid bech32 data length.');
dlen -= 6;
const data = Buffer.allocUnsafe(dlen);
let chk = 1;
let lower = false;
let upper = false;
let hrp = '';
for (let i = 0; i < hlen; i++) {
let ch = str.charCodeAt(i);
if (ch < 0x21 || ch > 0x7e)
throw new Error('Invalid bech32 character.');
if (ch >= 0x61 && ch <= 0x7a) {
lower = true;
} else if (ch >= 0x41 && ch <= 0x5a) {
upper = true;
ch = (ch - 0x41) + 0x61;
}
hrp += String.fromCharCode(ch);
chk = polymod(chk) ^ (ch >> 5);
}
chk = polymod(chk);
let i;
for (i = 0; i < hlen; i++)
chk = polymod(chk) ^ (str.charCodeAt(i) & 0x1f);
i++;
while (i < str.length) {
const ch = str.charCodeAt(i);
const v = (ch & 0x80) ? -1 : TABLE[ch];
if (ch >= 0x61 && ch <= 0x7a)
lower = true;
else if (ch >= 0x41 && ch <= 0x5a)
upper = true;
if (v === -1)
throw new Error('Invalid bech32 character.');
chk = polymod(chk) ^ v;
if (i + 6 < str.length)
data[i - (1 + hlen)] = v;
i++;
}
if (lower && upper)
throw new Error('Invalid bech32 casing.');
if (chk !== 1)
throw new Error('Invalid bech32 checksum.');
return [hrp, data.slice(0, dlen)];
}
/**
* Convert serialized data to bits,
* suitable to be serialized as bech32.
* @param {Buffer} data
* @param {Buffer} output
* @param {Number} frombits
* @param {Number} tobits
* @param {Number} pad
* @param {Number} off
* @returns {Buffer}
*/
function convert(data, output, frombits, tobits, pad, off) {
const maxv = (1 << tobits) - 1;
let acc = 0;
let bits = 0;
let j = 0;
if (pad !== -1)
output[j++] = pad;
for (let i = off; i < data.length; i++) {
const value = data[i];
if ((value >> frombits) !== 0)
throw new Error('Invalid bech32 bits.');
acc = (acc << frombits) | value;
bits += frombits;
while (bits >= tobits) {
bits -= tobits;
output[j++] = (acc >>> bits) & maxv;
}
}
if (pad !== -1) {
if (bits > 0)
output[j++] = (acc << (tobits - bits)) & maxv;
} else {
if (bits >= frombits || ((acc << (tobits - bits)) & maxv))
throw new Error('Invalid bech32 bits.');
}
return output.slice(0, j);
}
/**
* Serialize data to bech32 address.
* @param {String} hrp
* @param {Number} version
* @param {Buffer} hash
* @returns {String}
*/
function encode(hrp, version, hash) {
assert(typeof hrp === 'string');
assert((version & 0xff) === version);
assert(Buffer.isBuffer(hash));
const output = POOL65;
if (version < 0 || version > 16)
throw new Error('Invalid bech32 version.');
if (hash.length < 2 || hash.length > 40)
throw new Error('Invalid bech32 data length.');
const data = convert(hash, output, 8, 5, version, 0);
return serialize(hrp, data);
}
if (native)
encode = native.toBech32;
/**
* Deserialize data from bech32 address.
* @param {String} str
* @returns {Object}
*/
function decode(str) {
assert(typeof str === 'string');
const [hrp, data] = deserialize(str);
if (data.length === 0 || data.length > 65)
throw new Error('Invalid bech32 data length.');
if (data[0] > 16)
throw new Error('Invalid bech32 version.');
const version = data[0];
const output = data;
const hash = convert(data, output, 5, 8, -1, 1);
if (hash.length < 2 || hash.length > 40)
throw new Error('Invalid bech32 data length.');
return new AddrResult(hrp, version, hash);
}
if (native)
decode = native.fromBech32;
/**
* Test whether a string is bech32 (note that
* this doesn't guarantee address is bech32).
* @param {String?} str
* @returns {Boolean}
*/
function isBech32(str) {
if (typeof str !== 'string')
return false;
if (str.toUpperCase() !== str && str.toLowerCase() !== str)
return false;
if (str.length < 8 || str.length > 90)
return false;
// it's unlikely any network will have hrp other than a-z symbols.
return /^[a-zA-Z]{1,3}1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+$/i.test(str);
}
/**
* AddrResult
* @constructor
* @private
* @param {String} hrp
* @param {Number} version
* @param {Buffer} hash
* @property {String} hrp
* @property {Number} version
* @property {Buffer} hash
*/
function AddrResult(hrp, version, hash) {
this.hrp = hrp;
this.version = version;
this.hash = hash;
}
/*
* Expose
*/
exports.deserialize = deserialize;
exports.serialize = serialize;
exports.convert = convert;
exports.encode = encode;
exports.decode = decode;
exports.isBech32 = isBech32;

View File

@ -13,8 +13,6 @@
exports.AsyncEmitter = require('./asyncemitter');
exports.AsyncObject = require('./asyncobject');
exports.base32 = require('./base32');
exports.base58 = require('./base58');
exports.bech32 = require('./bech32');
exports.binary = require('./binary');
exports.co = require('./co');
exports.enforce = require('./enforce');

View File

@ -10,7 +10,7 @@
const assert = require('assert');
const path = require('path');
const {Server} = require('bweb');
const base58 = require('../utils/base58');
const base58 = require('bstr/lib/base58');
const MTX = require('../primitives/mtx');
const Outpoint = require('../primitives/outpoint');
const Script = require('../script/script');

View File

@ -16,7 +16,7 @@ const digest = require('bcrypto/lib/digest');
const cleanse = require('bcrypto/lib/cleanse');
const BufferReader = require('bbuf/lib/reader');
const StaticWriter = require('bbuf/lib/staticwriter');
const base58 = require('../utils/base58');
const base58 = require('bstr/lib/base58');
const TXDB = require('./txdb');
const Path = require('./path');
const common = require('./common');

View File

@ -23,18 +23,18 @@
"node": ">=7.6.0"
},
"dependencies": {
"bbuf": "^0.0.1",
"bclient": "^0.0.1",
"bcrypto": "^0.0.1",
"bdb": "^0.0.1",
"bfile": "^0.0.1",
"bfilter": "^0.0.1",
"breq": "^0.0.1",
"bsock": "^0.0.1",
"bstr": "^0.0.1",
"bweb": "^0.0.1",
"n64": "^0.0.18"
},
"optionalDependencies": {
"bcoin-native": "0.0.23"
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",

View File

@ -27,19 +27,9 @@
'use strict';
const assert = require('./util/assert');
const bech32 = require('../lib/utils/bech32');
const bech32 = require('bstr/lib/bech32');
const Address = require('../lib/primitives/address');
const validChecksums = [
'A12UEL5L',
'an83characterlonghumanreadablepartthatcontains'
+ 'thenumber1andtheexcludedcharactersbio1tt5tgs',
'abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw',
'11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq'
+ 'qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j',
'split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w'
];
const validAddresses = [
[
'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4',
@ -104,16 +94,6 @@ const invalidAddresses = [
'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv'
];
const validBech32Tests = [
'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4',
'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7',
'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw50'
+ '8d6qejxtdg4y5r3zarvary0c5xw7k7grplx',
'BC1SW50QA3JX3S',
'bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj',
'tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy'
];
function fromAddress(hrp, addr) {
const dec = bech32.decode(addr);
@ -146,49 +126,6 @@ function createProgram(version, program) {
}
describe('Bech32', function() {
for (const addr of validChecksums) {
it(`should have valid checksum for ${addr}`, () => {
assert(bech32.deserialize(addr));
});
}
for (const [addr, script] of validAddresses) {
it(`should have valid address for ${addr}`, () => {
let hrp = 'bc';
let ret = null;
try {
ret = fromAddress(hrp, addr);
} catch (e) {
ret = null;
}
if (ret === null) {
hrp = 'tb';
try {
ret = fromAddress(hrp, addr);
} catch (e) {
ret = null;
}
}
assert(ret !== null);
const output = createProgram(ret.version, ret.program);
assert.bufferEqual(output, script);
const recreate = toAddress(hrp, ret.version, ret.program);
assert.strictEqual(recreate, addr.toLowerCase());
});
}
for (const addr of invalidAddresses) {
it(`should have invalid address for ${addr}`, () => {
assert.throws(() => fromAddress('bc', addr));
assert.throws(() => fromAddress('tb', addr));
});
}
for (const [addr, script] of validAddresses) {
it(`should have valid address for ${addr}`, () => {
let ret = null;
@ -226,10 +163,4 @@ describe('Bech32', function() {
assert.throws(() => Address.fromBech32(addr, 'testnet'));
});
}
it('should validate bech32 addresses based only on string data', () => {
for (const bech32addr of validBech32Tests) {
assert.strictEqual(bech32.isBech32(bech32addr), true);
}
});
});

View File

@ -5,7 +5,7 @@
const assert = require('./util/assert');
const HD = require('../lib/hd');
const base58 = require('../lib/utils/base58');
const base58 = require('bstr/lib/base58');
const pbkdf2 = require('bcrypto/lib/pbkdf2');
const sha512 = require('bcrypto/lib/sha512');
const vectors = require('./data/hd.json');

View File

@ -5,7 +5,7 @@
const assert = require('./util/assert');
const {U64, I64} = require('n64');
const base58 = require('../lib/utils/base58');
const base58 = require('bstr/lib/base58');
const encoding = require('bbuf/lib/encoding');
const Amount = require('../lib/btc/amount');
const Validator = require('../lib/utils/validator');