Skip to content

Commit 40fc1b9

Browse files
committed
Merge pull request bitcoinjs#481 from bitcoinjs/minimalpush
isStandard compliance [for pubKeyHash / scriptHash outputs]
2 parents ce9670e + d48a7ab commit 40fc1b9

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

src/address.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ function fromBase58Check (address) {
1818
function fromOutputScript (scriptPubKey, network) {
1919
network = network || networks.bitcoin
2020

21-
var chunks = bscript.decompile(scriptPubKey)
22-
if (bscript.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
23-
if (bscript.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
21+
if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(scriptPubKey.slice(3, 23), network.pubKeyHash)
22+
if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(scriptPubKey.slice(2, 22), network.scriptHash)
2423

25-
throw new Error(bscript.toASM(chunks) + ' has no matching Address')
24+
throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address')
2625
}
2726

2827
function toBase58Check (hash, version) {

src/script.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,14 @@ function isPubKeyHashInput (script) {
156156
}
157157

158158
function isPubKeyHashOutput (script) {
159-
var chunks = decompile(script)
160-
161-
return chunks.length === 5 &&
162-
chunks[0] === OPS.OP_DUP &&
163-
chunks[1] === OPS.OP_HASH160 &&
164-
Buffer.isBuffer(chunks[2]) &&
165-
chunks[2].length === 20 &&
166-
chunks[3] === OPS.OP_EQUALVERIFY &&
167-
chunks[4] === OPS.OP_CHECKSIG
159+
var buffer = compile(script)
160+
161+
return buffer.length === 25 &&
162+
buffer[0] === OPS.OP_DUP &&
163+
buffer[1] === OPS.OP_HASH160 &&
164+
buffer[2] === 0x14 &&
165+
buffer[23] === OPS.OP_EQUALVERIFY &&
166+
buffer[24] === OPS.OP_CHECKSIG
168167
}
169168

170169
function isPubKeyInput (script) {
@@ -199,13 +198,12 @@ function isScriptHashInput (script, allowIncomplete) {
199198
}
200199

201200
function isScriptHashOutput (script) {
202-
var chunks = decompile(script)
201+
var buffer = compile(script)
203202

204-
return chunks.length === 3 &&
205-
chunks[0] === OPS.OP_HASH160 &&
206-
Buffer.isBuffer(chunks[1]) &&
207-
chunks[1].length === 20 &&
208-
chunks[2] === OPS.OP_EQUAL
203+
return buffer.length === 23 &&
204+
buffer[0] === OPS.OP_HASH160 &&
205+
buffer[1] === 0x14 &&
206+
buffer[22] === OPS.OP_EQUAL
209207
}
210208

211209
// allowIncomplete is to account for combining signatures

test/fixtures/script.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@
200200
"scriptPubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ffffff OP_CHECKSIG"
201201
}
202202
],
203+
"isPubKeyHashOutput": [
204+
{
205+
"description": "non-minimal encoded isPubKeyHashOutput (non BIP62 compliant)",
206+
"scriptPubKeyHex": "76a94c14aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac"
207+
}
208+
],
209+
"isScriptHashOutput": [
210+
{
211+
"description": "non-minimal encoded isScriptHashOutput (non BIP62 compliant)",
212+
"scriptPubKeyHex": "a94c14c286a1af0947f58d1ad787385b1c2c4a976f9e7187"
213+
}
214+
],
203215
"isMultisigOutput": [
204216
{
205217
"description": "OP_CHECKMULTISIG not found",

test/script.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,17 @@ describe('script', function () {
149149
if (!(inputFnName in fixtures.invalid)) return
150150

151151
fixtures.invalid[inputFnName].forEach(function (f) {
152-
if (inputFn && (f.scriptSig || f.scriptSigHex)) {
153-
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
154-
var scriptSig
152+
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
153+
var scriptSig
155154

156-
if (f.scriptSig) {
157-
scriptSig = bscript.fromASM(f.scriptSig)
158-
} else {
159-
scriptSig = bscript.fromHex(f.scriptSigHex)
160-
}
155+
if (f.scriptSig) {
156+
scriptSig = bscript.fromASM(f.scriptSig)
157+
} else {
158+
scriptSig = new Buffer(f.scriptSigHex, 'hex')
159+
}
161160

162-
assert.strictEqual(inputFn(scriptSig), false)
163-
})
164-
}
161+
assert.strictEqual(inputFn(scriptSig), false)
162+
})
165163
})
166164
})
167165

@@ -181,13 +179,17 @@ describe('script', function () {
181179
if (!(outputFnName in fixtures.invalid)) return
182180

183181
fixtures.invalid[outputFnName].forEach(function (f) {
184-
if (outputFn && f.scriptPubKey) {
185-
it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () {
186-
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
182+
it('returns false for ' + f.description + ' (' + (f.scriptPubKey || f.scriptPubKeyHex) + ')', function () {
183+
var scriptPubKey
187184

188-
assert.strictEqual(outputFn(scriptPubKey), false)
189-
})
190-
}
185+
if (f.scriptPubKey) {
186+
scriptPubKey = bscript.fromASM(f.scriptPubKey)
187+
} else {
188+
scriptPubKey = new Buffer(f.scriptPubKeyHex, 'hex')
189+
}
190+
191+
assert.strictEqual(outputFn(scriptPubKey), false)
192+
})
191193
})
192194
})
193195
})

0 commit comments

Comments
 (0)