Socket
Book a DemoInstallSign in
Socket

@fedimint/react

Package Overview
Dependencies
Maintainers
5
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fedimint/react - npm Package Compare versions

Comparing version

to
0.0.0-main-3d7dee585520f4d811de12cb43416909de43ab54

12

dist/contexts/FedimintWalletContext.d.ts

@@ -1,2 +0,2 @@

import { Wallet } from '@fedimint/core-web';
import { FedimintWallet } from '@fedimint/core-web';
type FedimintWalletConfig = {

@@ -6,18 +6,16 @@ lazy?: boolean;

};
export type WalletStatus = 'open' | 'closed' | 'opening' | 'uninitialized';
export declare const setupFedimintWallet: (config?: FedimintWalletConfig) => Promise<void>;
export type WalletStatus = 'open' | 'closed' | 'opening';
export declare const setupFedimintWallet: (config: FedimintWalletConfig) => void;
export declare const FedimintWalletContext: import('react').Context<{
wallet: Wallet | null;
wallet: FedimintWallet;
walletStatus: WalletStatus;
setWalletStatus: (status: WalletStatus) => void;
setWallet: (wallet: Wallet | null) => void;
} | undefined>;
export type FedimintWalletProviderProps = {};
export declare const FedimintWalletProvider: (parameters: React.PropsWithChildren<FedimintWalletProviderProps>) => import('react').FunctionComponentElement<import('react').ProviderProps<{
wallet: Wallet | null;
wallet: FedimintWallet;
walletStatus: WalletStatus;
setWalletStatus: (status: WalletStatus) => void;
setWallet: (wallet: Wallet | null) => void;
} | undefined>>;
export {};
//# sourceMappingURL=FedimintWalletContext.d.ts.map

@@ -1,2 +0,2 @@

export declare const useFedimintWallet: () => import('@fedimint/core-web').Wallet | null;
export declare const useFedimintWallet: () => import('@fedimint/core-web').FedimintWallet;
//# sourceMappingURL=useFedimintWallet.d.ts.map
export declare const useOpenWallet: () => {
walletStatus: import('../contexts/FedimintWalletContext').WalletStatus;
openWallet: (walletId: string) => Promise<boolean | undefined>;
joinFederation: (invite: string, walletId?: string) => Promise<boolean | undefined>;
getWalletsList: () => import('@fedimint/core-web').WalletInfo[];
openWallet: () => void;
joinFederation: (invite: string) => Promise<void>;
};
//# sourceMappingURL=useOpenWallet.d.ts.map
export * from './hooks';
export * from './contexts';
export * from './init';
//# sourceMappingURL=index.d.ts.map

@@ -1,9 +0,9 @@

