Skip to content

Commit

Permalink
Used Hex as return value in computeHash160
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 13, 2023
1 parent 00f0e9e commit 8b156c7
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions typescript/src/lib/bitcoin/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions typescript/src/lib/ethereum/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class EthereumBridge
redeemerOutputScript: string
): Promise<RedemptionRequest> {
const redemptionKey = EthereumBridge.buildRedemptionKey(
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)),
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(),
redeemerOutputScript
)

Expand All @@ -156,7 +156,7 @@ export class EthereumBridge
redeemerOutputScript: string
): Promise<RedemptionRequest> {
const redemptionKey = EthereumBridge.buildRedemptionKey(
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)),
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(),
redeemerOutputScript
)

Expand Down
5 changes: 4 additions & 1 deletion typescript/src/services/deposits/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/services/deposits/deposits-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class DepositsService {

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

const bitcoinNetwork = await this.bitcoinClient.getNetwork()

Expand Down
2 changes: 1 addition & 1 deletion typescript/src/services/deposits/refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/services/maintenance/wallet-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion typescript/test/lib/bitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ describe("Bitcoin", () => {
)
const expectedHash160 = "3e1dfbd72483fb3964ca828ee71cf3270cafdc65"

expect(computeHash160(compressedPublicKey)).to.be.equal(expectedHash160)
expect(computeHash160(compressedPublicKey).toString()).to.be.equal(
expectedHash160
)
})
})

Expand Down
4 changes: 2 additions & 2 deletions typescript/test/lib/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions typescript/test/services/redemptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/utils/mock-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export class MockBridge implements Bridge {
redemptionsMap: Map<BigNumberish, RedemptionRequest>
): RedemptionRequest {
const redemptionKey = MockBridge.buildRedemptionKey(
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)),
BitcoinHashUtils.computeHash160(Hex.from(walletPublicKey)).toString(),
redeemerOutputScript
)

Expand Down

0 comments on commit 8b156c7

Please sign in to comment.