diff --git a/src/payments/p2pkh.js b/src/payments/p2pkh.js index 443464a..55f4817 100644 --- a/src/payments/p2pkh.js +++ b/src/payments/p2pkh.js @@ -50,7 +50,7 @@ function p2pkh(a, opts) { if (a.address) return _address().hash; if (a.pubkey || o.pubkey) - return bcrypto.hash160(a.pubkey || o.pubkey); // eslint-disable-line + return bcrypto.hash160(a.pubkey || o.pubkey); }); lazy.prop(o, 'output', function () { if (!o.hash) diff --git a/src/payments/p2wpkh.js b/src/payments/p2wpkh.js index 98951c6..7b269b7 100644 --- a/src/payments/p2wpkh.js +++ b/src/payments/p2wpkh.js @@ -55,7 +55,7 @@ function p2wpkh(a, opts) { if (a.address) return _address().data; if (a.pubkey || o.pubkey) - return bcrypto.hash160(a.pubkey || o.pubkey); // eslint-disable-line + return bcrypto.hash160(a.pubkey || o.pubkey); }); lazy.prop(o, 'output', function () { if (!o.hash) diff --git a/src/transaction.js b/src/transaction.js index 6619b79..34d7b40 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -369,13 +369,13 @@ class Transaction { offset += slice.copy(buffer, offset); } function writeUInt8(i) { - offset = buffer.writeUInt8(i, offset); + offset = (buffer).writeUInt8(i, offset); } function writeUInt32(i) { - offset = buffer.writeUInt32LE(i, offset); + offset = (buffer).writeUInt32LE(i, offset); } function writeInt32(i) { - offset = buffer.writeInt32LE(i, offset); + offset = (buffer).writeInt32LE(i, offset); } function writeUInt64(i) { offset = bufferutils.writeUInt64LE(buffer, i, offset); diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 2121156..1cdc6bf 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -270,7 +270,7 @@ class TransactionBuilder { return this.__inputs.every(input => { if (input.signatures === undefined) return true; - return input.signatures.every((signature => { + return input.signatures.every(signature => { if (!signature) return true; const hashType = signatureHashType(signature); @@ -283,7 +283,8 @@ class TransactionBuilder { // of more outputs return nInputs <= nOutputs; } - })); + return false; + }); }); } __overMaximumFees(bytes) { diff --git a/ts_src/block.ts b/ts_src/block.ts index 82700aa..cb50559 100644 --- a/ts_src/block.ts +++ b/ts_src/block.ts @@ -129,7 +129,7 @@ export class Block { if (transactions.length === 0) throw errorMerkleNoTxes if (forWitness && !txesHaveWitness(transactions)) throw errorWitnessNotSegwit - const hashes = transactions.map(transaction => transaction.getHash((forWitness))) + const hashes = transactions.map(transaction => transaction.getHash(forWitness!)) const rootHash = fastMerkleRoot(hashes, bcrypto.hash256) @@ -139,7 +139,7 @@ export class Block { } hasWitnessCommit (): boolean { - return txesHaveWitness(>this.transactions) + return txesHaveWitness(this.transactions!) } byteLength (headersOnly: boolean): number { @@ -184,8 +184,8 @@ export class Block { } writeInt32(this.version) - writeSlice(this.prevHash) - writeSlice(this.merkleRoot) + writeSlice(this.prevHash!) + writeSlice(this.merkleRoot!) writeUInt32(this.timestamp) writeUInt32(this.bits) writeUInt32(this.nonce) @@ -212,7 +212,7 @@ export class Block { if (!this.transactions) throw errorMerkleNoTxes const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions) - return (this.merkleRoot).compare(actualMerkleRoot) === 0 + return this.merkleRoot!.compare(actualMerkleRoot) === 0 } checkWitnessCommit (): boolean { @@ -220,7 +220,7 @@ export class Block { if (!this.hasWitnessCommit()) throw errorWitnessNotSegwit const actualWitnessCommit = Block.calculateMerkleRoot(this.transactions, true) - return (this.witnessCommit).compare(actualWitnessCommit) === 0 + return this.witnessCommit!.compare(actualWitnessCommit) === 0 } checkProofOfWork (): boolean { diff --git a/ts_src/payments/embed.ts b/ts_src/payments/embed.ts index 8cc8899..bfaeb8b 100644 --- a/ts_src/payments/embed.ts +++ b/ts_src/payments/embed.ts @@ -36,15 +36,15 @@ export function p2data (a: Payment, opts?: PaymentOpts): Payment { }) lazy.prop(o, 'data', function () { if (!a.output) return - return (>bscript.decompile(a.output)).slice(1) + return bscript.decompile(a.output)!.slice(1) }) // extended validation if (opts.validate) { if (a.output) { const chunks = bscript.decompile(a.output) - if ((>chunks)[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid') - if (!(>chunks).slice(1).every(typef.Buffer)) throw new TypeError('Output is invalid') + if (chunks![0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid') + if (!chunks!.slice(1).every(typef.Buffer)) throw new TypeError('Output is invalid') if (a.data && !stacksEqual(a.data, >o.data)) throw new TypeError('Data mismatch') } diff --git a/ts_src/payments/p2ms.ts b/ts_src/payments/p2ms.ts index 67b9692..34e2d31 100644 --- a/ts_src/payments/p2ms.ts +++ b/ts_src/payments/p2ms.ts @@ -29,7 +29,7 @@ export function p2ms (a: Payment, opts?: PaymentOpts): Payment { function isAcceptableSignature (x: Buffer | number) { return bscript.isCanonicalScriptSignature(x) || - ((opts).allowIncomplete && + (opts!.allowIncomplete && ( x === OPS.OP_0)) !== undefined // eslint-disable-line } @@ -85,7 +85,7 @@ export function p2ms (a: Payment, opts?: PaymentOpts): Payment { }) lazy.prop(o, 'signatures', function () { if (!a.input) return - return (>bscript.decompile(a.input)).slice(1) + return bscript.decompile(a.input)!.slice(1) }) lazy.prop(o, 'input', function () { if (!a.signatures) return @@ -105,35 +105,35 @@ export function p2ms (a: Payment, opts?: PaymentOpts): Payment { if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) throw new TypeError('Output is invalid') if ( - (o).m <= 0 || // eslint-disable-line - (o).n > 16 || // eslint-disable-line - (o).m > (o).n || // eslint-disable-line + o.m! <= 0 || // eslint-disable-line + o.n! > 16 || // eslint-disable-line + o.m! > o.n! || // eslint-disable-line o.n !== chunks.length - 3) throw new TypeError('Output is invalid') - if (!(>o.pubkeys).every(x => ecc.isPoint(x))) throw new TypeError('Output is invalid') + if (!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, (>o.pubkeys))) throw new TypeError('Pubkeys mismatch') + if (a.pubkeys && !stacksEqual(a.pubkeys, 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 < (o).m) throw new TypeError('Pubkey count cannot be less than m') + if (o.n < o.m!) throw new TypeError('Pubkey count cannot be less than m') } if (a.signatures) { - 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') + 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') } if (a.input) { 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 (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') + 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') } } diff --git a/ts_src/payments/p2pk.ts b/ts_src/payments/p2pk.ts index 0825955..3385c56 100644 --- a/ts_src/payments/p2pk.ts +++ b/ts_src/payments/p2pk.ts @@ -27,7 +27,7 @@ export function p2pk (a: Payment, opts?: PaymentOpts): Payment { input: typef.maybe(typef.Buffer) }, a) - const _chunks = <()=>Array>lazy.value(function () { return bscript.decompile(a.input) }) + const _chunks = <()=>Array>lazy.value(function () { return bscript.decompile(a.input!) }) const network = a.network || BITCOIN_NETWORK const o: Payment = { network } @@ -61,16 +61,16 @@ export function p2pk (a: Payment, opts?: PaymentOpts): Payment { 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(o.pubkey)) throw new TypeError('Pubkey mismatch') + if (a.pubkey && !a.pubkey.equals(o.pubkey!)) throw new TypeError('Pubkey mismatch') } if (a.signature) { - if (a.input && !a.input.equals(o.input)) throw new TypeError('Signature mismatch') + if (a.input && !a.input.equals(o.input!)) throw new TypeError('Signature mismatch') } if (a.input) { if (_chunks().length !== 1) throw new TypeError('Input is invalid') - if (!bscript.isCanonicalScriptSignature(o.signature)) throw new TypeError('Input has invalid signature') + if (!bscript.isCanonicalScriptSignature(o.signature!)) throw new TypeError('Input has invalid signature') } } diff --git a/ts_src/payments/p2pkh.ts b/ts_src/payments/p2pkh.ts index f12faf8..ddb9cc3 100644 --- a/ts_src/payments/p2pkh.ts +++ b/ts_src/payments/p2pkh.ts @@ -38,7 +38,7 @@ export function p2pkh (a: Payment, opts?: PaymentOpts): Payment { const hash = payload.slice(1) return { version, hash } }) - const _chunks = <()=>Array>lazy.value(function () { return bscript.decompile(a.input) }) + const _chunks = <()=>Array>lazy.value(function () { return bscript.decompile(a.input!) }) const network = a.network || BITCOIN_NETWORK const o: Payment = { network } @@ -54,7 +54,7 @@ export function p2pkh (a: Payment, opts?: PaymentOpts): Payment { 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( a.pubkey || o.pubkey) // eslint-disable-line + if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!) }) lazy.prop(o, 'output', function () { if (!o.hash) return diff --git a/ts_src/payments/p2sh.ts b/ts_src/payments/p2sh.ts index e0292f3..1aa1a94 100644 --- a/ts_src/payments/p2sh.ts +++ b/ts_src/payments/p2sh.ts @@ -59,7 +59,7 @@ export function p2sh (a: Payment, opts?: PaymentOpts): Payment { const hash = payload.slice(1) return { version, hash } }) - const _chunks = <()=>Array>lazy.value(function () { return bscript.decompile(a.input) }) + const _chunks = <()=>Array>lazy.value(function () { return bscript.decompile(a.input!) }) const _redeem = lazy.value(function (): Payment { const chunks = _chunks() return { @@ -75,7 +75,7 @@ export function p2sh (a: Payment, opts?: PaymentOpts): Payment { if (!o.hash) return const payload = Buffer.allocUnsafe(21) - payload.writeUInt8((o.network).scriptHash, 0) + payload.writeUInt8(o.network!.scriptHash, 0) o.hash.copy(payload, 1) return bs58check.encode(payload) }) @@ -173,8 +173,8 @@ export function p2sh (a: Payment, opts?: PaymentOpts): Payment { 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(redeem.output)) throw new TypeError('Redeem.output mismatch') - if (a.redeem.input && !a.redeem.input.equals(redeem.input)) throw new TypeError('Redeem.input mismatch') + if (a.redeem.output && !a.redeem.output.equals(redeem.output!)) throw new TypeError('Redeem.output mismatch') + if (a.redeem.input && !a.redeem.input.equals(redeem.input!)) throw new TypeError('Redeem.input mismatch') } checkRedeem(a.redeem) diff --git a/ts_src/payments/p2wpkh.ts b/ts_src/payments/p2wpkh.ts index b40085b..6cfecc4 100644 --- a/ts_src/payments/p2wpkh.ts +++ b/ts_src/payments/p2wpkh.ts @@ -59,7 +59,7 @@ export function p2wpkh (a: Payment, opts?: PaymentOpts): Payment { 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( a.pubkey || o.pubkey) // eslint-disable-line + if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!) }) lazy.prop(o, 'output', function () { if (!o.hash) return diff --git a/ts_src/payments/p2wsh.ts b/ts_src/payments/p2wsh.ts index 7e2f237..452dcc3 100644 --- a/ts_src/payments/p2wsh.ts +++ b/ts_src/payments/p2wsh.ts @@ -58,7 +58,7 @@ export function p2wsh (a: Payment, opts?: PaymentOpts): Payment { data: Buffer.from(data) } }) - const _rchunks = <()=>Array>lazy.value(function () { return bscript.decompile((a.redeem).input) }) + const _rchunks = <()=>Array>lazy.value(function () { return bscript.decompile(a.redeem!.input!) }) let network = a.network if (!network) { @@ -71,7 +71,7 @@ export function p2wsh (a: Payment, opts?: PaymentOpts): Payment { if (!o.hash) return const words = bech32.toWords(o.hash) words.unshift(0x00) - return bech32.encode((network).bech32, words) + return bech32.encode(network!.bech32, words) }) lazy.prop(o, 'hash', function () { if (a.output) return a.output.slice(2) @@ -158,7 +158,7 @@ export function p2wsh (a: Payment, opts?: PaymentOpts): Payment { // is the redeem output non-empty? if (a.redeem.output) { - if ((>bscript.decompile(a.redeem.output)).length === 0) throw new TypeError('Redeem.output is invalid') + if (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) diff --git a/ts_src/script.ts b/ts_src/script.ts index 79e3342..64b74ad 100644 --- a/ts_src/script.ts +++ b/ts_src/script.ts @@ -125,7 +125,7 @@ export function decompile (buffer: Buffer | Array): Arrayop) + chunks.push(op) } else { chunks.push(data) } diff --git a/ts_src/templates/scripthash/input.ts b/ts_src/templates/scripthash/input.ts index 90af0fe..7b3107d 100644 --- a/ts_src/templates/scripthash/input.ts +++ b/ts_src/templates/scripthash/input.ts @@ -10,14 +10,14 @@ import * as p2wsho from '../witnessscripthash/output' export function check (script: Buffer | Array, allowIncomplete?: boolean): boolean { - const chunks = >bscript.decompile(script) + const chunks = bscript.decompile(script)! if (chunks.length < 1) return false const lastChunk = chunks[chunks.length - 1] if (!Buffer.isBuffer(lastChunk)) return false - const scriptSigChunks = >bscript.decompile(bscript.compile(chunks.slice(0, -1))) - const redeemScriptChunks = bscript.decompile(lastChunk) + const scriptSigChunks = bscript.decompile(bscript.compile(chunks.slice(0, -1)))! + const redeemScriptChunks = bscript.decompile(lastChunk) // is redeemScript a valid script? if (!redeemScriptChunks) return false diff --git a/ts_src/templates/witnesscommitment/output.ts b/ts_src/templates/witnesscommitment/output.ts index 71e8df3..0bdf11c 100644 --- a/ts_src/templates/witnesscommitment/output.ts +++ b/ts_src/templates/witnesscommitment/output.ts @@ -32,5 +32,5 @@ export function encode (commitment: Buffer): Buffer { export function decode (buffer: Buffer): Buffer { typeforce(check, buffer) - return ((>bscript.decompile(buffer))[1]).slice(4, 36) + return (bscript.decompile(buffer)![1]).slice(4, 36) } diff --git a/ts_src/transaction.ts b/ts_src/transaction.ts index 29b49c8..e6e1d24 100644 --- a/ts_src/transaction.ts +++ b/ts_src/transaction.ts @@ -294,8 +294,8 @@ export class Transaction { if (inIndex >= this.ins.length) return ONE // ignore OP_CODESEPARATOR - const ourScript = bscript.compile((>bscript.decompile(prevOutScript)).filter((x) => { - return x !== opcodes.OP_CODESEPARATOR + const ourScript = bscript.compile(bscript.decompile(prevOutScript)!.filter((x) => { + return x !== opcodes.OP_CODESEPARATOR })) const txTmp = this.clone() @@ -471,28 +471,28 @@ export class Transaction { } private __toBuffer (buffer?: Buffer, initialOffset?: number, __allowWitness?: boolean): Buffer { - if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength((__allowWitness))) + if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness!)) let offset = initialOffset || 0 function writeSlice (slice: Buffer): void { - offset += slice.copy(buffer, offset) + offset += slice.copy(buffer!, offset) } function writeUInt8 (i: number) { - offset = (buffer).writeUInt8(i, offset) + offset = (buffer!).writeUInt8(i, offset) } function writeUInt32 (i: number) { - offset = (buffer).writeUInt32LE(i, offset) + offset = (buffer!).writeUInt32LE(i, offset) } function writeInt32 (i: number) { - offset = (buffer).writeInt32LE(i, offset) + offset = (buffer!).writeInt32LE(i, offset) } function writeUInt64 (i: number) { - offset = bufferutils.writeUInt64LE(buffer, i, offset) + offset = bufferutils.writeUInt64LE(buffer!, i, offset) } function writeVarInt (i: number) { @@ -548,7 +548,7 @@ export class Transaction { writeUInt32(this.locktime) // avoid slicing unless necessary - if (initialOffset !== undefined) return buffer.slice((initialOffset), offset) + if (initialOffset !== undefined) return buffer.slice(initialOffset, offset) return buffer } diff --git a/ts_src/transaction_builder.ts b/ts_src/transaction_builder.ts index 59fc102..969462c 100644 --- a/ts_src/transaction_builder.ts +++ b/ts_src/transaction_builder.ts @@ -50,11 +50,11 @@ interface TxbOutput { } function txIsString(tx: Buffer | string | Transaction): tx is string { - return typeof (tx) === 'string' || tx instanceof String + return typeof tx === 'string' || tx instanceof String } function txIsTransaction(tx: Buffer | string | Transaction): tx is Transaction { - return (tx) instanceof Transaction + return tx instanceof Transaction } export class TransactionBuilder { @@ -209,7 +209,7 @@ export class TransactionBuilder { scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network) } - return this.__tx.addOutput(scriptPubKey, value) + return this.__tx.addOutput(scriptPubKey, value) } build (): Transaction { @@ -232,15 +232,15 @@ export class TransactionBuilder { this.__inputs.forEach((input, i) => { if (!input.prevOutType && !allowIncomplete) throw new Error('Transaction is not complete') - const result = build(input.prevOutType, input, allowIncomplete) + const result = build(input.prevOutType!, input, allowIncomplete) if (!result) { if (!allowIncomplete && input.prevOutType === SCRIPT_TYPES.NONSTANDARD) throw new Error('Unknown input type') if (!allowIncomplete) throw new Error('Not enough information') return } - tx.setInputScript(i, result.input) - tx.setWitness(i, >result.witness) + tx.setInputScript(i, result.input!) + tx.setWitness(i, result.witness!) }) if (!allowIncomplete) { @@ -270,7 +270,7 @@ export class TransactionBuilder { throw new Error('Inconsistent redeemScript') } - const ourPubKey = keyPair.publicKey || (<()=>Buffer>keyPair.getPublicKey)() + const ourPubKey = keyPair.publicKey || keyPair.getPublicKey!() if (!canSign(input)) { if (witnessValue !== undefined) { if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue') @@ -297,9 +297,9 @@ export class TransactionBuilder { } // enforce in order signing of public keys - const signed = (>input.pubkeys).some((pubKey, i) => { - if (!ourPubKey.equals(pubKey)) return false - if ((>input.signatures)[i]) throw new Error('Signature already exists') + const signed = input.pubkeys!.some((pubKey, i) => { + if (!ourPubKey.equals(pubKey!)) return false + if (input.signatures![i]) throw new Error('Signature already exists') // TODO: add tests if (ourPubKey.length !== 33 && input.hasWitness) { @@ -307,7 +307,7 @@ export class TransactionBuilder { } const signature = keyPair.sign(signatureHash) - ;(>input.signatures)[i] = bscript.signature.encode(signature, hashType) + input.signatures![i] = bscript.signature.encode(signature, hashType) return true }) @@ -355,7 +355,7 @@ export class TransactionBuilder { return this.__inputs.every(input => { if (input.signatures === undefined) return true - return input.signatures.every(<(signature: Buffer | undefined)=>boolean>(signature => { + return input.signatures.every(signature => { if (!signature) return true const hashType = signatureHashType(signature) @@ -367,13 +367,14 @@ export class TransactionBuilder { // of more outputs return nInputs <= nOutputs } - })) + return false + }) }) } private __overMaximumFees (bytes: number): boolean { // not all inputs will have .value defined - const incoming = this.__inputs.reduce((a, x) => a + (x.value >>> 0), 0) + const incoming = this.__inputs.reduce((a, x) => a + (x.value! >>> 0), 0) // but all outputs do, and if we have any input value // we can immediately determine if the outputs are too small @@ -449,14 +450,14 @@ function expandInput (scriptSig: Buffer, witnessStack: Array, type?: str witness: witnessStack }) - const outputType = classify.output((redeem).output) - const expanded = expandInput((redeem).input, >(redeem).witness, outputType, (redeem).output) + const outputType = classify.output(redeem!.output!) + const expanded = expandInput(redeem!.input!, redeem!.witness!, outputType, redeem!.output) if (!expanded.prevOutType) return {} return { prevOutScript: output, prevOutType: SCRIPT_TYPES.P2SH, - redeemScript: (redeem).output, + redeemScript: redeem!.output, redeemScriptType: expanded.prevOutType, witnessScript: expanded.witnessScript, witnessScriptType: expanded.witnessScriptType, @@ -471,19 +472,19 @@ function expandInput (scriptSig: Buffer, witnessStack: Array, type?: str input: scriptSig, witness: witnessStack }) - const outputType = classify.output((redeem).output) + const outputType = classify.output(redeem!.output!) let expanded if (outputType === SCRIPT_TYPES.P2WPKH) { - expanded = expandInput((redeem).input, >(redeem).witness, outputType) + expanded = expandInput(redeem!.input!, redeem!.witness!, outputType) } else { - expanded = expandInput(bscript.compile(>(redeem).witness), [], outputType, (redeem).output) + expanded = expandInput(bscript.compile(redeem!.witness!), [], outputType, redeem!.output) } if (!expanded.prevOutType) return {} return { prevOutScript: output, prevOutType: SCRIPT_TYPES.P2WSH, - witnessScript: (redeem).output, + witnessScript: redeem!.output, witnessScriptType: expanded.prevOutType, pubkeys: expanded.pubkeys, @@ -500,12 +501,12 @@ function expandInput (scriptSig: Buffer, witnessStack: Array, type?: str // could be done in expandInput, but requires the original Transaction for hashForSignature function fixMultisigOrder (input: TxbInput, transaction: Transaction, vin: number): void { if (input.redeemScriptType !== SCRIPT_TYPES.P2MS || !input.redeemScript) return - if ((>input.pubkeys).length === (>input.signatures).length) return + if (input.pubkeys!.length === input.signatures!.length) return - const unmatched = (>input.signatures).concat() + const unmatched = input.signatures!.concat() - input.signatures = >(>input.pubkeys).map(pubKey => { - const keyPair = ECPair.fromPublicKey(pubKey) + input.signatures = input.pubkeys!.map(pubKey => { + const keyPair = ECPair.fromPublicKey(pubKey!) let match: Buffer | undefined // check for a signature @@ -515,7 +516,7 @@ function fixMultisigOrder (input: TxbInput, transaction: Transaction, vin: numbe // TODO: avoid O(n) hashForSignature const parsed = bscript.signature.decode(signature) - const hash = transaction.hashForSignature(vin, (input.redeemScript), parsed.hashType) + const hash = transaction.hashForSignature(vin, input.redeemScript!, parsed.hashType) // skip if signature does not match pubKey if (!keyPair.verify(hash, parsed.signature)) return false @@ -542,7 +543,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { // does our hash160(pubKey) match the output scripts? const pkh1 = payments.p2pkh({ output: script }).hash const pkh2 = bcrypto.hash160(ourPubKey) - if (!(pkh1).equals(pkh2)) return { type } + if (!pkh1!.equals(pkh2)) return { type } return { type, @@ -557,7 +558,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { // does our hash160(pubKey) match the output scripts? const wpkh1 = payments.p2wpkh({ output: script }).hash const wpkh2 = bcrypto.hash160(ourPubKey) - if (!(wpkh1).equals(wpkh2)) return { type } + if (!wpkh1!.equals(wpkh2)) return { type } return { type, @@ -580,7 +581,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { return { type, pubkeys: p2ms.pubkeys, - signatures: (>p2ms.pubkeys).map((): undefined => undefined), + signatures: p2ms.pubkeys!.map((): undefined => undefined), maxSignatures: p2ms.m } } @@ -597,10 +598,10 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, const p2shAlt = payments.p2sh({ redeem: p2wsh }) // enforces P2SH(P2WSH(...)) - if (!(p2wsh.hash).equals(p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript') - if (!(p2sh.hash).equals(p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript') + if (!p2wsh.hash!.equals(p2wshAlt.hash!)) throw new Error('Witness script inconsistent with prevOutScript') + if (!p2sh.hash!.equals(p2shAlt.hash!)) throw new Error('Redeem script inconsistent with prevOutScript') - const expanded = expandOutput((p2wsh.redeem).output, ourPubKey) + const expanded = expandOutput(p2wsh.redeem!.output!, ourPubKey) if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')') if (input.signatures && input.signatures.some(x => x !== undefined)) { expanded.signatures = input.signatures @@ -637,10 +638,10 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, try { p2shAlt = payments.p2sh({ output: input.prevOutScript }) } catch (e) { throw new Error('PrevOutScript must be P2SH') } - if (!(p2sh.hash).equals(p2shAlt.hash)) throw new Error('Redeem script inconsistent with prevOutScript') + if (!p2sh.hash!.equals(p2shAlt.hash!)) throw new Error('Redeem script inconsistent with prevOutScript') } - const expanded = expandOutput((p2sh.redeem).output, ourPubKey) + const expanded = expandOutput(p2sh.redeem!.output!, ourPubKey) if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as redeemScript (' + bscript.toASM(redeemScript) + ')') if (input.signatures && input.signatures.some(x => x !== undefined)) { expanded.signatures = input.signatures @@ -648,7 +649,7 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, let signScript = redeemScript if (expanded.type === SCRIPT_TYPES.P2WPKH) { - signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output + signScript = payments.p2pkh({ pubkey: expanded.pubkeys[0] }).output! } return { @@ -669,14 +670,14 @@ function prepareInput (input: TxbInput, ourPubKey: Buffer, redeemScript: Buffer, } if (witnessScript) { - const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } }) + const p2wsh = payments.p2wsh({ redeem: { output: witnessScript } }) if (input.prevOutScript) { const p2wshAlt = payments.p2wsh({ output: input.prevOutScript }) - if (!(p2wsh.hash).equals(p2wshAlt.hash)) throw new Error('Witness script inconsistent with prevOutScript') + if (!p2wsh.hash!.equals(p2wshAlt.hash!)) throw new Error('Witness script inconsistent with prevOutScript') } - const expanded = expandOutput((p2wsh.redeem).output, ourPubKey) + const expanded = expandOutput(p2wsh.redeem!.output!, ourPubKey) if (!expanded.pubkeys) throw new Error(expanded.type + ' not supported as witnessScript (' + bscript.toASM(witnessScript) + ')') if (input.signatures && input.signatures.some(x => x !== undefined)) { expanded.signatures = input.signatures @@ -784,7 +785,7 @@ function build (type: string, input: TxbInput, allowIncomplete?: boolean): Payme return payments.p2ms({ m, pubkeys, signatures }, { allowIncomplete, validate }) } case SCRIPT_TYPES.P2SH: { - const redeem = build(input.redeemScriptType, input, allowIncomplete) + const redeem = build(input.redeemScriptType!, input, allowIncomplete) if (!redeem) return return payments.p2sh({ @@ -796,7 +797,7 @@ function build (type: string, input: TxbInput, allowIncomplete?: boolean): Payme }) } case SCRIPT_TYPES.P2WSH: { - const redeem = build(input.witnessScriptType, input, allowIncomplete) + const redeem = build(input.witnessScriptType!, input, allowIncomplete) if (!redeem) return return payments.p2wsh({