bitcoinjs-lib
Advanced tools
Comparing version 1.5.0 to 1.5.1
{ | ||
"name": "bitcoinjs-lib", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Client-side Bitcoin JavaScript library", | ||
@@ -39,3 +39,3 @@ "main": "./src/index.js", | ||
"integration": "mocha --reporter list test/integration/*.js", | ||
"jshint": "jshint --config jshint.json src/*.js ; true", | ||
"standard": "standard", | ||
"test": "npm run-script unit", | ||
@@ -61,7 +61,7 @@ "unit": "istanbul test mocha -- --reporter list test/*.js" | ||
"istanbul": "^0.3.5", | ||
"jshint": "^2.5.11", | ||
"mocha": "^2.1.0", | ||
"mocha-lcov-reporter": "0.0.1", | ||
"sinon": "^1.12.2" | ||
"sinon": "^1.12.2", | ||
"standard": "^2.7.3" | ||
} | ||
} |
@@ -7,3 +7,3 @@ var assert = require('assert') | ||
function findScriptTypeByVersion(version) { | ||
function findScriptTypeByVersion (version) { | ||
for (var networkName in networks) { | ||
@@ -17,3 +17,3 @@ var network = networks[networkName] | ||
function Address(hash, version) { | ||
function Address (hash, version) { | ||
typeForce('Buffer', hash) | ||
@@ -28,3 +28,3 @@ | ||
Address.fromBase58Check = function(string) { | ||
Address.fromBase58Check = function (string) { | ||
var payload = base58check.decode(string) | ||
@@ -37,3 +37,3 @@ var version = payload.readUInt8(0) | ||
Address.fromOutputScript = function(script, network) { | ||
Address.fromOutputScript = function (script, network) { | ||
network = network || networks.bitcoin | ||
@@ -55,3 +55,3 @@ | ||
Address.prototype.toOutputScript = function() { | ||
Address.prototype.toOutputScript = function () { | ||
var scriptType = findScriptTypeByVersion(this.version) | ||
@@ -58,0 +58,0 @@ |
var bs58check = require('bs58check') | ||
function decode() { | ||
console.warn('bs58check will be removed in 2.0.0. require("bs58check") instead.'); | ||
function decode () { | ||
console.warn('bs58check will be removed in 2.0.0. require("bs58check") instead.') | ||
@@ -9,4 +9,4 @@ return bs58check.decode.apply(undefined, arguments) | ||
function encode() { | ||
console.warn('bs58check will be removed in 2.0.0. require("bs58check") instead.'); | ||
function encode () { | ||
console.warn('bs58check will be removed in 2.0.0. require("bs58check") instead.') | ||
@@ -13,0 +13,0 @@ return bs58check.encode.apply(undefined, arguments) |
@@ -6,5 +6,4 @@ var assert = require('assert') | ||
var Transaction = require('./transaction') | ||
var Script = require('./script') | ||
function Block() { | ||
function Block () { | ||
this.version = 1 | ||
@@ -18,7 +17,7 @@ this.prevHash = null | ||
Block.fromBuffer = function(buffer) { | ||
Block.fromBuffer = function (buffer) { | ||
assert(buffer.length >= 80, 'Buffer too small (< 80 bytes)') | ||
var offset = 0 | ||
function readSlice(n) { | ||
function readSlice (n) { | ||
offset += n | ||
@@ -28,3 +27,3 @@ return buffer.slice(offset - n, offset) | ||
function readUInt32() { | ||
function readUInt32 () { | ||
var i = buffer.readUInt32LE(offset) | ||
@@ -45,3 +44,3 @@ offset += 4 | ||
function readVarInt() { | ||
function readVarInt () { | ||
var vi = bufferutils.readVarInt(buffer, offset) | ||
@@ -53,3 +52,3 @@ offset += vi.size | ||
// FIXME: poor performance | ||
function readTransaction() { | ||
function readTransaction () { | ||
var tx = Transaction.fromBuffer(buffer.slice(offset), true) | ||
@@ -72,15 +71,15 @@ | ||
Block.fromHex = function(hex) { | ||
Block.fromHex = function (hex) { | ||
return Block.fromBuffer(new Buffer(hex, 'hex')) | ||
} | ||
Block.prototype.getHash = function() { | ||
Block.prototype.getHash = function () { | ||
return crypto.hash256(this.toBuffer(true)) | ||
} | ||
Block.prototype.getId = function() { | ||
Block.prototype.getId = function () { | ||
return bufferutils.reverse(this.getHash()).toString('hex') | ||
} | ||
Block.prototype.getUTCDate = function() { | ||
Block.prototype.getUTCDate = function () { | ||
var date = new Date(0) // epoch | ||
@@ -92,7 +91,7 @@ date.setUTCSeconds(this.timestamp) | ||
Block.prototype.toBuffer = function(headersOnly) { | ||
Block.prototype.toBuffer = function (headersOnly) { | ||
var buffer = new Buffer(80) | ||
var offset = 0 | ||
function writeSlice(slice) { | ||
function writeSlice (slice) { | ||
slice.copy(buffer, offset) | ||
@@ -102,3 +101,3 @@ offset += slice.length | ||
function writeUInt32(i) { | ||
function writeUInt32 (i) { | ||
buffer.writeUInt32LE(i, offset) | ||
@@ -118,3 +117,3 @@ offset += 4 | ||
var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length) | ||
var txBuffers = this.transactions.map(function(tx) { | ||
var txBuffers = this.transactions.map(function (tx) { | ||
return tx.toBuffer() | ||
@@ -126,3 +125,3 @@ }) | ||
Block.prototype.toHex = function(headersOnly) { | ||
Block.prototype.toHex = function (headersOnly) { | ||
return this.toBuffer(headersOnly).toString('hex') | ||
@@ -129,0 +128,0 @@ } |
@@ -5,3 +5,3 @@ var assert = require('assert') | ||
// 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') | ||
@@ -13,10 +13,10 @@ assert(value >= 0, 'specified a negative value for writing an unsigned value') | ||
function pushDataSize(i) { | ||
function pushDataSize (i) { | ||
return i < opcodes.OP_PUSHDATA1 ? 1 | ||
: i < 0xff ? 2 | ||
: i < 0xffff ? 3 | ||
: 5 | ||
: i < 0xff ? 2 | ||
: i < 0xffff ? 3 | ||
: 5 | ||
} | ||
function readPushDataInt(buffer, offset) { | ||
function readPushDataInt (buffer, offset) { | ||
var opcode = buffer.readUInt8(offset) | ||
@@ -32,2 +32,3 @@ var number, size | ||
} else if (opcode === opcodes.OP_PUSHDATA1) { | ||
if (offset + 2 > buffer.length) return null | ||
number = buffer.readUInt8(offset + 1) | ||
@@ -38,2 +39,3 @@ size = 2 | ||
} else if (opcode === opcodes.OP_PUSHDATA2) { | ||
if (offset + 3 > buffer.length) return null | ||
number = buffer.readUInt16LE(offset + 1) | ||
@@ -44,2 +46,3 @@ size = 3 | ||
} else { | ||
if (offset + 5 > buffer.length) return null | ||
assert.equal(opcode, opcodes.OP_PUSHDATA4, 'Unexpected opcode') | ||
@@ -49,3 +52,2 @@ | ||
size = 5 | ||
} | ||
@@ -60,3 +62,3 @@ | ||
function readUInt64LE(buffer, offset) { | ||
function readUInt64LE (buffer, offset) { | ||
var a = buffer.readUInt32LE(offset) | ||
@@ -71,3 +73,3 @@ var b = buffer.readUInt32LE(offset + 4) | ||
function readVarInt(buffer, offset) { | ||
function readVarInt (buffer, offset) { | ||
var t = buffer.readUInt8(offset) | ||
@@ -103,3 +105,3 @@ var number, size | ||
function writePushDataInt(buffer, number, offset) { | ||
function writePushDataInt (buffer, number, offset) { | ||
var size = pushDataSize(number) | ||
@@ -125,3 +127,2 @@ | ||
buffer.writeUInt32LE(number, offset + 1) | ||
} | ||
@@ -132,3 +133,3 @@ | ||
function writeUInt64LE(buffer, value, offset) { | ||
function writeUInt64LE (buffer, value, offset) { | ||
verifuint(value, 0x001fffffffffffff) | ||
@@ -140,10 +141,10 @@ | ||
function varIntSize(i) { | ||
return i < 253 ? 1 | ||
: i < 0x10000 ? 3 | ||
: i < 0x100000000 ? 5 | ||
: 9 | ||
function varIntSize (i) { | ||
return i < 253 ? 1 | ||
: i < 0x10000 ? 3 | ||
: i < 0x100000000 ? 5 | ||
: 9 | ||
} | ||
function writeVarInt(buffer, number, offset) { | ||
function writeVarInt (buffer, number, offset) { | ||
var size = varIntSize(number) | ||
@@ -174,3 +175,3 @@ | ||
function varIntBuffer(i) { | ||
function varIntBuffer (i) { | ||
var size = varIntSize(i) | ||
@@ -183,3 +184,3 @@ var buffer = new Buffer(size) | ||
function reverse(buffer) { | ||
function reverse (buffer) { | ||
var buffer2 = new Buffer(buffer) | ||
@@ -186,0 +187,0 @@ Array.prototype.reverse.call(buffer2) |
var crypto = require('crypto') | ||
function hash160(buffer) { | ||
function hash160 (buffer) { | ||
return ripemd160(sha256(buffer)) | ||
} | ||
function hash256(buffer) { | ||
function hash256 (buffer) { | ||
return sha256(sha256(buffer)) | ||
} | ||
function ripemd160(buffer) { | ||
function ripemd160 (buffer) { | ||
return crypto.createHash('rmd160').update(buffer).digest() | ||
} | ||
function sha1(buffer) { | ||
function sha1 (buffer) { | ||
return crypto.createHash('sha1').update(buffer).digest() | ||
} | ||
function sha256(buffer) { | ||
function sha256 (buffer) { | ||
return crypto.createHash('sha256').update(buffer).digest() | ||
@@ -24,3 +24,3 @@ } | ||
// FIXME: Name not consistent with others | ||
function HmacSHA256(buffer, secret) { | ||
function HmacSHA256 (buffer, secret) { | ||
console.warn('Hmac* functions are deprecated for removal in 2.0.0, use node crypto instead') | ||
@@ -30,3 +30,3 @@ return crypto.createHmac('sha256', secret).update(buffer).digest() | ||
function HmacSHA512(buffer, secret) { | ||
function HmacSHA512 (buffer, secret) { | ||
console.warn('Hmac* functions are deprecated for removal in 2.0.0, use node crypto instead') | ||
@@ -33,0 +33,0 @@ return crypto.createHmac('sha512', secret).update(buffer).digest() |
@@ -12,3 +12,3 @@ var assert = require('assert') | ||
// https://tools.ietf.org/html/rfc6979#section-3.2 | ||
function deterministicGenerateK(curve, hash, d, checkSig) { | ||
function deterministicGenerateK (curve, hash, d, checkSig) { | ||
typeForce('Buffer', hash) | ||
@@ -18,3 +18,3 @@ typeForce('BigInteger', d) | ||
// FIXME: remove/uncomment for 2.0.0 | ||
// typeForce('Function', checkSig) | ||
// typeForce('Function', checkSig) | ||
@@ -24,3 +24,3 @@ if (typeof checkSig !== 'function') { | ||
checkSig = function(k) { | ||
checkSig = function (k) { | ||
var G = curve.G | ||
@@ -107,3 +107,3 @@ var n = curve.n | ||
function sign(curve, hash, d) { | ||
function sign (curve, hash, d) { | ||
var r, s | ||
@@ -115,3 +115,3 @@ | ||
deterministicGenerateK(curve, hash, d, function(k) { | ||
deterministicGenerateK(curve, hash, d, function (k) { | ||
var Q = G.multiply(k) | ||
@@ -143,3 +143,3 @@ | ||
function verifyRaw(curve, e, signature, Q) { | ||
function verifyRaw (curve, e, signature, Q) { | ||
var n = curve.n | ||
@@ -174,3 +174,3 @@ var G = curve.G | ||
function verify(curve, hash, signature, Q) { | ||
function verify (curve, hash, signature, Q) { | ||
// 1.4.2 H = Hash(M), already done by the user | ||
@@ -191,3 +191,3 @@ // 1.4.3 e = H | ||
*/ | ||
function recoverPubKey(curve, e, signature, i) { | ||
function recoverPubKey (curve, e, signature, i) { | ||
assert.strictEqual(i & 3, i, 'Recovery param is more than two bits') | ||
@@ -243,3 +243,3 @@ | ||
*/ | ||
function calcPubKeyRecoveryParam(curve, e, signature, Q) { | ||
function calcPubKeyRecoveryParam (curve, e, signature, Q) { | ||
for (var i = 0; i < 4; i++) { | ||
@@ -246,0 +246,0 @@ var Qprime = recoverPubKey(curve, e, signature, i) |
@@ -14,3 +14,3 @@ var assert = require('assert') | ||
function ECKey(d, compressed) { | ||
function ECKey (d, compressed) { | ||
assert(d.signum() > 0, 'Private key must be greater than 0') | ||
@@ -29,3 +29,3 @@ assert(d.compareTo(ECKey.curve.n) < 0, 'Private key must be less than the curve order') | ||
// Static constructors | ||
ECKey.fromWIF = function(string) { | ||
ECKey.fromWIF = function (string) { | ||
var payload = base58check.decode(string) | ||
@@ -51,3 +51,3 @@ var compressed = false | ||
ECKey.makeRandom = function(compressed, rng) { | ||
ECKey.makeRandom = function (compressed, rng) { | ||
rng = rng || crypto.randomBytes | ||
@@ -66,3 +66,3 @@ | ||
// Export functions | ||
ECKey.prototype.toWIF = function(network) { | ||
ECKey.prototype.toWIF = function (network) { | ||
network = network || networks.bitcoin | ||
@@ -84,3 +84,3 @@ | ||
// Operations | ||
ECKey.prototype.sign = function(hash) { | ||
ECKey.prototype.sign = function (hash) { | ||
return ecdsa.sign(ECKey.curve, hash, this.d) | ||
@@ -87,0 +87,0 @@ } |
@@ -11,4 +11,6 @@ var crypto = require('./crypto') | ||
function ECPubKey(Q, compressed) { | ||
if (compressed === undefined) compressed = true | ||
function ECPubKey (Q, compressed) { | ||
if (compressed === undefined) { | ||
compressed = true | ||
} | ||
@@ -26,3 +28,3 @@ typeForce('Point', Q) | ||
// Static constructors | ||
ECPubKey.fromBuffer = function(buffer) { | ||
ECPubKey.fromBuffer = function (buffer) { | ||
var Q = ecurve.Point.decodeFrom(ECPubKey.curve, buffer) | ||
@@ -32,3 +34,3 @@ return new ECPubKey(Q, Q.compressed) | ||
ECPubKey.fromHex = function(hex) { | ||
ECPubKey.fromHex = function (hex) { | ||
return ECPubKey.fromBuffer(new Buffer(hex, 'hex')) | ||
@@ -38,3 +40,3 @@ } | ||
// Operations | ||
ECPubKey.prototype.getAddress = function(network) { | ||
ECPubKey.prototype.getAddress = function (network) { | ||
network = network || networks.bitcoin | ||
@@ -45,3 +47,3 @@ | ||
ECPubKey.prototype.verify = function(hash, signature) { | ||
ECPubKey.prototype.verify = function (hash, signature) { | ||
return ecdsa.verify(ECPubKey.curve, hash, signature, this.Q) | ||
@@ -51,7 +53,7 @@ } | ||
// Export functions | ||
ECPubKey.prototype.toBuffer = function() { | ||
ECPubKey.prototype.toBuffer = function () { | ||
return this.Q.getEncoded(this.compressed) | ||
} | ||
ECPubKey.prototype.toHex = function() { | ||
ECPubKey.prototype.toHex = function () { | ||
return this.toBuffer().toString('hex') | ||
@@ -58,0 +60,0 @@ } |
@@ -6,3 +6,3 @@ var assert = require('assert') | ||
function ECSignature(r, s) { | ||
function ECSignature (r, s) { | ||
typeForce('BigInteger', r) | ||
@@ -15,3 +15,3 @@ typeForce('BigInteger', s) | ||
ECSignature.parseCompact = function(buffer) { | ||
ECSignature.parseCompact = function (buffer) { | ||
assert.equal(buffer.length, 65, 'Invalid signature length') | ||
@@ -37,3 +37,3 @@ var i = buffer.readUInt8(0) - 27 | ||
ECSignature.fromDER = function(buffer) { | ||
ECSignature.fromDER = function (buffer) { | ||
assert.equal(buffer.readUInt8(0), 0x30, 'Not a DER sequence') | ||
@@ -75,3 +75,3 @@ assert.equal(buffer.readUInt8(1), buffer.length - 2, 'Invalid sequence length') | ||
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed) | ||
ECSignature.parseScriptSignature = function(buffer) { | ||
ECSignature.parseScriptSignature = function (buffer) { | ||
var hashType = buffer.readUInt8(buffer.length - 1) | ||
@@ -88,4 +88,7 @@ var hashTypeMod = hashType & ~0x80 | ||
ECSignature.prototype.toCompact = function(i, compressed) { | ||
if (compressed) i += 4 | ||
ECSignature.prototype.toCompact = function (i, compressed) { | ||
if (compressed) { | ||
i += 4 | ||
} | ||
i += 27 | ||
@@ -102,3 +105,3 @@ | ||
ECSignature.prototype.toDER = function() { | ||
ECSignature.prototype.toDER = function () { | ||
var rBa = this.r.toDERInteger() | ||
@@ -123,3 +126,3 @@ var sBa = this.s.toDERInteger() | ||
ECSignature.prototype.toScriptSignature = function(hashType) { | ||
ECSignature.prototype.toScriptSignature = function (hashType) { | ||
var hashTypeMod = hashType & ~0x80 | ||
@@ -126,0 +129,0 @@ assert(hashTypeMod > 0x00 && hashTypeMod < 0x04, 'Invalid hashType ' + hashType) |
@@ -15,9 +15,7 @@ var assert = require('assert') | ||
function findBIP32NetworkByVersion(version) { | ||
function findBIP32NetworkByVersion (version) { | ||
for (var name in networks) { | ||
var network = networks[name] | ||
if (version === network.bip32.private || | ||
version === network.bip32.public) { | ||
if (version === network.bip32.private || version === network.bip32.public) { | ||
return network | ||
@@ -30,3 +28,3 @@ } | ||
function HDNode(K, chainCode, network) { | ||
function HDNode (K, chainCode, network) { | ||
network = network || networks.bitcoin | ||
@@ -63,3 +61,3 @@ | ||
HDNode.fromSeedBuffer = function(seed, network) { | ||
HDNode.fromSeedBuffer = function (seed, network) { | ||
typeForce('Buffer', seed) | ||
@@ -81,7 +79,7 @@ | ||
HDNode.fromSeedHex = function(hex, network) { | ||
HDNode.fromSeedHex = function (hex, network) { | ||
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network) | ||
} | ||
HDNode.fromBase58 = function(string, network) { | ||
HDNode.fromBase58 = function (string, network) { | ||
return HDNode.fromBuffer(base58check.decode(string), network, true) | ||
@@ -91,3 +89,3 @@ } | ||
// FIXME: remove in 2.x.y | ||
HDNode.fromBuffer = function(buffer, network, __ignoreDeprecation) { | ||
HDNode.fromBuffer = function (buffer, network, __ignoreDeprecation) { | ||
if (!__ignoreDeprecation) { | ||
@@ -103,3 +101,3 @@ console.warn('HDNode.fromBuffer() is deprecated for removal in 2.x.y, use fromBase58 instead') | ||
if (network) { | ||
assert(version === network.bip32.private || version === network.bip32.public, 'Network doesn\'t match') | ||
assert(version === network.bip32.private || version === network.bip32.public, "Network doesn't match") | ||
@@ -157,19 +155,19 @@ // auto-detect | ||
// FIXME: remove in 2.x.y | ||
HDNode.fromHex = function(hex, network) { | ||
HDNode.fromHex = function (hex, network) { | ||
return HDNode.fromBuffer(new Buffer(hex, 'hex'), network) | ||
} | ||
HDNode.prototype.getIdentifier = function() { | ||
HDNode.prototype.getIdentifier = function () { | ||
return bcrypto.hash160(this.pubKey.toBuffer()) | ||
} | ||
HDNode.prototype.getFingerprint = function() { | ||
HDNode.prototype.getFingerprint = function () { | ||
return this.getIdentifier().slice(0, 4) | ||
} | ||
HDNode.prototype.getAddress = function() { | ||
HDNode.prototype.getAddress = function () { | ||
return this.pubKey.getAddress(this.network) | ||
} | ||
HDNode.prototype.neutered = function() { | ||
HDNode.prototype.neutered = function () { | ||
var neutered = new HDNode(this.pubKey.Q, this.chainCode, this.network) | ||
@@ -183,3 +181,3 @@ neutered.depth = this.depth | ||
HDNode.prototype.toBase58 = function(isPrivate) { | ||
HDNode.prototype.toBase58 = function (isPrivate) { | ||
return base58check.encode(this.toBuffer(isPrivate, true)) | ||
@@ -189,3 +187,3 @@ } | ||
// FIXME: remove in 2.x.y | ||
HDNode.prototype.toBuffer = function(isPrivate, __ignoreDeprecation) { | ||
HDNode.prototype.toBuffer = function (isPrivate, __ignoreDeprecation) { | ||
if (isPrivate === undefined) { | ||
@@ -233,3 +231,2 @@ isPrivate = !!this.privKey | ||
} else { | ||
// X9.62 encoding for public keys | ||
@@ -243,3 +240,3 @@ this.pubKey.toBuffer().copy(buffer, 45) | ||
// FIXME: remove in 2.x.y | ||
HDNode.prototype.toHex = function(isPrivate) { | ||
HDNode.prototype.toHex = function (isPrivate) { | ||
return this.toBuffer(isPrivate).toString('hex') | ||
@@ -249,3 +246,3 @@ } | ||
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions | ||
HDNode.prototype.derive = function(index) { | ||
HDNode.prototype.derive = function (index) { | ||
var isHardened = index >= HDNode.HIGHEST_BIT | ||
@@ -322,3 +319,3 @@ var indexBuffer = new Buffer(4) | ||
HDNode.prototype.deriveHardened = function(index) { | ||
HDNode.prototype.deriveHardened = function (index) { | ||
// Only derives hardened private keys by default | ||
@@ -325,0 +322,0 @@ return this.derive(index + HDNode.HIGHEST_BIT) |
@@ -13,3 +13,3 @@ var bufferutils = require('./bufferutils') | ||
function magicHash(message, network) { | ||
function magicHash (message, network) { | ||
var magicPrefix = new Buffer(network.magicPrefix) | ||
@@ -23,3 +23,3 @@ var messageBuffer = new Buffer(message) | ||
function sign(privKey, message, network) { | ||
function sign (privKey, message, network) { | ||
network = network || networks.bitcoin | ||
@@ -36,3 +36,3 @@ | ||
// TODO: network could be implied from address | ||
function verify(address, signature, message, network) { | ||
function verify (address, signature, message, network) { | ||
if (!Buffer.isBuffer(signature)) { | ||
@@ -39,0 +39,0 @@ signature = new Buffer(signature, 'base64') |
@@ -130,4 +130,4 @@ // https://en.bitcoin.it/wiki/List_of_address_prefixes | ||
function estimateFee(type) { | ||
return function(tx) { | ||
function estimateFee (type) { | ||
return function (tx) { | ||
var network = networks[type] | ||
@@ -138,5 +138,5 @@ var baseFee = network.feePerKb | ||
var fee = baseFee * Math.ceil(byteSize / 1000) | ||
if (network.dustSoftThreshold == undefined) return fee | ||
if (network.dustSoftThreshold === undefined) return fee | ||
tx.outs.forEach(function(e){ | ||
tx.outs.forEach(function (e) { | ||
if (e.value < network.dustSoftThreshold) { | ||
@@ -143,0 +143,0 @@ fee += baseFee |
module.exports = { | ||
// push value | ||
OP_FALSE : 0, | ||
OP_0 : 0, | ||
OP_PUSHDATA1 : 76, | ||
OP_PUSHDATA2 : 77, | ||
OP_PUSHDATA4 : 78, | ||
OP_1NEGATE : 79, | ||
OP_RESERVED : 80, | ||
OP_1 : 81, | ||
OP_TRUE : 81, | ||
OP_2 : 82, | ||
OP_3 : 83, | ||
OP_4 : 84, | ||
OP_5 : 85, | ||
OP_6 : 86, | ||
OP_7 : 87, | ||
OP_8 : 88, | ||
OP_9 : 89, | ||
OP_10 : 90, | ||
OP_11 : 91, | ||
OP_12 : 92, | ||
OP_13 : 93, | ||
OP_14 : 94, | ||
OP_15 : 95, | ||
OP_16 : 96, | ||
OP_FALSE: 0, | ||
OP_0: 0, | ||
OP_PUSHDATA1: 76, | ||
OP_PUSHDATA2: 77, | ||
OP_PUSHDATA4: 78, | ||
OP_1NEGATE: 79, | ||
OP_RESERVED: 80, | ||
OP_1: 81, | ||
OP_TRUE: 81, | ||
OP_2: 82, | ||
OP_3: 83, | ||
OP_4: 84, | ||
OP_5: 85, | ||
OP_6: 86, | ||
OP_7: 87, | ||
OP_8: 88, | ||
OP_9: 89, | ||
OP_10: 90, | ||
OP_11: 91, | ||
OP_12: 92, | ||
OP_13: 93, | ||
OP_14: 94, | ||
OP_15: 95, | ||
OP_16: 96, | ||
// control | ||
OP_NOP : 97, | ||
OP_VER : 98, | ||
OP_IF : 99, | ||
OP_NOTIF : 100, | ||
OP_VERIF : 101, | ||
OP_VERNOTIF : 102, | ||
OP_ELSE : 103, | ||
OP_ENDIF : 104, | ||
OP_VERIFY : 105, | ||
OP_RETURN : 106, | ||
OP_NOP: 97, | ||
OP_VER: 98, | ||
OP_IF: 99, | ||
OP_NOTIF: 100, | ||
OP_VERIF: 101, | ||
OP_VERNOTIF: 102, | ||
OP_ELSE: 103, | ||
OP_ENDIF: 104, | ||
OP_VERIFY: 105, | ||
OP_RETURN: 106, | ||
// stack ops | ||
OP_TOALTSTACK : 107, | ||
OP_FROMALTSTACK : 108, | ||
OP_2DROP : 109, | ||
OP_2DUP : 110, | ||
OP_3DUP : 111, | ||
OP_2OVER : 112, | ||
OP_2ROT : 113, | ||
OP_2SWAP : 114, | ||
OP_IFDUP : 115, | ||
OP_DEPTH : 116, | ||
OP_DROP : 117, | ||
OP_DUP : 118, | ||
OP_NIP : 119, | ||
OP_OVER : 120, | ||
OP_PICK : 121, | ||
OP_ROLL : 122, | ||
OP_ROT : 123, | ||
OP_SWAP : 124, | ||
OP_TUCK : 125, | ||
OP_TOALTSTACK: 107, | ||
OP_FROMALTSTACK: 108, | ||
OP_2DROP: 109, | ||
OP_2DUP: 110, | ||
OP_3DUP: 111, | ||
OP_2OVER: 112, | ||
OP_2ROT: 113, | ||
OP_2SWAP: 114, | ||
OP_IFDUP: 115, | ||
OP_DEPTH: 116, | ||
OP_DROP: 117, | ||
OP_DUP: 118, | ||
OP_NIP: 119, | ||
OP_OVER: 120, | ||
OP_PICK: 121, | ||
OP_ROLL: 122, | ||
OP_ROT: 123, | ||
OP_SWAP: 124, | ||
OP_TUCK: 125, | ||
// splice ops | ||
OP_CAT : 126, | ||
OP_SUBSTR : 127, | ||
OP_LEFT : 128, | ||
OP_RIGHT : 129, | ||
OP_SIZE : 130, | ||
OP_CAT: 126, | ||
OP_SUBSTR: 127, | ||
OP_LEFT: 128, | ||
OP_RIGHT: 129, | ||
OP_SIZE: 130, | ||
// bit logic | ||
OP_INVERT : 131, | ||
OP_AND : 132, | ||
OP_OR : 133, | ||
OP_XOR : 134, | ||
OP_EQUAL : 135, | ||
OP_EQUALVERIFY : 136, | ||
OP_RESERVED1 : 137, | ||
OP_RESERVED2 : 138, | ||
OP_INVERT: 131, | ||
OP_AND: 132, | ||
OP_OR: 133, | ||
OP_XOR: 134, | ||
OP_EQUAL: 135, | ||
OP_EQUALVERIFY: 136, | ||
OP_RESERVED1: 137, | ||
OP_RESERVED2: 138, | ||
// numeric | ||
OP_1ADD : 139, | ||
OP_1SUB : 140, | ||
OP_2MUL : 141, | ||
OP_2DIV : 142, | ||
OP_NEGATE : 143, | ||
OP_ABS : 144, | ||
OP_NOT : 145, | ||
OP_0NOTEQUAL : 146, | ||
OP_1ADD: 139, | ||
OP_1SUB: 140, | ||
OP_2MUL: 141, | ||
OP_2DIV: 142, | ||
OP_NEGATE: 143, | ||
OP_ABS: 144, | ||
OP_NOT: 145, | ||
OP_0NOTEQUAL: 146, | ||
OP_ADD : 147, | ||
OP_SUB : 148, | ||
OP_MUL : 149, | ||
OP_DIV : 150, | ||
OP_MOD : 151, | ||
OP_LSHIFT : 152, | ||
OP_RSHIFT : 153, | ||
OP_ADD: 147, | ||
OP_SUB: 148, | ||
OP_MUL: 149, | ||
OP_DIV: 150, | ||
OP_MOD: 151, | ||
OP_LSHIFT: 152, | ||
OP_RSHIFT: 153, | ||
OP_BOOLAND : 154, | ||
OP_BOOLOR : 155, | ||
OP_NUMEQUAL : 156, | ||
OP_NUMEQUALVERIFY : 157, | ||
OP_NUMNOTEQUAL : 158, | ||
OP_LESSTHAN : 159, | ||
OP_GREATERTHAN : 160, | ||
OP_LESSTHANOREQUAL : 161, | ||
OP_GREATERTHANOREQUAL : 162, | ||
OP_MIN : 163, | ||
OP_MAX : 164, | ||
OP_BOOLAND: 154, | ||
OP_BOOLOR: 155, | ||
OP_NUMEQUAL: 156, | ||
OP_NUMEQUALVERIFY: 157, | ||
OP_NUMNOTEQUAL: 158, | ||
OP_LESSTHAN: 159, | ||
OP_GREATERTHAN: 160, | ||
OP_LESSTHANOREQUAL: 161, | ||
OP_GREATERTHANOREQUAL: 162, | ||
OP_MIN: 163, | ||
OP_MAX: 164, | ||
OP_WITHIN : 165, | ||
OP_WITHIN: 165, | ||
// crypto | ||
OP_RIPEMD160 : 166, | ||
OP_SHA1 : 167, | ||
OP_SHA256 : 168, | ||
OP_HASH160 : 169, | ||
OP_HASH256 : 170, | ||
OP_CODESEPARATOR : 171, | ||
OP_CHECKSIG : 172, | ||
OP_CHECKSIGVERIFY : 173, | ||
OP_CHECKMULTISIG : 174, | ||
OP_CHECKMULTISIGVERIFY : 175, | ||
OP_RIPEMD160: 166, | ||
OP_SHA1: 167, | ||
OP_SHA256: 168, | ||
OP_HASH160: 169, | ||
OP_HASH256: 170, | ||
OP_CODESEPARATOR: 171, | ||
OP_CHECKSIG: 172, | ||
OP_CHECKSIGVERIFY: 173, | ||
OP_CHECKMULTISIG: 174, | ||
OP_CHECKMULTISIGVERIFY: 175, | ||
// expansion | ||
OP_NOP1 : 176, | ||
OP_NOP2 : 177, | ||
OP_NOP3 : 178, | ||
OP_NOP4 : 179, | ||
OP_NOP5 : 180, | ||
OP_NOP6 : 181, | ||
OP_NOP7 : 182, | ||
OP_NOP8 : 183, | ||
OP_NOP9 : 184, | ||
OP_NOP10 : 185, | ||
OP_NOP1: 176, | ||
OP_NOP2: 177, | ||
OP_NOP3: 178, | ||
OP_NOP4: 179, | ||
OP_NOP5: 180, | ||
OP_NOP6: 181, | ||
OP_NOP7: 182, | ||
OP_NOP8: 183, | ||
OP_NOP9: 184, | ||
OP_NOP10: 185, | ||
// template matching params | ||
OP_PUBKEYHASH : 253, | ||
OP_PUBKEY : 254, | ||
OP_INVALIDOPCODE : 255 | ||
OP_PUBKEYHASH: 253, | ||
OP_PUBKEY: 254, | ||
OP_INVALIDOPCODE: 255 | ||
} |
@@ -7,3 +7,3 @@ var assert = require('assert') | ||
function Script(buffer, chunks) { | ||
function Script (buffer, chunks) { | ||
typeForce('Buffer', buffer) | ||
@@ -16,9 +16,10 @@ typeForce('Array', chunks) | ||
Script.fromASM = function(asm) { | ||
Script.fromASM = function (asm) { | ||
var strChunks = asm.split(' ') | ||
var chunks = strChunks.map(function(strChunk) { | ||
var chunks = strChunks.map(function (strChunk) { | ||
// opcode | ||
if (strChunk in opcodes) { | ||
return opcodes[strChunk] | ||
// data chunk | ||
} else { | ||
@@ -32,3 +33,3 @@ return new Buffer(strChunk, 'hex') | ||
Script.fromBuffer = function(buffer) { | ||
Script.fromBuffer = function (buffer) { | ||
var chunks = [] | ||
@@ -40,6 +41,13 @@ var i = 0 | ||
// data chunk | ||
if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) { | ||
var d = bufferutils.readPushDataInt(buffer, i) | ||
// did reading a pushDataInt fail? return non-chunked script | ||
if (d === null) return new Script(buffer, []) | ||
i += d.size | ||
// attempt to read too much data? | ||
if (i + d.number > buffer.length) return new Script(buffer, []) | ||
var data = buffer.slice(i, i + d.number) | ||
@@ -50,2 +58,3 @@ i += d.number | ||
// opcode | ||
} else { | ||
@@ -61,6 +70,7 @@ chunks.push(opcode) | ||
Script.fromChunks = function(chunks) { | ||
Script.fromChunks = function (chunks) { | ||
typeForce('Array', chunks) | ||
var bufferSize = chunks.reduce(function(accum, chunk) { | ||
var bufferSize = chunks.reduce(function (accum, chunk) { | ||
// data chunk | ||
if (Buffer.isBuffer(chunk)) { | ||
@@ -70,2 +80,3 @@ return accum + bufferutils.pushDataSize(chunk.length) + chunk.length | ||
// opcode | ||
return accum + 1 | ||
@@ -77,3 +88,4 @@ }, 0.0) | ||
chunks.forEach(function(chunk) { | ||
chunks.forEach(function (chunk) { | ||
// data chunk | ||
if (Buffer.isBuffer(chunk)) { | ||
@@ -85,2 +97,3 @@ offset += bufferutils.writePushDataInt(buffer, chunk.length, offset) | ||
// opcode | ||
} else { | ||
@@ -96,3 +109,3 @@ buffer.writeUInt8(chunk, offset) | ||
Script.fromHex = function(hex) { | ||
Script.fromHex = function (hex) { | ||
return Script.fromBuffer(new Buffer(hex, 'hex')) | ||
@@ -103,3 +116,3 @@ } | ||
Script.prototype.getHash = function() { | ||
Script.prototype.getHash = function () { | ||
return crypto.hash160(this.buffer) | ||
@@ -109,4 +122,4 @@ } | ||
// FIXME: doesn't work for data chunks, maybe time to use buffertools.compare... | ||
Script.prototype.without = function(needle) { | ||
return Script.fromChunks(this.chunks.filter(function(op) { | ||
Script.prototype.without = function (needle) { | ||
return Script.fromChunks(this.chunks.filter(function (op) { | ||
return op !== needle | ||
@@ -122,7 +135,9 @@ })) | ||
Script.prototype.toASM = function() { | ||
return this.chunks.map(function(chunk) { | ||
Script.prototype.toASM = function () { | ||
return this.chunks.map(function (chunk) { | ||
// data chunk | ||
if (Buffer.isBuffer(chunk)) { | ||
return chunk.toString('hex') | ||
// opcode | ||
} else { | ||
@@ -134,7 +149,7 @@ return reverseOps[chunk] | ||
Script.prototype.toBuffer = function() { | ||
Script.prototype.toBuffer = function () { | ||
return this.buffer | ||
} | ||
Script.prototype.toHex = function() { | ||
Script.prototype.toHex = function () { | ||
return this.toBuffer().toString('hex') | ||
@@ -141,0 +156,0 @@ } |
@@ -11,3 +11,3 @@ var assert = require('assert') | ||
function isCanonicalPubKey(buffer) { | ||
function isCanonicalPubKey (buffer) { | ||
if (!Buffer.isBuffer(buffer)) return false | ||
@@ -18,3 +18,4 @@ | ||
} catch (e) { | ||
if (!(e.message.match(/Invalid sequence (length|tag)/))) throw e | ||
if (!(e.message.match(/Invalid sequence (length|tag)/))) | ||
throw e | ||
@@ -27,3 +28,3 @@ return false | ||
function isCanonicalSignature(buffer) { | ||
function isCanonicalSignature (buffer) { | ||
if (!Buffer.isBuffer(buffer)) return false | ||
@@ -33,4 +34,6 @@ | ||
ECSignature.parseScriptSignature(buffer) | ||
} catch(e) { | ||
if (!(e.message.match(/Not a DER sequence|Invalid sequence length|Expected a DER integer|R length is zero|S length is zero|R value excessively padded|S value excessively padded|R value is negative|S value is negative|Invalid hashType/))) throw e | ||
} catch (e) { | ||
if (!(e.message.match(/Not a DER sequence|Invalid sequence length|Expected a DER integer|R length is zero|S length is zero|R value excessively padded|S value excessively padded|R value is negative|S value is negative|Invalid hashType/))) { | ||
throw e | ||
} | ||
@@ -43,3 +46,3 @@ return false | ||
function isPubKeyHashInput(script) { | ||
function isPubKeyHashInput (script) { | ||
return script.chunks.length === 2 && | ||
@@ -50,3 +53,3 @@ isCanonicalSignature(script.chunks[0]) && | ||
function isPubKeyHashOutput(script) { | ||
function isPubKeyHashOutput (script) { | ||
return script.chunks.length === 5 && | ||
@@ -61,3 +64,3 @@ script.chunks[0] === ops.OP_DUP && | ||
function isPubKeyInput(script) { | ||
function isPubKeyInput (script) { | ||
return script.chunks.length === 1 && | ||
@@ -67,3 +70,3 @@ isCanonicalSignature(script.chunks[0]) | ||
function isPubKeyOutput(script) { | ||
function isPubKeyOutput (script) { | ||
return script.chunks.length === 2 && | ||
@@ -74,3 +77,3 @@ isCanonicalPubKey(script.chunks[0]) && | ||
function isScriptHashInput(script, allowIncomplete) { | ||
function isScriptHashInput (script, allowIncomplete) { | ||
if (script.chunks.length < 2) return false | ||
@@ -82,14 +85,11 @@ | ||
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)) | ||
var scriptPubKey | ||
var redeemScript = Script.fromBuffer(lastChunk) | ||
try { | ||
scriptPubKey = Script.fromBuffer(lastChunk) | ||
} catch (e) { | ||
return false | ||
} | ||
// is redeemScript a valid script? | ||
if (redeemScript.chunks.length === 0) return false | ||
return classifyInput(scriptSig, allowIncomplete) === classifyOutput(scriptPubKey) | ||
return classifyInput(scriptSig, allowIncomplete) === classifyOutput(redeemScript) | ||
} | ||
function isScriptHashOutput(script) { | ||
function isScriptHashOutput (script) { | ||
return script.chunks.length === 3 && | ||
@@ -104,3 +104,3 @@ script.chunks[0] === ops.OP_HASH160 && | ||
// See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197 | ||
function isMultisigInput(script, allowIncomplete) { | ||
function isMultisigInput (script, allowIncomplete) { | ||
if (script.chunks.length < 2) return false | ||
@@ -110,3 +110,3 @@ if (script.chunks[0] !== ops.OP_0) return false | ||
if (allowIncomplete) { | ||
return script.chunks.slice(1).every(function(chunk) { | ||
return script.chunks.slice(1).every(function (chunk) { | ||
return chunk === ops.OP_0 || isCanonicalSignature(chunk) | ||
@@ -119,3 +119,3 @@ }) | ||
function isMultisigOutput(script) { | ||
function isMultisigOutput (script) { | ||
if (script.chunks.length < 4) return false | ||
@@ -144,7 +144,7 @@ if (script.chunks[script.chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false | ||
function isNullDataOutput(script) { | ||
function isNullDataOutput (script) { | ||
return script.chunks[0] === ops.OP_RETURN | ||
} | ||
function classifyOutput(script) { | ||
function classifyOutput (script) { | ||
typeForce('Script', script) | ||
@@ -167,3 +167,3 @@ | ||
function classifyInput(script, allowIncomplete) { | ||
function classifyInput (script, allowIncomplete) { | ||
typeForce('Script', script) | ||
@@ -186,3 +186,3 @@ | ||
// {pubKey} OP_CHECKSIG | ||
function pubKeyOutput(pubKey) { | ||
function pubKeyOutput (pubKey) { | ||
return Script.fromChunks([ | ||
@@ -195,3 +195,3 @@ pubKey.toBuffer(), | ||
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG | ||
function pubKeyHashOutput(hash) { | ||
function pubKeyHashOutput (hash) { | ||
typeForce('Buffer', hash) | ||
@@ -209,3 +209,3 @@ | ||
// OP_HASH160 {scriptHash} OP_EQUAL | ||
function scriptHashOutput(hash) { | ||
function scriptHashOutput (hash) { | ||
typeForce('Buffer', hash) | ||
@@ -221,3 +221,3 @@ | ||
// m [pubKeys ...] n OP_CHECKMULTISIG | ||
function multisigOutput(m, pubKeys) { | ||
function multisigOutput (m, pubKeys) { | ||
typeForce(['ECPubKey'], pubKeys) | ||
@@ -227,3 +227,3 @@ | ||
var pubKeyBuffers = pubKeys.map(function(pubKey) { | ||
var pubKeyBuffers = pubKeys.map(function (pubKey) { | ||
return pubKey.toBuffer() | ||
@@ -242,3 +242,3 @@ }) | ||
// {signature} | ||
function pubKeyInput(signature) { | ||
function pubKeyInput (signature) { | ||
typeForce('Buffer', signature) | ||
@@ -250,3 +250,3 @@ | ||
// {signature} {pubKey} | ||
function pubKeyHashInput(signature, pubKey) { | ||
function pubKeyHashInput (signature, pubKey) { | ||
typeForce('Buffer', signature) | ||
@@ -258,3 +258,3 @@ | ||
// <scriptSig> {serialized scriptPubKey script} | ||
function scriptHashInput(scriptSig, scriptPubKey) { | ||
function scriptHashInput (scriptSig, scriptPubKey) { | ||
return Script.fromChunks([].concat( | ||
@@ -267,3 +267,3 @@ scriptSig.chunks, | ||
// OP_0 [signatures ...] | ||
function multisigInput(signatures, scriptPubKey) { | ||
function multisigInput (signatures, scriptPubKey) { | ||
if (scriptPubKey) { | ||
@@ -277,9 +277,4 @@ assert(isMultisigOutput(scriptPubKey)) | ||
var count = 0 | ||
signatures.forEach(function(signature) { | ||
count += (signature !== ops.OP_0) | ||
}) | ||
assert(count >= m, 'Not enough signatures provided') | ||
assert(count <= n, 'Too many signatures provided') | ||
assert(signatures.length >= m, 'Not enough signatures provided') | ||
assert(signatures.length <= n, 'Too many signatures provided') | ||
} | ||
@@ -290,3 +285,3 @@ | ||
function nullDataOutput(data) { | ||
function nullDataOutput (data) { | ||
return Script.fromChunks([ops.OP_RETURN, data]) | ||
@@ -317,3 +312,3 @@ } | ||
multisigInput: multisigInput, | ||
dataOutput: function(data) { | ||
dataOutput: function (data) { | ||
console.warn('dataOutput is deprecated, use nullDataOutput by 2.0.0') | ||
@@ -320,0 +315,0 @@ return nullDataOutput(data) |
@@ -10,3 +10,3 @@ var assert = require('assert') | ||
function extractInput(txIn) { | ||
function extractInput (txIn) { | ||
var redeemScript | ||
@@ -25,3 +25,2 @@ var scriptSig = txIn.script | ||
scriptType = scripts.classifyInput(scriptSig, true) | ||
} else { | ||
@@ -58,3 +57,3 @@ scriptType = prevOutType | ||
case 'multisig': { | ||
signatures = scriptSig.chunks.slice(1).map(function(chunk) { | ||
signatures = scriptSig.chunks.slice(1).map(function (chunk) { | ||
if (chunk === ops.OP_0) return chunk | ||
@@ -87,3 +86,3 @@ | ||
function TransactionBuilder() { | ||
function TransactionBuilder () { | ||
this.prevTxMap = {} | ||
@@ -97,3 +96,3 @@ this.prevOutScripts = {} | ||
TransactionBuilder.fromTransaction = function(transaction) { | ||
TransactionBuilder.fromTransaction = function (transaction) { | ||
var txb = new TransactionBuilder() | ||
@@ -106,3 +105,3 @@ | ||
// Extract/add inputs | ||
transaction.ins.forEach(function(txIn) { | ||
transaction.ins.forEach(function (txIn) { | ||
txb.addInput(txIn.hash, txIn.index, txIn.sequence) | ||
@@ -112,3 +111,3 @@ }) | ||
// Extract/add outputs | ||
transaction.outs.forEach(function(txOut) { | ||
transaction.outs.forEach(function (txOut) { | ||
txb.addOutput(txOut.script, txOut.value) | ||
@@ -118,3 +117,3 @@ }) | ||
// Extract/add signatures | ||
txb.inputs = transaction.ins.map(function(txIn) { | ||
txb.inputs = transaction.ins.map(function (txIn) { | ||
// TODO: remove me after testcase added | ||
@@ -132,5 +131,6 @@ assert(!Transaction.isCoinbaseHash(txIn.hash), 'coinbase inputs not supported') | ||
TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) { | ||
TransactionBuilder.prototype.addInput = function (prevTx, index, sequence, prevOutScript) { | ||
var prevOutHash | ||
// txId | ||
if (typeof prevTx === 'string') { | ||
@@ -142,2 +142,3 @@ prevOutHash = new Buffer(prevTx, 'hex') | ||
// Transaction | ||
} else if (prevTx instanceof Transaction) { | ||
@@ -147,5 +148,5 @@ prevOutHash = prevTx.getHash() | ||
// txHash | ||
} else { | ||
prevOutHash = prevTx | ||
} | ||
@@ -159,9 +160,11 @@ | ||
switch (prevOutType) { | ||
case 'multisig': | ||
case 'multisig': { | ||
input.pubKeys = prevOutScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) | ||
break | ||
} | ||
case 'pubkey': | ||
case 'pubkey': { | ||
input.pubKeys = prevOutScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) | ||
break | ||
} | ||
} | ||
@@ -177,3 +180,3 @@ | ||
assert(this.inputs.every(function(input2) { | ||
assert(this.inputs.every(function (input2) { | ||
if (input2.hashType === undefined) return true | ||
@@ -194,4 +197,4 @@ | ||
TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) { | ||
assert(this.inputs.every(function(input) { | ||
TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) { | ||
assert(this.inputs.every(function (input) { | ||
if (input.hashType === undefined) return true | ||
@@ -205,8 +208,16 @@ | ||
TransactionBuilder.prototype.build = function() { return this.__build(false) } | ||
TransactionBuilder.prototype.buildIncomplete = function() { return this.__build(true) } | ||
TransactionBuilder.prototype.build = function () { | ||
return this.__build(false) | ||
} | ||
TransactionBuilder.prototype.buildIncomplete = function () { | ||
return this.__build(true) | ||
} | ||
var canSignTypes = { 'pubkeyhash': true, 'multisig': true, 'pubkey': true } | ||
var canSignTypes = { | ||
'pubkeyhash': true, | ||
'multisig': true, | ||
'pubkey': true | ||
} | ||
TransactionBuilder.prototype.__build = function(allowIncomplete) { | ||
TransactionBuilder.prototype.__build = function (allowIncomplete) { | ||
if (!allowIncomplete) { | ||
@@ -220,3 +231,3 @@ assert(this.tx.ins.length > 0, 'Transaction has no inputs') | ||
// Create script signatures from signature meta-data | ||
this.inputs.forEach(function(input, index) { | ||
this.inputs.forEach(function (input, index) { | ||
var scriptType = input.scriptType | ||
@@ -233,10 +244,11 @@ var scriptSig | ||
switch (scriptType) { | ||
case 'pubkeyhash': | ||
case 'pubkeyhash': { | ||
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) | ||
scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) | ||
break | ||
} | ||
case 'multisig': | ||
case 'multisig': { | ||
// Array.prototype.map is sparse-compatible | ||
var msSignatures = input.signatures.map(function(signature) { | ||
var msSignatures = input.signatures.map(function (signature) { | ||
return signature.toScriptSignature(input.hashType) | ||
@@ -246,6 +258,11 @@ }) | ||
// fill in blanks with OP_0 | ||
for (var i = 0; i < msSignatures.length; ++i) { | ||
if (msSignatures[i]) continue | ||
if (allowIncomplete) { | ||
for (var i = 0; i < msSignatures.length; ++i) { | ||
if (msSignatures[i]) continue | ||
msSignatures[i] = ops.OP_0 | ||
msSignatures[i] = ops.OP_0 | ||
} | ||
} else { | ||
// Array.prototype.filter returns non-sparse array | ||
msSignatures = msSignatures.filter(function (x) { return x }) | ||
} | ||
@@ -256,7 +273,9 @@ | ||
break | ||
} | ||
case 'pubkey': | ||
case 'pubkey': { | ||
var pkSignature = input.signatures[0].toScriptSignature(input.hashType) | ||
scriptSig = scripts.pubKeyInput(pkSignature) | ||
break | ||
} | ||
} | ||
@@ -279,3 +298,3 @@ } | ||
TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { | ||
TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hashType) { | ||
assert(index in this.inputs, 'No input at index: ' + index) | ||
@@ -286,7 +305,7 @@ hashType = hashType || Transaction.SIGHASH_ALL | ||
var canSign = input.hashType && | ||
input.prevOutScript && | ||
input.prevOutType && | ||
input.pubKeys && | ||
input.scriptType && | ||
input.signatures | ||
input.prevOutScript && | ||
input.prevOutType && | ||
input.pubKeys && | ||
input.scriptType && | ||
input.signatures | ||
@@ -304,2 +323,3 @@ // are we almost ready to sign? | ||
} else { | ||
// must be pay-to-scriptHash? | ||
if (redeemScript) { | ||
@@ -319,7 +339,8 @@ // if we have a prevOutScript, enforce scriptHash equality to the redeemScript | ||
switch (scriptType) { | ||
case 'multisig': | ||
case 'multisig': { | ||
pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) | ||
break | ||
} | ||
case 'pubkeyhash': | ||
case 'pubkeyhash': { | ||
var pkh1 = redeemScript.chunks[2] | ||
@@ -331,6 +352,8 @@ var pkh2 = privKey.pub.getAddress().hash | ||
break | ||
} | ||
case 'pubkey': | ||
case 'pubkey': { | ||
pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) | ||
break | ||
} | ||
} | ||
@@ -347,6 +370,7 @@ | ||
// cannot be pay-to-scriptHash | ||
} else { | ||
assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript') | ||
// can we sign this? | ||
// can we otherwise sign this? | ||
if (input.scriptType) { | ||
@@ -361,3 +385,2 @@ assert(input.pubKeys, input.scriptType + ' not supported') | ||
input.scriptType = input.prevOutType | ||
} | ||
@@ -371,3 +394,3 @@ } | ||
// enforce in order signing of public keys | ||
assert(input.pubKeys.some(function(pubKey, i) { | ||
assert(input.pubKeys.some(function (pubKey, i) { | ||
if (!privKey.pub.Q.equals(pubKey.Q)) return false | ||
@@ -374,0 +397,0 @@ |
@@ -12,3 +12,3 @@ var assert = require('assert') | ||
function Transaction() { | ||
function Transaction () { | ||
this.version = 1 | ||
@@ -26,5 +26,5 @@ this.locktime = 0 | ||
Transaction.fromBuffer = function(buffer, __disableAssert) { | ||
Transaction.fromBuffer = function (buffer, __disableAssert) { | ||
var offset = 0 | ||
function readSlice(n) { | ||
function readSlice (n) { | ||
offset += n | ||
@@ -34,3 +34,3 @@ return buffer.slice(offset - n, offset) | ||
function readUInt32() { | ||
function readUInt32 () { | ||
var i = buffer.readUInt32LE(offset) | ||
@@ -41,3 +41,3 @@ offset += 4 | ||
function readUInt64() { | ||
function readUInt64 () { | ||
var i = bufferutils.readUInt64LE(buffer, offset) | ||
@@ -48,3 +48,3 @@ offset += 8 | ||
function readVarInt() { | ||
function readVarInt () { | ||
var vi = bufferutils.readVarInt(buffer, offset) | ||
@@ -55,7 +55,7 @@ offset += vi.size | ||
function readScript() { | ||
function readScript () { | ||
return Script.fromBuffer(readSlice(readVarInt())) | ||
} | ||
function readGenerationScript() { | ||
function readGenerationScript () { | ||
return new Script(readSlice(readVarInt()), []) | ||
@@ -78,3 +78,2 @@ } | ||
}) | ||
} else { | ||
@@ -107,8 +106,8 @@ tx.ins.push({ | ||
Transaction.fromHex = function(hex) { | ||
Transaction.fromHex = function (hex) { | ||
return Transaction.fromBuffer(new Buffer(hex, 'hex')) | ||
} | ||
Transaction.isCoinbaseHash = function(buffer) { | ||
return Array.prototype.every.call(buffer, function(x) { | ||
Transaction.isCoinbaseHash = function (buffer) { | ||
return Array.prototype.every.call(buffer, function (x) { | ||
return x === 0 | ||
@@ -128,3 +127,3 @@ }) | ||
*/ | ||
Transaction.prototype.addInput = function(hash, index, sequence, script) { | ||
Transaction.prototype.addInput = function (hash, index, sequence, script) { | ||
if (sequence === undefined || sequence === null) { | ||
@@ -139,6 +138,4 @@ sequence = Transaction.DEFAULT_SEQUENCE | ||
hash = bufferutils.reverse(new Buffer(hash, 'hex')) | ||
} else if (hash instanceof Transaction) { | ||
hash = hash.getHash() | ||
} | ||
@@ -171,3 +168,3 @@ | ||
*/ | ||
Transaction.prototype.addOutput = function(scriptPubKey, value) { | ||
Transaction.prototype.addOutput = function (scriptPubKey, value) { | ||
// Attempt to get a valid address if it's a base58 address string | ||
@@ -198,3 +195,3 @@ if (typeof scriptPubKey === 'string') { | ||
newTx.ins = this.ins.map(function(txIn) { | ||
newTx.ins = this.ins.map(function (txIn) { | ||
return { | ||
@@ -208,3 +205,3 @@ hash: txIn.hash, | ||
newTx.outs = this.outs.map(function(txOut) { | ||
newTx.outs = this.outs.map(function (txOut) { | ||
return { | ||
@@ -227,3 +224,3 @@ script: txOut.script, | ||
*/ | ||
Transaction.prototype.hashForSignature = function(inIndex, prevOutScript, hashType) { | ||
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) { | ||
// FIXME: remove in 2.x.y | ||
@@ -250,3 +247,3 @@ if (arguments[0] instanceof Script) { | ||
// Blank out other inputs' signatures | ||
txTmp.ins.forEach(function(txIn) { | ||
txTmp.ins.forEach(function (txIn) { | ||
txIn.script = Script.EMPTY | ||
@@ -257,8 +254,7 @@ }) | ||
var hashTypeModifier = hashType & 0x1f | ||
if (hashTypeModifier === Transaction.SIGHASH_NONE) { | ||
assert(false, 'SIGHASH_NONE not yet supported') | ||
} else if (hashTypeModifier === Transaction.SIGHASH_SINGLE) { | ||
assert(false, 'SIGHASH_SINGLE not yet supported') | ||
} | ||
@@ -287,3 +283,3 @@ | ||
Transaction.prototype.toBuffer = function () { | ||
function scriptSize(script) { | ||
function scriptSize (script) { | ||
var length = script.buffer.length | ||
@@ -298,8 +294,8 @@ | ||
bufferutils.varIntSize(this.outs.length) + | ||
this.ins.reduce(function(sum, input) { return sum + 40 + scriptSize(input.script) }, 0) + | ||
this.outs.reduce(function(sum, output) { return sum + 8 + scriptSize(output.script) }, 0) | ||
this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) + | ||
this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0) | ||
) | ||
var offset = 0 | ||
function writeSlice(slice) { | ||
function writeSlice (slice) { | ||
slice.copy(buffer, offset) | ||
@@ -309,3 +305,3 @@ offset += slice.length | ||
function writeUInt32(i) { | ||
function writeUInt32 (i) { | ||
buffer.writeUInt32LE(i, offset) | ||
@@ -315,3 +311,3 @@ offset += 4 | ||
function writeUInt64(i) { | ||
function writeUInt64 (i) { | ||
bufferutils.writeUInt64LE(buffer, i, offset) | ||
@@ -321,3 +317,3 @@ offset += 8 | ||
function writeVarInt(i) { | ||
function writeVarInt (i) { | ||
var n = bufferutils.writeVarInt(buffer, i, offset) | ||
@@ -330,3 +326,3 @@ offset += n | ||
this.ins.forEach(function(txIn) { | ||
this.ins.forEach(function (txIn) { | ||
writeSlice(txIn.hash) | ||
@@ -340,3 +336,3 @@ writeUInt32(txIn.index) | ||
writeVarInt(this.outs.length) | ||
this.outs.forEach(function(txOut) { | ||
this.outs.forEach(function (txOut) { | ||
writeUInt64(txOut.value) | ||
@@ -352,7 +348,7 @@ writeVarInt(txOut.script.buffer.length) | ||
Transaction.prototype.toHex = function() { | ||
Transaction.prototype.toHex = function () { | ||
return this.toBuffer().toString('hex') | ||
} | ||
Transaction.prototype.setInputScript = function(index, script) { | ||
Transaction.prototype.setInputScript = function (index, script) { | ||
typeForce('Number', index) | ||
@@ -365,4 +361,4 @@ typeForce('Script', script) | ||
// FIXME: remove in 2.x.y | ||
Transaction.prototype.sign = function(index, privKey, hashType) { | ||
console.warn("Transaction.prototype.sign is deprecated. Use TransactionBuilder instead.") | ||
Transaction.prototype.sign = function (index, privKey, hashType) { | ||
console.warn('Transaction.prototype.sign is deprecated. Use TransactionBuilder instead.') | ||
@@ -377,4 +373,4 @@ var prevOutScript = privKey.pub.getAddress().toOutputScript() | ||
// FIXME: remove in 2.x.y | ||
Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) { | ||
console.warn("Transaction.prototype.signInput is deprecated. Use TransactionBuilder instead.") | ||
Transaction.prototype.signInput = function (index, prevOutScript, privKey, hashType) { | ||
console.warn('Transaction.prototype.signInput is deprecated. Use TransactionBuilder instead.') | ||
@@ -390,4 +386,4 @@ hashType = hashType || Transaction.SIGHASH_ALL | ||
// FIXME: remove in 2.x.y | ||
Transaction.prototype.validateInput = function(index, prevOutScript, pubKey, buffer) { | ||
console.warn("Transaction.prototype.validateInput is deprecated. Use TransactionBuilder instead.") | ||
Transaction.prototype.validateInput = function (index, prevOutScript, pubKey, buffer) { | ||
console.warn('Transaction.prototype.validateInput is deprecated. Use TransactionBuilder instead.') | ||
@@ -394,0 +390,0 @@ var parsed = ECSignature.parseScriptSignature(buffer) |
@@ -12,3 +12,3 @@ var assert = require('assert') | ||
function Wallet(seed, network) { | ||
function Wallet (seed, network) { | ||
console.warn('Wallet is deprecated and will be removed in 2.0.0, see #296') | ||
@@ -38,3 +38,3 @@ | ||
var me = this | ||
this.newMasterKey = function(seed) { | ||
this.newMasterKey = function (seed) { | ||
console.warn('newMasterKey is deprecated, please make a new Wallet instance instead') | ||
@@ -56,9 +56,17 @@ | ||
this.getMasterKey = function() { return masterKey } | ||
this.getAccountZero = function() { return accountZero } | ||
this.getExternalAccount = function() { return externalAccount } | ||
this.getInternalAccount = function() { return internalAccount } | ||
this.getMasterKey = function () { | ||
return masterKey | ||
} | ||
this.getAccountZero = function () { | ||
return accountZero | ||
} | ||
this.getExternalAccount = function () { | ||
return externalAccount | ||
} | ||
this.getInternalAccount = function () { | ||
return internalAccount | ||
} | ||
} | ||
Wallet.prototype.createTransaction = function(to, value, options) { | ||
Wallet.prototype.createTransaction = function (to, value, options) { | ||
// FIXME: remove in 2.0.0 | ||
@@ -85,7 +93,7 @@ if (typeof options !== 'object') { | ||
// filter by minConf, then pending and sort by descending value | ||
var unspents = this.unspents.filter(function(unspent) { | ||
var unspents = this.unspents.filter(function (unspent) { | ||
return unspent.confirmations >= minConf | ||
}).filter(function(unspent) { | ||
}).filter(function (unspent) { | ||
return !unspent.pending | ||
}).sort(function(o1, o2) { | ||
}).sort(function (o1, o2) { | ||
return o2.value - o1.value | ||
@@ -129,3 +137,3 @@ }) | ||
// FIXME: remove in 2.0.0 | ||
Wallet.prototype.processPendingTx = function(tx){ | ||
Wallet.prototype.processPendingTx = function (tx) { | ||
this.__processTx(tx, true) | ||
@@ -135,3 +143,3 @@ } | ||
// FIXME: remove in 2.0.0 | ||
Wallet.prototype.processConfirmedTx = function(tx){ | ||
Wallet.prototype.processConfirmedTx = function (tx) { | ||
this.__processTx(tx, false) | ||
@@ -141,3 +149,3 @@ } | ||
// FIXME: remove in 2.0.0 | ||
Wallet.prototype.__processTx = function(tx, isPending) { | ||
Wallet.prototype.__processTx = function (tx, isPending) { | ||
console.warn('processTransaction is considered harmful, see issue #260 for more information') | ||
@@ -148,3 +156,3 @@ | ||
tx.outs.forEach(function(txOut, i) { | ||
tx.outs.forEach(function (txOut, i) { | ||
var address | ||
@@ -154,4 +162,5 @@ | ||
address = Address.fromOutputScript(txOut.script, this.network).toString() | ||
} catch(e) { | ||
if (!(e.message.match(/has no matching Address/))) throw e | ||
} catch (e) { | ||
if (!(e.message.match(/has no matching Address/))) | ||
throw e | ||
} | ||
@@ -180,3 +189,3 @@ | ||
tx.ins.forEach(function(txIn, i) { | ||
tx.ins.forEach(function (txIn) { | ||
// copy and convert to big-endian hex | ||
@@ -193,7 +202,6 @@ var txInId = bufferutils.reverse(txIn.hash).toString('hex') | ||
unspent.spent = true | ||
} else { | ||
delete this.unspentMap[lookup] | ||
this.unspents = this.unspents.filter(function(unspent2) { | ||
this.unspents = this.unspents.filter(function (unspent2) { | ||
return unspent !== unspent2 | ||
@@ -205,3 +213,3 @@ }) | ||
Wallet.prototype.generateAddress = function() { | ||
Wallet.prototype.generateAddress = function () { | ||
var k = this.addresses.length | ||
@@ -215,3 +223,3 @@ var address = this.getExternalAccount().derive(k).getAddress() | ||
Wallet.prototype.generateChangeAddress = function() { | ||
Wallet.prototype.generateChangeAddress = function () { | ||
var k = this.changeAddresses.length | ||
@@ -225,3 +233,3 @@ var address = this.getInternalAccount().derive(k).getAddress() | ||
Wallet.prototype.getAddress = function() { | ||
Wallet.prototype.getAddress = function () { | ||
if (this.addresses.length === 0) { | ||
@@ -234,12 +242,12 @@ this.generateAddress() | ||
Wallet.prototype.getBalance = function(minConf) { | ||
Wallet.prototype.getBalance = function (minConf) { | ||
minConf = minConf || 0 | ||
return this.unspents.filter(function(unspent) { | ||
return this.unspents.filter(function (unspent) { | ||
return unspent.confirmations >= minConf | ||
// FIXME: remove spent filter in 2.0.0 | ||
}).filter(function(unspent) { | ||
// FIXME: remove spent filter in 2.0.0 | ||
}).filter(function (unspent) { | ||
return !unspent.spent | ||
}).reduce(function(accum, unspent) { | ||
}).reduce(function (accum, unspent) { | ||
return accum + unspent.value | ||
@@ -249,3 +257,3 @@ }, 0) | ||
Wallet.prototype.getChangeAddress = function() { | ||
Wallet.prototype.getChangeAddress = function () { | ||
if (this.changeAddresses.length === 0) { | ||
@@ -258,11 +266,11 @@ this.generateChangeAddress() | ||
Wallet.prototype.getInternalPrivateKey = function(index) { | ||
Wallet.prototype.getInternalPrivateKey = function (index) { | ||
return this.getInternalAccount().derive(index).privKey | ||
} | ||
Wallet.prototype.getPrivateKey = function(index) { | ||
Wallet.prototype.getPrivateKey = function (index) { | ||
return this.getExternalAccount().derive(index).privKey | ||
} | ||
Wallet.prototype.getPrivateKeyForAddress = function(address) { | ||
Wallet.prototype.getPrivateKeyForAddress = function (address) { | ||
var index | ||
@@ -281,12 +289,12 @@ | ||
Wallet.prototype.getUnspentOutputs = function(minConf) { | ||
Wallet.prototype.getUnspentOutputs = function (minConf) { | ||
minConf = minConf || 0 | ||
return this.unspents.filter(function(unspent) { | ||
return this.unspents.filter(function (unspent) { | ||
return unspent.confirmations >= minConf | ||
// FIXME: remove spent filter in 2.0.0 | ||
}).filter(function(unspent) { | ||
// FIXME: remove spent filter in 2.0.0 | ||
}).filter(function (unspent) { | ||
return !unspent.spent | ||
}).map(function(unspent) { | ||
}).map(function (unspent) { | ||
return { | ||
@@ -306,5 +314,5 @@ address: unspent.address, | ||
Wallet.prototype.setUnspentOutputs = function(unspents) { | ||
Wallet.prototype.setUnspentOutputs = function (unspents) { | ||
this.unspentMap = {} | ||
this.unspents = unspents.map(function(unspent) { | ||
this.unspents = unspents.map(function (unspent) { | ||
// FIXME: remove unspent.hash in 2.0.0 | ||
@@ -330,3 +338,5 @@ var txId = unspent.txId || unspent.hash | ||
assert.equal(txId.length, 64, 'Expected valid txId, got ' + txId) | ||
assert.doesNotThrow(function() { Address.fromBase58Check(unspent.address) }, 'Expected Base58 Address, got ' + unspent.address) | ||
assert.doesNotThrow(function () { | ||
Address.fromBase58Check(unspent.address) | ||
}, 'Expected Base58 Address, got ' + unspent.address) | ||
assert(isFinite(index), 'Expected finite index, got ' + index) | ||
@@ -360,4 +370,4 @@ | ||
Wallet.prototype.signWith = function(tx, addresses) { | ||
addresses.forEach(function(address, i) { | ||
Wallet.prototype.signWith = function (tx, addresses) { | ||
addresses.forEach(function (address, i) { | ||
var privKey = this.getPrivateKeyForAddress(address) | ||
@@ -371,3 +381,3 @@ | ||
function estimatePaddedFee(tx, network) { | ||
function estimatePaddedFee (tx, network) { | ||
var tmpTx = tx.clone() | ||
@@ -374,0 +384,0 @@ tmpTx.addOutput(Script.EMPTY, network.dustSoftThreshold || 0) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91631
2532