zcashname-sdk 0.5.1 → 0.6.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.
package/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Zcash Name System (ZNS) SDK
1
+ # ZNS SDK
2
2
 
3
- TypeScript SDK for interacting with the Zcash Name System a decentralized naming system built on Zcash.
4
-
5
- ## Install
3
+ TypeScript SDK for the Zcash Name System. Register names, manage listings, and resolve addresses on Zcash.
6
4
 
7
5
  ```bash
8
6
  npm install zcashname-sdk
@@ -13,81 +11,234 @@ npm install zcashname-sdk
13
11
  ```ts
14
12
  import { ZNS } from "zcashname-sdk";
15
13
 
16
- const zns = await ZNS.create();
14
+ // Create client (synchronous, no network calls)
15
+ const zns = new ZNS();
17
16
 
18
- const name = "alice";
19
- const resolved = await zns.resolve(name);
20
- if (resolved === null) {
21
- console.log(`${name} is available`);
22
- } else {
23
- console.log(`${name} resolves to ${resolved.address}`);
24
- }
17
+ // Resolve a name to get its Zcash address
18
+ const reg = await zns.resolveName("alice");
19
+ if (reg) console.log(`alice.zcash ${reg.address}`);
20
+
21
+ // Check availability
22
+ const available = await zns.isAvailable("bob");
23
+
24
+ // Verify server identity (optional but recommended)
25
+ await zns.verify();
25
26
  ```
26
27
 
27
- ## API
28
+ ## Actions Flow
28
29
 
29
- ### `ZNS.create(options?)`
30
- Create a new ZNS instance connected to the default testnet endpoint.
30
+ Every state-changing action follows the same pattern: **prepare → sign → complete**.
31
31
 
32
32
  ```ts
33
- const zns = await ZNS.create();
34
- const znsMainnet = await ZNS.create({ url: "https://light.zcash.me/zns-mainnet" });
33
+ import * as ed25519 from "@noble/ed25519";
34
+
35
+ // 1. Fetch status to get current pricing
36
+ const status = await zns.status();
37
+ const cost = zns.claimCost(5, status.pricing);
38
+
39
+ // 2. Prepare the action
40
+ const claim = zns.prepareClaim("myname", "u1qqlzrf9...", status.address, cost);
41
+
42
+ // 3. Sign the payload with your Ed25519 private key
43
+ const privateKey = ed25519.utils.randomPrivateKey();
44
+ const signature = await ed25519.signAsync(
45
+ new TextEncoder().encode(claim.payload),
46
+ privateKey
47
+ );
48
+
49
+ // 4. Complete and get the payment URI
50
+ const { memo, uri } = claim.complete(
51
+ Buffer.from(signature).toString("base64"),
52
+ Buffer.from(ed25519.getPublicKey(privateKey)).toString("base64")
53
+ );
54
+
55
+ // 5. Send the transaction using your Zcash wallet
56
+ console.log("Payment URI:", uri);
57
+ console.log("Memo:", memo);
35
58
  ```
36
59
 
37
- ### `zns.resolve(query)`
38
- Resolve a name to its registered address. Returns `null` if the name is available.
60
+ ## Available Actions
39
61
 
40
- ```ts
41
- const result = await zns.resolve("alice");
42
- ```
62
+ | Action | Description |
63
+ |--------|-------------|
64
+ | `prepareClaim(name, address, registryAddress, cost)` | Register a new name |
65
+ | `prepareList(name, price, nonce, registryAddress)` | List name for sale |
66
+ | `prepareDelist(name, nonce, registryAddress)` | Remove listing |
67
+ | `prepareUpdate(name, newAddress, nonce, registryAddress)` | Change address |
68
+ | `prepareBuy(name, buyerAddress, registryAddress)` | Buy a listed name |
69
+ | `prepareRelease(name, nonce, registryAddress)` | Release name (burn) |
70
+ | `prepareSetPrice(prices, nonce, registryAddress)` | Admin: set pricing tiers |
43
71
 
44
- ### `zns.isAvailable(name)`
45
- Check if a name is available for registration.
72
+ ## Reading Data
46
73
 
47
74
  ```ts
75
+ // Resolve name → address
76
+ const reg = await zns.resolveName("alice");
77
+
78
+ // Resolve address → names (reverse lookup)
79
+ const names = await zns.resolveAddress("u1qqlzrf9...");
80
+
81
+ // Check availability
48
82
  const available = await zns.isAvailable("bob");
83
+
84
+ // Get all listings
85
+ const listings = await zns.listings();
86
+
87
+ // Query events
88
+ const { events, total } = await zns.events({ action: "CLAIM", limit: 10 });
89
+
90
+ // Get current pricing from status
91
+ const status = await zns.status();
92
+ const cost = zns.claimCost(5, status.pricing); // cost for 5-char name
49
93
  ```
50
94
 
51
- ### `zns.listings()`
52
- Get all names currently listed for sale.
95
+ ## Server Verification (Optional)
96
+
97
+ For security-sensitive applications, verify the server identity:
53
98
 
54
99
  ```ts
55
- const listings = await zns.listings();
100
+ const zns = new ZNS({ url: "https://your-indexer.com/zns" });
101
+
102
+ // Verify this is a legitimate ZNS instance
103
+ await zns.verify();
104
+ console.log("Verified:", zns.verified); // true
105
+
106
+ // Now proceed with operations
107
+ const status = await zns.status();
56
108
  ```
57
109
 
58
- ### `zns.events(filter?)`
59
- Query blockchain events (claims, buys, lists, delists, updates, releases).
110
+ ## Validation & Utilities
60
111
 
61
112
  ```ts
62
- const { events } = await zns.events({ action: "CLAIM", limit: 50 });
113
+ // Validate a Zcash Unified Address
114
+ const valid = zns.isValidUnifiedAddress("u1qqlzrf9..."); // true/false
115
+
116
+ // Validate a ZNS name
117
+ const validName = zns.isValidName("alice"); // true
118
+ const invalid = zns.isValidName("Alice"); // false (uppercase)
119
+
120
+ // Parse ZIP-321 URI
121
+ const parts = zns.parseZip321Uri("zcash:u1...?amount=1&memo=...");
122
+ // { address: "u1...", amount: "1", memoRaw: "...", memoDecoded: "..." }
63
123
  ```
64
124
 
65
- ### Action Helpers
125
+ ## Signature Verification
66
126
 
67
- The SDK provides prepare/complete pairs for all ZNS actions:
127
+ ```ts
128
+ // Get admin pubkey from status
129
+ const status = await zns.status();
68
130
 
69
- - `zns.prepareClaim(name, address)` / `zns.completeClaim(...)` — claim a name
70
- - `zns.prepareList(name, price, nonce)` / `zns.completeList(...)` list a name for sale
71
- - `zns.prepareDelist(name, nonce)` / `zns.completeDelist(...)` — delist a name
72
- - `zns.prepareUpdate(name, newAddress, nonce)` / `zns.completeUpdate(...)` — update a name's address
73
- - `zns.prepareBuy(name, buyerAddress)` / `zns.completeBuy(...)` — buy a name
74
- - `zns.prepareRelease(name, nonce)` / `zns.completeRelease(...)` — release a name
131
+ // Verify a listing signature
132
+ const listing = await zns.listings().then(l => l[0]);
133
+ const valid = await zns.verifyListing(listing, status.admin_pubkey);
75
134
 
76
- Each `complete*` method returns a `{ memo, uri }` object suitable for constructing a ZIP 321 URI.
135
+ // Verify a registration
136
+ const reg = await zns.resolveName("alice");
137
+ const validReg = await zns.verifyRegistration(reg, status.admin_pubkey);
138
+ ```
77
139
 
78
- ### `zns.parseZip321Uri(uri)`
79
- Parse a ZIP 321 URI into its components.
140
+ ## Configuration
80
141
 
81
142
  ```ts
82
- const parsed = zns.parseZip321Uri("zcash:addr?amount=0.1&memo=...");
143
+ // Custom endpoint
144
+ const zns = new ZNS({ url: "https://your-indexer.com/zns" });
145
+
146
+ // Optional: verify server identity
147
+ await zns.verify(); // throws if not a known ZNS instance
148
+
149
+ // Access current status
150
+ const status = await zns.status();
151
+ console.log(status.admin_pubkey); // Registry admin pubkey
152
+ console.log(status.pricing); // Current pricing tiers
153
+ console.log(status.address); // Registry payment address
83
154
  ```
84
155
 
85
156
  ## Constants
86
157
 
87
- - `DEFAULT_URL` — default testnet endpoint
88
- - `TESTNET_UIVK` expected testnet UIVK for verification
89
- - `MAINNET_UIVK` — expected mainnet UIVK for verification
158
+ ```ts
159
+ import { DEFAULT_URL, TESTNET_UIVK, MAINNET_UIVK } from "zcashname-sdk";
160
+
161
+ console.log(DEFAULT_URL); // "https://light.zcash.me/zns-testnet"
162
+ ```
163
+
164
+ ## CLI Example
165
+
166
+ See `examples/cli/` for a working command-line interface:
167
+
168
+ ```bash
169
+ # Resolve a name
170
+ npx tsx src/index.ts resolve alice
171
+
172
+ # Check availability
173
+ npx tsx src/index.ts available bob
174
+
175
+ # List all names for sale
176
+ npx tsx src/index.ts listings
177
+
178
+ # Prepare a claim (outputs payload to sign)
179
+ npx tsx src/index.ts prepare-claim myname u1qqlzrf9...
180
+
181
+ # Sign and complete with a private key
182
+ npx tsx src/index.ts sign-claim myname u1qqlzrf9... --key-file ./key.txt
183
+ ```
184
+
185
+ ## Types
186
+
187
+ ```ts
188
+ type Zats = number; // 1 ZEC = 100,000,000 zats
189
+
190
+ interface Registration {
191
+ name: string;
192
+ address: string;
193
+ txid: string;
194
+ height: number;
195
+ nonce: number;
196
+ signature: string | null;
197
+ last_action: "CLAIM" | "BUY" | "UPDATE" | "DELIST" | "RELEASE";
198
+ pubkey: string | null; // null = admin-registered
199
+ listing: Listing | null;
200
+ }
201
+
202
+ interface Listing {
203
+ name: string;
204
+ price: Zats;
205
+ nonce: number;
206
+ txid: string;
207
+ height: number;
208
+ signature: string;
209
+ pubkey: string | null;
210
+ }
211
+
212
+ interface Status {
213
+ synced_height: number;
214
+ admin_pubkey: string;
215
+ uivk: string;
216
+ address: string;
217
+ registered: number;
218
+ listed: number;
219
+ pricing: Pricing;
220
+ }
221
+
222
+ interface Pricing {
223
+ nonce: number;
224
+ height: number;
225
+ tiers: Zats[]; // cost for names of length 1, 2, 3...
226
+ }
227
+
228
+ interface PreparedClaim {
229
+ readonly name: string;
230
+ readonly address: string;
231
+ readonly cost: Zats;
232
+ readonly payload: string; // sign this
233
+ complete(signature: string, userPubkey?: string): CompletedAction;
234
+ }
235
+
236
+ interface CompletedAction {
237
+ memo: string; // ZNS:CLAIM:name:address:sig:pubkey
238
+ uri: string; // zcash:addr?amount=...&memo=...
239
+ }
240
+ ```
90
241
 
91
242
  ## License
92
243
 
93
- MIT
244
+ MIT