bitcoinjs-lib/ts_src/payments/p2pkh.ts

145 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

2019-03-03 15:07:49 +01:00
import * as bcrypto from '../crypto';
import { bitcoin as BITCOIN_NETWORK } from '../networks';
2019-03-07 04:51:22 +01:00
import * as bscript from '../script';
import { isPoint, typeforce as typef } from '../types';
2019-03-07 04:51:22 +01:00
import { Payment, PaymentOpts, StackFunction } from './index';
import * as lazy from './lazy';
import * as bs58check from 'bs58check';
2019-03-03 15:07:49 +01:00
const OPS = bscript.OPS;
// input: {signature} {pubkey}
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
2019-03-03 15:07:49 +01:00
export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input)
throw new TypeError('Not enough data');
opts = Object.assign({ validate: true }, opts || {});
typef(
{
network: typef.maybe(typef.Object),
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
output: typef.maybe(typef.BufferN(25)),
pubkey: typef.maybe(isPoint),
2019-03-03 15:07:49 +01:00
signature: typef.maybe(bscript.isCanonicalScriptSignature),
input: typef.maybe(typef.Buffer),
},
a,
);
2019-03-07 04:51:22 +01:00
const _address = lazy.value(() => {
const payload = bs58check.decode(a.address!);
2019-03-03 15:07:49 +01:00
const version = payload.readUInt8(0);
const hash = payload.slice(1);
return { version, hash };
});
2019-03-07 04:51:22 +01:00
const _chunks = lazy.value(() => {
2019-03-03 15:07:49 +01:00
return bscript.decompile(a.input!);
2019-03-07 04:51:22 +01:00
}) as StackFunction;
2019-03-03 15:07:49 +01:00
const network = a.network || BITCOIN_NETWORK;
2019-07-11 07:49:26 +02:00
const o: Payment = { name: 'p2pkh', network };
2019-03-03 15:07:49 +01:00
2019-03-07 04:51:22 +01:00
lazy.prop(o, 'address', () => {
2019-03-03 15:07:49 +01:00
if (!o.hash) return;
const payload = Buffer.allocUnsafe(21);
payload.writeUInt8(network.pubKeyHash, 0);
o.hash.copy(payload, 1);
return bs58check.encode(payload);
});
2019-03-07 04:51:22 +01:00
lazy.prop(o, 'hash', () => {
2019-03-03 15:07:49 +01:00
if (a.output) return a.output.slice(3, 23);
if (a.address) return _address().hash;
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
});
2019-03-07 04:51:22 +01:00
lazy.prop(o, 'output', () => {
2019-03-03 15:07:49 +01:00
if (!o.hash) return;
return bscript.compile([
OPS.OP_DUP,
OPS.OP_HASH160,
o.hash,
OPS.OP_EQUALVERIFY,
2019-03-03 15:07:49 +01:00
OPS.OP_CHECKSIG,
]);
});
2019-03-07 04:51:22 +01:00
lazy.prop(o, 'pubkey', () => {
2019-03-03 15:07:49 +01:00
if (!a.input) return;
2019-03-07 04:51:22 +01:00
return _chunks()[1] as Buffer;
2019-03-03 15:07:49 +01:00
});
2019-03-07 04:51:22 +01:00
lazy.prop(o, 'signature', () => {
2019-03-03 15:07:49 +01:00
if (!a.input) return;
2019-03-07 04:51:22 +01:00
return _chunks()[0] as Buffer;
2019-03-03 15:07:49 +01:00
});
2019-03-07 04:51:22 +01:00
lazy.prop(o, 'input', () => {
2019-03-03 15:07:49 +01:00
if (!a.pubkey) return;
if (!a.signature) return;
return bscript.compile([a.signature, a.pubkey]);
});
2019-03-07 04:51:22 +01:00
lazy.prop(o, 'witness', () => {
2019-03-03 15:07:49 +01:00
if (!o.input) return;
return [];
});
// extended validation
if (opts.validate) {
2019-03-03 15:07:49 +01:00
let hash: Buffer = Buffer.from([]);
if (a.address) {
2019-03-03 15:07:49 +01:00
if (_address().version !== network.pubKeyHash)
throw new TypeError('Invalid version or Network mismatch');
if (_address().hash.length !== 20) throw new TypeError('Invalid address');
hash = _address().hash;
}
if (a.hash) {
2019-03-03 15:07:49 +01:00
if (hash.length > 0 && !hash.equals(a.hash))
throw new TypeError('Hash mismatch');
else hash = a.hash;
}
if (a.output) {
if (
a.output.length !== 25 ||
a.output[0] !== OPS.OP_DUP ||
a.output[1] !== OPS.OP_HASH160 ||
a.output[2] !== 0x14 ||
a.output[23] !== OPS.OP_EQUALVERIFY ||
2019-03-03 15:07:49 +01:00
a.output[24] !== OPS.OP_CHECKSIG
)
throw new TypeError('Output is invalid');
const hash2 = a.output.slice(3, 23);
if (hash.length > 0 && !hash.equals(hash2))
throw new TypeError('Hash mismatch');
else hash = hash2;
}
if (a.pubkey) {
2019-03-03 15:07:49 +01:00
const pkh = bcrypto.hash160(a.pubkey);
if (hash.length > 0 && !hash.equals(pkh))
throw new TypeError('Hash mismatch');
else hash = pkh;
}
if (a.input) {
2019-03-03 15:07:49 +01:00
const chunks = _chunks();
if (chunks.length !== 2) throw new TypeError('Input is invalid');
2019-03-07 04:51:22 +01:00
if (!bscript.isCanonicalScriptSignature(chunks[0] as Buffer))
2019-03-03 15:07:49 +01:00
throw new TypeError('Input has invalid signature');
if (!isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey');
2019-03-03 15:07:49 +01:00
2019-03-07 04:51:22 +01:00
if (a.signature && !a.signature.equals(chunks[0] as Buffer))
2019-03-03 15:07:49 +01:00
throw new TypeError('Signature mismatch');
2019-03-07 04:51:22 +01:00
if (a.pubkey && !a.pubkey.equals(chunks[1] as Buffer))
2019-03-03 15:07:49 +01:00
throw new TypeError('Pubkey mismatch');
2019-03-07 04:51:22 +01:00
const pkh = bcrypto.hash160(chunks[1] as Buffer);
2019-03-03 15:07:49 +01:00
if (hash.length > 0 && !hash.equals(pkh))
throw new TypeError('Hash mismatch');
}
}
2019-03-03 15:07:49 +01:00
return Object.assign(o, a);
}