Commit js, ts, and definitions in separate folders
This commit is contained in:
parent
e7ac2b9a4e
commit
bc28949056
148 changed files with 3850 additions and 39 deletions
54
ts_src/payments/embed.ts
Normal file
54
ts_src/payments/embed.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||||
import * as bscript from '../script'
|
||||
import * as lazy from './lazy'
|
||||
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
||||
const typef = require('typeforce')
|
||||
const OPS = bscript.OPS
|
||||
|
||||
function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||
if (a.length !== b.length) return false
|
||||
|
||||
return a.every(function (x, i) {
|
||||
return x.equals(b[i])
|
||||
})
|
||||
}
|
||||
|
||||
// output: OP_RETURN ...
|
||||
export function p2data (a: Payment, opts?: PaymentOpts): Payment {
|
||||
if (
|
||||
!a.data &&
|
||||
!a.output
|
||||
) throw new TypeError('Not enough data')
|
||||
opts = Object.assign({ validate: true }, opts || {})
|
||||
|
||||
typef({
|
||||
network: typef.maybe(typef.Object),
|
||||
output: typef.maybe(typef.Buffer),
|
||||
data: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||
}, a)
|
||||
|
||||
const network = a.network || BITCOIN_NETWORK
|
||||
const o = <Payment>{ network }
|
||||
|
||||
lazy.prop(o, 'output', function () {
|
||||
if (!a.data) return
|
||||
return bscript.compile((<Array<Buffer | number>>[OPS.OP_RETURN]).concat(a.data))
|
||||
})
|
||||
lazy.prop(o, 'data', function () {
|
||||
if (!a.output) return
|
||||
return (<Array<Buffer | number>>bscript.decompile(a.output)).slice(1)
|
||||
})
|
||||
|
||||
// extended validation
|
||||
if (opts.validate) {
|
||||
if (a.output) {
|
||||
const chunks = bscript.decompile(a.output)
|
||||
if ((<Array<Buffer | number>>chunks)[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid')
|
||||
if (!(<Array<Buffer | number>>chunks).slice(1).every(typef.Buffer)) throw new TypeError('Output is invalid')
|
||||
|
||||
if (a.data && !stacksEqual(a.data, <Array<Buffer>>o.data)) throw new TypeError('Data mismatch')
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(o, a)
|
||||
}
|
35
ts_src/payments/index.ts
Normal file
35
ts_src/payments/index.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { Network } from '../networks' // eslint-disable-line
|
||||
import { p2data as embed } from './embed'
|
||||
import { p2ms } from './p2ms'
|
||||
import { p2pk } from './p2pk'
|
||||
import { p2pkh } from './p2pkh'
|
||||
import { p2sh } from './p2sh'
|
||||
import { p2wpkh } from './p2wpkh'
|
||||
import { p2wsh } from './p2wsh'
|
||||
|
||||
export interface Payment {
|
||||
network?: Network,
|
||||
output?: Buffer,
|
||||
data?: Array<Buffer>,
|
||||
m?: number,
|
||||
n?: number,
|
||||
pubkeys?: Array<Buffer>,
|
||||
input?: Buffer,
|
||||
signatures?: Array<Buffer>,
|
||||
pubkey?: Buffer,
|
||||
signature?: Buffer,
|
||||
address?: string,
|
||||
hash?: Buffer,
|
||||
redeem?: Payment,
|
||||
witness?: Array<Buffer>,
|
||||
}
|
||||
|
||||
export interface PaymentOpts {
|
||||
validate?: boolean,
|
||||
allowIncomplete?: boolean,
|
||||
}
|
||||
|
||||
export { embed, p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh }
|
||||
|
||||
// TODO
|
||||
// witness commitment
|
28
ts_src/payments/lazy.ts
Normal file
28
ts_src/payments/lazy.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
export function prop (object: Object, name: string, f: ()=>any): void {
|
||||
Object.defineProperty(object, name, {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
let value = f.call(this)
|
||||
this[name] = value
|
||||
return value
|
||||
},
|
||||
set: function (value) {
|
||||
Object.defineProperty(this, name, {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: value,
|
||||
writable: true
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function value <T> (f: ()=>T): ()=>T {
|
||||
let value: T
|
||||
return function (): T {
|
||||
if (value !== undefined) return value
|
||||
value = f()
|
||||
return value
|
||||
}
|
||||
}
|
141
ts_src/payments/p2ms.ts
Normal file
141
ts_src/payments/p2ms.ts
Normal file
|
@ -0,0 +1,141 @@
|
|||
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||||
import * as bscript from '../script'
|
||||
import * as lazy from './lazy'
|
||||
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
||||
const OPS = bscript.OPS
|
||||
const typef = require('typeforce')
|
||||
const ecc = require('tiny-secp256k1')
|
||||
|
||||
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
||||
|
||||
function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||
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
|
||||
export function p2ms (a: Payment, opts?: PaymentOpts): Payment {
|
||||
if (
|
||||
!a.input &&
|
||||
!a.output &&
|
||||
!(a.pubkeys && a.m !== undefined) &&
|
||||
!a.signatures
|
||||
) throw new TypeError('Not enough data')
|
||||
opts = Object.assign({ validate: true }, opts || {})
|
||||
|
||||
function isAcceptableSignature (x: Buffer | number) {
|
||||
return bscript.isCanonicalScriptSignature(<Buffer>x) ||
|
||||
((<PaymentOpts>opts).allowIncomplete &&
|
||||
(<number> x === OPS.OP_0)) !== undefined // eslint-disable-line
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
const network = a.network || BITCOIN_NETWORK
|
||||
const o: Payment = { network }
|
||||
|
||||
let chunks: Array<Buffer | number> = []
|
||||
let decoded = false
|
||||
function decode (output: Buffer | Array<Buffer | number>): void {
|
||||
if (decoded) return
|
||||
decoded = true
|
||||
chunks = <Array<Buffer | number>>bscript.decompile(output)
|
||||
o.m = <number> chunks[0] - OP_INT_BASE // eslint-disable-line
|
||||
o.n = <number> chunks[chunks.length - 2] - OP_INT_BASE // eslint-disable-line
|
||||
o.pubkeys = <Array<Buffer>>chunks.slice(1, -2)
|
||||
}
|
||||
|
||||
lazy.prop(o, 'output', function () {
|
||||
if (!a.m) return
|
||||
if (!o.n) return
|
||||
if (!a.pubkeys) return
|
||||
return bscript.compile((<Array<Buffer | number>>[]).concat(
|
||||
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
|
||||
return (<Array<Buffer | number>>bscript.decompile(a.input)).slice(1)
|
||||
})
|
||||
lazy.prop(o, 'input', function () {
|
||||
if (!a.signatures) return
|
||||
return bscript.compile((<Array<Buffer | number>>[OPS.OP_0]).concat(a.signatures))
|
||||
})
|
||||
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 (
|
||||
<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
|
||||
o.n !== chunks.length - 3) throw new TypeError('Output is invalid')
|
||||
if (!(<Array<Buffer>>o.pubkeys).every(x => ecc.isPoint(x))) 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, (<Array<Buffer>>o.pubkeys))) throw new TypeError('Pubkeys mismatch')
|
||||
}
|
||||
|
||||
if (a.pubkeys) {
|
||||
if (a.n !== undefined && a.n !== a.pubkeys.length) throw new TypeError('Pubkey count mismatch')
|
||||
o.n = a.pubkeys.length
|
||||
|
||||
if (o.n < <number>(<Payment>o).m) throw new TypeError('Pubkey count cannot be less than m')
|
||||
}
|
||||
|
||||
if (a.signatures) {
|
||||
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')
|
||||
}
|
||||
|
||||
if (a.input) {
|
||||
if (a.input[0] !== OPS.OP_0) throw new TypeError('Input is invalid')
|
||||
if ((<Array<Buffer>>o.signatures).length === 0 || !(<Array<Buffer>>o.signatures).every(isAcceptableSignature)) throw new TypeError('Input has invalid signature(s)')
|
||||
|
||||
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')
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(o, a)
|
||||
}
|
78
ts_src/payments/p2pk.ts
Normal file
78
ts_src/payments/p2pk.ts
Normal file
|
@ -0,0 +1,78 @@
|
|||
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||||
import * as bscript from '../script'
|
||||
import * as lazy from './lazy'
|
||||
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
||||
const typef = require('typeforce')
|
||||
const OPS = bscript.OPS
|
||||
const ecc = require('tiny-secp256k1')
|
||||
|
||||
// input: {signature}
|
||||
// output: {pubKey} OP_CHECKSIG
|
||||
export function p2pk (a: Payment, opts?: PaymentOpts): Payment {
|
||||
if (
|
||||
!a.input &&
|
||||
!a.output &&
|
||||
!a.pubkey &&
|
||||
!a.input &&
|
||||
!a.signature
|
||||
) throw new TypeError('Not enough data')
|
||||
opts = Object.assign({ validate: true }, opts || {})
|
||||
|
||||
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)
|
||||
|
||||
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>a.input) })
|
||||
|
||||
const network = a.network || BITCOIN_NETWORK
|
||||
const o: Payment = { network }
|
||||
|
||||
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
|
||||
return <Buffer>_chunks()[0]
|
||||
})
|
||||
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')
|
||||
if (a.pubkey && !a.pubkey.equals(<Buffer>o.pubkey)) throw new TypeError('Pubkey mismatch')
|
||||
}
|
||||
|
||||
if (a.signature) {
|
||||
if (a.input && !a.input.equals(<Buffer>o.input)) throw new TypeError('Signature mismatch')
|
||||
}
|
||||
|
||||
if (a.input) {
|
||||
if (_chunks().length !== 1) throw new TypeError('Input is invalid')
|
||||
if (!bscript.isCanonicalScriptSignature(<Buffer>o.signature)) throw new TypeError('Input has invalid signature')
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(o, a)
|
||||
}
|
136
ts_src/payments/p2pkh.ts
Normal file
136
ts_src/payments/p2pkh.ts
Normal file
|
@ -0,0 +1,136 @@
|
|||
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||||
import * as bscript from '../script'
|
||||
import * as bcrypto from '../crypto'
|
||||
import * as lazy from './lazy'
|
||||
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
||||
const typef = require('typeforce')
|
||||
const OPS = bscript.OPS
|
||||
const ecc = require('tiny-secp256k1')
|
||||
|
||||
const bs58check = require('bs58check')
|
||||
|
||||
// input: {signature} {pubkey}
|
||||
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
|
||||
export function p2pkh (a: Payment, opts?: PaymentOpts): Payment {
|
||||
if (
|
||||
!a.address &&
|
||||
!a.hash &&
|
||||
!a.output &&
|
||||
!a.pubkey &&
|
||||
!a.input
|
||||
) throw new TypeError('Not enough data')
|
||||
opts = Object.assign({ validate: true }, opts || {})
|
||||
|
||||
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)
|
||||
|
||||
const _address = lazy.value(function () {
|
||||
const payload = bs58check.decode(a.address)
|
||||
const version = payload.readUInt8(0)
|
||||
const hash = payload.slice(1)
|
||||
return { version, hash }
|
||||
})
|
||||
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>a.input) })
|
||||
|
||||
const network = a.network || BITCOIN_NETWORK
|
||||
const o: Payment = { network }
|
||||
|
||||
lazy.prop(o, 'address', function () {
|
||||
if (!o.hash) return
|
||||
|
||||
const payload = Buffer.allocUnsafe(21)
|
||||
payload.writeUInt8(network.pubKeyHash, 0)
|
||||
o.hash.copy(payload, 1)
|
||||
return bs58check.encode(payload)
|
||||
})
|
||||
lazy.prop(o, 'hash', function () {
|
||||
if (a.output) return a.output.slice(3, 23)
|
||||
if (a.address) return _address().hash
|
||||
if (a.pubkey || o.pubkey) return bcrypto.hash160(<Buffer> a.pubkey || <Buffer>o.pubkey) // eslint-disable-line
|
||||
})
|
||||
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
|
||||
return <Buffer>_chunks()[1]
|
||||
})
|
||||
lazy.prop(o, 'signature', function () {
|
||||
if (!a.input) return
|
||||
return <Buffer>_chunks()[0]
|
||||
})
|
||||
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) {
|
||||
let hash: Buffer = Buffer.from([])
|
||||
if (a.address) {
|
||||
if (_address().version !== network.pubKeyHash) throw new TypeError('Invalid version or Network mismatch')
|
||||
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
|
||||
hash = _address().hash
|
||||
}
|
||||
|
||||
if (a.hash) {
|
||||
if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
|
||||
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')
|
||||
|
||||
const hash2 = a.output.slice(3, 23)
|
||||
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
||||
else hash = hash2
|
||||
}
|
||||
|
||||
if (a.pubkey) {
|
||||
const pkh = bcrypto.hash160(a.pubkey)
|
||||
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||
else hash = pkh
|
||||
}
|
||||
|
||||
if (a.input) {
|
||||
const chunks = _chunks()
|
||||
if (chunks.length !== 2) throw new TypeError('Input is invalid')
|
||||
if (!bscript.isCanonicalScriptSignature(<Buffer>chunks[0])) throw new TypeError('Input has invalid signature')
|
||||
if (!ecc.isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey')
|
||||
|
||||
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')
|
||||
|
||||
const pkh = bcrypto.hash160(<Buffer>chunks[1])
|
||||
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(o, a)
|
||||
}
|
192
ts_src/payments/p2sh.ts
Normal file
192
ts_src/payments/p2sh.ts
Normal file
|
@ -0,0 +1,192 @@
|
|||
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||||
import { Network, bitcoin as BITCOIN_NETWORK } from '../networks' // eslint-disable-line
|
||||
import * as bscript from '../script'
|
||||
import * as bcrypto from '../crypto'
|
||||
import * as lazy from './lazy'
|
||||
const typef = require('typeforce')
|
||||
const OPS = bscript.OPS
|
||||
|
||||
const bs58check = require('bs58check')
|
||||
|
||||
function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||
if (a.length !== b.length) return false
|
||||
|
||||
return a.every(function (x, i) {
|
||||
return x.equals(b[i])
|
||||
})
|
||||
}
|
||||
|
||||
// input: [redeemScriptSig ...] {redeemScript}
|
||||
// witness: <?>
|
||||
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
|
||||
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),
|
||||
|
||||
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),
|
||||
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||
}, a)
|
||||
|
||||
let network = a.network
|
||||
if (!network) {
|
||||
network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK
|
||||
}
|
||||
|
||||
const o: Payment = { network }
|
||||
|
||||
const _address = lazy.value(function () {
|
||||
const payload = bs58check.decode(a.address)
|
||||
const version = payload.readUInt8(0)
|
||||
const hash = payload.slice(1)
|
||||
return { version, hash }
|
||||
})
|
||||
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>a.input) })
|
||||
const _redeem = lazy.value(function (): Payment {
|
||||
const chunks = _chunks()
|
||||
return {
|
||||
network,
|
||||
output: <Buffer>chunks[chunks.length - 1],
|
||||
input: bscript.compile(chunks.slice(0, -1)),
|
||||
witness: a.witness || []
|
||||
}
|
||||
})
|
||||
|
||||
// output dependents
|
||||
lazy.prop(o, 'address', function () {
|
||||
if (!o.hash) return
|
||||
|
||||
const payload = Buffer.allocUnsafe(21)
|
||||
payload.writeUInt8((<Network>o.network).scriptHash, 0)
|
||||
o.hash.copy(payload, 1)
|
||||
return bs58check.encode(payload)
|
||||
})
|
||||
lazy.prop(o, 'hash', function () {
|
||||
// in order of least effort
|
||||
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)
|
||||
})
|
||||
lazy.prop(o, 'output', function () {
|
||||
if (!o.hash) return
|
||||
return bscript.compile([
|
||||
OPS.OP_HASH160,
|
||||
o.hash,
|
||||
OPS.OP_EQUAL
|
||||
])
|
||||
})
|
||||
|
||||
// input dependents
|
||||
lazy.prop(o, 'redeem', function () {
|
||||
if (!a.input) return
|
||||
return _redeem()
|
||||
})
|
||||
lazy.prop(o, 'input', function () {
|
||||
if (!a.redeem || !a.redeem.input || !a.redeem.output) return
|
||||
return bscript.compile((<Array<Buffer | number>>[]).concat(
|
||||
<Array<Buffer | number>>bscript.decompile(a.redeem.input),
|
||||
a.redeem.output
|
||||
))
|
||||
})
|
||||
lazy.prop(o, 'witness', function () {
|
||||
if (o.redeem && o.redeem.witness) return o.redeem.witness
|
||||
if (o.input) return []
|
||||
})
|
||||
|
||||
if (opts.validate) {
|
||||
let hash: Buffer = Buffer.from([])
|
||||
if (a.address) {
|
||||
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) {
|
||||
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 ||
|
||||
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
|
||||
const checkRedeem = function (redeem: Payment): void {
|
||||
// is the redeem output empty/invalid?
|
||||
if (redeem.output) {
|
||||
const decompile = bscript.decompile(redeem.output)
|
||||
if (!decompile || decompile.length < 1) throw new TypeError('Redeem.output too short')
|
||||
|
||||
// match hash against other sources
|
||||
const hash2 = bcrypto.hash160(redeem.output)
|
||||
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
||||
else hash = hash2
|
||||
}
|
||||
|
||||
if (redeem.input) {
|
||||
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) {
|
||||
const richunks = <Array<Buffer | number>>bscript.decompile(redeem.input)
|
||||
if (!bscript.isPushOnly(richunks)) throw new TypeError('Non push-only scriptSig')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (a.input) {
|
||||
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')
|
||||
|
||||
checkRedeem(_redeem())
|
||||
}
|
||||
|
||||
if (a.redeem) {
|
||||
if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch')
|
||||
if (a.input) {
|
||||
const redeem = _redeem()
|
||||
if (a.redeem.output && !a.redeem.output.equals(<Buffer>redeem.output)) throw new TypeError('Redeem.output mismatch')
|
||||
if (a.redeem.input && !a.redeem.input.equals(<Buffer>redeem.input)) throw new TypeError('Redeem.input mismatch')
|
||||
}
|
||||
|
||||
checkRedeem(a.redeem)
|
||||
}
|
||||
|
||||
if (a.witness) {
|
||||
if (
|
||||
a.redeem &&
|
||||
a.redeem.witness &&
|
||||
!stacksEqual(a.redeem.witness, a.witness)) throw new TypeError('Witness and redeem.witness mismatch')
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(o, a)
|
||||
}
|
134
ts_src/payments/p2wpkh.ts
Normal file
134
ts_src/payments/p2wpkh.ts
Normal file
|
@ -0,0 +1,134 @@
|
|||
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||||
import * as bscript from '../script'
|
||||
import * as bcrypto from '../crypto'
|
||||
import * as lazy from './lazy'
|
||||
import { bitcoin as BITCOIN_NETWORK } from '../networks'
|
||||
const typef = require('typeforce')
|
||||
const OPS = bscript.OPS
|
||||
const ecc = require('tiny-secp256k1')
|
||||
|
||||
const bech32 = require('bech32')
|
||||
|
||||
const EMPTY_BUFFER = Buffer.alloc(0)
|
||||
|
||||
// witness: {signature} {pubKey}
|
||||
// input: <>
|
||||
// output: OP_0 {pubKeyHash}
|
||||
export function p2wpkh (a: Payment, opts?: PaymentOpts): Payment {
|
||||
if (
|
||||
!a.address &&
|
||||
!a.hash &&
|
||||
!a.output &&
|
||||
!a.pubkey &&
|
||||
!a.witness
|
||||
) throw new TypeError('Not enough data')
|
||||
opts = Object.assign({ validate: true }, opts || {})
|
||||
|
||||
typef({
|
||||
address: typef.maybe(typef.String),
|
||||
hash: typef.maybe(typef.BufferN(20)),
|
||||
input: typef.maybe(typef.BufferN(0)),
|
||||
network: typef.maybe(typef.Object),
|
||||
output: typef.maybe(typef.BufferN(22)),
|
||||
pubkey: typef.maybe(ecc.isPoint),
|
||||
signature: typef.maybe(bscript.isCanonicalScriptSignature),
|
||||
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||
}, a)
|
||||
|
||||
const _address = lazy.value(function () {
|
||||
const result = bech32.decode(a.address)
|
||||
const version = result.words.shift()
|
||||
const data = bech32.fromWords(result.words)
|
||||
return {
|
||||
version,
|
||||
prefix: result.prefix,
|
||||
data: Buffer.from(data)
|
||||
}
|
||||
})
|
||||
|
||||
const network = a.network || BITCOIN_NETWORK
|
||||
const o: Payment = { network }
|
||||
|
||||
lazy.prop(o, 'address', function () {
|
||||
if (!o.hash) return
|
||||
|
||||
const words = bech32.toWords(o.hash)
|
||||
words.unshift(0x00)
|
||||
return bech32.encode(network.bech32, words)
|
||||
})
|
||||
lazy.prop(o, 'hash', function () {
|
||||
if (a.output) return a.output.slice(2, 22)
|
||||
if (a.address) return _address().data
|
||||
if (a.pubkey || o.pubkey) return bcrypto.hash160(<Buffer> a.pubkey || <Buffer>o.pubkey) // eslint-disable-line
|
||||
})
|
||||
lazy.prop(o, 'output', function () {
|
||||
if (!o.hash) return
|
||||
return bscript.compile([
|
||||
OPS.OP_0,
|
||||
o.hash
|
||||
])
|
||||
})
|
||||
lazy.prop(o, 'pubkey', function () {
|
||||
if (a.pubkey) return a.pubkey
|
||||
if (!a.witness) return
|
||||
return a.witness[1]
|
||||
})
|
||||
lazy.prop(o, 'signature', function () {
|
||||
if (!a.witness) return
|
||||
return a.witness[0]
|
||||
})
|
||||
lazy.prop(o, 'input', function () {
|
||||
if (!o.witness) return
|
||||
return EMPTY_BUFFER
|
||||
})
|
||||
lazy.prop(o, 'witness', function () {
|
||||
if (!a.pubkey) return
|
||||
if (!a.signature) return
|
||||
return [a.signature, a.pubkey]
|
||||
})
|
||||
|
||||
// extended validation
|
||||
if (opts.validate) {
|
||||
let hash: Buffer = Buffer.from([])
|
||||
if (a.address) {
|
||||
if (network && network.bech32 !== _address().prefix) throw new TypeError('Invalid prefix or Network mismatch')
|
||||
if (_address().version !== 0x00) throw new TypeError('Invalid address version')
|
||||
if (_address().data.length !== 20) throw new TypeError('Invalid address data')
|
||||
hash = _address().data
|
||||
}
|
||||
|
||||
if (a.hash) {
|
||||
if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
|
||||
else hash = a.hash
|
||||
}
|
||||
|
||||
if (a.output) {
|
||||
if (
|
||||
a.output.length !== 22 ||
|
||||
a.output[0] !== OPS.OP_0 ||
|
||||
a.output[1] !== 0x14) throw new TypeError('Output is invalid')
|
||||
if (hash.length > 0 && !hash.equals(a.output.slice(2))) throw new TypeError('Hash mismatch')
|
||||
else hash = a.output.slice(2)
|
||||
}
|
||||
|
||||
if (a.pubkey) {
|
||||
const pkh = bcrypto.hash160(a.pubkey)
|
||||
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||
else hash = pkh
|
||||
}
|
||||
|
||||
if (a.witness) {
|
||||
if (a.witness.length !== 2) throw new TypeError('Witness is invalid')
|
||||
if (!bscript.isCanonicalScriptSignature(a.witness[0])) throw new TypeError('Witness has invalid signature')
|
||||
if (!ecc.isPoint(a.witness[1])) throw new TypeError('Witness has invalid pubkey')
|
||||
|
||||
if (a.signature && !a.signature.equals(a.witness[0])) throw new TypeError('Signature mismatch')
|
||||
if (a.pubkey && !a.pubkey.equals(a.witness[1])) throw new TypeError('Pubkey mismatch')
|
||||
|
||||
const pkh = bcrypto.hash160(a.witness[1])
|
||||
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(o, a)
|
||||
}
|
179
ts_src/payments/p2wsh.ts
Normal file
179
ts_src/payments/p2wsh.ts
Normal file
|
@ -0,0 +1,179 @@
|
|||
import { Payment, PaymentOpts } from './index' // eslint-disable-line
|
||||
import { Network, bitcoin as BITCOIN_NETWORK } from '../networks' // eslint-disable-line
|
||||
import * as bscript from '../script'
|
||||
import * as bcrypto from '../crypto'
|
||||
import * as lazy from './lazy'
|
||||
const typef = require('typeforce')
|
||||
const OPS = bscript.OPS
|
||||
|
||||
const bech32 = require('bech32')
|
||||
|
||||
const EMPTY_BUFFER = Buffer.alloc(0)
|
||||
|
||||
function stacksEqual (a: Array<Buffer>, b: Array<Buffer>): boolean {
|
||||
if (a.length !== b.length) return false
|
||||
|
||||
return a.every(function (x, i) {
|
||||
return x.equals(b[i])
|
||||
})
|
||||
}
|
||||
|
||||
// input: <>
|
||||
// witness: [redeemScriptSig ...] {redeemScript}
|
||||
// output: OP_0 {sha256(redeemScript)}
|
||||
export function p2wsh (a: Payment, opts?: PaymentOpts): Payment {
|
||||
if (
|
||||
!a.address &&
|
||||
!a.hash &&
|
||||
!a.output &&
|
||||
!a.redeem &&
|
||||
!a.witness
|
||||
) throw new TypeError('Not enough data')
|
||||
opts = Object.assign({ validate: true }, opts || {})
|
||||
|
||||
typef({
|
||||
network: typef.maybe(typef.Object),
|
||||
|
||||
address: typef.maybe(typef.String),
|
||||
hash: typef.maybe(typef.BufferN(32)),
|
||||
output: typef.maybe(typef.BufferN(34)),
|
||||
|
||||
redeem: typef.maybe({
|
||||
input: typef.maybe(typef.Buffer),
|
||||
network: typef.maybe(typef.Object),
|
||||
output: typef.maybe(typef.Buffer),
|
||||
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||
}),
|
||||
input: typef.maybe(typef.BufferN(0)),
|
||||
witness: typef.maybe(typef.arrayOf(typef.Buffer))
|
||||
}, a)
|
||||
|
||||
const _address = lazy.value(function () {
|
||||
const result = bech32.decode(a.address)
|
||||
const version = result.words.shift()
|
||||
const data = bech32.fromWords(result.words)
|
||||
return {
|
||||
version,
|
||||
prefix: result.prefix,
|
||||
data: Buffer.from(data)
|
||||
}
|
||||
})
|
||||
const _rchunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>(<Payment>a.redeem).input) })
|
||||
|
||||
let network = a.network
|
||||
if (!network) {
|
||||
network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK
|
||||
}
|
||||
|
||||
const o: Payment = { network }
|
||||
|
||||
lazy.prop(o, 'address', function () {
|
||||
if (!o.hash) return
|
||||
const words = bech32.toWords(o.hash)
|
||||
words.unshift(0x00)
|
||||
return bech32.encode((<Network>network).bech32, words)
|
||||
})
|
||||
lazy.prop(o, 'hash', function () {
|
||||
if (a.output) return a.output.slice(2)
|
||||
if (a.address) return _address().data
|
||||
if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output)
|
||||
})
|
||||
lazy.prop(o, 'output', function () {
|
||||
if (!o.hash) return
|
||||
return bscript.compile([
|
||||
OPS.OP_0,
|
||||
o.hash
|
||||
])
|
||||
})
|
||||
lazy.prop(o, 'redeem', function () {
|
||||
if (!a.witness) return
|
||||
return {
|
||||
output: a.witness[a.witness.length - 1],
|
||||
input: EMPTY_BUFFER,
|
||||
witness: a.witness.slice(0, -1)
|
||||
}
|
||||
})
|
||||
lazy.prop(o, 'input', function () {
|
||||
if (!o.witness) return
|
||||
return EMPTY_BUFFER
|
||||
})
|
||||
lazy.prop(o, 'witness', function () {
|
||||
// transform redeem input to witness stack?
|
||||
if (
|
||||
a.redeem &&
|
||||
a.redeem.input &&
|
||||
a.redeem.input.length > 0 &&
|
||||
a.redeem.output &&
|
||||
a.redeem.output.length > 0
|
||||
) {
|
||||
const stack = bscript.toStack(_rchunks())
|
||||
|
||||
// assign, and blank the existing input
|
||||
o.redeem = Object.assign({ witness: stack }, a.redeem)
|
||||
o.redeem.input = EMPTY_BUFFER
|
||||
return (<Array<Buffer>>[]).concat(stack, a.redeem.output)
|
||||
}
|
||||
|
||||
if (!a.redeem) return
|
||||
if (!a.redeem.output) return
|
||||
if (!a.redeem.witness) return
|
||||
return (<Array<Buffer>>[]).concat(a.redeem.witness, a.redeem.output)
|
||||
})
|
||||
|
||||
// extended validation
|
||||
if (opts.validate) {
|
||||
let hash: Buffer = Buffer.from([])
|
||||
if (a.address) {
|
||||
if (_address().prefix !== network.bech32) throw new TypeError('Invalid prefix or Network mismatch')
|
||||
if (_address().version !== 0x00) throw new TypeError('Invalid address version')
|
||||
if (_address().data.length !== 32) throw new TypeError('Invalid address data')
|
||||
hash = _address().data
|
||||
}
|
||||
|
||||
if (a.hash) {
|
||||
if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch')
|
||||
else hash = a.hash
|
||||
}
|
||||
|
||||
if (a.output) {
|
||||
if (
|
||||
a.output.length !== 34 ||
|
||||
a.output[0] !== OPS.OP_0 ||
|
||||
a.output[1] !== 0x20) throw new TypeError('Output is invalid')
|
||||
const hash2 = a.output.slice(2)
|
||||
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
||||
else hash = hash2
|
||||
}
|
||||
|
||||
if (a.redeem) {
|
||||
if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch')
|
||||
|
||||
// is there two redeem sources?
|
||||
if (
|
||||
a.redeem.input &&
|
||||
a.redeem.input.length > 0 &&
|
||||
a.redeem.witness &&
|
||||
a.redeem.witness.length > 0
|
||||
) throw new TypeError('Ambiguous witness source')
|
||||
|
||||
// is the redeem output non-empty?
|
||||
if (a.redeem.output) {
|
||||
if ((<Array<Buffer | number>>bscript.decompile(a.redeem.output)).length === 0) throw new TypeError('Redeem.output is invalid')
|
||||
|
||||
// match hash against other sources
|
||||
const hash2 = bcrypto.sha256(a.redeem.output)
|
||||
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
|
||||
else hash = hash2
|
||||
}
|
||||
|
||||
if (a.redeem.input && !bscript.isPushOnly(_rchunks())) throw new TypeError('Non push-only scriptSig')
|
||||
if (a.witness && a.redeem.witness && !stacksEqual(a.witness, a.redeem.witness)) throw new TypeError('Witness and redeem.witness mismatch')
|
||||
}
|
||||
|
||||
if (a.witness) {
|
||||
if (a.redeem && a.redeem.output && !a.redeem.output.equals(a.witness[a.witness.length - 1])) throw new TypeError('Witness and redeem.output mismatch')
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(o, a)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue