Skip to content

Commit 366cbe5

Browse files
committed
save wallet configuration when possible
1 parent a244176 commit 366cbe5

File tree

8 files changed

+94
-7
lines changed

8 files changed

+94
-7
lines changed

ndk-mobile/src/hooks/session.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk';
1+
import NDK, { NDKEvent, NDKKind, NDKUser } from '@nostr-dev-kit/ndk';
22
import { useNDK } from './ndk';
33
import { NDKEventWithFrom } from './subscribe';
44
import { useNDKSessionStore } from '../stores/session';
5+
import { useNDKWallet } from './wallet';
6+
import { walletFromLoadingString } from '@nostr-dev-kit/ndk-wallet';
7+
import { SessionInitOpts, SessionInitCallbacks } from '../stores/session/types';
8+
import { SettingsStore } from '../types';
59

610
const useNDKSession = () => {
711
const init = useNDKSessionStore(s => s.init);
812
const mutePubkey = useNDKSessionStore(s => s.mutePubkey);
913

10-
return { init, mutePubkey };
14+
const { setActiveWallet } = useNDKWallet();
15+
16+
const wrappedInit = (ndk: NDK, user: NDKUser, settingsStore: SettingsStore, opts: SessionInitOpts, on: SessionInitCallbacks) => {
17+
init(ndk, user, settingsStore, opts, on);
18+
19+
const walletString = settingsStore?.getSync('wallet');
20+
if (walletString) {
21+
walletFromLoadingString(ndk, walletString).then((wallet) => {
22+
if (wallet) setActiveWallet(wallet);
23+
}).catch((e) => {
24+
console.error('error setting active wallet', e);
25+
});
26+
}
27+
}
28+
29+
return { init: wrappedInit, mutePubkey };
1130
}
1231

1332
const useFollows = () => useNDKSessionStore(s => s.follows);

ndk-mobile/src/hooks/wallet.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1+
import { NDKWallet } from '@nostr-dev-kit/ndk-wallet';
12
import { useWalletStore } from '../stores/wallet';
3+
import { useNDK } from './ndk';
4+
import { useNDKStore } from '../stores/ndk';
25

36
const useNDKWallet = () => {
7+
const { ndk } = useNDK();
8+
const settingsStore = useNDKStore(s => s.settingsStore);
49
const activeWallet = useWalletStore(s => s.activeWallet);
5-
const setActiveWallet = useWalletStore(s => s.setActiveWallet);
10+
const storeSetActiveWallet = useWalletStore(s => s.setActiveWallet);
611
const balances = useWalletStore(s => s.balances);
712
const setBalances = useWalletStore(s => s.setBalances);
813
const nutzapMonitor = useWalletStore(s => s.nutzapMonitor);
914
const setNutzapMonitor = useWalletStore(s => s.setNutzapMonitor);
1015

16+
const setActiveWallet = (wallet: NDKWallet) => {
17+
storeSetActiveWallet(wallet);
18+
ndk.wallet = wallet;
19+
20+
let loadingString: string | undefined;
21+
22+
if (wallet) loadingString = wallet.toLoadingString?.();
23+
if (loadingString)
24+
settingsStore.set('wallet', loadingString);
25+
else
26+
settingsStore.delete('wallet');
27+
}
28+
1129
return { activeWallet, setActiveWallet, balances, setBalances, nutzapMonitor, setNutzapMonitor };
1230
}
1331

