diff --git a/typescript/src/lib/bitcoin/hash.ts b/typescript/src/lib/bitcoin/hash.ts index 79c1f277a..f7764c6a7 100644 --- a/typescript/src/lib/bitcoin/hash.ts +++ b/typescript/src/lib/bitcoin/hash.ts @@ -4,13 +4,13 @@ import { Hex } from "../utils" /** * Computes the HASH160 for the given text. * @param text - Text the HASH160 is computed for. - * @returns Hash as a 20-byte un-prefixed hex string. + * @returns 20-byte-long hash. */ -function computeHash160(text: Hex): string { +function computeHash160(text: Hex): Hex { const sha256Hash = utils.sha256(text.toPrefixedString()) const hash160 = utils.ripemd160(sha256Hash) - return Hex.from(hash160).toString() + return Hex.from(hash160) } /** diff --git a/typescript/src/lib/ethereum/bridge.ts b/typescript/src/lib/ethereum/bridge.ts index f20d70d19..e8e409085 100644 --- a/typescript/src/lib/ethereum/bridge.ts +++ b/typescript/src/lib/ethereum/bridge.ts @@ -133,7 +133,7 @@ export class EthereumBridge redeemerOutputScript: string ): Promise { const redemptionKey = EthereumBridge.buildRedemptionKey( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)), + BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(), redeemerOutputScript ) @@ -156,7 +156,7 @@ export class EthereumBridge redeemerOutputScript: string ): Promise { const redemptionKey = EthereumBridge.buildRedemptionKey( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)), + BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(), redeemerOutputScript ) diff --git a/typescript/src/services/deposits/deposit.ts b/typescript/src/services/deposits/deposit.ts index 49d160de1..80a3dc93a 100644 --- a/typescript/src/services/deposits/deposit.ts +++ b/typescript/src/services/deposits/deposit.ts @@ -186,7 +186,10 @@ export class DepositScript { // Legacy script hash needs HASH160. return this.witness ? BitcoinHashUtils.computeSha256(Hex.from(script)).toBuffer() - : Buffer.from(BitcoinHashUtils.computeHash160(Hex.from(script)), "hex") + : Buffer.from( + BitcoinHashUtils.computeHash160(Hex.from(script)).toString(), + "hex" + ) } /** diff --git a/typescript/src/services/deposits/deposits-service.ts b/typescript/src/services/deposits/deposits-service.ts index 1ebfcb9a7..b8fba8d21 100644 --- a/typescript/src/services/deposits/deposits-service.ts +++ b/typescript/src/services/deposits/deposits-service.ts @@ -78,7 +78,7 @@ export class DepositsService { const walletPublicKeyHash = BitcoinHashUtils.computeHash160( Hex.from(walletPublicKey) - ) + ).toString() const bitcoinNetwork = await this.bitcoinClient.getNetwork() diff --git a/typescript/src/services/deposits/refund.ts b/typescript/src/services/deposits/refund.ts index 66821e4c2..81d331704 100644 --- a/typescript/src/services/deposits/refund.ts +++ b/typescript/src/services/deposits/refund.ts @@ -192,7 +192,7 @@ export class DepositRefund { const refunderPublicKey = refunderKeyPair.publicKey.toString("hex") if ( - BitcoinHashUtils.computeHash160(Hex.from(refunderPublicKey)) != + BitcoinHashUtils.computeHash160(Hex.from(refunderPublicKey)).toString() != this.script.receipt.refundPublicKeyHash ) { throw new Error( diff --git a/typescript/src/services/maintenance/wallet-tx.ts b/typescript/src/services/maintenance/wallet-tx.ts index b461f6599..b0306cdff 100644 --- a/typescript/src/services/maintenance/wallet-tx.ts +++ b/typescript/src/services/maintenance/wallet-tx.ts @@ -469,7 +469,7 @@ class DepositSweep { const walletPublicKey = walletKeyPair.publicKey.toString("hex") if ( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)) != + BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString() != deposit.walletPublicKeyHash ) { throw new Error( diff --git a/typescript/test/lib/bitcoin.test.ts b/typescript/test/lib/bitcoin.test.ts index c144f617e..cdb706947 100644 --- a/typescript/test/lib/bitcoin.test.ts +++ b/typescript/test/lib/bitcoin.test.ts @@ -158,7 +158,9 @@ describe("Bitcoin", () => { ) const expectedHash160 = "3e1dfbd72483fb3964ca828ee71cf3270cafdc65" - expect(computeHash160(compressedPublicKey)).to.be.equal(expectedHash160) + expect(computeHash160(compressedPublicKey).toString()).to.be.equal( + expectedHash160 + ) }) }) diff --git a/typescript/test/lib/ethereum.test.ts b/typescript/test/lib/ethereum.test.ts index 806297a0c..11b4086d6 100644 --- a/typescript/test/lib/ethereum.test.ts +++ b/typescript/test/lib/ethereum.test.ts @@ -556,8 +556,8 @@ describe("Ethereum", () => { ["address", "bytes20", "bytes32", "uint32", "uint64", "bytes"], [ redeemer.identifierHex, - Hex.from( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)) + BitcoinHashUtils.computeHash160( + Hex.from(walletPublicKey) ).toPrefixedString(), mainUtxo.transactionHash.reverse().toPrefixedString(), mainUtxo.outputIndex, diff --git a/typescript/test/services/redemptions.test.ts b/typescript/test/services/redemptions.test.ts index 04cd7252b..7b22f26ac 100644 --- a/typescript/test/services/redemptions.test.ts +++ b/typescript/test/services/redemptions.test.ts @@ -53,8 +53,8 @@ describe("Redemptions", () => { tbtcContracts = new MockTBTCContracts() bitcoinClient = new MockBitcoinClient() - const walletPublicKeyHash = Hex.from( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)) + const walletPublicKeyHash = BitcoinHashUtils.computeHash160( + Hex.from(walletPublicKey) ) // Prepare NewWalletRegisteredEvent history. Set only relevant fields. diff --git a/typescript/test/utils/mock-bridge.ts b/typescript/test/utils/mock-bridge.ts index 48cd4603f..fe6a8a27c 100644 --- a/typescript/test/utils/mock-bridge.ts +++ b/typescript/test/utils/mock-bridge.ts @@ -295,7 +295,7 @@ export class MockBridge implements Bridge { redemptionsMap: Map ): RedemptionRequest { const redemptionKey = MockBridge.buildRedemptionKey( - BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)), + BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(), redeemerOutputScript )