Update TypeScript to use ! instead of casting

This commit is contained in:
junderw 2019-01-15 17:47:30 +09:00
parent bc28949056
commit 1732bafbc1
No known key found for this signature in database
GPG key ID: B256185D3A971908
17 changed files with 99 additions and 97 deletions

View file

@ -36,15 +36,15 @@ export function p2data (a: Payment, opts?: PaymentOpts): Payment {
})
lazy.prop(o, 'data', function () {
if (!a.output) return
return (<Array<Buffer | number>>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 ((<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 (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, <Array<Buffer>>o.data)) throw new TypeError('Data mismatch')
}

View file

@ -29,7 +29,7 @@ export function p2ms (a: Payment, opts?: PaymentOpts): Payment {
function isAcceptableSignature (x: Buffer | number) {
return bscript.isCanonicalScriptSignature(<Buffer>x) ||
((<PaymentOpts>opts).allowIncomplete &&
(opts!.allowIncomplete &&
(<number> 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 (<Array<Buffer | number>>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 (
<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.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 (!(<Array<Buffer>>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, (<Array<Buffer>>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 < <number>(<Payment>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 < <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.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 ((<Array<Buffer>>o.signatures).length === 0 || !(<Array<Buffer>>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, (<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')
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')
}
}

View file

@ -27,7 +27,7 @@ export function p2pk (a: Payment, opts?: PaymentOpts): Payment {
input: typef.maybe(typef.Buffer)
}, a)
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>a.input) })
const _chunks = <()=>Array<Buffer | number>>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(<Buffer>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(<Buffer>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(<Buffer>o.signature)) throw new TypeError('Input has invalid signature')
if (!bscript.isCanonicalScriptSignature(o.signature!)) throw new TypeError('Input has invalid signature')
}
}

View file

@ -38,7 +38,7 @@ export function p2pkh (a: Payment, opts?: PaymentOpts): Payment {
const hash = payload.slice(1)
return { version, hash }
})
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>a.input) })
const _chunks = <()=>Array<Buffer | number>>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(<Buffer> a.pubkey || <Buffer>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

View file

@ -59,7 +59,7 @@ export function p2sh (a: Payment, opts?: PaymentOpts): Payment {
const hash = payload.slice(1)
return { version, hash }
})
const _chunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>a.input) })
const _chunks = <()=>Array<Buffer | number>>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((<Network>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(<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')
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)

View file

@ -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(<Buffer> a.pubkey || <Buffer>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

View file

@ -58,7 +58,7 @@ export function p2wsh (a: Payment, opts?: PaymentOpts): Payment {
data: Buffer.from(data)
}
})
const _rchunks = <()=>Array<Buffer | number>>lazy.value(function () { return bscript.decompile(<Buffer>(<Payment>a.redeem).input) })
const _rchunks = <()=>Array<Buffer | number>>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>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 ((<Array<Buffer | number>>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)