Skip to content

Commit

Permalink
Fix URL API error on the browser
Browse files Browse the repository at this point in the history
The SDK uses a Node implementation of the URL API which is not
complementary to the native browser implementation. I have added
a conditional that if you have window.URL available then use it,
and if you don't have it then fallback to the Node implementation
  • Loading branch information
kpyszkowski committed Nov 1, 2023
1 parent 79abf70 commit 637570e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion typescript/src/lib/electrum/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import {
} from "../bitcoin"
import Electrum from "electrum-client-js"
import { BigNumber } from "ethers"
import { URL } from "url"
import { URL as nodeURL } from "url"
import { backoffRetrier, Hex, RetrierFn } from "../utils"

import MainnetElectrumUrls from "./urls/mainnet.json"
import TestnetElectrumUrls from "./urls/testnet.json"

const browserURL = window.URL
const URL = nodeURL ?? browserURL

/**
* Represents a set of credentials required to establish an Electrum connection.
*/
Expand Down Expand Up @@ -143,6 +146,7 @@ export class ElectrumClient implements BitcoinClient {
*/
private static parseElectrumCredentials(url: string): ElectrumCredentials {
const urlObj = new URL(url)
console.log({url, urlObj})

Check failure on line 149 in typescript/src/lib/electrum/client.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Replace `url,·urlObj` with `·url,·urlObj·`

Check failure on line 149 in typescript/src/lib/electrum/client.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Replace `url,·urlObj` with `·url,·urlObj·`

return {
host: urlObj.hostname,
Expand Down
2 changes: 1 addition & 1 deletion typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"lib": ["es2020"],
"lib": ["es2020", "dom"],
"declaration": true,
"declarationMap": true,
"sourceMap": true,
Expand Down

0 comments on commit 637570e

Please sign in to comment.