-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathauth_manual.html
81 lines (75 loc) · 2.49 KB
/
auth_manual.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<button id="connect-button" onclick="window.launchNwc()">Connect NWC</button>
<button id="reset-button" onclick="window.resetNwc()">Reset</button>
<script type="module">
import { webln, nwc } from "https://esm.sh/@getalby/[email protected]"; // jsdelivr.net, skypack.dev also work
import {
generateSecretKey,
getPublicKey,
} from "https://esm.sh/[email protected]"; // jsdelivr.net, skypack.dev also work
import {
bytesToHex,
hexToBytes,
} from "https://esm.sh/@noble/[email protected]/utils"; // jsdelivr.net, skypack.dev also work
const params = new URL(window.location.href).searchParams;
const walletPubkey = params.get("pubkey");
const relayUrl = params.get("relay");
const lud16 = params.get("lud16");
const secret = window.localStorage.getItem("demo_secret");
if (walletPubkey && relayUrl) {
try {
if (!secret) {
throw new Error("No secret saved locally");
}
window.document.getElementById("connect-button").remove();
const nwcClient = new nwc.NWCClient({
secret,
walletPubkey,
relayUrl,
lud16,
});
const weblnProvider = new webln.NostrWebLNProvider({
client: nwcClient,
});
await weblnProvider.enable();
const result = await weblnProvider.getInfo();
alert("Info response: " + JSON.stringify(result) + " lud16: " + lud16);
} catch (error) {
console.error(error);
alert("Something went wrong: " + error);
}
} else {
window.document.getElementById("reset-button").remove();
}
window.resetNwc = async () => {
window.localStorage.removeItem("demo_secret");
window.location.href = window.origin;
};
window.launchNwc = async () => {
try {
if (!window.origin.startsWith("http")) {
alert(
"Please use a webserver from this directory e.g. python3 -m http.server",
);
}
const secret = bytesToHex(generateSecretKey());
const pubkey = getPublicKey(hexToBytes(secret));
window.localStorage.setItem("demo_secret", secret);
const authorizationBasePath = prompt(
"Auth URL",
"https://my.albyhub.com/apps/new",
);
const authUrl = await nwc.NWCClient.getAuthorizationUrl(
authorizationBasePath,
{
name: "Deeplink " + Date.now(),
returnTo: window.location.href,
},
pubkey,
);
window.location = authUrl;
} catch (error) {
console.error(error);
alert("Something went wrong: " + error);
}
};
</script>