bitcoinjs-lib/src/classify.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

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')
2018-06-25 08:25:12 +02:00
const types = {
MULTISIG: 'multisig',
NONSTANDARD: 'nonstandard',
NULLDATA: 'nulldata',
P2PK: 'pubkey',
P2PKH: 'pubkeyhash',
P2SH: 'scripthash',
P2WPKH: 'witnesspubkeyhash',
P2WSH: 'witnessscripthash',
WITNESS_COMMITMENT: 'witnesscommitment'
}
2016-11-02 04:11:55 +01:00
function classifyOutput (script) {
if (witnessPubKeyHash.output.check(script)) return types.P2WPKH
if (witnessScriptHash.output.check(script)) return types.P2WSH
if (pubKeyHash.output.check(script)) return types.P2PKH
if (scriptHash.output.check(script)) return types.P2SH
2016-11-02 04:11:55 +01:00
// XXX: optimization, below functions .decompile before use
const chunks = decompile(script)
if (!chunks) throw new TypeError('Invalid script')
if (multisig.output.check(chunks)) return types.MULTISIG
if (pubKey.output.check(chunks)) return types.P2PK
if (witnessCommitment.output.check(chunks)) return types.WITNESS_COMMITMENT
if (nullData.output.check(chunks)) return types.NULLDATA
2016-11-02 04:11:55 +01:00
return types.NONSTANDARD
2016-11-02 04:11:55 +01:00
}
function classifyInput (script, allowIncomplete) {
// XXX: optimization, below functions .decompile before use
const chunks = decompile(script)
if (!chunks) throw new TypeError('Invalid script')
2016-11-02 04:11:55 +01:00
if (pubKeyHash.input.check(chunks)) return types.P2PKH
if (scriptHash.input.check(chunks, allowIncomplete)) return types.P2SH
if (multisig.input.check(chunks, allowIncomplete)) return types.MULTISIG
if (pubKey.input.check(chunks)) return types.P2PK
2016-11-02 04:11:55 +01:00
return types.NONSTANDARD
2016-11-02 04:11:55 +01:00
}
function classifyWitness (script, allowIncomplete) {
// XXX: optimization, below functions .decompile before use
const chunks = decompile(script)
if (!chunks) throw new TypeError('Invalid script')
2016-11-02 04:11:55 +01:00
if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH
if (witnessScriptHash.input.check(chunks, allowIncomplete)) return types.P2WSH
return types.NONSTANDARD
2016-11-02 04:11:55 +01:00
}
module.exports = {
input: classifyInput,
output: classifyOutput,
witness: classifyWitness,
types: types
2016-11-02 04:11:55 +01:00
}