Skip to content
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

Replace script type strings with constants #679

Merged
merged 1 commit into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"coverage": "nyc --check-coverage --branches 90 --functions 90 mocha",
"integration": "mocha test/integration/",
"standard": "standard",
"standard-fixer": "standard --fix",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unnecessary IMHO... it should rarely be needed...

"test": "npm run standard && npm run coverage",
"unit": "mocha"
},
Expand Down
39 changes: 24 additions & 15 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ var REVERSE_OPS = (function () {
}
return result
})()

var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
var scriptTypes = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a constant too? (caps)

P2PKH: 'pubkeyhash',
P2PK: 'pubkey',
MULTISIG: 'multisig',
P2SH: 'scripthash',
P2WSH: 'witnessscripthash',
P2WPKH: 'witnesspubkeyhash',
NULLDATA: 'nulldata',
NONSTANDARD: 'nonstandard'
}

function compile (chunks) {
// TODO: remove me
Expand Down Expand Up @@ -283,38 +292,38 @@ function classifyOutput (script) {
var chunks = decompile(script)

if (isWitnessPubKeyHashOutput(chunks)) {
return 'witnesspubkeyhash'
return scriptTypes.P2WPKH
} else if (isWitnessScriptHashOutput(chunks)) {
return 'witnessscripthash'
return scriptTypes.P2WSH
} else if (isPubKeyHashOutput(chunks)) {
return 'pubkeyhash'
return scriptTypes.P2PKH
} else if (isScriptHashOutput(chunks)) {
return 'scripthash'
return scriptTypes.P2SH
} else if (isMultisigOutput(chunks)) {
return 'multisig'
return scriptTypes.MULTISIG
} else if (isPubKeyOutput(chunks)) {
return 'pubkey'
return scriptTypes.P2PK
} else if (isNullDataOutput(chunks)) {
return 'nulldata'
return scriptTypes.NULLDATA
}

return 'nonstandard'
return scriptTypes.NONSTANDARD
}

function classifyInput (script, allowIncomplete) {
var chunks = decompile(script)

if (isPubKeyHashInput(chunks)) {
return 'pubkeyhash'
return scriptTypes.P2PKH
} else if (isMultisigInput(chunks, allowIncomplete)) {
return 'multisig'
return scriptTypes.MULTISIG
} else if (isScriptHashInput(chunks, allowIncomplete)) {
return 'scripthash'
return scriptTypes.P2SH
} else if (isPubKeyInput(chunks)) {
return 'pubkey'
return scriptTypes.P2PK
}

return 'nonstandard'
return scriptTypes.NONSTANDARD
}

// Standard Script Templates
Expand Down Expand Up @@ -425,7 +434,7 @@ module.exports = {
toASM: toASM,

number: require('./script_number'),

types: scriptTypes,
isCanonicalPubKey: isCanonicalPubKey,
isCanonicalSignature: isCanonicalSignature,
isDefinedHashType: isDefinedHashType,
Expand Down
37 changes: 19 additions & 18 deletions src/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var networks = require('./networks')
var ops = require('./opcodes.json')
var typeforce = require('typeforce')
var types = require('./types')
var scriptTypes = bscript.types

var ECPair = require('./ecpair')
var ECSignature = require('./ecsignature')
Expand All @@ -19,7 +20,7 @@ function expandInput (scriptSig, redeemScript) {
var pubKeys, signatures, prevOutScript

switch (prevOutType) {
case 'scripthash':
case scriptTypes.P2SH:
// FIXME: maybe depth limit instead, how possible is this anyway?
if (redeemScript) throw new Error('Recursive P2SH script')

Expand All @@ -30,10 +31,10 @@ function expandInput (scriptSig, redeemScript) {
result.redeemScript = redeemScript
result.redeemScriptType = result.prevOutType
result.prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
result.prevOutType = 'scripthash'
result.prevOutType = scriptTypes.P2SH
return result

case 'pubkeyhash':
case scriptTypes.P2PKH:
// if (redeemScript) throw new Error('Nonstandard... P2SH(P2PKH)')
pubKeys = scriptSigChunks.slice(1)
signatures = scriptSigChunks.slice(0, 1)
Expand All @@ -42,15 +43,15 @@ function expandInput (scriptSig, redeemScript) {
prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
break

case 'pubkey':
case scriptTypes.P2PK:
if (redeemScript) {
pubKeys = bscript.decompile(redeemScript).slice(0, 1)
}

signatures = scriptSigChunks.slice(0, 1)
break

case 'multisig':
case scriptTypes.MULTISIG:
if (redeemScript) {
pubKeys = bscript.decompile(redeemScript).slice(1, -2)
}
Expand All @@ -71,7 +72,7 @@ function expandInput (scriptSig, redeemScript) {

// could be done in expandInput, but requires the original Transaction for hashForSignature
function fixMultisigOrder (input, transaction, vin) {
if (input.redeemScriptType !== 'multisig' || !input.redeemScript) return
if (input.redeemScriptType !== scriptTypes.MULTISIG || !input.redeemScript) return
if (input.pubKeys.length === input.signatures.length) return

var unmatched = input.signatures.concat()
Expand Down Expand Up @@ -115,19 +116,19 @@ function expandOutput (script, scriptType, ourPubKey) {

switch (scriptType) {
// does our hash160(pubKey) match the output scripts?
case 'pubkeyhash':
case scriptTypes.P2PKH:
if (!ourPubKey) break

var pkh1 = scriptChunks[2]
var pkh2 = bcrypto.hash160(ourPubKey)
if (pkh1.equals(pkh2)) pubKeys = [ourPubKey]
break

case 'pubkey':
case scriptTypes.P2PK:
pubKeys = scriptChunks.slice(0, 1)
break

case 'multisig':
case scriptTypes.MULTISIG:
pubKeys = scriptChunks.slice(1, -2)
break

Expand All @@ -148,7 +149,7 @@ function prepareInput (input, kpPubKey, redeemScript) {
// if redeemScript exists, it is pay-to-scriptHash
// if we have a prevOutScript, enforce hash160(redeemScriptequality) to the redeemScript
if (input.prevOutType) {
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH')

var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1]
if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(RedeemScript)')
Expand All @@ -162,12 +163,12 @@ function prepareInput (input, kpPubKey, redeemScript) {
input.redeemScript = redeemScript
input.redeemScriptType = expanded.scriptType
input.prevOutScript = input.prevOutScript || bscript.scriptHashOutput(redeemScriptHash)
input.prevOutType = 'scripthash'
input.prevOutType = scriptTypes.P2SH

// maybe we have some prevOut knowledge
} else if (input.prevOutType) {
// pay-to-scriptHash is not possible without a redeemScript
if (input.prevOutType === 'scripthash') throw new Error('PrevOutScript is P2SH, missing redeemScript')
if (input.prevOutType === scriptTypes.P2SH) throw new Error('PrevOutScript is P2SH, missing redeemScript')

// try to derive missing information using our kpPubKey
expanded = expandOutput(input.prevOutScript, input.prevOutType, kpPubKey)
Expand All @@ -179,7 +180,7 @@ function prepareInput (input, kpPubKey, redeemScript) {
// no prior knowledge, assume pubKeyHash
} else {
input.prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(kpPubKey))
input.prevOutType = 'pubkeyhash'
input.prevOutType = scriptTypes.P2PKH
input.pubKeys = [kpPubKey]
input.signatures = [undefined]
}
Expand All @@ -191,10 +192,10 @@ function buildInput (input, allowIncomplete) {
var scriptSig

switch (scriptType) {
case 'pubkeyhash':
case 'pubkey':
case scriptTypes.P2PKH:
case scriptTypes.P2PK:
if (signatures.length < 1 || !signatures[0]) throw new Error('Not enough signatures provided')
if (scriptType === 'pubkeyhash') {
if (scriptType === scriptTypes.P2PKH) {
scriptSig = bscript.pubKeyHashInput(signatures[0], input.pubKeys[0])
} else {
scriptSig = bscript.pubKeyInput(signatures[0])
Expand All @@ -203,7 +204,7 @@ function buildInput (input, allowIncomplete) {
break

// ref https://github.com/bitcoin/bitcoin/blob/d612837814020ae832499d18e6ee5eb919a87907/src/script/sign.cpp#L232
case 'multisig':
case scriptTypes.MULTISIG:
signatures = signatures.map(function (signature) {
return signature || ops.OP_0
})
Expand All @@ -220,7 +221,7 @@ function buildInput (input, allowIncomplete) {
}

// wrap as scriptHash if necessary
if (input.prevOutType === 'scripthash') {
if (input.prevOutType === scriptTypes.P2SH) {
scriptSig = bscript.scriptHashInput(scriptSig, input.redeemScript)
}

Expand Down