Skip to content

Commit

Permalink
Used Hex as argument in Bridge functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 16, 2023
1 parent 5122eaa commit aad0b94
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 79 deletions.
24 changes: 11 additions & 13 deletions typescript/src/lib/contracts/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ export interface Bridge {
* @param mainUtxo - The main UTXO of the wallet. Must match the main UTXO
* held by the on-chain contract.
* @param redeemerOutputScript - The output script that the redeemed funds will
* be locked to. Must be un-prefixed and not prepended with length.
* be locked to. Must not be prepended with length.
* @param amount - The amount to be redeemed in satoshis.
* @returns Empty promise.
*/
requestRedemption(
walletPublicKey: string,
walletPublicKey: Hex,
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string,
redeemerOutputScript: Hex,
amount: BigNumber
): Promise<void>

Expand All @@ -99,7 +99,7 @@ export interface Bridge {
redemptionTx: BitcoinRawTxVectors,
redemptionProof: BitcoinSpvProof,
mainUtxo: BitcoinUtxo,
walletPublicKey: string
walletPublicKey: Hex
): Promise<void>

/**
Expand All @@ -116,13 +116,12 @@ export interface Bridge {
* targeted to. Must be in the compressed form (33 bytes long with 02
* or 03 prefix).
* @param redeemerOutputScript The redeemer output script the redeemed funds
* are supposed to be locked on. Must be un-prefixed and not prepended
* with length.
* are supposed to be locked on. Must not be prepended with length.
* @returns Promise with the pending redemption.
*/
pendingRedemptions(
walletPublicKey: string,
redeemerOutputScript: string
walletPublicKey: Hex,
redeemerOutputScript: Hex
): Promise<RedemptionRequest>

/**
Expand All @@ -131,13 +130,12 @@ export interface Bridge {
* targeted to. Must be in the compressed form (33 bytes long with 02
* or 03 prefix).
* @param redeemerOutputScript The redeemer output script the redeemed funds
* are supposed to be locked on. Must be un-prefixed and not prepended
* with length.
* are supposed to be locked on. Must not be prepended with length.
* @returns Promise with the pending redemption.
*/
timedOutRedemptions(
walletPublicKey: string,
redeemerOutputScript: string
walletPublicKey: Hex,
redeemerOutputScript: Hex
): Promise<RedemptionRequest>

/**
Expand All @@ -146,7 +144,7 @@ export interface Bridge {
* public key. If there is no active wallet at the moment, undefined
* is returned.
*/
activeWalletPublicKey(): Promise<string | undefined>
activeWalletPublicKey(): Promise<Hex | undefined>

/**
* Get emitted NewWalletRegisteredEvent events.
Expand Down
55 changes: 26 additions & 29 deletions typescript/src/lib/ethereum/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ export class EthereumBridge
* @see {Bridge#pendingRedemptions}
*/
async pendingRedemptions(
walletPublicKey: string,
redeemerOutputScript: string
walletPublicKey: Hex,
redeemerOutputScript: Hex
): Promise<RedemptionRequest> {
const redemptionKey = EthereumBridge.buildRedemptionKey(
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(),
BitcoinHashUtils.computeHash160(walletPublicKey),
redeemerOutputScript
)

Expand All @@ -152,11 +152,11 @@ export class EthereumBridge
* @see {Bridge#timedOutRedemptions}
*/
async timedOutRedemptions(
walletPublicKey: string,
redeemerOutputScript: string
walletPublicKey: Hex,
redeemerOutputScript: Hex
): Promise<RedemptionRequest> {
const redemptionKey = EthereumBridge.buildRedemptionKey(
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(),
BitcoinHashUtils.computeHash160(walletPublicKey),
redeemerOutputScript
)

Expand All @@ -173,19 +173,18 @@ export class EthereumBridge
/**
* Builds a redemption key required to refer a redemption request.
* @param walletPublicKeyHash The wallet public key hash that identifies the
* pending redemption (along with the redeemer output script). Must be
* unprefixed.
* pending redemption (along with the redeemer output script).
* @param redeemerOutputScript The redeemer output script that identifies the
* pending redemption (along with the wallet public key hash). Must be
* un-prefixed and not prepended with length.
* pending redemption (along with the wallet public key hash). Must not
* be prepended with length.
* @returns The redemption key.
*/
static buildRedemptionKey(
walletPublicKeyHash: string,
redeemerOutputScript: string
walletPublicKeyHash: Hex,
redeemerOutputScript: Hex
): string {
// Convert the output script to raw bytes buffer.
const rawRedeemerOutputScript = Buffer.from(redeemerOutputScript, "hex")
const rawRedeemerOutputScript = redeemerOutputScript.toBuffer()
// Prefix the output script bytes buffer with 0x and its own length.
const prefixedRawRedeemerOutputScript = `0x${Buffer.concat([
Buffer.from([rawRedeemerOutputScript.length]),
Expand All @@ -206,17 +205,17 @@ export class EthereumBridge
* Parses a redemption request using data fetched from the on-chain contract.
* @param request Data of the request.
* @param redeemerOutputScript The redeemer output script that identifies the
* pending redemption (along with the wallet public key hash). Must be
* un-prefixed and not prepended with length.
* pending redemption (along with the wallet public key hash). Must not
* be prepended with length.
* @returns Parsed redemption request.
*/
private parseRedemptionRequest(
request: RedemptionRequestTypechain,
redeemerOutputScript: string
redeemerOutputScript: Hex
): RedemptionRequest {
return {
redeemer: EthereumAddress.from(request.redeemer),
redeemerOutputScript: redeemerOutputScript,
redeemerOutputScript: redeemerOutputScript.toString(),
requestedAmount: BigNumber.from(request.requestedAmount),
treasuryFee: BigNumber.from(request.treasuryFee),
txMaxFee: BigNumber.from(request.txMaxFee),
Expand Down Expand Up @@ -329,14 +328,13 @@ export class EthereumBridge
* @see {Bridge#requestRedemption}
*/
async requestRedemption(
walletPublicKey: string,
walletPublicKey: Hex,
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string,
redeemerOutputScript: Hex,
amount: BigNumber
): Promise<void> {
const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
).toPrefixedString()
const walletPublicKeyHash =
BitcoinHashUtils.computeHash160(walletPublicKey).toPrefixedString()

const mainUtxoParam = {
// The Ethereum Bridge expects this hash to be in the Bitcoin internal
Expand All @@ -347,7 +345,7 @@ export class EthereumBridge
}

// Convert the output script to raw bytes buffer.
const rawRedeemerOutputScript = Buffer.from(redeemerOutputScript, "hex")
const rawRedeemerOutputScript = redeemerOutputScript.toBuffer()
// Prefix the output script bytes buffer with 0x and its own length.
const prefixedRawRedeemerOutputScript = `0x${Buffer.concat([
Buffer.from([rawRedeemerOutputScript.length]),
Expand Down Expand Up @@ -375,7 +373,7 @@ export class EthereumBridge
redemptionTx: BitcoinRawTxVectors,
redemptionProof: BitcoinSpvProof,
mainUtxo: BitcoinUtxo,
walletPublicKey: string
walletPublicKey: Hex
): Promise<void> {
const redemptionTxParam = {
version: `0x${redemptionTx.version}`,
Expand All @@ -398,9 +396,8 @@ export class EthereumBridge
txOutputValue: mainUtxo.value,
}

const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
).toPrefixedString()
const walletPublicKeyHash =
BitcoinHashUtils.computeHash160(walletPublicKey).toPrefixedString()

await EthersTransactionUtils.sendWithRetry<ContractTransaction>(
async () => {
Expand Down Expand Up @@ -484,7 +481,7 @@ export class EthereumBridge
/**
* @see {Bridge#activeWalletPublicKey}
*/
async activeWalletPublicKey(): Promise<string | undefined> {
async activeWalletPublicKey(): Promise<Hex | undefined> {
const activeWalletPublicKeyHash: string = await backoffRetrier<string>(
this._totalRetryAttempts
)(async () => {
Expand All @@ -502,7 +499,7 @@ export class EthereumBridge
Hex.from(activeWalletPublicKeyHash)
)

return walletPublicKey.toString()
return walletPublicKey
}

private async getWalletCompressedPublicKey(ecdsaWalletID: Hex): Promise<Hex> {
Expand Down
6 changes: 2 additions & 4 deletions typescript/src/services/deposits/deposits-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
BitcoinLocktimeUtils,
} from "../../lib/bitcoin"
import { Deposit } from "./deposit"
import { Hex } from "../../lib/utils"
import * as crypto from "crypto"

/**
Expand Down Expand Up @@ -76,9 +75,8 @@ export class DepositsService {
throw new Error("Could not get active wallet public key")
}

const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
).toString()
const walletPublicKeyHash =
BitcoinHashUtils.computeHash160(walletPublicKey).toString()

const bitcoinNetwork = await this.bitcoinClient.getNetwork()

Expand Down
3 changes: 2 additions & 1 deletion typescript/src/services/maintenance/spv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
BitcoinUtxo,
extractBitcoinRawTxVectors,
} from "../../lib/bitcoin"
import { Hex } from "../../lib/utils"
import { ChainIdentifier, TBTCContracts } from "../../lib/contracts"

export class Spv {
Expand Down Expand Up @@ -84,7 +85,7 @@ export class Spv {
rawTransactionVectors,
proof,
mainUtxo,
walletPublicKey
Hex.from(walletPublicKey)
)
}
}
4 changes: 2 additions & 2 deletions typescript/src/services/maintenance/wallet-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ class Redemption {
for (const redeemerOutputScript of redeemerOutputScripts) {
const redemptionRequest =
await this.tbtcContracts.bridge.pendingRedemptions(
walletPublicKey,
redeemerOutputScript
Hex.from(walletPublicKey),
Hex.from(redeemerOutputScript)
)

if (redemptionRequest.requestedAt == 0) {
Expand Down
10 changes: 5 additions & 5 deletions typescript/src/services/redemptions/redemptions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export class RedemptionsService {

const pendingRedemption =
await this.tbtcContracts.bridge.pendingRedemptions(
walletPublicKey.toString(),
redeemerOutputScript
walletPublicKey,
Hex.from(redeemerOutputScript)
)

if (pendingRedemption.requestedAt != 0) {
Expand Down Expand Up @@ -338,21 +338,21 @@ export class RedemptionsService {
const redeemerOutputScript = BitcoinAddressConverter.addressToOutputScript(
bitcoinRedeemerAddress,
bitcoinNetwork
).toString()
)

let redemptionRequest: RedemptionRequest | undefined = undefined

switch (type) {
case "pending": {
redemptionRequest = await this.tbtcContracts.bridge.pendingRedemptions(
walletPublicKey,
Hex.from(walletPublicKey),
redeemerOutputScript
)
break
}
case "timedOut": {
redemptionRequest = await this.tbtcContracts.bridge.timedOutRedemptions(
walletPublicKey,
Hex.from(walletPublicKey),
redeemerOutputScript
)
break
Expand Down
26 changes: 18 additions & 8 deletions typescript/test/lib/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ describe("Ethereum", () => {
it("should return the pending redemption", async () => {
expect(
await bridgeHandle.pendingRedemptions(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9",
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"
Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
),
Hex.from("a9143ec459d0f3c29286ae5df5fcc421e2786024277e87")
)
).to.be.eql({
redeemer: EthereumAddress.from(
Expand Down Expand Up @@ -113,8 +115,10 @@ describe("Ethereum", () => {
it("should return the timed-out redemption", async () => {
expect(
await bridgeHandle.timedOutRedemptions(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9",
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"
Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
),
Hex.from("a9143ec459d0f3c29286ae5df5fcc421e2786024277e87")
)
).to.be.eql({
redeemer: EthereumAddress.from(
Expand Down Expand Up @@ -244,15 +248,17 @@ describe("Ethereum", () => {
await bridgeContract.mock.requestRedemption.returns()

await bridgeHandle.requestRedemption(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9",
Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
),
{
transactionHash: BitcoinTxHash.from(
"f8eaf242a55ea15e602f9f990e33f67f99dfbe25d1802bbde63cc1caabf99668"
),
outputIndex: 8,
value: BigNumber.from(9999),
},
"a9143ec459d0f3c29286ae5df5fcc421e2786024277e87",
Hex.from("a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"),
BigNumber.from(10000)
)
})
Expand Down Expand Up @@ -295,7 +301,9 @@ describe("Ethereum", () => {
outputIndex: 8,
value: BigNumber.from(9999),
},
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
Hex.from(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
)
)
})

Expand Down Expand Up @@ -441,7 +449,9 @@ describe("Ethereum", () => {
})

it("should return the active wallet's public key", async () => {
expect(await bridgeHandle.activeWalletPublicKey()).to.be.equal(
expect(
(await bridgeHandle.activeWalletPublicKey())?.toString()
).to.be.equal(
"03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9"
)
})
Expand Down
Loading

0 comments on commit aad0b94

Please sign in to comment.