From aace2bca1288ca0a06a51a53d7ff8d58b491ad33 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Wed, 1 Nov 2023 02:11:58 +0100 Subject: [PATCH] Fix URL API error on the browser 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 --- typescript/src/lib/electrum/client.ts | 6 +++++- typescript/tsconfig.json | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/typescript/src/lib/electrum/client.ts b/typescript/src/lib/electrum/client.ts index 881f28564..6bcd6f3a8 100644 --- a/typescript/src/lib/electrum/client.ts +++ b/typescript/src/lib/electrum/client.ts @@ -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 = browserURL ?? nodeURL + /** * Represents a set of credentials required to establish an Electrum connection. */ @@ -143,6 +146,7 @@ export class ElectrumClient implements BitcoinClient { */ private static parseElectrumCredentials(url: string): ElectrumCredentials { const urlObj = new URL(url) + console.log({url, urlObj}) return { host: urlObj.hostname, diff --git a/typescript/tsconfig.json b/typescript/tsconfig.json index 8584d28ba..8f75788d7 100644 --- a/typescript/tsconfig.json +++ b/typescript/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "ES2017", "module": "commonjs", - "lib": ["es2020"], + "lib": ["es2020", "dom"], "declaration": true, "declarationMap": true, "sourceMap": true,