zcashname-sdk 0.5.0 → 0.6.0

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 ADDED
@@ -0,0 +1,244 @@
1
+ # ZNS SDK
2
+
3
+ TypeScript SDK for the Zcash Name System. Register names, manage listings, and resolve addresses on Zcash.
4
+
5
+ ```bash
6
+ npm install zcashname-sdk
7
+ ```
8
+
9
+ ## Quick Start
10
+
11
+ ```ts
12
+ import { ZNS } from "zcashname-sdk";
13
+
14
+ // Create client (synchronous, no network calls)
15
+ const zns = new ZNS();
16
+
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();
26
+ ```
27
+
28
+ ## Actions Flow
29
+
30
+ Every state-changing action follows the same pattern: **prepare → sign → complete**.
31
+
32
+ ```ts
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);
58
+ ```
59
+
60
+ ## Available Actions
61
+
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 |
71
+
72
+ ## Reading Data
73
+
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
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
93
+ ```
94
+
95
+ ## Server Verification (Optional)
96
+
97
+ For security-sensitive applications, verify the server identity:
98
+
99
+ ```ts
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();
108
+ ```
109
+
110
+ ## Validation & Utilities
111
+
112
+ ```ts
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: "..." }
123
+ ```
124
+
125
+ ## Signature Verification
126
+
127
+ ```ts
128
+ // Get admin pubkey from status
129
+ const status = await zns.status();
130
+
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);
134
+
135
+ // Verify a registration
136
+ const reg = await zns.resolveName("alice");
137
+ const validReg = await zns.verifyRegistration(reg, status.admin_pubkey);
138
+ ```
139
+
140
+ ## Configuration
141
+
142
+ ```ts
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
154
+ ```
155
+
156
+ ## Constants
157
+
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
+ ```
241
+
242
+ ## License
243
+
244
+ MIT