-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #911 from bitcoinjs/segwittests
Add tests for scripts.witness*.input and fixes
- Loading branch information
Showing
3 changed files
with
130 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
// {signature} {pubKey} | ||
// <scriptSig> {serialized scriptPubKey script} | ||
|
||
var p2sh = require('../scripthash/input') | ||
var bscript = require('../../script') | ||
var types = require('../../types') | ||
var typeforce = require('typeforce') | ||
|
||
var p2ms = require('../multisig/') | ||
var p2pk = require('../pubkey/') | ||
var p2pkh = require('../pubkeyhash/') | ||
|
||
function check (chunks, allowIncomplete) { | ||
typeforce(types.Array, chunks) | ||
if (chunks.length < 1) return false | ||
|
||
var witnessScript = chunks[chunks.length - 1] | ||
if (!Buffer.isBuffer(witnessScript)) return false | ||
|
||
var witnessScriptChunks = bscript.decompile(witnessScript) | ||
|
||
// is witnessScript a valid script? | ||
if (witnessScriptChunks.length === 0) return false | ||
|
||
var witnessRawScriptSig = bscript.compile(chunks.slice(0, -1)) | ||
|
||
// match types | ||
if (p2pkh.input.check(witnessRawScriptSig) && | ||
p2pkh.output.check(witnessScriptChunks)) return true | ||
|
||
if (p2ms.input.check(witnessRawScriptSig, allowIncomplete) && | ||
p2ms.output.check(witnessScriptChunks)) return true | ||
|
||
if (p2pk.input.check(witnessRawScriptSig) && | ||
p2pk.output.check(witnessScriptChunks)) return true | ||
|
||
return false | ||
} | ||
check.toJSON = function () { return 'witnessScriptHash input' } | ||
|
||
function encodeStack (witnessData, witnessScript) { | ||
typeforce({ | ||
witnessData: [types.Buffer], | ||
witnessScript: types.Buffer | ||
}, { | ||
witnessData: witnessData, | ||
witnessScript: witnessScript | ||
}) | ||
|
||
return [].concat(witnessData, witnessScript) | ||
} | ||
|
||
function decodeStack (chunks) { | ||
typeforce(check, chunks) | ||
return { | ||
witnessData: chunks.slice(0, -1), | ||
witnessScript: chunks[chunks.length - 1] | ||
} | ||
} | ||
|
||
module.exports = { | ||
check: p2sh.check, | ||
decodeStack: p2sh.decodeStack, | ||
encodeStack: p2sh.encodeStack | ||
check: check, | ||
decodeStack: decodeStack, | ||
encodeStack: encodeStack | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters