Merge pull request #481 from bitcoinjs/minimalpush
isStandard compliance [for pubKeyHash / scriptHash outputs]
This commit is contained in:
commit
40fc1b9e2e
4 changed files with 46 additions and 35 deletions
|
@ -18,11 +18,10 @@ function fromBase58Check (address) {
|
||||||
function fromOutputScript (scriptPubKey, network) {
|
function fromOutputScript (scriptPubKey, network) {
|
||||||
network = network || networks.bitcoin
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
var chunks = bscript.decompile(scriptPubKey)
|
if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(scriptPubKey.slice(3, 23), network.pubKeyHash)
|
||||||
if (bscript.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
|
if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(scriptPubKey.slice(2, 22), network.scriptHash)
|
||||||
if (bscript.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
|
|
||||||
|
|
||||||
throw new Error(bscript.toASM(chunks) + ' has no matching Address')
|
throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address')
|
||||||
}
|
}
|
||||||
|
|
||||||
function toBase58Check (hash, version) {
|
function toBase58Check (hash, version) {
|
||||||
|
|
|
@ -156,15 +156,14 @@ function isPubKeyHashInput (script) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPubKeyHashOutput (script) {
|
function isPubKeyHashOutput (script) {
|
||||||
var chunks = decompile(script)
|
var buffer = compile(script)
|
||||||
|
|
||||||
return chunks.length === 5 &&
|
return buffer.length === 25 &&
|
||||||
chunks[0] === OPS.OP_DUP &&
|
buffer[0] === OPS.OP_DUP &&
|
||||||
chunks[1] === OPS.OP_HASH160 &&
|
buffer[1] === OPS.OP_HASH160 &&
|
||||||
Buffer.isBuffer(chunks[2]) &&
|
buffer[2] === 0x14 &&
|
||||||
chunks[2].length === 20 &&
|
buffer[23] === OPS.OP_EQUALVERIFY &&
|
||||||
chunks[3] === OPS.OP_EQUALVERIFY &&
|
buffer[24] === OPS.OP_CHECKSIG
|
||||||
chunks[4] === OPS.OP_CHECKSIG
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPubKeyInput (script) {
|
function isPubKeyInput (script) {
|
||||||
|
@ -199,13 +198,12 @@ function isScriptHashInput (script, allowIncomplete) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isScriptHashOutput (script) {
|
function isScriptHashOutput (script) {
|
||||||
var chunks = decompile(script)
|
var buffer = compile(script)
|
||||||
|
|
||||||
return chunks.length === 3 &&
|
return buffer.length === 23 &&
|
||||||
chunks[0] === OPS.OP_HASH160 &&
|
buffer[0] === OPS.OP_HASH160 &&
|
||||||
Buffer.isBuffer(chunks[1]) &&
|
buffer[1] === 0x14 &&
|
||||||
chunks[1].length === 20 &&
|
buffer[22] === OPS.OP_EQUAL
|
||||||
chunks[2] === OPS.OP_EQUAL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// allowIncomplete is to account for combining signatures
|
// allowIncomplete is to account for combining signatures
|
||||||
|
|
12
test/fixtures/script.json
vendored
12
test/fixtures/script.json
vendored
|
@ -200,6 +200,18 @@
|
||||||
"scriptPubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ffffff OP_CHECKSIG"
|
"scriptPubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ffffff OP_CHECKSIG"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"isPubKeyHashOutput": [
|
||||||
|
{
|
||||||
|
"description": "non-minimal encoded isPubKeyHashOutput (non BIP62 compliant)",
|
||||||
|
"scriptPubKeyHex": "76a94c14aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"isScriptHashOutput": [
|
||||||
|
{
|
||||||
|
"description": "non-minimal encoded isScriptHashOutput (non BIP62 compliant)",
|
||||||
|
"scriptPubKeyHex": "a94c14c286a1af0947f58d1ad787385b1c2c4a976f9e7187"
|
||||||
|
}
|
||||||
|
],
|
||||||
"isMultisigOutput": [
|
"isMultisigOutput": [
|
||||||
{
|
{
|
||||||
"description": "OP_CHECKMULTISIG not found",
|
"description": "OP_CHECKMULTISIG not found",
|
||||||
|
|
|
@ -149,19 +149,17 @@ describe('script', function () {
|
||||||
if (!(inputFnName in fixtures.invalid)) return
|
if (!(inputFnName in fixtures.invalid)) return
|
||||||
|
|
||||||
fixtures.invalid[inputFnName].forEach(function (f) {
|
fixtures.invalid[inputFnName].forEach(function (f) {
|
||||||
if (inputFn && (f.scriptSig || f.scriptSigHex)) {
|
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
|
||||||
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
|
var scriptSig
|
||||||
var scriptSig
|
|
||||||
|
|
||||||
if (f.scriptSig) {
|
if (f.scriptSig) {
|
||||||
scriptSig = bscript.fromASM(f.scriptSig)
|
scriptSig = bscript.fromASM(f.scriptSig)
|
||||||
} else {
|
} else {
|
||||||
scriptSig = bscript.fromHex(f.scriptSigHex)
|
scriptSig = new Buffer(f.scriptSigHex, 'hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.strictEqual(inputFn(scriptSig), false)
|
assert.strictEqual(inputFn(scriptSig), false)
|
||||||
})
|
})
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -181,13 +179,17 @@ describe('script', function () {
|
||||||
if (!(outputFnName in fixtures.invalid)) return
|
if (!(outputFnName in fixtures.invalid)) return
|
||||||
|
|
||||||
fixtures.invalid[outputFnName].forEach(function (f) {
|
fixtures.invalid[outputFnName].forEach(function (f) {
|
||||||
if (outputFn && f.scriptPubKey) {
|
it('returns false for ' + f.description + ' (' + (f.scriptPubKey || f.scriptPubKeyHex) + ')', function () {
|
||||||
it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () {
|
var scriptPubKey
|
||||||
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
|
||||||
|
|
||||||
assert.strictEqual(outputFn(scriptPubKey), false)
|
if (f.scriptPubKey) {
|
||||||
})
|
scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
||||||
}
|
} else {
|
||||||
|
scriptPubKey = new Buffer(f.scriptPubKeyHex, 'hex')
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.strictEqual(outputFn(scriptPubKey), false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue