diff --git a/typescript/src/lib/contracts/bridge.ts b/typescript/src/lib/contracts/bridge.ts index 0e26c1587..62afd55ec 100644 --- a/typescript/src/lib/contracts/bridge.ts +++ b/typescript/src/lib/contracts/bridge.ts @@ -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 @@ -99,7 +99,7 @@ export interface Bridge { redemptionTx: BitcoinRawTxVectors, redemptionProof: BitcoinSpvProof, mainUtxo: BitcoinUtxo, - walletPublicKey: string + walletPublicKey: Hex ): Promise /** @@ -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 /** @@ -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 /** @@ -146,7 +144,7 @@ export interface Bridge { * public key. If there is no active wallet at the moment, undefined * is returned. */ - activeWalletPublicKey(): Promise + activeWalletPublicKey(): Promise /** * Get emitted NewWalletRegisteredEvent events. diff --git a/typescript/src/lib/ethereum/bridge.ts b/typescript/src/lib/ethereum/bridge.ts index f3e4ce5ab..80aa69c90 100644 --- a/typescript/src/lib/ethereum/bridge.ts +++ b/typescript/src/lib/ethereum/bridge.ts @@ -129,11 +129,11 @@ export class EthereumBridge * @see {Bridge#pendingRedemptions} */ async pendingRedemptions( - walletPublicKey: string, - redeemerOutputScript: string + walletPublicKey: Hex, + redeemerOutputScript: Hex ): Promise { const redemptionKey = EthereumBridge.buildRedemptionKey( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(), + BitcoinHashUtils.computeHash160(walletPublicKey), redeemerOutputScript ) @@ -152,11 +152,11 @@ export class EthereumBridge * @see {Bridge#timedOutRedemptions} */ async timedOutRedemptions( - walletPublicKey: string, - redeemerOutputScript: string + walletPublicKey: Hex, + redeemerOutputScript: Hex ): Promise { const redemptionKey = EthereumBridge.buildRedemptionKey( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(), + BitcoinHashUtils.computeHash160(walletPublicKey), redeemerOutputScript ) @@ -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]), @@ -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), @@ -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 { - 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 @@ -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]), @@ -375,7 +373,7 @@ export class EthereumBridge redemptionTx: BitcoinRawTxVectors, redemptionProof: BitcoinSpvProof, mainUtxo: BitcoinUtxo, - walletPublicKey: string + walletPublicKey: Hex ): Promise { const redemptionTxParam = { version: `0x${redemptionTx.version}`, @@ -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( async () => { @@ -484,7 +481,7 @@ export class EthereumBridge /** * @see {Bridge#activeWalletPublicKey} */ - async activeWalletPublicKey(): Promise { + async activeWalletPublicKey(): Promise { const activeWalletPublicKeyHash: string = await backoffRetrier( this._totalRetryAttempts )(async () => { @@ -502,7 +499,7 @@ export class EthereumBridge Hex.from(activeWalletPublicKeyHash) ) - return walletPublicKey.toString() + return walletPublicKey } private async getWalletCompressedPublicKey(ecdsaWalletID: Hex): Promise { diff --git a/typescript/src/services/deposits/deposits-service.ts b/typescript/src/services/deposits/deposits-service.ts index b8fba8d21..091101898 100644 --- a/typescript/src/services/deposits/deposits-service.ts +++ b/typescript/src/services/deposits/deposits-service.ts @@ -10,7 +10,6 @@ import { BitcoinLocktimeUtils, } from "../../lib/bitcoin" import { Deposit } from "./deposit" -import { Hex } from "../../lib/utils" import * as crypto from "crypto" /** @@ -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() diff --git a/typescript/src/services/maintenance/spv.ts b/typescript/src/services/maintenance/spv.ts index c00d690db..8f546330e 100644 --- a/typescript/src/services/maintenance/spv.ts +++ b/typescript/src/services/maintenance/spv.ts @@ -5,6 +5,7 @@ import { BitcoinUtxo, extractBitcoinRawTxVectors, } from "../../lib/bitcoin" +import { Hex } from "../../lib/utils" import { ChainIdentifier, TBTCContracts } from "../../lib/contracts" export class Spv { @@ -84,7 +85,7 @@ export class Spv { rawTransactionVectors, proof, mainUtxo, - walletPublicKey + Hex.from(walletPublicKey) ) } } diff --git a/typescript/src/services/maintenance/wallet-tx.ts b/typescript/src/services/maintenance/wallet-tx.ts index 4756b8e59..cccf05f94 100644 --- a/typescript/src/services/maintenance/wallet-tx.ts +++ b/typescript/src/services/maintenance/wallet-tx.ts @@ -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) { diff --git a/typescript/src/services/redemptions/redemptions-service.ts b/typescript/src/services/redemptions/redemptions-service.ts index a6bc302f1..5a1a304ae 100644 --- a/typescript/src/services/redemptions/redemptions-service.ts +++ b/typescript/src/services/redemptions/redemptions-service.ts @@ -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) { @@ -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 diff --git a/typescript/test/lib/ethereum.test.ts b/typescript/test/lib/ethereum.test.ts index 11b4086d6..dfd558362 100644 --- a/typescript/test/lib/ethereum.test.ts +++ b/typescript/test/lib/ethereum.test.ts @@ -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( @@ -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( @@ -244,7 +248,9 @@ describe("Ethereum", () => { await bridgeContract.mock.requestRedemption.returns() await bridgeHandle.requestRedemption( - "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9", + Hex.from( + "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9" + ), { transactionHash: BitcoinTxHash.from( "f8eaf242a55ea15e602f9f990e33f67f99dfbe25d1802bbde63cc1caabf99668" @@ -252,7 +258,7 @@ describe("Ethereum", () => { outputIndex: 8, value: BigNumber.from(9999), }, - "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87", + Hex.from("a9143ec459d0f3c29286ae5df5fcc421e2786024277e87"), BigNumber.from(10000) ) }) @@ -295,7 +301,9 @@ describe("Ethereum", () => { outputIndex: 8, value: BigNumber.from(9999), }, - "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9" + Hex.from( + "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9" + ) ) }) @@ -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" ) }) diff --git a/typescript/test/utils/mock-bridge.ts b/typescript/test/utils/mock-bridge.ts index fe6a8a27c..0a7c143de 100644 --- a/typescript/test/utils/mock-bridge.ts +++ b/typescript/test/utils/mock-bridge.ts @@ -70,7 +70,7 @@ export class MockBridge implements Bridge { private _requestRedemptionLog: RequestRedemptionLogEntry[] = [] private _redemptionProofLog: RedemptionProofLogEntry[] = [] private _deposits = new Map() - private _activeWalletPublicKey: string | undefined + private _activeWalletPublicKey: Hex | undefined private _newWalletRegisteredEvents: NewWalletRegisteredEvent[] = [] private _newWalletRegisteredEventsLog: NewWalletRegisteredEventsLog[] = [] private _wallets = new Map() @@ -120,7 +120,7 @@ export class MockBridge implements Bridge { this._deposits = value } - setActiveWalletPublicKey(activeWalletPublicKey: string) { + setActiveWalletPublicKey(activeWalletPublicKey: Hex) { this._activeWalletPublicKey = activeWalletPublicKey } @@ -223,13 +223,13 @@ export class MockBridge implements Bridge { redemptionTx: BitcoinRawTxVectors, redemptionProof: BitcoinSpvProof, mainUtxo: BitcoinUtxo, - walletPublicKey: string + walletPublicKey: Hex ): Promise { this._redemptionProofLog.push({ redemptionTx, redemptionProof, mainUtxo, - walletPublicKey, + walletPublicKey: walletPublicKey.toString(), }) return new Promise((resolve, _) => { resolve() @@ -237,15 +237,15 @@ export class MockBridge implements Bridge { } requestRedemption( - walletPublicKey: string, + walletPublicKey: Hex, mainUtxo: BitcoinUtxo, - redeemerOutputScript: string, + redeemerOutputScript: Hex, amount: BigNumber ) { this._requestRedemptionLog.push({ - walletPublicKey, + walletPublicKey: walletPublicKey.toString(), mainUtxo, - redeemerOutputScript, + redeemerOutputScript: redeemerOutputScript.toString(), amount, }) return new Promise((resolve, _) => { @@ -260,14 +260,14 @@ export class MockBridge implements Bridge { } pendingRedemptions( - walletPublicKey: string, - redeemerOutputScript: string + walletPublicKey: Hex, + redeemerOutputScript: Hex ): Promise { return new Promise((resolve, _) => { resolve( this.redemptions( - walletPublicKey, - redeemerOutputScript, + walletPublicKey.toString(), + redeemerOutputScript.toString(), this._pendingRedemptions ) ) @@ -275,14 +275,14 @@ export class MockBridge implements Bridge { } timedOutRedemptions( - walletPublicKey: string, - redeemerOutputScript: string + walletPublicKey: Hex, + redeemerOutputScript: Hex ): Promise { return new Promise((resolve, _) => { resolve( this.redemptions( - walletPublicKey, - redeemerOutputScript, + walletPublicKey.toString(), + redeemerOutputScript.toString(), this._timedOutRedemptions ) ) @@ -335,7 +335,7 @@ export class MockBridge implements Bridge { ) } - async activeWalletPublicKey(): Promise { + async activeWalletPublicKey(): Promise { return this._activeWalletPublicKey }