Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions src/base58check.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,24 @@ function varIntBuffer (i) {
return buffer
}

function equal (a, b) {
if (a.length !== b.length) return false

for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false
}

return true
}

function reverse (buffer) {
var buffer2 = new Buffer(buffer)
Array.prototype.reverse.call(buffer2)
return buffer2
}

module.exports = {
equal: equal,
pushDataSize: pushDataSize,
readPushDataInt: readPushDataInt,
readUInt64LE: readUInt64LE,
Expand Down
19 changes: 3 additions & 16 deletions src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,10 @@ function sha256 (buffer) {
return crypto.createHash('sha256').update(buffer).digest()
}

// FIXME: Name not consistent with others
function HmacSHA256 (buffer, secret) {
console.warn('Hmac* functions are deprecated for removal in 2.0.0, use node crypto instead')
return crypto.createHmac('sha256', secret).update(buffer).digest()
}

function HmacSHA512 (buffer, secret) {
console.warn('Hmac* functions are deprecated for removal in 2.0.0, use node crypto instead')
return crypto.createHmac('sha512', secret).update(buffer).digest()
}

module.exports = {
ripemd160: ripemd160,
sha1: sha1,
sha256: sha256,
hash160: hash160,
hash256: hash256,
HmacSHA256: HmacSHA256,
HmacSHA512: HmacSHA512
ripemd160: ripemd160,
sha1: sha1,
sha256: sha256
}
29 changes: 1 addition & 28 deletions src/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,7 @@ var ONE = new Buffer([1])
function deterministicGenerateK (curve, hash, d, checkSig) {
typeForce('Buffer', hash)
typeForce('BigInteger', d)

// FIXME: remove/uncomment for 2.0.0
// typeForce('Function', checkSig)

if (typeof checkSig !== 'function') {
console.warn('deterministicGenerateK requires a checkSig callback in 2.0.0, see #337 for more information')

checkSig = function (k) {
var G = curve.G
var n = curve.n
var e = BigInteger.fromBuffer(hash)

var Q = G.multiply(k)

if (curve.isInfinity(Q))
return false

var r = Q.affineX.mod(n)
if (r.signum() === 0)
return false

var s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n)
if (s.signum() === 0)
return false

return true
}
}
typeForce('Function', checkSig)

// sanity check
assert.equal(hash.length, 32, 'Hash must be 256 bit')
Expand Down
52 changes: 9 additions & 43 deletions src/hdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ HDNode.fromSeedHex = function (hex, network) {
}

HDNode.fromBase58 = function (string, network) {
return HDNode.fromBuffer(base58check.decode(string), network, true)
}

// FIXME: remove in 2.x.y
HDNode.fromBuffer = function (buffer, network, __ignoreDeprecation) {
if (!__ignoreDeprecation) {
console.warn('HDNode.fromBuffer() is deprecated for removal in 2.x.y, use fromBase58 instead')
}

var buffer = base58check.decode(string)
assert.strictEqual(buffer.length, HDNode.LENGTH, 'Invalid buffer length')

// 4 byte: version bytes
Expand Down Expand Up @@ -145,11 +137,6 @@ HDNode.fromBuffer = function (buffer, network, __ignoreDeprecation) {
return hd
}

// FIXME: remove in 2.x.y
HDNode.fromHex = function (hex, network) {
return HDNode.fromBuffer(new Buffer(hex, 'hex'), network)
}

HDNode.prototype.getIdentifier = function () {
return bcrypto.hash160(this.pubKey.toBuffer())
}
Expand All @@ -171,26 +158,11 @@ HDNode.prototype.neutered = function () {
return neutered
}

HDNode.prototype.toBase58 = function (isPrivate) {
return base58check.encode(this.toBuffer(isPrivate, true))
}

// FIXME: remove in 2.x.y
HDNode.prototype.toBuffer = function (isPrivate, __ignoreDeprecation) {
if (isPrivate === undefined) {
isPrivate = !!this.privKey

// FIXME: remove in 2.x.y
} else {
console.warn('isPrivate flag is deprecated, please use the .neutered() method instead')
}

if (!__ignoreDeprecation) {
console.warn('HDNode.toBuffer() is deprecated for removal in 2.x.y, use toBase58 instead')
}
HDNode.prototype.toBase58 = function (__isPrivate) {
assert.strictEqual(__isPrivate, undefined, 'Unsupported argument in 2.0.0')

// Version
var version = isPrivate ? this.network.bip32.private : this.network.bip32.public
var version = this.privKey ? this.network.bip32.private : this.network.bip32.public
var buffer = new Buffer(HDNode.LENGTH)

// 4 bytes: version bytes
Expand All @@ -210,25 +182,19 @@ HDNode.prototype.toBuffer = function (isPrivate, __ignoreDeprecation) {
// 32 bytes: the chain code
this.chainCode.copy(buffer, 13)

// 33 bytes: the public key or private key data
if (isPrivate) {
// FIXME: remove in 2.x.y
assert(this.privKey, 'Missing private key')

// 33 bytes: the private key, or
if (this.privKey) {
// 0x00 + k for private keys
buffer.writeUInt8(0, 45)
this.privKey.d.toBuffer(32).copy(buffer, 46)

// 33 bytes: the public key
} else {
// X9.62 encoding for public keys
this.pubKey.toBuffer().copy(buffer, 45)
}

return buffer
}

// FIXME: remove in 2.x.y
HDNode.prototype.toHex = function (isPrivate) {
return this.toBuffer(isPrivate).toString('hex')
return base58check.encode(buffer)
}

// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
Expand Down
6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
module.exports = {
Address: require('./address'),
base58check: require('./base58check'),
Block: require('./block'),
bufferutils: require('./bufferutils'),
crypto: require('./crypto'),
ecdsa: require('./ecdsa'),
ECKey: require('./eckey'),
ECPubKey: require('./ecpubkey'),
ECSignature: require('./ecsignature'),
Message: require('./message'),
message: require('./message'),
opcodes: require('./opcodes'),
HDNode: require('./hdnode'),
Script: require('./script'),
scripts: require('./scripts'),
Transaction: require('./transaction'),
TransactionBuilder: require('./transaction_builder'),
networks: require('./networks'),
Wallet: require('./wallet')
networks: require('./networks')
}
4 changes: 0 additions & 4 deletions src/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,5 @@ module.exports = {
pubKeyHashInput: pubKeyHashInput,
scriptHashInput: scriptHashInput,
multisigInput: multisigInput,
dataOutput: function (data) {
console.warn('dataOutput is deprecated, use nullDataOutput by 2.0.0')
return nullDataOutput(data)
},
nullDataOutput: nullDataOutput
}
90 changes: 4 additions & 86 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var typeForce = require('typeforce')
var opcodes = require('./opcodes')
var scripts = require('./scripts')

var Address = require('./address')
var ECSignature = require('./ecsignature')
var Script = require('./script')

function Transaction () {
Expand Down Expand Up @@ -106,30 +103,13 @@ Transaction.isCoinbaseHash = function (buffer) {
})
}

/**
* Create a new txIn.
*
* Can be called with any of:
*
* - A transaction and an index
* - A transaction hash and an index
*
* Note that this method does not sign the created input.
*/
Transaction.prototype.addInput = function (hash, index, sequence, script) {
if (sequence === undefined || sequence === null) {
sequence = Transaction.DEFAULT_SEQUENCE
}

script = script || Script.EMPTY

if (typeof hash === 'string') {
// TxId hex is big-endian, we need little-endian
hash = bufferutils.reverse(new Buffer(hash, 'hex'))
} else if (hash instanceof Transaction) {
hash = hash.getHash()
}

typeForce('Buffer', hash)
typeForce('Number', index)
typeForce('Number', sequence)
Expand All @@ -146,26 +126,7 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) {
}) - 1)
}

/**
* Create a new txOut.
*
* Can be called with:
*
* - A base58 address string and a value
* - An Address object and a value
* - A scriptPubKey Script and a value
*/
Transaction.prototype.addOutput = function (scriptPubKey, value) {
// Attempt to get a valid address if it's a base58 address string
if (typeof scriptPubKey === 'string') {
scriptPubKey = Address.fromBase58Check(scriptPubKey)
}

// Attempt to get a valid script if it's an Address object
if (scriptPubKey instanceof Address) {
scriptPubKey = scriptPubKey.toOutputScript()
}

typeForce('Script', scriptPubKey)
typeForce('Number', value)

Expand Down Expand Up @@ -203,22 +164,12 @@ Transaction.prototype.clone = function () {
/**
* Hash transaction for signing a specific input.
*
* Bitcoin uses a different hash for each signed transaction input. This
* method copies the transaction, makes the necessary changes based on the
* hashType, serializes and finally hashes the result. This hash can then be
* used to sign the transaction input in question.
* Bitcoin uses a different hash for each signed transaction input.
* This method copies the transaction, makes the necessary changes based on the
* hashType, and then hashes the result.
* This hash can then be used to sign the provided transaction input.
*/
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
// FIXME: remove in 2.x.y
if (arguments[0] instanceof Script) {
console.warn('hashForSignature(prevOutScript, inIndex, ...) has been deprecated. Use hashForSignature(inIndex, prevOutScript, ...)')

// swap the arguments (must be stored in tmp, arguments is special)
var tmp = arguments[0]
inIndex = arguments[1]
prevOutScript = tmp
}

typeForce('Number', inIndex)
typeForce('Script', prevOutScript)
typeForce('Number', hashType)
Expand Down Expand Up @@ -333,37 +284,4 @@ Transaction.prototype.setInputScript = function (index, script) {
this.ins[index].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.')

var prevOutScript = privKey.pub.getAddress().toOutputScript()
var signature = this.signInput(index, prevOutScript, privKey, hashType)

var scriptSig = scripts.pubKeyHashInput(signature, privKey.pub)
this.setInputScript(index, scriptSig)
}

// FIXME: remove in 2.x.y
Transaction.prototype.signInput = function (index, prevOutScript, privKey, hashType) {
console.warn('Transaction.prototype.signInput is deprecated. Use TransactionBuilder instead.')

hashType = hashType || Transaction.SIGHASH_ALL

var hash = this.hashForSignature(index, prevOutScript, hashType)
var signature = privKey.sign(hash)

return signature.toScriptSignature(hashType)
}

// FIXME: remove in 2.x.y
Transaction.prototype.validateInput = function (index, prevOutScript, pubKey, buffer) {
console.warn('Transaction.prototype.validateInput is deprecated. Use TransactionBuilder instead.')

var parsed = ECSignature.parseScriptSignature(buffer)
var hash = this.hashForSignature(index, prevOutScript, parsed.hashType)

return pubKey.verify(hash, parsed.signature)
}

module.exports = Transaction
Loading