import { useState as i, useEffect as m, createContext as F, useMemo as I, createElement as L, useContext as g, useCallback as d } from "react";
import { initialize as S, setLogLevel as P, openWallet as R, joinFederation as x, listClients as C } from "@fedimint/core-web";
const O = () => {
const e = h(), { walletStatus: r } = f(), [t, s] = i();
return m(() => {
if (r !== "open" || !e) return;
import { useState as r, useEffect as h, createContext as y, useMemo as W, createElement as F, useContext as f, useCallback as m } from "react";
import { FedimintWallet as I } from "@fedimint/core-web";
const L = () => {
const e = g(), { walletStatus: n } = E(), [t, o] = r();
return h(() => {
if (n !== "open") return;
const a = e.balance.subscribeBalance((c) => {
s(c);
o(c);
});

@@ -13,34 +13,32 @@ return () => {

};
}, [r, e]), t;
}, [n]), t;
};
let y = null, W = !1;
const j = async (e = {}) => {
W || (await S(), W = !0), e.debug && P("debug");
}, E = F(void 0), B = (e) => {
const [r, t] = i(
() => W ? "closed" : "uninitialized"
), [s, a] = i(y), { children: c } = e, u = I(
let w;
const R = (e) => {
w = new I(!!e.lazy), e.debug && w.setLogLevel("debug");
}, v = y(void 0), O = (e) => {
const [n, t] = r("closed"), { children: o } = e;
if (!w)
throw new Error(
"You must call setupFedimintWallet() first. See the getting started guide."
);
const a = W(
() => ({
wallet: s,
walletStatus: r,
setWalletStatus: t,
setWallet: (l) => {
y = l, a(l);
}
wallet: w,
walletStatus: n,
setWalletStatus: t
}),
[s, r]
[n]
);
return m(() => {
if (!W) {
t("uninitialized");
return;
}
s && t("open");
}, [s]), L(
E.Provider,
{ value: u },
c
return h(() => {
w.waitForOpen().then(() => {
t("open");
});
}, [w]), F(
v.Provider,
{ value: a },
o
);
}, f = () => {
const e = g(E);
}, E = () => {
const e = f(v);
if (!e)

@@ -50,33 +48,18 @@ throw new Error(

);
const { wallet: r, walletStatus: t, setWalletStatus: s, setWallet: a } = e, c = d(
async (p) => {
if (t !== "open") {
s("opening");
try {
const o = await R(p);
return a(o), s("open"), !0;
} catch {
return s("closed"), !1;
}
}
const { wallet: n, walletStatus: t, setWalletStatus: o } = e, a = m(() => {
t !== "open" && (o("opening"), n.open().then((l) => {
o(l ? "open" : "closed");
}));
}, [n]), c = m(
async (l) => {
t !== "open" && (o("opening"), await n.joinFederation(l).then((u) => {
o(u ? "open" : "closed");
}));
},
[s, a, t]
), u = d(
async (p, o) => {
if (t !== "open") {
s("opening");
try {
const n = await x(p, !1, o);
return a(n), s("open"), !0;
} catch {
return s("closed"), !1;
}
}
},
[s, a, t]
), l = d(() => C(), []);
return { walletStatus: t, openWallet: c, joinFederation: u, getWalletsList: l };
}, h = () => {
const e = g(E);
if (!e)
[n]
);
return { walletStatus: t, openWallet: a, joinFederation: c };
}, g = () => {
const e = f(v);
if (!(e != null && e.wallet))
throw new Error(

@@ -86,142 +69,137 @@ "useFedimintWallet must be used within a FedimintWalletProvider"

return e.wallet;
}, _ = () => {
const e = h(), { walletStatus: r } = f(), [t, s] = i(), [a, c] = i(), [u, l] = i(), p = d(
async (o, n) => {
if (r !== "open" || !e)
throw new Error("Wallet is not open");
const w = await e.lightning.createInvoice(o, n);
return s(w), w.invoice;
}, C = () => {
const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), p = m(
async (i, s) => {
if (n !== "open") throw new Error("Wallet is not open");
const d = await e.lightning.createInvoice(i, s);
return o(d), d.invoice;
},
[e, r]
[e, n]
);
return m(() => {
if (r !== "open" || !t || !e) return;
const o = e.lightning.subscribeLnReceive(
return h(() => {
if (n !== "open" || !t) return;
const i = e.lightning.subscribeLnReceive(
t.operation_id,
(n) => {
c(n);
(s) => {
c(s);
},
(n) => {
l(n);
(s) => {
u(s);
}
);
return () => {
o();
i();
};
}, [r, t, e]), {
}, [n, t]), {
generateInvoice: p,
bolt11: t == null ? void 0 : t.invoice,
invoiceStatus: a,
error: u
error: l
};
}, $ = () => {
const e = h(), { walletStatus: r } = f(), [t, s] = i(), [a, c] = i(), [u, l] = i(), p = d(
async (o) => {
if (r !== "open" || !e)
throw new Error("Wallet is not open");
const n = await e.lightning.payInvoice(o);
return s(n), n;
}, N = () => {
const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), p = m(
async (i) => {
if (n !== "open") throw new Error("Wallet is not open");
const s = await e.lightning.payInvoice(i);
return o(s), s;
},
[e, r]
[e, n]
);
return m(() => {
if (r !== "open" || !t || !e) return;
const o = e.lightning.subscribeLnPay(
return h(() => {
if (n !== "open" || !t) return;
const i = e.lightning.subscribeLnPay(
// @ts-ignore
t.payment_type.lightning,
(n) => {
c(n);
(s) => {
c(s);
},
(n) => {
l(n);
(s) => {
u(s);
}
);
return () => {
o();
i();
};
}, [r, t, e]), {
}, [n, t]), {
payInvoice: p,
payment: t,
paymentStatus: a,
paymentError: u
paymentError: l
};
}, k = () => {
const e = h(), { walletStatus: r } = f(), [t, s] = i(), [a, c] = i(), [u, l] = i(), [p, o] = i();
return m(() => {
if (!t || !e) return;
const w = e.mint.subscribeSpendNotes(
}, B = () => {
const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r(), [p, i] = r();
return h(() => {
if (!t) return;
const d = e.mint.subscribeSpendNotes(
t,
(b) => {
l(b);
u(b);
},
(b) => {
l("Error"), o(b);
u("Error"), i(b);
}
);
return () => {
w();
d();
};
}, [t, e]), {
spendEcash: d(
async (w, b) => {
if (r !== "open" || !e)
throw new Error("Wallet is not open");
const v = await e.mint.spendNotes(
w * 1e3,
}, [t]), {
spendEcash: m(
async (d, b) => {
if (n !== "open") throw new Error("Wallet is not open");
const S = await e.mint.spendNotes(
d * 1e3,
b
);
return s(v.operation_id), c(v.notes), v.notes;
return o(S.operation_id), c(S.notes), S.notes;
},
[e, r]
[e, n]
),
notes: a,
state: u,
state: l,
error: p
};
}, M = () => {
const e = h(), { walletStatus: r } = f(), [t, s] = i(), [a, c] = i(), [u, l] = i();
return m(() => {
if (!t || !e) return;
const o = e.mint.subscribeReissueExternalNotes(
}, _ = () => {
const e = g(), { walletStatus: n } = E(), [t, o] = r(), [a, c] = r(), [l, u] = r();
return h(() => {
if (!t) return;
const i = e.mint.subscribeReissueExternalNotes(
t,
(n) => {
c(n);
(s) => {
c(s);
},
(n) => {
l(n);
(s) => {
u(s);
}
);
return () => {
o();
i();
};
}, [t, e]), {
redeemEcash: d(
async (o) => {
if (r !== "open" || !e)
throw new Error("Wallet is not open");
}, [t]), {
redeemEcash: m(
async (i) => {
if (n !== "open") throw new Error("Wallet is not open");
try {
const n = await e.mint.redeemEcash(o);
s(n);
} catch (n) {
c("Error"), l(n);
const s = await e.mint.redeemEcash(i);
o(s);
} catch (s) {
c("Error"), u(s);
}
},
[e, r]
[e, n]
),
state: a,
error: u
error: l
};
}, V = async (e = !1) => (e || await S(), {});
};
export {
E as FedimintWalletContext,
B as FedimintWalletProvider,
V as initFedimintReact,
j as setupFedimintWallet,
O as useBalance,
h as useFedimintWallet,
f as useOpenWallet,
M as useReceiveEcash,
_ as useReceiveLightning,
$ as useSendLightning,
k as useSpendEcash
v as FedimintWalletContext,
O as FedimintWalletProvider,
R as setupFedimintWallet,
L as useBalance,
g as useFedimintWallet,
E as useOpenWallet,
_ as useReceiveEcash,
C as useReceiveLightning,
N as useSendLightning,
B as useSpendEcash
};

@@ -1,2 +0,2 @@

export declare const initFedimintReact: (lazy?: boolean) => Promise<{}>;
export declare const initFedimintReact: (lazy?: boolean) => {};
//# sourceMappingURL=init.d.ts.map

@@ -0,8 +1,3 @@

import { FedimintWallet } from '@fedimint/core-web'
import {
Wallet,
initialize,
setLogLevel,
type WalletInfo,
} from '@fedimint/core-web'
import {
createContext,

@@ -15,4 +10,3 @@ createElement,

let currentWallet: Wallet | null = null
let isInitialized = false
let wallet: FedimintWallet

@@ -24,14 +18,8 @@ type FedimintWalletConfig = {

export type WalletStatus = 'open' | 'closed' | 'opening' | 'uninitialized'
export type WalletStatus = 'open' | 'closed' | 'opening'
export const setupFedimintWallet = async (
config: FedimintWalletConfig = {},
) => {
if (!isInitialized) {
await initialize()
isInitialized = true
}
export const setupFedimintWallet = (config: FedimintWalletConfig) => {
wallet = new FedimintWallet(!!config.lazy)
if (config.debug) {
setLogLevel('debug')
wallet.setLogLevel('debug')
}

@@ -42,6 +30,5 @@ }

| {
wallet: Wallet | null
wallet: FedimintWallet
walletStatus: WalletStatus
setWalletStatus: (status: WalletStatus) => void
setWallet: (wallet: Wallet | null) => void
}

@@ -56,8 +43,10 @@ | undefined

) => {
const [walletStatus, setWalletStatus] = useState<WalletStatus>(() =>
isInitialized ? 'closed' : 'uninitialized',
)
const [wallet, setWallet] = useState<Wallet | null>(currentWallet)
const [walletStatus, setWalletStatus] = useState<WalletStatus>('closed')
const { children } = parameters
if (!wallet)
throw new Error(
'You must call setupFedimintWallet() first. See the getting started guide.',
)
const contextValue = useMemo(

@@ -68,19 +57,10 @@ () => ({

setWalletStatus,
setWallet: (newWallet: Wallet | null) => {
currentWallet = newWallet
setWallet(newWallet)
},
}),
[wallet, walletStatus],
[walletStatus],
)
useEffect(() => {
if (!isInitialized) {
setWalletStatus('uninitialized')
return
}
if (wallet) {
wallet.waitForOpen().then(() => {
setWalletStatus('open')
}
})
}, [wallet])

@@ -87,0 +67,0 @@

@@ -10,5 +10,5 @@ import { useEffect, useState } from 'react'

useEffect(() => {
if (walletStatus !== 'open' || !wallet) return
if (walletStatus !== 'open') return
const unsubscribe = wallet.balance.subscribeBalance((balance: number) => {
const unsubscribe = wallet.balance.subscribeBalance((balance) => {
setBalance(balance)

@@ -20,5 +20,5 @@ })

}
}, [walletStatus, wallet])
}, [walletStatus])
return balance
}

@@ -6,3 +6,3 @@ import { useContext } from 'react'

const value = useContext(FedimintWalletContext)
if (!value) {
if (!value?.wallet) {
throw new Error(

@@ -9,0 +9,0 @@ 'useFedimintWallet must be used within a FedimintWalletProvider',

import { useCallback, useContext } from 'react'
import {
openWallet as openWalletCore,
joinFederation as joinFederationCore,
getWallet,
listClients,
} from '@fedimint/core-web'
import { FedimintWalletContext } from '../contexts/FedimintWalletContext'

@@ -19,45 +13,27 @@

const { wallet, walletStatus, setWalletStatus, setWallet } = value
const { wallet, walletStatus, setWalletStatus } = value
const openWallet = useCallback(
async (walletId: string) => {
if (walletStatus === 'open') return
const openWallet = useCallback(() => {
if (walletStatus === 'open') return
setWalletStatus('opening')
try {
const openedWallet = await openWalletCore(walletId)
setWallet(openedWallet)
setWalletStatus('open')
return true
} catch (error: any) {
setWalletStatus('closed')
return false
}
},
[setWalletStatus, setWallet, walletStatus],
)
setWalletStatus('opening')
wallet.open().then((res) => {
setWalletStatus(res ? 'open' : 'closed')
})
}, [wallet])
const joinFederation = useCallback(
async (invite: string, walletId?: string) => {
async (invite: string) => {
if (walletStatus === 'open') return
setWalletStatus('opening')
try {
const newWallet = await joinFederationCore(invite, false, walletId)
setWallet(newWallet)
setWalletStatus('open')
return true
} catch (error: any) {
setWalletStatus('closed')
return false
}
await wallet.joinFederation(invite).then((res) => {
setWalletStatus(res ? 'open' : 'closed')
})
},
[setWalletStatus, setWallet, walletStatus],
[wallet],
)
const getWalletsList = useCallback(() => {
return listClients()
}, [])
return { walletStatus, openWallet, joinFederation, getWalletsList }
return { walletStatus, openWallet, joinFederation }
}

@@ -14,10 +14,10 @@ import { useCallback, useEffect, useState } from 'react'

useEffect(() => {
if (!operationId || !wallet) return
if (!operationId) return
const unsubscribe = wallet.mint.subscribeReissueExternalNotes(
operationId,
(_state: ReissueExternalNotesState) => {
(_state) => {
setState(_state)
},
(error: string) => {
(error) => {
setError(error)

@@ -30,8 +30,7 @@ },

}
}, [operationId, wallet])
}, [operationId])
const redeemEcash = useCallback(
async (notes: string) => {
if (walletStatus !== 'open' || !wallet)
throw new Error('Wallet is not open')
if (walletStatus !== 'open') throw new Error('Wallet is not open')
try {

@@ -38,0 +37,0 @@ const response = await wallet.mint.redeemEcash(notes)

@@ -15,4 +15,3 @@ import { useCallback, useEffect, useState } from 'react'

async (amount: number, description: string) => {
if (walletStatus !== 'open' || !wallet)
throw new Error('Wallet is not open')
if (walletStatus !== 'open') throw new Error('Wallet is not open')
const response = await wallet.lightning.createInvoice(amount, description)

@@ -26,9 +25,9 @@ setInvoice(response)

useEffect(() => {
if (walletStatus !== 'open' || !invoice || !wallet) return
if (walletStatus !== 'open' || !invoice) return
const unsubscribe = wallet.lightning.subscribeLnReceive(
invoice.operation_id,
(state: LnReceiveState) => {
(state) => {
setInvoiceReceiveState(state)
},
(error: string) => {
(error) => {
setError(error)

@@ -41,3 +40,3 @@ },

}
}, [walletStatus, invoice, wallet])
}, [walletStatus, invoice])

@@ -44,0 +43,0 @@ return {

@@ -17,4 +17,3 @@ import { useCallback, useEffect, useState } from 'react'

async (bolt11: string) => {
if (walletStatus !== 'open' || !wallet)
throw new Error('Wallet is not open')
if (walletStatus !== 'open') throw new Error('Wallet is not open')
const response = await wallet.lightning.payInvoice(bolt11)

@@ -28,10 +27,10 @@ setPayment(response)

useEffect(() => {
if (walletStatus !== 'open' || !payment || !wallet) return
if (walletStatus !== 'open' || !payment) return
const unsubscribe = wallet.lightning.subscribeLnPay(
// @ts-ignore
payment.payment_type.lightning,
(state: LnPayState) => {
(state) => {
setPaymentState(state)
},
(error: string) => {
(error) => {
setError(error)

@@ -44,3 +43,3 @@ },

}
}, [walletStatus, payment, wallet])
}, [walletStatus, payment])

@@ -47,0 +46,0 @@ return {

@@ -16,10 +16,10 @@ import { useCallback, useEffect, useState } from 'react'

useEffect(() => {
if (!operationId || !wallet) return
if (!operationId) return
const unsubscribe = wallet.mint.subscribeSpendNotes(
operationId,
(_state: SpendNotesState) => {
(_state) => {
setState(_state)
},
(error: string) => {
(error) => {
setState('Error')

@@ -33,8 +33,7 @@ setError(error)

}
}, [operationId, wallet])
}, [operationId])
const spendEcash = useCallback(
async (amountSats: number, reclaimAfter?: number) => {
if (walletStatus !== 'open' || !wallet)
throw new Error('Wallet is not open')
if (walletStatus !== 'open') throw new Error('Wallet is not open')
const response = await wallet.mint.spendNotes(

@@ -41,0 +40,0 @@ amountSats * 1000,

export * from './hooks'
export * from './contexts'
export * from './init'

@@ -1,8 +0,8 @@

import { Wallet, initialize } from '@fedimint/core-web'
import { FedimintWallet } from '@fedimint/core-web'
let wallet: Wallet | undefined
let wallet: FedimintWallet | undefined
export const initFedimintReact = async (lazy: boolean = false) => {
export const initFedimintReact = (lazy: boolean = false) => {
if (!lazy) {
await initialize()
wallet = new FedimintWallet(lazy)
}

@@ -9,0 +9,0 @@

{
"name": "@fedimint/react",
"version": "0.0.0-dev-test3-8f5bd196fa69309b9166fe9e76f076275c1210af",
"version": "0.0.0-main-3d7dee585520f4d811de12cb43416909de43ab54",
"type": "module",

@@ -13,3 +13,3 @@ "files": [

"react": "^18.3.1",
"@fedimint/core-web": "0.0.0-dev-test3-8f5bd196fa69309b9166fe9e76f076275c1210af"
"@fedimint/core-web": "0.0.0-main-3d7dee585520f4d811de12cb43416909de43ab54"
},

@@ -16,0 +16,0 @@ "devDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet