Skip to content

Commit

Permalink
feat: remember custom derivation paths for swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Oct 21, 2024
1 parent ef8feec commit 3d925e7
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 34 deletions.
37 changes: 31 additions & 6 deletions src/components/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import HardwareDerivationPaths, { connect } from "./HardwareDerivationPaths";
const Modal = ({
show,
setShow,
derivationPath,
}: {
derivationPath: string;
show: Accessor<boolean>;
setShow: Setter<boolean>;
}) => {
Expand Down Expand Up @@ -50,7 +52,13 @@ const Modal = ({
return;
}

await connect(notify, connectProvider, provider);
await connect(
notify,
connectProvider,
providers,
provider,
derivationPath,
);
}}>
<hr />
<div
Expand Down Expand Up @@ -104,7 +112,7 @@ const Modal = ({
);
};

const ConnectModal = () => {
const ConnectModal = ({ derivationPath }: { derivationPath: string }) => {
const { t, notify } = useGlobalContext();
const { providers, connectProvider } = useWeb3Signer();

Expand All @@ -122,13 +130,19 @@ const ConnectModal = () => {
connect(
notify,
connectProvider,
providers,
Object.values(providers())[0].info,
derivationPath,
).then();
}
}}>
{t("connect_wallet")}
</button>
<Modal show={show} setShow={setShow} />
<Modal
show={show}
setShow={setShow}
derivationPath={derivationPath}
/>
</>
);
};
Expand Down Expand Up @@ -167,7 +181,11 @@ const ShowAddress = ({
);
};

