From 21c55310589bb2b306ecec4af9ff8352b1412455 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Tue, 17 Oct 2023 14:13:01 +0200 Subject: [PATCH] Used Hex for TBTC token functionalities --- typescript/src/lib/contracts/tbtc-token.ts | 7 +++---- typescript/src/lib/ethereum/tbtc-token.ts | 19 ++++++++--------- .../redemptions/redemptions-service.ts | 4 ++-- typescript/test/data/redemption.ts | 3 ++- typescript/test/lib/ethereum.test.ts | 21 ++++++++++--------- typescript/test/services/maintenance.test.ts | 2 +- typescript/test/services/redemptions.test.ts | 15 +++++++------ typescript/test/utils/mock-tbtc-token.ts | 8 +++---- 8 files changed, 39 insertions(+), 40 deletions(-) diff --git a/typescript/src/lib/contracts/tbtc-token.ts b/typescript/src/lib/contracts/tbtc-token.ts index 596c88ddb..01ea195fe 100644 --- a/typescript/src/lib/contracts/tbtc-token.ts +++ b/typescript/src/lib/contracts/tbtc-token.ts @@ -32,16 +32,15 @@ export interface TBTCToken { * @param mainUtxo - The main UTXO of the wallet. Must match the main UTXO * held by the on-chain Bridge contract. * @param redeemerOutputScript - The output script that the redeemed funds - * will be locked to. Must be un-prefixed and not prepended with - * length. + * will be locked to. Must not be prepended with length. * @param amount - The amount to be redeemed with the precision of the tBTC * on-chain token contract. * @returns Transaction hash of the approve and call transaction. */ requestRedemption( - walletPublicKey: string, + walletPublicKey: Hex, mainUtxo: BitcoinUtxo, - redeemerOutputScript: string, + redeemerOutputScript: Hex, amount: BigNumber ): Promise } diff --git a/typescript/src/lib/ethereum/tbtc-token.ts b/typescript/src/lib/ethereum/tbtc-token.ts index 7ec5dbab3..719cb3aca 100644 --- a/typescript/src/lib/ethereum/tbtc-token.ts +++ b/typescript/src/lib/ethereum/tbtc-token.ts @@ -69,9 +69,9 @@ export class EthereumTBTCToken * @see {TBTCToken#requestRedemption} */ async requestRedemption( - walletPublicKey: string, + walletPublicKey: Hex, mainUtxo: BitcoinUtxo, - redeemerOutputScript: string, + redeemerOutputScript: Hex, amount: BigNumber ): Promise { const redeemer = await this._instance?.signer?.getAddress() @@ -103,9 +103,9 @@ export class EthereumTBTCToken private buildRequestRedemptionData( redeemer: EthereumAddress, - walletPublicKey: string, + walletPublicKey: Hex, mainUtxo: BitcoinUtxo, - redeemerOutputScript: string + redeemerOutputScript: Hex ): Hex { const { walletPublicKeyHash, @@ -133,13 +133,12 @@ export class EthereumTBTCToken } private buildBridgeRequestRedemptionData( - walletPublicKey: string, + walletPublicKey: Hex, mainUtxo: BitcoinUtxo, - redeemerOutputScript: string + redeemerOutputScript: Hex ) { - 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 @@ -150,7 +149,7 @@ export class EthereumTBTCToken } // 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]), diff --git a/typescript/src/services/redemptions/redemptions-service.ts b/typescript/src/services/redemptions/redemptions-service.ts index 5a1a304ae..74b10b680 100644 --- a/typescript/src/services/redemptions/redemptions-service.ts +++ b/typescript/src/services/redemptions/redemptions-service.ts @@ -66,9 +66,9 @@ export class RedemptionsService { ) const txHash = await this.tbtcContracts.tbtcToken.requestRedemption( - walletPublicKey, + Hex.from(walletPublicKey), mainUtxo, - redeemerOutputScript, + Hex.from(redeemerOutputScript), amount ) diff --git a/typescript/test/data/redemption.ts b/typescript/test/data/redemption.ts index 3d806da34..9bdec0dc2 100644 --- a/typescript/test/data/redemption.ts +++ b/typescript/test/data/redemption.ts @@ -25,8 +25,9 @@ export const walletPrivateKey = * Public key of the wallet in the compressed form corresponding to * walletPrivateKey. */ -export const walletPublicKey = +export const walletPublicKey = Hex.from( "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9" +) /** * P2PKH address corresponding to walletPrivateKey. diff --git a/typescript/test/lib/ethereum.test.ts b/typescript/test/lib/ethereum.test.ts index 42fe48453..88608f0e1 100644 --- a/typescript/test/lib/ethereum.test.ts +++ b/typescript/test/lib/ethereum.test.ts @@ -528,8 +528,9 @@ describe("Ethereum", () => { vault: EthereumAddress.from( "0x24BE35e7C04E2e0a628614Ce0Ed58805e1C894F7" ), - walletPublicKey: - "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9", + walletPublicKey: Hex.from( + "03989d253b17a6a0f41838b84ff0d20e8898f9d7b1a98f2564da4cc29dcf8581d9" + ), mainUtxo: { transactionHash: BitcoinTxHash.from( "f8eaf242a55ea15e602f9f990e33f67f99dfbe25d1802bbde63cc1caabf99668" @@ -540,10 +541,12 @@ describe("Ethereum", () => { redeemer: EthereumAddress.from(signer.address), amount: BigNumber.from(10000), redeemerOutputScript: { - unprefixed: - "0020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70", - prefixed: - "0x220020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70", + unprefixed: Hex.from( + "0020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70" + ), + prefixed: Hex.from( + "220020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70" + ), }, } @@ -572,13 +575,11 @@ describe("Ethereum", () => { ["address", "bytes20", "bytes32", "uint32", "uint64", "bytes"], [ redeemer.identifierHex, - BitcoinHashUtils.computeHash160( - Hex.from(walletPublicKey) - ).toPrefixedString(), + BitcoinHashUtils.computeHash160(walletPublicKey).toPrefixedString(), mainUtxo.transactionHash.reverse().toPrefixedString(), mainUtxo.outputIndex, mainUtxo.value, - redeemerOutputScript.prefixed, + redeemerOutputScript.prefixed.toPrefixedString(), ] ) diff --git a/typescript/test/services/maintenance.test.ts b/typescript/test/services/maintenance.test.ts index f21d253c9..3fabe2036 100644 --- a/typescript/test/services/maintenance.test.ts +++ b/typescript/test/services/maintenance.test.ts @@ -2533,7 +2533,7 @@ describe("Maintenance", () => { await maintenanceService.spv.submitRedemptionProof( transactionHash, mainUtxo, - walletPublicKey + walletPublicKey.toString() ) }) diff --git a/typescript/test/services/redemptions.test.ts b/typescript/test/services/redemptions.test.ts index 384568c41..1a974b9e4 100644 --- a/typescript/test/services/redemptions.test.ts +++ b/typescript/test/services/redemptions.test.ts @@ -53,21 +53,20 @@ describe("Redemptions", () => { tbtcContracts = new MockTBTCContracts() bitcoinClient = new MockBitcoinClient() - const walletPublicKeyHash = BitcoinHashUtils.computeHash160( - Hex.from(walletPublicKey) - ) + const walletPublicKeyHash = + BitcoinHashUtils.computeHash160(walletPublicKey) // Prepare NewWalletRegisteredEvent history. Set only relevant fields. tbtcContracts.bridge.newWalletRegisteredEvents = [ { - walletPublicKeyHash: walletPublicKeyHash, + walletPublicKeyHash, } as NewWalletRegisteredEvent, ] // Prepare wallet data in the Bridge. Set only relevant fields. tbtcContracts.bridge.setWallet(walletPublicKeyHash.toPrefixedString(), { state: WalletState.Live, - walletPublicKey: Hex.from(walletPublicKey), + walletPublicKey, pendingRedemptionsValue: BigNumber.from(0), mainUtxoHash: tbtcContracts.bridge.buildUtxoHash(mainUtxo), } as Wallet) @@ -119,7 +118,7 @@ describe("Redemptions", () => { expect(tokenLog[0]).to.deep.equal({ walletPublicKey, mainUtxo, - redeemerOutputScript: redeemerOutputScript.toString(), + redeemerOutputScript, amount, }) }) @@ -156,7 +155,7 @@ describe("Redemptions", () => { redemptionRequest.redeemerOutputScript, BitcoinNetwork.Testnet ), - walletPublicKey, + walletPublicKey.toString(), "pending" ) @@ -194,7 +193,7 @@ describe("Redemptions", () => { redemptionRequest.redeemerOutputScript, BitcoinNetwork.Testnet ), - walletPublicKey, + walletPublicKey.toString(), "timedOut" ) diff --git a/typescript/test/utils/mock-tbtc-token.ts b/typescript/test/utils/mock-tbtc-token.ts index b2238f550..ba7fdc08d 100644 --- a/typescript/test/utils/mock-tbtc-token.ts +++ b/typescript/test/utils/mock-tbtc-token.ts @@ -5,9 +5,9 @@ import { BitcoinUtxo } from "../../src/lib/bitcoin" import { EthereumAddress } from "../../src" interface RequestRedemptionLog { - walletPublicKey: string + walletPublicKey: Hex mainUtxo: BitcoinUtxo - redeemerOutputScript: string + redeemerOutputScript: Hex amount: BigNumber } @@ -23,9 +23,9 @@ export class MockTBTCToken implements TBTCToken { } async requestRedemption( - walletPublicKey: string, + walletPublicKey: Hex, mainUtxo: BitcoinUtxo, - redeemerOutputScript: string, + redeemerOutputScript: Hex, amount: BigNumber ): Promise { this._requestRedemptionLog.push({