Skip to content

Commit

Permalink
Used Hex for electrum
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 17, 2023
1 parent 21c5531 commit 5ca3037
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
13 changes: 5 additions & 8 deletions typescript/src/lib/electrum/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
BitcoinHashUtils,
} from "../bitcoin"
import Electrum from "electrum-client-js"
import { BigNumber, utils } from "ethers"
import { BigNumber } from "ethers"
import { URL } from "url"
import { backoffRetrier, Hex, RetrierFn } from "../utils"

Expand Down Expand Up @@ -261,7 +261,7 @@ export class ElectrumClient implements BitcoinClient {
const script = BitcoinAddressConverter.addressToOutputScript(
address,
bitcoinNetwork
).toString()
)

// eslint-disable-next-line camelcase
type UnspentOutput = { tx_pos: number; value: number; tx_hash: string }
Expand Down Expand Up @@ -292,7 +292,7 @@ export class ElectrumClient implements BitcoinClient {
const script = BitcoinAddressConverter.addressToOutputScript(
address,
bitcoinNetwork
).toString()
)

// eslint-disable-next-line camelcase
type HistoryItem = { height: number; tx_hash: string }
Expand Down Expand Up @@ -585,9 +585,6 @@ export class ElectrumClient implements BitcoinClient {
* @param script - Bitcoin script as hex string
* @returns Electrum script hash as a hex string.
*/
export function computeElectrumScriptHash(script: string): string {
const _script = Hex.from(Buffer.from(script, "hex")).toPrefixedString()
const hash256 = utils.sha256(_script)

return Hex.from(hash256).reverse().toString()
export function computeElectrumScriptHash(script: Hex): string {
return BitcoinHashUtils.computeSha256(script).reverse().toString()
}
5 changes: 4 additions & 1 deletion typescript/test/lib/electrum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ElectrumCredentials,
ElectrumClient,
computeElectrumScriptHash,
Hex,
} from "../../src"
import {
testnetAddress,
Expand Down Expand Up @@ -224,7 +225,9 @@ describe("Electrum", () => {

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

Expand Down

0 comments on commit 5ca3037

Please sign in to comment.