Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/app/screens/connectors/ConnectSpark/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useNavigate } from "react-router-dom";
import toast from "~/app/components/Toast";
import msg from "~/common/lib/msg";

import api from "~/common/lib/api";
import logo from "/static/assets/icons/albyhub.png";

export default function ConnectSpark() {
Expand All @@ -22,10 +23,16 @@ export default function ConnectSpark() {
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setLoading(true);

const mnemonic = await api.generateMnemonic();
const encryptedMnemonic = await api.encryptMnemonic(mnemonic);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this OK? I saw we have an existing setMnemonic function which will also derive the nostr key

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can't use that though because we need the account to be properly setup before validating it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yess! at this stage there is no account stored in db. so we can't use that script

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what about the nostr key? do we need to do anything there?


const account = {
name: "Spark",
config: {},
connector: getConnectorType(),
mnemonic: encryptedMnemonic,
useMnemonicForLnurlAuth: true,
};

try {
Expand Down
6 changes: 6 additions & 0 deletions src/common/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ const setMnemonic = (id: string, mnemonic: string | null): Promise<void> =>
mnemonic,
});

const encryptMnemonic = (mnemonic: string): Promise<string> =>
msg.request("encryptMnemonic", {
mnemonic,
});

const getSwapInfo = (): Promise<SwapInfoResponse> => msg.request("getSwapInfo");
const createSwap = (params: CreateSwapParams): Promise<CreateSwapResponse> =>
msg.request("createSwap", params);
Expand Down Expand Up @@ -332,6 +337,7 @@ export default {
getMnemonic,
setMnemonic,
generateMnemonic,
encryptMnemonic,
getSwapInfo,
createSwap,
liquid: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { encryptData } from "~/common/lib/crypto";
import type { MessageMnemonicEncrypt } from "~/types";

import state from "../../state";

const encryptMnemonic = async (message: MessageMnemonicEncrypt) => {
const password = await state.getState().password();
if (!password) {
return {
error: "Password is missing.",
};
}
let mnemonic = message.args.mnemonic;
mnemonic = encryptData(mnemonic, password);
return {
data: mnemonic,
};
};

export default encryptMnemonic;
3 changes: 2 additions & 1 deletion src/extension/background-script/actions/mnemonic/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import encryptMnemonic from "./encryptMnemonic";
import generateMnemonic from "./generateMnemonic";
import getMnemonic from "./getMnemonic";
import setMnemonic from "./setMnemonic";

export { generateMnemonic, getMnemonic, setMnemonic };
export { encryptMnemonic, generateMnemonic, getMnemonic, setMnemonic };
34 changes: 29 additions & 5 deletions src/extension/background-script/connectors/spark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
WalletTransfer,
} from "@buildonspark/spark-sdk/dist/types";
import { Invoice } from "@getalby/lightning-tools";
import { decryptData } from "~/common/lib/crypto";
import state from "~/extension/background-script/state";
import { Account } from "~/types";
import Connector, {
CheckPaymentArgs,
Expand All @@ -29,6 +31,8 @@ interface Config {

class SparkConnector implements Connector {
private _wallet: SparkWallet | undefined;
private account: Account;
private config: Config;

get supportedMethods() {
return [
Expand All @@ -42,19 +46,27 @@ class SparkConnector implements Connector {
];
}

constructor(_account: Account, _config: Config) {}
constructor(account: Account, config: Config) {
this.account = account;
this.config = config;
}

async init(mnemonic?: string) {
if (!mnemonic) {
async init() {
if (!this.account.mnemonic) {
throw new Error("Account has no mnemonic set");
}

const SparkWallet = await import("@buildonspark/spark-sdk").then(
(mod) => mod.SparkWallet
);
const password = (await state.getState().password()) as string;
const decryptedMnemonic = await decryptData(
this.account.mnemonic,
password
);

const { wallet } = await SparkWallet.initialize({
mnemonicOrSeed: mnemonic,
mnemonicOrSeed: decryptedMnemonic,
options: {
network: "MAINNET",
},
Expand Down Expand Up @@ -257,7 +269,19 @@ class SparkConnector implements Connector {
}

async signMessage(args: SignMessageArgs): Promise<SignMessageResponse> {
throw new Error("Method not implemented.");
if (!this._wallet) {
throw new Error("Wallet not initialized");
}
const signature = await this._wallet.signMessageWithIdentityKey(
args.message
);

return {
data: {
message: args.message,
signature: signature,
},
};
}

connectPeer(args: ConnectPeerArgs): Promise<ConnectPeerResponse> {
Expand Down
1 change: 1 addition & 0 deletions src/extension/background-script/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const routes = {
setMnemonic: mnemonic.setMnemonic,
getMnemonic: mnemonic.getMnemonic,
generateMnemonic: mnemonic.generateMnemonic,
encryptMnemonic: mnemonic.encryptMnemonic,
getSwapInfo: swaps.info,
createSwap: swaps.createSwap,
liquid: {
Expand Down
7 changes: 1 addition & 6 deletions src/extension/background-script/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,7 @@ const state = create<State>((set, get) => ({
if (!password) throw new Error("Password is not set");
const config = decryptData(account.config as string, password);
const connector = new connectors[account.connector](account, config);
let mnemonic = "";
if (!account.mnemonic) {
throw new Error("Mnemonic is not set");
}
mnemonic = decryptData(account.mnemonic, password);
await connector.init(mnemonic);
await connector.init();
return connector;
})();
set({ connector: connectorPromise });
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@ export interface MessageMnemonicGenerate extends MessageDefault {
action: "generateMnemonic";
}

export interface MessageMnemonicEncrypt extends MessageDefault {
args: {
mnemonic: Account["mnemonic"];
};
action: "encryptMnemonic";
}

export interface MessageSignEvent extends MessageDefault {
args: {
event: Event;
Expand Down