@fedimint/react 0.0.3 → 0.1.0
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.
- package/dist/contexts/FedimintWalletContext.d.ts +5 -0
- package/dist/contexts/FedimintWalletContext.d.ts.map +1 -1
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/useOpenWallet.d.ts +1 -3
- package/dist/hooks/useOpenWallet.d.ts.map +1 -1
- package/dist/hooks/useReceiveEcash.d.ts +7 -0
- package/dist/hooks/useReceiveEcash.d.ts.map +1 -0
- package/dist/hooks/useSpendEcash.d.ts +8 -0
- package/dist/hooks/useSpendEcash.d.ts.map +1 -0
- package/dist/index.js +167 -84
- package/dist/init.d.ts.map +1 -1
- package/lib/contexts/FedimintWalletContext.ts +41 -7
- package/lib/hooks/index.ts +2 -0
- package/lib/hooks/useOpenWallet.ts +14 -18
- package/lib/hooks/useReceiveEcash.ts +50 -0
- package/lib/hooks/useSpendEcash.ts +54 -0
- package/lib/init.ts +6 -2
- package/package.json +12 -12
|
@@ -3,13 +3,18 @@ type FedimintWalletConfig = {
|
|
|
3
3
|
lazy?: boolean;
|
|
4
4
|
debug?: boolean;
|
|
5
5
|
};
|
|
6
|
+
export type WalletStatus = 'open' | 'closed' | 'opening';
|
|
6
7
|
export declare const setupFedimintWallet: (config: FedimintWalletConfig) => void;
|
|
7
8
|
export declare const FedimintWalletContext: import('react').Context<{
|
|
8
9
|
wallet: FedimintWallet;
|
|
10
|
+
walletStatus: WalletStatus;
|
|
11
|
+
setWalletStatus: (status: WalletStatus) => void;
|
|
9
12
|
} | undefined>;
|
|
10
13
|
export type FedimintWalletProviderProps = {};
|
|
11
14
|
export declare const FedimintWalletProvider: (parameters: React.PropsWithChildren<FedimintWalletProviderProps>) => import('react').FunctionComponentElement<import('react').ProviderProps<{
|
|
12
15
|
wallet: FedimintWallet;
|
|
16
|
+
walletStatus: WalletStatus;
|
|
17
|
+
setWalletStatus: (status: WalletStatus) => void;
|
|
13
18
|
} | undefined>>;
|
|
14
19
|
export {};
|
|
15
20
|
//# sourceMappingURL=FedimintWalletContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FedimintWalletContext.d.ts","sourceRoot":"","sources":["../../lib/contexts/FedimintWalletContext.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"FedimintWalletContext.d.ts","sourceRoot":"","sources":["../../lib/contexts/FedimintWalletContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAYnE,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,SAO/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"}
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -3,4 +3,6 @@ export { useOpenWallet } from './useOpenWallet';
|
|
|
3
3
|
export { useFedimintWallet } from './useFedimintWallet';
|
|
4
4
|
export { useReceiveLightning } from './useReceiveLightning';
|
|
5
5
|
export { useSendLightning } from './useSendLightning';
|
|
6
|
+
export { useSpendEcash } from './useSpendEcash';
|
|
7
|
+
export { useReceiveEcash } from './useReceiveEcash';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +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"}
|
|
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"}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
type WalletStatus = 'open' | 'closed' | 'opening';
|
|
2
1
|
export declare const useOpenWallet: () => {
|
|
3
|
-
walletStatus: WalletStatus
|
|
2
|
+
walletStatus: import('../contexts/FedimintWalletContext').WalletStatus;
|
|
4
3
|
openWallet: () => void;
|
|
5
4
|
joinFederation: (invite: string) => Promise<void>;
|
|
6
5
|
};
|
|
7
|
-
export {};
|
|
8
6
|
//# sourceMappingURL=useOpenWallet.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOpenWallet.d.ts","sourceRoot":"","sources":["../../lib/hooks/useOpenWallet.ts"],"names":[],"mappings":"AAGA,
|
|
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 { 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"}
|
package/dist/index.js
CHANGED
|
@@ -1,121 +1,204 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
import {
|
|
3
|
-
const
|
|
4
|
-
const e =
|
|
5
|
-
return
|
|
6
|
-
if (
|
|
7
|
-
const a = e.balance.subscribeBalance((
|
|
8
|
-
|
|
1
|
+
import { useState as r, useEffect as m, createContext as y, useMemo as F, createElement as I, useContext as W, useCallback as w } from "react";
|
|
2
|
+
import { WalletDirector as P } from "@fedimint/core-web";
|
|
3
|
+
const R = () => {
|
|
4
|
+
const e = g(), { walletStatus: t } = E(), [n, o] = r();
|
|
5
|
+
return m(() => {
|
|
6
|
+
if (t !== "open") return;
|
|
7
|
+
const a = e.balance.subscribeBalance((c) => {
|
|
8
|
+
o(c);
|
|
9
9
|
});
|
|
10
10
|
return () => {
|
|
11
11
|
a();
|
|
12
12
|
};
|
|
13
|
-
}, [
|
|
14
|
-
}, b = () => {
|
|
15
|
-
const e = w(), [n, t] = s(), i = u(() => {
|
|
16
|
-
n !== "open" && (t("opening"), e.open().then((r) => {
|
|
17
|
-
t(r ? "open" : "closed");
|
|
18
|
-
}));
|
|
19
|
-
}, [e]), a = u(
|
|
20
|
-
async (r) => {
|
|
21
|
-
if (n === "open") return;
|
|
22
|
-
t("opening");
|
|
23
|
-
const l = await e.joinFederation(r);
|
|
24
|
-
t(l ? "open" : "closed");
|
|
25
|
-
},
|
|
26
|
-
[e]
|
|
27
|
-
);
|
|
28
|
-
return d(() => (e.waitForOpen().then(() => {
|
|
29
|
-
t("open");
|
|
30
|
-
}), () => {
|
|
31
|
-
t("closed");
|
|
32
|
-
}), [e]), { walletStatus: n, openWallet: i, joinFederation: a };
|
|
13
|
+
}, [t]), n;
|
|
33
14
|
};
|
|
34
|
-
let
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
15
|
+
let b;
|
|
16
|
+
const f = new P(), O = (e) => {
|
|
17
|
+
f.createWallet().then((t) => {
|
|
18
|
+
b = t;
|
|
19
|
+
}), e.debug && f.setLogLevel("debug");
|
|
20
|
+
}, v = y(void 0), C = (e) => {
|
|
21
|
+
const [t, n] = r("closed"), { children: o } = e;
|
|
22
|
+
if (!b)
|
|
40
23
|
throw new Error(
|
|
41
24
|
"You must call setupFedimintWallet() first. See the getting started guide."
|
|
42
25
|
);
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
26
|
+
const a = F(
|
|
27
|
+
() => ({
|
|
28
|
+
wallet: b,
|
|
29
|
+
walletStatus: t,
|
|
30
|
+
setWalletStatus: n
|
|
31
|
+
}),
|
|
32
|
+
[t]
|
|
33
|
+
);
|
|
34
|
+
return m(() => {
|
|
35
|
+
b.waitForOpen().then(() => {
|
|
36
|
+
n("open");
|
|
37
|
+
});
|
|
38
|
+
}, [b]), I(
|
|
39
|
+
v.Provider,
|
|
40
|
+
{ value: a },
|
|
41
|
+
o
|
|
42
|
+
);
|
|
43
|
+
}, E = () => {
|
|
44
|
+
const e = W(v);
|
|
45
|
+
if (!e)
|
|
46
|
+
throw new Error(
|
|
47
|
+
"useOpenWallet must be used within a FedimintWalletProvider"
|
|
48
|
+
);
|
|
49
|
+
const { wallet: t, walletStatus: n, setWalletStatus: o } = e, a = w(() => {
|
|
50
|
+
n !== "open" && (o("opening"), t.open().then((l) => {
|
|
51
|
+
o(l ? "open" : "closed");
|
|
52
|
+
}));
|
|
53
|
+
}, [t]), c = w(
|
|
54
|
+
async (l) => {
|
|
55
|
+
n !== "open" && (o("opening"), await t.joinFederation(l).then((u) => {
|
|
56
|
+
o(u ? "open" : "closed");
|
|
57
|
+
}));
|
|
58
|
+
},
|
|
59
|
+
[t]
|
|
60
|
+
);
|
|
61
|
+
return { walletStatus: n, openWallet: a, joinFederation: c };
|
|
62
|
+
}, g = () => {
|
|
63
|
+
const e = W(v);
|
|
47
64
|
if (!(e != null && e.wallet))
|
|
48
65
|
throw new Error(
|
|
49
66
|
"useFedimintWallet must be used within a FedimintWalletProvider"
|
|
50
67
|
);
|
|
51
68
|
return e.wallet;
|
|
52
|
-
},
|
|
53
|
-
const e =
|
|
54
|
-
async (
|
|
55
|
-
if (
|
|
56
|
-
const
|
|
57
|
-
return
|
|
69
|
+
}, N = () => {
|
|
70
|
+
const e = g(), { walletStatus: t } = E(), [n, o] = r(), [a, c] = r(), [l, u] = r(), p = w(
|
|
71
|
+
async (i, s) => {
|
|
72
|
+
if (t !== "open") throw new Error("Wallet is not open");
|
|
73
|
+
const d = await e.lightning.createInvoice(i, s);
|
|
74
|
+
return o(d), d.invoice;
|
|
58
75
|
},
|
|
59
|
-
[e,
|
|
76
|
+
[e, t]
|
|
60
77
|
);
|
|
61
|
-
return
|
|
62
|
-
if (
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
(
|
|
66
|
-
|
|
78
|
+
return m(() => {
|
|
79
|
+
if (t !== "open" || !n) return;
|
|
80
|
+
const i = e.lightning.subscribeLnReceive(
|
|
81
|
+
n.operation_id,
|
|
82
|
+
(s) => {
|
|
83
|
+
c(s);
|
|
67
84
|
},
|
|
68
|
-
(
|
|
69
|
-
|
|
85
|
+
(s) => {
|
|
86
|
+
u(s);
|
|
70
87
|
}
|
|
71
88
|
);
|
|
72
89
|
return () => {
|
|
73
|
-
|
|
90
|
+
i();
|
|
74
91
|
};
|
|
75
|
-
}, [
|
|
76
|
-
generateInvoice:
|
|
77
|
-
bolt11:
|
|
92
|
+
}, [t, n]), {
|
|
93
|
+
generateInvoice: p,
|
|
94
|
+
bolt11: n == null ? void 0 : n.invoice,
|
|
78
95
|
invoiceStatus: a,
|
|
79
96
|
error: l
|
|
80
97
|
};
|
|
81
|
-
},
|
|
82
|
-
const e =
|
|
83
|
-
async (
|
|
84
|
-
if (
|
|
85
|
-
const
|
|
86
|
-
return
|
|
98
|
+
}, B = () => {
|
|
99
|
+
const e = g(), { walletStatus: t } = E(), [n, o] = r(), [a, c] = r(), [l, u] = r(), p = w(
|
|
100
|
+
async (i) => {
|
|
101
|
+
if (t !== "open") throw new Error("Wallet is not open");
|
|
102
|
+
const s = await e.lightning.payInvoice(i);
|
|
103
|
+
return o(s), s;
|
|
87
104
|
},
|
|
88
|
-
[e,
|
|
105
|
+
[e, t]
|
|
89
106
|
);
|
|
90
|
-
return
|
|
91
|
-
if (
|
|
92
|
-
const
|
|
107
|
+
return m(() => {
|
|
108
|
+
if (t !== "open" || !n) return;
|
|
109
|
+
const i = e.lightning.subscribeLnPay(
|
|
93
110
|
// @ts-ignore
|
|
94
|
-
|
|
95
|
-
(
|
|
96
|
-
|
|
111
|
+
n.payment_type.lightning,
|
|
112
|
+
(s) => {
|
|
113
|
+
c(s);
|
|
97
114
|
},
|
|
98
|
-
(
|
|
99
|
-
|
|
115
|
+
(s) => {
|
|
116
|
+
u(s);
|
|
100
117
|
}
|
|
101
118
|
);
|
|
102
119
|
return () => {
|
|
103
|
-
|
|
120
|
+
i();
|
|
104
121
|
};
|
|
105
|
-
}, [
|
|
106
|
-
payInvoice:
|
|
107
|
-
payment:
|
|
122
|
+
}, [t, n]), {
|
|
123
|
+
payInvoice: p,
|
|
124
|
+
payment: n,
|
|
108
125
|
paymentStatus: a,
|
|
109
126
|
paymentError: l
|
|
110
127
|
};
|
|
128
|
+
}, _ = () => {
|
|
129
|
+
const e = g(), { walletStatus: t } = E(), [n, o] = r(), [a, c] = r(), [l, u] = r(), [p, i] = r();
|
|
130
|
+
return m(() => {
|
|
131
|
+
if (!n) return;
|
|
132
|
+
const d = e.mint.subscribeSpendNotes(
|
|
133
|
+
n,
|
|
134
|
+
(h) => {
|
|
135
|
+
u(h);
|
|
136
|
+
},
|
|
137
|
+
(h) => {
|
|
138
|
+
u("Error"), i(h);
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
return () => {
|
|
142
|
+
d();
|
|
143
|
+
};
|
|
144
|
+
}, [n]), {
|
|
145
|
+
spendEcash: w(
|
|
146
|
+
async (d, h) => {
|
|
147
|
+
if (t !== "open") throw new Error("Wallet is not open");
|
|
148
|
+
const S = await e.mint.spendNotes(
|
|
149
|
+
d * 1e3,
|
|
150
|
+
h
|
|
151
|
+
);
|
|
152
|
+
return o(S.operation_id), c(S.notes), S.notes;
|
|
153
|
+
},
|
|
154
|
+
[e, t]
|
|
155
|
+
),
|
|
156
|
+
notes: a,
|
|
157
|
+
state: l,
|
|
158
|
+
error: p
|
|
159
|
+
};
|
|
160
|
+
}, j = () => {
|
|
161
|
+
const e = g(), { walletStatus: t } = E(), [n, o] = r(), [a, c] = r(), [l, u] = r();
|
|
162
|
+
return m(() => {
|
|
163
|
+
if (!n) return;
|
|
164
|
+
const i = e.mint.subscribeReissueExternalNotes(
|
|
165
|
+
n,
|
|
166
|
+
(s) => {
|
|
167
|
+
c(s);
|
|
168
|
+
},
|
|
169
|
+
(s) => {
|
|
170
|
+
u(s);
|
|
171
|
+
}
|
|
172
|
+
);
|
|
173
|
+
return () => {
|
|
174
|
+
i();
|
|
175
|
+
};
|
|
176
|
+
}, [n]), {
|
|
177
|
+
redeemEcash: w(
|
|
178
|
+
async (i) => {
|
|
179
|
+
if (t !== "open") throw new Error("Wallet is not open");
|
|
180
|
+
try {
|
|
181
|
+
const s = await e.mint.redeemEcash(i);
|
|
182
|
+
o(s);
|
|
183
|
+
} catch (s) {
|
|
184
|
+
c("Error"), u(s);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
[e, t]
|
|
188
|
+
),
|
|
189
|
+
state: a,
|
|
190
|
+
error: l
|
|
191
|
+
};
|
|
111
192
|
};
|
|
112
193
|
export {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
194
|
+
v as FedimintWalletContext,
|
|
195
|
+
C as FedimintWalletProvider,
|
|
196
|
+
O as setupFedimintWallet,
|
|
197
|
+
R as useBalance,
|
|
198
|
+
g as useFedimintWallet,
|
|
199
|
+
E as useOpenWallet,
|
|
200
|
+
j as useReceiveEcash,
|
|
201
|
+
N as useReceiveLightning,
|
|
202
|
+
B as useSendLightning,
|
|
203
|
+
_ as useSpendEcash
|
|
121
204
|
};
|
package/dist/init.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../lib/init.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../lib/init.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,iBAAiB,GAAI,OAAM,OAAe,OAQtD,CAAA"}
|
|
@@ -1,22 +1,38 @@
|
|
|
1
|
-
import { FedimintWallet } from '@fedimint/core-web'
|
|
2
|
-
import {
|
|
1
|
+
import { WalletDirector, FedimintWallet } from '@fedimint/core-web'
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
createElement,
|
|
5
|
+
useEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useState,
|
|
8
|
+
} from 'react'
|
|
3
9
|
|
|
4
10
|
let wallet: FedimintWallet
|
|
11
|
+
const walletDirector = new WalletDirector()
|
|
5
12
|
|
|
6
13
|
type FedimintWalletConfig = {
|
|
7
14
|
lazy?: boolean
|
|
8
15
|
debug?: boolean
|
|
9
16
|
}
|
|
10
17
|
|
|
18
|
+
export type WalletStatus = 'open' | 'closed' | 'opening'
|
|
19
|
+
|
|
11
20
|
export const setupFedimintWallet = (config: FedimintWalletConfig) => {
|
|
12
|
-
|
|
21
|
+
walletDirector.createWallet().then((w) => {
|
|
22
|
+
wallet = w
|
|
23
|
+
})
|
|
13
24
|
if (config.debug) {
|
|
14
|
-
|
|
25
|
+
walletDirector.setLogLevel('debug')
|
|
15
26
|
}
|
|
16
27
|
}
|
|
17
28
|
|
|
18
29
|
export const FedimintWalletContext = createContext<
|
|
19
|
-
|
|
30
|
+
| {
|
|
31
|
+
wallet: FedimintWallet
|
|
32
|
+
walletStatus: WalletStatus
|
|
33
|
+
setWalletStatus: (status: WalletStatus) => void
|
|
34
|
+
}
|
|
35
|
+
| undefined
|
|
20
36
|
>(undefined)
|
|
21
37
|
|
|
22
38
|
export type FedimintWalletProviderProps = {}
|
|
@@ -24,6 +40,7 @@ export type FedimintWalletProviderProps = {}
|
|
|
24
40
|
export const FedimintWalletProvider = (
|
|
25
41
|
parameters: React.PropsWithChildren<FedimintWalletProviderProps>,
|
|
26
42
|
) => {
|
|
43
|
+
const [walletStatus, setWalletStatus] = useState<WalletStatus>('closed')
|
|
27
44
|
const { children } = parameters
|
|
28
45
|
|
|
29
46
|
if (!wallet)
|
|
@@ -31,7 +48,24 @@ export const FedimintWalletProvider = (
|
|
|
31
48
|
'You must call setupFedimintWallet() first. See the getting started guide.',
|
|
32
49
|
)
|
|
33
50
|
|
|
34
|
-
const
|
|
51
|
+
const contextValue = useMemo(
|
|
52
|
+
() => ({
|
|
53
|
+
wallet,
|
|
54
|
+
walletStatus,
|
|
55
|
+
setWalletStatus,
|
|
56
|
+
}),
|
|
57
|
+
[walletStatus],
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
wallet.waitForOpen().then(() => {
|
|
62
|
+
setWalletStatus('open')
|
|
63
|
+
})
|
|
64
|
+
}, [wallet])
|
|
35
65
|
|
|
36
|
-
return createElement(
|
|
66
|
+
return createElement(
|
|
67
|
+
FedimintWalletContext.Provider,
|
|
68
|
+
{ value: contextValue },
|
|
69
|
+
children,
|
|
70
|
+
)
|
|
37
71
|
}
|
package/lib/hooks/index.ts
CHANGED
|
@@ -3,3 +3,5 @@ export { useOpenWallet } from './useOpenWallet'
|
|
|
3
3
|
export { useFedimintWallet } from './useFedimintWallet'
|
|
4
4
|
export { useReceiveLightning } from './useReceiveLightning'
|
|
5
5
|
export { useSendLightning } from './useSendLightning'
|
|
6
|
+
export { useSpendEcash } from './useSpendEcash'
|
|
7
|
+
export { useReceiveEcash } from './useReceiveEcash'
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { useCallback,
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type WalletStatus = 'open' | 'closed' | 'opening'
|
|
1
|
+
import { useCallback, useContext } from 'react'
|
|
2
|
+
import { FedimintWalletContext } from '../contexts/FedimintWalletContext'
|
|
5
3
|
|
|
6
4
|
export const useOpenWallet = () => {
|
|
7
|
-
const
|
|
8
|
-
|
|
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
|
|
9
14
|
|
|
10
15
|
const openWallet = useCallback(() => {
|
|
11
16
|
if (walletStatus === 'open') return
|
|
@@ -22,21 +27,12 @@ export const useOpenWallet = () => {
|
|
|
22
27
|
|
|
23
28
|
setWalletStatus('opening')
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
await wallet.joinFederation(invite).then((res) => {
|
|
31
|
+
setWalletStatus(res ? 'open' : 'closed')
|
|
32
|
+
})
|
|
27
33
|
},
|
|
28
34
|
[wallet],
|
|
29
35
|
)
|
|
30
36
|
|
|
31
|
-
useEffect(() => {
|
|
32
|
-
wallet.waitForOpen().then(() => {
|
|
33
|
-
setWalletStatus('open')
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
return () => {
|
|
37
|
-
setWalletStatus('closed')
|
|
38
|
-
}
|
|
39
|
-
}, [wallet])
|
|
40
|
-
|
|
41
37
|
return { walletStatus, openWallet, joinFederation }
|
|
42
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,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/init.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { FedimintWallet } from '@fedimint/core-web'
|
|
1
|
+
import { FedimintWallet, WalletDirector } from '@fedimint/core-web'
|
|
2
2
|
|
|
3
3
|
let wallet: FedimintWallet | undefined
|
|
4
4
|
|
|
5
|
+
const walletDirector = new WalletDirector()
|
|
6
|
+
|
|
5
7
|
export const initFedimintReact = (lazy: boolean = false) => {
|
|
6
8
|
if (!lazy) {
|
|
7
|
-
|
|
9
|
+
walletDirector.createWallet().then((w) => {
|
|
10
|
+
wallet = w
|
|
11
|
+
})
|
|
8
12
|
}
|
|
9
13
|
|
|
10
14
|
return {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedimint/react",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -10,22 +10,22 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"peerDependencies": {
|
|
12
12
|
"react": "^18.3.1",
|
|
13
|
-
"@fedimint/core-web": "0.0
|
|
13
|
+
"@fedimint/core-web": "0.1.0"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"@types/react": ">=
|
|
17
|
-
"@types/react-dom": "^18.3.
|
|
16
|
+
"@types/react": ">=19.1.4",
|
|
17
|
+
"@types/react-dom": "^18.3.7",
|
|
18
18
|
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
19
19
|
"@typescript-eslint/parser": "^7.18.0",
|
|
20
|
-
"@vitejs/plugin-react": "^4.
|
|
21
|
-
"eslint": "^9.
|
|
20
|
+
"@vitejs/plugin-react": "^4.4.1",
|
|
21
|
+
"eslint": "^9.27.0",
|
|
22
22
|
"eslint-plugin-react-hooks": "^4.6.2",
|
|
23
|
-
"eslint-plugin-react-refresh": "^0.4.
|
|
24
|
-
"react-dom": ">=
|
|
25
|
-
"typescript": "^5.
|
|
26
|
-
"vite": "^5.4.
|
|
27
|
-
"vite-plugin-dts": "^4.
|
|
28
|
-
"vite-plugin-wasm": "^3.
|
|
23
|
+
"eslint-plugin-react-refresh": "^0.4.20",
|
|
24
|
+
"react-dom": ">=19.1.0",
|
|
25
|
+
"typescript": "^5.8.3",
|
|
26
|
+
"vite": "^5.4.20",
|
|
27
|
+
"vite-plugin-dts": "^4.5.4",
|
|
28
|
+
"vite-plugin-wasm": "^3.4.1"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"dev": "vite",
|