ndk-mobile/src/stores/session/actions/init.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export const initSession = (
4646
sub.on('event', handleEvent);
4747

4848
sub.once('eose', () => {
49-
console.log('EOSE on main sub', sub.internalId);
5049
on?.onReady?.();
5150
eosed = true;
5251

@@ -75,7 +74,6 @@ export const initSession = (
7574
eosed = true;
7675
});
7776
sub.start();
78-
console.log('sub started', sub.filters, ndk.pool.connectedRelays());
7977

8078
set({ ndk, follows: opts.follows ? [user.pubkey] : [] });
8179
};

ndk-wallet/src/wallets/cashu/wallet/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents & {
155155
public checkProofs = consolidateTokens.bind(this);
156156
public consolidateTokens = consolidateTokens.bind(this);
157157

158+
public toLoadingString() {
159+
return JSON.stringify({
160+
type: 'nip60',
161+
bech32: this.event!.encode(),
162+
});
163+
}
164+
158165
async mintNuts(amounts: number[], unit: string) {
159166
let result: SendResponse | undefined;
160167
const totalAmount = amounts.reduce((acc, amount) => acc + amount, 0);

ndk-wallet/src/wallets/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import NDK, {
22
CashuPaymentInfo,
33
LnPaymentInfo,
44
NDKPaymentConfirmation,
@@ -9,6 +9,8 @@ import {
99
NDKZapSplit,
1010
} from "@nostr-dev-kit/ndk";
1111
import { EventEmitter } from "tseep";
12+
import { NDKNWCWallet } from "./nwc";
13+
import { NDKCashuWallet } from "./cashu/wallet";
1214

1315
export type NDKWalletTypes = 'nwc' | 'nip-60' | 'webln';
1416

@@ -86,4 +88,24 @@ export interface NDKWallet
8688
* Get the balance of this wallet
8789
*/
8890
balance(): NDKWalletBalance[] | undefined;
91+
92+
/**
93+
* Serializes the wallet configuration in a way that can be restored later.
94+
*/
95+
toLoadingString?(): string;
96+
}
97+
98+
export async function walletFromLoadingString(ndk: NDK, str: string): Promise<NDKNWCWallet | NDKCashuWallet | undefined> {
99+
const payload = JSON.parse(str);
100+
101+
switch (payload.type) {
102+
case 'nwc':
103+
const w = new NDKNWCWallet(ndk);
104+
await w.initWithPairingCode(payload.pairingCode);
105+
return w;
106+
case 'nip60':
107+
const event = await ndk.fetchEvent(payload.bech32);
108+
if (!event) return undefined;
109+
return await NDKCashuWallet.from(event);
110+
}
89111
}

ndk-wallet/src/wallets/nwc/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ export class NDKNWCWallet extends EventEmitter<NDKWalletEvents> implements NDKWa
6868
return this.init(pubkey, relayUrls, secret);
6969
}
7070

71+
toLoadingString(): string {
72+
return JSON.stringify({
73+
type: 'nwc',
74+
pairingCode: this.pairingCode
75+
});
76+
}
77+
7178
async lnPay(payment: LnPaymentInfo): Promise<NDKPaymentConfirmationLN | undefined> {
7279
if (!this.signer) throw new Error("Wallet not initialized");
7380

ndk/src/events/nip19.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { nip19 } from "nostr-tools";
22

33
import type { NDKEvent } from "./index.js";
44

5-
export function encode(this: NDKEvent, maxRelayCount: number = 5): string {
5+
const DEFAULT_RELAY_COUNT = 2 as const;
6+
7+
export function encode(this: NDKEvent, maxRelayCount: number = DEFAULT_RELAY_COUNT): string {
68
let relays: string[] = [];
79

810
if (this.onRelays.length > 0) {

ndk/src/zapper/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ export type OnCompleteCb = (
9595
) => void;
9696

9797
interface NDKZapperOptions {
98+
/**
99+
* Comment to include in the zap event
100+
*/
98101
comment?: string;
102+
103+
/**
104+
* Extra tags to add to the zap event
105+
*/
99106
tags?: NDKTag[];
100107
signer?: NDKSigner;
101108
lnPay?: LnPayCb;
@@ -139,6 +146,13 @@ class NDKZapper extends EventEmitter<{
139146

140147
public maxRelays = 3;
141148

149+
/**
150+
*
151+
* @param target The target of the zap
152+
* @param amount The amount to send indicated in the unit
153+
* @param unit The unit of the amount
154+
* @param opts Options for the zap
155+
*/
142156
constructor(
143157
target: NDKEvent | NDKUser,
144158
amount: number,

0 commit comments

Comments
 (0)