@fedimint/react 0.0.0-20250617190659

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +73 -0
  3. package/dist/contexts/FedimintWalletContext.d.ts +20 -0
  4. package/dist/contexts/FedimintWalletContext.d.ts.map +1 -0
  5. package/dist/contexts/index.d.ts +3 -0
  6. package/dist/contexts/index.d.ts.map +1 -0
  7. package/dist/hooks/index.d.ts +8 -0
  8. package/dist/hooks/index.d.ts.map +1 -0
  9. package/dist/hooks/useBalance.d.ts +2 -0
  10. package/dist/hooks/useBalance.d.ts.map +1 -0
  11. package/dist/hooks/useFedimintWallet.d.ts +2 -0
  12. package/dist/hooks/useFedimintWallet.d.ts.map +1 -0
  13. package/dist/hooks/useOpenWallet.d.ts +6 -0
  14. package/dist/hooks/useOpenWallet.d.ts.map +1 -0
  15. package/dist/hooks/useReceiveEcash.d.ts +7 -0
  16. package/dist/hooks/useReceiveEcash.d.ts.map +1 -0
  17. package/dist/hooks/useReceiveLightning.d.ts +8 -0
  18. package/dist/hooks/useReceiveLightning.d.ts.map +1 -0
  19. package/dist/hooks/useSendLightning.d.ts +8 -0
  20. package/dist/hooks/useSendLightning.d.ts.map +1 -0
  21. package/dist/hooks/useSpendEcash.d.ts +8 -0
  22. package/dist/hooks/useSpendEcash.d.ts.map +1 -0
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +202 -0
  26. package/dist/init.d.ts +2 -0
  27. package/dist/init.d.ts.map +1 -0
  28. package/lib/contexts/FedimintWalletContext.ts +68 -0
  29. package/lib/contexts/index.ts +7 -0
  30. package/lib/hooks/index.ts +7 -0
  31. package/lib/hooks/useBalance.ts +22 -0
  32. package/lib/hooks/useFedimintWallet.ts +12 -0
  33. package/lib/hooks/useOpenWallet.ts +38 -0
  34. package/lib/hooks/useReceiveEcash.ts +50 -0
  35. package/lib/hooks/useReceiveLightning.ts +46 -0
  36. package/lib/hooks/useSendLightning.ts +49 -0
  37. package/lib/hooks/useSpendEcash.ts +54 -0
  38. package/lib/index.ts +2 -0
  39. package/lib/init.ts +11 -0
  40. package/package.json +36 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 fedimint
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,73 @@
1
+ <p align="center">
2
+ <img src="../../docs/public/icon.png" alt="Fedimint Logo" width="300" />
3
+ <!-- Removes the border below the header tag -->
4
+ <div id="toc"><ul align="center" style="list-style: none;"><summary>
5
+ <h1><b>@fedimint/react</b></h1>
6
+ <p>Helpful React hooks for building with the Fedimint Web SDK.</p>
7
+ </summary></ul></div>
8
+
9
+ # @fedimint/react
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ pnpm install @fedimint/core-web @fedimint/react
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```tsx
20
+ import { FedimintWalletProvider, setupFedimintWallet } from '@fedimint/react'
21
+
22
+ setupFedimintWallet({
23
+ lazy: false,
24
+ debug: true,
25
+ })
26
+
27
+ // Wrap your app in the FedimintWalletProvider
28
+ <FedimintWalletProvider>
29
+ <App />
30
+ </FedimintWalletProvider>
31
+
32
+
33
+ // App.tsx
34
+
35
+
36
+ // Balance
37
+
38
+ import { useBalance } from '@fedimint/react'
39
+
40
+ const balance = useBalance()
41
+
42
+ // Wallet
43
+
44
+ import { useOpenWallet } from '@fedimint/react'
45
+
46
+ const {
47
+ walletStatus, // 'open' | 'closed' | 'connecting' | 'failed'
48
+ openWallet, // () => Promise<boolean> - Returns true if wallet was opened successfully.
49
+ joinFederation, // () => Promise<boolean> - Returns true if joined federation successfully.
50
+ } = useOpenWallet()
51
+
52
+ // Receive Lightning
53
+
54
+ import { useReceiveLightning } from '@fedimint/react'
55
+
56
+ const {
57
+ generateInvoice, // (amount: number) => Promise<void>
58
+ bolt11, // string
59
+ invoiceStatus, // 'pending' | 'confirmed' | 'failed'
60
+ error, // Error | undefined
61
+ } = useReceiveLightning()
62
+
63
+ // Send Lightning
64
+
65
+ import { useSendLightning } from '@fedimint/react'
66
+
67
+ const {
68
+ payInvoice, // (bolt11: string) => Promise<void>
69
+ paymentStatus, // 'pending' | 'confirmed' | 'failed'
70
+ paymentError, // Error | undefined
71
+ } = useSendLightning()
72
+
73
+ ```
@@ -0,0 +1,20 @@
1
+ import { FedimintWallet } from '@fedimint/core-web';
2
+ type FedimintWalletConfig = {
3
+ lazy?: boolean;
4
+ debug?: boolean;
5
+ };
6
+ export type WalletStatus = 'open' | 'closed' | 'opening';
7
+ export declare const setupFedimintWallet: (config: FedimintWalletConfig) => void;
8
+ export declare const FedimintWalletContext: import('react').Context<{
9
+ wallet: FedimintWallet;
10
+ walletStatus: WalletStatus;
11
+ setWalletStatus: (status: WalletStatus) => void;
12
+ } | undefined>;
13
+ export type FedimintWalletProviderProps = {};
14
+ export declare const FedimintWalletProvider: (parameters: React.PropsWithChildren<FedimintWalletProviderProps>) => import('react').FunctionComponentElement<import('react').ProviderProps<{
15
+ wallet: FedimintWallet;
16
+ walletStatus: WalletStatus;
17
+ setWalletStatus: (status: WalletStatus) => void;
18
+ } | undefined>>;
19
+ export {};
20
+ //# sourceMappingURL=FedimintWalletContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FedimintWalletContext.d.ts","sourceRoot":"","sources":["../../lib/contexts/FedimintWalletContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAWnD,KAAK,oBAAoB,GAAG;IAC1B,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;AAExD,eAAO,MAAM,mBAAmB,GAAI,QAAQ,oBAAoB,SAK/D,CAAA;AAED,eAAO,MAAM,qBAAqB;YAEpB,cAAc;kBACR,YAAY;qBACT,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI;cAGzC,CAAA;AAEZ,MAAM,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAE5C,eAAO,MAAM,sBAAsB,GACjC,YAAY,KAAK,CAAC,iBAAiB,CAAC,2BAA2B,CAAC;YAVpD,cAAc;kBACR,YAAY;qBACT,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI;eAsCpD,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { FedimintWalletContext, FedimintWalletProvider, setupFedimintWallet } from './FedimintWalletContext';
2
+ export { FedimintWalletContext, FedimintWalletProvider, setupFedimintWallet };
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/contexts/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACpB,MAAM,yBAAyB,CAAA;AAEhC,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,CAAA"}
@@ -0,0 +1,8 @@
1
+ export { useBalance } from './useBalance';
2
+ export { useOpenWallet } from './useOpenWallet';
3
+ export { useFedimintWallet } from './useFedimintWallet';
4
+ export { useReceiveLightning } from './useReceiveLightning';
5
+ export { useSendLightning } from './useSendLightning';
6
+ export { useSpendEcash } from './useSpendEcash';
7
+ export { useReceiveEcash } from './useReceiveEcash';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const useBalance: () => number | undefined;
2
+ //# sourceMappingURL=useBalance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useBalance.d.ts","sourceRoot":"","sources":["../../lib/hooks/useBalance.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,UAAU,0BAkBtB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const useFedimintWallet: () => import('@fedimint/core-web').FedimintWallet;
2
+ //# sourceMappingURL=useFedimintWallet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFedimintWallet.d.ts","sourceRoot":"","sources":["../../lib/hooks/useFedimintWallet.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,iBAAiB,mDAQ7B,CAAA"}
@@ -0,0 +1,6 @@
1
+ export declare const useOpenWallet: () => {
2
+ walletStatus: import('../contexts/FedimintWalletContext').WalletStatus;
3
+ openWallet: () => void;
4
+ joinFederation: (invite: string) => Promise<void>;
5
+ };
6
+ //# sourceMappingURL=useOpenWallet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useOpenWallet.d.ts","sourceRoot":"","sources":["../../lib/hooks/useOpenWallet.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,aAAa;;;6BAqBP,MAAM;CAaxB,CAAA"}
@@ -0,0 +1,7 @@
1
+ import { ReissueExternalNotesState } from '@fedimint/core-web';
2
+ export declare const useReceiveEcash: () => {
3
+ redeemEcash: (notes: string) => Promise<void>;
4
+ state: "Error" | ReissueExternalNotesState | undefined;
5
+ error: string | undefined;
6
+ };
7
+ //# sourceMappingURL=useReceiveEcash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useReceiveEcash.d.ts","sourceRoot":"","sources":["../../lib/hooks/useReceiveEcash.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAA;AAE9D,eAAO,MAAM,eAAe;yBA2BV,MAAM;;;CAkBvB,CAAA"}
@@ -0,0 +1,8 @@
1
+ import { LnReceiveState } from '@fedimint/core-web';
2
+ export declare const useReceiveLightning: () => {
3
+ generateInvoice: (amount: number, description: string) => Promise<string>;
4
+ bolt11: string | undefined;
5
+ invoiceStatus: LnReceiveState | undefined;
6
+ error: string | undefined;
7
+ };
8
+ //# sourceMappingURL=useReceiveLightning.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useReceiveLightning.d.ts","sourceRoot":"","sources":["../../lib/hooks/useReceiveLightning.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAA6B,MAAM,oBAAoB,CAAA;AAE9E,eAAO,MAAM,mBAAmB;8BASb,MAAM,eAAe,MAAM;;;;CAgC7C,CAAA"}
@@ -0,0 +1,8 @@
1
+ import { LnPayState, OutgoingLightningPayment } from '@fedimint/core-web';
2
+ export declare const useSendLightning: () => {
3
+ payInvoice: (bolt11: string) => Promise<OutgoingLightningPayment>;
4
+ payment: OutgoingLightningPayment | undefined;
5
+ paymentStatus: LnPayState | undefined;
6
+ paymentError: string | undefined;
7
+ };
8
+ //# sourceMappingURL=useSendLightning.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSendLightning.d.ts","sourceRoot":"","sources":["../../lib/hooks/useSendLightning.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,UAAU,EACf,KAAK,wBAAwB,EAC9B,MAAM,oBAAoB,CAAA;AAE3B,eAAO,MAAM,gBAAgB;yBAQV,MAAM;;;;CAiCxB,CAAA"}
@@ -0,0 +1,8 @@
1
+ import { SpendNotesState } from '@fedimint/core-web';
2
+ export declare const useSpendEcash: () => {
3
+ spendEcash: (amountSats: number, reclaimAfter?: number) => Promise<string>;
4
+ notes: string | undefined;
5
+ state: SpendNotesState | "Error" | undefined;
6
+ error: string | undefined;
7
+ };
8
+ //# sourceMappingURL=useSpendEcash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSpendEcash.d.ts","sourceRoot":"","sources":["../../lib/hooks/useSpendEcash.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEzD,eAAO,MAAM,aAAa;6BA8BH,MAAM,iBAAiB,MAAM;;;;CAmBnD,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './hooks';
2
+ export * from './contexts';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,202 @@
1
+ import { useState as r, useEffect as h, createContext as y, useMemo as W, createElement as F, useContext as f, useCallback as m } from "react";
2
+ import { FedimintWallet as I } from "@fedimint/core-web";
3
+ const L = () => {
4
+ const e = g(), { walletStatus: n } = E(), [t, o] = r();
5
+ return h(() => {
6
+ if (n !== "open") return;
7
+ const a = e.balance.subscribeBalance((c) => {
8
+ o(c);
9
+ });
10
+ return () => {
11
+ a();
12
+ };
13
+ }, [n]), t;
14
+ };
15
+ let w;
16
+ const R = (e) => {
17
+ w = new I(!!e.lazy), e.debug && w.setLogLevel("debug");
18
+ }, v = y(void 0), O = (e) => {
19
+ const [n, t] = r("closed"), { children: o } = e;
20
+ if (!w)
21
+ throw new Error(
22
+ "You must call setupFedimintWallet() first. See the getting started guide."
23
+ );
24
+ const a = W(
25
+ () => ({
26
+ wallet: w,
27
+ walletStatus: n,
28
+ setWalletStatus: t
29
+ }),
30
+ [n]
31
+ );
32
+ return h(() => {
33
+ w.waitForOpen().then(() => {
34
+ t("open");
35
+ });
36
+ }, [w]), F(
37
+ v.Provider,
38
+ { value: a },
39
+ o
40
+ );
41
+ }, E = () => {
42
+ const e = f(v);
43
+ if (!e)
44
+ throw new Error(
45
+ "useOpenWallet must be used within a FedimintWalletProvider"
46
+ );
47
+ const { wallet: n, walletStatus: t, setWalletStatus: o } = e, a = m(() => {
48
+ t !== "open" && (o("opening"), n.open().then((l) => {
49
+ o(l ? "open" : "closed");
50
+ }));
51
+ }, [n]), c = m(
52
+ async (l) => {
53
+ t !== "open" && (o("opening"), await n.joinFederation(l).then((u) => {
54
+ o(u ? "open" : "closed");
55
+ }));
56
+ },
57
+ [n]
58
+ );
59
+ return { walletStatus: t, openWallet: a, joinFederation: c };
60
+ }, g = () => {
61
+ const e = f(v);
62
+ if (!(e != null && e.wallet))
63
+ throw new Error(
64
+ "useFedimintWallet must be used within a FedimintWalletProvider"
65
+ );
66
+ return e.wallet;
67
+ }, C = () => {
68
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), p = m(
69
+ async (i, s) => {
70
+ if (n !== "open") throw new Error("Wallet is not open");
71
+ const d = await e.lightning.createInvoice(i, s);
72
+ return o(d), d.invoice;
73
+ },
74
+ [e, n]
75
+ );
76
+ return h(() => {
77
+ if (n !== "open" || !t) return;
78
+ const i = e.lightning.subscribeLnReceive(
79
+ t.operation_id,
80
+ (s) => {
81
+ c(s);
82
+ },
83
+ (s) => {
84
+ u(s);
85
+ }
86
+ );
87
+ return () => {
88
+ i();
89
+ };
90
+ }, [n, t]), {
91
+ generateInvoice: p,
92
+ bolt11: t == null ? void 0 : t.invoice,
93
+ invoiceStatus: a,
94
+ error: l
95
+ };
96
+ }, N = () => {
97
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), p = m(
98
+ async (i) => {
99
+ if (n !== "open") throw new Error("Wallet is not open");
100
+ const s = await e.lightning.payInvoice(i);
101
+ return o(s), s;
102
+ },
103
+ [e, n]
104
+ );
105
+ return h(() => {
106
+ if (n !== "open" || !t) return;
107
+ const i = e.lightning.subscribeLnPay(
108
+ // @ts-ignore
109
+ t.payment_type.lightning,
110
+ (s) => {
111
+ c(s);
112
+ },
113
+ (s) => {
114
+ u(s);
115
+ }
116
+ );
117
+ return () => {
118
+ i();
119
+ };
120
+ }, [n, t]), {
121
+ payInvoice: p,
122
+ payment: t,
123
+ paymentStatus: a,
124
+ paymentError: l
125
+ };
126
+ }, B = () => {
127
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), [p, i] = r();
128
+ return h(() => {
129
+ if (!t) return;
130
+ const d = e.mint.subscribeSpendNotes(
131
+ t,
132
+ (b) => {
133
+ u(b);
134
+ },
135
+ (b) => {
136
+ u("Error"), i(b);
137
+ }
138
+ );
139
+ return () => {
140
+ d();
141
+ };
142
+ }, [t]), {
143
+ spendEcash: m(
144
+ async (d, b) => {
145
+ if (n !== "open") throw new Error("Wallet is not open");
146
+ const S = await e.mint.spendNotes(
147
+ d * 1e3,
148
+ b
149
+ );
150
+ return o(S.operation_id), c(S.notes), S.notes;
151
+ },
152
+ [e, n]
153
+ ),
154
+ notes: a,
155
+ state: l,
156
+ error: p
157
+ };
158
+ }, _ = () => {
159
+ const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r();
160
+ return h(() => {
161
+ if (!t) return;
162
+ const i = e.mint.subscribeReissueExternalNotes(
163
+ t,
164
+ (s) => {
165
+ c(s);
166
+ },
167
+ (s) => {
168
+ u(s);
169
+ }
170
+ );
171
+ return () => {
172
+ i();
173
+ };
174
+ }, [t]), {
175
+ redeemEcash: m(
176
+ async (i) => {
177
+ if (n !== "open") throw new Error("Wallet is not open");
178
+ try {
179
+ const s = await e.mint.redeemEcash(i);
180
+ o(s);
181
+ } catch (s) {
182
+ c("Error"), u(s);
183
+ }
184
+ },
185
+ [e, n]
186
+ ),
187
+ state: a,
188
+ error: l
189
+ };
190
+ };
191
+ export {
192
+ v as FedimintWalletContext,
193
+ O as FedimintWalletProvider,
194
+ R as setupFedimintWallet,
195
+ L as useBalance,
196
+ g as useFedimintWallet,
197
+ E as useOpenWallet,
198
+ _ as useReceiveEcash,
199
+ C as useReceiveLightning,
200
+ N as useSendLightning,
201
+ B as useSpendEcash
202
+ };
package/dist/init.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const initFedimintReact: (lazy?: boolean) => {};
2
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../lib/init.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,iBAAiB,GAAI,OAAM,OAAe,OAMtD,CAAA"}
@@ -0,0 +1,68 @@
1
+ import { FedimintWallet } from '@fedimint/core-web'
2
+ import {
3
+ createContext,
4
+ createElement,
5
+ useEffect,
6
+ useMemo,
7
+ useState,
8
+ } from 'react'
9
+
10
+ let wallet: FedimintWallet
11
+
12
+ type FedimintWalletConfig = {
13
+ lazy?: boolean
14
+ debug?: boolean
15
+ }
16
+
17
+ export type WalletStatus = 'open' | 'closed' | 'opening'
18
+
19
+ export const setupFedimintWallet = (config: FedimintWalletConfig) => {
20
+ wallet = new FedimintWallet(!!config.lazy)
21
+ if (config.debug) {
22
+ wallet.setLogLevel('debug')
23
+ }
24
+ }
25
+
26
+ export const FedimintWalletContext = createContext<
27
+ | {
28
+ wallet: FedimintWallet
29
+ walletStatus: WalletStatus
30
+ setWalletStatus: (status: WalletStatus) => void
31
+ }
32
+ | undefined
33
+ >(undefined)
34
+
35
+ export type FedimintWalletProviderProps = {}
36
+
37
+ export const FedimintWalletProvider = (
38
+ parameters: React.PropsWithChildren<FedimintWalletProviderProps>,
39
+ ) => {
40
+ const [walletStatus, setWalletStatus] = useState<WalletStatus>('closed')
41
+ const { children } = parameters
42
+
43
+ if (!wallet)
44
+ throw new Error(
45
+ 'You must call setupFedimintWallet() first. See the getting started guide.',
46
+ )
47
+
48
+ const contextValue = useMemo(
49
+ () => ({
50
+ wallet,
51
+ walletStatus,
52
+ setWalletStatus,
53
+ }),
54
+ [walletStatus],
55
+ )
56
+
57
+ useEffect(() => {
58
+ wallet.waitForOpen().then(() => {
59
+ setWalletStatus('open')
60
+ })
61
+ }, [wallet])
62
+
63
+ return createElement(
64
+ FedimintWalletContext.Provider,
65
+ { value: contextValue },
66
+ children,
67
+ )
68
+ }
@@ -0,0 +1,7 @@
1
+ import {
2
+ FedimintWalletContext,
3
+ FedimintWalletProvider,
4
+ setupFedimintWallet,
5
+ } from './FedimintWalletContext'
6
+
7
+ export { FedimintWalletContext, FedimintWalletProvider, setupFedimintWallet }
@@ -0,0 +1,7 @@
1
+ export { useBalance } from './useBalance'
2
+ export { useOpenWallet } from './useOpenWallet'
3
+ export { useFedimintWallet } from './useFedimintWallet'
4
+ export { useReceiveLightning } from './useReceiveLightning'
5
+ export { useSendLightning } from './useSendLightning'
6
+ export { useSpendEcash } from './useSpendEcash'
7
+ export { useReceiveEcash } from './useReceiveEcash'
@@ -0,0 +1,22 @@
1
+ import { useEffect, useState } from 'react'
2
+ import { useFedimintWallet, useOpenWallet } from '.'
3
+
4
+ export const useBalance = () => {
5
+ const wallet = useFedimintWallet()
6
+ const { walletStatus } = useOpenWallet()
7
+ const [balance, setBalance] = useState<number>()
8
+
9
+ useEffect(() => {
10
+ if (walletStatus !== 'open') return
11
+
12
+ const unsubscribe = wallet.balance.subscribeBalance((balance) => {
13
+ setBalance(balance)
14
+ })
15
+
16
+ return () => {
17
+ unsubscribe()
18
+ }
19
+ }, [walletStatus])
20
+
21
+ return balance
22
+ }
@@ -0,0 +1,12 @@
1
+ import { useContext } from 'react'
2
+ import { FedimintWalletContext } from '../contexts'
3
+
4
+ export const useFedimintWallet = () => {
5
+ const value = useContext(FedimintWalletContext)
6
+ if (!value?.wallet) {
7
+ throw new Error(
8
+ 'useFedimintWallet must be used within a FedimintWalletProvider',
9
+ )
10
+ }
11
+ return value.wallet
12
+ }
@@ -0,0 +1,38 @@
1
+ import { useCallback, useContext } from 'react'
2
+ import { FedimintWalletContext } from '../contexts/FedimintWalletContext'
3
+
4
+ export const useOpenWallet = () => {
5
+ const value = useContext(FedimintWalletContext)
6
+
7
+ if (!value) {
8
+ throw new Error(
9
+ 'useOpenWallet must be used within a FedimintWalletProvider',
10
+ )
11
+ }
12
+
13
+ const { wallet, walletStatus, setWalletStatus } = value
14
+
15
+ const openWallet = useCallback(() => {
16
+ if (walletStatus === 'open') return
17
+
18
+ setWalletStatus('opening')
19
+ wallet.open().then((res) => {
20
+ setWalletStatus(res ? 'open' : 'closed')
21
+ })
22
+ }, [wallet])
23
+
24
+ const joinFederation = useCallback(
25
+ async (invite: string) => {
26
+ if (walletStatus === 'open') return
27
+
28
+ setWalletStatus('opening')
29
+
30
+ await wallet.joinFederation(invite).then((res) => {
31
+ setWalletStatus(res ? 'open' : 'closed')
32
+ })
33
+ },
34
+ [wallet],
35
+ )
36
+
37
+ return { walletStatus, openWallet, joinFederation }
38
+ }
@@ -0,0 +1,50 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { useFedimintWallet, useOpenWallet } from '.'
3
+ import { ReissueExternalNotesState } from '@fedimint/core-web'
4
+
5
+ export const useReceiveEcash = () => {
6
+ const wallet = useFedimintWallet()
7
+ const { walletStatus } = useOpenWallet()
8
+
9
+ const [operationId, setOperationId] = useState<string>()
10
+ const [state, setState] = useState<ReissueExternalNotesState | 'Error'>()
11
+ const [error, setError] = useState<string>()
12
+
13
+ useEffect(() => {
14
+ if (!operationId) return
15
+
16
+ const unsubscribe = wallet.mint.subscribeReissueExternalNotes(
17
+ operationId,
18
+ (_state) => {
19
+ setState(_state)
20
+ },
21
+ (error) => {
22
+ setError(error)
23
+ },
24
+ )
25
+
26
+ return () => {
27
+ unsubscribe()
28
+ }
29
+ }, [operationId])
30
+
31
+ const redeemEcash = useCallback(
32
+ async (notes: string) => {
33
+ if (walletStatus !== 'open') throw new Error('Wallet is not open')
34
+ try {
35
+ const response = await wallet.mint.redeemEcash(notes)
36
+ setOperationId(response)
37
+ } catch (e) {
38
+ setState('Error')
39
+ setError(e as string)
40
+ }
41
+ },
42
+ [wallet, walletStatus],
43
+ )
44
+
45
+ return {
46
+ redeemEcash,
47
+ state,
48
+ error,
49
+ }
50
+ }
@@ -0,0 +1,46 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { useFedimintWallet, useOpenWallet } from '.'
3
+ import { LnReceiveState, type CreateBolt11Response } from '@fedimint/core-web'
4
+
5
+ export const useReceiveLightning = () => {
6
+ const wallet = useFedimintWallet()
7
+ const { walletStatus } = useOpenWallet()
8
+ const [invoice, setInvoice] = useState<CreateBolt11Response>()
9
+ const [invoiceReceiveState, setInvoiceReceiveState] =
10
+ useState<LnReceiveState>()
11
+ const [error, setError] = useState<string>()
12
+
13
+ const generateInvoice = useCallback(
14
+ async (amount: number, description: string) => {
15
+ if (walletStatus !== 'open') throw new Error('Wallet is not open')
16
+ const response = await wallet.lightning.createInvoice(amount, description)
17
+ setInvoice(response)
18
+ return response.invoice
19
+ },
20
+ [wallet, walletStatus],
21
+ )
22
+
23
+ useEffect(() => {
24
+ if (walletStatus !== 'open' || !invoice) return
25
+ const unsubscribe = wallet.lightning.subscribeLnReceive(
26
+ invoice.operation_id,
27
+ (state) => {
28
+ setInvoiceReceiveState(state)
29
+ },
30
+ (error) => {
31
+ setError(error)
32
+ },
33
+ )
34
+
35
+ return () => {
36
+ unsubscribe()
37
+ }
38
+ }, [walletStatus, invoice])
39
+
40
+ return {
41
+ generateInvoice,
42
+ bolt11: invoice?.invoice,
43
+ invoiceStatus: invoiceReceiveState,
44
+ error,
45
+ }
46
+ }
@@ -0,0 +1,49 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { useFedimintWallet, useOpenWallet } from '.'
3
+ import {
4
+ type LnPayState,
5
+ type OutgoingLightningPayment,
6
+ } from '@fedimint/core-web'
7
+
8
+ export const useSendLightning = () => {
9
+ const wallet = useFedimintWallet()
10
+ const { walletStatus } = useOpenWallet()
11
+ const [payment, setPayment] = useState<OutgoingLightningPayment>()
12
+ const [paymentState, setPaymentState] = useState<LnPayState>()
13
+ const [error, setError] = useState<string>()
14
+
15
+ const payInvoice = useCallback(
16
+ async (bolt11: string) => {
17
+ if (walletStatus !== 'open') throw new Error('Wallet is not open')
18
+ const response = await wallet.lightning.payInvoice(bolt11)
19
+ setPayment(response)
20
+ return response
21
+ },
22
+ [wallet, walletStatus],
23
+ )
24
+
25
+ useEffect(() => {
26
+ if (walletStatus !== 'open' || !payment) return
27
+ const unsubscribe = wallet.lightning.subscribeLnPay(
28
+ // @ts-ignore
29
+ payment.payment_type.lightning,
30
+ (state) => {
31
+ setPaymentState(state)
32
+ },
33
+ (error) => {
34
+ setError(error)
35
+ },
36
+ )
37
+
38
+ return () => {
39
+ unsubscribe()
40
+ }
41
+ }, [walletStatus, payment])
42
+
43
+ return {
44
+ payInvoice,
45
+ payment,
46
+ paymentStatus: paymentState,
47
+ paymentError: error,
48
+ }
49
+ }
@@ -0,0 +1,54 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { useFedimintWallet, useOpenWallet } from '.'
3
+ import { type SpendNotesState } from '@fedimint/core-web'
4
+
5
+ export const useSpendEcash = () => {
6
+ const wallet = useFedimintWallet()
7
+ const { walletStatus } = useOpenWallet()
8
+
9
+ const [operationId, setOperationId] = useState<string>()
10
+ const [notes, setNotes] = useState<string>()
11
+
12
+ const [state, setState] = useState<SpendNotesState | 'Error'>()
13
+ const [error, setError] = useState<string>()
14
+
15
+ useEffect(() => {
16
+ if (!operationId) return
17
+
18
+ const unsubscribe = wallet.mint.subscribeSpendNotes(
19
+ operationId,
20
+ (_state) => {
21
+ setState(_state)
22
+ },
23
+ (error) => {
24
+ setState('Error')
25
+ setError(error)
26
+ },
27
+ )
28
+
29
+ return () => {
30
+ unsubscribe()
31
+ }
32
+ }, [operationId])
33
+
34
+ const spendEcash = useCallback(
35
+ async (amountSats: number, reclaimAfter?: number) => {
36
+ if (walletStatus !== 'open') throw new Error('Wallet is not open')
37
+ const response = await wallet.mint.spendNotes(
38
+ amountSats * 1000,
39
+ reclaimAfter,
40
+ )
41
+ setOperationId(response.operation_id)
42
+ setNotes(response.notes)
43
+ return response.notes
44
+ },
45
+ [wallet, walletStatus],
46
+ )
47
+
48
+ return {
49
+ spendEcash,
50
+ notes,
51
+ state,
52
+ error,
53
+ }
54
+ }
package/lib/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './hooks'
2
+ export * from './contexts'
package/lib/init.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { FedimintWallet } from '@fedimint/core-web'
2
+
3
+ let wallet: FedimintWallet | undefined
4
+
5
+ export const initFedimintReact = (lazy: boolean = false) => {
6
+ if (!lazy) {
7
+ wallet = new FedimintWallet(lazy)
8
+ }
9
+
10
+ return {}
11
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@fedimint/react",
3
+ "version": "0.0.0-20250617190659",
4
+ "type": "module",
5
+ "files": [
6
+ "dist",
7
+ "lib"
8
+ ],
9
+ "main": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "peerDependencies": {
12
+ "react": "^18.3.1",
13
+ "@fedimint/core-web": "0.0.0-20250617190659"
14
+ },
15
+ "devDependencies": {
16
+ "@types/react": ">=19.1.4",
17
+ "@types/react-dom": "^18.3.7",
18
+ "@typescript-eslint/eslint-plugin": "^7.18.0",
19
+ "@typescript-eslint/parser": "^7.18.0",
20
+ "@vitejs/plugin-react": "^4.4.1",
21
+ "eslint": "^9.27.0",
22
+ "eslint-plugin-react-hooks": "^4.6.2",
23
+ "eslint-plugin-react-refresh": "^0.4.20",
24
+ "react-dom": ">=19.1.0",
25
+ "typescript": "^5.8.3",
26
+ "vite": "^5.4.19",
27
+ "vite-plugin-dts": "^4.5.4",
28
+ "vite-plugin-wasm": "^3.4.1"
29
+ },
30
+ "scripts": {
31
+ "dev": "vite",
32
+ "build": "tsc --p ./tsconfig.build.json && vite build",
33
+ "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
34
+ "preview": "vite preview"
35
+ }
36
+ }