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 bcrypto from '../crypto'
|
|
|
|
import * as lazy from './lazy'
|
|
|
|
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
2018-07-03 14:06:44 +02:00
|
|
|
const typef = require('typeforce')
|
2018-12-29 14:49:35 +01:00
|
|
|
const OPS = bscript.OPS
|
2018-07-03 14:06:44 +02:00
|
|
|
const ecc = require('tiny-secp256k1')
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-07-03 14:06:44 +02:00
|
|
|
const bs58check = require('bs58check')
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
// input: {signature} {pubkey}
|
|
|
|
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
|
2018-12-29 08:10:36 +01:00
|
|
|
export function p2pkh (a: Payment, opts?: PaymentOpts): Payment {
|
2018-06-05 09:24:47 +02:00
|
|
|
if (
|
|
|
|
!a.address &&
|
|
|
|
!a.hash &&
|
|
|
|
!a.output &&
|
|
|
|
!a.pubkey &&
|
|
|
|
!a.input
|
|
|
|
) 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),
|
|
|
|
address: typef.maybe(typef.String),
|
|
|
|
hash: typef.maybe(typef.BufferN(20)),
|
|
|
|
output: typef.maybe(typef.BufferN(25)),
|
|
|
|
|
|
|
|
pubkey: typef.maybe(ecc.isPoint),
|
|
|
|
signature: typef.maybe(bscript.isCanonicalScriptSignature),
|
|
|
|
input: typef.maybe(typef.Buffer)
|
|
|
|
}, a)
|
|
|
|
|
2018-07-03 14:06:44 +02:00
|
|
|
const _address = lazy.value(function () {
|
|
|
|
const payload = bs58check.decode(a.address)
|
|
|
|
const version = payload.readUInt8(0)
|
|
|
|
const hash = payload.slice(1)
|
|
|
|
return { version, hash }
|
|
|
|
})
|
2019-01-15 09:47:30 +01:00
|
|
|
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(a.input!) })
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-07-03 14:06:44 +02:00
|
|
|
const network = a.network || BITCOIN_NETWORK
|
2018-12-28 16:53:54 +01:00
|
|
|
const o: Payment = { network }
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
lazy.prop(o, 'address', function () {
|
|
|
|
if (!o.hash) return
|
2018-07-03 14:06:44 +02:00
|
|
|
|
|
|
|
const payload = Buffer.allocUnsafe(21)
|
|
|
|
payload.writeUInt8(network.pubKeyHash, 0)
|
|
|
|
o.hash.copy(payload, 1)
|
|
|
|
return bs58check.encode(payload)
|
2018-06-05 09:24:47 +02:00
|
|
|
})
|
|
|
|
lazy.prop(o, 'hash', function () {
|
|
|
|
if (a.output) return a.output.slice(3, 23)
|
|
|
|
if (a.address) return _address().hash
|
2019-01-15 09:47:30 +01:00
|
|
|
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!)
|
2018-06-05 09:24:47 +02:00
|
|
|
})
|
|
|
|
lazy.prop(o, 'output', function () {
|
|
|
|
if (!o.hash) return
|
|
|
|
return bscript.compile([
|
|
|
|
OPS.OP_DUP,
|
|
|
|
OPS.OP_HASH160,
|
|
|
|
o.hash,
|
|
|
|
OPS.OP_EQUALVERIFY,
|
|
|
|
OPS.OP_CHECKSIG
|
|
|
|
])
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'pubkey', function () {
|
|
|
|
if (!a.input) return
|
2018-12-28 17:55:07 +01:00
|
|
|
return <Buffer>_chunks()[1]
|
2018-06-05 09:24:47 +02:00
|
|
|
})
|
|
|
|
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.pubkey) return
|
|
|
|
if (!a.signature) return
|
|
|
|
return bscript.compile([a.signature, a.pubkey])
|
|
|
|
})
|
|
|
|
lazy.prop(o, 'witness', function () {
|
|
|
|
if (!o.input) return
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
|
|
|
|
// extended validation
|
|
|
|
if (opts.validate) {
|
2018-12-28 17:55:07 +01:00
|
|
|
let hash: Buffer = Buffer.from([])
|
2018-06-05 09:24:47 +02:00
|
|
|
if (a.address) {
|
2018-07-14 12:24:11 +02:00
|
|
|
if (_address().version !== network.pubKeyHash) throw new TypeError('Invalid version or Network mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
|
2018-07-14 12:24:11 +02:00
|
|
|
hash = _address().hash
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.hash) {
|
2018-12-28 17:55:07 +01:00
|
|
|
if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
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 ||
|
|
|
|
a.output[24] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid')
|
|
|
|
|
2018-07-27 12:14:11 +02:00
|
|
|
const hash2 = a.output.slice(3, 23)
|
2018-12-28 17:55:07 +01:00
|
|
|
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
2018-07-27 12:14:11 +02:00
|
|
|
else hash = hash2
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.pubkey) {
|
2018-07-27 12:14:11 +02:00
|
|
|
const pkh = bcrypto.hash160(a.pubkey)
|
2018-12-28 17:55:07 +01:00
|
|
|
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
else hash = pkh
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.input) {
|
2018-07-27 12:14:11 +02:00
|
|
|
const chunks = _chunks()
|
2018-06-05 09:24:47 +02:00
|
|
|
if (chunks.length !== 2) throw new TypeError('Input is invalid')
|
2018-12-28 16:00:52 +01:00
|
|
|
if (!bscript.isCanonicalScriptSignature(<Buffer>chunks[0])) throw new TypeError('Input has invalid signature')
|
2018-06-05 09:24:47 +02:00
|
|
|
if (!ecc.isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey')
|
|
|
|
|
2018-12-28 16:00:52 +01:00
|
|
|
if (a.signature && !a.signature.equals(<Buffer>chunks[0])) throw new TypeError('Signature mismatch')
|
|
|
|
if (a.pubkey && !a.pubkey.equals(<Buffer>chunks[1])) throw new TypeError('Pubkey mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
|
2018-12-28 16:00:52 +01:00
|
|
|
const pkh = bcrypto.hash160(<Buffer>chunks[1])
|
2018-12-28 17:55:07 +01:00
|
|
|
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
2018-06-05 09:24:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign(o, a)
|
|
|
|
}
|