Skip to content

Commit

Permalink
nwc keysend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Apr 18, 2024
1 parent e26aae8 commit 1a8431a
Showing 1 changed file with 73 additions and 3 deletions.
76 changes: 73 additions & 3 deletions src/components/NWC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,12 @@ async function publishMultiInvoiceRequest(invoices: string[], nwc: NWCInfo) {
event.tags = [["p", nwc.npubHex]];

await event.encrypt(undefined, signer);

await event.sign();
console.log("publishing zap request", event.rawEvent());

console.log("publishing multi zap request", event.rawEvent());
await event.publish();
}


async function fetchBolt11(): Promise<{ bolt11: string }> {
const res = await fetch(`${FAUCET_API_URL}/api/bolt11`, {
method: "POST",
Expand Down Expand Up @@ -153,6 +152,71 @@ function Zapper(props: { nwc: NWCInfo }) {
}
setLoading(false);
}

async function handleKeysend() {
setLoading(true);

const signer = new NDKPrivateKeySigner(props.nwc.secret);
const ndk = new NDK({ explicitRelayUrls: [props.nwc.relay], signer });

console.log("connecting to ndk");
await ndk.connect();

const event = new NDKEvent(ndk);
event.kind = 23194;
event.content = JSON.stringify({
method: "pay_keysend",
params: {
amount: 42 * 1000,
pubkey: "02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b"
},
});
event.tags = [["p", props.nwc.npubHex]];

await event.encrypt(undefined, signer);
await event.sign();
console.log("publishing keysend request", event.rawEvent());
await event.publish();

setLoading(false);
}

async function handleMultiKeysend() {
setLoading(true);

const signer = new NDKPrivateKeySigner(props.nwc.secret);
const ndk = new NDK({ explicitRelayUrls: [props.nwc.relay], signer });

console.log("connecting to ndk");
await ndk.connect();

let keysendParams = [];
for (let i = 0; i < 3; i++) {
let keysendParam = {
id: String(Math.floor(Math.random() * 10000000)),
pubkey: "02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b",
amount: 42 * 1000
}
keysendParams.push(keysendParam)
}

const event = new NDKEvent(ndk);
event.kind = 23194;
event.content = JSON.stringify({
method: "multi_pay_keysend",
params: {
keysends: keysendParams
},
});
event.tags = [["p", props.nwc.npubHex]];

await event.encrypt(undefined, signer);
await event.sign();
console.log("publishing multi keysend request", event.rawEvent());
await event.publish();

setLoading(false);
}

return (
<div class="rounded-xl p-4 flex flex-col items-center gap-2 bg-[rgba(0,0,0,0.5)] drop-shadow-blue-glow">
Expand All @@ -172,6 +236,12 @@ function Zapper(props: { nwc: NWCInfo }) {
<button class={GREEN_BUTTON} onClick={handleMultiZap}>
{loading() ? "..." : "Send Multiple Zap Requests"}
</button>
<button class={GREEN_BUTTON} onClick={handleKeysend}>
{loading() ? "..." : "Send Keysend Request"}
</button>
<button class={GREEN_BUTTON} onClick={handleMultiKeysend}>
{loading() ? "..." : "Send Multiple Keysend Requests"}
</button>
</Match>
</Switch>
</div>
Expand Down

0 comments on commit 1a8431a

Please sign in to comment.