-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
124 lines (106 loc) · 4.93 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NWC Tester</title>
</head>
<body>
<h1>NWC Tester</h1>
<form id="nwcForm" action="#" method="GET">
<label for="nwcSecret">NWC Secret:</label>
<input type="text" id="nwcSecret" name="nwc" placeholder="Enter NWC secret" required>
<label>
<input type="checkbox" name="invoice" value="1"> Load Invoice
</label>
<label>
<input type="checkbox" name="info" value="1"> Load Info
</label>
<label>
<input type="checkbox" name="transactions" value="1"> Load Transactions
</label>
<button type="submit">Submit</button>
</form>
<div id="result"></div>
<script type="module">
import { nwc } from "https://esm.sh/@getalby/sdk@3.7.0";
async function generateInvoice(client) {
const invoice = await client.makeInvoice({
amount: 1000,
memo: 'Test invoice'
});
return invoice;
}
async function getNodeInfo(client) {
const info = await client.getInfo();
return info;
}
async function getLatestTransactions(client) {
const transactions = await client.listTransactions({
limit: 5 // Adjust this number to show more or fewer transactions
});
return transactions;
}
async function processNWC(nwcSecret, loadInvoice, loadInfo, loadTransactions) {
if (!nwcSecret) {
document.getElementById('result').innerHTML = 'Error: NWC secret not provided';
return;
}
const resultElement = document.getElementById('result');
resultElement.innerHTML = ''; // Clear previous results
try {
const client = new nwc.NWCClient({nostrWalletConnectUrl: nwcSecret});
if (loadInvoice) {
const invoice = await generateInvoice(client);
resultElement.innerHTML +=
'<h2>Invoice:</h2>' +
'<pre><code id="invoice">' + JSON.stringify(invoice, null, 2) + '</code></pre>';
}
if (loadInfo) {
const info = await getNodeInfo(client);
resultElement.innerHTML +=
'<h2>Info:</h2>' +
'<pre><code id="info">' + JSON.stringify(info, null, 2) + '</code></pre>';
}
if (loadTransactions) {
const transactions = await getLatestTransactions(client);
resultElement.innerHTML +=
'<h2>Latest Transactions:</h2>' +
'<pre><code id="transactions">' + JSON.stringify(transactions, null, 2) + '</code></pre>';
}
} catch (error) {
resultElement.innerHTML = 'Error: ' + error.message;
}
}
function main() {
const urlParams = new URLSearchParams(window.location.search);
const nwcSecret = urlParams.get('nwc') || ''; // Get NWC secret from query params
const loadInvoice = urlParams.get('invoice') === '1'; // Get invoice option
const loadInfo = urlParams.get('info') === '1'; // Get info option
const loadTransactions = urlParams.get('transactions') === '1'; // Get transactions option
// Set the input field if NWC secret is provided in query params
if (nwcSecret) {
document.getElementById('nwcSecret').value = nwcSecret;
}
// Set the checkboxes based on query params
document.querySelector('input[name="invoice"]').checked = loadInvoice;
document.querySelector('input[name="info"]').checked = loadInfo;
document.querySelector('input[name="transactions"]').checked = loadTransactions;
const form = document.getElementById('nwcForm');
form.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
const nwcSecret = document.getElementById('nwcSecret').value;
const loadInvoice = form.querySelector('input[name="invoice"]').checked;
const loadInfo = form.querySelector('input[name="info"]').checked;
const loadTransactions = form.querySelector('input[name="transactions"]').checked;
processNWC(nwcSecret, loadInvoice, loadInfo, loadTransactions);
});
// If query params are present, process them immediately
if (nwcSecret) {
processNWC(nwcSecret, loadInvoice, loadInfo, loadTransactions);
}
}
document.addEventListener('DOMContentLoaded', main);
</script>
</body>
</html>