bitcoinjs-lib/ts_src/payments/p2sh.ts

214 lines
6.1 KiB
TypeScript
Raw Normal View History

2019-03-07 04:59:52 +01:00
import * as bcrypto from '../crypto';
2019-03-05 07:11:20 +01:00
import { bitcoin as BITCOIN_NETWORK } from '../networks';
2019-03-04 14:17:29 +01:00
import * as bscript from '../script';
2019-03-07 04:59:52 +01:00
import {
Payment,
PaymentFunction,
PaymentOpts,
Stack,
StackFunction,
} from './index';
2019-03-04 14:17:29 +01:00
import * as lazy from './lazy';
const typef = require('typeforce');
const OPS = bscript.OPS;
2019-03-04 14:17:29 +01:00
const bs58check = require('bs58check');
2019-03-07 04:59:52 +01:00
function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
2019-03-04 14:17:29 +01:00
if (a.length !== b.length) return false;
2019-03-07 04:59:52 +01:00
return a.every((x, i) => {
2019-03-04 14:17:29 +01:00
return x.equals(b[i]);
});
}
// input: [redeemScriptSig ...] {redeemScript}
// witness: <?>
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
2019-03-03 15:07:49 +01:00
export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
if (!a.address && !a.hash && !a.output && !a.redeem && !a.input)
throw new TypeError('Not enough data');
opts = Object.assign({ validate: true }, opts || {});
typef(
{
network: typef.maybe(typef.Object),
2019-03-03 15:07:49 +01:00
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
output: typef.maybe(typef.BufferN(23)),
redeem: typef.maybe({
network: typef.maybe(typef.Object),
output: typef.maybe(typef.Buffer),
input: typef.maybe(typef.Buffer),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
}),
input: typef.maybe(typef.Buffer),
2019-03-03 15:07:49 +01:00
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
},
a,
);
2019-03-03 15:07:49 +01:00
let network = a.network;
if (!network) {
2019-03-03 15:07:49 +01:00
network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
}
2019-03-03 15:07:49 +01:00
const o: Payment = { network };
2019-03-07 04:59:52 +01:00
const _address = lazy.value(() => {
2019-03-03 15:07:49 +01:00
const payload = bs58check.decode(a.address);
const version = payload.readUInt8(0);
const hash = payload.slice(1);
return { version, hash };
});
2019-03-07 04:59:52 +01:00
const _chunks = lazy.value(() => {
2019-03-03 15:07:49 +01:00
return bscript.decompile(a.input!);
2019-03-07 04:59:52 +01:00
}) as StackFunction;
const _redeem = lazy.value(
(): Payment => {
const chunks = _chunks();
return {
network,
output: chunks[chunks.length - 1] as Buffer,
input: bscript.compile(chunks.slice(0, -1)),
witness: a.witness || [],
};
},
) as PaymentFunction;
// output dependents
2019-03-07 04:59:52 +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(o.network!.scriptHash, 0);
o.hash.copy(payload, 1);
return bs58check.encode(payload);
});
2019-03-07 04:59:52 +01:00
lazy.prop(o, 'hash', () => {
// in order of least effort
2019-03-03 15:07:49 +01:00
if (a.output) return a.output.slice(2, 22);
if (a.address) return _address().hash;
if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
});
2019-03-07 04:59:52 +01:00
lazy.prop(o, 'output', () => {
2019-03-03 15:07:49 +01:00
if (!o.hash) return;
return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
});
// input dependents
2019-03-07 04:59:52 +01:00
lazy.prop(o, 'redeem', () => {
2019-03-03 15:07:49 +01:00
if (!a.input) return;
return _redeem();
});
2019-03-07 04:59:52 +01:00
lazy.prop(o, 'input', () => {
2019-03-03 15:07:49 +01:00
if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
return bscript.compile(
2019-03-07 04:59:52 +01:00
([] as Stack).concat(
bscript.decompile(a.redeem.input) as Stack,
2019-03-03 15:07:49 +01:00
a.redeem.output,
),
);
});
2019-03-07 04:59:52 +01:00
lazy.prop(o, 'witness', () => {
2019-03-03 15:07:49 +01:00
if (o.redeem && o.redeem.witness) return o.redeem.witness;
if (o.input) return [];
});
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.scriptHash)
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 !== 23 ||
a.output[0] !== OPS.OP_HASH160 ||
a.output[1] !== 0x14 ||
2019-03-03 15:07:49 +01:00
a.output[22] !== OPS.OP_EQUAL
)
throw new TypeError('Output is invalid');
const hash2 = a.output.slice(2, 22);
if (hash.length > 0 && !hash.equals(hash2))
throw new TypeError('Hash mismatch');
else hash = hash2;
}
// inlined to prevent 'no-inner-declarations' failing
2019-03-07 04:59:52 +01:00
const checkRedeem = (redeem: Payment): void => {
// is the redeem output empty/invalid?
if (redeem.output) {
2019-03-03 15:07:49 +01:00
const decompile = bscript.decompile(redeem.output);
if (!decompile || decompile.length < 1)
throw new TypeError('Redeem.output too short');
// match hash against other sources
2019-03-03 15:07:49 +01:00
const hash2 = bcrypto.hash160(redeem.output);
if (hash.length > 0 && !hash.equals(hash2))
throw new TypeError('Hash mismatch');
else hash = hash2;
}
if (redeem.input) {
2019-03-03 15:07:49 +01:00
const hasInput = redeem.input.length > 0;
const hasWitness = redeem.witness && redeem.witness.length > 0;
if (!hasInput && !hasWitness) throw new TypeError('Empty input');
if (hasInput && hasWitness)
throw new TypeError('Input and witness provided');
if (hasInput) {
2019-03-07 04:59:52 +01:00
const richunks = bscript.decompile(redeem.input) as Stack;
2019-03-03 15:07:49 +01:00
if (!bscript.isPushOnly(richunks))
throw new TypeError('Non push-only scriptSig');
}
}
2019-03-03 15:07:49 +01:00
};
if (a.input) {
2019-03-03 15:07:49 +01:00
const chunks = _chunks();
if (!chunks || chunks.length < 1) throw new TypeError('Input too short');
if (!Buffer.isBuffer(_redeem().output))
throw new TypeError('Input is invalid');
2019-03-03 15:07:49 +01:00
checkRedeem(_redeem());
}
if (a.redeem) {
2019-03-03 15:07:49 +01:00
if (a.redeem.network && a.redeem.network !== network)
throw new TypeError('Network mismatch');
if (a.input) {
2019-03-03 15:07:49 +01:00
const redeem = _redeem();
if (a.redeem.output && !a.redeem.output.equals(redeem.output!))
throw new TypeError('Redeem.output mismatch');
if (a.redeem.input && !a.redeem.input.equals(redeem.input!))
throw new TypeError('Redeem.input mismatch');
}
2019-03-03 15:07:49 +01:00
checkRedeem(a.redeem);
}
if (a.witness) {
if (
a.redeem &&
a.redeem.witness &&
2019-03-03 15:07:49 +01:00
!stacksEqual(a.redeem.witness, a.witness)
)
throw new TypeError('Witness and redeem.witness mismatch');
}
}
2019-03-03 15:07:49 +01:00
return Object.assign(o, a);
}