Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions examples/nwc/auth.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<script type="module">
import { webln } from "https://esm.sh/@getalby/sdk@5.1.0"; // jsdelivr.net, skypack.dev also work
import { NostrWebLNProvider } from "https://esm.sh/@getalby/sdk@7.0.0"; // jsdelivr.net, skypack.dev also work
window.launchNwc = async () => {
try {
const authUrl = prompt("Auth URL", "https://my.albyhub.com/apps/new");
const nwcWebln = await webln.NostrWebLNProvider.fromAuthorizationUrl(
authUrl,
{
name: "Deeplink " + Date.now(),
},
);
const nwcWebln = await NostrWebLNProvider.fromAuthorizationUrl(authUrl, {
name: "Deeplink " + Date.now(),
});
// connect to the relay
await nwcWebln.enable();
const result = await nwcWebln.getInfo();
Expand Down
16 changes: 10 additions & 6 deletions examples/nwc/auth_manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<button id="reset-button" onclick="window.resetNwc()">Reset</button>

<script type="module">
import {
NostrWebLNProvider,
NWCClient,
} from "https://esm.sh/@getalby/[email protected]"; // jsdelivr.net, skypack.dev also work
import { webln, nwc } from "https://esm.sh/@getalby/[email protected]"; // jsdelivr.net, skypack.dev also work
import {
generateSecretKey,
Expand All @@ -14,23 +18,23 @@

const params = new URL(window.location.href).searchParams;
const walletPubkey = params.get("pubkey");
const relayUrl = params.get("relay");
const relayUrls = params.getAll("relay");
const lud16 = params.get("lud16");
const secret = window.localStorage.getItem("demo_secret");

if (walletPubkey && relayUrl) {
if (walletPubkey && relayUrls) {
try {
if (!secret) {
throw new Error("No secret saved locally");
}
window.document.getElementById("connect-button").remove();
const nwcClient = new nwc.NWCClient({
const nwcClient = new NWCClient({
secret,
walletPubkey,
relayUrl,
relayUrls,
lud16,
});
const weblnProvider = new webln.NostrWebLNProvider({
const weblnProvider = new NostrWebLNProvider({
client: nwcClient,
});
await weblnProvider.enable();
Expand Down Expand Up @@ -64,7 +68,7 @@
"Auth URL",
"https://my.albyhub.com/apps/new",
);
const authUrl = await nwc.NWCClient.getAuthorizationUrl(
const authUrl = await NWCClient.getAuthorizationUrl(
authorizationBasePath,
{
name: "Deeplink " + Date.now(),
Expand Down
4 changes: 2 additions & 2 deletions examples/nwc/client/auth.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script type="module">
import { nwc } from "https://esm.sh/@getalby/sdk@5.1.0"; // jsdelivr.net, skypack.dev also work
import { NWCClient } from "https://esm.sh/@getalby/sdk@7.0.0"; // jsdelivr.net, skypack.dev also work

window.launchNwc = async () => {
try {
const authUrl = prompt("Auth URL", "https://my.albyhub.com/apps/new");
const nwcClient = await nwc.NWCClient.fromAuthorizationUrl(authUrl, {
const nwcClient = await NWCClient.fromAuthorizationUrl(authUrl, {
name: "Deeplink " + Date.now(),
});
const result = await nwcClient.getInfo();
Expand Down
12 changes: 6 additions & 6 deletions examples/nwc/client/auth_manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<button id="reset-button" onclick="window.resetNwc()">Reset</button>

<script type="module">
import { nwc } from "https://esm.sh/@getalby/sdk@5.1.0"; // jsdelivr.net, skypack.dev also work
import { NWCClient } from "https://esm.sh/@getalby/sdk@7.0.0"; // jsdelivr.net, skypack.dev also work
import {
generateSecretKey,
getPublicKey,
Expand All @@ -14,20 +14,20 @@

const params = new URL(window.location.href).searchParams;
const walletPubkey = params.get("pubkey");
const relayUrl = params.get("relay");
const relayUrls = params.getAll("relay");
const lud16 = params.get("lud16");
const secret = window.localStorage.getItem("demo_secret");

if (walletPubkey && relayUrl) {
if (walletPubkey && relayUrls) {
try {
if (!secret) {
throw new Error("No secret saved locally");
}
window.document.getElementById("connect-button").remove();
const nwcClient = new nwc.NWCClient({
const nwcClient = new NWCClient({
secret,
walletPubkey,
relayUrl,
relayUrls,
lud16,
});
const result = await nwcClient.getInfo();
Expand Down Expand Up @@ -60,7 +60,7 @@
"Auth URL",
"https://my.albyhub.com/apps/new",
);
const authUrl = await nwc.NWCClient.getAuthorizationUrl(
const authUrl = await NWCClient.getAuthorizationUrl(
authorizationBasePath,
{
name: "Deeplink " + Date.now(),
Expand Down
12 changes: 7 additions & 5 deletions examples/nwc/client/nwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import { NWAClient } from "@getalby/sdk/nwc";

const rl = readline.createInterface({ input, output });

const DEFAULT_RELAY_URL = "wss://relay.getalby.com/v1";
//const DEFAULT_RELAY_URLs = "ws://localhost:7447/v1,ws://localhost:7448/v1";
const DEFAULT_RELAY_URLs = "wss://relay.getalby.com/v1";

const relayUrl =
(await rl.question(`Relay URL (${DEFAULT_RELAY_URL}): `)) ||
DEFAULT_RELAY_URL;
const relayUrls =
(await rl.question(
`Relay URLs, comma separated (${DEFAULT_RELAY_URLs}): `,
)) || DEFAULT_RELAY_URLs;
rl.close();

const nwaClient = new NWAClient({
relayUrl,
relayUrls: relayUrls.split(","),
requestMethods: ["get_info"],
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getalby/sdk",
"version": "6.0.2",
"version": "7.0.0",
"description": "The SDK to integrate with Nostr Wallet Connect and the Alby API",
"type": "module",
"repository": "https://github.com/getAlby/js-sdk.git",
Expand Down
19 changes: 11 additions & 8 deletions src/nwc/NWAClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ describe("NWA URI", () => {
const appPubkey = getPublicKey(hexToBytes(appSecretKey));

const nwaClient = new NWAClient({
relayUrl: "wss://relay.getalby.com/v1",
relayUrls: ["wss://relay.getalby.com/v1", "wss://relay2.getalby.com/v1"],
appSecretKey,
requestMethods: ["get_info"],
});

expect(nwaClient.connectionUri).toEqual(
`nostr+walletauth://${appPubkey}?relay=${encodeURIComponent(nwaClient.options.relayUrl)}&request_methods=get_info`,
`nostr+walletauth://${appPubkey}?request_methods=get_info&relay=${encodeURIComponent(nwaClient.options.relayUrls[0])}&relay=${encodeURIComponent(nwaClient.options.relayUrls[1])}`,
);
});
test("constructs correct connection URI", () => {
Expand All @@ -25,7 +25,7 @@ describe("NWA URI", () => {
const nwaClient = new NWAClient({
name: "App Name",
icon: "https://example.com/image.png",
relayUrl: "wss://relay.getalby.com/v1",
relayUrls: ["wss://relay.getalby.com/v1"],
requestMethods: ["get_info", "pay_invoice"],
notificationTypes: ["payment_received", "payment_sent"],
expiresAt,
Expand All @@ -37,18 +37,18 @@ describe("NWA URI", () => {
});

expect(nwaClient.connectionUri).toEqual(
`nostr+walletauth://${nwaClient.options.appPubkey}?relay=wss%3A%2F%2Frelay.getalby.com%2Fv1&request_methods=get_info%20pay_invoice&name=App%20Name&icon=https%3A%2F%2Fexample.com%2Fimage.png&return_to=https%3A%2F%2Fexample.com&notification_types=payment_received%20payment_sent&max_amount=${maxAmount}&budget_renewal=monthly&expires_at=${expiresAt}&isolated=true&metadata=%7B%22message%22%3A%22hello%20world%22%7D`,
`nostr+walletauth://${nwaClient.options.appPubkey}?request_methods=get_info%20pay_invoice&name=App%20Name&icon=https%3A%2F%2Fexample.com%2Fimage.png&return_to=https%3A%2F%2Fexample.com&notification_types=payment_received%20payment_sent&max_amount=${maxAmount}&budget_renewal=monthly&expires_at=${expiresAt}&isolated=true&metadata=%7B%22message%22%3A%22hello%20world%22%7D&relay=wss%3A%2F%2Frelay.getalby.com%2Fv1`,
);
});

test("constructs correct connection URI for specific app", () => {
const nwaClient = new NWAClient({
relayUrl: "wss://relay.getalby.com/v1",
relayUrls: ["wss://relay.getalby.com/v1"],
requestMethods: ["get_info"],
});

expect(nwaClient.getConnectionUri("alby")).toEqual(
`nostr+walletauth+alby://${nwaClient.options.appPubkey}?relay=wss%3A%2F%2Frelay.getalby.com%2Fv1&request_methods=get_info`,
`nostr+walletauth+alby://${nwaClient.options.appPubkey}?request_methods=get_info&relay=wss%3A%2F%2Frelay.getalby.com%2Fv1`,
);
});

Expand All @@ -60,13 +60,16 @@ describe("NWA URI", () => {
]) {
test(`parses connection URI (${scheme})`, () => {
const nwaOptions = NWAClient.parseWalletAuthUrl(
`${scheme}e73575d76c731102aefd4eb6fb0ddfaaf335eabe60255a22e6ca5e7074eb4992?relay=wss%3A%2F%2Frelay.getalby.com%2Fv1&request_methods=get_info%20pay_invoice&name=App%20Name&icon=https%3A%2F%2Fexample.com%2Fimage.png&return_to=https%3A%2F%2Fexample.com&notification_types=payment_received%20payment_sent&max_amount=1000000&budget_renewal=monthly&expires_at=1740470142968&isolated=true&metadata=%7B%22message%22%3A%22hello%20world%22%7D`,
`${scheme}e73575d76c731102aefd4eb6fb0ddfaaf335eabe60255a22e6ca5e7074eb4992?relay=wss%3A%2F%2Frelay.getalby.com%2Fv1&relay=wss%3A%2F%2Frelay2.getalby.com%2Fv1&request_methods=get_info%20pay_invoice&name=App%20Name&icon=https%3A%2F%2Fexample.com%2Fimage.png&return_to=https%3A%2F%2Fexample.com&notification_types=payment_received%20payment_sent&max_amount=1000000&budget_renewal=monthly&expires_at=1740470142968&isolated=true&metadata=%7B%22message%22%3A%22hello%20world%22%7D`,
);

expect(nwaOptions.appPubkey).toEqual(
"e73575d76c731102aefd4eb6fb0ddfaaf335eabe60255a22e6ca5e7074eb4992",
);
expect(nwaOptions.relayUrl).toEqual("wss://relay.getalby.com/v1");
expect(nwaOptions.relayUrls).toEqual([
"wss://relay.getalby.com/v1",
"wss://relay2.getalby.com/v1",
]);
expect(nwaOptions.requestMethods).toEqual([
"get_info",
"pay_invoice",
Expand Down
Loading