@fedimint/react 0.0.0-canary-20241023092328
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/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/contexts/FedimintWalletContext.d.ts +15 -0
- package/dist/contexts/FedimintWalletContext.d.ts.map +1 -0
- package/dist/contexts/index.d.ts +3 -0
- package/dist/contexts/index.d.ts.map +1 -0
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/useBalance.d.ts +2 -0
- package/dist/hooks/useBalance.d.ts.map +1 -0
- package/dist/hooks/useFedimintWallet.d.ts +2 -0
- package/dist/hooks/useFedimintWallet.d.ts.map +1 -0
- package/dist/hooks/useOpenWallet.d.ts +8 -0
- package/dist/hooks/useOpenWallet.d.ts.map +1 -0
- package/dist/hooks/useReceiveLightning.d.ts +8 -0
- package/dist/hooks/useReceiveLightning.d.ts.map +1 -0
- package/dist/hooks/useSendLightning.d.ts +8 -0
- package/dist/hooks/useSendLightning.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +121 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.d.ts.map +1 -0
- package/lib/contexts/FedimintWalletContext.ts +37 -0
- package/lib/contexts/index.ts +7 -0
- package/lib/hooks/index.ts +5 -0
- package/lib/hooks/useBalance.ts +22 -0
- package/lib/hooks/useFedimintWallet.ts +12 -0
- package/lib/hooks/useOpenWallet.ts +42 -0
- package/lib/hooks/useReceiveLightning.ts +46 -0
- package/lib/hooks/useSendLightning.ts +49 -0
- package/lib/index.ts +2 -0
- package/lib/init.ts +11 -0
- 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,30 @@
|
|
|
1
|
+
# React + TypeScript + Vite
|
|
2
|
+
|
|
3
|
+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
|
4
|
+
|
|
5
|
+
Currently, two official plugins are available:
|
|
6
|
+
|
|
7
|
+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
|
|
8
|
+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
|
9
|
+
|
|
10
|
+
## Expanding the ESLint configuration
|
|
11
|
+
|
|
12
|
+
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
|
|
13
|
+
|
|
14
|
+
- Configure the top-level `parserOptions` property like this:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
export default {
|
|
18
|
+
// other rules...
|
|
19
|
+
parserOptions: {
|
|
20
|
+
ecmaVersion: 'latest',
|
|
21
|
+
sourceType: 'module',
|
|
22
|
+
project: ['./tsconfig.json', './tsconfig.node.json'],
|
|
23
|
+
tsconfigRootDir: __dirname,
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
|
|
29
|
+
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
|
|
30
|
+
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FedimintWallet } from '@fedimint/core-web';
|
|
2
|
+
type FedimintWalletConfig = {
|
|
3
|
+
lazy?: boolean;
|
|
4
|
+
debug?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare const setupFedimintWallet: (config: FedimintWalletConfig) => void;
|
|
7
|
+
export declare const FedimintWalletContext: import('react').Context<{
|
|
8
|
+
wallet: FedimintWallet;
|
|
9
|
+
} | undefined>;
|
|
10
|
+
export type FedimintWalletProviderProps = {};
|
|
11
|
+
export declare const FedimintWalletProvider: (parameters: React.PropsWithChildren<FedimintWalletProviderProps>) => import('react').FunctionComponentElement<import('react').ProviderProps<{
|
|
12
|
+
wallet: FedimintWallet;
|
|
13
|
+
} | undefined>>;
|
|
14
|
+
export {};
|
|
15
|
+
//# 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;AAKnD,KAAK,oBAAoB,GAAG;IAC1B,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,eAAO,MAAM,mBAAmB,WAAY,oBAAoB,SAK/D,CAAA;AAED,eAAO,MAAM,qBAAqB;YACtB,cAAc;cACd,CAAA;AAEZ,MAAM,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAE5C,eAAO,MAAM,sBAAsB,eACrB,KAAK,CAAC,iBAAiB,CAAC,2BAA2B,CAAC;YANtD,cAAc;eAkBzB,CAAA"}
|
|
@@ -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,6 @@
|
|
|
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
|
+
//# 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"}
|
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"useFedimintWallet.d.ts","sourceRoot":"","sources":["../../lib/hooks/useFedimintWallet.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,iBAAiB,mDAQ7B,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type WalletStatus = 'open' | 'closed' | 'opening';
|
|
2
|
+
export declare const useOpenWallet: () => {
|
|
3
|
+
walletStatus: WalletStatus | undefined;
|
|
4
|
+
openWallet: () => void;
|
|
5
|
+
joinFederation: (invite: string) => Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=useOpenWallet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useOpenWallet.d.ts","sourceRoot":"","sources":["../../lib/hooks/useOpenWallet.ts"],"names":[],"mappings":"AAGA,KAAK,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;AAEjD,eAAO,MAAM,aAAa;;;6BAcP,MAAM;CAsBxB,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"}
|
package/dist/index.d.ts
ADDED
|
@@ -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,121 @@
|
|
|
1
|
+
import { useState as s, useEffect as d, useCallback as u, createContext as y, createElement as f, useContext as S } from "react";
|
|
2
|
+
import { FedimintWallet as W } from "@fedimint/core-web";
|
|
3
|
+
const I = () => {
|
|
4
|
+
const e = w(), { walletStatus: n } = b(), [t, i] = s();
|
|
5
|
+
return d(() => {
|
|
6
|
+
if (n !== "open") return;
|
|
7
|
+
const a = e.balance.subscribeBalance((r) => {
|
|
8
|
+
i(r);
|
|
9
|
+
});
|
|
10
|
+
return () => {
|
|
11
|
+
a();
|
|
12
|
+
};
|
|
13
|
+
}, [n]), t;
|
|
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 };
|
|
33
|
+
};
|
|
34
|
+
let p;
|
|
35
|
+
const L = (e) => {
|
|
36
|
+
p = new W(!!e.lazy), e.debug && p.setLogLevel("debug");
|
|
37
|
+
}, h = y(void 0), P = (e) => {
|
|
38
|
+
const { children: n } = e;
|
|
39
|
+
if (!p)
|
|
40
|
+
throw new Error(
|
|
41
|
+
"You must call setupFedimintWallet() first. See the getting started guide."
|
|
42
|
+
);
|
|
43
|
+
const t = { value: { wallet: p } };
|
|
44
|
+
return f(h.Provider, t, n);
|
|
45
|
+
}, w = () => {
|
|
46
|
+
const e = S(h);
|
|
47
|
+
if (!(e != null && e.wallet))
|
|
48
|
+
throw new Error(
|
|
49
|
+
"useFedimintWallet must be used within a FedimintWalletProvider"
|
|
50
|
+
);
|
|
51
|
+
return e.wallet;
|
|
52
|
+
}, x = () => {
|
|
53
|
+
const e = w(), { walletStatus: n } = b(), [t, i] = s(), [a, r] = s(), [l, g] = s(), m = u(
|
|
54
|
+
async (c, o) => {
|
|
55
|
+
if (n !== "open") throw new Error("Wallet is not open");
|
|
56
|
+
const v = await e.lightning.createInvoice(c, o);
|
|
57
|
+
return i(v), v.invoice;
|
|
58
|
+
},
|
|
59
|
+
[e, n]
|
|
60
|
+
);
|
|
61
|
+
return d(() => {
|
|
62
|
+
if (n !== "open" || !t) return;
|
|
63
|
+
const c = e.lightning.subscribeLnReceive(
|
|
64
|
+
t.operation_id,
|
|
65
|
+
(o) => {
|
|
66
|
+
r(o);
|
|
67
|
+
},
|
|
68
|
+
(o) => {
|
|
69
|
+
g(o);
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
return () => {
|
|
73
|
+
c();
|
|
74
|
+
};
|
|
75
|
+
}, [n, t]), {
|
|
76
|
+
generateInvoice: m,
|
|
77
|
+
bolt11: t == null ? void 0 : t.invoice,
|
|
78
|
+
invoiceStatus: a,
|
|
79
|
+
error: l
|
|
80
|
+
};
|
|
81
|
+
}, C = () => {
|
|
82
|
+
const e = w(), { walletStatus: n } = b(), [t, i] = s(), [a, r] = s(), [l, g] = s(), m = u(
|
|
83
|
+
async (c) => {
|
|
84
|
+
if (n !== "open") throw new Error("Wallet is not open");
|
|
85
|
+
const o = await e.lightning.payInvoice(c);
|
|
86
|
+
return i(o), o;
|
|
87
|
+
},
|
|
88
|
+
[e, n]
|
|
89
|
+
);
|
|
90
|
+
return d(() => {
|
|
91
|
+
if (n !== "open" || !t) return;
|
|
92
|
+
const c = e.lightning.subscribeLnPay(
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
t.payment_type.lightning,
|
|
95
|
+
(o) => {
|
|
96
|
+
r(o);
|
|
97
|
+
},
|
|
98
|
+
(o) => {
|
|
99
|
+
g(o);
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
return () => {
|
|
103
|
+
c();
|
|
104
|
+
};
|
|
105
|
+
}, [n, t]), {
|
|
106
|
+
payInvoice: m,
|
|
107
|
+
payment: t,
|
|
108
|
+
paymentStatus: a,
|
|
109
|
+
paymentError: l
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
export {
|
|
113
|
+
h as FedimintWalletContext,
|
|
114
|
+
P as FedimintWalletProvider,
|
|
115
|
+
L as setupFedimintWallet,
|
|
116
|
+
I as useBalance,
|
|
117
|
+
w as useFedimintWallet,
|
|
118
|
+
b as useOpenWallet,
|
|
119
|
+
x as useReceiveLightning,
|
|
120
|
+
C as useSendLightning
|
|
121
|
+
};
|
package/dist/init.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../lib/init.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,iBAAiB,UAAU,OAAO,OAM9C,CAAA"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { FedimintWallet } from '@fedimint/core-web'
|
|
2
|
+
import { createContext, createElement } from 'react'
|
|
3
|
+
|
|
4
|
+
let wallet: FedimintWallet
|
|
5
|
+
|
|
6
|
+
type FedimintWalletConfig = {
|
|
7
|
+
lazy?: boolean
|
|
8
|
+
debug?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const setupFedimintWallet = (config: FedimintWalletConfig) => {
|
|
12
|
+
wallet = new FedimintWallet(!!config.lazy)
|
|
13
|
+
if (config.debug) {
|
|
14
|
+
wallet.setLogLevel('debug')
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const FedimintWalletContext = createContext<
|
|
19
|
+
{ wallet: FedimintWallet } | undefined
|
|
20
|
+
>(undefined)
|
|
21
|
+
|
|
22
|
+
export type FedimintWalletProviderProps = {}
|
|
23
|
+
|
|
24
|
+
export const FedimintWalletProvider = (
|
|
25
|
+
parameters: React.PropsWithChildren<FedimintWalletProviderProps>,
|
|
26
|
+
) => {
|
|
27
|
+
const { children } = parameters
|
|
28
|
+
|
|
29
|
+
if (!wallet)
|
|
30
|
+
throw new Error(
|
|
31
|
+
'You must call setupFedimintWallet() first. See the getting started guide.',
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
const props = { value: { wallet } }
|
|
35
|
+
|
|
36
|
+
return createElement(FedimintWalletContext.Provider, props, children)
|
|
37
|
+
}
|
|
@@ -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,42 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
2
|
+
import { useFedimintWallet } from '.'
|
|
3
|
+
|
|
4
|
+
type WalletStatus = 'open' | 'closed' | 'opening'
|
|
5
|
+
|
|
6
|
+
export const useOpenWallet = () => {
|
|
7
|
+
const wallet = useFedimintWallet()
|
|
8
|
+
const [walletStatus, setWalletStatus] = useState<WalletStatus>()
|
|
9
|
+
|
|
10
|
+
const openWallet = useCallback(() => {
|
|
11
|
+
if (walletStatus === 'open') return
|
|
12
|
+
|
|
13
|
+
setWalletStatus('opening')
|
|
14
|
+
wallet.open().then((res) => {
|
|
15
|
+
setWalletStatus(res ? 'open' : 'closed')
|
|
16
|
+
})
|
|
17
|
+
}, [wallet])
|
|
18
|
+
|
|
19
|
+
const joinFederation = useCallback(
|
|
20
|
+
async (invite: string) => {
|
|
21
|
+
if (walletStatus === 'open') return
|
|
22
|
+
|
|
23
|
+
setWalletStatus('opening')
|
|
24
|
+
|
|
25
|
+
const res = await wallet.joinFederation(invite)
|
|
26
|
+
setWalletStatus(res ? 'open' : 'closed')
|
|
27
|
+
},
|
|
28
|
+
[wallet],
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
wallet.waitForOpen().then(() => {
|
|
33
|
+
setWalletStatus('open')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
return () => {
|
|
37
|
+
setWalletStatus('closed')
|
|
38
|
+
}
|
|
39
|
+
}, [wallet])
|
|
40
|
+
|
|
41
|
+
return { walletStatus, openWallet, joinFederation }
|
|
42
|
+
}
|
|
@@ -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
|
+
}
|
package/lib/index.ts
ADDED
package/lib/init.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fedimint/react",
|
|
3
|
+
"version": "0.0.0-canary-20241023092328",
|
|
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-canary-20241023092328"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/react": ">=18.3.11",
|
|
17
|
+
"@types/react-dom": "^18.3.0",
|
|
18
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
19
|
+
"@typescript-eslint/parser": "^7.18.0",
|
|
20
|
+
"@vitejs/plugin-react": "^4.3.2",
|
|
21
|
+
"eslint": "^9.11.1",
|
|
22
|
+
"eslint-plugin-react-hooks": "^4.6.2",
|
|
23
|
+
"eslint-plugin-react-refresh": "^0.4.12",
|
|
24
|
+
"react-dom": ">=18.3.1",
|
|
25
|
+
"typescript": "^5.6.2",
|
|
26
|
+
"vite": "^5.4.8",
|
|
27
|
+
"vite-plugin-dts": "^4.2.4",
|
|
28
|
+
"vite-plugin-wasm": "^3.3.0"
|
|
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
|
+
}
|