Merge pull request #534 from bitcoinjs/tforce
typeforce for hash160/256 throws hard error
This commit is contained in:
commit
64e45e694f
3 changed files with 29 additions and 2 deletions
|
@ -69,7 +69,7 @@
|
||||||
"create-hmac": "^1.1.3",
|
"create-hmac": "^1.1.3",
|
||||||
"ecurve": "^1.0.0",
|
"ecurve": "^1.0.0",
|
||||||
"randombytes": "^2.0.1",
|
"randombytes": "^2.0.1",
|
||||||
"typeforce": "^1.5.5",
|
"typeforce": "^1.6.2",
|
||||||
"wif": "^1.1.0"
|
"wif": "^1.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -2,7 +2,8 @@ var typeforce = require('typeforce')
|
||||||
|
|
||||||
function nBuffer (value, n) {
|
function nBuffer (value, n) {
|
||||||
typeforce(types.Buffer, value)
|
typeforce(types.Buffer, value)
|
||||||
if (value.length !== n) throw new Error('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit Buffer')
|
if (value.length !== n) throw new typeforce.TfTypeError('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit Buffer')
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var types = require('../src/types')
|
var types = require('../src/types')
|
||||||
|
var typeforce = require('typeforce')
|
||||||
|
|
||||||
describe('types', function () {
|
describe('types', function () {
|
||||||
describe('BigInt/ECPoint', function () {
|
describe('BigInt/ECPoint', function () {
|
||||||
|
@ -15,4 +16,29 @@ describe('types', function () {
|
||||||
assert(!types.ECPoint(new function NotAPoint () {}))
|
assert(!types.ECPoint(new function NotAPoint () {}))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('Buffer Hash160/Hash256', function () {
|
||||||
|
var buffer20byte = new Buffer((new Array(20 + 1)).join('00'), 'hex')
|
||||||
|
var buffer32byte = new Buffer((new Array(32 + 1)).join('00'), 'hex')
|
||||||
|
|
||||||
|
it('return true for valid size', function () {
|
||||||
|
assert(types.Hash160bit(buffer20byte))
|
||||||
|
assert(types.Hash256bit(buffer32byte))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('return true for oneOf', function () {
|
||||||
|
assert(typeforce(types.oneOf(types.Hash160bit, types.Hash256bit), buffer32byte))
|
||||||
|
assert(typeforce(types.oneOf(types.Hash256bit, types.Hash160bit), buffer32byte))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws for invalid size', function () {
|
||||||
|
assert.throws(function () {
|
||||||
|
types.Hash160bit(buffer32byte)
|
||||||
|
}, /Expected 160-bit Buffer, got 256-bit Buffer/)
|
||||||
|
|
||||||
|
assert.throws(function () {
|
||||||
|
types.Hash256bit(buffer20byte)
|
||||||
|
}, /Expected 256-bit Buffer, got 160-bit Buffer/)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue