-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Bitcoin core tests and fixed exception testing #199
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
Changes from all commits
47ae862
2fc69b0
6cfa729
7494a14
8b3634c
b05d17f
12a2dc3
db81443
54950c9
36a1d57
d18f2db
7d94d1b
2f44628
1406915
6f3d829
bda1a83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| var assert = require('assert') | ||
| var bufferutils = require('./bufferutils') | ||
| var crypto = require('./crypto') | ||
| var opcodes = require('./opcodes') | ||
|
|
||
|
|
@@ -246,7 +247,7 @@ Script.prototype.writeOp = function(opcode) { | |
| Script.prototype.writeBytes = function(data) { | ||
| // FIXME: Script module doesn't support buffers yet | ||
| if (Buffer.isBuffer(data)) data = Array.prototype.slice.call(data); | ||
| assert(Array.isArray(data), "Expected a byte array. Got " + data) | ||
| assert(Array.isArray(data), 'Expected a byte array, got ' + data) | ||
|
|
||
| if (data.length < opcodes.OP_PUSHDATA1) { | ||
| this.buffer.push(data.length) | ||
|
|
@@ -304,7 +305,7 @@ Script.createP2SHScriptPubKey = function(hash) { | |
|
|
||
| // m [pubKeys ...] n OP_CHECKMULTISIG | ||
| Script.createMultisigScriptPubKey = function(m, pubKeys) { | ||
| assert(Array.isArray(pubKeys), 'Expected Array, got: ' + pubKeys) | ||
| assert(Array.isArray(pubKeys), 'Expected Array, got ' + pubKeys) | ||
| assert(pubKeys.length >= m, 'Not enough pubKeys provided') | ||
| var script = new Script() | ||
| var n = pubKeys.length | ||
|
|
@@ -367,4 +368,50 @@ Script.prototype.clone = function() { | |
| return new Script(this.buffer) | ||
| } | ||
|
|
||
| Script.fromChunks = function(chunks) { | ||
| assert(Array.isArray(chunks), 'Expected Array, got ' + chunks) | ||
|
|
||
| var bufferSize = chunks.reduce(function(accum, chunk) { | ||
| var chunkSize = 1 | ||
|
|
||
| // FIXME: transitionary | ||
| if (Array.isArray(chunk) || Buffer.isBuffer(chunk)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just convert every array-like chunk in chunks into a buffer after line 372? then the type check won't be necessary here and below. And when chunks are fully bufferified, just remove the conversion code we are good to go. minor point given that this is still WIP
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly what will happen, just avoided for now because this would require changing a lot more code. |
||
| chunkSize = bufferutils.pushDataSize(chunk.length) + chunk.length | ||
| } | ||
|
|
||
| return accum + chunkSize | ||
| }, 0.0) | ||
|
|
||
| var buffer = new Buffer(bufferSize) | ||
| var offset = 0 | ||
|
|
||
| chunks.forEach(function(chunk) { | ||
| // FIXME: transitionary | ||
| if (Array.isArray(chunk) || Buffer.isBuffer(chunk)) { | ||
| offset += bufferutils.writePushDataInt(buffer, chunk.length, offset) | ||
|
|
||
| // FIXME: transitionary | ||
| // chunk.copy(buffer, offset) | ||
| for (var i = 0; i < chunk.length; ++i) { | ||
| buffer[offset + i] = chunk[i] | ||
| } | ||
|
|
||
| offset += chunk.length | ||
|
|
||
| } else { | ||
| buffer.writeUInt8(chunk, offset) | ||
| offset += 1 | ||
| } | ||
| }) | ||
|
|
||
| return Script.fromBuffer(buffer) | ||
| } | ||
|
|
||
| // 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) { | ||
| return op !== needle | ||
| })) | ||
| } | ||
|
|
||
| module.exports = Script | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per discussed in IRC, this implied branch for handling opcodes may not be the clearest. Perhaps making it an actual
elsebranch makes the code easier to read.