From f9eba3f5a6d6ad442e8637e2bbb1d49e57ad4710 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 1 Nov 2017 15:41:32 -0700 Subject: [PATCH] crypto: start using `hash.digest()`. --- bench/chacha.js | 4 ++-- browser/proxysocket.js | 4 ++-- browser/wsproxy.js | 4 ++-- lib/blockchain/chainentry.js | 4 ++-- lib/hd/mnemonic.js | 6 +++--- lib/hd/private.js | 18 ++++++++++-------- lib/hd/public.js | 12 +++++++----- lib/mining/mine.js | 6 +++--- lib/net/bip152.js | 4 ++-- lib/net/framer.js | 4 ++-- lib/net/parser.js | 5 +++-- lib/node/http.js | 24 +++++++++++++----------- lib/node/rpc.js | 9 +++++---- lib/primitives/abstractblock.js | 4 ++-- lib/primitives/address.js | 18 ++++++++++-------- lib/primitives/keyring.js | 11 ++++++----- lib/primitives/tx.js | 18 +++++++++--------- lib/script/script.js | 32 +++++++++++++++++++------------- lib/wallet/http.js | 9 +++++---- lib/wallet/rpc.js | 4 ++-- lib/wallet/wallet.js | 9 +++++---- migrate/chaindb2to3.js | 4 ++-- migrate/ensure-tip-index.js | 4 ++-- test/wallet-test.js | 8 ++++---- 24 files changed, 122 insertions(+), 103 deletions(-) diff --git a/bench/chacha.js b/bench/chacha.js index 4bc66e7c..a1f3161c 100644 --- a/bench/chacha.js +++ b/bench/chacha.js @@ -2,7 +2,7 @@ const ChaCha20 = require('bcrypto/lib/chacha20'); const Poly1305 = require('bcrypto/lib/poly1305'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const bench = require('./bench'); console.log('note: rate measured in kb/s'); @@ -51,6 +51,6 @@ poly.init(key); { const end = bench('sha256'); for (let i = 0; i < 1000000; i++) - digest.hash256(data); + hash256.digest(data); end(1000000 * 32 / 1024); } diff --git a/browser/proxysocket.js b/browser/proxysocket.js index 5a7844a1..49df2512 100644 --- a/browser/proxysocket.js +++ b/browser/proxysocket.js @@ -9,7 +9,7 @@ const assert = require('assert'); const EventEmitter = require('events'); const bsock = require('bsock'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const BufferWriter = require('bbuf/lib/writer'); function ProxySocket(uri) { @@ -133,7 +133,7 @@ ProxySocket.prototype.connect = function connect(port, host) { nonce++; assert(nonce <= 0xffffffff, 'Could not create socket.'); pow.writeUInt32LE(nonce, 0, true); - } while (digest.hash256(pow).compare(this.target) > 0); + } while (hash256.digest(pow).compare(this.target) > 0); console.log('Solved proof of work: %d', nonce); } diff --git a/browser/wsproxy.js b/browser/wsproxy.js index 22f7b0b9..0d26e94a 100644 --- a/browser/wsproxy.js +++ b/browser/wsproxy.js @@ -4,7 +4,7 @@ const assert = require('assert'); const net = require('net'); const EventEmitter = require('events'); const bsock = require('bsock'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const IP = require('binet'); const BufferWriter = require('bbuf/lib/writer'); @@ -100,7 +100,7 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce) const pow = bw.render(); - if (digest.hash256(pow).compare(this.target) > 0) { + if (hash256.digest(pow).compare(this.target) > 0) { this.log('Client did not solve proof of work (%s).', state.host); ws.fire('tcp close'); ws.destroy(); diff --git a/lib/blockchain/chainentry.js b/lib/blockchain/chainentry.js index 68661b3a..8360471b 100644 --- a/lib/blockchain/chainentry.js +++ b/lib/blockchain/chainentry.js @@ -10,7 +10,7 @@ const assert = require('assert'); const BN = require('bcrypto/lib/bn'); const consensus = require('../protocol/consensus'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const encoding = require('bbuf/lib/encoding'); const BufferReader = require('bbuf/lib/reader'); const StaticWriter = require('bbuf/lib/staticwriter'); @@ -241,7 +241,7 @@ ChainEntry.prototype.toRaw = function toRaw() { ChainEntry.prototype.fromRaw = function fromRaw(data) { const br = new BufferReader(data, true); - const hash = digest.hash256(br.readBytes(80)); + const hash = hash256.digest(br.readBytes(80)); br.seek(-80); diff --git a/lib/hd/mnemonic.js b/lib/hd/mnemonic.js index 209ede5a..9d3aafac 100644 --- a/lib/hd/mnemonic.js +++ b/lib/hd/mnemonic.js @@ -7,7 +7,7 @@ 'use strict'; const assert = require('assert'); -const digest = require('bcrypto/lib/digest'); +const sha256 = require('bcrypto/lib/sha256'); const cleanse = require('bcrypto/lib/cleanse'); const random = require('bcrypto/lib/random'); const pbkdf2 = require('bcrypto/lib/pbkdf2'); @@ -189,7 +189,7 @@ Mnemonic.prototype.getPhrase = function getPhrase() { // Get entropy and checksum. const entropy = this.getEntropy(); - const chk = digest.sha256(entropy); + const chk = sha256.digest(entropy); // Append the hash to the entropy to // make things easy when grabbing @@ -276,7 +276,7 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) { const cbytes = Math.ceil(cbits / 8); const entropy = data.slice(0, data.length - cbytes); const chk1 = data.slice(data.length - cbytes); - const chk2 = digest.sha256(entropy); + const chk2 = sha256.digest(entropy); // Verify checksum. for (let i = 0; i < cbits; i++) { diff --git a/lib/hd/private.js b/lib/hd/private.js index 26e42f8d..c48fe96c 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -7,15 +7,17 @@ 'use strict'; const assert = require('assert'); -const digest = require('bcrypto/lib/digest'); +const sha512 = require('bcrypto/lib/sha512'); +const hash160 = require('bcrypto/lib/hash160'); +const hash256 = require('bcrypto/lib/hash256'); const cleanse = require('bcrypto/lib/cleanse'); const random = require('bcrypto/lib/random'); 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('bstr/lib/base58'); const encoding = require('bbuf/lib/encoding'); +const base58 = require('bstr/lib/base58'); +const Network = require('../protocol/network'); const common = require('./common'); const Mnemonic = require('./mnemonic'); const HDPublicKey = require('./public'); @@ -199,7 +201,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { const data = bw.render(); - const hash = digest.hmac('sha512', data, this.chainCode); + const hash = sha512.mac(data, this.chainCode); const left = hash.slice(0, 32); const right = hash.slice(32, 64); @@ -211,7 +213,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { } if (this.fingerPrint === -1) { - const fp = digest.hash160(this.publicKey); + const fp = hash160.digest(this.publicKey); this.fingerPrint = fp.readUInt32BE(0, true); } @@ -426,7 +428,7 @@ HDPrivateKey.prototype.fromSeed = function fromSeed(seed) { throw new Error('Entropy not in range.'); } - const hash = digest.hmac('sha512', seed, SEED_SALT); + const hash = sha512.mac(seed, SEED_SALT); const left = hash.slice(0, 32); const right = hash.slice(32, 64); @@ -570,7 +572,7 @@ HDPrivateKey.prototype.fromReader = function fromReader(br, network) { this.privateKey = br.readBytes(32); this.publicKey = secp256k1.publicKeyCreate(this.privateKey, true); - br.verifyChecksum(digest.hash256); + br.verifyChecksum(hash256.digest); return this; }; @@ -621,7 +623,7 @@ HDPrivateKey.prototype.toWriter = function toWriter(bw, network) { bw.writeBytes(this.chainCode); bw.writeU8(0); bw.writeBytes(this.privateKey); - bw.writeChecksum(digest.hash256); + bw.writeChecksum(hash256.digest); return bw; }; diff --git a/lib/hd/public.js b/lib/hd/public.js index a7565964..d49992a7 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -7,7 +7,9 @@ 'use strict'; const assert = require('assert'); -const digest = require('bcrypto/lib/digest'); +const sha512 = require('bcrypto/lib/sha512'); +const hash160 = require('bcrypto/lib/hash160'); +const hash256 = require('bcrypto/lib/hash256'); const cleanse = require('bcrypto/lib/cleanse'); const secp256k1 = require('bcrypto/lib/secp256k1'); const Network = require('../protocol/network'); @@ -160,7 +162,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { const data = bw.render(); - const hash = digest.hmac('sha512', data, this.chainCode); + const hash = sha512.mac(data, this.chainCode); const left = hash.slice(0, 32); const right = hash.slice(32, 64); @@ -172,7 +174,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { } if (this.fingerPrint === -1) { - const fp = digest.hash160(this.publicKey); + const fp = hash160.digest(this.publicKey); this.fingerPrint = fp.readUInt32BE(0, true); } @@ -439,7 +441,7 @@ HDPublicKey.prototype.fromReader = function fromReader(br, network) { this.chainCode = br.readBytes(32); this.publicKey = br.readBytes(33); - br.verifyChecksum(digest.hash256); + br.verifyChecksum(hash256.digest); return this; }; @@ -480,7 +482,7 @@ HDPublicKey.prototype.toWriter = function toWriter(bw, network) { bw.writeU32BE(this.childIndex); bw.writeBytes(this.chainCode); bw.writeBytes(this.publicKey); - bw.writeChecksum(digest.hash256); + bw.writeChecksum(hash256.digest); return bw; }; diff --git a/lib/mining/mine.js b/lib/mining/mine.js index 3def1d5f..e14753b0 100644 --- a/lib/mining/mine.js +++ b/lib/mining/mine.js @@ -7,7 +7,7 @@ 'use strict'; const assert = require('assert'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); /** * Hash until the nonce overflows. @@ -27,11 +27,11 @@ function mine(data, target, min, max) { // The heart and soul of the miner: match the target. while (nonce <= max) { // Hash and test against the next target. - if (rcmp(digest.hash256(data), target) <= 0) + if (rcmp(hash256.digest(data), target) <= 0) return nonce; // Increment the nonce to get a different hash. - nonce++; + nonce += 1; // Update the raw buffer. data.writeUInt32LE(nonce, 76, true); diff --git a/lib/net/bip152.js b/lib/net/bip152.js index b94c9a27..8dcb6573 100644 --- a/lib/net/bip152.js +++ b/lib/net/bip152.js @@ -15,7 +15,7 @@ const BufferReader = require('bbuf/lib/reader'); const StaticWriter = require('bbuf/lib/staticwriter'); const encoding = require('bbuf/lib/encoding'); const consensus = require('../protocol/consensus'); -const digest = require('bcrypto/lib/digest'); +const sha256 = require('bcrypto/lib/sha256'); const siphash256 = require('bcrypto/lib/siphash').siphash256; const AbstractBlock = require('../primitives/abstractblock'); const TX = require('../primitives/tx'); @@ -392,7 +392,7 @@ CompactBlock.prototype.hasIndex = function hasIndex(index) { CompactBlock.prototype.getKey = function getKey() { const data = Buffer.concat([this.toHead(), this.keyNonce]); - const hash = digest.sha256(data); + const hash = sha256.digest(data); return hash.slice(0, 16); }; diff --git a/lib/net/framer.js b/lib/net/framer.js index b90b3944..964ca450 100644 --- a/lib/net/framer.js +++ b/lib/net/framer.js @@ -9,7 +9,7 @@ const assert = require('assert'); const Network = require('../protocol/network'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); /** * Protocol packet framer @@ -53,7 +53,7 @@ Framer.prototype.packet = function packet(cmd, payload, checksum) { msg.writeUInt32LE(payload.length, 16, true); if (!checksum) - checksum = digest.hash256(payload); + checksum = hash256.digest(payload); // Checksum checksum.copy(msg, 20, 0, 4); diff --git a/lib/net/parser.js b/lib/net/parser.js index 00ec9700..705c486c 100644 --- a/lib/net/parser.js +++ b/lib/net/parser.js @@ -13,7 +13,7 @@ const assert = require('assert'); const EventEmitter = require('events'); const {format} = require('util'); const Network = require('../protocol/network'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const common = require('./common'); const packets = require('./packets'); @@ -95,7 +95,8 @@ Parser.prototype.parse = function parse(data) { return; } - const checksum = digest.hash256(data).readUInt32LE(0, true); + const hash = hash256.digest(data); + const checksum = hash.readUInt32LE(0, true); if (checksum !== this.header.checksum) { this.waiting = 24; diff --git a/lib/node/http.js b/lib/node/http.js index 49b60e72..f4b19195 100644 --- a/lib/node/http.js +++ b/lib/node/http.js @@ -10,17 +10,17 @@ const assert = require('assert'); const path = require('path'); const {Server} = require('bweb'); -const util = require('../utils/util'); -const base58 = require('bstr/lib/base58'); -const BloomFilter = require('bfilter/lib/bloom'); -const TX = require('../primitives/tx'); -const Outpoint = require('../primitives/outpoint'); -const digest = require('bcrypto/lib/digest'); +const sha256 = require('bcrypto/lib/sha256'); const random = require('bcrypto/lib/random'); const ccmp = require('bcrypto/lib/ccmp'); -const Network = require('../protocol/network'); -const Validator = require('bval/lib/validator'); const encoding = require('bbuf/lib/encoding'); +const base58 = require('bstr/lib/base58'); +const BloomFilter = require('bfilter/lib/bloom'); +const Validator = require('bval/lib/validator'); +const util = require('../utils/util'); +const TX = require('../primitives/tx'); +const Outpoint = require('../primitives/outpoint'); +const Network = require('../protocol/network'); const pkg = require('../pkg'); class HTTP extends Server { @@ -84,6 +84,7 @@ class HTTP extends Server { if (!this.options.noAuth) { this.use(this.basicAuth({ + hash: sha256.digest, password: this.options.apiKey, realm: 'node' })); @@ -110,6 +111,7 @@ class HTTP extends Server { this.get('/', async (req, res) => { const totalTX = this.mempool ? this.mempool.map.size : 0; const size = this.mempool ? this.mempool.getSize() : 0; + let addr = this.pool.hosts.getLocal(); if (!addr) @@ -359,7 +361,7 @@ class HTTP extends Server { throw new Error('Invalid API key.'); const data = Buffer.from(key, 'ascii'); - const hash = digest.hash256(data); + const hash = sha256.digest(data); if (!ccmp(hash, this.options.apiHash)) throw new Error('Invalid API key.'); @@ -675,7 +677,7 @@ class HTTPOptions { this.logger = null; this.node = null; this.apiKey = base58.encode(random.randomBytes(20)); - this.apiHash = digest.hash256(Buffer.from(this.apiKey, 'ascii')); + this.apiHash = sha256.digest(Buffer.from(this.apiKey, 'ascii')); this.noAuth = false; this.prefix = null; @@ -717,7 +719,7 @@ class HTTPOptions { assert(options.apiKey.length <= 255, 'API key must be under 256 bytes.'); this.apiKey = options.apiKey; - this.apiHash = digest.hash256(Buffer.from(this.apiKey, 'ascii')); + this.apiHash = sha256.digest(Buffer.from(this.apiKey, 'ascii')); } if (options.noAuth != null) { diff --git a/lib/node/rpc.js b/lib/node/rpc.js index 63746d8c..5423d5a5 100644 --- a/lib/node/rpc.js +++ b/lib/node/rpc.js @@ -9,7 +9,8 @@ const assert = require('assert'); const bweb = require('bweb'); const util = require('../utils/util'); -const digest = require('bcrypto/lib/digest'); +const hash160 = require('bcrypto/lib/hash160'); +const hash256 = require('bcrypto/lib/hash256'); const ccmp = require('bcrypto/lib/ccmp'); const common = require('../blockchain/common'); const secp256k1 = require('bcrypto/lib/secp256k1'); @@ -2103,14 +2104,14 @@ class RPC extends RPCBase { const addr = parseAddress(b58, this.network); const msg = Buffer.from(MAGIC_STRING + str, 'utf8'); - const hash = digest.hash256(msg); + const hash = hash256.digest(msg); const key = secp256k1.recover(hash, sig, 0, true); if (!key) return false; - return ccmp(digest.hash160(key), addr.hash); + return ccmp(hash160.digest(key), addr.hash); } async signMessageWithPrivkey(args, help) { @@ -2125,7 +2126,7 @@ class RPC extends RPCBase { const key = parseSecret(wif, this.network); const msg = Buffer.from(MAGIC_STRING + str, 'utf8'); - const hash = digest.hash256(msg); + const hash = hash256.digest(msg); const sig = key.sign(hash); return sig.toString('base64'); diff --git a/lib/primitives/abstractblock.js b/lib/primitives/abstractblock.js index ca2e17bc..6421ab03 100644 --- a/lib/primitives/abstractblock.js +++ b/lib/primitives/abstractblock.js @@ -8,7 +8,7 @@ 'use strict'; const assert = require('assert'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const BufferReader = require('bbuf/lib/reader'); const StaticWriter = require('bbuf/lib/staticwriter'); const InvItem = require('./invitem'); @@ -137,7 +137,7 @@ AbstractBlock.prototype.hash = function hash(enc) { let h = this._hash; if (!h) { - h = digest.hash256(this.toHead()); + h = hash256.digest(this.toHead()); if (!this.mutable) this._hash = h; } diff --git a/lib/primitives/address.js b/lib/primitives/address.js index 33a33fc7..c5e754aa 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -10,7 +10,9 @@ const assert = require('assert'); const Network = require('../protocol/network'); const encoding = require('bbuf/lib/encoding'); -const digest = require('bcrypto/lib/digest'); +const sha256 = require('bcrypto/lib/sha256'); +const hash160 = require('bcrypto/lib/hash160'); +const hash256 = require('bcrypto/lib/hash256'); const BufferReader = require('bbuf/lib/reader'); const StaticWriter = require('bbuf/lib/staticwriter'); const base58 = require('bstr/lib/base58'); @@ -205,7 +207,7 @@ Address.prototype.toRaw = function toRaw(network) { } bw.writeBytes(this.hash); - bw.writeChecksum(digest.hash256); + bw.writeChecksum(hash256.digest); return bw.render(); }; @@ -335,7 +337,7 @@ Address.prototype.fromRaw = function fromRaw(data, network) { const hash = br.readBytes(br.left() - 4); - br.verifyChecksum(digest.hash256); + br.verifyChecksum(hash256.digest); return this.fromHash(hash, type, version); }; @@ -422,7 +424,7 @@ Address.prototype.fromScript = function fromScript(script) { const pk = script.getPubkey(); if (pk) { - this.hash = digest.hash160(pk); + this.hash = hash160.digest(pk); this.type = Address.types.PUBKEYHASH; this.version = -1; return this; @@ -478,7 +480,7 @@ Address.prototype.fromWitness = function fromWitness(witness) { // We're pretty much screwed here // since we can't get the version. if (pk) { - this.hash = digest.hash160(pk); + this.hash = hash160.digest(pk); this.type = Address.types.WITNESS; this.version = 0; return this; @@ -487,7 +489,7 @@ Address.prototype.fromWitness = function fromWitness(witness) { const redeem = witness.getScripthashInput(); if (redeem) { - this.hash = digest.sha256(redeem); + this.hash = sha256.digest(redeem); this.type = Address.types.WITNESS; this.version = 0; return this; @@ -506,7 +508,7 @@ Address.prototype.fromInputScript = function fromInputScript(script) { const [, pk] = script.getPubkeyhashInput(); if (pk) { - this.hash = digest.hash160(pk); + this.hash = hash160.digest(pk); this.type = Address.types.PUBKEYHASH; this.version = -1; return this; @@ -515,7 +517,7 @@ Address.prototype.fromInputScript = function fromInputScript(script) { const redeem = script.getScripthashInput(); if (redeem) { - this.hash = digest.hash160(redeem); + this.hash = hash160.digest(redeem); this.type = Address.types.SCRIPTHASH; this.version = -1; return this; diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index c070f75f..83ca091f 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -9,7 +9,8 @@ const assert = require('assert'); const encoding = require('bbuf/lib/encoding'); -const digest = require('bcrypto/lib/digest'); +const hash160 = require('bcrypto/lib/hash160'); +const hash256 = require('bcrypto/lib/hash256'); const Network = require('../protocol/network'); const BufferReader = require('bbuf/lib/reader'); const StaticWriter = require('bbuf/lib/staticwriter'); @@ -281,7 +282,7 @@ KeyRing.prototype.toSecret = function toSecret(network) { if (this.publicKey.length === 33) bw.writeU8(1); - bw.writeChecksum(digest.hash256); + bw.writeChecksum(hash256.digest); return base58.encode(bw.render()); }; @@ -309,7 +310,7 @@ KeyRing.prototype.fromSecret = function fromSecret(data, network) { compress = true; } - br.verifyChecksum(digest.hash256); + br.verifyChecksum(hash256.digest); return this.fromPrivate(key, compress); }; @@ -381,7 +382,7 @@ KeyRing.prototype.getProgram = function getProgram() { if (!this._program) { let program; if (!this.script) { - const hash = digest.hash160(this.publicKey); + const hash = hash160.digest(this.publicKey); program = Script.fromProgram(0, hash); } else { const hash = this.script.sha256(); @@ -524,7 +525,7 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress(enc, network) { KeyRing.prototype.getKeyHash = function getKeyHash(enc) { if (!this._keyHash) - this._keyHash = digest.hash160(this.publicKey); + this._keyHash = hash160.digest(this.publicKey); return enc === 'hex' ? this._keyHash.toString('hex') diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index e7c909ae..0681301f 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -8,7 +8,7 @@ 'use strict'; const assert = require('assert'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const secp256k1 = require('bcrypto/lib/secp256k1'); const util = require('../utils/util'); const encoding = require('bbuf/lib/encoding'); @@ -176,7 +176,7 @@ TX.prototype.hash = function hash(enc) { let h = this._hash; if (!h) { - h = digest.hash256(this.toNormal()); + h = hash256.digest(this.toNormal()); if (!this.mutable) this._hash = h; } @@ -210,7 +210,7 @@ TX.prototype.witnessHash = function witnessHash(enc) { let hash = this._whash; if (!hash) { - hash = digest.hash256(this.toRaw()); + hash = hash256.digest(this.toRaw()); if (!this.mutable) this._whash = hash; } @@ -546,7 +546,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) { // Append the hash type. bw.writeU32(type); - return digest.hash256(bw.render()); + return hash256.digest(bw.render()); }; /** @@ -622,7 +622,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type for (const input of this.inputs) input.prevout.toWriter(bw); - prevouts = digest.hash256(bw.render()); + prevouts = hash256.digest(bw.render()); if (!this.mutable) this._hashPrevouts = prevouts; @@ -640,7 +640,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type for (const input of this.inputs) bw.writeU32(input.sequence); - sequences = digest.hash256(bw.render()); + sequences = hash256.digest(bw.render()); if (!this.mutable) this._hashSequence = sequences; @@ -662,7 +662,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type for (const output of this.outputs) output.toWriter(bw); - outputs = digest.hash256(bw.render()); + outputs = hash256.digest(bw.render()); if (!this.mutable) this._hashOutputs = outputs; @@ -670,7 +670,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type } else if ((type & 0x1f) === hashType.SINGLE) { if (index < this.outputs.length) { const output = this.outputs[index]; - outputs = digest.hash256(output.toRaw()); + outputs = hash256.digest(output.toRaw()); } } @@ -689,7 +689,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type bw.writeU32(this.locktime); bw.writeU32(type); - return digest.hash256(bw.render()); + return hash256.digest(bw.render()); }; /** diff --git a/lib/script/script.js b/lib/script/script.js index db141ada..d5fe2c49 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -8,7 +8,11 @@ 'use strict'; const assert = require('assert'); -const digest = require('bcrypto/lib/digest'); +const ripemd160 = require('bcrypto/lib/ripemd160'); +const sha1 = require('bcrypto/lib/sha1'); +const sha256 = require('bcrypto/lib/sha256'); +const hash160 = require('bcrypto/lib/hash160'); +const hash256 = require('bcrypto/lib/hash256'); const merkle = require('bcrypto/lib/merkle'); const secp256k1 = require('bcrypto/lib/secp256k1'); const consensus = require('../protocol/consensus'); @@ -26,6 +30,8 @@ const encoding = require('bbuf/lib/encoding'); const Address = require('../primitives/address'); const opcodes = common.opcodes; const scriptTypes = common.types; +const Hash160 = hash160; +const Sha256 = sha256; const EMPTY_BUFFER = Buffer.alloc(0); /** @@ -1111,35 +1117,35 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers if (stack.length === 0) throw new ScriptError('INVALID_STACK_OPERATION', op, ip); - stack.push(digest.ripemd160(stack.pop())); + stack.push(ripemd160.digest(stack.pop())); break; } case opcodes.OP_SHA1: { if (stack.length === 0) throw new ScriptError('INVALID_STACK_OPERATION', op, ip); - stack.push(digest.sha1(stack.pop())); + stack.push(sha1.digest(stack.pop())); break; } case opcodes.OP_SHA256: { if (stack.length === 0) throw new ScriptError('INVALID_STACK_OPERATION', op, ip); - stack.push(digest.sha256(stack.pop())); + stack.push(sha256.digest(stack.pop())); break; } case opcodes.OP_HASH160: { if (stack.length === 0) throw new ScriptError('INVALID_STACK_OPERATION', op, ip); - stack.push(digest.hash160(stack.pop())); + stack.push(hash160.digest(stack.pop())); break; } case opcodes.OP_HASH256: { if (stack.length === 0) throw new ScriptError('INVALID_STACK_OPERATION', op, ip); - stack.push(digest.hash256(stack.pop())); + stack.push(hash256.digest(stack.pop())); break; } case opcodes.OP_CODESEPARATOR: { @@ -1847,7 +1853,7 @@ Script.prototype.getAddress = function getAddress() { */ Script.prototype.hash160 = function hash160(enc) { - let hash = digest.hash160(this.toRaw()); + let hash = Hash160.digest(this.toRaw()); if (enc === 'hex') hash = hash.toString('hex'); return hash; @@ -1860,7 +1866,7 @@ Script.prototype.hash160 = function hash160(enc) { */ Script.prototype.sha256 = function sha256(enc) { - let hash = digest.sha256(this.toRaw()); + let hash = Sha256.digest(this.toRaw()); if (enc === 'hex') hash = hash.toString('hex'); return hash; @@ -2158,7 +2164,7 @@ Script.prototype.forWitness = function forWitness() { const pk = this.getPubkey(); if (pk) { - const hash = digest.hash160(pk); + const hash = hash160.digest(pk); return Script.fromProgram(0, hash); } @@ -3273,7 +3279,7 @@ Script.verifyProgram = function verifyProgram(witness, output, flags, tx, index, const witnessScript = stack.pop(); - if (!digest.sha256(witnessScript).equals(program.data)) + if (!sha256.digest(witnessScript).equals(program.data)) throw new ScriptError('WITNESS_PROGRAM_MISMATCH'); redeem = Script.fromRaw(witnessScript); @@ -3418,15 +3424,15 @@ Script.verifyMast = function verifyMast(program, stack, output, flags, tx, index if ((scripts.offset + script.length) > consensus.MAX_SCRIPT_SIZE) throw new ScriptError('SCRIPT_SIZE'); } - scriptRoot.writeBytes(digest.hash256(script)); + scriptRoot.writeBytes(hash256.digest(script)); scripts.writeBytes(script); } - scriptRoot = digest.hash256(scriptRoot.render()); + scriptRoot = hash256.digest(scriptRoot.render()); scriptRoot = merkle.verifyBranch(scriptRoot, path, pos); mastRoot.writeBytes(scriptRoot); - mastRoot = digest.hash256(mastRoot.render()); + mastRoot = hash256.digest(mastRoot.render()); if (!mastRoot.equals(program.data)) throw new ScriptError('WITNESS_PROGRAM_MISMATCH'); diff --git a/lib/wallet/http.js b/lib/wallet/http.js index b2c28028..984d7f3d 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -14,7 +14,7 @@ const base58 = require('bstr/lib/base58'); const MTX = require('../primitives/mtx'); const Outpoint = require('../primitives/outpoint'); const Script = require('../script/script'); -const digest = require('bcrypto/lib/digest'); +const sha256 = require('bcrypto/lib/sha256'); const random = require('bcrypto/lib/random'); const ccmp = require('bcrypto/lib/ccmp'); const Network = require('../protocol/network'); @@ -81,6 +81,7 @@ class HTTP extends Server { if (!this.options.noAuth) { this.use(this.basicAuth({ + hash: sha256.digest, password: this.options.apiKey, realm: 'wallet' })); @@ -895,7 +896,7 @@ class HTTP extends Server { throw new Error('Invalid API key.'); const data = Buffer.from(key, 'utf8'); - const hash = digest.hash256(data); + const hash = sha256.digest(data); if (!ccmp(hash, this.options.apiHash)) throw new Error('Invalid API key.'); @@ -979,7 +980,7 @@ class HTTPOptions { this.logger = null; this.node = null; this.apiKey = base58.encode(random.randomBytes(20)); - this.apiHash = digest.hash256(Buffer.from(this.apiKey, 'ascii')); + this.apiHash = sha256.digest(Buffer.from(this.apiKey, 'ascii')); this.serviceHash = this.apiHash; this.noAuth = false; this.walletAuth = false; @@ -1022,7 +1023,7 @@ class HTTPOptions { assert(options.apiKey.length <= 255, 'API key must be under 255 bytes.'); this.apiKey = options.apiKey; - this.apiHash = digest.hash256(Buffer.from(this.apiKey, 'ascii')); + this.apiHash = sha256.digest(Buffer.from(this.apiKey, 'ascii')); } if (options.noAuth != null) { diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index a4cbe281..3bbba6fd 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -11,7 +11,7 @@ const bweb = require('bweb'); const fs = require('bfile'); const {format} = require('util'); const util = require('../utils/util'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const Amount = require('../btc/amount'); const Script = require('../script/script'); const Address = require('../primitives/address'); @@ -1464,7 +1464,7 @@ class RPC extends RPCBase { throw new RPCError(errs.WALLET_UNLOCK_NEEDED, 'Wallet is locked.'); const msg = Buffer.from(MAGIC_STRING + str, 'utf8'); - const hash = digest.hash256(msg); + const hash = hash256.digest(msg); const sig = ring.sign(hash); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index be808007..6606935e 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -12,7 +12,8 @@ const EventEmitter = require('events'); const Network = require('../protocol/network'); const encoding = require('bbuf/lib/encoding'); const Lock = require('../utils/lock'); -const digest = require('bcrypto/lib/digest'); +const hash160 = require('bcrypto/lib/hash160'); +const hash256 = require('bcrypto/lib/hash256'); const cleanse = require('bcrypto/lib/cleanse'); const BufferReader = require('bbuf/lib/reader'); const StaticWriter = require('bbuf/lib/staticwriter'); @@ -537,14 +538,14 @@ Wallet.prototype.getID = function getID() { bw.writeBytes(key.publicKey); bw.writeU32(this.network.magic); - const hash = digest.hash160(bw.render()); + const hash = hash160.digest(bw.render()); const b58 = new StaticWriter(27); b58.writeU8(0x03); b58.writeU8(0xbe); b58.writeU8(0x04); b58.writeBytes(hash); - b58.writeChecksum(digest.hash256); + b58.writeChecksum(hash256.digest); return base58.encode(b58.render()); }; @@ -568,7 +569,7 @@ Wallet.prototype.getToken = function getToken(nonce) { bw.writeBytes(key.privateKey); bw.writeU32(nonce); - return digest.hash256(bw.render()); + return hash256.digest(bw.render()); }; /** diff --git a/migrate/chaindb2to3.js b/migrate/chaindb2to3.js index 31a4d423..41516358 100644 --- a/migrate/chaindb2to3.js +++ b/migrate/chaindb2to3.js @@ -18,7 +18,7 @@ if (process.argv.indexOf('-h') !== -1 const assert = require('assert'); const BDB = require('bdb'); const encoding = require('bbuf/lib/encoding'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const BN = require('bcrypto/lib/bn'); const StaticWriter = require('bbuf/lib/staticwriter'); const BufferReader = require('bbuf/lib/reader'); @@ -578,7 +578,7 @@ async function isMainChain(entry, tip) { function entryFromRaw(data) { const br = new BufferReader(data, true); - const hash = digest.hash256(br.readBytes(80)); + const hash = hash256.digest(br.readBytes(80)); br.seek(-80); diff --git a/migrate/ensure-tip-index.js b/migrate/ensure-tip-index.js index 7537a338..a22386da 100644 --- a/migrate/ensure-tip-index.js +++ b/migrate/ensure-tip-index.js @@ -4,7 +4,7 @@ const assert = require('assert'); const BDB = require('bdb'); const encoding = require('bbuf/lib/encoding'); const BufferReader = require('bbuf/lib/reader'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const BN = require('bcrypto/lib/bn'); const DUMMY = Buffer.from([0]); @@ -40,7 +40,7 @@ async function checkVersion() { function entryFromRaw(data) { const p = new BufferReader(data, true); - const hash = digest.hash256(p.readBytes(80)); + const hash = hash256.digest(p.readBytes(80)); const entry = {}; p.seek(-80); diff --git a/test/wallet-test.js b/test/wallet-test.js index 2294785c..d5e96dee 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -7,7 +7,7 @@ const assert = require('./util/assert'); const consensus = require('../lib/protocol/consensus'); const util = require('../lib/utils/util'); const encoding = require('bbuf/lib/encoding'); -const digest = require('bcrypto/lib/digest'); +const hash256 = require('bcrypto/lib/hash256'); const random = require('bcrypto/lib/random'); const WalletDB = require('../lib/wallet/walletdb'); const WorkerPool = require('../lib/workers/workerpool'); @@ -46,9 +46,9 @@ function nextBlock(wdb) { } function fakeBlock(height) { - const prev = digest.hash256(u32((height - 1) >>> 0)); - const hash = digest.hash256(u32(height >>> 0)); - const root = digest.hash256(u32((height | 0x80000000) >>> 0)); + const prev = hash256.digest(u32((height - 1) >>> 0)); + const hash = hash256.digest(u32(height >>> 0)); + const root = hash256.digest(u32((height | 0x80000000) >>> 0)); return { hash: hash.toString('hex'),