@synonymdev/pubky 0.6.0-rc.7 → 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 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)
@@ -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());
package/index.cjs CHANGED
@@ -471,8 +471,7 @@ class AuthFlow {
471
471
  * @returns {Promise<Session>}
472
472
  */
473
473
  awaitApproval() {
474
- const ptr = this.__destroy_into_raw();
475
- const ret = wasm.authflow_awaitApproval(ptr);
474
+ const ret = wasm.authflow_awaitApproval(this.__wbg_ptr);
476
475
  return ret;
477
476
  }
478
477
  /**
@@ -486,8 +485,7 @@ class AuthFlow {
486
485
  * @returns {Promise<AuthToken>}
487
486
  */
488
487
  awaitToken() {
489
- const ptr = this.__destroy_into_raw();
490
- const ret = wasm.authflow_awaitToken(ptr);
488
+ const ret = wasm.authflow_awaitToken(this.__wbg_ptr);
491
489
  return ret;
492
490
  }
493
491
  /**
@@ -600,7 +598,7 @@ const AuthTokenFinalization = (typeof FinalizationRegistry === 'undefined')
600
598
  * const token = await flow.awaitToken();
601
599
  *
602
600
  * // Identify the user
603
- * console.log(token.publicKey().z32());
601
+ * console.log(token.publicKey().toString());
604
602
  *
605
603
  * // Optionally forward to a server for verification:
606
604
  * await fetch("/api/verify", { method: "POST", body: token.toBytes() });
@@ -645,7 +643,7 @@ class AuthToken {
645
643
  * export async function POST(req) {
646
644
  * const bytes = new Uint8Array(await req.arrayBuffer());
647
645
  * const token = AuthToken.verify(bytes); // throws on failure
648
- * return new Response(token.publicKey().z32(), { status: 200 });
646
+ * return new Response(token.publicKey().toString(), { status: 200 });
649
647
  * }
650
648
  * ```
651
649
  * @param {Uint8Array} bytes
@@ -678,10 +676,11 @@ class AuthToken {
678
676
  /**
679
677
  * Returns the **public key** that authenticated with this token.
680
678
  *
681
- * Use `.z32()` on the returned `PublicKey` to get the string form.
679
+ * Use `.toString()` on the returned `PublicKey` to get the `pubky<z32>` identifier.
680
+ * Call `.z32()` when you specifically need the raw z-base32 value (e.g. hostnames).
682
681
  *
683
682
  * @example
684
- * const who = sessionInfo.publicKey.z32();
683
+ * const who = token.publicKey.toString();
685
684
  * @returns {PublicKey}
686
685
  */
687
686
  get publicKey() {
@@ -755,6 +754,26 @@ class Client {
755
754
  const ptr = this.__destroy_into_raw();
756
755
  wasm.__wbg_client_free(ptr, 0);
757
756
  }
757
+ /**
758
+ * Perform a raw fetch. Works with `http(s)://` URLs.
759
+ *
760
+ * @param {string} url
761
+ * @param {RequestInit} init Standard fetch options; `credentials: "include"` recommended for session I/O.
762
+ * @returns {Promise<Response>}
763
+ *
764
+ * @example
765
+ * const client = pubky.client;
766
+ * const res = await client.fetch(`https://_pubky.${user}/pub/app/file.txt`, { method: "PUT", body: "hi", credentials: "include" });
767
+ * @param {string} url
768
+ * @param {RequestInit | null} [init]
769
+ * @returns {Promise<Response>}
770
+ */
771
+ fetch(url, init) {
772
+ const ptr0 = passStringToWasm0(url, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
773
+ const len0 = WASM_VECTOR_LEN;
774
+ const ret = wasm.client_fetch(this.__wbg_ptr, ptr0, len0, isLikeNone(init) ? 0 : addToExternrefTable0(init));
775
+ return ret;
776
+ }
758
777
  /**
759
778
  * Create a Pubky HTTP client.
760
779
  *
@@ -814,26 +833,6 @@ class Client {
814
833
  }
815
834
  return Client.__wrap(ret[0]);
816
835
  }
817
- /**
818
- * Perform a raw fetch. Works with `http(s)://` URLs.
819
- *
820
- * @param {string} url
821
- * @param {RequestInit} init Standard fetch options; `credentials: "include"` recommended for session I/O.
822
- * @returns {Promise<Response>}
823
- *
824
- * @example
825
- * const client = pubky.client;
826
- * const res = await client.fetch(`https://_pubky.${user}/pub/app/file.txt`, { method: "PUT", body: "hi", credentials: "include" });
827
- * @param {string} url
828
- * @param {RequestInit | null} [init]
829
- * @returns {Promise<Response>}
830
- */
831
- fetch(url, init) {
832
- const ptr0 = passStringToWasm0(url, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
833
- const len0 = WASM_VECTOR_LEN;
834
- const ret = wasm.client_fetch(this.__wbg_ptr, ptr0, len0, isLikeNone(init) ? 0 : addToExternrefTable0(init));
835
- return ret;
836
- }
837
836
  }
838
837
  if (Symbol.dispose) Client.prototype[Symbol.dispose] = Client.prototype.free;
839
838
 
@@ -1016,34 +1015,36 @@ class Keypair {
1016
1015
  return Keypair.__wrap(ret);
1017
1016
  }
1018
1017
  /**
1019
- * Generate a [Keypair] from a secret key.
1020
- * @param {Uint8Array} secret_key
1018
+ * Generate a [Keypair] from a 32-byte secret.
1019
+ * @param {Uint8Array} secret
1021
1020
  * @returns {Keypair}
1022
1021
  */
1023
- static fromSecretKey(secret_key) {
1024
- const ptr0 = passArray8ToWasm0(secret_key, wasm.__wbindgen_malloc);
1022
+ static fromSecret(secret) {
1023
+ const ptr0 = passArray8ToWasm0(secret, wasm.__wbindgen_malloc);
1025
1024
  const len0 = WASM_VECTOR_LEN;
1026
- const ret = wasm.keypair_fromSecretKey(ptr0, len0);
1025
+ const ret = wasm.keypair_fromSecret(ptr0, len0);
1027
1026
  if (ret[2]) {
1028
1027
  throw takeFromExternrefTable0(ret[1]);
1029
1028
  }
1030
1029
  return Keypair.__wrap(ret[0]);
1031
1030
  }
1032
1031
  /**
1033
- * Returns the secret key of this keypair.
1032
+ * Returns the secret of this keypair.
1034
1033
  * @returns {Uint8Array}
1035
1034
  */
1036
- secretKey() {
1037
- const ret = wasm.keypair_secretKey(this.__wbg_ptr);
1035
+ secret() {
1036
+ const ret = wasm.keypair_secret(this.__wbg_ptr);
1038
1037
  return ret;
1039
1038
  }
1040
1039
  /**
1041
1040
  * Returns the [PublicKey] of this keypair.
1042
1041
  *
1043
- * Use `.z32()` on the returned `PublicKey` to get the string form.
1042
+ * Use `.toString()` on the returned `PublicKey` to get the string form
1043
+ * or `.z32()` to get the z32 string form without prefix.
1044
+ * Transport/storage (query params, headers, persistence) should use `.z32()`.
1044
1045
  *
1045
1046
  * @example
1046
- * const who = keypair.publicKey.z32();
1047
+ * const who = keypair.publicKey.toString();
1047
1048
  * @returns {PublicKey}
1048
1049
  */
1049
1050
  get publicKey() {
@@ -1231,6 +1232,10 @@ class Pubky {
1231
1232
  /**
1232
1233
  * Create a Pubky facade wired for **mainnet** defaults (public relays).
1233
1234
  *
1235
+ * Prefer to instantiate only once and use trough your application a single shared `Pubky`
1236
+ * instead of constructing one per request. This avoids reinitializing transports and keeps
1237
+ * the same client available for repeated usage.
1238
+ *
1234
1239
  * @returns {Pubky}
1235
1240
  * A new facade instance. Use this to create signers, start auth flows, etc.
1236
1241
  *
@@ -1355,12 +1360,12 @@ class Pubky {
1355
1360
  * Public, unauthenticated storage API.
1356
1361
  *
1357
1362
  * Use for **read-only** public access via addressed paths:
1358
- * `"<user-z32>/pub/…"`.
1363
+ * `"pubky<user>/pub/…"`.
1359
1364
  *
1360
1365
  * @returns {PublicStorage}
1361
1366
  *
1362
1367
  * @example
1363
- * const text = await pubky.publicStorage.getText(`${userPk.z32()}/pub/example.com/hello.txt`);
1368
+ * const text = await pubky.publicStorage.getText(`${userPk.toString()}/pub/example.com/hello.txt`);
1364
1369
  * @returns {PublicStorage}
1365
1370
  */
1366
1371
  get publicStorage() {
@@ -1373,7 +1378,7 @@ class Pubky {
1373
1378
  * Uses an internal read-only Pkdns actor.
1374
1379
  *
1375
1380
  * @param {PublicKey} user
1376
- * @returns {Promise<PublicKey|undefined>} Homeserver public key (z32) or `undefined` if not found.
1381
+ * @returns {Promise<PublicKey|undefined>} Homeserver public key or `undefined` if not found.
1377
1382
  * @param {PublicKey} user_public_key
1378
1383
  * @returns {Promise<PublicKey | undefined>}
1379
1384
  */
@@ -1389,7 +1394,7 @@ class Pubky {
1389
1394
  * Use this for low-level `fetch()` calls or testing with raw URLs.
1390
1395
  *
1391
1396
  * @example
1392
- * const r = await pubky.client.fetch(`pubky://${user}/pub/app/file.txt`, { credentials: "include" });
1397
+ * const r = await pubky.client.fetch(`pubky://${userPk.z32()}/pub/app/file.txt`, { credentials: "include" });
1393
1398
  * @returns {Client}
1394
1399
  */
1395
1400
  get client() {
@@ -1471,6 +1476,23 @@ class PublicKey {
1471
1476
  wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
1472
1477
  }
1473
1478
  }
1479
+ /**
1480
+ * Returns the identifier form with the `pubky` prefix.
1481
+ * Use for display only; transport/storage should use `.z32()`.
1482
+ * @returns {string}
1483
+ */
1484
+ toString() {
1485
+ let deferred1_0;
1486
+ let deferred1_1;
1487
+ try {
1488
+ const ret = wasm.publickey_toString(this.__wbg_ptr);
1489
+ deferred1_0 = ret[0];
1490
+ deferred1_1 = ret[1];
1491
+ return getStringFromWasm0(ret[0], ret[1]);
1492
+ } finally {
1493
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
1494
+ }
1495
+ }
1474
1496
  /**
1475
1497
  * @throws
1476
1498
  * @param {string} value
@@ -1494,7 +1516,7 @@ const PublicStorageFinalization = (typeof FinalizationRegistry === 'undefined')
1494
1516
  ? { register: () => {}, unregister: () => {} }
1495
1517
  : new FinalizationRegistry(ptr => wasm.__wbg_publicstorage_free(ptr >>> 0, 1));
1496
1518
  /**
1497
- * Read-only public storage using addressed paths (`"<user-z32>/pub/...")`.
1519
+ * Read-only public storage using addressed paths (`"pubky<user>/pub/..."`).
1498
1520
  */
1499
1521
  class PublicStorage {
1500
1522
 
@@ -1851,12 +1873,13 @@ class SessionInfo {
1851
1873
  /**
1852
1874
  * The user’s public key for this session.
1853
1875
  *
1854
- * Use `.z32()` on the returned `PublicKey` to get the string form.
1876
+ * Use `.toString()` on the returned `PublicKey` to get the `pubky<z32>` identifier.
1877
+ * Call `.z32()` when you specifically need the raw z-base32 value (e.g. hostnames).
1855
1878
  *
1856
1879
  * @returns {PublicKey}
1857
1880
  *
1858
1881
  * @example
1859
- * const who = sessionInfo.publicKey.z32();
1882
+ * const who = sessionInfo.publicKey.toString();
1860
1883
  * @returns {PublicKey}
1861
1884
  */
1862
1885
  get publicKey() {
@@ -2642,12 +2665,12 @@ exports.__wbg_error_d8b22cf4e59a6791 = function(arg0, arg1, arg2, arg3) {
2642
2665
  console.error(arg0, arg1, arg2, arg3);
2643
2666
  };
2644
2667
 
2645
- exports.__wbg_fetch_74a3e84ebd2c9a0e = function(arg0) {
2668
+ exports.__wbg_fetch_013f0d75d4254327 = function(arg0) {
2646
2669
  const ret = fetch(arg0);
2647
2670
  return ret;
2648
2671
  };
2649
2672
 
2650
- exports.__wbg_fetch_cd778f2325984326 = function(arg0) {
2673
+ exports.__wbg_fetch_74a3e84ebd2c9a0e = function(arg0) {
2651
2674
  const ret = fetch(arg0);
2652
2675
  return ret;
2653
2676
  };