Skip to content

Commit

Permalink
Merge branch 'install-bitcoinjs-lib' into deposit-sweep-use-bitcoinjs…
Browse files Browse the repository at this point in the history
…-lib
  • Loading branch information
tomaszslabon committed Sep 21, 2023
2 parents e609cee + e019dd1 commit 9aa8323
Show file tree
Hide file tree
Showing 6 changed files with 54 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",
"bitcoinjs-lib": "6.0.2",
"bufio": "^1.0.6",
"ecpair": "^2.1.0",
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"
import { payments } from "bitcoinjs-lib"
Expand Down Expand Up @@ -526,7 +524,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 @@ -535,8 +538,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
10 changes: 6 additions & 4 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 @@ -556,6 +555,9 @@ export class Client implements BitcoinClient {
* @param script - Bitcoin script as hex string
* @returns Electrum script hash as a hex string.
*/
function computeScriptHash(script: string): string {
return sha256.digest(Buffer.from(script, "hex")).reverse().toString("hex")
export function computeScriptHash(script: string): string {
const _script = Hex.from(Buffer.from(script, "hex")).toPrefixedString()
const hash256 = utils.sha256(_script)

return Hex.from(hash256).reverse().toString()
}
27 changes: 27 additions & 0 deletions typescript/test/bitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
createOutputScriptFromAddress,
createAddressFromOutputScript,
readCompactSizeUint,
computeHash160,
computeHash256,
} from "../src/bitcoin"
import { calculateDepositRefundLocktime } from "../src/deposit"
import { BitcoinNetwork } from "../src/bitcoin-network"
Expand Down Expand Up @@ -66,6 +68,31 @@ describe("Bitcoin", () => {
})
})

describe("computeHash160", () => {
it("should compute hash160 correctly", () => {
const compressedPublicKey =
"03474444cca71c678f5019d16782b6522735717a94602085b4adf707b465c36ca8"
const expectedHash160 = "3e1dfbd72483fb3964ca828ee71cf3270cafdc65"

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

describe("computeHash256", () => {
it("should compute hash256 correctly", () => {
const hexValue = Hex.from(
"03474444cca71c678f5019d16782b6522735717a94602085b4adf707b465c36ca8"
)
const expectedHash256 = Hex.from(
"9f0b7447ca6ea11b8badd8a60a4dec1b846451551ef455975b1720f52bc90546"
)

expect(computeHash256(hexValue).toString()).to.be.equal(
expectedHash256.toString()
)
})
})

describe("P2PKH <-> public key hash conversion", () => {
const publicKeyHash = "3a38d44d6a0c8d0bb84e0232cc632b7e48c72e0e"
const P2WPKHAddress = "bc1q8gudgnt2pjxshwzwqgevccet0eyvwtswt03nuy"
Expand Down
11 changes: 11 additions & 0 deletions typescript/test/electrum.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Credentials as ElectrumCredentials,
Client as ElectrumClient,
computeScriptHash,
} from "../src/electrum"
import { BitcoinNetwork } from "../src/bitcoin-network"
import {
Expand Down Expand Up @@ -217,6 +218,16 @@ describe("Electrum", () => {
expect(result).to.be.eql(testnetTransactionMerkleBranch)
})
})

describe("computeScriptHash", () => {
it("should convert Bitcoin script to an Electrum script hash correctly", () => {
const script = "00144b47c798d12edd17dfb4ea98e5447926f664731c"
const expectedScriptHash =
"cabdea0bfc10fb3521721dde503487dd1f0e41dd6609da228066757563f292ab"

expect(computeScriptHash(script)).to.be.equal(expectedScriptHash)
})
})
})
})

Expand Down
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"

0 comments on commit 9aa8323

Please sign in to comment.