refactor: avoid using utils in crypto and vice versa.

This commit is contained in:
Christopher Jeffrey 2017-10-20 06:21:12 -07:00
parent f3b94ded65
commit 9a8106f4f7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
11 changed files with 36 additions and 28 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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