export const ConnectAddress = ({ address }: { address: string }) => {
export const ConnectAddress = ({
address,
}: {
address: { address: string; derivationPath?: string };
}) => {
const { t, notify } = useGlobalContext();
const { connectProviderForAddress } = useWeb3Signer();

Expand All @@ -176,7 +194,10 @@ export const ConnectAddress = ({ address }: { address: string }) => {
class="btn"
onClick={async () => {
try {
await connectProviderForAddress(address);
await connectProviderForAddress(
address.address,
address.derivationPath,
);
} catch (e) {
log.error(
`Provider connect for address ${address} failed: ${formatError(e)}`,
Expand Down Expand Up @@ -213,8 +234,10 @@ export const SwitchNetwork = () => {
};

const ConnectWallet = ({
derivationPath,
addressOverride,
}: {
derivationPath?: string;
addressOverride?: Accessor<string | undefined>;
}) => {
const { t } = useGlobalContext();
Expand Down Expand Up @@ -249,7 +272,9 @@ const ConnectWallet = ({
{t("no_wallet")}
</button>
}>
<Show when={address() !== undefined} fallback={<ConnectModal />}>
<Show
when={address() !== undefined}
fallback={<ConnectModal derivationPath={derivationPath} />}>
<Show when={networkValid()} fallback={<SwitchNetwork />}>
<ShowAddress
address={address}
Expand Down
14 changes: 10 additions & 4 deletions src/components/ContractTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const ContractTransaction = ({
}: {
onClick: () => Promise<any>;
children?: any;
address: string;
showHr?: boolean;
buttonText: string;
promptText?: string;
showHr?: boolean;
waitingText?: string;
address: { address: string; derivationPath?: string };
}) => {
const { notify } = useGlobalContext();
const { signer, getContracts } = useWeb3Signer();
Expand All @@ -42,10 +42,16 @@ const ContractTransaction = ({
<Show
when={
signer() !== undefined &&
(address === signer().address || address === undefined)
(address === undefined || address.address === signer().address)
}
fallback={
<Show when={address !== undefined} fallback={<ConnectWallet />}>
<Show
when={address !== undefined}
fallback={
<ConnectWallet
derivationPath={address.derivationPath}
/>
}>
<ConnectAddress address={address} />
</Show>
}>
Expand Down
15 changes: 13 additions & 2 deletions src/components/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { SwapType } from "../consts/Enums";
import { ButtonLabelParams } from "../consts/Types";
import { useCreateContext } from "../context/Create";
import { useGlobalContext } from "../context/Global";
import { useWeb3Signer } from "../context/Web3";
import { customDerivationPathRdns, useWeb3Signer } from "../context/Web3";
import { GasNeededToClaim, getSmartWalletAddress } from "../rif/Signer";
import { fetchBolt12Invoice, getPairs } from "../utils/boltzClient";
import { formatAmount } from "../utils/denomination";
import { formatError } from "../utils/errors";
import { HardwareSigner } from "../utils/hardware/HadwareSigner";
import { coalesceLn } from "../utils/helper";
import { fetchBip353, fetchLnurl } from "../utils/invoice";
import {
Expand Down Expand Up @@ -62,7 +63,7 @@ export const CreateButton = () => {
bolt12Offer,
setBolt12Offer,
} = useCreateContext();
const { getEtherSwap, signer } = useWeb3Signer();
const { getEtherSwap, signer, providers } = useWeb3Signer();

const [buttonDisable, setButtonDisable] = createSignal(false);
const [buttonClass, setButtonClass] = createSignal("btn");
Expand Down Expand Up @@ -307,13 +308,23 @@ export const CreateButton = () => {
return;
}

console.log(providers()[signer().rdns].provider);
await setSwapStorage({
...data,
signer:
// We do not have to commit to a signer when creating submarine swaps
swapType() !== SwapType.Submarine
? signer()?.address
: undefined,
derivationPath:
swapType() !== SwapType.Submarine &&
signer() !== undefined &&
customDerivationPathRdns.includes(signer().rdns)
? (
providers()[signer().rdns]
.provider as unknown as HardwareSigner
).getDerivationPath()
: undefined,
});

setInvoice("");
Expand Down
15 changes: 9 additions & 6 deletions src/components/HardwareDerivationPaths.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ import LoadingSpinner from "./LoadingSpinner";
export const connect = async (
notify: (type: string, message: string) => void,
connectProvider: (rdns: string) => Promise<void>,
providers: Accessor<Record<string, EIP6963ProviderDetail>>,
provider: EIP6963ProviderInfo,
derivationPath?: string,
) => {
try {
if (derivationPath !== undefined) {
const prov = providers()[provider.rdns]
.provider as unknown as HardwareSigner;
prov.setDerivationPath(derivationPath);
}

await connectProvider(provider.rdns);
} catch (e) {
log.error(
Expand All @@ -51,12 +59,7 @@ const connectHardware = async (
try {
setLoading(true);

const hardwareProvider = provider();
const prov = providers()[hardwareProvider.rdns]
.provider as unknown as HardwareSigner;
prov.setDerivationPath(path);

await connect(notify, connectProvider, hardwareProvider);
await connect(notify, connectProvider, providers, provider(), path);
} finally {
setLoading(false);
}
Expand Down
19 changes: 15 additions & 4 deletions src/components/LockupEvm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Show, createEffect, createSignal } from "solid-js";

import { useGlobalContext } from "../context/Global";
import { useWeb3Signer } from "../context/Web3";
import { customDerivationPathRdns, useWeb3Signer } from "../context/Web3";
import { HardwareSigner } from "../utils/hardware/HadwareSigner";
import { prefix0x, satoshiToWei } from "../utils/rootstock";
import ConnectWallet from "./ConnectWallet";
import ContractTransaction from "./ContractTransaction";
Expand All @@ -26,16 +27,18 @@ const LockupEvm = ({
preimageHash,
claimAddress,
signerAddress,
derivationPath,
timeoutBlockHeight,
}: {
swapId: string;
amount: number;
preimageHash: string;
claimAddress: string;
signerAddress: string;
derivationPath?: string;
timeoutBlockHeight: number;
}) => {
const { getEtherSwap, signer } = useWeb3Signer();
const { getEtherSwap, signer, providers } = useWeb3Signer();
const { t, getSwap, setSwapStorage } = useGlobalContext();

const value = () => satoshiToWei(amount);
Expand All @@ -58,7 +61,7 @@ const LockupEvm = ({
fallback={<InsufficientBalance />}>
<ContractTransaction
onClick={async () => {
const contract = await getEtherSwap();
const contract = getEtherSwap();
const tx = await contract.lock(
prefix0x(preimageHash),
claimAddress,
Expand All @@ -70,10 +73,18 @@ const LockupEvm = ({
const currentSwap = await getSwap(swapId);
currentSwap.lockupTx = tx.hash;
currentSwap.signer = signer().address;

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

await setSwapStorage(currentSwap);
}}
children={<ConnectWallet />}
address={signerAddress}
address={{ derivationPath, address: signerAddress }}
buttonText={t("send")}
promptText={t("transaction_prompt", { button: t("send") })}
waitingText={t("tx_in_mempool_subline")}
Expand Down
10 changes: 7 additions & 3 deletions src/components/RefundButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ import ContractTransaction from "./ContractTransaction";

export const RefundEvm = ({
swapId,
setRefundTxHash,
amount,
claimAddress,
preimageHash,
signerAddress,
derivationPath,
setRefundTxHash,
timeoutBlockHeight,
}: {
swapId?: string;
setRefundTxHash?: Setter<string>;
amount: number;
preimageHash: string;
claimAddress: string;
signerAddress: string;
derivationPath?: string;
timeoutBlockHeight: number;
setRefundTxHash?: Setter<string>;
}) => {
const { setSwap } = usePayContext();
const { getEtherSwap, signer } = useWeb3Signer();
Expand Down Expand Up @@ -94,7 +96,7 @@ export const RefundEvm = ({

await tx.wait(1);
}}
address={signerAddress}
address={{ derivationPath, address: signerAddress }}
buttonText={t("refund")}
/>
);
Expand Down Expand Up @@ -131,6 +133,7 @@ const RefundButton = ({
signerAddress={submarine.signer}
amount={submarine.expectedAmount}
claimAddress={submarine.claimAddress}
derivationPath={submarine.derivationPath}
timeoutBlockHeight={submarine.timeoutBlockHeight}
preimageHash={decodeInvoice(submarine.invoice).preimageHash}
/>
Expand All @@ -143,6 +146,7 @@ const RefundButton = ({
swapId={chain.id}
signerAddress={chain.signer}
amount={chain.lockupDetails.amount}
derivationPath={chain.derivationPath}
claimAddress={chain.lockupDetails.claimAddress}
timeoutBlockHeight={chain.lockupDetails.timeoutBlockHeight}
preimageHash={crypto
Expand Down
Loading

0 comments on commit 3d925e7

Please sign in to comment.