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 5faf6b4
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 23 deletions.
10 changes: 5 additions & 5 deletions typescript/src/lib/bitcoin/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ 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)
}

/**
* Computes the double SHA256 for the given text.
* @param text - Text the double SHA256 is computed for.
* @returns Hash as a 32-byte un-prefixed hex string.
* @returns 32-byte-long hash.
* @dev Do not confuse it with computeSha256 which computes single SHA256.
*/
function computeHash256(text: Hex): Hex {
Expand All @@ -38,7 +38,7 @@ function hashLEToBigNumber(hash: Hex): BigNumber {
/**
* Computes the single SHA256 for the given text.
* @param text - Text the single SHA256 is computed for.
* @returns Hash as a 32-byte un-prefixed hex string.
* @returns 32-byte-long hash.
* @dev Do not confuse it with computeHash256 which computes double SHA256.
*/
function computeSha256(text: Hex): Hex {
Expand Down
12 changes: 6 additions & 6 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 Expand Up @@ -334,9 +334,9 @@ export class EthereumBridge
redeemerOutputScript: string,
amount: BigNumber
): Promise<void> {
const walletPublicKeyHash = `0x${BitcoinHashUtils.computeHash160(
const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
)}`
).toPrefixedString()

const mainUtxoParam = {
// The Ethereum Bridge expects this hash to be in the Bitcoin internal
Expand Down Expand Up @@ -398,9 +398,9 @@ export class EthereumBridge
txOutputValue: mainUtxo.value,
}

const walletPublicKeyHash = `0x${BitcoinHashUtils.computeHash160(
const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
)}`
).toPrefixedString()

await EthersTransactionUtils.sendWithRetry<ContractTransaction>(
async () => {
Expand Down
4 changes: 2 additions & 2 deletions typescript/src/lib/ethereum/tbtc-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ export class EthereumTBTCToken
mainUtxo: BitcoinUtxo,
redeemerOutputScript: string
) {
const walletPublicKeyHash = `0x${BitcoinHashUtils.computeHash160(
const walletPublicKeyHash = BitcoinHashUtils.computeHash160(
Hex.from(walletPublicKey)
)}`
).toPrefixedString()

const mainUtxoParam = {
// The Ethereum Bridge expects this hash to be in the Bitcoin internal
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/services/deposits/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ 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")
: BitcoinHashUtils.computeHash160(Hex.from(script)).toBuffer()
}

/**
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 5faf6b4

Please sign in to comment.