From f2abdf68cb8bb43419dc4c6050c27c5404fdc8e5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 16 Nov 2017 23:52:14 -0800 Subject: [PATCH] bcoin: move encoding constants around. --- lib/hd/common.js | 8 ++++++++ lib/hd/private.js | 2 +- lib/hd/public.js | 2 +- lib/mining/template.js | 6 +++--- lib/net/bip150.js | 5 +++-- lib/net/bip151.js | 5 +++-- lib/net/common.js | 24 ++++++++++++++++++++++++ lib/net/packets.js | 10 +++++----- lib/net/peer.js | 2 +- lib/primitives/address.js | 10 ++++++++-- lib/primitives/keyring.js | 8 +++++++- lib/primitives/mtx.js | 2 +- lib/utils/util.js | 18 ++++++++++++++++++ test/chain-test.js | 6 ++++-- test/mempool-test.js | 2 +- test/wallet-test.js | 2 +- 16 files changed, 89 insertions(+), 23 deletions(-) diff --git a/lib/hd/common.js b/lib/hd/common.js index 659a6f34..cb066b9e 100644 --- a/lib/hd/common.js +++ b/lib/hd/common.js @@ -127,3 +127,11 @@ common.isAccount = function isAccount(key, account) { } return key.depth === 3 && (key.childIndex & common.HARDENED) !== 0; }; + +/** + * A compressed pubkey of all zeroes. + * @const {Buffer} + * @default + */ + +common.ZERO_KEY = Buffer.alloc(33, 0x00); diff --git a/lib/hd/private.js b/lib/hd/private.js index 1c5a1137..cfa9ebe5 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -56,7 +56,7 @@ class HDPrivateKey { this.chainCode = encoding.ZERO_HASH; this.privateKey = encoding.ZERO_HASH; - this.publicKey = encoding.ZERO_KEY; + this.publicKey = common.ZERO_KEY; this.fingerPrint = -1; this._hdPublicKey = null; diff --git a/lib/hd/public.js b/lib/hd/public.js index 07dbd3ff..3df86c48 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -46,7 +46,7 @@ class HDPublicKey { this.parentFingerPrint = 0; this.childIndex = 0; this.chainCode = encoding.ZERO_HASH; - this.publicKey = encoding.ZERO_KEY; + this.publicKey = common.ZERO_KEY; this.fingerPrint = -1; diff --git a/lib/mining/template.js b/lib/mining/template.js index 60d3533a..33cc9e4c 100644 --- a/lib/mining/template.js +++ b/lib/mining/template.js @@ -244,7 +244,7 @@ class BlockTemplate { input.script.pushInt(this.height); // Coinbase flags. - input.script.pushData(encoding.ZERO_HASH160); + input.script.pushData(Buffer.alloc(20, 0x00)); // Smaller nonce for good measure. const nonce = Buffer.allocUnsafe(4); @@ -253,7 +253,7 @@ class BlockTemplate { // Extra nonce: incremented when // the nonce overflows. - input.script.pushData(encoding.ZERO_U64); + input.script.pushData(Buffer.alloc(8, 0x00)); input.script.compile(); @@ -267,7 +267,7 @@ class BlockTemplate { // Reward output. const output = new Output(); - output.script.fromPubkeyhash(encoding.ZERO_HASH160); + output.script.fromPubkeyhash(Buffer.alloc(20, 0x00)); output.value = this.getReward(); cb.outputs.push(output); diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 583079ba..97645547 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -23,6 +23,7 @@ const hash256 = require('bcrypto/lib/hash256'); const random = require('bcrypto/lib/random'); const secp256k1 = require('bcrypto/lib/secp256k1'); const packets = require('./packets'); +const common = require('./common'); const {encoding} = bio; /** @@ -134,7 +135,7 @@ class BIP150 extends EventEmitter { const msg = this.hash(this.input.sid, type, this.publicKey); if (!ccmp(hash, msg)) - return encoding.ZERO_SIG64; + return common.ZERO_SIG; if (this.isAuthed()) { this.auth = true; @@ -162,7 +163,7 @@ class BIP150 extends EventEmitter { assert(!this.replyReceived, 'Peer replied twice.'); this.replyReceived = true; - if (data.equals(encoding.ZERO_SIG64)) + if (data.equals(common.ZERO_SIG)) throw new Error('Auth failure.'); if (!this.peerIdentity) diff --git a/lib/net/bip151.js b/lib/net/bip151.js index 1d8f68e0..6fc913bd 100644 --- a/lib/net/bip151.js +++ b/lib/net/bip151.js @@ -25,6 +25,7 @@ const AEAD = require('bcrypto/lib/aead'); const hkdf = require('bcrypto/lib/hkdf'); const secp256k1 = require('bcrypto/lib/secp256k1'); const packets = require('./packets'); +const common = require('./common'); const {encoding} = bio; const {EncinitPacket, EncackPacket} = packets; @@ -394,7 +395,7 @@ class BIP151 extends EventEmitter { toRekey() { assert(this.handshake, 'Cannot rekey before handshake.'); - return new EncackPacket(encoding.ZERO_KEY); + return new EncackPacket(common.ZERO_KEY); } /** @@ -418,7 +419,7 @@ class BIP151 extends EventEmitter { encack(publicKey) { assert(this.initSent, 'Unsolicited ACK.'); - if (publicKey.equals(encoding.ZERO_KEY)) { + if (publicKey.equals(common.ZERO_KEY)) { assert(this.handshake, 'No initialization before rekey.'); if (this.bip150 && this.bip150.auth) { diff --git a/lib/net/common.js b/lib/net/common.js index 0a09ba9c..13b78c0f 100644 --- a/lib/net/common.js +++ b/lib/net/common.js @@ -171,3 +171,27 @@ exports.nonce = function nonce() { data.writeUInt32LE(Math.random() * 0x100000000, true, 4); return data; }; + +/** + * A compressed pubkey of all zeroes. + * @const {Buffer} + * @default + */ + +exports.ZERO_KEY = Buffer.alloc(33, 0x00); + +/** + * A 64 byte signature of all zeroes. + * @const {Buffer} + * @default + */ + +exports.ZERO_SIG = Buffer.alloc(64, 0x00); + +/** + * 8 zero bytes. + * @const {Buffer} + * @default + */ + +exports.ZERO_NONCE = Buffer.alloc(8, 0x00); diff --git a/lib/net/packets.js b/lib/net/packets.js index e14b1a34..b742bf74 100644 --- a/lib/net/packets.js +++ b/lib/net/packets.js @@ -218,7 +218,7 @@ class VersionPacket extends Packet { this.time = util.now(); this.remote = new NetAddress(); this.local = new NetAddress(); - this.nonce = encoding.ZERO_U64; + this.nonce = common.ZERO_NONCE; this.agent = common.USER_AGENT; this.height = 0; this.noRelay = false; @@ -555,7 +555,7 @@ class PongPacket extends Packet { this.cmd = 'pong'; this.type = exports.types.PONG; - this.nonce = nonce || encoding.ZERO_U64; + this.nonce = nonce || common.ZERO_NONCE; } /** @@ -2660,7 +2660,7 @@ class EncinitPacket extends Packet { this.cmd = 'encinit'; this.type = exports.types.ENCINIT; - this.publicKey = publicKey || encoding.ZERO_KEY; + this.publicKey = publicKey || common.ZERO_KEY; this.cipher = cipher || 0; } @@ -2758,7 +2758,7 @@ class EncackPacket extends Packet { this.cmd = 'encack'; this.type = exports.types.ENCACK; - this.publicKey = publicKey || encoding.ZERO_KEY; + this.publicKey = publicKey || common.ZERO_KEY; } /** @@ -2948,7 +2948,7 @@ class AuthReplyPacket extends Packet { this.cmd = 'authreply'; this.type = exports.types.AUTHREPLY; - this.signature = signature || encoding.ZERO_SIG64; + this.signature = signature || common.ZERO_SIG; } /** diff --git a/lib/net/peer.js b/lib/net/peer.js index 9495c0e5..8a7c91d4 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -1685,7 +1685,7 @@ class Peer extends EventEmitter { } if (!nonce.equals(this.challenge)) { - if (nonce.equals(encoding.ZERO_U64)) { + if (nonce.equals(common.ZERO_NONCE)) { this.logger.debug('Peer sent a zero nonce (%s).', this.hostname()); this.challenge = null; return; diff --git a/lib/primitives/address.js b/lib/primitives/address.js index 81009c6a..2796a068 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -16,6 +16,12 @@ const hash256 = require('bcrypto/lib/hash256'); const Network = require('../protocol/network'); const {encoding} = bio; +/* + * Constants + */ + +const ZERO_HASH160 = Buffer.alloc(20, 0x00); + /** * Address * Represents an address. @@ -35,7 +41,7 @@ class Address { constructor(options, network) { this.type = Address.types.PUBKEYHASH; this.version = -1; - this.hash = encoding.ZERO_HASH160; + this.hash = ZERO_HASH160; if (options) this.fromOptions(options, network); @@ -87,7 +93,7 @@ class Address { isNull() { if (this.hash.length === 20) - return this.hash.equals(encoding.ZERO_HASH160); + return this.hash.equals(ZERO_HASH160); if (this.hash.length === 32) return this.hash.equals(encoding.ZERO_HASH); diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index a6f0e8c2..a4225789 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -19,6 +19,12 @@ const Output = require('./output'); const secp256k1 = require('bcrypto/lib/secp256k1'); const {encoding} = bio; +/* + * Constants + */ + +const ZERO_KEY = Buffer.alloc(33, 0x00); + /** * Key Ring * Represents a key ring which amounts to an address. @@ -35,7 +41,7 @@ class KeyRing { constructor(options) { this.witness = false; this.nested = false; - this.publicKey = encoding.ZERO_KEY; + this.publicKey = ZERO_KEY; this.privateKey = null; this.script = null; diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index d9940205..0dae4a26 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -1841,7 +1841,7 @@ class CoinSelector { } else { // In case we don't have a change address, // we use a fake p2pkh output to gauge size. - change.script.fromPubkeyhash(encoding.ZERO_HASH160); + change.script.fromPubkeyhash(Buffer.allocUnsafe(20)); } this.tx.outputs.push(change); diff --git a/lib/utils/util.js b/lib/utils/util.js index 2719ce16..11fac487 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -82,3 +82,21 @@ util.time = function time(date) { return new Date(date) / 1000 | 0; }; + +/** + * Reverse a hex-string. + * @param {String} str - Hex string. + * @returns {String} Reversed hex string. + */ + +util.revHex = function revHex(str) { + assert(typeof str === 'string'); + assert((str.length & 1) === 0); + + let out = ''; + + for (let i = str.length - 2; i >= 0; i -= 2) + out += str[i] + str[i + 1]; + + return out; +}; diff --git a/test/chain-test.js b/test/chain-test.js index 62549f99..27f5bd9f 100644 --- a/test/chain-test.js +++ b/test/chain-test.js @@ -19,6 +19,8 @@ const common = require('../lib/blockchain/common'); const Opcode = require('../lib/script/opcode'); const opcodes = Script.opcodes; +const ZERO_KEY = Buffer.alloc(33, 0x00); + const ONE_HASH = Buffer.alloc(32, 0x00); ONE_HASH[0] = 0x01; @@ -791,7 +793,7 @@ describe('Chain', function() { redeem.pushInt(20); for (let i = 0; i < 20; i++) - redeem.pushData(encoding.ZERO_KEY); + redeem.pushData(ZERO_KEY); redeem.pushInt(20); redeem.pushOp(opcodes.OP_CHECKMULTISIG); @@ -834,7 +836,7 @@ describe('Chain', function() { script.pushInt(20); for (let i = 0; i < 20; i++) - script.pushData(encoding.ZERO_KEY); + script.pushData(ZERO_KEY); script.pushInt(20); script.pushOp(opcodes.OP_CHECKMULTISIG); diff --git a/test/mempool-test.js b/test/mempool-test.js index a8352c21..674a43c5 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -132,7 +132,7 @@ describe('Mempool', function() { // Fake signature const input = fake.inputs[0]; - input.script.setData(0, encoding.ZERO_SIG); + input.script.setData(0, Buffer.alloc(73, 0x00)); input.script.compile(); // balance: 11000 diff --git a/test/wallet-test.js b/test/wallet-test.js index 693aa196..de0f209a 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -214,7 +214,7 @@ async function testP2SH(witness, nesting) { assert((await carol.changeAddress()).equals(change2)); const input = tx.inputs[0]; - input[vector].setData(2, encoding.ZERO_SIG); + input[vector].setData(2, Buffer.alloc(73, 0x00)); input[vector].compile(); assert(!tx.verify(view, flags));