rm templates export, rename to classify

This commit is contained in:
Daniel Cousens 2018-07-03 21:43:34 +10:00
parent 7711bba1dc
commit 6cacea6f31
6 changed files with 188 additions and 557 deletions

View file

@ -2,10 +2,10 @@ const Buffer = require('safe-buffer').Buffer
const bech32 = require('bech32')
const bs58check = require('bs58check')
const bscript = require('./script')
const btemplates = require('./templates')
const networks = require('./networks')
const typeforce = require('typeforce')
const types = require('./types')
const payments = require('./payments')
function fromBase58Check (address) {
const payload = bs58check.decode(address)
@ -48,15 +48,15 @@ function toBech32 (data, version, prefix) {
return bech32.encode(prefix, words)
}
function fromOutputScript (outputScript, network) {
function fromOutputScript (output, network) {
network = network || networks.bitcoin
if (btemplates.pubKeyHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(3, 23), network.pubKeyHash)
if (btemplates.scriptHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(2, 22), network.scriptHash)
if (btemplates.witnessPubKeyHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 22), 0, network.bech32)
if (btemplates.witnessScriptHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 34), 0, network.bech32)
try { return payments.p2pkh({ output, network }).address } catch (e) {}
try { return payments.p2sh({ output, network }).address } catch (e) {}
try { return payments.p2wpkh({ output, network }).address } catch (e) {}
try { return payments.p2wsh({ output, network }).address } catch (e) {}
throw new Error(bscript.toASM(outputScript) + ' has no matching Address')
throw new Error(bscript.toASM(output) + ' has no matching Address')
}
function toOutputScript (address, network) {
@ -68,8 +68,8 @@ function toOutputScript (address, network) {
} catch (e) {}
if (decode) {
if (decode.version === network.pubKeyHash) return btemplates.pubKeyHash.output.encode(decode.hash)
if (decode.version === network.scriptHash) return btemplates.scriptHash.output.encode(decode.hash)
if (decode.version === network.pubKeyHash) return payments.p2pkh({ hash: decode.hash }).output
if (decode.version === network.scriptHash) return payments.p2sh({ hash: decode.hash }).output
} else {
try {
decode = fromBech32(address)
@ -78,8 +78,8 @@ function toOutputScript (address, network) {
if (decode) {
if (decode.prefix !== network.bech32) throw new Error(address + ' has an invalid prefix')
if (decode.version === 0) {
if (decode.data.length === 20) return btemplates.witnessPubKeyHash.output.encode(decode.data)
if (decode.data.length === 32) return btemplates.witnessScriptHash.output.encode(decode.data)
if (decode.data.length === 20) return payments.p2wpkh({ hash: decode.data }).output
if (decode.data.length === 32) return payments.p2wsh({ hash: decode.data }).output
}
}
}

View file

@ -1,12 +1,12 @@
const decompile = require('../script').decompile
const multisig = require('./multisig')
const nullData = require('./nulldata')
const pubKey = require('./pubkey')
const pubKeyHash = require('./pubkeyhash')
const scriptHash = require('./scripthash')
const witnessPubKeyHash = require('./witnesspubkeyhash')
const witnessScriptHash = require('./witnessscripthash')
const witnessCommitment = require('./witnesscommitment')
const decompile = require('./script').decompile
const multisig = require('./templates/multisig')
const nullData = require('./templates/nulldata')
const pubKey = require('./templates/pubkey')
const pubKeyHash = require('./templates/pubkeyhash')
const scriptHash = require('./templates/scripthash')
const witnessPubKeyHash = require('./templates/witnesspubkeyhash')
const witnessScriptHash = require('./templates/witnessscripthash')
const witnessCommitment = require('./templates/witnesscommitment')
const types = {
MULTISIG: 'multisig',
@ -63,16 +63,8 @@ function classifyWitness (script, allowIncomplete) {
}
module.exports = {
classifyInput: classifyInput,
classifyOutput: classifyOutput,
classifyWitness: classifyWitness,
multisig: multisig,
nullData: nullData,
pubKey: pubKey,
pubKeyHash: pubKeyHash,
scriptHash: scriptHash,
witnessPubKeyHash: witnessPubKeyHash,
witnessScriptHash: witnessScriptHash,
witnessCommitment: witnessCommitment,
input: classifyInput,
output: classifyOutput,
witness: classifyWitness,
types: types
}

View file

@ -1,8 +1,4 @@
const script = require('./script')
const templates = require('./templates')
for (let key in templates) {
script[key] = templates[key]
}
module.exports = {
Block: require('./block'),

View file

@ -2,13 +2,13 @@ const Buffer = require('safe-buffer').Buffer
const baddress = require('./address')
const bcrypto = require('./crypto')
const bscript = require('./script')
const btemplates = require('./templates')
const networks = require('./networks')
const ops = require('bitcoin-ops')
const payments = require('./payments')
const SCRIPT_TYPES = btemplates.types
const typeforce = require('typeforce')
const types = require('./types')
const classify = require('./classify')
const SCRIPT_TYPES = classify.types
const ECPair = require('./ecpair')
const Transaction = require('./transaction')
@ -16,8 +16,8 @@ const Transaction = require('./transaction')
function expandInput (scriptSig, witnessStack, type, scriptPubKey) {
if (scriptSig.length === 0 && witnessStack.length === 0) return {}
if (!type) {
let ssType = btemplates.classifyInput(scriptSig, true)
let wsType = btemplates.classifyWitness(witnessStack, true)
let ssType = classify.input(scriptSig, true)
let wsType = classify.witness(witnessStack, true)
if (ssType === SCRIPT_TYPES.NONSTANDARD) ssType = undefined
if (wsType === SCRIPT_TYPES.NONSTANDARD) wsType = undefined
type = ssType || wsType
@ -76,7 +76,7 @@ function expandInput (scriptSig, witnessStack, type, scriptPubKey) {
witness: witnessStack
})
const outputType = btemplates.classifyOutput(redeem.output)
const outputType = classify.output(redeem.output)
const expanded = expandInput(redeem.input, redeem.witness, outputType, redeem.output)
if (!expanded.prevOutType) return {}
@ -98,7 +98,7 @@ function expandInput (scriptSig, witnessStack, type, scriptPubKey) {
input: scriptSig,
witness: witnessStack
})
const outputType = btemplates.classifyOutput(redeem.output)
const outputType = classify.output(redeem.output)
let expanded
if (outputType === SCRIPT_TYPES.P2WPKH) {
expanded = expandInput(redeem.input, redeem.witness, outputType)
@ -160,7 +160,7 @@ function fixMultisigOrder (input, transaction, vin) {
function expandOutput (script, ourPubKey) {
typeforce(types.Buffer, script)
const type = btemplates.classifyOutput(script)
const type = classify.output(script)
switch (type) {
case SCRIPT_TYPES.P2PKH: {
@ -543,7 +543,7 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
}
input.prevOutScript = options.prevOutScript
input.prevOutType = prevOutType || btemplates.classifyOutput(options.prevOutScript)
input.prevOutType = prevOutType || classify.output(options.prevOutScript)
}
const vin = this.__tx.addInput(txHash, vout, options.sequence, options.scriptSig)