Skip to content

Commit

Permalink
more default function args; default Interp flags
Browse files Browse the repository at this point in the history
- Use default function arguments of ES6/ES2015 available in Node 6.1 in more
  places. I went through every library class and replaced all obvious
  roundabout uses of default arguments with real default arguments.

- Replace Interp default flags = 0 with more reasonable defaults, particularly
  P2SH and CHECKLOCKTIMEVERIFY. When using this code in a real full node, you
  need to specify flags manually because which flags to use depends on context.
  But when using the interpreter casually to build and test transactions, it is
  better to use all the flags. The only major flag that is not yet active is
  CHECKSEQUENCEVERIFY - we should enable that flag by default once it gets
  activated. It's worth noting however that whether that flag is used or not
  depends on the version of the transaction.
  • Loading branch information
ryanxcharles committed May 14, 2016
1 parent ef69f4d commit 4d309cc
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 40 deletions.
6 changes: 2 additions & 4 deletions lib/bip-39.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,12 @@ let inject = function (deps) {
* Convert a mnemonic to a seed. Does not check for validity of the mnemonic -
* for that, you should manually run check() first.
*/
mnemonic2Seed (passphrase) {
mnemonic2Seed (passphrase = '') {
let mnemonic = this.mnemonic
if (!this.check()) {
throw new Error('Mnemonic does not pass the check - was the mnemonic typed incorrectly? Are there extra spaces?')
}
if (passphrase === undefined) {
passphrase = ''
} else if (typeof passphrase !== 'string') {
if (typeof passphrase !== 'string') {
throw new Error('passphrase must be a string or undefined')
}
mnemonic = mnemonic.normalize('NFKD')
Expand Down
29 changes: 11 additions & 18 deletions lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ let inject = function (deps) {
return this
}

BN.fromBuffer = function (buf, opts) {
if (opts !== undefined && opts.endian === 'little') {
BN.fromBuffer = function (buf, opts = {endian: 'big'}) {
if (opts.endian === 'little') {
buf = reverseBuf(buf)
}
let hex = buf.toString('hex')
Expand All @@ -102,9 +102,9 @@ let inject = function (deps) {
return this
}

BN.prototype.toBuffer = function (opts) {
BN.prototype.toBuffer = function (opts = {size: undefined, endian: 'big'}) {
let buf
if (opts && opts.size) {
if (opts.size) {
let hex = this.toString(16, 2)
let natlen = hex.length / 2
buf = new Buffer(hex, 'hex')
Expand All @@ -128,7 +128,7 @@ let inject = function (deps) {
buf = new Buffer(hex, 'hex')
}

if (opts !== undefined && opts.endian === 'little') {
if (opts.endian === 'little') {
buf = reverseBuf(buf)
}
let longzero = new Buffer([0])
Expand All @@ -142,16 +142,12 @@ let inject = function (deps) {
* Signed magnitude buffer. Most significant bit represents sign (0 = positive,
* 1 = negative).
*/
BN.prototype.fromSm = function (buf, opts) {
BN.prototype.fromSm = function (buf, opts = {endian: 'big'}) {
if (buf.length === 0) {
this.fromBuffer(new Buffer([0]))
}

let endian = 'big'
if (opts) {
endian = opts.endian
}

let endian = opts.endian
if (endian === 'little') {
buf = reverseBuf(buf)
}
Expand All @@ -166,11 +162,8 @@ let inject = function (deps) {
return this
}

BN.prototype.toSm = function (opts) {
let endian = 'big'
if (opts) {
endian = opts.endian
}
BN.prototype.toSm = function (opts = {endian: 'big'}) {
let endian = opts.endian

let buf
if (this.cmp(0) === -1) {
Expand Down Expand Up @@ -202,13 +195,13 @@ let inject = function (deps) {
* Produce a BN from the "bits" value in a blockheader. Analagous to Bitcoin
* Core's uint256 SetCompact method. bits is assumed to be UInt32.
*/
BN.prototype.fromBits = function (bits, opts) {
BN.prototype.fromBits = function (bits, opts = {strict: false}) {
// To performed bitwise operations in javascript, we need to convert to a
// signed 32 bit value.
let buf = new Buffer(4)
buf.writeUInt32BE(bits, 0)
bits = buf.readInt32BE(0)
if (opts && opts.strict && (bits & 0x00800000)) {
if (opts.strict && (bits & 0x00800000)) {
throw new Error('negative bit set')
}
let nsize = bits >> 24
Expand Down
10 changes: 2 additions & 8 deletions lib/br.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,13 @@ let inject = function (deps) {
return this.pos >= this.buf.length
}

read (len) {
if (len === undefined) {
len = this.buf.length
}
read (len = this.buf.length) {
let buf = this.buf.slice(this.pos, this.pos + len)
this.pos = this.pos + len
return buf
}

readReverse (len) {
if (len === undefined) {
len = this.buf.length
}
readReverse (len = this.buf.length) {
let buf = this.buf.slice(this.pos, this.pos + len)
this.pos = this.pos + len
let buf2 = new Buffer(buf.length)
Expand Down
10 changes: 8 additions & 2 deletions lib/interp.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let inject = function (deps) {
let TxIn = deps.TxIn

class Interp extends Struct {
constructor (script, tx, nIn, stack = [], altStack = [], pc = 0, pBeginCodeHash = 0, nOpCount = 0, ifStack = [], errStr = '', flags = 0) {
constructor (script, tx, nIn, stack = [], altStack = [], pc = 0, pBeginCodeHash = 0, nOpCount = 0, ifStack = [], errStr = '', flags = Interp.defaultFlags) {
super({script, tx, nIn, stack, altStack, pc, pBeginCodeHash, nOpCount, ifStack, errStr, flags})
}

Expand All @@ -56,7 +56,7 @@ let inject = function (deps) {
this.nOpCount = 0
this.ifStack = []
this.errStr = ''
this.flags = 0
this.flags = Interp.defaultFlags
return this
}

Expand Down Expand Up @@ -1527,6 +1527,12 @@ let inject = function (deps) {
// See Bip112 for details
Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = (1 << 10)

// These are the things we wish to verify by default. At the time of writing,
// P2SH and CHECKLOCKTIMEVERIFY are both active, but CHECKSEQUENCEVERIFY is
// not.
Interp.defaultFlags = Interp.SCRIPT_VERIFY_P2SH | Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
// Interp.defaultFlags = Interp.SCRIPT_VERIFY_P2SH | Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY | Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY

return Interp
}

Expand Down
4 changes: 2 additions & 2 deletions lib/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ let inject = function (deps) {
* defaults to true. If sort is true, the pubKeys are sorted
* lexicographically.
*/
fromPubKeys (m, pubKeys, sort) {
fromPubKeys (m, pubKeys, sort = true) {
if (typeof m !== 'number') {
throw new Error('m must be a number')
}
if (sort === true || sort === undefined) {
if (sort === true) {
pubKeys = Script.sortPubKeys(pubKeys)
}
this.writeOpCode(m + OpCode.OP_1 - 1)
Expand Down
6 changes: 2 additions & 4 deletions lib/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ let inject = function (deps) {
throw new Error('buf must be a buffer')
}
let br = new Br(buf)
let args = [br].concat(rest)
return this.fromBr.apply(this, args)
return this.fromBr(br, ...rest)
}

static fromBuffer (...rest) {
Expand All @@ -160,8 +159,7 @@ let inject = function (deps) {
throw new Error('buf must be a buffer')
}
let br = new Br(buf)
let args = [br].concat(rest)
return this.asyncFromBr.apply(this, args)
return this.asyncFromBr(br, ...rest)
}

static asyncFromBuffer (buf, ...rest) {
Expand Down
4 changes: 2 additions & 2 deletions test/interp.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Interp', function () {
interp.nOpCount.should.equal(0)
interp.ifStack.length.should.equal(0)
interp.errStr.should.equal('')
interp.flags.should.equal(0)
interp.flags.should.equal(Interp.defaultFlags)
interp = new Interp()
;(interp instanceof Interp).should.equal(true)
interp.stack.length.should.equal(0)
Expand All @@ -31,7 +31,7 @@ describe('Interp', function () {
interp.nOpCount.should.equal(0)
interp.ifStack.length.should.equal(0)
interp.errStr.should.equal('')
interp.flags.should.equal(0)
interp.flags.should.equal(Interp.defaultFlags)
})

describe('#fromJson', function () {
Expand Down

0 comments on commit 4d309cc

Please sign in to comment.