@synonymdev/pubky 0.6.0-rc.7 → 0.7.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.
Files changed (6) hide show
  1. package/README.md +29 -14
  2. package/index.cjs +2289 -1989
  3. package/index.js +2292 -2138
  4. package/package.json +3 -3
  5. package/pubky.d.ts +1122 -814
  6. package/pubky_bg.wasm +0 -0
package/README.md CHANGED
@@ -38,8 +38,8 @@ const path = "/pub/example.com/hello.json";
38
38
  await session.storage.putJson(path, { hello: "world" });
39
39
 
40
40
  // 4) Read it publicly (no auth needed)
41
- const userPk = session.info.publicKey.z32();
42
- const addr = `pubky${userPk}/pub/example.com/hello.json`;
41
+ const userPk = session.info.publicKey.toString();
42
+ const addr = `${userPk}/pub/example.com/hello.json`;
43
43
  const json = await pubky.publicStorage.getJson(addr); // -> { hello: "world" }
44
44
 
45
45
  // 5) Authenticate on a 3rd-party app
@@ -50,10 +50,23 @@ const session = await authFlow.awaitApproval();
50
50
 
51
51
  Find here [**ready-to-run examples**](https://github.com/pubky/pubky-core/tree/main/examples).
52
52
 
53
+ ### Key formats (display vs transport)
54
+
55
+ `PublicKey` has two string forms:
56
+
57
+ - **Display format**: `pubky<z32>` (for logs/UI/human-facing identifiers).
58
+ - **Transport/storage format**: raw `z32` (for hostnames, headers, query params, serde/JSON, DB storage).
59
+
60
+ Use `publicKey.z32()` for transport/storage. Use `publicKey.toString()` for display.
61
+
53
62
  ### Initialization & events
54
63
 
55
64
  The npm package bundles the WebAssembly module and **initializes it before exposing any APIs**. This avoids the common wasm-pack pitfall where events fire before the module finishes instantiating. Long-polling flows such as `authFlow.awaitApproval()` or `authFlow.tryPollOnce()` only start their relay calls after the underlying module is ready, so you won't miss approvals while the bundle is loading.
56
65
 
66
+ ### Reuse a single facade across your app
67
+
68
+ Use a shared `Pubky` (e.g, via context or prop drilling) instead of constructing one per request. This avoids reinitializing transports and keeps the same client available for repeated usage.
69
+
57
70
  ## API Overview
58
71
 
59
72
  Use `new Pubky()` to quickly get any flow started:
@@ -87,12 +100,13 @@ const client = pubky.client;
87
100
  ### Client (HTTP bridge)
88
101
 
89
102
  ```js
90
- import { Client, resolvePubky } from "@synonymdev/pubky";
103
+ import { Client, PublicKey, resolvePubky } from "@synonymdev/pubky";
91
104
 
92
105
  const client = new Client(); // or: pubky.client.fetch(); instead of constructing a client manually
93
106
 
94
107
  // Convert the identifier into a transport URL before fetching.
95
- const url = resolvePubky("pubky<pubky>/pub/example.com/file.txt");
108
+ const userId = PublicKey.from("pubky<z32>").toString();
109
+ const url = resolvePubky(`${userId}/pub/example.com/file.txt`);
96
110
  const res = await client.fetch(url);
97
111
  ```
98
112
 
@@ -108,6 +122,7 @@ const pubkey = keypair.publicKey;
108
122
 
109
123
  // z-base-32 roundtrip
110
124
  const parsed = PublicKey.from(pubkey.z32());
125
+ const displayId = pubkey.toString(); // pubky<z32> (display only)
111
126
  ```
112
127
 
113
128
  #### Recovery file (encrypt/decrypt root secret)
@@ -151,7 +166,7 @@ await session.signout(); // invalidates server session
151
166
  **Session details**
152
167
 
153
168
  ```js
154
- const userPk = session.info.publicKey.z32(); // -> PublicKey as z32 string
169
+ const userPk = session.info.publicKey.toString(); // -> pubky<z32> identifier
155
170
  const caps = session.info.capabilities; // -> string[] permissions and paths
156
171
 
157
172
  const storage = session.storage; // -> This User's storage API (absolute paths)
@@ -191,7 +206,7 @@ const pubky = new Pubky();
191
206
  const caps = "/pub/my-cool-app/:rw,/pub/another-app/folder/:w";
192
207
 
193
208
  // Optional relay; defaults to Synonym-hosted relay if omitted
194
- const relay = "https://httprelay.pubky.app/link/"; // optional (defaults to this)
209
+ const relay = "https://httprelay.pubky.app/inbox/"; // optional (defaults to this)
195
210
 
196
211
  // Start the auth polling
197
212
  const flow = pubky.startAuthFlow(caps, AuthFlowKind::signin(), relay);
@@ -252,20 +267,20 @@ const pub = pubky.publicStorage;
252
267
 
253
268
  // Reads
254
269
  const response = await pub.get(
255
- `pubky${userPk.z32()}/pub/example.com/data.json`
270
+ `${userPk}/pub/example.com/data.json`
256
271
  ); // -> Response (stream it)
257
- await pub.getJson(`pubky${userPk.z32()}/pub/example.com/data.json`);
258
- await pub.getText(`pubky${userPk.z32()}/pub/example.com/readme.txt`);
259
- await pub.getBytes(`pubky${userPk.z32()}/pub/example.com/icon.png`); // Uint8Array
272
+ await pub.getJson(`${userPk}/pub/example.com/data.json`);
273
+ await pub.getText(`${userPk}/pub/example.com/readme.txt`);
274
+ await pub.getBytes(`${userPk}/pub/example.com/icon.png`); // Uint8Array
260
275
 
261
276
  // Metadata
262
- await pub.exists(`pubky${userPk.z32()}/pub/example.com/foo`); // boolean
263
- await pub.stats(`pubky${userPk.z32()}/pub/example.com/foo`); // { content_length, content_type, etag, last_modified } | null
277
+ await pub.exists(`${userPk}/pub/example.com/foo`); // boolean
278
+ await pub.stats(`${userPk}/pub/example.com/foo`); // { content_length, content_type, etag, last_modified } | null
264
279
 
265
280
  // List directory (addressed path "<pubky>/pub/.../") must include trailing `/`.
266
281
  // list(addr, cursor=null|suffix|fullUrl, reverse=false, limit?, shallow=false)
267
282
  await pub.list(
268
- `pubky${userPk.z32()}/pub/example.com/`,
283
+ `${userPk}/pub/example.com/`,
269
284
  null,
270
285
  false,
271
286
  100,
@@ -323,7 +338,7 @@ import { Pubky, PublicKey, Keypair } from "@synonymdev/pubky";
323
338
  const pubky = new Pubky();
324
339
 
325
340
  // Read-only resolver
326
- const homeserver = await pubky.getHomeserverOf(PublicKey.from("<user-z32>")); // string | undefined
341
+ const homeserver = await pubky.getHomeserverOf(PublicKey.from("pubky<z32>")); // string | undefined
327
342
 
328
343
  // With keys (signer-bound)
329
344
  const signer = pubky.signer(Keypair.random());