mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-05 00:32:03 +00:00
started on bitcoinconnect update
This commit is contained in:
parent
014d438414
commit
5dd71c3de0
@ -6,20 +6,25 @@ const Button = dynamic(() => import('@getalby/bitcoin-connect-react').then(mod =
|
|||||||
});
|
});
|
||||||
|
|
||||||
let initialized = false;
|
let initialized = false;
|
||||||
|
let bitcoinConnectClient = null;
|
||||||
|
|
||||||
export async function initializeBitcoinConnect() {
|
export async function initializeBitcoinConnect() {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
try {
|
try {
|
||||||
const { init } = await import('@getalby/bitcoin-connect-react');
|
// Import the full module
|
||||||
// Check if custom elements are already registered
|
const bc = await import('@getalby/bitcoin-connect-react');
|
||||||
if (!customElements.get('bc-balance')) {
|
|
||||||
init({
|
// Initialize with the config options
|
||||||
appName: 'PlebDevs',
|
bc.init({
|
||||||
filters: ['nwc'],
|
appName: 'PlebDevs',
|
||||||
showBalance: false,
|
filters: ['nwc'],
|
||||||
});
|
showBalance: false,
|
||||||
initialized = true;
|
});
|
||||||
}
|
|
||||||
|
// Store the client for use in components
|
||||||
|
bitcoinConnectClient = bc.client;
|
||||||
|
initialized = true;
|
||||||
|
console.log('Bitcoin Connect initialized successfully, client:', bitcoinConnectClient);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// If the error is about custom element already being defined, we can ignore it
|
// If the error is about custom element already being defined, we can ignore it
|
||||||
// as it means the component is already initialized
|
// as it means the component is already initialized
|
||||||
@ -27,7 +32,10 @@ export async function initializeBitcoinConnect() {
|
|||||||
console.error('Error initializing Bitcoin Connect:', error);
|
console.error('Error initializing Bitcoin Connect:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Bitcoin Connect already initialized');
|
||||||
}
|
}
|
||||||
|
return bitcoinConnectClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
const BitcoinConnectButton = () => {
|
const BitcoinConnectButton = () => {
|
||||||
|
@ -4,7 +4,7 @@ import { initializeBitcoinConnect } from './BitcoinConnect';
|
|||||||
import { LightningAddress } from '@getalby/lightning-tools';
|
import { LightningAddress } from '@getalby/lightning-tools';
|
||||||
import { useToast } from '@/hooks/useToast';
|
import { useToast } from '@/hooks/useToast';
|
||||||
import { useSession } from 'next-auth/react';
|
import { useSession } from 'next-auth/react';
|
||||||
import { webln, nwc } from '@getalby/sdk';
|
import { webln } from '@getalby/sdk';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { Divider } from 'primereact/divider';
|
import { Divider } from 'primereact/divider';
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
@ -30,6 +30,7 @@ const SubscriptionPaymentButtons = ({
|
|||||||
const [invoice, setInvoice] = useState(null);
|
const [invoice, setInvoice] = useState(null);
|
||||||
const [showRecurringOptions, setShowRecurringOptions] = useState(false);
|
const [showRecurringOptions, setShowRecurringOptions] = useState(false);
|
||||||
const [nwcInput, setNwcInput] = useState('');
|
const [nwcInput, setNwcInput] = useState('');
|
||||||
|
const [bitcoinConnectClient, setBitcoinConnectClient] = useState(null);
|
||||||
const { showToast } = useToast();
|
const { showToast } = useToast();
|
||||||
const { data: session, status } = useSession();
|
const { data: session, status } = useSession();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -38,7 +39,18 @@ const SubscriptionPaymentButtons = ({
|
|||||||
const amount = 50000;
|
const amount = 50000;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
initializeBitcoinConnect();
|
// Initialize Bitcoin Connect as early as possible
|
||||||
|
const initBC = async () => {
|
||||||
|
try {
|
||||||
|
const client = await initializeBitcoinConnect();
|
||||||
|
console.log("Client in SubscriptionPaymentButton:", client);
|
||||||
|
setBitcoinConnectClient(client);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error initializing Bitcoin Connect in SubscriptionPaymentButton:", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
initBC();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -95,11 +107,35 @@ const SubscriptionPaymentButtons = ({
|
|||||||
|
|
||||||
const handleRecurringSubscription = async () => {
|
const handleRecurringSubscription = async () => {
|
||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
const newNwc = nwc.NWCClient.withNewSecret();
|
|
||||||
const yearFromNow = new Date();
|
// Re-initialize if not already initialized
|
||||||
yearFromNow.setFullYear(yearFromNow.getFullYear() + 1);
|
if (!bitcoinConnectClient) {
|
||||||
|
try {
|
||||||
|
console.log("Client not found, reinitializing");
|
||||||
|
const client = await initializeBitcoinConnect();
|
||||||
|
setBitcoinConnectClient(client);
|
||||||
|
|
||||||
|
if (!client) {
|
||||||
|
showToast('error', 'Connection Error', 'Failed to initialize Bitcoin Connect client');
|
||||||
|
setIsProcessing(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error reinitializing Bitcoin Connect:", err);
|
||||||
|
showToast('error', 'Connection Error', 'Failed to initialize Bitcoin Connect client');
|
||||||
|
setIsProcessing(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Import the SDK directly to avoid client issues
|
||||||
|
const { nwc } = await import('@getalby/sdk');
|
||||||
|
const newNwc = nwc.NWCClient.withNewSecret();
|
||||||
|
|
||||||
|
const yearFromNow = new Date();
|
||||||
|
yearFromNow.setFullYear(yearFromNow.getFullYear() + 1);
|
||||||
|
|
||||||
const initNwcOptions = {
|
const initNwcOptions = {
|
||||||
name: 'plebdevs.com',
|
name: 'plebdevs.com',
|
||||||
requestMethods: ['pay_invoice'],
|
requestMethods: ['pay_invoice'],
|
||||||
@ -108,28 +144,34 @@ const SubscriptionPaymentButtons = ({
|
|||||||
budgetRenewal: 'monthly',
|
budgetRenewal: 'monthly',
|
||||||
expiresAt: yearFromNow,
|
expiresAt: yearFromNow,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Initialize NWC directly with the SDK
|
||||||
await newNwc.initNWC(initNwcOptions);
|
await newNwc.initNWC(initNwcOptions);
|
||||||
showToast('info', 'Alby', 'Alby connection window opened.');
|
showToast('info', 'Alby', 'Alby connection window opened.');
|
||||||
|
|
||||||
|
// Get NWC URL directly
|
||||||
const newNWCUrl = newNwc.getNostrWalletConnectUrl();
|
const newNWCUrl = newNwc.getNostrWalletConnectUrl();
|
||||||
|
|
||||||
if (newNWCUrl) {
|
if (newNWCUrl) {
|
||||||
const nwc = new webln.NostrWebLNProvider({
|
const nwcProvider = new webln.NostrWebLNProvider({
|
||||||
nostrWalletConnectUrl: newNWCUrl,
|
nostrWalletConnectUrl: newNWCUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
await nwc.enable();
|
await nwcProvider.enable();
|
||||||
|
|
||||||
const invoice = await fetchInvoice();
|
const invoice = await fetchInvoice();
|
||||||
|
|
||||||
if (!invoice || !invoice.paymentRequest) {
|
if (!invoice || !invoice.paymentRequest) {
|
||||||
showToast('error', 'NWC', `Failed to fetch invoice from ${lnAddress}`);
|
showToast('error', 'NWC', `Failed to fetch invoice from ${lnAddress}`);
|
||||||
|
setIsProcessing(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const paymentResponse = await nwc.sendPayment(invoice.paymentRequest);
|
const paymentResponse = await nwcProvider.sendPayment(invoice.paymentRequest);
|
||||||
|
|
||||||
if (!paymentResponse || !paymentResponse?.preimage) {
|
if (!paymentResponse || !paymentResponse?.preimage) {
|
||||||
showToast('error', 'NWC', 'Payment failed');
|
showToast('error', 'NWC', 'Payment failed');
|
||||||
|
setIsProcessing(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,8 @@ const appConfig = {
|
|||||||
],
|
],
|
||||||
authorPubkeys: [
|
authorPubkeys: [
|
||||||
'f33c8a9617cb15f705fc70cd461cfd6eaf22f9e24c33eabad981648e5ec6f741',
|
'f33c8a9617cb15f705fc70cd461cfd6eaf22f9e24c33eabad981648e5ec6f741',
|
||||||
'c67cd3e1a83daa56cff16f635db2fdb9ed9619300298d4701a58e68e84098345'
|
'c67cd3e1a83daa56cff16f635db2fdb9ed9619300298d4701a58e68e84098345',
|
||||||
|
'6260f29fa75c91aaa292f082e5e87b438d2ab4fdf96af398567b01802ee2fcd4'
|
||||||
],
|
],
|
||||||
customLightningAddresses: [
|
customLightningAddresses: [
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user