@synonymdev/pubky 0.4.0-rc3 → 0.4.2-rc1
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 +69 -32
- package/index.cjs +340 -318
- package/index.js +345 -323
- package/package.json +2 -2
- package/pubky.d.ts +49 -18
- package/pubky_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
JavaScript implementation of [Pubky](https://github.com/pubky/pubky-core) client.
|
|
4
4
|
|
|
5
5
|
## Table of Contents
|
|
6
|
+
|
|
6
7
|
- [Install](#install)
|
|
7
8
|
- [Getting Started](#getting-started)
|
|
8
9
|
- [API](#api)
|
|
@@ -21,7 +22,7 @@ For Nodejs, you need Node v20 or later.
|
|
|
21
22
|
## Getting started
|
|
22
23
|
|
|
23
24
|
```js
|
|
24
|
-
import { Client, Keypair, PublicKey } from
|
|
25
|
+
import { Client, Keypair, PublicKey } from "../index.js";
|
|
25
26
|
|
|
26
27
|
// Initialize Client with Pkarr relay(s).
|
|
27
28
|
let client = new Client();
|
|
@@ -30,9 +31,11 @@ let client = new Client();
|
|
|
30
31
|
let keypair = Keypair.random();
|
|
31
32
|
|
|
32
33
|
// Create a new account
|
|
33
|
-
let homeserver = PublicKey.from(
|
|
34
|
+
let homeserver = PublicKey.from(
|
|
35
|
+
"8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo"
|
|
36
|
+
);
|
|
34
37
|
|
|
35
|
-
await client.signup(keypair, homeserver)
|
|
38
|
+
await client.signup(keypair, homeserver, signup_token);
|
|
36
39
|
|
|
37
40
|
const publicKey = keypair.publicKey();
|
|
38
41
|
|
|
@@ -40,24 +43,24 @@ const publicKey = keypair.publicKey();
|
|
|
40
43
|
let url = `pubky://${publicKey.z32()}/pub/example.com/arbitrary`;
|
|
41
44
|
|
|
42
45
|
// Verify that you are signed in.
|
|
43
|
-
const session = await client.session(publicKey)
|
|
46
|
+
const session = await client.session(publicKey);
|
|
44
47
|
|
|
45
48
|
// PUT public data, by authorized client
|
|
46
|
-
await client.fetch(url, {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
await client.fetch(url, {
|
|
50
|
+
method: "PUT",
|
|
51
|
+
body: JSON.stringify({ foo: "bar" }),
|
|
52
|
+
credentials: "include",
|
|
50
53
|
});
|
|
51
54
|
|
|
52
55
|
// GET public data without signup or signin
|
|
53
56
|
{
|
|
54
|
-
|
|
57
|
+
const client = new Client();
|
|
55
58
|
|
|
56
|
-
|
|
59
|
+
let response = await client.fetch(url);
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
// Delete public data, by authorized client
|
|
60
|
-
await client.fetch(url, { method: "DELETE", credentials: "include "});
|
|
63
|
+
await client.fetch(url, { method: "DELETE", credentials: "include " });
|
|
61
64
|
```
|
|
62
65
|
|
|
63
66
|
## API
|
|
@@ -65,11 +68,13 @@ await client.fetch(url, { method: "DELETE", credentials: "include "});
|
|
|
65
68
|
### Client
|
|
66
69
|
|
|
67
70
|
#### constructor
|
|
71
|
+
|
|
68
72
|
```js
|
|
69
|
-
let client = new Client()
|
|
73
|
+
let client = new Client();
|
|
70
74
|
```
|
|
71
75
|
|
|
72
76
|
#### fetch
|
|
77
|
+
|
|
73
78
|
```js
|
|
74
79
|
let response = await client.fetch(url, opts);
|
|
75
80
|
```
|
|
@@ -77,37 +82,49 @@ let response = await client.fetch(url, opts);
|
|
|
77
82
|
Just like normal Fetch API, but it can handle `pubky://` urls and `http(s)://` urls with Pkarr domains.
|
|
78
83
|
|
|
79
84
|
#### signup
|
|
85
|
+
|
|
80
86
|
```js
|
|
81
|
-
await client.signup(keypair, homeserver)
|
|
87
|
+
await client.signup(keypair, homeserver, signup_token);
|
|
82
88
|
```
|
|
89
|
+
|
|
83
90
|
- keypair: An instance of [Keypair](#keypair).
|
|
84
91
|
- homeserver: An instance of [PublicKey](#publickey) representing the homeserver.
|
|
92
|
+
- signup_token: A homeserver could optionally ask for a valid signup token (aka, invitation code).
|
|
85
93
|
|
|
86
94
|
Returns:
|
|
95
|
+
|
|
87
96
|
- session: An instance of [Session](#session).
|
|
88
97
|
|
|
89
98
|
#### signin
|
|
99
|
+
|
|
90
100
|
```js
|
|
91
|
-
let session = await client.signin(keypair)
|
|
101
|
+
let session = await client.signin(keypair);
|
|
92
102
|
```
|
|
103
|
+
|
|
93
104
|
- keypair: An instance of [Keypair](#keypair).
|
|
94
105
|
|
|
95
106
|
Returns:
|
|
107
|
+
|
|
96
108
|
- An instance of [Session](#session).
|
|
97
109
|
|
|
98
110
|
#### signout
|
|
111
|
+
|
|
99
112
|
```js
|
|
100
|
-
await client.signout(publicKey)
|
|
113
|
+
await client.signout(publicKey);
|
|
101
114
|
```
|
|
115
|
+
|
|
102
116
|
- publicKey: An instance of [PublicKey](#publicKey).
|
|
103
117
|
|
|
104
118
|
#### authRequest
|
|
119
|
+
|
|
105
120
|
```js
|
|
106
|
-
let
|
|
121
|
+
let pubkyAuthRequest = client.authRequest(relay, capabilities);
|
|
122
|
+
|
|
123
|
+
let pubkyauthUrl = pubkyAuthRequest.url();
|
|
107
124
|
|
|
108
125
|
showQr(pubkyauthUrl);
|
|
109
126
|
|
|
110
|
-
let pubky = await
|
|
127
|
+
let pubky = await pubkyAuthRequest.response();
|
|
111
128
|
```
|
|
112
129
|
|
|
113
130
|
Sign in to a user's Homeserver, without access to their [Keypair](#keypair), nor even [PublicKey](#publickey),
|
|
@@ -116,30 +133,32 @@ instead request permissions (showing the user pubkyauthUrl), and await a Session
|
|
|
116
133
|
- relay: A URL to an [HTTP relay](https://httprelay.io/features/link/) endpoint.
|
|
117
134
|
- capabilities: A list of capabilities required for the app for example `/pub/pubky.app/:rw,/pub/example.com/:r`.
|
|
118
135
|
|
|
119
|
-
Returns:
|
|
120
|
-
- pubkyauthUrl: A url to show to the user to scan or paste into an Authenticator app holding the user [Keypair](#keypair)
|
|
121
|
-
- sessionPromise: A promise that resolves into a [PublicKey](#publickey) on success, which you can use in `client.session(pubky)` to resolve more information about the Session.
|
|
122
|
-
|
|
123
136
|
#### sendAuthToken
|
|
137
|
+
|
|
124
138
|
```js
|
|
125
139
|
await client.sendAuthToken(keypair, pubkyauthUrl);
|
|
126
140
|
```
|
|
141
|
+
|
|
127
142
|
Consenting to authentication or authorization according to the required capabilities in the `pubkyauthUrl` , and sign and send an auth token to the requester.
|
|
128
143
|
|
|
129
144
|
- keypair: An instance of [KeyPair](#keypair)
|
|
130
145
|
- pubkyauthUrl: A string `pubkyauth://` url
|
|
131
146
|
|
|
132
147
|
#### session {#session-method}
|
|
148
|
+
|
|
133
149
|
```js
|
|
134
|
-
let session = await client.session(publicKey)
|
|
150
|
+
let session = await client.session(publicKey);
|
|
135
151
|
```
|
|
152
|
+
|
|
136
153
|
- publicKey: An instance of [PublicKey](#publickey).
|
|
137
154
|
- Returns: A [Session](#session) object if signed in, or undefined if not.
|
|
138
155
|
|
|
139
156
|
### list
|
|
157
|
+
|
|
140
158
|
```js
|
|
141
|
-
let response = await client.list(url, cursor, reverse, limit)
|
|
159
|
+
let response = await client.list(url, cursor, reverse, limit);
|
|
142
160
|
```
|
|
161
|
+
|
|
143
162
|
- url: A string representing the Pubky URL. The path in that url is the prefix that you want to list files within.
|
|
144
163
|
- cursor: Usually the last URL from previous calls. List urls after/before (depending on `reverse`) the cursor.
|
|
145
164
|
- reverse: Whether or not return urls in reverse order.
|
|
@@ -149,29 +168,36 @@ let response = await client.list(url, cursor, reverse, limit)
|
|
|
149
168
|
### Keypair
|
|
150
169
|
|
|
151
170
|
#### random
|
|
171
|
+
|
|
152
172
|
```js
|
|
153
|
-
let keypair = Keypair.random()
|
|
173
|
+
let keypair = Keypair.random();
|
|
154
174
|
```
|
|
175
|
+
|
|
155
176
|
- Returns: A new random Keypair.
|
|
156
177
|
|
|
157
178
|
#### fromSecretKey
|
|
179
|
+
|
|
158
180
|
```js
|
|
159
|
-
let keypair = Keypair.fromSecretKey(secretKey)
|
|
181
|
+
let keypair = Keypair.fromSecretKey(secretKey);
|
|
160
182
|
```
|
|
183
|
+
|
|
161
184
|
- secretKey: A 32 bytes Uint8array.
|
|
162
185
|
- Returns: A new Keypair.
|
|
163
186
|
|
|
164
|
-
|
|
165
187
|
#### publicKey {#publickey-method}
|
|
188
|
+
|
|
166
189
|
```js
|
|
167
|
-
let publicKey = keypair.publicKey()
|
|
190
|
+
let publicKey = keypair.publicKey();
|
|
168
191
|
```
|
|
192
|
+
|
|
169
193
|
- Returns: The [PublicKey](#publickey) associated with the Keypair.
|
|
170
194
|
|
|
171
195
|
#### secretKey
|
|
196
|
+
|
|
172
197
|
```js
|
|
173
|
-
let secretKey = keypair.secretKey()
|
|
198
|
+
let secretKey = keypair.secretKey();
|
|
174
199
|
```
|
|
200
|
+
|
|
175
201
|
- Returns: The Uint8array secret key associated with the Keypair.
|
|
176
202
|
|
|
177
203
|
### PublicKey
|
|
@@ -181,43 +207,54 @@ let secretKey = keypair.secretKey()
|
|
|
181
207
|
```js
|
|
182
208
|
let publicKey = PublicKey.from(string);
|
|
183
209
|
```
|
|
210
|
+
|
|
184
211
|
- string: A string representing the public key.
|
|
185
212
|
- Returns: A new PublicKey instance.
|
|
186
213
|
|
|
187
214
|
#### z32
|
|
215
|
+
|
|
188
216
|
```js
|
|
189
217
|
let pubky = publicKey.z32();
|
|
190
218
|
```
|
|
219
|
+
|
|
191
220
|
Returns: The z-base-32 encoded string representation of the PublicKey.
|
|
192
221
|
|
|
193
|
-
### Session
|
|
222
|
+
### Session
|
|
194
223
|
|
|
195
224
|
#### pubky
|
|
225
|
+
|
|
196
226
|
```js
|
|
197
227
|
let pubky = session.pubky();
|
|
198
228
|
```
|
|
229
|
+
|
|
199
230
|
Returns an instance of [PublicKey](#publickey)
|
|
200
231
|
|
|
201
232
|
#### capabilities
|
|
233
|
+
|
|
202
234
|
```js
|
|
203
235
|
let capabilities = session.capabilities();
|
|
204
236
|
```
|
|
237
|
+
|
|
205
238
|
Returns an array of capabilities, for example `["/pub/pubky.app/:rw"]`
|
|
206
239
|
|
|
207
240
|
### Helper functions
|
|
208
241
|
|
|
209
242
|
#### createRecoveryFile
|
|
243
|
+
|
|
210
244
|
```js
|
|
211
|
-
let recoveryFile = createRecoveryFile(keypair, passphrase)
|
|
245
|
+
let recoveryFile = createRecoveryFile(keypair, passphrase);
|
|
212
246
|
```
|
|
247
|
+
|
|
213
248
|
- keypair: An instance of [Keypair](#keypair).
|
|
214
249
|
- passphrase: A utf-8 string [passphrase](https://www.useapassphrase.com/).
|
|
215
250
|
- Returns: A recovery file with a spec line and an encrypted secret key.
|
|
216
251
|
|
|
217
252
|
#### createRecoveryFile
|
|
253
|
+
|
|
218
254
|
```js
|
|
219
|
-
let keypair = decryptRecoveryfile(recoveryFile, passphrase)
|
|
255
|
+
let keypair = decryptRecoveryfile(recoveryFile, passphrase);
|
|
220
256
|
```
|
|
257
|
+
|
|
221
258
|
- recoveryFile: An instance of Uint8Array containing the recovery file blob.
|
|
222
259
|
- passphrase: A utf-8 string [passphrase](https://www.useapassphrase.com/).
|
|
223
260
|
- Returns: An instance of [Keypair](#keypair).
|
|
@@ -248,7 +285,7 @@ npm run testnet
|
|
|
248
285
|
Use the logged addresses as inputs to `Client`
|
|
249
286
|
|
|
250
287
|
```js
|
|
251
|
-
import { Client } from
|
|
288
|
+
import { Client } from "../index.js";
|
|
252
289
|
|
|
253
290
|
const client = Client().testnet();
|
|
254
291
|
```
|