2018-12-29 14:49:35 +01:00
|
|
|
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
2018-12-28 16:00:52 +01:00
|
|
|
import * as bscript from '../script'
|
|
|
|
import * as lazy from './lazy'
|
|
|
|
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
2018-07-27 12:14:11 +02:00
|
|
|
const typef = require('typeforce')
|
2018-12-29 14:49:35 +01:00
|
|
|
const OPS = bscript.OPS
|
2018-07-27 12:14:11 +02:00
|
|
|
const ecc = require('tiny-secp256k1')
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
// input: {signature}
|
|
|
|
// output: {pubKey} OP_CHECKSIG
|
2018-12-29 08:10:36 +01:00
|
|
|
export function p2pk (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.pubkey &&
|
|
|
|
!a.input &&
|
|
|
|
!a.signature
|
2018-06-05 09:24:47 +02:00
|
|
|
) throw new TypeError('Not enough data')
|
2018-08-28 07:21:18 +02:00
|
|
|
opts = Object.assign({ validate: true }, opts || {})
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
typef({
|
|
|
|
network: typef.maybe(typef.Object),
|
|
|
|
output: typef.maybe(typef.Buffer),
|
|
|
|
pubkey: typef.maybe(ecc.isPoint),
|
|
|
|
|
|
|
|
signature: typef.maybe(bscript.isCanonicalScriptSignature),
|
|
|
|
input: typef.maybe(typef.Buffer)
|
|
|
|
}, a)
|
|
|
|
|
2018-12-28 17:55:07 +01:00
|
|
|
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>a.input) })
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-07-27 12:14:11 +02:00
|
|
|
const network = a.network || BITCOIN_NETWORK
|
2018-12-28 16:00:52 +01:00
|
|
|
const o: Payment = { network }
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
lazy.prop(o, 'output', function () {
|
|
|
|
if (!a.pubkey) return
|
|
|
|
return bscript.compile([
|
|
|
|
a.pubkey,
|
|
|
|
OPS.OP_CHECKSIG
|
|
|
|
])
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'pubkey', function () {
|
|
|
|
if (!a.output) return
|
|
|
|
return a.output.slice(1, -1)
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'signature', function () {
|
|
|
|
if (!a.input) return
|
2018-12-28 17:55:07 +01:00
|
|
|
return <Buffer>_chunks()[0]
|
2018-06-05 09:24:47 +02:00
|
|
|
})
|
|
|
|
lazy.prop(o, 'input', function () {
|
|
|
|
if (!a.signature) return
|
|
|
|
return bscript.compile([a.signature])
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'witness', function () {
|
|
|
|
if (!o.input) return
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
|
|
|
|
// extended validation
|
|
|
|
if (opts.validate) {
|
|
|
|
if (a.output) {
|
|
|
|
if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid')
|
|
|
|
if (!ecc.isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid')
|
2018-12-28 17:55:07 +01:00
|
|
|
if (a.pubkey && !a.pubkey.equals(<Buffer>o.pubkey)) throw new TypeError('Pubkey mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.signature) {
|
2018-12-28 17:55:07 +01:00
|
|
|
if (a.input && !a.input.equals(<Buffer>o.input)) throw new TypeError('Signature mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.input) {
|
|
|
|
if (_chunks().length !== 1) throw new TypeError('Input is invalid')
|
2018-12-28 17:55:07 +01:00
|
|
|
if (!bscript.isCanonicalScriptSignature(<Buffer>o.signature)) throw new TypeError('Input has invalid signature')
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign(o, a)
|
|
|
|
}
|