bitcoinjs-lib/ts_src/classify.ts

72 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2019-03-03 15:07:49 +01:00
import { decompile } from './script';
import * as multisig from './templates/multisig';
import * as nullData from './templates/nulldata';
import * as pubKey from './templates/pubkey';
import * as pubKeyHash from './templates/pubkeyhash';
import * as scriptHash from './templates/scripthash';
2019-03-07 03:54:37 +01:00
import * as witnessCommitment from './templates/witnesscommitment';
2019-03-03 15:07:49 +01:00
import * as witnessPubKeyHash from './templates/witnesspubkeyhash';
import * as witnessScriptHash from './templates/witnessscripthash';
2018-06-25 08:25:12 +02:00
const types = {
2019-03-07 03:54:37 +01:00
P2MS: 'multisig' as string,
NONSTANDARD: 'nonstandard' as string,
NULLDATA: 'nulldata' as string,
P2PK: 'pubkey' as string,
P2PKH: 'pubkeyhash' as string,
P2SH: 'scripthash' as string,
P2WPKH: 'witnesspubkeyhash' as string,
P2WSH: 'witnessscripthash' as string,
WITNESS_COMMITMENT: 'witnesscommitment' as string,
2019-03-03 15:07:49 +01:00
};
2016-11-02 04:11:55 +01:00
2019-03-03 15:07:49 +01:00
function classifyOutput(script: Buffer): string {
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
2019-03-03 15:07:49 +01:00
const chunks = decompile(script);
if (!chunks) throw new TypeError('Invalid script');
2019-03-03 15:07:49 +01:00
if (multisig.output.check(chunks)) return types.P2MS;
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
2019-03-03 15:07:49 +01:00
return types.NONSTANDARD;
2016-11-02 04:11:55 +01:00
}
2019-03-03 15:07:49 +01:00
function classifyInput(script: Buffer, allowIncomplete: boolean): string {
2016-11-02 04:11:55 +01:00
// XXX: optimization, below functions .decompile before use
2019-03-03 15:07:49 +01:00
const chunks = decompile(script);
if (!chunks) throw new TypeError('Invalid script');
2016-11-02 04:11:55 +01:00
2019-03-03 15:07:49 +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.P2MS;
if (pubKey.input.check(chunks)) return types.P2PK;
2016-11-02 04:11:55 +01:00
2019-03-03 15:07:49 +01:00
return types.NONSTANDARD;
2016-11-02 04:11:55 +01:00
}
2019-03-07 03:54:37 +01:00
function classifyWitness(script: Buffer[], allowIncomplete: boolean): string {
2016-11-02 04:11:55 +01:00
// XXX: optimization, below functions .decompile before use
2019-03-03 15:07:49 +01:00
const chunks = decompile(script);
if (!chunks) throw new TypeError('Invalid script');
2016-11-02 04:11:55 +01:00
2019-03-03 15:07:49 +01:00
if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH;
2019-03-07 03:54:37 +01:00
if (witnessScriptHash.input.check(chunks as Buffer[], allowIncomplete))
2019-03-03 15:07:49 +01:00
return types.P2WSH;
2019-03-03 15:07:49 +01:00
return types.NONSTANDARD;
2016-11-02 04:11:55 +01:00
}
export {
classifyInput as input,
classifyOutput as output,
classifyWitness as witness,
types,
2019-03-03 15:07:49 +01:00
};