Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
comply with typescript strict checks
Browse files Browse the repository at this point in the history
  • Loading branch information
keppel committed Sep 1, 2019
1 parent 0c5465e commit b030571
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 109 deletions.
34 changes: 23 additions & 11 deletions src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import {
getCurrentP2ssAddress,
getSignatoryScriptHashFromPegZone
} from './signatory'
import { BitcoinNetwork, SignatoryMap, SignedTx, ValidatorMap } from './types'
import {
BitcoinNetwork,
SignatoryMap,
SignedTx,
ValidatorMap,
Header,
BitcoinRPC,
RPCHeader,
LightClient
} from './types'
import { p2sh } from 'bitcoinjs-lib/types/payments'

let encodeBitcoinTx = require('bitcoin-protocol').types.transaction.encode
let decodeBitcoinTx = require('bitcoin-protocol').types.transaction.decode
let { getTxHash, getBlockHash } = require('bitcoin-net/src/utils.js')

interface RelayOptions {
bitcoinRPC: any
bitcoinRPC: BitcoinRPC
lotionLightClient: any
network: BitcoinNetwork
}
Expand All @@ -30,7 +40,7 @@ interface RelayOptions {
*
*/
export class Relay {
private bitcoinRPC: any
private bitcoinRPC: BitcoinRPC
private lotionLightClient: any
private network: BitcoinNetwork

Expand All @@ -40,7 +50,7 @@ export class Relay {
this.network = relayOpts.network
}

async relayHeaders(pegChainHeaders) {
async relayHeaders(pegChainHeaders: Header[]) {
let rpc = this.bitcoinRPC
// Compute common ancestor
let commonHeaderHash
Expand Down Expand Up @@ -174,7 +184,7 @@ export class Relay {
}
}

function formatHeader(header) {
function formatHeader(header: RPCHeader) {
return {
height: Number(header.height),
version: Number(header.version),
Expand All @@ -188,8 +198,10 @@ function formatHeader(header) {
}
}

export function convertValidatorsToLotion(validators): ValidatorMap {
return validators.reduce((obj, v) => {
export function convertValidatorsToLotion(
validators: LightClient['validators']
): ValidatorMap {
return validators.reduce((obj: ValidatorMap, v) => {
obj[v.pub_key.value] = v.voting_power
return obj
}, {})
Expand All @@ -216,7 +228,9 @@ function buildDisbursalTransaction(
output: redeemScript
}
})
tx.setWitness(i, p2wsh.witness)
if (p2wsh.witness) {
tx.setWitness(i, p2wsh.witness)
}
}

return tx
Expand All @@ -228,9 +242,7 @@ function buildDisbursalTransaction(
function getSignatures(signatures: SignedTx['signatures'], index: number) {
let result: string[] = []
for (let i = 0; i < signatures.length; i++) {
result.push(
signatures[i] ? signatures[i][index].toString('hex') + '01' : null
) // SIGHASH_ALL
result.push(signatures[i][index].toString('hex') + '01') // SIGHASH_ALL
}
return result
}
29 changes: 15 additions & 14 deletions src/reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BitcoinNetwork, SignatoryMap, SigningTx, ValidatorMap } from './types'
const MAX_SIGNATORIES = 76
const MIN_RELAY_FEE = 1000

const firstSignatory = signatory => {
const firstSignatory = (signatory: { votingPower: number; pubkey: string }) => {
return `
${signatory.pubkey} OP_CHECKSIG
OP_IF
Expand All @@ -21,25 +21,25 @@ const firstSignatory = signatory => {
OP_ENDIF
`
}
const nthSignatory = ({ pubkey, votingPower }) => `
const nthSignatory = (signatory: { pubkey: string; votingPower: number }) => `
OP_SWAP
${pubkey} OP_CHECKSIG
${signatory.pubkey} OP_CHECKSIG
OP_IF
${uint(votingPower)}
${uint(signatory.votingPower)}
OP_ADD
OP_ENDIF
`

const compare = threshold => `
const compare = (threshold: number) => `
${uint(threshold)}
OP_GREATERTHAN
`

function signature(signature) {
function signature(signature: string) {
return signature || 'OP_0'
}

function uint(n) {
function uint(n: number) {
n = Number(n)
if (!Number.isInteger(n)) {
throw Error('Number must be an integer')
Expand All @@ -54,7 +54,9 @@ function uint(n) {
return nHex
}

export function getVotingPowerThreshold(signatories) {
export function getVotingPowerThreshold(
signatories: { votingPower: number }[]
) {
let totalVotingPower = signatories.reduce((sum, s) => sum + s.votingPower, 0)
let twoThirdsVotingPower = Math.ceil((totalVotingPower * 2) / 3)
return twoThirdsVotingPower
Expand All @@ -65,13 +67,13 @@ export function createWitnessScript(
signatoryKeys: SignatoryMap
) {
// get signatory key for each signatory
let signatories = getSignatorySet(validators)
let signatories: { pubkey: string; votingPower: number }[] = getSignatorySet(
validators
)
.map(({ validatorKey, votingPower }) => {
let pubkeyHex
let pubkeyBytes: Buffer = signatoryKeys[validatorKey]
if (pubkeyBytes) {
pubkeyHex = pubkeyBytes.toString('hex')
}
pubkeyHex = pubkeyBytes.toString('hex')
return { pubkey: pubkeyHex, votingPower }
})
.filter(s => s.pubkey != null)
Expand All @@ -90,7 +92,6 @@ export function createWitnessScript(
}

export function createScriptSig(signatures: string[]) {
console.log(signatures)
let asm = signatures
.map(signature)
.reverse()
Expand All @@ -99,7 +100,7 @@ export function createScriptSig(signatures: string[]) {
return script.fromASM(trim(asm))
}

function trim(s) {
function trim(s: string) {
return s
.split(/\s/g)
.filter(s => !!s)
Expand Down
39 changes: 25 additions & 14 deletions src/signatory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { createHash } from 'crypto'
import ed = require('ed25519-supercop')
import secp = require('secp256k1')
let ed = require('ed25519-supercop')
let secp = require('secp256k1')
import * as bitcoin from 'bitcoinjs-lib'
import { ValidatorKey, ValidatorMap, BitcoinNetwork } from './types'
import {
ValidatorKey,
ValidatorMap,
BitcoinNetwork,
LightClient,
SigningTx
} from './types'
import {
getSignatorySet,
buildOutgoingTx,
Expand All @@ -11,7 +17,7 @@ import {
import { convertValidatorsToLotion } from './relay.js'

export async function commitPubkey(
client,
client: LightClient,
privValidator: ValidatorKey,
signatoryPub: Buffer
) {
Expand Down Expand Up @@ -47,8 +53,8 @@ export async function commitPubkey(
}

export async function signDisbursal(
client,
signatoryPriv,
client: LightClient,
signatoryPriv: Buffer,
network: BitcoinNetwork
) {
let signatoryPub = secp.publicKeyCreate(signatoryPriv)
Expand All @@ -68,7 +74,7 @@ export async function signDisbursal(
throw Error('Given signatory key not found in signatory set')
}

let signingTx = await client.state.bitcoin.signingTx
let signingTx: SigningTx = await client.state.bitcoin.signingTx
if (signingTx == null) {
throw Error('No tx to be signed')
}
Expand Down Expand Up @@ -98,13 +104,13 @@ export async function signDisbursal(
)
}

function sha512(data) {
function sha512(data: Buffer) {
return createHash('sha512')
.update(data)
.digest()
}

function ed25519Sign(privValidator: ValidatorKey, message) {
function ed25519Sign(privValidator: ValidatorKey, message: Buffer) {
if (privValidator.priv_key.type !== 'tendermint/PrivKeyEd25519') {
throw Error('Expected privkey type "tendermint/PrivKeyEd25519"')
}
Expand All @@ -116,10 +122,12 @@ function ed25519Sign(privValidator: ValidatorKey, message) {
return ed.sign(message, pub, priv)
}

export async function getSignatoryScriptHashFromPegZone(lightClient: any) {
export async function getSignatoryScriptHashFromPegZone(
lightClient: LightClient
) {
let signatoryKeys = await lightClient.state.bitcoin.signatoryKeys
let lotionValidators = {}
lightClient.validators.forEach((validator: any) => {
let lotionValidators: ValidatorMap = {}
lightClient.validators.forEach(validator => {
lotionValidators[validator.pub_key.value] = validator.voting_power
})
let p2ss = createWitnessScript(lotionValidators, signatoryKeys)
Expand All @@ -136,11 +144,14 @@ export async function getCurrentP2ssAddress(
redeem: { output: p2ss },
network: bitcoin.networks[network === 'mainnet' ? 'bitcoin' : network]
}).address
if (!p2ssAddress) {
throw new Error('Could not derive p2ss address from peg zone')
}
return p2ssAddress
}

// TODO: move this somewhere else
export function convertEd25519(ref10Priv) {
export function convertEd25519(ref10Priv: Buffer) {
// see https://github.com/orlp/ed25519/issues/10#issuecomment-242761092
let privConverted = sha512(ref10Priv.slice(0, 32))
privConverted[0] &= 248
Expand All @@ -149,7 +160,7 @@ export function convertEd25519(ref10Priv) {
return privConverted
}

function checkResult(res) {
function checkResult(res: any) {
if (res.check_tx.code || res.deliver_tx.code) {
let log = res.check_tx.log || res.deliver_tx.log
throw Error(log)
Expand Down
Loading

0 comments on commit b030571

Please sign in to comment.