Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various breaking changes #444

Merged
merged 4 commits into from
Aug 20, 2015
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
24 changes: 12 additions & 12 deletions src/address.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var base58check = require('bs58check')
var bs58check = require('bs58check')
var bscript = require('./script')
var networks = require('./networks')
var scripts = require('./scripts')
var typeforce = require('typeforce')
var types = require('./types')

function fromBase58Check (address) {
var payload = base58check.decode(address)
var payload = bs58check.decode(address)
if (payload.length < 21) throw new TypeError(address + ' is too short')
if (payload.length > 21) throw new TypeError(address + ' is too long')

Expand All @@ -15,14 +15,14 @@ function fromBase58Check (address) {
return { hash: hash, version: version }
}

function fromOutputScript (script, network) {
function fromOutputScript (scriptPubKey, network) {
network = network || networks.bitcoin

var chunks = scripts.decompile(script)
if (scripts.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (scripts.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
var chunks = bscript.decompile(scriptPubKey)
if (bscript.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (bscript.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)

throw new Error(scripts.toASM(chunks) + ' has no matching Address')
throw new Error(bscript.toASM(chunks) + ' has no matching Address')
}

function toBase58Check (hash, version) {
Expand All @@ -32,21 +32,21 @@ function toBase58Check (hash, version) {
payload.writeUInt8(version, 0)
hash.copy(payload, 1)

return base58check.encode(payload)
return bs58check.encode(payload)
}

function toOutputScript (address, network) {
network = network || networks.bitcoin

var payload = base58check.decode(address)
var payload = bs58check.decode(address)
if (payload.length < 21) throw new TypeError(address + ' is too short')
if (payload.length > 21) throw new TypeError(address + ' is too long')

var version = payload.readUInt8(0)
var hash = payload.slice(1)

if (version === network.pubKeyHash) return scripts.pubKeyHashOutput(hash)
if (version === network.scriptHash) return scripts.scriptHashOutput(hash)
if (version === network.pubKeyHash) return bscript.pubKeyHashOutput(hash)
if (version === network.scriptHash) return bscript.scriptHashOutput(hash)

throw new Error(address + ' has no matching Script')
}
Expand Down
4 changes: 2 additions & 2 deletions src/block.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')

var Transaction = require('./transaction')

Expand Down Expand Up @@ -66,7 +66,7 @@ Block.fromHex = function (hex) {
}

Block.prototype.getHash = function () {
return crypto.hash256(this.toBuffer(true))
return bcrypto.hash256(this.toBuffer(true))
}

Block.prototype.getId = function () {
Expand Down
33 changes: 17 additions & 16 deletions src/ecpair.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
var bs58check = require('bs58check')
var bcrypto = require('./crypto')
var bs58check = require('bs58check')
var ecdsa = require('./ecdsa')
var ecurve = require('ecurve')
var NETWORKS = require('./networks')
var randomBytes = require('randombytes')
var typeforce = require('typeforce')
var types = require('./types')

var NETWORKS = require('./networks')
var BigInteger = require('bigi')

var secp256k1 = ecurve.getCurveByName('secp256k1')

function ECPair (d, Q, options) {
options = options || {}
if (options) {
typeforce({
compressed: types.maybe(types.Boolean),
network: types.maybe(types.Network)
}, options)
}

typeforce({
compressed: types.maybe(types.Boolean),
network: types.maybe(types.Network)
}, options)
options = options || {}

if (d) {
if (d.signum() <= 0) throw new Error('Private key must be greater than 0')
if (d.compareTo(ECPair.curve.n) >= 0) throw new Error('Private key must be less than the curve order')
if (d.compareTo(secp256k1.n) >= 0) throw new Error('Private key must be less than the curve order')
if (Q) throw new TypeError('Unexpected publicKey parameter')

this.d = d
Expand All @@ -37,18 +41,15 @@ function ECPair (d, Q, options) {
Object.defineProperty(ECPair.prototype, 'Q', {
get: function () {
if (!this.__Q && this.d) {
this.__Q = ECPair.curve.G.multiply(this.d)
this.__Q = secp256k1.G.multiply(this.d)
}

return this.__Q
}
})

// Public access to secp256k1 curve
ECPair.curve = ecurve.getCurveByName('secp256k1')

ECPair.fromPublicKeyBuffer = function (buffer, network) {
var Q = ecurve.Point.decodeFrom(ECPair.curve, buffer)
var Q = ecurve.Point.decodeFrom(secp256k1, buffer)

return new ECPair(null, Q, {
compressed: Q.compressed,
Expand Down Expand Up @@ -108,7 +109,7 @@ ECPair.makeRandom = function (options) {
typeforce(types.Buffer256bit, buffer)

var d = BigInteger.fromBuffer(buffer)
d = d.mod(ECPair.curve.n)
d = d.mod(secp256k1.n)

return new ECPair(d, null, options)
}
Expand Down Expand Up @@ -147,11 +148,11 @@ ECPair.prototype.getPublicKeyBuffer = function () {
ECPair.prototype.sign = function (hash) {
if (!this.d) throw new Error('Missing private key')

return ecdsa.sign(ECPair.curve, hash, this.d)
return ecdsa.sign(secp256k1, hash, this.d)
}

ECPair.prototype.verify = function (hash, signature) {
return ecdsa.verify(ECPair.curve, hash, signature, this.Q)
return ecdsa.verify(secp256k1, hash, signature, this.Q)
}

module.exports = ECPair
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module.exports = {
Address: require('./address'),
Block: require('./block'),
ECPair: require('./ecpair'),
ECSignature: require('./ecsignature'),
HDNode: require('./hdnode'),
Transaction: require('./transaction'),
TransactionBuilder: require('./transaction_builder'),

address: require('./address'),
bufferutils: require('./bufferutils'),
crypto: require('./crypto'),
message: require('./message'),
networks: require('./networks'),
opcodes: require('./opcodes'),
scripts: require('./scripts')
script: require('./script')
}
4 changes: 2 additions & 2 deletions src/message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')
var ecdsa = require('./ecdsa')
var networks = require('./networks')

Expand All @@ -16,7 +16,7 @@ function magicHash (message, network) {
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)

var buffer = Buffer.concat([messagePrefix, lengthBuffer, messageBuffer])
return crypto.hash256(buffer)
return bcrypto.hash256(buffer)
}

function sign (keyPair, message, network) {
Expand Down
File renamed without changes.
24 changes: 11 additions & 13 deletions src/transaction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')
var opcodes = require('./opcodes')
var scripts = require('./scripts')
var bscript = require('./script')
var typeforce = require('typeforce')
var types = require('./types')

Expand Down Expand Up @@ -99,7 +99,7 @@ Transaction.isCoinbaseHash = function (buffer) {

var EMPTY_SCRIPT = new Buffer(0)

Transaction.prototype.addInput = function (hash, index, sequence, script) {
Transaction.prototype.addInput = function (hash, index, sequence, scriptSig) {
typeforce(types.tuple(
types.Hash256bit,
types.UInt32,
Expand All @@ -111,13 +111,11 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) {
sequence = Transaction.DEFAULT_SEQUENCE
}

script = script || EMPTY_SCRIPT

// Add the input and return the input's index
return (this.ins.push({
hash: hash,
index: index,
script: script,
script: scriptSig || EMPTY_SCRIPT,
sequence: sequence
}) - 1)
}
Expand All @@ -133,8 +131,8 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
}

Transaction.prototype.byteLength = function () {
function scriptSize (script) {
var length = script.length
function scriptSize (someScript) {
var length = someScript.length

return bufferutils.varIntSize(length) + length
}
Expand Down Expand Up @@ -193,7 +191,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT

// in case concatenating two scripts ends up with two codeseparators,
// or an extra one at the end, this prevents all those possible incompatibilities.
var hashScript = scripts.compile(scripts.decompile(prevOutScript).filter(function (x) {
var hashScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) {
return x !== opcodes.OP_CODESEPARATOR
}))
var i
Expand Down Expand Up @@ -252,11 +250,11 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
buffer.writeInt32LE(hashType, buffer.length - 4)
txTmp.toBuffer().copy(buffer, 0)

return crypto.hash256(buffer)
return bcrypto.hash256(buffer)
}

Transaction.prototype.getHash = function () {
return crypto.hash256(this.toBuffer())
return bcrypto.hash256(this.toBuffer())
}

Transaction.prototype.getId = function () {
Expand Down Expand Up @@ -320,10 +318,10 @@ Transaction.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}

Transaction.prototype.setInputScript = function (index, script) {
Transaction.prototype.setInputScript = function (index, scriptSig) {
typeforce(types.tuple(types.Number, types.Buffer), arguments)

this.ins[index].script = script
this.ins[index].script = scriptSig
}

module.exports = Transaction
Loading