@defdone/annon 0.1.0-alpha.0 → 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,3 @@
1
- import { setupWalletSelector } from '@near-wallet-selector/core';
2
- import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet';
3
1
  import { Buffer } from 'buffer';
4
2
  import { signInAccountOnly } from '../wallet/account-only.js';
5
3
  import { evmWallet } from '../wallet/wallet.js';
@@ -8,6 +6,27 @@ const pair = document.getElementById('pair');
8
6
  const display = document.getElementById('action');
9
7
  const status = document.getElementById('status');
10
8
  const approve = document.getElementById('approve');
9
+ const nearChoice = document.getElementById('near-wallet');
10
+ const hotChoice = document.getElementById('near-wallet-hot');
11
+ /** Signing-request chains the page can bind a NEAR wallet to. Anything else is refused. */
12
+ const NEAR_NETWORKS = new Map([
13
+ ['near:mainnet', 'mainnet'],
14
+ ['near:testnet', 'testnet'],
15
+ ]);
16
+ /** The selector's only RPC per network. The page CSP allows exactly these (browser-login.ts). */
17
+ const NEAR_RPC = {
18
+ mainnet: 'https://free.rpc.fastnear.com',
19
+ testnet: 'https://test.rpc.fastnear.com',
20
+ };
21
+ /**
22
+ * NEAR wallets offered on this page and the networks each is bound to. MyNearWallet is retired
23
+ * and no longer connects to apps. HOT's selector module sends no network with its requests, so
24
+ * it is offered for mainnet only and never loaded for another network.
25
+ */
26
+ const NEAR_WALLETS = new Map([
27
+ ['meteor-wallet', { name: 'Meteor Wallet', networks: ['mainnet', 'testnet'] }],
28
+ ['hot-wallet', { name: 'HOT Wallet', networks: ['mainnet'] }],
29
+ ]);
11
30
  const fragment = new URLSearchParams(location.hash.slice(1));
12
31
  const redirected = location.pathname === '/callback';
13
32
  let capability = redirected ? '' : location.hash.slice(1);
@@ -69,6 +88,9 @@ async function poll() {
69
88
  current = r.action ?? undefined;
70
89
  display.textContent = current ? JSON.stringify(current, null, 2) : 'Waiting for the CLI…';
71
90
  approve.disabled = !current || used.has(current.id);
91
+ if (hotChoice)
92
+ hotChoice.disabled =
93
+ current?.chain?.startsWith('near:') === true && current.chain !== 'near:mainnet';
72
94
  setTimeout(() => {
73
95
  void poll();
74
96
  }, 1000);
@@ -108,12 +130,29 @@ approve.onclick = async () => {
108
130
  : { signature: await wallet.signTypedData(q.payload) };
109
131
  }
110
132
  else {
111
- const networkId = q.chain === 'near:mainnet' ? 'mainnet' : 'testnet';
133
+ const networkId = NEAR_NETWORKS.get(q.chain);
134
+ if (!networkId)
135
+ throw new Error(`Browser NEAR wallets support near:mainnet and near:testnet, not ${q.chain}.`);
136
+ const id = nearChoice?.value || 'meteor-wallet';
137
+ const offered = NEAR_WALLETS.get(id);
138
+ if (!offered)
139
+ throw new Error('Unknown NEAR wallet');
140
+ if (!offered.networks.includes(networkId))
141
+ throw new Error(`${offered.name} is offered for near:mainnet only. Pick Meteor Wallet.`);
142
+ // Only the chosen wallet is loaded, on first use: the page starts without wallet code
143
+ // touching storage or listeners, and a refused wallet never runs.
144
+ const [{ setupWalletSelector }, module] = await Promise.all([
145
+ import('@near-wallet-selector/core'),
146
+ id === 'hot-wallet'
147
+ ? import('@near-wallet-selector/hot-wallet').then((m) => m.setupHotWallet())
148
+ : import('@near-wallet-selector/meteor-wallet').then((m) => m.setupMeteorWallet()),
149
+ ]);
112
150
  const selector = await setupWalletSelector({
113
151
  network: networkId,
114
- modules: [setupMyNearWallet()],
152
+ fallbackRpcUrls: [NEAR_RPC[networkId]],
153
+ modules: [module],
115
154
  });
116
- const wallet = await selector.wallet('my-near-wallet');
155
+ const wallet = await selector.wallet(id);
117
156
  const accounts = await signInAccountOnly(wallet);
118
157
  const account = q.account || accounts[0]?.accountId;
119
158
  if (!account || !accounts.some((a) => a.accountId === account))
@@ -130,7 +169,7 @@ approve.onclick = async () => {
130
169
  state,
131
170
  });
132
171
  if (!signed)
133
- return; // redirect wallet: /callback forwards fragment
172
+ return; // redirect wallets: /callback forwards the fragment
134
173
  callbacks.delete(state);
135
174
  result = signed;
136
175
  }