Skip to content

Commit

Permalink
Ger rid of bcrypto dependency
Browse files Browse the repository at this point in the history
We use `bcrypto` to compute hashes, but since this library is a bit
problematic, here we replace it with hashing algorithms from the
`ethers` library. `ethers` has no `hash160` algorithm but under the hood
`HASH160 = RIPEMD160(SHA256(X))` so it's possible to compute `hash160`
using `ethers`.
  • Loading branch information
r-czajkowski committed Sep 6, 2023
1 parent 1d225c3 commit b6509de
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
1 change: 0 additions & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@keep-network/ecdsa": "development",
"@keep-network/tbtc-v2": "development",
"bcoin": "git+https://github.com/keep-network/bcoin.git#5accd32c63e6025a0d35d67739c4a6e84095a1f8",
"bcrypto": "git+https://github.com/bcoin-org/bcrypto.git#semver:~5.5.0",
"bufio": "^1.0.6",
"electrum-client-js": "git+https://github.com/keep-network/electrum-client-js.git#v0.1.1",
"ethers": "^5.5.2",
Expand Down
16 changes: 10 additions & 6 deletions typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import bcoin, { TX, Script } from "bcoin"
import wif from "wif"
import bufio from "bufio"
import hash160 from "bcrypto/lib/hash160"
import sha256 from "bcrypto/lib/sha256-browser.js"
import { BigNumber } from "ethers"
import { BigNumber, utils } from "ethers"
import { Hex } from "./hex"
import { BitcoinNetwork, toBcoinNetwork } from "./bitcoin-network"

Expand Down Expand Up @@ -525,7 +523,12 @@ export function createKeyRing(
* @returns Hash as a 20-byte un-prefixed hex string.
*/
export function computeHash160(text: string): string {
return hash160.digest(Buffer.from(text, "hex")).toString("hex")
const sha256Hash = utils.sha256(
Hex.from(Buffer.from(text, "hex")).toPrefixedString()
)
const hash160 = utils.ripemd160(sha256Hash)

return Hex.from(hash160).toString()
}

/**
Expand All @@ -534,8 +537,9 @@ export function computeHash160(text: string): string {
* @returns Hash as a 32-byte un-prefixed hex string.
*/
export function computeHash256(text: Hex): Hex {
const firstHash: Buffer = sha256.digest(text.toBuffer())
const secondHash: Buffer = sha256.digest(firstHash)
const firstHash = utils.sha256(text.toPrefixedString())
const secondHash = utils.sha256(firstHash)

return Hex.from(secondHash)
}

Expand Down
8 changes: 5 additions & 3 deletions typescript/src/electrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
} from "./bitcoin"
import { BitcoinNetwork } from "./bitcoin-network"
import Electrum from "electrum-client-js"
import sha256 from "bcrypto/lib/sha256-browser.js"
import { BigNumber } from "ethers"
import { BigNumber, utils } from "ethers"
import { URL } from "url"
import { Hex } from "./hex"
import { backoffRetrier, RetrierFn } from "./backoff"
Expand Down Expand Up @@ -557,5 +556,8 @@ export class Client implements BitcoinClient {
* @returns Electrum script hash as a hex string.
*/
function computeScriptHash(script: string): string {
return sha256.digest(Buffer.from(script, "hex")).reverse().toString("hex")
const _script = Hex.from(Buffer.from(script, "hex")).toPrefixedString()
const hash256 = utils.sha256(_script)

return Hex.from(hash256).reverse().toString()
}
2 changes: 0 additions & 2 deletions typescript/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* don't provide their own typings.
*/
declare module "bcoin"
declare module "bcrypto/lib/hash160"
declare module "bcrypto/lib/sha256-browser.js"
declare module "bufio"
declare module "electrum-client-js"
declare module "wif"
2 changes: 1 addition & 1 deletion typescript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@
"@openzeppelin/upgrades" "^2.7.2"
openzeppelin-solidity "2.4.0"

"@keep-network/keep-ecdsa@1.9.0-dev.1", "@keep-network/keep-ecdsa@>1.9.0-dev <1.9.0-ropsten":
"@keep-network/keep-ecdsa@>1.9.0-dev <1.9.0-ropsten":
version "1.9.0-dev.1"
resolved "https://registry.yarnpkg.com/@keep-network/keep-ecdsa/-/keep-ecdsa-1.9.0-dev.1.tgz#7522b47dd639ddd7479a0e71dc328a9e0bba7cae"
integrity sha512-FRIDejTUiQO7c9gBXgjtTp2sXkEQKFBBqVjYoZE20OCGRxbgum9FbgD/B5RWIctBy4GGr5wJHnA1789iaK3X6A==
Expand Down

0 comments on commit b6509de

Please sign in to comment.