2022-04-06 00:41:17 +02:00
|
|
|
import { mainnet as LBRY_MAINNET } from '../networks';
|
2019-03-03 15:07:49 +01:00
|
|
|
import * as bscript from '../script';
|
2021-10-20 12:18:01 +02:00
|
|
|
import { isPoint, typeforce as typef } from '../types';
|
2019-03-07 04:40:18 +01:00
|
|
|
import { Payment, PaymentOpts, Stack } from './index';
|
2019-03-03 15:07:49 +01:00
|
|
|
import * as lazy from './lazy';
|
|
|
|
const OPS = bscript.OPS;
|
|
|
|
|
|
|
|
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
|
|
|
|
|
2019-03-07 04:40:18 +01:00
|
|
|
function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (a.length !== b.length) return false;
|
|
|
|
|
2019-03-07 04:40:18 +01:00
|
|
|
return a.every((x, i) => {
|
2019-03-03 15:07:49 +01:00
|
|
|
return x.equals(b[i]);
|
|
|
|
});
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// input: OP_0 [signatures ...]
|
|
|
|
// output: m [pubKeys ...] n OP_CHECKMULTISIG
|
2019-03-03 15:07:49 +01:00
|
|
|
export function p2ms(a: Payment, opts?: PaymentOpts): Payment {
|
2018-06-05 09:24:47 +02:00
|
|
|
if (
|
2018-06-27 09:29:18 +02:00
|
|
|
!a.input &&
|
2018-06-05 09:24:47 +02:00
|
|
|
!a.output &&
|
2018-06-27 03:22:35 +02:00
|
|
|
!(a.pubkeys && a.m !== undefined) &&
|
|
|
|
!a.signatures
|
2019-03-03 15:07:49 +01:00
|
|
|
)
|
|
|
|
throw new TypeError('Not enough data');
|
|
|
|
opts = Object.assign({ validate: true }, opts || {});
|
|
|
|
|
2019-03-21 16:15:37 +01:00
|
|
|
function isAcceptableSignature(x: Buffer | number): boolean {
|
2019-03-03 15:07:49 +01:00
|
|
|
return (
|
2019-03-07 04:40:18 +01:00
|
|
|
bscript.isCanonicalScriptSignature(x as Buffer) ||
|
|
|
|
(opts!.allowIncomplete && (x as number) === OPS.OP_0) !== undefined
|
2019-03-05 07:11:20 +01:00
|
|
|
);
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
typef(
|
|
|
|
{
|
|
|
|
network: typef.maybe(typef.Object),
|
|
|
|
m: typef.maybe(typef.Number),
|
|
|
|
n: typef.maybe(typef.Number),
|
|
|
|
output: typef.maybe(typef.Buffer),
|
2021-10-20 12:18:01 +02:00
|
|
|
pubkeys: typef.maybe(typef.arrayOf(isPoint)),
|
2019-03-03 15:07:49 +01:00
|
|
|
|
|
|
|
signatures: typef.maybe(typef.arrayOf(isAcceptableSignature)),
|
|
|
|
input: typef.maybe(typef.Buffer),
|
|
|
|
},
|
|
|
|
a,
|
|
|
|
);
|
|
|
|
|
2022-04-06 00:41:17 +02:00
|
|
|
const network = a.network || LBRY_MAINNET;
|
2019-03-03 15:07:49 +01:00
|
|
|
const o: Payment = { network };
|
|
|
|
|
2019-03-07 04:40:18 +01:00
|
|
|
let chunks: Stack = [];
|
2019-03-03 15:07:49 +01:00
|
|
|
let decoded = false;
|
2019-03-07 04:40:18 +01:00
|
|
|
function decode(output: Buffer | Stack): void {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (decoded) return;
|
|
|
|
decoded = true;
|
2019-03-07 04:40:18 +01:00
|
|
|
chunks = bscript.decompile(output) as Stack;
|
|
|
|
o.m = (chunks[0] as number) - OP_INT_BASE;
|
|
|
|
o.n = (chunks[chunks.length - 2] as number) - OP_INT_BASE;
|
|
|
|
o.pubkeys = chunks.slice(1, -2) as Buffer[];
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:40:18 +01:00
|
|
|
lazy.prop(o, 'output', () => {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!a.m) return;
|
|
|
|
if (!o.n) return;
|
|
|
|
if (!a.pubkeys) return;
|
|
|
|
return bscript.compile(
|
2019-03-07 04:40:18 +01:00
|
|
|
([] as Stack).concat(
|
2019-03-03 15:07:49 +01:00
|
|
|
OP_INT_BASE + a.m,
|
|
|
|
a.pubkeys,
|
|
|
|
OP_INT_BASE + o.n,
|
|
|
|
OPS.OP_CHECKMULTISIG,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2019-03-07 04:40:18 +01:00
|
|
|
lazy.prop(o, 'm', () => {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!o.output) return;
|
|
|
|
decode(o.output);
|
|
|
|
return o.m;
|
|
|
|
});
|
2019-03-07 04:40:18 +01:00
|
|
|
lazy.prop(o, 'n', () => {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!o.pubkeys) return;
|
|
|
|
return o.pubkeys.length;
|
|
|
|
});
|
2019-03-07 04:40:18 +01:00
|
|
|
lazy.prop(o, 'pubkeys', () => {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!a.output) return;
|
|
|
|
decode(a.output);
|
|
|
|
return o.pubkeys;
|
|
|
|
});
|
2019-03-07 04:40:18 +01:00
|
|
|
lazy.prop(o, 'signatures', () => {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!a.input) return;
|
|
|
|
return bscript.decompile(a.input)!.slice(1);
|
|
|
|
});
|
2019-03-07 04:40:18 +01:00
|
|
|
lazy.prop(o, 'input', () => {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!a.signatures) return;
|
2019-03-07 04:40:18 +01:00
|
|
|
return bscript.compile(([OPS.OP_0] as Stack).concat(a.signatures));
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
2019-03-07 04:40:18 +01:00
|
|
|
lazy.prop(o, 'witness', () => {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!o.input) return;
|
|
|
|
return [];
|
|
|
|
});
|
2019-07-11 07:49:26 +02:00
|
|
|
lazy.prop(o, 'name', () => {
|
|
|
|
if (!o.m || !o.n) return;
|
|
|
|
return `p2ms(${o.m} of ${o.n})`;
|
|
|
|
});
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
// extended validation
|
|
|
|
if (opts.validate) {
|
|
|
|
if (a.output) {
|
2019-03-03 15:07:49 +01:00
|
|
|
decode(a.output);
|
|
|
|
if (!typef.Number(chunks[0])) throw new TypeError('Output is invalid');
|
|
|
|
if (!typef.Number(chunks[chunks.length - 2]))
|
|
|
|
throw new TypeError('Output is invalid');
|
|
|
|
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG)
|
|
|
|
throw new TypeError('Output is invalid');
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2019-03-05 07:11:20 +01:00
|
|
|
if (o.m! <= 0 || o.n! > 16 || o.m! > o.n! || o.n !== chunks.length - 3)
|
2019-03-03 15:07:49 +01:00
|
|
|
throw new TypeError('Output is invalid');
|
2021-10-20 12:18:01 +02:00
|
|
|
if (!o.pubkeys!.every(x => isPoint(x)))
|
2019-03-03 15:07:49 +01:00
|
|
|
throw new TypeError('Output is invalid');
|
|
|
|
|
|
|
|
if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch');
|
|
|
|
if (a.n !== undefined && a.n !== o.n) throw new TypeError('n mismatch');
|
|
|
|
if (a.pubkeys && !stacksEqual(a.pubkeys, o.pubkeys!))
|
|
|
|
throw new TypeError('Pubkeys mismatch');
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.pubkeys) {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (a.n !== undefined && a.n !== a.pubkeys.length)
|
|
|
|
throw new TypeError('Pubkey count mismatch');
|
|
|
|
o.n = a.pubkeys.length;
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
if (o.n < o.m!) throw new TypeError('Pubkey count cannot be less than m');
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.signatures) {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (a.signatures.length < o.m!)
|
|
|
|
throw new TypeError('Not enough signatures provided');
|
|
|
|
if (a.signatures.length > o.m!)
|
|
|
|
throw new TypeError('Too many signatures provided');
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.input) {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (a.input[0] !== OPS.OP_0) throw new TypeError('Input is invalid');
|
|
|
|
if (
|
|
|
|
o.signatures!.length === 0 ||
|
|
|
|
!o.signatures!.every(isAcceptableSignature)
|
|
|
|
)
|
|
|
|
throw new TypeError('Input has invalid signature(s)');
|
|
|
|
|
|
|
|
if (a.signatures && !stacksEqual(a.signatures, o.signatures!))
|
|
|
|
throw new TypeError('Signature mismatch');
|
|
|
|
if (a.m !== undefined && a.m !== a.signatures!.length)
|
|
|
|
throw new TypeError('Signature count mismatch');
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
return Object.assign(o, a);
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|