You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.9 KiB
142 lines
4.9 KiB
6 years ago
|
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||
6 years ago
|
import * as bscript from '../script'
|
||
|
import * as lazy from './lazy'
|
||
|
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
||
6 years ago
|
const OPS = bscript.OPS
|
||
7 years ago
|
const typef = require('typeforce')
|
||
|
const ecc = require('tiny-secp256k1')
|
||
7 years ago
|
|
||
7 years ago
|
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
||
7 years ago
|
|
||
6 years ago
|
function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||
7 years ago
|
if (a.length !== b.length) return false
|
||
|
|
||
|
return a.every(function (x, i) {
|
||
|
return x.equals(b[i])
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// input: OP_0 [signatures ...]
|
||
|
// output: m [pubKeys ...] n OP_CHECKMULTISIG
|
||
6 years ago
|
export function p2ms (a: Payment, opts?: PaymentOpts): Payment {
|
||
7 years ago
|
if (
|
||
7 years ago
|
!a.input &&
|
||
7 years ago
|
!a.output &&
|
||
7 years ago
|
!(a.pubkeys && a.m !== undefined) &&
|
||
|
!a.signatures
|
||
7 years ago
|
) throw new TypeError('Not enough data')
|
||
6 years ago
|
opts = Object.assign({ validate: true }, opts || {})
|
||
7 years ago
|
|
||
6 years ago
|
function isAcceptableSignature (x: Buffer | number) {
|
||
6 years ago
|
return bscript.isCanonicalScriptSignature(<Buffer>x) ||
|
||
|
((<PaymentOpts>opts).allowIncomplete &&
|
||
|
(<number> x === OPS.OP_0)) !== undefined // eslint-disable-line
|
||
7 years ago
|
}
|
||
|
|
||
|
typef({
|
||
|
network: typef.maybe(typef.Object),
|
||
|
m: typef.maybe(typef.Number),
|
||
|
n: typef.maybe(typef.Number),
|
||
|
output: typef.maybe(typef.Buffer),
|
||
|
pubkeys: typef.maybe(typef.arrayOf(ecc.isPoint)),
|
||
|
|
||
|
signatures: typef.maybe(typef.arrayOf(isAcceptableSignature)),
|
||
|
input: typef.maybe(typef.Buffer)
|
||
|
}, a)
|
||
|
|
||
7 years ago
|
const network = a.network || BITCOIN_NETWORK
|
||
6 years ago
|
const o: Payment = { network }
|
||
7 years ago
|
|
||
6 years ago
|
let chunks: Array<Buffer | number> = []
|
||
7 years ago
|
let decoded = false
|
||
6 years ago
|
function decode (output: Buffer | Array<Buffer | number>): void {
|
||
7 years ago
|
if (decoded) return
|
||
|
decoded = true
|
||
6 years ago
|
chunks = <Array<Buffer | number>>bscript.decompile(output)
|
||
6 years ago
|
o.m = <number> chunks[0] - OP_INT_BASE // eslint-disable-line
|
||
|
o.n = <number> chunks[chunks.length - 2] - OP_INT_BASE // eslint-disable-line
|
||
6 years ago
|
o.pubkeys = <Array<Buffer>>chunks.slice(1, -2)
|
||
7 years ago
|
}
|
||
|
|
||
|
lazy.prop(o, 'output', function () {
|
||
|
if (!a.m) return
|
||
|
if (!o.n) return
|
||
|
if (!a.pubkeys) return
|
||
6 years ago
|
return bscript.compile((<Array<Buffer | number>>[]).concat(
|
||
7 years ago
|
OP_INT_BASE + a.m,
|
||
|
a.pubkeys,
|
||
|
OP_INT_BASE + o.n,
|
||
|
OPS.OP_CHECKMULTISIG
|
||
|
))
|
||
|
})
|
||
|
lazy.prop(o, 'm', function () {
|
||
|
if (!o.output) return
|
||
|
decode(o.output)
|
||
|
return o.m
|
||
|
})
|
||
|
lazy.prop(o, 'n', function () {
|
||
|
if (!o.pubkeys) return
|
||
|
return o.pubkeys.length
|
||
|
})
|
||
|
lazy.prop(o, 'pubkeys', function () {
|
||
|
if (!a.output) return
|
||
|
decode(a.output)
|
||
|
return o.pubkeys
|
||
|
})
|
||
|
lazy.prop(o, 'signatures', function () {
|
||
|
if (!a.input) return
|
||
6 years ago
|
return (<Array<Buffer | number>>bscript.decompile(a.input)).slice(1)
|
||
7 years ago
|
})
|
||
|
lazy.prop(o, 'input', function () {
|
||
|
if (!a.signatures) return
|
||
6 years ago
|
return bscript.compile((<Array<Buffer | number>>[OPS.OP_0]).concat(a.signatures))
|
||
7 years ago
|
})
|
||
|
lazy.prop(o, 'witness', function () {
|
||
|
if (!o.input) return
|
||
|
return []
|
||
|
})
|
||
|
|
||
|
// extended validation
|
||
|
if (opts.validate) {
|
||
|
if (a.output) {
|
||
|
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')
|
||
|
|
||
|
if (
|
||
6 years ago
|
<number>(<Payment>o).m <= 0 || // eslint-disable-line
|
||
|
<number>(<Payment>o).n > 16 || // eslint-disable-line
|
||
|
<number>(<Payment>o).m > <number>(<Payment>o).n || // eslint-disable-line
|
||
7 years ago
|
o.n !== chunks.length - 3) throw new TypeError('Output is invalid')
|
||
6 years ago
|
if (!(<Array<Buffer>>o.pubkeys).every(x => ecc.isPoint(x))) throw new TypeError('Output is invalid')
|
||
7 years ago
|
|
||
|
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')
|
||
6 years ago
|
if (a.pubkeys && !stacksEqual(a.pubkeys, (<Array<Buffer>>o.pubkeys))) throw new TypeError('Pubkeys mismatch')
|
||
7 years ago
|
}
|
||
|
|
||
|
if (a.pubkeys) {
|
||
|
if (a.n !== undefined && a.n !== a.pubkeys.length) throw new TypeError('Pubkey count mismatch')
|
||
|
o.n = a.pubkeys.length
|
||
|
|
||
6 years ago
|
if (o.n < <number>(<Payment>o).m) throw new TypeError('Pubkey count cannot be less than m')
|
||
7 years ago
|
}
|
||
|
|
||
|
if (a.signatures) {
|
||
6 years ago
|
if (a.signatures.length < <number>(<Payment>o).m) throw new TypeError('Not enough signatures provided')
|
||
|
if (a.signatures.length > <number>(<Payment>o).m) throw new TypeError('Too many signatures provided')
|
||
7 years ago
|
}
|
||
|
|
||
|
if (a.input) {
|
||
|
if (a.input[0] !== OPS.OP_0) throw new TypeError('Input is invalid')
|
||
6 years ago
|
if ((<Array<Buffer>>o.signatures).length === 0 || !(<Array<Buffer>>o.signatures).every(isAcceptableSignature)) throw new TypeError('Input has invalid signature(s)')
|
||
7 years ago
|
|
||
6 years ago
|
if (a.signatures && !stacksEqual(a.signatures, (<Array<Buffer>>o.signatures))) throw new TypeError('Signature mismatch')
|
||
|
if (a.m !== undefined && a.m !== (<Array<Buffer>>a.signatures).length) throw new TypeError('Signature count mismatch')
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return Object.assign(o, a)
|
||
|
}
|