bcoin: move encoding constants around.
This commit is contained in:
parent
9b269dd1f0
commit
f2abdf68cb
@ -127,3 +127,11 @@ common.isAccount = function isAccount(key, account) {
|
|||||||
}
|
}
|
||||||
return key.depth === 3 && (key.childIndex & common.HARDENED) !== 0;
|
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);
|
||||||
|
|||||||
@ -56,7 +56,7 @@ class HDPrivateKey {
|
|||||||
this.chainCode = encoding.ZERO_HASH;
|
this.chainCode = encoding.ZERO_HASH;
|
||||||
this.privateKey = encoding.ZERO_HASH;
|
this.privateKey = encoding.ZERO_HASH;
|
||||||
|
|
||||||
this.publicKey = encoding.ZERO_KEY;
|
this.publicKey = common.ZERO_KEY;
|
||||||
this.fingerPrint = -1;
|
this.fingerPrint = -1;
|
||||||
|
|
||||||
this._hdPublicKey = null;
|
this._hdPublicKey = null;
|
||||||
|
|||||||
@ -46,7 +46,7 @@ class HDPublicKey {
|
|||||||
this.parentFingerPrint = 0;
|
this.parentFingerPrint = 0;
|
||||||
this.childIndex = 0;
|
this.childIndex = 0;
|
||||||
this.chainCode = encoding.ZERO_HASH;
|
this.chainCode = encoding.ZERO_HASH;
|
||||||
this.publicKey = encoding.ZERO_KEY;
|
this.publicKey = common.ZERO_KEY;
|
||||||
|
|
||||||
this.fingerPrint = -1;
|
this.fingerPrint = -1;
|
||||||
|
|
||||||
|
|||||||
@ -244,7 +244,7 @@ class BlockTemplate {
|
|||||||
input.script.pushInt(this.height);
|
input.script.pushInt(this.height);
|
||||||
|
|
||||||
// Coinbase flags.
|
// Coinbase flags.
|
||||||
input.script.pushData(encoding.ZERO_HASH160);
|
input.script.pushData(Buffer.alloc(20, 0x00));
|
||||||
|
|
||||||
// Smaller nonce for good measure.
|
// Smaller nonce for good measure.
|
||||||
const nonce = Buffer.allocUnsafe(4);
|
const nonce = Buffer.allocUnsafe(4);
|
||||||
@ -253,7 +253,7 @@ class BlockTemplate {
|
|||||||
|
|
||||||
// Extra nonce: incremented when
|
// Extra nonce: incremented when
|
||||||
// the nonce overflows.
|
// the nonce overflows.
|
||||||
input.script.pushData(encoding.ZERO_U64);
|
input.script.pushData(Buffer.alloc(8, 0x00));
|
||||||
|
|
||||||
input.script.compile();
|
input.script.compile();
|
||||||
|
|
||||||
@ -267,7 +267,7 @@ class BlockTemplate {
|
|||||||
|
|
||||||
// Reward output.
|
// Reward output.
|
||||||
const output = new Output();
|
const output = new Output();
|
||||||
output.script.fromPubkeyhash(encoding.ZERO_HASH160);
|
output.script.fromPubkeyhash(Buffer.alloc(20, 0x00));
|
||||||
output.value = this.getReward();
|
output.value = this.getReward();
|
||||||
|
|
||||||
cb.outputs.push(output);
|
cb.outputs.push(output);
|
||||||
|
|||||||
@ -23,6 +23,7 @@ const hash256 = require('bcrypto/lib/hash256');
|
|||||||
const random = require('bcrypto/lib/random');
|
const random = require('bcrypto/lib/random');
|
||||||
const secp256k1 = require('bcrypto/lib/secp256k1');
|
const secp256k1 = require('bcrypto/lib/secp256k1');
|
||||||
const packets = require('./packets');
|
const packets = require('./packets');
|
||||||
|
const common = require('./common');
|
||||||
const {encoding} = bio;
|
const {encoding} = bio;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,7 +135,7 @@ class BIP150 extends EventEmitter {
|
|||||||
const msg = this.hash(this.input.sid, type, this.publicKey);
|
const msg = this.hash(this.input.sid, type, this.publicKey);
|
||||||
|
|
||||||
if (!ccmp(hash, msg))
|
if (!ccmp(hash, msg))
|
||||||
return encoding.ZERO_SIG64;
|
return common.ZERO_SIG;
|
||||||
|
|
||||||
if (this.isAuthed()) {
|
if (this.isAuthed()) {
|
||||||
this.auth = true;
|
this.auth = true;
|
||||||
@ -162,7 +163,7 @@ class BIP150 extends EventEmitter {
|
|||||||
assert(!this.replyReceived, 'Peer replied twice.');
|
assert(!this.replyReceived, 'Peer replied twice.');
|
||||||
this.replyReceived = true;
|
this.replyReceived = true;
|
||||||
|
|
||||||
if (data.equals(encoding.ZERO_SIG64))
|
if (data.equals(common.ZERO_SIG))
|
||||||
throw new Error('Auth failure.');
|
throw new Error('Auth failure.');
|
||||||
|
|
||||||
if (!this.peerIdentity)
|
if (!this.peerIdentity)
|
||||||
|
|||||||
@ -25,6 +25,7 @@ const AEAD = require('bcrypto/lib/aead');
|
|||||||
const hkdf = require('bcrypto/lib/hkdf');
|
const hkdf = require('bcrypto/lib/hkdf');
|
||||||
const secp256k1 = require('bcrypto/lib/secp256k1');
|
const secp256k1 = require('bcrypto/lib/secp256k1');
|
||||||
const packets = require('./packets');
|
const packets = require('./packets');
|
||||||
|
const common = require('./common');
|
||||||
const {encoding} = bio;
|
const {encoding} = bio;
|
||||||
const {EncinitPacket, EncackPacket} = packets;
|
const {EncinitPacket, EncackPacket} = packets;
|
||||||
|
|
||||||
@ -394,7 +395,7 @@ class BIP151 extends EventEmitter {
|
|||||||
|
|
||||||
toRekey() {
|
toRekey() {
|
||||||
assert(this.handshake, 'Cannot rekey before handshake.');
|
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) {
|
encack(publicKey) {
|
||||||
assert(this.initSent, 'Unsolicited ACK.');
|
assert(this.initSent, 'Unsolicited ACK.');
|
||||||
|
|
||||||
if (publicKey.equals(encoding.ZERO_KEY)) {
|
if (publicKey.equals(common.ZERO_KEY)) {
|
||||||
assert(this.handshake, 'No initialization before rekey.');
|
assert(this.handshake, 'No initialization before rekey.');
|
||||||
|
|
||||||
if (this.bip150 && this.bip150.auth) {
|
if (this.bip150 && this.bip150.auth) {
|
||||||
|
|||||||
@ -171,3 +171,27 @@ exports.nonce = function nonce() {
|
|||||||
data.writeUInt32LE(Math.random() * 0x100000000, true, 4);
|
data.writeUInt32LE(Math.random() * 0x100000000, true, 4);
|
||||||
return data;
|
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);
|
||||||
|
|||||||
@ -218,7 +218,7 @@ class VersionPacket extends Packet {
|
|||||||
this.time = util.now();
|
this.time = util.now();
|
||||||
this.remote = new NetAddress();
|
this.remote = new NetAddress();
|
||||||
this.local = new NetAddress();
|
this.local = new NetAddress();
|
||||||
this.nonce = encoding.ZERO_U64;
|
this.nonce = common.ZERO_NONCE;
|
||||||
this.agent = common.USER_AGENT;
|
this.agent = common.USER_AGENT;
|
||||||
this.height = 0;
|
this.height = 0;
|
||||||
this.noRelay = false;
|
this.noRelay = false;
|
||||||
@ -555,7 +555,7 @@ class PongPacket extends Packet {
|
|||||||
this.cmd = 'pong';
|
this.cmd = 'pong';
|
||||||
this.type = exports.types.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.cmd = 'encinit';
|
||||||
this.type = exports.types.ENCINIT;
|
this.type = exports.types.ENCINIT;
|
||||||
|
|
||||||
this.publicKey = publicKey || encoding.ZERO_KEY;
|
this.publicKey = publicKey || common.ZERO_KEY;
|
||||||
this.cipher = cipher || 0;
|
this.cipher = cipher || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2758,7 +2758,7 @@ class EncackPacket extends Packet {
|
|||||||
this.cmd = 'encack';
|
this.cmd = 'encack';
|
||||||
this.type = exports.types.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.cmd = 'authreply';
|
||||||
this.type = exports.types.AUTHREPLY;
|
this.type = exports.types.AUTHREPLY;
|
||||||
|
|
||||||
this.signature = signature || encoding.ZERO_SIG64;
|
this.signature = signature || common.ZERO_SIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1685,7 +1685,7 @@ class Peer extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!nonce.equals(this.challenge)) {
|
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.logger.debug('Peer sent a zero nonce (%s).', this.hostname());
|
||||||
this.challenge = null;
|
this.challenge = null;
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -16,6 +16,12 @@ const hash256 = require('bcrypto/lib/hash256');
|
|||||||
const Network = require('../protocol/network');
|
const Network = require('../protocol/network');
|
||||||
const {encoding} = bio;
|
const {encoding} = bio;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Constants
|
||||||
|
*/
|
||||||
|
|
||||||
|
const ZERO_HASH160 = Buffer.alloc(20, 0x00);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Address
|
* Address
|
||||||
* Represents an address.
|
* Represents an address.
|
||||||
@ -35,7 +41,7 @@ class Address {
|
|||||||
constructor(options, network) {
|
constructor(options, network) {
|
||||||
this.type = Address.types.PUBKEYHASH;
|
this.type = Address.types.PUBKEYHASH;
|
||||||
this.version = -1;
|
this.version = -1;
|
||||||
this.hash = encoding.ZERO_HASH160;
|
this.hash = ZERO_HASH160;
|
||||||
|
|
||||||
if (options)
|
if (options)
|
||||||
this.fromOptions(options, network);
|
this.fromOptions(options, network);
|
||||||
@ -87,7 +93,7 @@ class Address {
|
|||||||
|
|
||||||
isNull() {
|
isNull() {
|
||||||
if (this.hash.length === 20)
|
if (this.hash.length === 20)
|
||||||
return this.hash.equals(encoding.ZERO_HASH160);
|
return this.hash.equals(ZERO_HASH160);
|
||||||
|
|
||||||
if (this.hash.length === 32)
|
if (this.hash.length === 32)
|
||||||
return this.hash.equals(encoding.ZERO_HASH);
|
return this.hash.equals(encoding.ZERO_HASH);
|
||||||
|
|||||||
@ -19,6 +19,12 @@ const Output = require('./output');
|
|||||||
const secp256k1 = require('bcrypto/lib/secp256k1');
|
const secp256k1 = require('bcrypto/lib/secp256k1');
|
||||||
const {encoding} = bio;
|
const {encoding} = bio;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Constants
|
||||||
|
*/
|
||||||
|
|
||||||
|
const ZERO_KEY = Buffer.alloc(33, 0x00);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Key Ring
|
* Key Ring
|
||||||
* Represents a key ring which amounts to an address.
|
* Represents a key ring which amounts to an address.
|
||||||
@ -35,7 +41,7 @@ class KeyRing {
|
|||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.witness = false;
|
this.witness = false;
|
||||||
this.nested = false;
|
this.nested = false;
|
||||||
this.publicKey = encoding.ZERO_KEY;
|
this.publicKey = ZERO_KEY;
|
||||||
this.privateKey = null;
|
this.privateKey = null;
|
||||||
this.script = null;
|
this.script = null;
|
||||||
|
|
||||||
|
|||||||
@ -1841,7 +1841,7 @@ class CoinSelector {
|
|||||||
} else {
|
} else {
|
||||||
// In case we don't have a change address,
|
// In case we don't have a change address,
|
||||||
// we use a fake p2pkh output to gauge size.
|
// 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);
|
this.tx.outputs.push(change);
|
||||||
|
|||||||
@ -82,3 +82,21 @@ util.time = function time(date) {
|
|||||||
|
|
||||||
return new Date(date) / 1000 | 0;
|
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;
|
||||||
|
};
|
||||||
|
|||||||
@ -19,6 +19,8 @@ const common = require('../lib/blockchain/common');
|
|||||||
const Opcode = require('../lib/script/opcode');
|
const Opcode = require('../lib/script/opcode');
|
||||||
const opcodes = Script.opcodes;
|
const opcodes = Script.opcodes;
|
||||||
|
|
||||||
|
const ZERO_KEY = Buffer.alloc(33, 0x00);
|
||||||
|
|
||||||
const ONE_HASH = Buffer.alloc(32, 0x00);
|
const ONE_HASH = Buffer.alloc(32, 0x00);
|
||||||
ONE_HASH[0] = 0x01;
|
ONE_HASH[0] = 0x01;
|
||||||
|
|
||||||
@ -791,7 +793,7 @@ describe('Chain', function() {
|
|||||||
redeem.pushInt(20);
|
redeem.pushInt(20);
|
||||||
|
|
||||||
for (let i = 0; i < 20; i++)
|
for (let i = 0; i < 20; i++)
|
||||||
redeem.pushData(encoding.ZERO_KEY);
|
redeem.pushData(ZERO_KEY);
|
||||||
|
|
||||||
redeem.pushInt(20);
|
redeem.pushInt(20);
|
||||||
redeem.pushOp(opcodes.OP_CHECKMULTISIG);
|
redeem.pushOp(opcodes.OP_CHECKMULTISIG);
|
||||||
@ -834,7 +836,7 @@ describe('Chain', function() {
|
|||||||
script.pushInt(20);
|
script.pushInt(20);
|
||||||
|
|
||||||
for (let i = 0; i < 20; i++)
|
for (let i = 0; i < 20; i++)
|
||||||
script.pushData(encoding.ZERO_KEY);
|
script.pushData(ZERO_KEY);
|
||||||
|
|
||||||
script.pushInt(20);
|
script.pushInt(20);
|
||||||
script.pushOp(opcodes.OP_CHECKMULTISIG);
|
script.pushOp(opcodes.OP_CHECKMULTISIG);
|
||||||
|
|||||||
@ -132,7 +132,7 @@ describe('Mempool', function() {
|
|||||||
|
|
||||||
// Fake signature
|
// Fake signature
|
||||||
const input = fake.inputs[0];
|
const input = fake.inputs[0];
|
||||||
input.script.setData(0, encoding.ZERO_SIG);
|
input.script.setData(0, Buffer.alloc(73, 0x00));
|
||||||
input.script.compile();
|
input.script.compile();
|
||||||
// balance: 11000
|
// balance: 11000
|
||||||
|
|
||||||
|
|||||||
@ -214,7 +214,7 @@ async function testP2SH(witness, nesting) {
|
|||||||
assert((await carol.changeAddress()).equals(change2));
|
assert((await carol.changeAddress()).equals(change2));
|
||||||
|
|
||||||
const input = tx.inputs[0];
|
const input = tx.inputs[0];
|
||||||
input[vector].setData(2, encoding.ZERO_SIG);
|
input[vector].setData(2, Buffer.alloc(73, 0x00));
|
||||||
input[vector].compile();
|
input[vector].compile();
|
||||||
|
|
||||||
assert(!tx.verify(view, flags));
|
assert(!tx.verify(view, flags));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user