zcashname-sdk 0.5.1 → 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 +197 -46
- package/dist/zns.cjs +229 -180
- package/dist/zns.d.cts +137 -59
- package/dist/zns.d.ts +137 -59
- package/dist/zns.js +218 -179
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ZNS SDK
|
|
2
2
|
|
|
3
|
-
TypeScript SDK for
|
|
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
|
-
|
|
14
|
+
// Create client (synchronous, no network calls)
|
|
15
|
+
const zns = new ZNS();
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
##
|
|
28
|
+
## Actions Flow
|
|
28
29
|
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
-
Resolve a name to its registered address. Returns `null` if the name is available.
|
|
60
|
+
## Available Actions
|
|
39
61
|
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
-
|
|
52
|
-
|
|
95
|
+
## Server Verification (Optional)
|
|
96
|
+
|
|
97
|
+
For security-sensitive applications, verify the server identity:
|
|
53
98
|
|
|
54
99
|
```ts
|
|
55
|
-
const
|
|
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
|
-
|
|
59
|
-
Query blockchain events (claims, buys, lists, delists, updates, releases).
|
|
110
|
+
## Validation & Utilities
|
|
60
111
|
|
|
61
112
|
```ts
|
|
62
|
-
|
|
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
|
-
|
|
125
|
+
## Signature Verification
|
|
66
126
|
|
|
67
|
-
|
|
127
|
+
```ts
|
|
128
|
+
// Get admin pubkey from status
|
|
129
|
+
const status = await zns.status();
|
|
68
130
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
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
|
-
|
|
79
|
-
Parse a ZIP 321 URI into its components.
|
|
140
|
+
## Configuration
|
|
80
141
|
|
|
81
142
|
```ts
|
|
82
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|