mint.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>cdk-wasm Mint Example</title>
  7. <style>
  8. * { box-sizing: border-box; margin: 0; padding: 0; }
  9. body {
  10. font-family: system-ui, -apple-system, sans-serif;
  11. background: #0f0f0f;
  12. color: #e0e0e0;
  13. padding: 2rem;
  14. max-width: 720px;
  15. margin: 0 auto;
  16. }
  17. h1 { color: #f7931a; margin-bottom: 0.5rem; }
  18. p.subtitle { color: #888; margin-bottom: 1.5rem; font-size: 0.9rem; }
  19. .card {
  20. background: #1a1a1a;
  21. border: 1px solid #333;
  22. border-radius: 8px;
  23. padding: 1.25rem;
  24. margin-bottom: 1rem;
  25. }
  26. .card h2 {
  27. font-size: 0.85rem;
  28. text-transform: uppercase;
  29. letter-spacing: 0.05em;
  30. color: #888;
  31. margin-bottom: 0.75rem;
  32. }
  33. .status {
  34. padding: 0.5rem 0.75rem;
  35. border-radius: 4px;
  36. font-size: 0.9rem;
  37. margin-bottom: 0.5rem;
  38. }
  39. .status.ok { background: #0d2818; color: #4ade80; }
  40. .status.err { background: #2d0f0f; color: #f87171; }
  41. .status.info { background: #1a1a2e; color: #93c5fd; }
  42. .status.loading { background: #1a1a1a; color: #fbbf24; }
  43. .detail { color: #aaa; font-size: 0.9rem; line-height: 1.6; }
  44. .detail strong { color: #e0e0e0; }
  45. pre {
  46. background: #111;
  47. padding: 0.75rem;
  48. border-radius: 4px;
  49. overflow-x: auto;
  50. font-size: 0.8rem;
  51. color: #aaa;
  52. margin-top: 0.5rem;
  53. word-break: break-all;
  54. white-space: pre-wrap;
  55. }
  56. input, button {
  57. font-family: inherit;
  58. font-size: 0.9rem;
  59. padding: 0.5rem 0.75rem;
  60. border-radius: 4px;
  61. border: 1px solid #444;
  62. background: #222;
  63. color: #e0e0e0;
  64. }
  65. input { width: 100%; margin-bottom: 0.5rem; }
  66. button {
  67. background: #f7931a;
  68. color: #000;
  69. border: none;
  70. cursor: pointer;
  71. font-weight: 600;
  72. }
  73. button:disabled { opacity: 0.4; cursor: not-allowed; }
  74. button:hover:not(:disabled) { background: #ffa940; }
  75. .row { display: flex; gap: 0.5rem; align-items: center; margin-bottom: 0.5rem; }
  76. .row input { margin-bottom: 0; }
  77. #log {
  78. max-height: 300px;
  79. overflow-y: auto;
  80. font-size: 0.8rem;
  81. color: #aaa;
  82. background: #111;
  83. padding: 0.75rem;
  84. border-radius: 4px;
  85. margin-top: 0.5rem;
  86. }
  87. #log div { margin-bottom: 0.25rem; }
  88. #log .ts { color: #666; }
  89. #log .event { color: #93c5fd; }
  90. #log .ok { color: #4ade80; }
  91. #log .err { color: #f87171; }
  92. </style>
  93. </head>
  94. <body>
  95. <h1>Mint &amp; Subscribe</h1>
  96. <p class="subtitle">Create a mint quote, pay the invoice, and watch the status update in real-time.</p>
  97. <div class="card">
  98. <h2>Setup</h2>
  99. <div id="setup-status" class="status loading">Loading WASM...</div>
  100. <div class="detail">
  101. <div class="row">
  102. <input id="mint-url" type="text" value="https://testnut.cashu.space" placeholder="Mint URL">
  103. </div>
  104. <div class="row">
  105. <input id="amount" type="number" value="21" placeholder="Amount (sats)" min="1">
  106. <button id="btn-mint" disabled>Create Mint Quote</button>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="card">
  111. <h2>Mint Quote</h2>
  112. <div id="quote-status" class="status info">Waiting...</div>
  113. <pre id="quote-detail" style="display:none"></pre>
  114. </div>
  115. <div class="card">
  116. <h2>Invoice</h2>
  117. <div id="invoice-status" class="status info">Waiting for quote...</div>
  118. <pre id="invoice" style="display:none"></pre>
  119. </div>
  120. <div class="card">
  121. <h2>Subscription Log</h2>
  122. <div id="sub-status" class="status info">Waiting for quote...</div>
  123. <div id="log"></div>
  124. </div>
  125. <div class="card">
  126. <h2>Minted Tokens</h2>
  127. <div id="mint-status" class="status info">Waiting for payment...</div>
  128. <pre id="mint-result" style="display:none"></pre>
  129. </div>
  130. <div class="card">
  131. <h2>Balance</h2>
  132. <div id="balance-status" class="status info">Waiting...</div>
  133. </div>
  134. <script type="module">
  135. import init, { initDefaultLogging, generateMnemonic, Wallet } from '../pkg/cdk_wasm.js';
  136. // ---- helpers ----------------------------------------------------------
  137. const $ = id => document.getElementById(id);
  138. function setStatus(id, cls, text) {
  139. const el = $(id);
  140. el.className = 'status ' + cls;
  141. el.textContent = text;
  142. }
  143. function log(msg, cls = '') {
  144. const el = $('log');
  145. const ts = new Date().toLocaleTimeString();
  146. el.innerHTML += `<div><span class="ts">[${ts}]</span> <span class="${cls}">${msg}</span></div>`;
  147. el.scrollTop = el.scrollHeight;
  148. }
  149. // ---- init WASM --------------------------------------------------------
  150. try {
  151. await init();
  152. setStatus('setup-status', 'ok', 'WASM loaded');
  153. } catch (e) {
  154. setStatus('setup-status', 'err', 'Failed: ' + e.message);
  155. throw e;
  156. }
  157. try { initDefaultLogging(); } catch (_) {}
  158. // ---- state ------------------------------------------------------------
  159. let wallet = null;
  160. let currentQuote = null;
  161. let polling = false;
  162. // ---- create quote -----------------------------------------------------
  163. $('btn-mint').disabled = false;
  164. $('btn-mint').addEventListener('click', async () => {
  165. const mintUrl = $('mint-url').value.trim();
  166. const amount = parseInt($('amount').value, 10);
  167. if (!mintUrl || !amount || amount <= 0) return;
  168. $('btn-mint').disabled = true;
  169. polling = false; // stop any previous poll
  170. try {
  171. // Create wallet
  172. setStatus('quote-status', 'loading', 'Creating wallet...');
  173. const mnemonic = generateMnemonic();
  174. wallet = new Wallet(mintUrl, 'Sat', mnemonic, null, 3);
  175. log('Wallet created for ' + mintUrl);
  176. // Create mint quote
  177. setStatus('quote-status', 'loading', 'Requesting mint quote...');
  178. currentQuote = await wallet.mintQuote('Bolt11', { value: amount }, null, null);
  179. setStatus('quote-status', 'ok', `Quote ${currentQuote.id} created`);
  180. $('quote-detail').style.display = 'block';
  181. $('quote-detail').textContent = JSON.stringify(currentQuote, null, 2);
  182. log(`Quote created: ${currentQuote.id}`, 'event');
  183. // Show invoice
  184. $('invoice').style.display = 'block';
  185. $('invoice').textContent = currentQuote.request;
  186. setStatus('invoice-status', 'ok', 'Pay this Lightning invoice to continue');
  187. // Start polling
  188. startPolling(currentQuote.id);
  189. } catch (e) {
  190. setStatus('quote-status', 'err', 'Error: ' + (e.message || e));
  191. log('Error: ' + (e.message || e), 'err');
  192. $('btn-mint').disabled = false;
  193. }
  194. });
  195. // ---- poll for payment -------------------------------------------------
  196. async function startPolling(quoteId) {
  197. polling = true;
  198. setStatus('sub-status', 'loading', 'Polling quote status every 3s...');
  199. log('Started polling for quote ' + quoteId, 'event');
  200. while (polling) {
  201. await new Promise(r => setTimeout(r, 3000));
  202. if (!polling) break;
  203. try {
  204. const updated = await wallet.refreshMintQuoteStatus(quoteId);
  205. const state = updated.state;
  206. log(`Status: ${state}`);
  207. if (state === 'Paid' || state === 'Issued') {
  208. polling = false;
  209. setStatus('sub-status', 'ok', `Quote is ${state}!`);
  210. log(`Invoice paid! State: ${state}`, 'ok');
  211. await mintTokens(quoteId);
  212. return;
  213. }
  214. } catch (e) {
  215. log('Poll error: ' + (e.message || e), 'err');
  216. }
  217. }
  218. }
  219. // ---- mint tokens ------------------------------------------------------
  220. async function mintTokens(quoteId) {
  221. try {
  222. setStatus('mint-status', 'loading', 'Minting tokens...');
  223. log('Minting tokens...', 'event');
  224. const proofs = await wallet.mint(quoteId, 'Default', null);
  225. setStatus('mint-status', 'ok', `Minted ${Array.isArray(proofs) ? proofs.length : '?'} proofs`);
  226. $('mint-result').style.display = 'block';
  227. $('mint-result').textContent = JSON.stringify(proofs, null, 2);
  228. log(`Minted ${Array.isArray(proofs) ? proofs.length : '?'} proofs`, 'ok');
  229. // Show balance
  230. const balance = await wallet.totalBalance();
  231. setStatus('balance-status', 'ok', `Balance: ${balance.value} sat`);
  232. log(`Balance: ${balance.value} sat`, 'ok');
  233. } catch (e) {
  234. setStatus('mint-status', 'err', 'Mint error: ' + (e.message || e));
  235. log('Mint error: ' + (e.message || e), 'err');
  236. }
  237. $('btn-mint').disabled = false;
  238. }
  239. </script>
  240. </body>
  241. </html>