Merge pull request #384 from bitcoinjs/buildreduce
crypto: use packages directly
This commit is contained in:
commit
c66b8883f7
7 changed files with 43 additions and 49 deletions
|
@ -46,8 +46,11 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bigi": "^1.4.0",
|
"bigi": "^1.4.0",
|
||||||
"bs58check": "^1.0.4",
|
"bs58check": "^1.0.5",
|
||||||
|
"create-hash": "^1.1.0",
|
||||||
|
"create-hmac": "^1.1.3",
|
||||||
"ecurve": "^1.0.0",
|
"ecurve": "^1.0.0",
|
||||||
|
"randombytes": "^2.0.1",
|
||||||
"typeforce": "^1.0.0"
|
"typeforce": "^1.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -58,6 +61,7 @@
|
||||||
"coveralls": "^2.11.2",
|
"coveralls": "^2.11.2",
|
||||||
"istanbul": "^0.3.5",
|
"istanbul": "^0.3.5",
|
||||||
"mocha": "^2.2.0",
|
"mocha": "^2.2.0",
|
||||||
|
"proxyquire": "^1.4.0",
|
||||||
"sinon": "^1.12.2",
|
"sinon": "^1.12.2",
|
||||||
"standard": "^2.11.0"
|
"standard": "^2.11.0"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var crypto = require('crypto')
|
var createHash = require('create-hash')
|
||||||
|
|
||||||
function hash160 (buffer) {
|
function hash160 (buffer) {
|
||||||
return ripemd160(sha256(buffer))
|
return ripemd160(sha256(buffer))
|
||||||
|
@ -9,15 +9,15 @@ function hash256 (buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function ripemd160 (buffer) {
|
function ripemd160 (buffer) {
|
||||||
return crypto.createHash('rmd160').update(buffer).digest()
|
return createHash('rmd160').update(buffer).digest()
|
||||||
}
|
}
|
||||||
|
|
||||||
function sha1 (buffer) {
|
function sha1 (buffer) {
|
||||||
return crypto.createHash('sha1').update(buffer).digest()
|
return createHash('sha1').update(buffer).digest()
|
||||||
}
|
}
|
||||||
|
|
||||||
function sha256 (buffer) {
|
function sha256 (buffer) {
|
||||||
return crypto.createHash('sha256').update(buffer).digest()
|
return createHash('sha256').update(buffer).digest()
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
18
src/ecdsa.js
18
src/ecdsa.js
|
@ -1,5 +1,5 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var crypto = require('crypto')
|
var createHmac = require('create-hmac')
|
||||||
var typeForce = require('typeforce')
|
var typeForce = require('typeforce')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
|
@ -29,7 +29,7 @@ function deterministicGenerateK (curve, hash, d, checkSig) {
|
||||||
k.fill(0)
|
k.fill(0)
|
||||||
|
|
||||||
// Step D
|
// Step D
|
||||||
k = crypto.createHmac('sha256', k)
|
k = createHmac('sha256', k)
|
||||||
.update(v)
|
.update(v)
|
||||||
.update(ZERO)
|
.update(ZERO)
|
||||||
.update(x)
|
.update(x)
|
||||||
|
@ -37,10 +37,10 @@ function deterministicGenerateK (curve, hash, d, checkSig) {
|
||||||
.digest()
|
.digest()
|
||||||
|
|
||||||
// Step E
|
// Step E
|
||||||
v = crypto.createHmac('sha256', k).update(v).digest()
|
v = createHmac('sha256', k).update(v).digest()
|
||||||
|
|
||||||
// Step F
|
// Step F
|
||||||
k = crypto.createHmac('sha256', k)
|
k = createHmac('sha256', k)
|
||||||
.update(v)
|
.update(v)
|
||||||
.update(ONE)
|
.update(ONE)
|
||||||
.update(x)
|
.update(x)
|
||||||
|
@ -48,26 +48,26 @@ function deterministicGenerateK (curve, hash, d, checkSig) {
|
||||||
.digest()
|
.digest()
|
||||||
|
|
||||||
// Step G
|
// Step G
|
||||||
v = crypto.createHmac('sha256', k).update(v).digest()
|
v = createHmac('sha256', k).update(v).digest()
|
||||||
|
|
||||||
// Step H1/H2a, ignored as tlen === qlen (256 bit)
|
// Step H1/H2a, ignored as tlen === qlen (256 bit)
|
||||||
// Step H2b
|
// Step H2b
|
||||||
v = crypto.createHmac('sha256', k).update(v).digest()
|
v = createHmac('sha256', k).update(v).digest()
|
||||||
|
|
||||||
var T = BigInteger.fromBuffer(v)
|
var T = BigInteger.fromBuffer(v)
|
||||||
|
|
||||||
// Step H3, repeat until T is within the interval [1, n - 1] and is suitable for ECDSA
|
// Step H3, repeat until T is within the interval [1, n - 1] and is suitable for ECDSA
|
||||||
while ((T.signum() <= 0) || (T.compareTo(curve.n) >= 0) || !checkSig(T)) {
|
while ((T.signum() <= 0) || (T.compareTo(curve.n) >= 0) || !checkSig(T)) {
|
||||||
k = crypto.createHmac('sha256', k)
|
k = createHmac('sha256', k)
|
||||||
.update(v)
|
.update(v)
|
||||||
.update(ZERO)
|
.update(ZERO)
|
||||||
.digest()
|
.digest()
|
||||||
|
|
||||||
v = crypto.createHmac('sha256', k).update(v).digest()
|
v = createHmac('sha256', k).update(v).digest()
|
||||||
|
|
||||||
// Step H1/H2a, again, ignored as tlen === qlen (256 bit)
|
// Step H1/H2a, again, ignored as tlen === qlen (256 bit)
|
||||||
// Step H2b again
|
// Step H2b again
|
||||||
v = crypto.createHmac('sha256', k).update(v).digest()
|
v = createHmac('sha256', k).update(v).digest()
|
||||||
T = BigInteger.fromBuffer(v)
|
T = BigInteger.fromBuffer(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var base58check = require('bs58check')
|
var base58check = require('bs58check')
|
||||||
var crypto = require('crypto')
|
|
||||||
var ecdsa = require('./ecdsa')
|
var ecdsa = require('./ecdsa')
|
||||||
var typeForce = require('typeforce')
|
|
||||||
var networks = require('./networks')
|
var networks = require('./networks')
|
||||||
|
var randomBytes = require('randombytes')
|
||||||
|
var typeForce = require('typeforce')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECPubKey = require('./ecpubkey')
|
var ECPubKey = require('./ecpubkey')
|
||||||
|
@ -47,7 +47,7 @@ ECKey.fromWIF = function (string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ECKey.makeRandom = function (compressed, rng) {
|
ECKey.makeRandom = function (compressed, rng) {
|
||||||
rng = rng || crypto.randomBytes
|
rng = rng || randomBytes
|
||||||
|
|
||||||
var buffer = rng(32)
|
var buffer = rng(32)
|
||||||
typeForce('Buffer', buffer)
|
typeForce('Buffer', buffer)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var base58check = require('bs58check')
|
var base58check = require('bs58check')
|
||||||
var bcrypto = require('./crypto')
|
var bcrypto = require('./crypto')
|
||||||
var crypto = require('crypto')
|
var createHmac = require('create-hmac')
|
||||||
var typeForce = require('typeforce')
|
var typeForce = require('typeforce')
|
||||||
var networks = require('./networks')
|
var networks = require('./networks')
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ HDNode.fromSeedBuffer = function (seed, network) {
|
||||||
assert(seed.length >= 16, 'Seed should be at least 128 bits')
|
assert(seed.length >= 16, 'Seed should be at least 128 bits')
|
||||||
assert(seed.length <= 64, 'Seed should be at most 512 bits')
|
assert(seed.length <= 64, 'Seed should be at most 512 bits')
|
||||||
|
|
||||||
var I = crypto.createHmac('sha512', HDNode.MASTER_SECRET).update(seed).digest()
|
var I = createHmac('sha512', HDNode.MASTER_SECRET).update(seed).digest()
|
||||||
var IL = I.slice(0, 32)
|
var IL = I.slice(0, 32)
|
||||||
var IR = I.slice(32)
|
var IR = I.slice(32)
|
||||||
|
|
||||||
|
@ -225,7 +225,7 @@ HDNode.prototype.derive = function (index) {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
var I = crypto.createHmac('sha512', this.chainCode).update(data).digest()
|
var I = createHmac('sha512', this.chainCode).update(data).digest()
|
||||||
var IL = I.slice(0, 32)
|
var IL = I.slice(0, 32)
|
||||||
var IR = I.slice(32)
|
var IR = I.slice(32)
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
/* global describe, it, beforeEach, afterEach */
|
/* global describe, it */
|
||||||
/* eslint-disable no-new */
|
/* eslint-disable no-new */
|
||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var crypto = require('crypto')
|
|
||||||
var ecurve = require('ecurve')
|
var ecurve = require('ecurve')
|
||||||
var networks = require('../src/networks')
|
var networks = require('../src/networks')
|
||||||
var sinon = require('sinon')
|
var proxyquire = require('proxyquire')
|
||||||
|
var randomBytes = require('randombytes')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECKey = require('../src/eckey')
|
var ECKey = require('../src/eckey')
|
||||||
|
@ -101,25 +101,13 @@ describe('ECKey', function () {
|
||||||
var exPrivKey = ECKey.fromWIF(exWIF)
|
var exPrivKey = ECKey.fromWIF(exWIF)
|
||||||
var exBuffer = exPrivKey.d.toBuffer(32)
|
var exBuffer = exPrivKey.d.toBuffer(32)
|
||||||
|
|
||||||
describe('uses default crypto RNG', function () {
|
it("uses the RNG provided by the 'randombytes' module by default", function () {
|
||||||
beforeEach(function () {
|
var stub = { randombytes: function () { return exBuffer } }
|
||||||
sinon.stub(crypto, 'randomBytes').returns(exBuffer)
|
var ProxiedECKey = proxyquire('../src/eckey', stub)
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(function () {
|
var privKey = ProxiedECKey.makeRandom()
|
||||||
crypto.randomBytes.restore()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('generates a ECKey', function () {
|
assert.equal(privKey.toWIF(), exWIF)
|
||||||
var privKey = ECKey.makeRandom()
|
|
||||||
|
|
||||||
assert.equal(privKey.toWIF(), exWIF)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('supports compression', function () {
|
|
||||||
assert.equal(ECKey.makeRandom(true).pub.compressed, true)
|
|
||||||
assert.equal(ECKey.makeRandom(false).pub.compressed, false)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allows a custom RNG to be used', function () {
|
it('allows a custom RNG to be used', function () {
|
||||||
|
@ -130,10 +118,15 @@ describe('ECKey', function () {
|
||||||
var privKey = ECKey.makeRandom(undefined, rng)
|
var privKey = ECKey.makeRandom(undefined, rng)
|
||||||
assert.equal(privKey.toWIF(), exWIF)
|
assert.equal(privKey.toWIF(), exWIF)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('supports compression', function () {
|
||||||
|
assert.equal(ECKey.makeRandom(true).pub.compressed, true)
|
||||||
|
assert.equal(ECKey.makeRandom(false).pub.compressed, false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('signing', function () {
|
describe('signing', function () {
|
||||||
var hash = crypto.randomBytes(32)
|
var hash = randomBytes(32)
|
||||||
var priv = ECKey.makeRandom()
|
var priv = ECKey.makeRandom()
|
||||||
var signature = priv.sign(hash)
|
var signature = priv.sign(hash)
|
||||||
|
|
||||||
|
|
|
@ -3,21 +3,18 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var bigi = require('bigi')
|
var bigi = require('bigi')
|
||||||
var bitcoin = require('../../')
|
var bitcoin = require('../../')
|
||||||
var crypto = require('crypto')
|
|
||||||
var sinon = require('sinon')
|
|
||||||
|
|
||||||
describe('bitcoinjs-lib (basic)', function () {
|
describe('bitcoinjs-lib (basic)', function () {
|
||||||
it('can generate a random bitcoin address', sinon.test(function () {
|
it('can generate a random bitcoin address', function () {
|
||||||
// for testing only
|
// for testing only
|
||||||
this.mock(crypto).expects('randomBytes')
|
function rng () { return new Buffer('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') }
|
||||||
.onCall(0).returns(new Buffer('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'))
|
|
||||||
|
|
||||||
// generate random key
|
// generate random key (custom rng for testing only)
|
||||||
var key = bitcoin.ECKey.makeRandom()
|
var key = bitcoin.ECKey.makeRandom(undefined, rng)
|
||||||
var address = key.pub.getAddress().toString()
|
var address = key.pub.getAddress().toString()
|
||||||
|
|
||||||
assert.equal(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64')
|
assert.equal(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64')
|
||||||
}))
|
})
|
||||||
|
|
||||||
it('can generate an address from a SHA256 hash', function () {
|
it('can generate an address from a SHA256 hash', function () {
|
||||||
var hash = bitcoin.crypto.sha256('correct horse battery staple')
|
var hash = bitcoin.crypto.sha256('correct horse battery staple')
|
||||||
|
|
Loading…
Reference in a new issue