From ce3dcff7a43ab0165158f357c9d2ac4684f7805c Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Tue, 17 Oct 2023 13:32:57 +0200 Subject: [PATCH] Used Hex for walletPublicKeyHash and redeemerOutputScript --- typescript/src/lib/contracts/bridge.ts | 11 ++-- typescript/src/lib/ethereum/bridge.ts | 6 +-- .../src/services/maintenance/wallet-tx.ts | 4 +- typescript/test/data/redemption.ts | 53 +++++++++++++------ typescript/test/lib/ethereum.test.ts | 10 ++-- typescript/test/services/maintenance.test.ts | 2 +- typescript/test/services/redemptions.test.ts | 22 ++++---- typescript/test/utils/mock-bridge.ts | 2 +- 8 files changed, 65 insertions(+), 45 deletions(-) diff --git a/typescript/src/lib/contracts/bridge.ts b/typescript/src/lib/contracts/bridge.ts index 8742b6cf3..58c3dd786 100644 --- a/typescript/src/lib/contracts/bridge.ts +++ b/typescript/src/lib/contracts/bridge.ts @@ -294,10 +294,10 @@ export interface RedemptionRequest { redeemer: ChainIdentifier /** - * The output script the redeemed Bitcoin funds are locked to. It is un-prefixed - * and is not prepended with length. + * The output script the redeemed Bitcoin funds are locked to. It is not + * prepended with length. */ - redeemerOutputScript: string + redeemerOutputScript: Hex /** * The amount of Bitcoins in satoshis that is requested to be redeemed. @@ -334,10 +334,9 @@ export type RedemptionRequestedEvent = Omit< "requestedAt" > & { /** - * Public key hash of the wallet that is meant to handle the redemption. Must - * be an unprefixed hex string (without 0x prefix). + * Public key hash of the wallet that is meant to handle the redemption. */ - walletPublicKeyHash: string + walletPublicKeyHash: Hex } & ChainEvent /* eslint-disable no-unused-vars */ diff --git a/typescript/src/lib/ethereum/bridge.ts b/typescript/src/lib/ethereum/bridge.ts index 924523148..25f059a0f 100644 --- a/typescript/src/lib/ethereum/bridge.ts +++ b/typescript/src/lib/ethereum/bridge.ts @@ -215,7 +215,7 @@ export class EthereumBridge ): RedemptionRequest { return { redeemer: EthereumAddress.from(request.redeemer), - redeemerOutputScript: redeemerOutputScript.toString(), + redeemerOutputScript: redeemerOutputScript, requestedAmount: BigNumber.from(request.requestedAmount), treasuryFee: BigNumber.from(request.treasuryFee), txMaxFee: BigNumber.from(request.txMaxFee), @@ -653,9 +653,9 @@ export class EthereumBridge blockNumber: BigNumber.from(event.blockNumber).toNumber(), blockHash: Hex.from(event.blockHash), transactionHash: Hex.from(event.transactionHash), - walletPublicKeyHash: Hex.from(event.args!.walletPubKeyHash).toString(), + walletPublicKeyHash: Hex.from(event.args!.walletPubKeyHash), redeemer: EthereumAddress.from(event.args!.redeemer), - redeemerOutputScript: redeemerOutputScript, + redeemerOutputScript: Hex.from(redeemerOutputScript), requestedAmount: BigNumber.from(event.args!.requestedAmount), treasuryFee: BigNumber.from(event.args!.treasuryFee), txMaxFee: BigNumber.from(event.args!.txMaxFee), diff --git a/typescript/src/services/maintenance/wallet-tx.ts b/typescript/src/services/maintenance/wallet-tx.ts index 4e2ae267a..e170cacf6 100644 --- a/typescript/src/services/maintenance/wallet-tx.ts +++ b/typescript/src/services/maintenance/wallet-tx.ts @@ -597,7 +597,7 @@ class Redemption { redemptionRequests.push({ ...redemptionRequest, - redeemerOutputScript: redeemerOutputScript, + redeemerOutputScript: Hex.from(redeemerOutputScript), }) } @@ -710,7 +710,7 @@ class Redemption { txTotalFee = txTotalFee.add(request.txMaxFee) psbt.addOutput({ - script: Buffer.from(request.redeemerOutputScript, "hex"), + script: request.redeemerOutputScript.toBuffer(), value: outputValue.toNumber(), }) } diff --git a/typescript/test/data/redemption.ts b/typescript/test/data/redemption.ts index f840e2b6a..3d806da34 100644 --- a/typescript/test/data/redemption.ts +++ b/typescript/test/data/redemption.ts @@ -84,8 +84,9 @@ export const singleP2PKHRedemptionWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2PKH address mmTeMR8RKu6QzMGTG4ipA71uewm3EuJng5 - redeemerOutputScript: - "76a9144130879211c54df460e484ddf9aac009cb38ee7488ac", + redeemerOutputScript: Hex.from( + "76a9144130879211c54df460e484ddf9aac009cb38ee7488ac" + ), requestedAmount: BigNumber.from(10000), treasuryFee: BigNumber.from(1000), txMaxFee: BigNumber.from(1600), @@ -141,7 +142,9 @@ export const singleP2WPKHRedemptionWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2WPKH address tb1qgycg0ys3c4xlgc8ysnwln2kqp89n3mn5ts7z3l - redeemerOutputScript: "00144130879211c54df460e484ddf9aac009cb38ee74", + redeemerOutputScript: Hex.from( + "00144130879211c54df460e484ddf9aac009cb38ee74" + ), requestedAmount: BigNumber.from(15000), treasuryFee: BigNumber.from(1100), txMaxFee: BigNumber.from(1700), @@ -197,7 +200,9 @@ export const singleP2SHRedemptionWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2SH address 2Mxy76sc1qAxiJ1fXMXDXqHvVcPLh6Lf12C - redeemerOutputScript: "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87", + redeemerOutputScript: Hex.from( + "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87" + ), requestedAmount: BigNumber.from(13000), treasuryFee: BigNumber.from(800), txMaxFee: BigNumber.from(1700), @@ -253,8 +258,9 @@ export const singleP2WSHRedemptionWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2WSH address tb1qs63s8nwjut4tr5t8nudgzwp4m3dpkefjzpmumn90pruce0cye2tq2jkq0y - redeemerOutputScript: - "002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96", + redeemerOutputScript: Hex.from( + "002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96" + ), requestedAmount: BigNumber.from(18000), treasuryFee: BigNumber.from(1000), txMaxFee: BigNumber.from(1400), @@ -310,8 +316,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2PKH address mmTeMR8RKu6QzMGTG4ipA71uewm3EuJng5 - redeemerOutputScript: - "76a9144130879211c54df460e484ddf9aac009cb38ee7488ac", + redeemerOutputScript: Hex.from( + "76a9144130879211c54df460e484ddf9aac009cb38ee7488ac" + ), requestedAmount: BigNumber.from(18000), treasuryFee: BigNumber.from(1000), txMaxFee: BigNumber.from(1100), @@ -326,7 +333,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2WPKH address tb1qgycg0ys3c4xlgc8ysnwln2kqp89n3mn5ts7z3l - redeemerOutputScript: "00144130879211c54df460e484ddf9aac009cb38ee74", + redeemerOutputScript: Hex.from( + "00144130879211c54df460e484ddf9aac009cb38ee74" + ), requestedAmount: BigNumber.from(13000), treasuryFee: BigNumber.from(800), txMaxFee: BigNumber.from(900), @@ -341,7 +350,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2SH address 2Mxy76sc1qAxiJ1fXMXDXqHvVcPLh6Lf12C - redeemerOutputScript: "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87", + redeemerOutputScript: Hex.from( + "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87" + ), requestedAmount: BigNumber.from(12000), treasuryFee: BigNumber.from(1100), txMaxFee: BigNumber.from(1000), @@ -356,8 +367,9 @@ export const multipleRedemptionsWithWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2WSH address tb1qs63s8nwjut4tr5t8nudgzwp4m3dpkefjzpmumn90pruce0cye2tq2jkq0y - redeemerOutputScript: - "002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96", + redeemerOutputScript: Hex.from( + "002086a303cdd2e2eab1d1679f1a813835dc5a1b65321077cdccaf08f98cbf04ca96" + ), requestedAmount: BigNumber.from(15000), treasuryFee: BigNumber.from(700), txMaxFee: BigNumber.from(1400), @@ -416,8 +428,9 @@ export const multipleRedemptionsWithoutChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2PKH address mmTeMR8RKu6QzMGTG4ipA71uewm3EuJng5 - redeemerOutputScript: - "76a9144130879211c54df460e484ddf9aac009cb38ee7488ac", + redeemerOutputScript: Hex.from( + "76a9144130879211c54df460e484ddf9aac009cb38ee7488ac" + ), requestedAmount: BigNumber.from(6000), treasuryFee: BigNumber.from(0), txMaxFee: BigNumber.from(800), @@ -432,7 +445,9 @@ export const multipleRedemptionsWithoutChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2WPKH address tb1qf0ulldawp79s7knz9v254j5zjyn0demfx2d0xx - redeemerOutputScript: "00144bf9ffb7ae0f8b0f5a622b154aca829126f6e769", + redeemerOutputScript: Hex.from( + "00144bf9ffb7ae0f8b0f5a622b154aca829126f6e769" + ), requestedAmount: BigNumber.from(4000), treasuryFee: BigNumber.from(0), txMaxFee: BigNumber.from(900), @@ -491,7 +506,9 @@ export const singleP2SHRedemptionWithNonWitnessChange: RedemptionTestData = { "82883a4c7a8dd73ef165deb402d432613615ced4" ), // script for testnet P2SH address 2Mxy76sc1qAxiJ1fXMXDXqHvVcPLh6Lf12C - redeemerOutputScript: "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87", + redeemerOutputScript: Hex.from( + "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87" + ), requestedAmount: BigNumber.from(12000), treasuryFee: BigNumber.from(1000), txMaxFee: BigNumber.from(1200), @@ -902,7 +919,9 @@ export const findWalletForRedemptionData: { "0xeb9af8E66869902476347a4eFe59a527a57240ED" ), // script for testnet P2PKH address mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc - redeemerOutputScript: "76a9142cd680318747b720d67bf4246eb7403b476adb3488ac", + redeemerOutputScript: Hex.from( + "76a9142cd680318747b720d67bf4246eb7403b476adb3488ac" + ), requestedAmount: BigNumber.from(1000000), treasuryFee: BigNumber.from(20000), txMaxFee: BigNumber.from(20000), diff --git a/typescript/test/lib/ethereum.test.ts b/typescript/test/lib/ethereum.test.ts index da2653f77..42fe48453 100644 --- a/typescript/test/lib/ethereum.test.ts +++ b/typescript/test/lib/ethereum.test.ts @@ -83,8 +83,9 @@ describe("Ethereum", () => { redeemer: EthereumAddress.from( "f39fd6e51aad88f6f4ce6ab8827279cfffb92266" ), - redeemerOutputScript: - "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87", + redeemerOutputScript: Hex.from( + "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87" + ), requestedAmount: BigNumber.from(10000), treasuryFee: BigNumber.from(100), txMaxFee: BigNumber.from(50), @@ -124,8 +125,9 @@ describe("Ethereum", () => { redeemer: EthereumAddress.from( "f39fd6e51aad88f6f4ce6ab8827279cfffb92266" ), - redeemerOutputScript: - "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87", + redeemerOutputScript: Hex.from( + "a9143ec459d0f3c29286ae5df5fcc421e2786024277e87" + ), requestedAmount: BigNumber.from(10000), treasuryFee: BigNumber.from(100), txMaxFee: BigNumber.from(50), diff --git a/typescript/test/services/maintenance.test.ts b/typescript/test/services/maintenance.test.ts index cbca52a78..f21d253c9 100644 --- a/typescript/test/services/maintenance.test.ts +++ b/typescript/test/services/maintenance.test.ts @@ -1450,7 +1450,7 @@ describe("Maintenance", () => { it("should revert", async () => { const redeemerOutputScripts = data.pendingRedemptions.map( (redemption) => - redemption.pendingRedemption.redeemerOutputScript + redemption.pendingRedemption.redeemerOutputScript.toString() ) const walletTx = new WalletTx( diff --git a/typescript/test/services/redemptions.test.ts b/typescript/test/services/redemptions.test.ts index 7b22f26ac..384568c41 100644 --- a/typescript/test/services/redemptions.test.ts +++ b/typescript/test/services/redemptions.test.ts @@ -105,7 +105,7 @@ describe("Redemptions", () => { await redemptionsService.requestRedemption( BitcoinAddressConverter.outputScriptToAddress( - Hex.from(redeemerOutputScript), + redeemerOutputScript, BitcoinNetwork.Testnet ), amount @@ -119,7 +119,7 @@ describe("Redemptions", () => { expect(tokenLog[0]).to.deep.equal({ walletPublicKey, mainUtxo, - redeemerOutputScript, + redeemerOutputScript: redeemerOutputScript.toString(), amount, }) }) @@ -153,7 +153,7 @@ describe("Redemptions", () => { const actualRedemptionRequest = await redemptionsService.getRedemptionRequests( BitcoinAddressConverter.outputScriptToAddress( - Hex.from(redemptionRequest.redeemerOutputScript), + redemptionRequest.redeemerOutputScript, BitcoinNetwork.Testnet ), walletPublicKey, @@ -191,7 +191,7 @@ describe("Redemptions", () => { const actualRedemptionRequest = await redemptionsService.getRedemptionRequests( BitcoinAddressConverter.outputScriptToAddress( - Hex.from(redemptionRequest.redeemerOutputScript), + redemptionRequest.redeemerOutputScript, BitcoinNetwork.Testnet ), walletPublicKey, @@ -377,7 +377,7 @@ describe("Redemptions", () => { const key = MockBridge.buildRedemptionKey( walletPublicKeyHash.toString(), - redeemerOutputScript + redeemerOutputScript.toString() ) pendingRedemptions.set( @@ -387,7 +387,7 @@ describe("Redemptions", () => { tbtcContracts.bridge.setPendingRedemptions(pendingRedemptions) result = await redemptionsService.findWalletForRedemption( - redeemerOutputScript, + redeemerOutputScript.toString(), amount ) }) @@ -466,12 +466,12 @@ describe("Redemptions", () => { const pendingRedemption1 = MockBridge.buildRedemptionKey( walletPublicKeyHash.toString(), - redeemerOutputScript + redeemerOutputScript.toString() ) const pendingRedemption2 = MockBridge.buildRedemptionKey( findWalletForRedemptionData.liveWallet.event.walletPublicKeyHash.toString(), - redeemerOutputScript + redeemerOutputScript.toString() ) pendingRedemptions.set( @@ -489,7 +489,7 @@ describe("Redemptions", () => { it("should throw an error", async () => { await expect( redemptionsService.findWalletForRedemption( - redeemerOutputScript, + redeemerOutputScript.toString(), amount ) ).to.be.rejectedWith( @@ -825,8 +825,8 @@ export async function runRedemptionScenario( ) ) - const redeemerOutputScripts = data.pendingRedemptions.map( - (redemption) => redemption.pendingRedemption.redeemerOutputScript + const redeemerOutputScripts = data.pendingRedemptions.map((redemption) => + redemption.pendingRedemption.redeemerOutputScript.toString() ) const walletTx = new WalletTx(tbtcContracts, bitcoinClient, data.witness) diff --git a/typescript/test/utils/mock-bridge.ts b/typescript/test/utils/mock-bridge.ts index 0a7c143de..31cb72365 100644 --- a/typescript/test/utils/mock-bridge.ts +++ b/typescript/test/utils/mock-bridge.ts @@ -305,7 +305,7 @@ export class MockBridge implements Bridge { ? (redemptionsMap.get(redemptionKey) as RedemptionRequest) : { redeemer: EthereumAddress.from(constants.AddressZero), - redeemerOutputScript: "", + redeemerOutputScript: Hex.from(""), requestedAmount: BigNumber.from(0), treasuryFee: BigNumber.from(0), txMaxFee: BigNumber.from(0),