bcoin: move encoding constants around.

This commit is contained in:
Christopher Jeffrey 2017-11-16 23:52:14 -08:00
parent 9b269dd1f0
commit f2abdf68cb
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
16 changed files with 89 additions and 23 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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)

View File

@ -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) {

View File

@ -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);

View File

@ -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;
}
/**

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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;
};

View File

@ -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);

View File

@ -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

View File

@ -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));