zcashname-sdk 0.2.3 → 0.3.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.
@@ -0,0 +1,61 @@
1
+ import {
2
+ isValidName
3
+ } from "./chunk-TNP4GYNO.js";
4
+
5
+ // src/memo.ts
6
+ function claimPayload(name, ua) {
7
+ return `CLAIM:${name}:${ua}`;
8
+ }
9
+ function buyPayload(name, buyerUa) {
10
+ return `BUY:${name}:${buyerUa}`;
11
+ }
12
+ function listPayload(name, price, nonce) {
13
+ return `LIST:${name}:${price}:${nonce}`;
14
+ }
15
+ function delistPayload(name, nonce) {
16
+ return `DELIST:${name}:${nonce}`;
17
+ }
18
+ function updatePayload(name, newUa, nonce) {
19
+ return `UPDATE:${name}:${newUa}:${nonce}`;
20
+ }
21
+ function setPricePayload(prices, nonce) {
22
+ return `SETPRICE:${prices.length}:${prices.join(":")}:${nonce}`;
23
+ }
24
+ function buildClaimMemo(name, ua, signature) {
25
+ if (!isValidName(name)) throw new Error(`Invalid name: ${name}`);
26
+ return `ZNS:CLAIM:${name}:${ua}:${signature}`;
27
+ }
28
+ function buildBuyMemo(name, buyerUa, signature) {
29
+ if (!isValidName(name)) throw new Error(`Invalid name: ${name}`);
30
+ return `ZNS:BUY:${name}:${buyerUa}:${signature}`;
31
+ }
32
+ function buildListMemo(name, price, nonce, signature) {
33
+ if (!isValidName(name)) throw new Error(`Invalid name: ${name}`);
34
+ return `ZNS:LIST:${name}:${price}:${nonce}:${signature}`;
35
+ }
36
+ function buildDelistMemo(name, nonce, signature) {
37
+ if (!isValidName(name)) throw new Error(`Invalid name: ${name}`);
38
+ return `ZNS:DELIST:${name}:${nonce}:${signature}`;
39
+ }
40
+ function buildUpdateMemo(name, newUa, nonce, signature) {
41
+ if (!isValidName(name)) throw new Error(`Invalid name: ${name}`);
42
+ return `ZNS:UPDATE:${name}:${newUa}:${nonce}:${signature}`;
43
+ }
44
+ function buildSetPriceMemo(prices, nonce, signature) {
45
+ return `ZNS:SETPRICE:${prices.length}:${prices.join(":")}:${nonce}:${signature}`;
46
+ }
47
+
48
+ export {
49
+ claimPayload,
50
+ buyPayload,
51
+ listPayload,
52
+ delistPayload,
53
+ updatePayload,
54
+ setPricePayload,
55
+ buildClaimMemo,
56
+ buildBuyMemo,
57
+ buildListMemo,
58
+ buildDelistMemo,
59
+ buildUpdateMemo,
60
+ buildSetPriceMemo
61
+ };
@@ -0,0 +1,98 @@
1
+ // src/errors.ts
2
+ var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
3
+ ErrorType2[ErrorType2["ParseError"] = -32700] = "ParseError";
4
+ ErrorType2[ErrorType2["InvalidRequest"] = -32600] = "InvalidRequest";
5
+ ErrorType2[ErrorType2["MethodNotFound"] = -32601] = "MethodNotFound";
6
+ ErrorType2[ErrorType2["InvalidParams"] = -32602] = "InvalidParams";
7
+ ErrorType2[ErrorType2["InternalError"] = -32603] = "InternalError";
8
+ ErrorType2[ErrorType2["HttpError"] = -1] = "HttpError";
9
+ ErrorType2[ErrorType2["UivkMismatch"] = -2] = "UivkMismatch";
10
+ return ErrorType2;
11
+ })(ErrorType || {});
12
+ var ZNSError = class extends Error {
13
+ constructor(type, message) {
14
+ super(message ?? ErrorType[type]);
15
+ this.name = "ZNSError";
16
+ this.type = type;
17
+ }
18
+ };
19
+
20
+ // src/constants.ts
21
+ var DEFAULT_URL = "https://light.zcash.me/zns-testnet";
22
+ var TESTNET_UIVK = "uivktest1hzw7wyadutvzfgpna80yftsk5l7jeyu2p5me5quvp28tytxueta00cx4068wnlzcv7tx9n3t3gfhsy83pe4y6jrhxtzaq0hj6xtg5zrk2dn7zen3vns2a5pgs4fxdjlletmqrhfa42";
23
+ var MAINNET_UIVK = "uivk1gl26qy0xjja7lqhyg3pf0x4j4j66kqwewrjkdcg28eqq4wgtzjmujpee7x9cs2ec9xhnlgrm8ptlw8z80j2aryw8nqtssser2ys778a0s00uvgkdjnfr58sndhfvc3f4zqjs6ywva6";
24
+ var KNOWN_UIVKS = [TESTNET_UIVK, MAINNET_UIVK];
25
+
26
+ // src/rpc.ts
27
+ async function rpc(url, method, params = {}, id = 1) {
28
+ const body = JSON.stringify({ jsonrpc: "2.0", id, method, params });
29
+ const res = await fetch(url, {
30
+ method: "POST",
31
+ headers: { "Content-Type": "application/json" },
32
+ body
33
+ });
34
+ if (!res.ok) {
35
+ throw new ZNSError(-1 /* HttpError */, `HTTP ${res.status}: ${res.statusText}`);
36
+ }
37
+ const json = await res.json();
38
+ if (json.error) {
39
+ const type = Object.values(ErrorType).includes(json.error.code) ? json.error.code : -32603 /* InternalError */;
40
+ throw new ZNSError(type, json.error.message);
41
+ }
42
+ return json.result;
43
+ }
44
+
45
+ // src/client.ts
46
+ async function createClient(url = DEFAULT_URL, options = {}) {
47
+ let nextId = 1;
48
+ let verified = false;
49
+ async function call(method, params = {}) {
50
+ return rpc(url, method, params, nextId++);
51
+ }
52
+ if (!options.skipVerify) {
53
+ const s = await call("status");
54
+ if (!KNOWN_UIVKS.includes(s.uivk)) {
55
+ throw new ZNSError(
56
+ -2 /* UivkMismatch */,
57
+ `UIVK mismatch: indexer returned "${s.uivk.slice(0, 20)}..." which is not a known ZNS instance`
58
+ );
59
+ }
60
+ verified = true;
61
+ }
62
+ const client = {
63
+ url,
64
+ verified,
65
+ async resolve(query) {
66
+ return call("resolve", { query });
67
+ },
68
+ async listings() {
69
+ const result = await call("list_for_sale");
70
+ return result.listings;
71
+ },
72
+ async status() {
73
+ return call("status");
74
+ },
75
+ async isAvailable(name) {
76
+ const result = await client.resolve(name);
77
+ return result === null;
78
+ },
79
+ async events(filter = {}) {
80
+ return call("events", filter);
81
+ },
82
+ async getNonce(name) {
83
+ const result = await client.resolve(name);
84
+ return result?.nonce ?? null;
85
+ }
86
+ };
87
+ return client;
88
+ }
89
+
90
+ export {
91
+ ErrorType,
92
+ ZNSError,
93
+ DEFAULT_URL,
94
+ TESTNET_UIVK,
95
+ MAINNET_UIVK,
96
+ KNOWN_UIVKS,
97
+ createClient
98
+ };
@@ -0,0 +1,45 @@
1
+ // src/zip321.ts
2
+ function toBase64Url(text) {
3
+ try {
4
+ return btoa(unescape(encodeURIComponent(text))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
5
+ } catch {
6
+ return "";
7
+ }
8
+ }
9
+ function decodeBase64Url(value) {
10
+ try {
11
+ const normalized = String(value).replace(/-/g, "+").replace(/_/g, "/");
12
+ const paddingLength = normalized.length % 4 === 0 ? 0 : 4 - normalized.length % 4;
13
+ const padded = normalized + "=".repeat(paddingLength);
14
+ const binary = atob(padded);
15
+ const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
16
+ return new TextDecoder().decode(bytes);
17
+ } catch {
18
+ return "";
19
+ }
20
+ }
21
+ function buildZcashUri(address, amount, memo) {
22
+ if (!address) return "";
23
+ const base = `zcash:${address}`;
24
+ const params = [];
25
+ if (amount !== void 0 && Number(amount) > 0) params.push(`amount=${amount}`);
26
+ if (memo) params.push(`memo=${toBase64Url(memo)}`);
27
+ return params.length ? `${base}?${params.join("&")}` : base;
28
+ }
29
+ function parseZip321Uri(uri) {
30
+ const withoutScheme = String(uri ?? "").replace(/^zcash:/i, "");
31
+ const [addressPart, queryPart = ""] = withoutScheme.split("?");
32
+ const address = addressPart.trim();
33
+ const params = new URLSearchParams(queryPart);
34
+ const amount = String(params.get("amount") ?? "").trim();
35
+ const memoRaw = String(params.get("memo") ?? "").trim();
36
+ const memoDecoded = memoRaw ? decodeBase64Url(memoRaw) : "";
37
+ return { address, amount, memoRaw, memoDecoded };
38
+ }
39
+
40
+ export {
41
+ toBase64Url,
42
+ decodeBase64Url,
43
+ buildZcashUri,
44
+ parseZip321Uri
45
+ };
@@ -0,0 +1,10 @@
1
+ // src/pricing.ts
2
+ function claimCost(tiers, nameLength) {
3
+ if (tiers.length === 0) return null;
4
+ const idx = Math.min(Math.max(nameLength - 1, 0), tiers.length - 1);
5
+ return tiers[idx];
6
+ }
7
+
8
+ export {
9
+ claimCost
10
+ };
@@ -0,0 +1,8 @@
1
+ // src/validation.ts
2
+ function isValidName(name) {
3
+ return /^[a-z0-9](?:[a-z0-9-]{0,60}[a-z0-9])?$/.test(name) && !name.includes("--");
4
+ }
5
+
6
+ export {
7
+ isValidName
8
+ };
@@ -0,0 +1,76 @@
1
+ interface Registration {
2
+ name: string;
3
+ address: string;
4
+ txid: string;
5
+ height: number;
6
+ nonce: number;
7
+ signature: string | null;
8
+ }
9
+ interface Listing {
10
+ name: string;
11
+ price: number;
12
+ txid: string;
13
+ height: number;
14
+ signature: string;
15
+ }
16
+ interface ResolveResult extends Registration {
17
+ listing: Listing | null;
18
+ }
19
+ interface ListForSaleResult {
20
+ listings: Listing[];
21
+ }
22
+ interface Pricing {
23
+ nonce: number;
24
+ height: number;
25
+ /** Per-character claim prices in zats.
26
+ * Index 0 = 1-char names, index 1 = 2-char, etc.
27
+ * Names longer than the array clamp to the last entry. */
28
+ tiers: number[];
29
+ }
30
+ interface StatusResult {
31
+ synced_height: number;
32
+ admin_pubkey: string;
33
+ uivk: string;
34
+ registered: number;
35
+ listed: number;
36
+ pricing: Pricing | null;
37
+ }
38
+ interface Event {
39
+ id: number;
40
+ name: string;
41
+ action: "CLAIM" | "LIST" | "DELIST" | "UPDATE" | "BUY";
42
+ txid: string;
43
+ height: number;
44
+ ua: string | null;
45
+ price: number | null;
46
+ nonce: number | null;
47
+ signature: string | null;
48
+ }
49
+ interface EventsFilter {
50
+ name?: string;
51
+ action?: string | string[];
52
+ since_height?: number;
53
+ limit?: number;
54
+ offset?: number;
55
+ }
56
+ interface EventsResult {
57
+ events: Event[];
58
+ total: number;
59
+ }
60
+
61
+ interface ClientOptions {
62
+ skipVerify?: boolean;
63
+ }
64
+ interface ZNSClient {
65
+ readonly url: string;
66
+ readonly verified: boolean;
67
+ resolve(query: string): Promise<ResolveResult | null>;
68
+ listings(): Promise<Listing[]>;
69
+ status(): Promise<StatusResult>;
70
+ events(filter?: EventsFilter): Promise<EventsResult>;
71
+ isAvailable(name: string): Promise<boolean>;
72
+ getNonce(name: string): Promise<number | null>;
73
+ }
74
+ declare function createClient(url?: string, options?: ClientOptions): Promise<ZNSClient>;
75
+
76
+ export { type ClientOptions as C, type Event as E, type ListForSaleResult as L, type Pricing as P, type Registration as R, type StatusResult as S, type ZNSClient as Z, type EventsFilter as a, type EventsResult as b, type Listing as c, type ResolveResult as d, createClient as e };
@@ -0,0 +1,76 @@
1
+ interface Registration {
2
+ name: string;
3
+ address: string;
4
+ txid: string;
5
+ height: number;
6
+ nonce: number;
7
+ signature: string | null;
8
+ }
9
+ interface Listing {
10
+ name: string;
11
+ price: number;
12
+ txid: string;
13
+ height: number;
14
+ signature: string;
15
+ }
16
+ interface ResolveResult extends Registration {
17
+ listing: Listing | null;
18
+ }
19
+ interface ListForSaleResult {
20
+ listings: Listing[];
21
+ }
22
+ interface Pricing {
23
+ nonce: number;
24
+ height: number;
25
+ /** Per-character claim prices in zats.
26
+ * Index 0 = 1-char names, index 1 = 2-char, etc.
27
+ * Names longer than the array clamp to the last entry. */
28
+ tiers: number[];
29
+ }
30
+ interface StatusResult {
31
+ synced_height: number;
32
+ admin_pubkey: string;
33
+ uivk: string;
34
+ registered: number;
35
+ listed: number;
36
+ pricing: Pricing | null;
37
+ }
38
+ interface Event {
39
+ id: number;
40
+ name: string;
41
+ action: "CLAIM" | "LIST" | "DELIST" | "UPDATE" | "BUY";
42
+ txid: string;
43
+ height: number;
44
+ ua: string | null;
45
+ price: number | null;
46
+ nonce: number | null;
47
+ signature: string | null;
48
+ }
49
+ interface EventsFilter {
50
+ name?: string;
51
+ action?: string | string[];
52
+ since_height?: number;
53
+ limit?: number;
54
+ offset?: number;
55
+ }
56
+ interface EventsResult {
57
+ events: Event[];
58
+ total: number;
59
+ }
60
+
61
+ interface ClientOptions {
62
+ skipVerify?: boolean;
63
+ }
64
+ interface ZNSClient {
65
+ readonly url: string;
66
+ readonly verified: boolean;
67
+ resolve(query: string): Promise<ResolveResult | null>;
68
+ listings(): Promise<Listing[]>;
69
+ status(): Promise<StatusResult>;
70
+ events(filter?: EventsFilter): Promise<EventsResult>;
71
+ isAvailable(name: string): Promise<boolean>;
72
+ getNonce(name: string): Promise<number | null>;
73
+ }
74
+ declare function createClient(url?: string, options?: ClientOptions): Promise<ZNSClient>;
75
+
76
+ export { type ClientOptions as C, type Event as E, type ListForSaleResult as L, type Pricing as P, type Registration as R, type StatusResult as S, type ZNSClient as Z, type EventsFilter as a, type EventsResult as b, type Listing as c, type ResolveResult as d, createClient as e };
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/client.ts
21
+ var client_exports = {};
22
+ __export(client_exports, {
23
+ createClient: () => createClient
24
+ });
25
+ module.exports = __toCommonJS(client_exports);
26
+
27
+ // src/errors.ts
28
+ var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
29
+ ErrorType2[ErrorType2["ParseError"] = -32700] = "ParseError";
30
+ ErrorType2[ErrorType2["InvalidRequest"] = -32600] = "InvalidRequest";
31
+ ErrorType2[ErrorType2["MethodNotFound"] = -32601] = "MethodNotFound";
32
+ ErrorType2[ErrorType2["InvalidParams"] = -32602] = "InvalidParams";
33
+ ErrorType2[ErrorType2["InternalError"] = -32603] = "InternalError";
34
+ ErrorType2[ErrorType2["HttpError"] = -1] = "HttpError";
35
+ ErrorType2[ErrorType2["UivkMismatch"] = -2] = "UivkMismatch";
36
+ return ErrorType2;
37
+ })(ErrorType || {});
38
+ var ZNSError = class extends Error {
39
+ constructor(type, message) {
40
+ super(message ?? ErrorType[type]);
41
+ this.name = "ZNSError";
42
+ this.type = type;
43
+ }
44
+ };
45
+
46
+ // src/constants.ts
47
+ var DEFAULT_URL = "https://light.zcash.me/zns-testnet";
48
+ var TESTNET_UIVK = "uivktest1hzw7wyadutvzfgpna80yftsk5l7jeyu2p5me5quvp28tytxueta00cx4068wnlzcv7tx9n3t3gfhsy83pe4y6jrhxtzaq0hj6xtg5zrk2dn7zen3vns2a5pgs4fxdjlletmqrhfa42";
49
+ var MAINNET_UIVK = "uivk1gl26qy0xjja7lqhyg3pf0x4j4j66kqwewrjkdcg28eqq4wgtzjmujpee7x9cs2ec9xhnlgrm8ptlw8z80j2aryw8nqtssser2ys778a0s00uvgkdjnfr58sndhfvc3f4zqjs6ywva6";
50
+ var KNOWN_UIVKS = [TESTNET_UIVK, MAINNET_UIVK];
51
+
52
+ // src/rpc.ts
53
+ async function rpc(url, method, params = {}, id = 1) {
54
+ const body = JSON.stringify({ jsonrpc: "2.0", id, method, params });
55
+ const res = await fetch(url, {
56
+ method: "POST",
57
+ headers: { "Content-Type": "application/json" },
58
+ body
59
+ });
60
+ if (!res.ok) {
61
+ throw new ZNSError(-1 /* HttpError */, `HTTP ${res.status}: ${res.statusText}`);
62
+ }
63
+ const json = await res.json();
64
+ if (json.error) {
65
+ const type = Object.values(ErrorType).includes(json.error.code) ? json.error.code : -32603 /* InternalError */;
66
+ throw new ZNSError(type, json.error.message);
67
+ }
68
+ return json.result;
69
+ }
70
+
71
+ // src/client.ts
72
+ async function createClient(url = DEFAULT_URL, options = {}) {
73
+ let nextId = 1;
74
+ let verified = false;
75
+ async function call(method, params = {}) {
76
+ return rpc(url, method, params, nextId++);
77
+ }
78
+ if (!options.skipVerify) {
79
+ const s = await call("status");
80
+ if (!KNOWN_UIVKS.includes(s.uivk)) {
81
+ throw new ZNSError(
82
+ -2 /* UivkMismatch */,
83
+ `UIVK mismatch: indexer returned "${s.uivk.slice(0, 20)}..." which is not a known ZNS instance`
84
+ );
85
+ }
86
+ verified = true;
87
+ }
88
+ const client = {
89
+ url,
90
+ verified,
91
+ async resolve(query) {
92
+ return call("resolve", { query });
93
+ },
94
+ async listings() {
95
+ const result = await call("list_for_sale");
96
+ return result.listings;
97
+ },
98
+ async status() {
99
+ return call("status");
100
+ },
101
+ async isAvailable(name) {
102
+ const result = await client.resolve(name);
103
+ return result === null;
104
+ },
105
+ async events(filter = {}) {
106
+ return call("events", filter);
107
+ },
108
+ async getNonce(name) {
109
+ const result = await client.resolve(name);
110
+ return result?.nonce ?? null;
111
+ }
112
+ };
113
+ return client;
114
+ }
115
+ // Annotate the CommonJS export names for ESM import in node:
116
+ 0 && (module.exports = {
117
+ createClient
118
+ });
@@ -0,0 +1 @@
1
+ export { C as ClientOptions, Z as ZNSClient, e as createClient } from './client-N6gdk287.cjs';
@@ -0,0 +1 @@
1
+ export { C as ClientOptions, Z as ZNSClient, e as createClient } from './client-N6gdk287.js';
package/dist/client.js ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ createClient
3
+ } from "./chunk-E3JAIK65.js";
4
+ export {
5
+ createClient
6
+ };