diff --git a/lib/crypto/pbkdf2.js b/lib/crypto/pbkdf2.js index 3f387907..5e25a11f 100644 --- a/lib/crypto/pbkdf2.js +++ b/lib/crypto/pbkdf2.js @@ -11,7 +11,6 @@ */ const crypto = require('crypto'); -const co = require('../utils/co'); /** * Perform key derivation using PBKDF2. @@ -39,6 +38,12 @@ exports.derive = function derive(key, salt, iter, len, alg) { exports.deriveAsync = function deriveAsync(key, salt, iter, len, alg) { return new Promise((resolve, reject) => { - crypto.pbkdf2(key, salt, iter, len, alg, co.wrap(resolve, reject)); + crypto.pbkdf2(key, salt, iter, len, alg, (err, result) => { + if (err) { + reject(err); + return; + } + resolve(result); + }); }); }; diff --git a/lib/crypto/scrypt.js b/lib/crypto/scrypt.js index b07dedc6..b9fbe89e 100644 --- a/lib/crypto/scrypt.js +++ b/lib/crypto/scrypt.js @@ -39,7 +39,6 @@ * @module crypto/scrypt */ -const co = require('../utils/co'); const pbkdf2 = require('./pbkdf2'); const native = require('../native').binding; @@ -236,14 +235,14 @@ async function smixAsync(B, Bo, r, N, V, XY) { for (let i = 0; i < N; i++) { blkcpy(V, X, i * (128 * r), 0, 128 * r); blockmix_salsa8(X, Y, 128 * r, r); - await co.wait(); + await wait(); } for (let i = 0; i < N; i++) { const j = integerify(X, r) & (N - 1); blkxor(X, V, 0, j * (128 * r), 128 * r); blockmix_salsa8(X, Y, 128 * r, r); - await co.wait(); + await wait(); } blkcpy(B, X, Bo, 0, 128 * r); @@ -258,6 +257,10 @@ function blkxor(dest, src, s1, s2, len) { dest[s1 + i] ^= src[s2 + i]; } +function wait() { + return new Promise(r => setImmediate(r)); +} + /* * Expose */ diff --git a/lib/hd/private.js b/lib/hd/private.js index a0e02df0..662bd01d 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -571,7 +571,7 @@ HDPrivateKey.prototype.fromReader = function fromReader(br, network) { this.privateKey = br.readBytes(32); this.publicKey = secp256k1.publicKeyCreate(this.privateKey, true); - br.verifyChecksum(); + br.verifyChecksum(digest.hash256); return this; }; @@ -622,7 +622,7 @@ HDPrivateKey.prototype.toWriter = function toWriter(bw, network) { bw.writeBytes(this.chainCode); bw.writeU8(0); bw.writeBytes(this.privateKey); - bw.writeChecksum(); + bw.writeChecksum(digest.hash256); return bw; }; diff --git a/lib/hd/public.js b/lib/hd/public.js index a1ecc859..e2104294 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -440,7 +440,7 @@ HDPublicKey.prototype.fromReader = function fromReader(br, network) { this.chainCode = br.readBytes(32); this.publicKey = br.readBytes(33); - br.verifyChecksum(); + br.verifyChecksum(digest.hash256); return this; }; @@ -481,7 +481,7 @@ HDPublicKey.prototype.toWriter = function toWriter(bw, network) { bw.writeU32BE(this.childIndex); bw.writeBytes(this.chainCode); bw.writeBytes(this.publicKey); - bw.writeChecksum(); + bw.writeChecksum(digest.hash256); return bw; }; diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 810857bc..62d807ef 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -446,7 +446,7 @@ BIP150.address = function address(key) { bw.writeU8(0x0f); bw.writeU16BE(0xff01); bw.writeBytes(digest.hash160(key)); - bw.writeChecksum(); + bw.writeChecksum(digest.hash256); return base58.encode(bw.render()); }; diff --git a/lib/primitives/address.js b/lib/primitives/address.js index 2f105823..16068a66 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -200,7 +200,7 @@ Address.prototype.toRaw = function toRaw(network) { } bw.writeBytes(this.hash); - bw.writeChecksum(); + bw.writeChecksum(digest.hash256); return bw.render(); }; @@ -330,7 +330,7 @@ Address.prototype.fromRaw = function fromRaw(data, network) { const hash = br.readBytes(br.left() - 4); - br.verifyChecksum(); + br.verifyChecksum(digest.hash256); return this.fromHash(hash, type, version); }; diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index 529ce8ca..e74897ca 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -281,7 +281,7 @@ KeyRing.prototype.toSecret = function toSecret(network) { if (this.publicKey.length === 33) bw.writeU8(1); - bw.writeChecksum(); + bw.writeChecksum(digest.hash256); return base58.encode(bw.render()); }; @@ -309,7 +309,7 @@ KeyRing.prototype.fromSecret = function fromSecret(data, network) { compress = true; } - br.verifyChecksum(); + br.verifyChecksum(digest.hash256); return this.fromPrivate(key, compress); }; diff --git a/lib/utils/reader.js b/lib/utils/reader.js index c2e75c7a..f8d13b3c 100644 --- a/lib/utils/reader.js +++ b/lib/utils/reader.js @@ -9,7 +9,6 @@ const assert = require('assert'); const encoding = require('./encoding'); -const digest = require('../crypto/digest'); const EMPTY = Buffer.alloc(0); @@ -567,10 +566,11 @@ BufferReader.prototype.readNullString = function readNullString(enc) { /** * Create a checksum from the last start position. + * @param {Function} hash * @returns {Number} Checksum. */ -BufferReader.prototype.createChecksum = function createChecksum() { +BufferReader.prototype.createChecksum = function createChecksum(hash) { let start = 0; if (this.stack.length > 0) @@ -578,17 +578,18 @@ BufferReader.prototype.createChecksum = function createChecksum() { const data = this.data.slice(start, this.offset); - return digest.hash256(data).readUInt32LE(0, true); + return hash(data).readUInt32LE(0, true); }; /** * Verify a 4-byte checksum against a calculated checksum. + * @param {Function} hash * @returns {Number} checksum * @throws on bad checksum */ -BufferReader.prototype.verifyChecksum = function verifyChecksum() { - const chk = this.createChecksum(); +BufferReader.prototype.verifyChecksum = function verifyChecksum(hash) { + const chk = this.createChecksum(hash); const checksum = this.readU32(); this.enforce(chk === checksum, 'Checksum mismatch.'); return checksum; diff --git a/lib/utils/staticwriter.js b/lib/utils/staticwriter.js index 0530c096..7e54ab4e 100644 --- a/lib/utils/staticwriter.js +++ b/lib/utils/staticwriter.js @@ -8,7 +8,6 @@ const assert = require('assert'); const encoding = require('./encoding'); -const digest = require('../crypto/digest'); const EMPTY = Buffer.alloc(0); const POOLSIZE = 100 << 10; @@ -429,12 +428,12 @@ StaticWriter.prototype.writeNullString = function writeNullString(value, enc) { /** * Calculate and write a checksum for the data written so far. + * @param {Function} hash */ -StaticWriter.prototype.writeChecksum = function writeChecksum() { +StaticWriter.prototype.writeChecksum = function writeChecksum(hash) { const data = this.data.slice(0, this.offset); - const hash = digest.hash256(data); - hash.copy(this.data, this.offset, 0, 4); + hash(data).copy(this.data, this.offset, 0, 4); this.offset += 4; }; diff --git a/lib/utils/writer.js b/lib/utils/writer.js index 700cfdbd..e641c31a 100644 --- a/lib/utils/writer.js +++ b/lib/utils/writer.js @@ -9,7 +9,6 @@ const assert = require('assert'); const encoding = require('./encoding'); -const digest = require('../crypto/digest'); /* * Constants @@ -168,7 +167,7 @@ BufferWriter.prototype.render = function render() { off += data.write(op.value, off, op.enc); break; case CHECKSUM: - off += digest.hash256(data.slice(0, off)).copy(data, off, 0, 4); + off += op.value(data.slice(0, off)).copy(data, off, 0, 4); break; case FILL: data.fill(op.value, off, off + op.size); @@ -581,11 +580,12 @@ BufferWriter.prototype.writeNullString = function writeNullString(value, enc) { /** * Calculate and write a checksum for the data written so far. + * @param {Function} hash */ -BufferWriter.prototype.writeChecksum = function writeChecksum() { +BufferWriter.prototype.writeChecksum = function writeChecksum(hash) { this.offset += 4; - this.ops.push(new WriteOp(CHECKSUM)); + this.ops.push(new WriteOp(CHECKSUM, hash)); }; /** diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 50f28ac6..39e9be80 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -545,7 +545,7 @@ Wallet.prototype.getID = function getID() { b58.writeU8(0xbe); b58.writeU8(0x04); b58.writeBytes(hash); - b58.writeChecksum(); + b58.writeChecksum(digest.hash256); return base58.encode(b58.render()); };