Merge pull request #434 from bitcoinjs/noassert
Remove assert, use typeforce typing
This commit is contained in:
commit
21980b60b5
16 changed files with 212 additions and 148 deletions
|
@ -51,7 +51,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.0.0"
|
"typeforce": "^1.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"async": "^0.9.0",
|
"async": "^0.9.0",
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
var base58check = require('bs58check')
|
var base58check = require('bs58check')
|
||||||
var typeForce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
var networks = require('./networks')
|
var networks = require('./networks')
|
||||||
var scripts = require('./scripts')
|
var scripts = require('./scripts')
|
||||||
|
var types = require('./types')
|
||||||
|
|
||||||
function fromBase58Check (string) {
|
function fromBase58Check (string) {
|
||||||
var payload = base58check.decode(string)
|
var payload = base58check.decode(string)
|
||||||
|
@ -23,10 +24,7 @@ function fromOutputScript (script, network) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function toBase58Check (hash, version) {
|
function toBase58Check (hash, version) {
|
||||||
typeForce('Buffer', hash)
|
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
||||||
|
|
||||||
if (hash.length !== 20) throw new TypeError('Invalid hash length')
|
|
||||||
if (version & ~0xff) throw new TypeError('Invalid version byte')
|
|
||||||
|
|
||||||
var payload = new Buffer(21)
|
var payload = new Buffer(21)
|
||||||
payload.writeUInt8(version, 0)
|
payload.writeUInt8(version, 0)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
var assert = require('assert')
|
|
||||||
var bufferutils = require('./bufferutils')
|
var bufferutils = require('./bufferutils')
|
||||||
var crypto = require('./crypto')
|
var crypto = require('./crypto')
|
||||||
|
|
||||||
|
@ -14,7 +13,7 @@ function Block () {
|
||||||
}
|
}
|
||||||
|
|
||||||
Block.fromBuffer = function (buffer) {
|
Block.fromBuffer = function (buffer) {
|
||||||
assert(buffer.length >= 80, 'Buffer too small (< 80 bytes)')
|
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)')
|
||||||
|
|
||||||
var offset = 0
|
var offset = 0
|
||||||
function readSlice (n) {
|
function readSlice (n) {
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
var assert = require('assert')
|
|
||||||
var opcodes = require('./opcodes')
|
var opcodes = require('./opcodes')
|
||||||
|
|
||||||
// https://github.com/feross/buffer/blob/master/index.js#L1127
|
// https://github.com/feross/buffer/blob/master/index.js#L1127
|
||||||
function verifuint (value, max) {
|
function verifuint (value, max) {
|
||||||
assert(typeof value === 'number', 'cannot write a non-number as a number')
|
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
|
||||||
assert(value >= 0, 'specified a negative value for writing an unsigned value')
|
if (value < 0) throw new Error('specified a negative value for writing an unsigned value')
|
||||||
assert(value <= max, 'value is larger than maximum value for type')
|
if (value > max) throw new Error('value is larger than maximum value for type')
|
||||||
assert(Math.floor(value) === value, 'value has a fractional component')
|
if (Math.floor(value) !== value) throw new Error('value has a fractional component')
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushDataSize (i) {
|
function pushDataSize (i) {
|
||||||
|
@ -40,7 +39,7 @@ function readPushDataInt (buffer, offset) {
|
||||||
// 32 bit
|
// 32 bit
|
||||||
} else {
|
} else {
|
||||||
if (offset + 5 > buffer.length) return null
|
if (offset + 5 > buffer.length) return null
|
||||||
assert.equal(opcode, opcodes.OP_PUSHDATA4, 'Unexpected opcode')
|
if (opcode !== opcodes.OP_PUSHDATA4) throw new Error('Unexpected opcode')
|
||||||
|
|
||||||
number = buffer.readUInt32LE(offset + 1)
|
number = buffer.readUInt32LE(offset + 1)
|
||||||
size = 5
|
size = 5
|
||||||
|
|
58
src/ecdsa.js
58
src/ecdsa.js
|
@ -1,6 +1,6 @@
|
||||||
var assert = require('assert')
|
|
||||||
var createHmac = require('create-hmac')
|
var createHmac = require('create-hmac')
|
||||||
var typeForce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
|
var types = require('./types')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECSignature = require('./ecsignature')
|
var ECSignature = require('./ecsignature')
|
||||||
|
@ -10,12 +10,12 @@ var ONE = new Buffer([1])
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6979#section-3.2
|
// https://tools.ietf.org/html/rfc6979#section-3.2
|
||||||
function deterministicGenerateK (curve, hash, d, checkSig) {
|
function deterministicGenerateK (curve, hash, d, checkSig) {
|
||||||
typeForce('Buffer', hash)
|
typeforce(types.tuple(
|
||||||
typeForce('BigInteger', d)
|
types.ECCurve,
|
||||||
typeForce('Function', checkSig)
|
types.Hash256bit,
|
||||||
|
types.BigInt,
|
||||||
// sanity check
|
types.Function
|
||||||
assert.equal(hash.length, 32, 'Hash must be 256 bit')
|
), arguments)
|
||||||
|
|
||||||
var x = d.toBuffer(32)
|
var x = d.toBuffer(32)
|
||||||
var k = new Buffer(32)
|
var k = new Buffer(32)
|
||||||
|
@ -75,9 +75,7 @@ function deterministicGenerateK (curve, hash, d, checkSig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function sign (curve, hash, d) {
|
function sign (curve, hash, d) {
|
||||||
typeForce('Curve', curve)
|
typeforce(types.tuple(types.ECCurve, types.Hash256bit, types.BigInt), arguments)
|
||||||
typeForce('Buffer', hash)
|
|
||||||
typeForce('BigInteger', d)
|
|
||||||
|
|
||||||
var e = BigInteger.fromBuffer(hash)
|
var e = BigInteger.fromBuffer(hash)
|
||||||
var n = curve.n
|
var n = curve.n
|
||||||
|
@ -109,10 +107,12 @@ function sign (curve, hash, d) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function verify (curve, hash, signature, Q) {
|
function verify (curve, hash, signature, Q) {
|
||||||
typeForce('Curve', curve)
|
typeforce(types.tuple(
|
||||||
typeForce('Buffer', hash)
|
types.ECCurve,
|
||||||
typeForce('ECSignature', signature)
|
types.Hash256bit,
|
||||||
typeForce('Point', Q)
|
types.ECSignature,
|
||||||
|
types.ECPoint
|
||||||
|
), arguments)
|
||||||
|
|
||||||
var n = curve.n
|
var n = curve.n
|
||||||
var G = curve.G
|
var G = curve.G
|
||||||
|
@ -162,20 +162,20 @@ function verify (curve, hash, signature, Q) {
|
||||||
* http://www.secg.org/download/aid-780/sec1-v2.pdf
|
* http://www.secg.org/download/aid-780/sec1-v2.pdf
|
||||||
*/
|
*/
|
||||||
function recoverPubKey (curve, e, signature, i) {
|
function recoverPubKey (curve, e, signature, i) {
|
||||||
typeForce('Curve', curve)
|
typeforce(types.tuple(
|
||||||
typeForce('BigInteger', e)
|
types.ECCurve,
|
||||||
typeForce('ECSignature', signature)
|
types.BigInt,
|
||||||
typeForce('Number', i)
|
types.ECSignature,
|
||||||
assert.strictEqual(i & 3, i, 'Recovery param is more than two bits')
|
types.UInt2
|
||||||
|
), arguments)
|
||||||
|
|
||||||
var n = curve.n
|
var n = curve.n
|
||||||
var G = curve.G
|
var G = curve.G
|
||||||
|
|
||||||
var r = signature.r
|
var r = signature.r
|
||||||
var s = signature.s
|
var s = signature.s
|
||||||
|
|
||||||
assert(r.signum() > 0 && r.compareTo(n) < 0, 'Invalid r value')
|
if (r.signum() <= 0 || r.compareTo(n) >= 0) throw new Error('Invalid r value')
|
||||||
assert(s.signum() > 0 && s.compareTo(n) < 0, 'Invalid s value')
|
if (s.signum() <= 0 || s.compareTo(n) >= 0) throw new Error('Invalid s value')
|
||||||
|
|
||||||
// A set LSB signifies that the y-coordinate is odd
|
// A set LSB signifies that the y-coordinate is odd
|
||||||
var isYOdd = i & 1
|
var isYOdd = i & 1
|
||||||
|
@ -190,7 +190,7 @@ function recoverPubKey (curve, e, signature, i) {
|
||||||
|
|
||||||
// 1.4 Check that nR is at infinity
|
// 1.4 Check that nR is at infinity
|
||||||
var nR = R.multiply(n)
|
var nR = R.multiply(n)
|
||||||
assert(curve.isInfinity(nR), 'nR is not a valid curve point')
|
if (!curve.isInfinity(nR)) throw new Error('nR is not a valid curve point')
|
||||||
|
|
||||||
// Compute r^-1
|
// Compute r^-1
|
||||||
var rInv = r.modInverse(n)
|
var rInv = r.modInverse(n)
|
||||||
|
@ -219,10 +219,12 @@ function recoverPubKey (curve, e, signature, i) {
|
||||||
* that resulted in a successful pubkey recovery.
|
* that resulted in a successful pubkey recovery.
|
||||||
*/
|
*/
|
||||||
function calcPubKeyRecoveryParam (curve, e, signature, Q) {
|
function calcPubKeyRecoveryParam (curve, e, signature, Q) {
|
||||||
typeForce('Curve', curve)
|
typeforce(types.tuple(
|
||||||
typeForce('BigInteger', e)
|
types.ECCurve,
|
||||||
typeForce('ECSignature', signature)
|
types.BigInt,
|
||||||
typeForce('Point', Q)
|
types.ECSignature,
|
||||||
|
types.ECPoint
|
||||||
|
), arguments)
|
||||||
|
|
||||||
for (var i = 0; i < 4; i++) {
|
for (var i = 0; i < 4; i++) {
|
||||||
var Qprime = recoverPubKey(curve, e, signature, i)
|
var Qprime = recoverPubKey(curve, e, signature, i)
|
||||||
|
|
|
@ -1,38 +1,37 @@
|
||||||
var assert = require('assert')
|
|
||||||
var bs58check = require('bs58check')
|
var bs58check = require('bs58check')
|
||||||
var bcrypto = require('./crypto')
|
var bcrypto = require('./crypto')
|
||||||
var ecdsa = require('./ecdsa')
|
var ecdsa = require('./ecdsa')
|
||||||
var ecurve = require('ecurve')
|
var ecurve = require('ecurve')
|
||||||
var NETWORKS = require('./networks')
|
var NETWORKS = require('./networks')
|
||||||
var randomBytes = require('randombytes')
|
var randomBytes = require('randombytes')
|
||||||
var typeForce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
|
var types = require('./types')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
|
|
||||||
function ECPair (d, Q, options) {
|
function ECPair (d, Q, options) {
|
||||||
options = options || {}
|
options = options || {}
|
||||||
|
|
||||||
var compressed = options.compressed === undefined ? true : options.compressed
|
typeforce({
|
||||||
var network = options.network === undefined ? NETWORKS.bitcoin : options.network
|
compressed: types.maybe(types.Boolean),
|
||||||
|
network: types.maybe(types.Network)
|
||||||
typeForce('Boolean', compressed)
|
}, options)
|
||||||
assert('pubKeyHash' in network, 'Unknown pubKeyHash constants for network')
|
|
||||||
|
|
||||||
if (d) {
|
if (d) {
|
||||||
assert(d.signum() > 0, 'Private key must be greater than 0')
|
if (d.signum() <= 0) throw new Error('Private key must be greater than 0')
|
||||||
assert(d.compareTo(ECPair.curve.n) < 0, 'Private key must be less than the curve order')
|
if (d.compareTo(ECPair.curve.n) >= 0) throw new Error('Private key must be less than the curve order')
|
||||||
|
if (Q) throw new TypeError('Unexpected publicKey parameter')
|
||||||
|
|
||||||
assert(!Q, 'Unexpected publicKey parameter')
|
|
||||||
this.d = d
|
this.d = d
|
||||||
|
|
||||||
// enforce Q is a public key if no private key given
|
|
||||||
} else {
|
} else {
|
||||||
typeForce('Point', Q)
|
typeforce(types.ECPoint, Q)
|
||||||
|
|
||||||
this.__Q = Q
|
this.__Q = Q
|
||||||
}
|
}
|
||||||
|
|
||||||
this.compressed = compressed
|
this.compressed = options.compressed === undefined ? true : options.compressed
|
||||||
this.network = network
|
this.network = options.network || NETWORKS.bitcoin
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(ECPair.prototype, 'Q', {
|
Object.defineProperty(ECPair.prototype, 'Q', {
|
||||||
|
@ -63,7 +62,7 @@ ECPair.fromWIF = function (string, networks) {
|
||||||
var compressed
|
var compressed
|
||||||
|
|
||||||
if (payload.length === 34) {
|
if (payload.length === 34) {
|
||||||
assert.strictEqual(payload[33], 0x01, 'Invalid compression flag')
|
if (payload[33] !== 0x01) throw new Error('Invalid compression flag')
|
||||||
|
|
||||||
// truncate the version/compression bytes
|
// truncate the version/compression bytes
|
||||||
payload = payload.slice(1, -1)
|
payload = payload.slice(1, -1)
|
||||||
|
@ -71,7 +70,7 @@ ECPair.fromWIF = function (string, networks) {
|
||||||
|
|
||||||
// no compression flag
|
// no compression flag
|
||||||
} else {
|
} else {
|
||||||
assert.equal(payload.length, 33, 'Invalid WIF payload length')
|
if (payload.length !== 33) throw new Error('Invalid WIF payload length')
|
||||||
|
|
||||||
// Truncate the version byte
|
// Truncate the version byte
|
||||||
payload = payload.slice(1)
|
payload = payload.slice(1)
|
||||||
|
@ -106,8 +105,7 @@ ECPair.makeRandom = function (options) {
|
||||||
|
|
||||||
var rng = options.rng || randomBytes
|
var rng = options.rng || randomBytes
|
||||||
var buffer = rng(32)
|
var buffer = rng(32)
|
||||||
typeForce('Buffer', buffer)
|
typeforce(types.Buffer256bit, buffer)
|
||||||
assert.equal(buffer.length, 32, 'Expected 256-bit Buffer from RNG')
|
|
||||||
|
|
||||||
var d = BigInteger.fromBuffer(buffer)
|
var d = BigInteger.fromBuffer(buffer)
|
||||||
d = d.mod(ECPair.curve.n)
|
d = d.mod(ECPair.curve.n)
|
||||||
|
@ -116,7 +114,7 @@ ECPair.makeRandom = function (options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ECPair.prototype.toWIF = function () {
|
ECPair.prototype.toWIF = function () {
|
||||||
assert(this.d, 'Missing private key')
|
if (!this.d) throw new Error('Missing private key')
|
||||||
|
|
||||||
var bufferLen = this.compressed ? 34 : 33
|
var bufferLen = this.compressed ? 34 : 33
|
||||||
var buffer = new Buffer(bufferLen)
|
var buffer = new Buffer(bufferLen)
|
||||||
|
@ -147,7 +145,7 @@ ECPair.prototype.getPublicKeyBuffer = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
ECPair.prototype.sign = function (hash) {
|
ECPair.prototype.sign = function (hash) {
|
||||||
assert(this.d, 'Missing private key')
|
if (!this.d) throw new Error('Missing private key')
|
||||||
|
|
||||||
return ecdsa.sign(ECPair.curve, hash, this.d)
|
return ecdsa.sign(ECPair.curve, hash, this.d)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,30 @@
|
||||||
var assert = require('assert')
|
var typeforce = require('typeforce')
|
||||||
var typeForce = require('typeforce')
|
var types = require('./types')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
|
|
||||||
function ECSignature (r, s) {
|
function ECSignature (r, s) {
|
||||||
typeForce('BigInteger', r)
|
typeforce(types.tuple(types.BigInt, types.BigInt), arguments)
|
||||||
typeForce('BigInteger', s)
|
|
||||||
|
|
||||||
this.r = r
|
this.r = r
|
||||||
this.s = s
|
this.s = s
|
||||||
}
|
}
|
||||||
|
|
||||||
ECSignature.parseCompact = function (buffer) {
|
ECSignature.parseCompact = function (buffer) {
|
||||||
assert.equal(buffer.length, 65, 'Invalid signature length')
|
if (buffer.length !== 65) throw new Error('Invalid signature length')
|
||||||
var i = buffer.readUInt8(0) - 27
|
|
||||||
|
|
||||||
// At most 3 bits
|
var flagByte = buffer.readUInt8(0) - 27
|
||||||
assert.equal(i, i & 7, 'Invalid signature parameter')
|
if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter')
|
||||||
var compressed = !!(i & 4)
|
|
||||||
|
|
||||||
// Recovery param only
|
var compressed = !!(flagByte & 4)
|
||||||
i = i & 3
|
var recoveryParam = flagByte & 3
|
||||||
|
|
||||||
var r = BigInteger.fromBuffer(buffer.slice(1, 33))
|
var r = BigInteger.fromBuffer(buffer.slice(1, 33))
|
||||||
var s = BigInteger.fromBuffer(buffer.slice(33))
|
var s = BigInteger.fromBuffer(buffer.slice(33))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
compressed: compressed,
|
compressed: compressed,
|
||||||
i: i,
|
i: recoveryParam,
|
||||||
signature: new ECSignature(r, s)
|
signature: new ECSignature(r, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +39,7 @@ ECSignature.fromDER = function (buffer) {
|
||||||
if (buffer[1] !== buffer.length - 2) throw new Error('Invalid sequence length')
|
if (buffer[1] !== buffer.length - 2) throw new Error('Invalid sequence length')
|
||||||
if (buffer[2] !== 0x02) throw new Error('Expected a DER integer')
|
if (buffer[2] !== 0x02) throw new Error('Expected a DER integer')
|
||||||
|
|
||||||
var lenR = buffer.readUInt8(3)
|
var lenR = buffer[3]
|
||||||
if (lenR === 0) throw new Error('R length is zero')
|
if (lenR === 0) throw new Error('R length is zero')
|
||||||
if (5 + lenR >= buffer.length) throw new Error('Invalid DER encoding')
|
if (5 + lenR >= buffer.length) throw new Error('Invalid DER encoding')
|
||||||
if (buffer[4 + lenR] !== 0x02) throw new Error('Expected a DER integer (2)')
|
if (buffer[4 + lenR] !== 0x02) throw new Error('Expected a DER integer (2)')
|
||||||
|
@ -117,7 +114,7 @@ ECSignature.prototype.toDER = function () {
|
||||||
|
|
||||||
ECSignature.prototype.toScriptSignature = function (hashType) {
|
ECSignature.prototype.toScriptSignature = function (hashType) {
|
||||||
var hashTypeMod = hashType & ~0x80
|
var hashTypeMod = hashType & ~0x80
|
||||||
assert(hashTypeMod > 0x00 && hashTypeMod < 0x04, 'Invalid hashType ' + hashType)
|
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||||
|
|
||||||
var hashTypeBuffer = new Buffer(1)
|
var hashTypeBuffer = new Buffer(1)
|
||||||
hashTypeBuffer.writeUInt8(hashType, 0)
|
hashTypeBuffer.writeUInt8(hashType, 0)
|
||||||
|
|
|
@ -41,6 +41,7 @@ function verify (address, signature, message, network) {
|
||||||
var parsed = ECSignature.parseCompact(signature)
|
var parsed = ECSignature.parseCompact(signature)
|
||||||
var e = BigInteger.fromBuffer(hash)
|
var e = BigInteger.fromBuffer(hash)
|
||||||
var Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i)
|
var Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i)
|
||||||
|
|
||||||
var keyPair = new ECPair(null, Q, {
|
var keyPair = new ECPair(null, Q, {
|
||||||
compressed: parsed.compressed,
|
compressed: parsed.compressed,
|
||||||
network: network
|
network: network
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
var assert = require('assert')
|
|
||||||
var bufferutils = require('./bufferutils')
|
var bufferutils = require('./bufferutils')
|
||||||
var crypto = require('./crypto')
|
var crypto = require('./crypto')
|
||||||
var typeForce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
|
var types = require('./types')
|
||||||
var opcodes = require('./opcodes')
|
var opcodes = require('./opcodes')
|
||||||
|
|
||||||
function Script (buffer, chunks) {
|
function Script (buffer, chunks) {
|
||||||
typeForce('Buffer', buffer)
|
typeforce(types.tuple(types.Buffer, types.Array), arguments)
|
||||||
typeForce('Array', chunks)
|
|
||||||
|
|
||||||
this.buffer = buffer
|
this.buffer = buffer
|
||||||
this.chunks = chunks
|
this.chunks = chunks
|
||||||
|
@ -63,7 +62,7 @@ Script.fromBuffer = function (buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.fromChunks = function (chunks) {
|
Script.fromChunks = function (chunks) {
|
||||||
typeForce('Array', chunks)
|
typeforce(types.Array, chunks)
|
||||||
|
|
||||||
var bufferSize = chunks.reduce(function (accum, chunk) {
|
var bufferSize = chunks.reduce(function (accum, chunk) {
|
||||||
// data chunk
|
// data chunk
|
||||||
|
@ -93,7 +92,7 @@ Script.fromChunks = function (chunks) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.equal(offset, buffer.length, 'Could not decode chunks')
|
if (offset !== buffer.length) throw new Error('Could not decode chunks')
|
||||||
return new Script(buffer, chunks)
|
return new Script(buffer, chunks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +102,10 @@ Script.fromHex = function (hex) {
|
||||||
|
|
||||||
Script.EMPTY = Script.fromChunks([])
|
Script.EMPTY = Script.fromChunks([])
|
||||||
|
|
||||||
|
Script.prototype.equals = function (script) {
|
||||||
|
return bufferutils.equal(this.buffer, script.buffer)
|
||||||
|
}
|
||||||
|
|
||||||
Script.prototype.getHash = function () {
|
Script.prototype.getHash = function () {
|
||||||
return crypto.hash160(this.buffer)
|
return crypto.hash160(this.buffer)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var assert = require('assert')
|
|
||||||
var ops = require('./opcodes')
|
var ops = require('./opcodes')
|
||||||
var typeForce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
|
var types = require('./types')
|
||||||
|
|
||||||
var ecurve = require('ecurve')
|
var ecurve = require('ecurve')
|
||||||
var curve = ecurve.getCurveByName('secp256k1')
|
var curve = ecurve.getCurveByName('secp256k1')
|
||||||
|
@ -134,7 +134,7 @@ function isNullDataOutput (script) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function classifyOutput (script) {
|
function classifyOutput (script) {
|
||||||
typeForce('Script', script)
|
typeforce(types.Script, script)
|
||||||
|
|
||||||
if (isPubKeyHashOutput(script)) {
|
if (isPubKeyHashOutput(script)) {
|
||||||
return 'pubkeyhash'
|
return 'pubkeyhash'
|
||||||
|
@ -152,7 +152,7 @@ function classifyOutput (script) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function classifyInput (script, allowIncomplete) {
|
function classifyInput (script, allowIncomplete) {
|
||||||
typeForce('Script', script)
|
typeforce(types.Script, script)
|
||||||
|
|
||||||
if (isPubKeyHashInput(script)) {
|
if (isPubKeyHashInput(script)) {
|
||||||
return 'pubkeyhash'
|
return 'pubkeyhash'
|
||||||
|
@ -178,7 +178,7 @@ function pubKeyOutput (pubKey) {
|
||||||
|
|
||||||
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
||||||
function pubKeyHashOutput (hash) {
|
function pubKeyHashOutput (hash) {
|
||||||
typeForce('Buffer', hash)
|
typeforce(types.Hash160bit, hash)
|
||||||
|
|
||||||
return Script.fromChunks([
|
return Script.fromChunks([
|
||||||
ops.OP_DUP,
|
ops.OP_DUP,
|
||||||
|
@ -191,7 +191,7 @@ function pubKeyHashOutput (hash) {
|
||||||
|
|
||||||
// OP_HASH160 {scriptHash} OP_EQUAL
|
// OP_HASH160 {scriptHash} OP_EQUAL
|
||||||
function scriptHashOutput (hash) {
|
function scriptHashOutput (hash) {
|
||||||
typeForce('Buffer', hash)
|
typeforce(types.Hash160bit, hash)
|
||||||
|
|
||||||
return Script.fromChunks([
|
return Script.fromChunks([
|
||||||
ops.OP_HASH160,
|
ops.OP_HASH160,
|
||||||
|
@ -202,10 +202,10 @@ function scriptHashOutput (hash) {
|
||||||
|
|
||||||
// m [pubKeys ...] n OP_CHECKMULTISIG
|
// m [pubKeys ...] n OP_CHECKMULTISIG
|
||||||
function multisigOutput (m, pubKeys) {
|
function multisigOutput (m, pubKeys) {
|
||||||
typeForce(['Buffer'], pubKeys)
|
typeforce(types.tuple(types.Number, [types.Buffer]), arguments)
|
||||||
|
|
||||||
var n = pubKeys.length
|
var n = pubKeys.length
|
||||||
assert(n >= m, 'Not enough pubKeys provided')
|
if (n < m) throw new Error('Not enough pubKeys provided')
|
||||||
|
|
||||||
return Script.fromChunks([].concat(
|
return Script.fromChunks([].concat(
|
||||||
(ops.OP_1 - 1) + m,
|
(ops.OP_1 - 1) + m,
|
||||||
|
@ -217,15 +217,14 @@ function multisigOutput (m, pubKeys) {
|
||||||
|
|
||||||
// {signature}
|
// {signature}
|
||||||
function pubKeyInput (signature) {
|
function pubKeyInput (signature) {
|
||||||
typeForce('Buffer', signature)
|
typeforce(types.Buffer, signature)
|
||||||
|
|
||||||
return Script.fromChunks([signature])
|
return Script.fromChunks([signature])
|
||||||
}
|
}
|
||||||
|
|
||||||
// {signature} {pubKey}
|
// {signature} {pubKey}
|
||||||
function pubKeyHashInput (signature, pubKey) {
|
function pubKeyHashInput (signature, pubKey) {
|
||||||
typeForce('Buffer', signature)
|
typeforce(types.tuple(types.Buffer, types.Buffer), arguments)
|
||||||
typeForce('Buffer', pubKey)
|
|
||||||
|
|
||||||
return Script.fromChunks([signature, pubKey])
|
return Script.fromChunks([signature, pubKey])
|
||||||
}
|
}
|
||||||
|
@ -241,15 +240,15 @@ function scriptHashInput (scriptSig, scriptPubKey) {
|
||||||
// OP_0 [signatures ...]
|
// OP_0 [signatures ...]
|
||||||
function multisigInput (signatures, scriptPubKey) {
|
function multisigInput (signatures, scriptPubKey) {
|
||||||
if (scriptPubKey) {
|
if (scriptPubKey) {
|
||||||
assert(isMultisigOutput(scriptPubKey))
|
if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey')
|
||||||
|
|
||||||
var mOp = scriptPubKey.chunks[0]
|
var mOp = scriptPubKey.chunks[0]
|
||||||
var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2]
|
var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2]
|
||||||
var m = mOp - (ops.OP_1 - 1)
|
var m = mOp - (ops.OP_1 - 1)
|
||||||
var n = nOp - (ops.OP_1 - 1)
|
var n = nOp - (ops.OP_1 - 1)
|
||||||
|
|
||||||
assert(signatures.length >= m, 'Not enough signatures provided')
|
if (signatures.length < m) throw new Error('Not enough signatures provided')
|
||||||
assert(signatures.length <= n, 'Too many signatures provided')
|
if (signatures.length > n) throw new Error('Too many signatures provided')
|
||||||
}
|
}
|
||||||
|
|
||||||
return Script.fromChunks([].concat(ops.OP_0, signatures))
|
return Script.fromChunks([].concat(ops.OP_0, signatures))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
var assert = require('assert')
|
|
||||||
var bufferutils = require('./bufferutils')
|
var bufferutils = require('./bufferutils')
|
||||||
var crypto = require('./crypto')
|
var crypto = require('./crypto')
|
||||||
var typeForce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
|
var types = require('./types')
|
||||||
var opcodes = require('./opcodes')
|
var opcodes = require('./opcodes')
|
||||||
|
|
||||||
var Script = require('./script')
|
var Script = require('./script')
|
||||||
|
@ -19,7 +19,7 @@ Transaction.SIGHASH_NONE = 0x02
|
||||||
Transaction.SIGHASH_SINGLE = 0x03
|
Transaction.SIGHASH_SINGLE = 0x03
|
||||||
Transaction.SIGHASH_ANYONECANPAY = 0x80
|
Transaction.SIGHASH_ANYONECANPAY = 0x80
|
||||||
|
|
||||||
Transaction.fromBuffer = function (buffer, __disableAssert) {
|
Transaction.fromBuffer = function (buffer, __noStrict) {
|
||||||
var offset = 0
|
var offset = 0
|
||||||
function readSlice (n) {
|
function readSlice (n) {
|
||||||
offset += n
|
offset += n
|
||||||
|
@ -86,9 +86,8 @@ Transaction.fromBuffer = function (buffer, __disableAssert) {
|
||||||
|
|
||||||
tx.locktime = readUInt32()
|
tx.locktime = readUInt32()
|
||||||
|
|
||||||
if (!__disableAssert) {
|
if (__noStrict) return tx
|
||||||
assert.equal(offset, buffer.length, 'Transaction has unexpected data')
|
if (offset !== buffer.length) throw new Error('Transaction has unexpected data')
|
||||||
}
|
|
||||||
|
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
@ -104,19 +103,19 @@ Transaction.isCoinbaseHash = function (buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.addInput = function (hash, index, sequence, script) {
|
Transaction.prototype.addInput = function (hash, index, sequence, script) {
|
||||||
if (sequence === undefined || sequence === null) {
|
typeforce(types.tuple(
|
||||||
|
types.Hash256bit,
|
||||||
|
types.UInt32,
|
||||||
|
types.maybe(types.UInt32),
|
||||||
|
types.maybe(types.Script)
|
||||||
|
), arguments)
|
||||||
|
|
||||||
|
if (types.Null(sequence)) {
|
||||||
sequence = Transaction.DEFAULT_SEQUENCE
|
sequence = Transaction.DEFAULT_SEQUENCE
|
||||||
}
|
}
|
||||||
|
|
||||||
script = script || Script.EMPTY
|
script = script || Script.EMPTY
|
||||||
|
|
||||||
typeForce('Buffer', hash)
|
|
||||||
typeForce('Number', index)
|
|
||||||
typeForce('Number', sequence)
|
|
||||||
typeForce('Script', script)
|
|
||||||
|
|
||||||
assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length)
|
|
||||||
|
|
||||||
// Add the input and return the input's index
|
// Add the input and return the input's index
|
||||||
return (this.ins.push({
|
return (this.ins.push({
|
||||||
hash: hash,
|
hash: hash,
|
||||||
|
@ -127,8 +126,7 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.addOutput = function (scriptPubKey, value) {
|
Transaction.prototype.addOutput = function (scriptPubKey, value) {
|
||||||
typeForce('Script', scriptPubKey)
|
typeforce(types.tuple(types.Script, types.UInt53), arguments)
|
||||||
typeForce('Number', value)
|
|
||||||
|
|
||||||
// Add the output and return the output's index
|
// Add the output and return the output's index
|
||||||
return (this.outs.push({
|
return (this.outs.push({
|
||||||
|
@ -188,11 +186,7 @@ var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000
|
||||||
* This hash can then be used to sign the provided transaction input.
|
* This hash can then be used to sign the provided transaction input.
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
|
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
|
||||||
typeForce('Number', inIndex)
|
typeforce(types.tuple(types.UInt32, types.Script, /* types.UInt8 */ types.Number), arguments)
|
||||||
typeForce('Script', prevOutScript)
|
|
||||||
typeForce('Number', hashType)
|
|
||||||
|
|
||||||
assert(inIndex >= 0, 'Invalid vin index')
|
|
||||||
|
|
||||||
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
|
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
|
||||||
if (inIndex >= this.ins.length) return ONE
|
if (inIndex >= this.ins.length) return ONE
|
||||||
|
@ -327,8 +321,7 @@ Transaction.prototype.toHex = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.setInputScript = function (index, script) {
|
Transaction.prototype.setInputScript = function (index, script) {
|
||||||
typeForce('Number', index)
|
typeforce(types.tuple(types.Number, types.Script), arguments)
|
||||||
typeForce('Script', script)
|
|
||||||
|
|
||||||
this.ins[index].script = script
|
this.ins[index].script = script
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
var assert = require('assert')
|
|
||||||
var bcrypto = require('./crypto')
|
var bcrypto = require('./crypto')
|
||||||
var bufferutils = require('./bufferutils')
|
var bufferutils = require('./bufferutils')
|
||||||
var networks = require('./networks')
|
var networks = require('./networks')
|
||||||
|
@ -160,14 +159,16 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
||||||
input.prevOutType = prevOutType
|
input.prevOutType = prevOutType
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(this.inputs.every(function (input2) {
|
var valid = this.inputs.every(function (input2) {
|
||||||
if (input2.hashType === undefined) return true
|
if (input2.hashType === undefined) return true
|
||||||
|
|
||||||
return input2.hashType & Transaction.SIGHASH_ANYONECANPAY
|
return input2.hashType & Transaction.SIGHASH_ANYONECANPAY
|
||||||
}), 'No, this would invalidate signatures')
|
})
|
||||||
|
|
||||||
|
if (!valid) throw new Error('No, this would invalidate signatures')
|
||||||
|
|
||||||
var prevOut = txHash.toString('hex') + ':' + vout
|
var prevOut = txHash.toString('hex') + ':' + vout
|
||||||
assert(!(prevOut in this.prevTxMap), 'Transaction is already an input')
|
if (this.prevTxMap[prevOut]) throw new Error('Transaction is already an input')
|
||||||
|
|
||||||
var vin = this.tx.addInput(txHash, vout, sequence)
|
var vin = this.tx.addInput(txHash, vout, sequence)
|
||||||
this.inputs[vin] = input
|
this.inputs[vin] = input
|
||||||
|
@ -177,11 +178,13 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
|
TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
|
||||||
assert(this.inputs.every(function (input) {
|
var valid = this.inputs.every(function (input) {
|
||||||
if (input.hashType === undefined) return true
|
if (input.hashType === undefined) return true
|
||||||
|
|
||||||
return (input.hashType & 0x1f) === Transaction.SIGHASH_SINGLE
|
return (input.hashType & 0x1f) === Transaction.SIGHASH_SINGLE
|
||||||
}), 'No, this would invalidate signatures')
|
})
|
||||||
|
|
||||||
|
if (!valid) throw new Error('No, this would invalidate signatures')
|
||||||
|
|
||||||
// Attempt to get a script if it's a base58 address string
|
// Attempt to get a script if it's a base58 address string
|
||||||
if (typeof scriptPubKey === 'string') {
|
if (typeof scriptPubKey === 'string') {
|
||||||
|
@ -206,8 +209,8 @@ var canSignTypes = {
|
||||||
|
|
||||||
TransactionBuilder.prototype.__build = function (allowIncomplete) {
|
TransactionBuilder.prototype.__build = function (allowIncomplete) {
|
||||||
if (!allowIncomplete) {
|
if (!allowIncomplete) {
|
||||||
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
if (!this.tx.ins.length) throw new Error('Transaction has no inputs')
|
||||||
assert(this.tx.outs.length > 0, 'Transaction has no outputs')
|
if (!this.tx.outs.length) throw new Error('Transaction has no outputs')
|
||||||
}
|
}
|
||||||
|
|
||||||
var tx = this.tx.clone()
|
var tx = this.tx.clone()
|
||||||
|
@ -218,9 +221,9 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
|
||||||
var scriptSig
|
var scriptSig
|
||||||
|
|
||||||
if (!allowIncomplete) {
|
if (!allowIncomplete) {
|
||||||
assert(!!scriptType, 'Transaction is not complete')
|
if (!scriptType) throw new Error('Transaction is not complete')
|
||||||
assert(scriptType in canSignTypes, scriptType + ' not supported')
|
if (!canSignTypes[scriptType]) throw new Error(scriptType + ' not supported')
|
||||||
assert(input.signatures, 'Transaction is missing signatures')
|
if (!input.signatures) throw new Error('Transaction is missing signatures')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.signatures) {
|
if (input.signatures) {
|
||||||
|
@ -274,8 +277,8 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hashType) {
|
TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hashType) {
|
||||||
assert.equal(keyPair.network, this.network, 'Inconsistent network')
|
if (keyPair.network !== this.network) throw new Error('Inconsistent network')
|
||||||
assert(index in this.inputs, 'No input at index: ' + index)
|
if (!this.inputs[index]) throw new Error('No input at index: ' + index)
|
||||||
hashType = hashType || Transaction.SIGHASH_ALL
|
hashType = hashType || Transaction.SIGHASH_ALL
|
||||||
|
|
||||||
var input = this.inputs[index]
|
var input = this.inputs[index]
|
||||||
|
@ -292,10 +295,10 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
||||||
if (canSign) {
|
if (canSign) {
|
||||||
// if redeemScript was provided, enforce consistency
|
// if redeemScript was provided, enforce consistency
|
||||||
if (redeemScript) {
|
if (redeemScript) {
|
||||||
assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript')
|
if (!input.redeemScript.equals(redeemScript)) throw new Error('Inconsistent redeemScript')
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.equal(input.hashType, hashType, 'Inconsistent hashType')
|
if (input.hashType !== hashType) throw new Error('Inconsistent hashType')
|
||||||
|
|
||||||
// no? prepare
|
// no? prepare
|
||||||
} else {
|
} else {
|
||||||
|
@ -303,14 +306,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
||||||
if (redeemScript) {
|
if (redeemScript) {
|
||||||
// if we have a prevOutScript, enforce scriptHash equality to the redeemScript
|
// if we have a prevOutScript, enforce scriptHash equality to the redeemScript
|
||||||
if (input.prevOutScript) {
|
if (input.prevOutScript) {
|
||||||
assert.equal(input.prevOutType, 'scripthash', 'PrevOutScript must be P2SH')
|
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
|
||||||
|
|
||||||
var scriptHash = input.prevOutScript.chunks[1]
|
var scriptHash = input.prevOutScript.chunks[1]
|
||||||
assert.deepEqual(scriptHash, redeemScript.getHash(), 'RedeemScript does not match ' + scriptHash.toString('hex'))
|
if (!bufferutils.equal(scriptHash, redeemScript.getHash())) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
|
||||||
}
|
}
|
||||||
|
|
||||||
var scriptType = scripts.classifyOutput(redeemScript)
|
var scriptType = scripts.classifyOutput(redeemScript)
|
||||||
assert(scriptType in canSignTypes, 'RedeemScript not supported (' + scriptType + ')')
|
if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')')
|
||||||
|
|
||||||
var pubKeys = []
|
var pubKeys = []
|
||||||
switch (scriptType) {
|
switch (scriptType) {
|
||||||
|
@ -322,7 +325,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
||||||
var pkh1 = redeemScript.chunks[2]
|
var pkh1 = redeemScript.chunks[2]
|
||||||
var pkh2 = bcrypto.hash160(keyPair.getPublicKeyBuffer())
|
var pkh2 = bcrypto.hash160(keyPair.getPublicKeyBuffer())
|
||||||
|
|
||||||
assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input')
|
if (!bufferutils.equal(pkh1, pkh2)) throw new Error('privateKey cannot sign for this input')
|
||||||
pubKeys = [kpPubKey]
|
pubKeys = [kpPubKey]
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -342,11 +345,11 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
||||||
|
|
||||||
// cannot be pay-to-scriptHash
|
// cannot be pay-to-scriptHash
|
||||||
} else {
|
} else {
|
||||||
assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript')
|
if (input.prevOutType === 'scripthash') throw new Error('PrevOutScript is P2SH, missing redeemScript')
|
||||||
|
|
||||||
// can we otherwise sign this?
|
// can we otherwise sign this?
|
||||||
if (input.scriptType) {
|
if (input.scriptType) {
|
||||||
assert(input.pubKeys, input.scriptType + ' not supported')
|
if (!input.pubKeys) throw new Error(input.scriptType + ' not supported')
|
||||||
|
|
||||||
// we know nothin' Jon Snow, assume pubKeyHash
|
// we know nothin' Jon Snow, assume pubKeyHash
|
||||||
} else {
|
} else {
|
||||||
|
@ -389,16 +392,17 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
||||||
}
|
}
|
||||||
|
|
||||||
// enforce in order signing of public keys
|
// enforce in order signing of public keys
|
||||||
assert(input.pubKeys.some(function (pubKey, i) {
|
var valid = input.pubKeys.some(function (pubKey, i) {
|
||||||
if (!bufferutils.equal(kpPubKey, pubKey)) return false
|
if (!bufferutils.equal(kpPubKey, pubKey)) return false
|
||||||
|
if (input.signatures[i]) throw new Error('Signature already exists')
|
||||||
assert(!input.signatures[i], 'Signature already exists')
|
|
||||||
|
|
||||||
var signature = keyPair.sign(signatureHash)
|
var signature = keyPair.sign(signatureHash)
|
||||||
input.signatures[i] = signature
|
input.signatures[i] = signature
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}), 'key pair cannot sign for this input')
|
})
|
||||||
|
|
||||||
|
if (!valid) throw new Error('Key pair cannot sign for this input')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TransactionBuilder
|
module.exports = TransactionBuilder
|
||||||
|
|
71
src/types.js
Normal file
71
src/types.js
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
var bigi = require('bigi')
|
||||||
|
var ecurve = require('ecurve')
|
||||||
|
var typeforce = require('typeforce')
|
||||||
|
|
||||||
|
function nBuffer (value, n) {
|
||||||
|
if (!Buffer.isBuffer(value)) return false
|
||||||
|
if (value.length !== n) throw new Error('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit')
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
function Hash160bit (value) { return nBuffer(value, 20) }
|
||||||
|
function Hash256bit (value) { return nBuffer(value, 32) }
|
||||||
|
function Buffer256bit (value) { return nBuffer(value, 32) }
|
||||||
|
|
||||||
|
var UINT53_MAX = Math.pow(2, 53) - 1
|
||||||
|
function UInt2 (value) { return (value & 3) === value }
|
||||||
|
function UInt8 (value) { return (value & 0xff) === value }
|
||||||
|
function UInt32 (value) { return (value >>> 0) === value }
|
||||||
|
function UInt53 (value) {
|
||||||
|
return typeforce.Number(value) &&
|
||||||
|
value >= 0 &&
|
||||||
|
value <= UINT53_MAX &&
|
||||||
|
Math.floor(value) === value
|
||||||
|
}
|
||||||
|
|
||||||
|
// external dependent types
|
||||||
|
function BigInt (value) { return !typeforce.Null(value) && value.constructor === bigi }
|
||||||
|
function ECCurve (value) { return !typeforce.Null(value) && value.constructor === ecurve.Curve }
|
||||||
|
function ECPoint (value) { return !typeforce.Null(value) && value.constructor === ecurve.Point }
|
||||||
|
|
||||||
|
// exposed, external API
|
||||||
|
var ECSignature = typeforce.compile({ r: BigInt, s: BigInt })
|
||||||
|
var Network = typeforce.compile({
|
||||||
|
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
|
||||||
|
bip32: {
|
||||||
|
public: UInt32,
|
||||||
|
private: UInt32
|
||||||
|
},
|
||||||
|
pubKeyHash: UInt8,
|
||||||
|
scriptHash: UInt8,
|
||||||
|
wif: UInt8,
|
||||||
|
dustThreshold: UInt53
|
||||||
|
})
|
||||||
|
|
||||||
|
var Script = typeforce.compile({
|
||||||
|
buffer: typeforce.Buffer,
|
||||||
|
chunks: typeforce.arrayOf(typeforce.oneOf(typeforce.Number, typeforce.Buffer))
|
||||||
|
})
|
||||||
|
|
||||||
|
// extend typeforce types with ours
|
||||||
|
var types = {
|
||||||
|
BigInt: BigInt,
|
||||||
|
Buffer256bit: Buffer256bit,
|
||||||
|
ECCurve: ECCurve,
|
||||||
|
ECPoint: ECPoint,
|
||||||
|
ECSignature: ECSignature,
|
||||||
|
Hash160bit: Hash160bit,
|
||||||
|
Hash256bit: Hash256bit,
|
||||||
|
Network: Network,
|
||||||
|
Script: Script,
|
||||||
|
UInt2: UInt2,
|
||||||
|
UInt8: UInt8,
|
||||||
|
UInt32: UInt32,
|
||||||
|
UInt53: UInt53
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var typeName in typeforce) {
|
||||||
|
types[typeName] = typeforce[typeName]
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = types
|
2
test/fixtures/ecdsa.json
vendored
2
test/fixtures/ecdsa.json
vendored
|
@ -188,7 +188,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "Invalid i value (> 3)",
|
"description": "Invalid i value (> 3)",
|
||||||
"exception": "Recovery param is more than two bits",
|
"exception": "Expected UInt2, got Number 4",
|
||||||
"e": "01",
|
"e": "01",
|
||||||
"signatureRaw": {
|
"signatureRaw": {
|
||||||
"r": "00",
|
"r": "00",
|
||||||
|
|
4
test/fixtures/transaction.json
vendored
4
test/fixtures/transaction.json
vendored
|
@ -229,12 +229,12 @@
|
||||||
"invalid": {
|
"invalid": {
|
||||||
"addInput": [
|
"addInput": [
|
||||||
{
|
{
|
||||||
"exception": "Expected hash length of 32, got 30",
|
"exception": "Expected 256-bit Buffer, got 240-bit",
|
||||||
"hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816d",
|
"hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816d",
|
||||||
"index": 0
|
"index": 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"exception": "Expected hash length of 32, got 34",
|
"exception": "Expected 256-bit Buffer, got 272-bit",
|
||||||
"hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816da9b0ffff",
|
"hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816da9b0ffff",
|
||||||
"index": 0
|
"index": 0
|
||||||
}
|
}
|
||||||
|
|
2
test/fixtures/transaction_builder.json
vendored
2
test/fixtures/transaction_builder.json
vendored
|
@ -780,7 +780,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"description": "Wrong key pair for multisig redeemScript",
|
"description": "Wrong key pair for multisig redeemScript",
|
||||||
"exception": "key pair cannot sign for this input",
|
"exception": "Key pair cannot sign for this input",
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
|
Loading…
Reference in a new issue