2024-08-04 17:02:34 -05:00
|
|
|
import React, { createContext, useContext, useEffect, useState } from 'react';
|
2024-08-06 19:52:06 -05:00
|
|
|
import NDK, { NDKNip07Signer } from "@nostr-dev-kit/ndk";
|
2024-08-04 17:02:34 -05:00
|
|
|
|
|
|
|
const NDKContext = createContext(null);
|
|
|
|
|
|
|
|
const relayUrls = [
|
2024-08-13 16:28:25 -05:00
|
|
|
"wss://nos.lol/",
|
|
|
|
"wss://relay.damus.io/",
|
|
|
|
"wss://relay.snort.social/",
|
|
|
|
"wss://relay.nostr.band/",
|
|
|
|
"wss://nostr.mutinywallet.com/",
|
|
|
|
"wss://relay.mutinywallet.com/",
|
|
|
|
"wss://relay.primal.net/"
|
2024-08-04 17:02:34 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
export const NDKProvider = ({ children }) => {
|
2024-08-13 16:28:25 -05:00
|
|
|
const [ndk, setNdk] = useState(null);
|
2024-08-04 17:02:34 -05:00
|
|
|
|
2024-08-13 16:28:25 -05:00
|
|
|
useEffect(() => {
|
|
|
|
const instance = new NDK({ explicitRelayUrls: relayUrls });
|
|
|
|
setNdk(instance);
|
|
|
|
}, []);
|
2024-08-04 17:02:34 -05:00
|
|
|
|
2024-08-13 16:28:25 -05:00
|
|
|
const addSigner = async () => {
|
|
|
|
if (ndk) {
|
|
|
|
const nip07signer = new NDKNip07Signer();
|
|
|
|
await ndk.signer?.user();
|
|
|
|
ndk.signer = nip07signer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<NDKContext.Provider value={{ ndk, addSigner }}>
|
|
|
|
{children}
|
|
|
|
</NDKContext.Provider>
|
|
|
|
);
|
2024-08-04 17:02:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useNDKContext = () => {
|
2024-08-13 16:28:25 -05:00
|
|
|
return useContext(NDKContext);
|
|
|
|
};
|