Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jackstar12 committed Nov 3, 2024
1 parent dd15284 commit 6a13057
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/components/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ export const CreateButton = () => {
signer() !== undefined &&
customDerivationPathRdns.includes(signer().rdns)
? (
providers()[signer().rdns]
.provider as unknown as HardwareSigner
await providers()[signer().rdns]
.provider.get() as unknown as HardwareSigner
).getDerivationPath()
: undefined,
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/HardwareDerivationPaths.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export const connect = async (
) => {
try {
if (derivationPath !== undefined) {
const prov = providers()[provider.rdns]
.provider as unknown as HardwareSigner;
const prov = await providers()[provider.rdns]
.provider.get() as unknown as HardwareSigner;
prov.setDerivationPath(derivationPath);
}

Expand Down
6 changes: 4 additions & 2 deletions src/components/InvoiceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
decodeInvoice,
extractAddress,
extractInvoice,
isBolt12Offer,
Bolt12,
isLnurl,
} from "../utils/invoice";
import { validateInvoice } from "../utils/validation";
Expand Down Expand Up @@ -58,13 +58,15 @@ const InvoiceInput = () => {

const inputValue = extractInvoice(val);

const bolt12 = await Bolt12.get();

try {
input.setCustomValidity("");
setInvoiceError(undefined);
input.classList.remove("invalid");
if (isLnurl(inputValue)) {
setLnurl(inputValue);
} else if (await isBolt12Offer(inputValue)) {
} else if (bolt12.isOffer(inputValue)) {
setBolt12Offer(inputValue);
} else {
const sats = await validateInvoice(inputValue);
Expand Down
4 changes: 2 additions & 2 deletions src/components/LockupEvm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const LockupEvm = (props: {

if (customDerivationPathRdns.includes(signer().rdns)) {
currentSwap.derivationPath = (
providers()[signer().rdns]
.provider as unknown as HardwareSigner
await providers()[signer().rdns]
.provider.get() as unknown as HardwareSigner
).getDerivationPath();
}

Expand Down
3 changes: 2 additions & 1 deletion src/consts/Types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DictKey } from "../i18n/i18n";
import Loader from "../lazy/Loader";

export type ButtonLabelParams = {
key: DictKey;
Expand Down Expand Up @@ -36,5 +37,5 @@ export type EIP1193Provider = {

export type EIP6963ProviderDetail = {
info: EIP6963ProviderInfo;
provider: EIP1193Provider;
provider: Loader<EIP1193Provider>;
};
25 changes: 15 additions & 10 deletions src/context/Web3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import TrezorSigner from "../utils/hardware/TrezorSigner";
import { useGlobalContext } from "./Global";
import LedgerIcon from "/ledger.svg";
import TrezorIcon from "/trezor.svg";
import Loader from "../lazy/Loader";

declare global {
interface WindowEventMap {
Expand Down Expand Up @@ -86,7 +87,7 @@ const Web3SignerProvider = (props: {
Record<string, EIP6963ProviderDetail>
>({
[HardwareRdns.Ledger]: {
provider: new LedgerSigner(t),
provider: LedgerSigner.loader(t),
info: {
name: "Ledger",
uuid: "ledger",
Expand All @@ -97,7 +98,7 @@ const Web3SignerProvider = (props: {
},
},
[HardwareRdns.Trezor]: {
provider: new TrezorSigner(),
provider: TrezorSigner.loader,
info: {
name: "Trezor",
uuid: "trezor",
Expand All @@ -117,7 +118,7 @@ const Web3SignerProvider = (props: {
setProviders({
...providers(),
[browserRdns]: {
provider: window.ethereum,
provider: new Loader("Ethereum", async () => window.ethereum),

Check failure on line 121 in src/context/Web3.tsx

View workflow job for this annotation

GitHub Actions / ci

Async arrow function has no 'await' expression

Check failure on line 121 in src/context/Web3.tsx

View workflow job for this annotation

GitHub Actions / ci

Async arrow function has no 'await' expression
info: {
name: "Browser native",
uuid: browserRdns,
Expand All @@ -141,7 +142,10 @@ const Web3SignerProvider = (props: {

setProviders({
...existingProviders,
[event.detail.info.rdns]: event.detail,
[event.detail.info.rdns]: {
provider: new Loader("EIP6963", async () => event.detail.provider),

Check failure on line 146 in src/context/Web3.tsx

View workflow job for this annotation

GitHub Actions / ci

Async arrow function has no 'await' expression

Check failure on line 146 in src/context/Web3.tsx

View workflow job for this annotation

GitHub Actions / ci

Async arrow function has no 'await' expression
info: event.detail.info,
},
});
},
);
Expand Down Expand Up @@ -175,8 +179,8 @@ const Web3SignerProvider = (props: {
`Setting derivation path (${derivationPath}) for signer:`,
rdns,
);
const prov = providers()[rdns]
.provider as unknown as HardwareSigner;
const prov = await providers()[rdns]
.provider.get() as unknown as HardwareSigner;
prov.setDerivationPath(derivationPath);
}

Expand All @@ -188,9 +192,10 @@ const Web3SignerProvider = (props: {
if (wallet == undefined) {
throw "wallet not found";
}
const provider = await wallet.provider.get()

log.debug(`Using wallet ${wallet.info.rdns}: ${wallet.info.name}`);
const addresses = (await wallet.provider.request({
const addresses = (await provider.request({
method: "eth_requestAccounts",
})) as string[];
if (addresses.length === 0) {
Expand All @@ -199,13 +204,13 @@ const Web3SignerProvider = (props: {

log.info(`Connected address from ${wallet.info.rdns}: ${addresses[0]}`);

wallet.provider.on("chainChanged", () => {
provider.on("chainChanged", () => {
window.location.reload();
});
setRawProvider(wallet.provider);
setRawProvider(provider);

const signer = new JsonRpcSigner(
new BrowserProvider(wallet.provider),
new BrowserProvider(provider),
addresses[0],
) as unknown as Signer;
signer.rdns = wallet.info.rdns;
Expand Down
4 changes: 3 additions & 1 deletion src/lazy/Loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import log from "loglevel";

export type LoadType<R> = Awaited<ReturnType<R>>

class Loader<T> {
private modules?: T;

Expand All @@ -10,7 +12,7 @@ class Loader<T> {

public get = async (): Promise<T> => {
if (this.modules === undefined) {
log.info(`Loading ${this.name} modules`);
log.info(`Loading ${this.name}`);
this.modules = await this.initializer();
}

Expand Down
4 changes: 1 addition & 3 deletions src/lazy/bolt12.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Loader from "./Loader";

export default new Loader("BOLT12", async () => await import("boltz-bolt12"));
export const load = () => import("boltz-bolt12");
9 changes: 5 additions & 4 deletions src/lazy/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type Transport from "@ledgerhq/hw-transport";
import { LoadType } from "./Loader";

import Loader from "./Loader";

export default new Loader("Ledger", async () => {
export const load = async () => {
const [eth, webhid] = await Promise.all([
import("@ledgerhq/hw-app-eth"),
import("@ledgerhq/hw-transport-webhid"),
Expand All @@ -12,6 +11,8 @@ export default new Loader("Ledger", async () => {
eth: eth.default,
webhid: webhid.default,
};
});
}

export type LedgerModules = LoadType<typeof load>;

export { Transport };
9 changes: 5 additions & 4 deletions src/lazy/trezor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import type {
Response,
SuccessWithDevice,
} from "@trezor/connect/lib/types/params";
import { LoadType } from "./Loader";

import Loader from "./Loader";

export default new Loader("Trezor", async () => {
export const load = async () => {
return (await import("@trezor/connect-web")).default;
});
}

export type TrezorConnect = LoadType<typeof load>;

export { Address, Unsuccessful, Response, SuccessWithDevice };
5 changes: 3 additions & 2 deletions src/utils/boltzClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Transaction as LiquidTransaction } from "liquidjs-lib";
import { config } from "../config";
import { SwapType } from "../consts/Enums";
import { fetcher } from "./helper";
import { validateInvoiceForOffer } from "./invoice";
import { Bolt12 } from "./invoice";

const cooperativeErrorMessage = "cooperative signatures for swaps are disabled";
const checkCooperative = () => {
Expand Down Expand Up @@ -190,7 +190,8 @@ export const fetchBolt12Invoice = async (
amount: amountSat,
},
);
await validateInvoiceForOffer(offer, res.invoice);
const bolt12 = await Bolt12.get();
bolt12.validateInvoiceForOffer(offer, res.invoice);

return res;
};
Expand Down
Loading

0 comments on commit 6a13057

Please sign in to comment.