@did-plc/lib 0.0.3 → 1.0.0-next.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 +64 -1
- package/dist/client.d.ts +64 -29
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +188 -0
- package/dist/client.js.map +1 -0
- package/dist/data.d.ts +4 -2
- package/dist/data.d.ts.map +1 -0
- package/dist/data.js +102 -0
- package/dist/data.js.map +1 -0
- package/dist/document.d.ts +2 -1
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +67 -0
- package/dist/document.js.map +1 -0
- package/dist/error.d.ts +1 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +66 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +7 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -33173
- package/dist/index.js.map +1 -7
- package/dist/operations.d.ts +19 -27
- package/dist/operations.d.ts.map +1 -0
- package/dist/operations.js +202 -0
- package/dist/operations.js.map +1 -0
- package/dist/types.d.ts +517 -223
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +108 -0
- package/dist/types.js.map +1 -0
- package/package.json +40 -24
- package/src/client.ts +283 -72
- package/src/data.ts +31 -15
- package/src/document.ts +31 -18
- package/src/error.ts +14 -4
- package/src/index.ts +6 -6
- package/src/operations.ts +76 -71
- package/src/types.ts +46 -27
- package/build.js +0 -26
- package/jest.config.js +0 -6
- package/tests/compatibility.test.ts +0 -89
- package/tests/data.test.ts +0 -214
- package/tests/document.test.ts +0 -81
- package/tests/recovery.test.ts +0 -188
- package/tsconfig.build.json +0 -4
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -10
- package/update-pkg.js +0 -14
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAA;AACxB,OAAO,K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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAaf,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
import * as mf from 'multiformats/cid';
|
|
3
|
+
const cid = z
|
|
4
|
+
.any()
|
|
5
|
+
.refine((obj) => mf.CID.asCID(obj) !== null, {
|
|
6
|
+
message: 'Not a CID',
|
|
7
|
+
})
|
|
8
|
+
.transform((obj) => mf.CID.asCID(obj));
|
|
9
|
+
const service = z
|
|
10
|
+
.object({
|
|
11
|
+
type: z.string(),
|
|
12
|
+
endpoint: z.string(),
|
|
13
|
+
})
|
|
14
|
+
.strict();
|
|
15
|
+
const documentData = z.object({
|
|
16
|
+
did: z.string(),
|
|
17
|
+
rotationKeys: z.array(z.string()),
|
|
18
|
+
verificationMethods: z.record(z.string()),
|
|
19
|
+
alsoKnownAs: z.array(z.string()),
|
|
20
|
+
services: z.record(service),
|
|
21
|
+
});
|
|
22
|
+
const unsignedCreateOpV1 = z
|
|
23
|
+
.object({
|
|
24
|
+
type: z.literal('create'),
|
|
25
|
+
signingKey: z.string(),
|
|
26
|
+
recoveryKey: z.string(),
|
|
27
|
+
handle: z.string(),
|
|
28
|
+
service: z.string(),
|
|
29
|
+
prev: z.null(),
|
|
30
|
+
})
|
|
31
|
+
.strict();
|
|
32
|
+
const createOpV1 = unsignedCreateOpV1.extend({ sig: z.string() }).strict();
|
|
33
|
+
const unsignedOperation = z
|
|
34
|
+
.object({
|
|
35
|
+
type: z.literal('plc_operation'),
|
|
36
|
+
rotationKeys: z.array(z.string()),
|
|
37
|
+
verificationMethods: z.record(z.string()),
|
|
38
|
+
alsoKnownAs: z.array(z.string()),
|
|
39
|
+
services: z.record(service),
|
|
40
|
+
prev: z.string().nullable(),
|
|
41
|
+
})
|
|
42
|
+
.strict();
|
|
43
|
+
const operation = unsignedOperation.extend({ sig: z.string() }).strict();
|
|
44
|
+
const unsignedTombstone = z
|
|
45
|
+
.object({
|
|
46
|
+
type: z.literal('plc_tombstone'),
|
|
47
|
+
prev: z.string(),
|
|
48
|
+
})
|
|
49
|
+
.strict();
|
|
50
|
+
const tombstone = unsignedTombstone.extend({ sig: z.string() }).strict();
|
|
51
|
+
const opOrTombstone = z.union([operation, tombstone]);
|
|
52
|
+
const compatibleOp = z.union([createOpV1, operation]);
|
|
53
|
+
const compatibleOpOrTombstone = z.union([createOpV1, operation, tombstone]);
|
|
54
|
+
export const indexedOperation = z.object({
|
|
55
|
+
did: z.string(),
|
|
56
|
+
operation: compatibleOpOrTombstone,
|
|
57
|
+
cid: cid,
|
|
58
|
+
nullified: z.boolean(),
|
|
59
|
+
createdAt: z.date(),
|
|
60
|
+
});
|
|
61
|
+
export const exportedOp = z.object({
|
|
62
|
+
did: z.string(),
|
|
63
|
+
operation: compatibleOpOrTombstone,
|
|
64
|
+
cid: z.string(),
|
|
65
|
+
nullified: z.boolean(),
|
|
66
|
+
createdAt: z.string(),
|
|
67
|
+
});
|
|
68
|
+
export const exportedOpWithSeq = z.object({
|
|
69
|
+
did: z.string(),
|
|
70
|
+
operation: compatibleOpOrTombstone,
|
|
71
|
+
cid: z.string(),
|
|
72
|
+
createdAt: z.string(),
|
|
73
|
+
seq: z.number(),
|
|
74
|
+
type: z.literal('sequenced_op'),
|
|
75
|
+
});
|
|
76
|
+
export const didDocVerificationMethod = z.object({
|
|
77
|
+
id: z.string(),
|
|
78
|
+
type: z.string(),
|
|
79
|
+
controller: z.string(),
|
|
80
|
+
publicKeyMultibase: z.string(),
|
|
81
|
+
});
|
|
82
|
+
export const didDocService = z.object({
|
|
83
|
+
id: z.string(),
|
|
84
|
+
type: z.string(),
|
|
85
|
+
serviceEndpoint: z.string(),
|
|
86
|
+
});
|
|
87
|
+
export const didDocument = z.object({
|
|
88
|
+
'@context': z.array(z.string()),
|
|
89
|
+
id: z.string(),
|
|
90
|
+
alsoKnownAs: z.array(z.string()),
|
|
91
|
+
verificationMethod: z.array(didDocVerificationMethod),
|
|
92
|
+
service: z.array(didDocService),
|
|
93
|
+
});
|
|
94
|
+
export const def = {
|
|
95
|
+
documentData,
|
|
96
|
+
createOpV1,
|
|
97
|
+
unsignedOperation,
|
|
98
|
+
operation,
|
|
99
|
+
tombstone,
|
|
100
|
+
opOrTombstone,
|
|
101
|
+
compatibleOp,
|
|
102
|
+
compatibleOpOrTombstone,
|
|
103
|
+
indexedOperation,
|
|
104
|
+
exportedOp,
|
|
105
|
+
exportedOpWithSeq,
|
|
106
|
+
didDocument,
|
|
107
|
+
};
|
|
108
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAA;AACxB,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAEtC,MAAM,GAAG,GAAG,CAAC;KACV,GAAG,EAAE;KACL,MAAM,CAAC,CAAC,GAAY,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE;IACpD,OAAO,EAAE,WAAW;CACrB,CAAC;KACD,SAAS,CAAC,CAAC,GAAY,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAW,CAAC,CAAA;AAE3D,MAAM,OAAO,GAAG,CAAC;KACd,MAAM,CAAC;IACN,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;CACrB,CAAC;KACD,MAAM,EAAE,CAAA;AAEX,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IAC5B,GAAG,EAAE,CAAC,CAAC,MA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AAG,GAAG;IACjB,YAAY;IACZ,UAAU;IACV,iBAAiB;IACjB,SAAS;IACT,SAAS;IACT,aAAa;IACb,YAAY;IACZ,uBAAuB;IACvB,gBAAgB;IAChB,UAAU;IACV,iBAAiB;IACjB,WAAW;CACZ,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,33 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-plc/lib",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0-next.0",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"description": "DID PLC Typescript Client Library",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"did-plc",
|
|
9
|
+
"did",
|
|
10
|
+
"atproto"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://web.plc.directory",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/did-method-plc/did-method-plc",
|
|
16
|
+
"directory": "packages/lib"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"src"
|
|
21
|
+
],
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"types": "dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22.18"
|
|
32
|
+
},
|
|
6
33
|
"scripts": {
|
|
7
|
-
"test": "
|
|
8
|
-
"
|
|
9
|
-
"prettier:fix": "prettier --write src/",
|
|
10
|
-
"lint": "eslint . --ext .ts,.tsx",
|
|
11
|
-
"lint:fix": "yarn lint --fix",
|
|
12
|
-
"verify": "run-p prettier lint",
|
|
13
|
-
"verify:fix": "yarn prettier:fix && yarn lint:fix",
|
|
14
|
-
"build": "node ./build.js",
|
|
15
|
-
"postbuild": "tsc --build tsconfig.build.json",
|
|
16
|
-
"update-main-to-dist": "node ./update-pkg.js --update-main-to-dist",
|
|
17
|
-
"update-main-to-src": "node ./update-pkg.js --update-main-to-src",
|
|
18
|
-
"prepublish": "npm run update-main-to-dist",
|
|
19
|
-
"postpublish": "npm run update-main-to-src"
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"build": "tsc --build tsconfig.build.json"
|
|
20
36
|
},
|
|
21
37
|
"dependencies": {
|
|
22
|
-
"@atproto/common": "0.
|
|
23
|
-
"@atproto/crypto": "0.
|
|
24
|
-
"@ipld/dag-cbor": "^
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"zod": "^3.14.2"
|
|
38
|
+
"@atproto/common": "0.8.1",
|
|
39
|
+
"@atproto/crypto": "0.5.4",
|
|
40
|
+
"@ipld/dag-cbor": "^9.0.0",
|
|
41
|
+
"multiformats": "^13.0.0",
|
|
42
|
+
"uint8arrays": "^5.0.0",
|
|
43
|
+
"zod": "^3.23.8"
|
|
29
44
|
},
|
|
30
45
|
"devDependencies": {
|
|
31
|
-
"eslint-plugin-prettier": "^
|
|
32
|
-
}
|
|
46
|
+
"eslint-plugin-prettier": "^5.1.3"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "99997358abe4e2d5e03ac9f53d7ce4426665597f"
|
|
33
49
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,48 +1,135 @@
|
|
|
1
1
|
import { check, cidForCbor } from '@atproto/common'
|
|
2
|
-
import { Keypair } from '@atproto/crypto'
|
|
3
|
-
import
|
|
2
|
+
import type { Keypair } from '@atproto/crypto'
|
|
3
|
+
import type { AtprotoOpOpts } from './operations.js'
|
|
4
4
|
import {
|
|
5
5
|
atprotoOp,
|
|
6
6
|
createUpdateOp,
|
|
7
7
|
didForCreateOp,
|
|
8
8
|
tombstoneOp,
|
|
9
|
-
|
|
9
|
+
updateAtprotoKemKeyOp,
|
|
10
|
+
updateAtprotoSigningKeyOp,
|
|
10
11
|
updateHandleOp,
|
|
11
12
|
updatePdsOp,
|
|
12
13
|
updateRotationKeysOp,
|
|
13
|
-
} from './operations'
|
|
14
|
-
import * as t from './types'
|
|
14
|
+
} from './operations.js'
|
|
15
|
+
import * as t from './types.js'
|
|
16
|
+
|
|
17
|
+
export type RequestOptions = {
|
|
18
|
+
/** Request headers, overriding the client's defaults. */
|
|
19
|
+
headers?: HeadersInit
|
|
20
|
+
/**
|
|
21
|
+
* Aborts the request(s) made by the call. An aborted call rejects with the
|
|
22
|
+
* signal's `reason` (an `AbortError` DOMException unless another reason was
|
|
23
|
+
* given to `abort()`), not a `PlcClientError`.
|
|
24
|
+
*/
|
|
25
|
+
signal?: AbortSignal
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type MutationOptions = RequestOptions & {
|
|
29
|
+
/** Match any existing head instead of requiring the operation's `prev`. */
|
|
30
|
+
ignorePrevMismatch?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ClientOptions = {
|
|
34
|
+
/** Default headers sent with every request. */
|
|
35
|
+
headers?: HeadersInit
|
|
36
|
+
/**
|
|
37
|
+
* The `fetch` implementation used for requests. Defaults to the global
|
|
38
|
+
* `fetch`, resolved at call time. Rejections from it (network failures,
|
|
39
|
+
* custom errors) propagate to the caller unchanged.
|
|
40
|
+
*/
|
|
41
|
+
fetch?: typeof globalThis.fetch
|
|
42
|
+
}
|
|
15
43
|
|
|
16
44
|
export class Client {
|
|
17
|
-
|
|
45
|
+
private customFetch: typeof globalThis.fetch | undefined
|
|
46
|
+
private headers: Headers
|
|
18
47
|
|
|
19
|
-
|
|
48
|
+
constructor(
|
|
49
|
+
public url: string,
|
|
50
|
+
opts?: ClientOptions,
|
|
51
|
+
) {
|
|
52
|
+
this.customFetch = opts?.fetch
|
|
53
|
+
this.headers = new Headers(opts?.headers)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Performs the request and reads a 2xx response with `read`. Failed requests
|
|
58
|
+
* reject with the fetch error as-is (not a `PlcClientError`); non-2xx
|
|
59
|
+
* responses reject with a `PlcClientError`. If the signal aborts at any
|
|
60
|
+
* point, including while a body is being read, the call rejects with the
|
|
61
|
+
* signal's reason.
|
|
62
|
+
*/
|
|
63
|
+
private async request<T>(
|
|
64
|
+
url: string,
|
|
65
|
+
init: RequestInit,
|
|
66
|
+
read: (res: Response) => Promise<T>,
|
|
67
|
+
opts?: RequestOptions,
|
|
68
|
+
): Promise<T> {
|
|
69
|
+
const signal = opts?.signal
|
|
70
|
+
const headers = new Headers(this.headers)
|
|
71
|
+
new Headers(opts?.headers).forEach((value, name) =>
|
|
72
|
+
headers.set(name, value),
|
|
73
|
+
)
|
|
74
|
+
// Protocol headers take precedence over caller-supplied headers.
|
|
75
|
+
new Headers(init.headers).forEach((value, name) => headers.set(name, value))
|
|
76
|
+
// a local variable, so the default fetch is called unbound
|
|
77
|
+
const fetchFn = this.customFetch ?? globalThis.fetch
|
|
20
78
|
try {
|
|
21
|
-
const res = await
|
|
22
|
-
|
|
79
|
+
const res = await fetchFn(url, { ...init, headers, signal })
|
|
80
|
+
if (!res.ok) throw await errorFromResponse(res)
|
|
81
|
+
return await read(res)
|
|
23
82
|
} catch (err) {
|
|
24
|
-
if (!
|
|
25
|
-
throw
|
|
83
|
+
if (signal?.aborted && !(err instanceof PlcClientError)) {
|
|
84
|
+
throw signal.reason
|
|
26
85
|
}
|
|
27
|
-
throw
|
|
86
|
+
throw err
|
|
28
87
|
}
|
|
29
88
|
}
|
|
30
89
|
|
|
31
|
-
async
|
|
32
|
-
return await this.
|
|
90
|
+
private async makeGetReq<T>(url: string, opts?: RequestOptions): Promise<T> {
|
|
91
|
+
return await this.request(
|
|
92
|
+
url,
|
|
93
|
+
{ method: 'GET' },
|
|
94
|
+
async (res) => (await res.json()) as T,
|
|
95
|
+
opts,
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async getDocument(
|
|
100
|
+
did: string,
|
|
101
|
+
opts?: RequestOptions,
|
|
102
|
+
): Promise<t.DidDocument> {
|
|
103
|
+
return await this.makeGetReq(`${this.url}/${encodeURIComponent(did)}`, opts)
|
|
33
104
|
}
|
|
34
105
|
|
|
35
|
-
async getDocumentData(
|
|
36
|
-
|
|
106
|
+
async getDocumentData(
|
|
107
|
+
did: string,
|
|
108
|
+
opts?: RequestOptions,
|
|
109
|
+
): Promise<t.DocumentData> {
|
|
110
|
+
return await this.makeGetReq(
|
|
111
|
+
`${this.url}/${encodeURIComponent(did)}/data`,
|
|
112
|
+
opts,
|
|
113
|
+
)
|
|
37
114
|
}
|
|
38
115
|
|
|
39
|
-
async getOperationLog(
|
|
40
|
-
|
|
116
|
+
async getOperationLog(
|
|
117
|
+
did: string,
|
|
118
|
+
opts?: RequestOptions,
|
|
119
|
+
): Promise<t.CompatibleOpOrTombstone[]> {
|
|
120
|
+
return await this.makeGetReq(
|
|
121
|
+
`${this.url}/${encodeURIComponent(did)}/log`,
|
|
122
|
+
opts,
|
|
123
|
+
)
|
|
41
124
|
}
|
|
42
125
|
|
|
43
|
-
async getAuditableLog(
|
|
126
|
+
async getAuditableLog(
|
|
127
|
+
did: string,
|
|
128
|
+
opts?: RequestOptions,
|
|
129
|
+
): Promise<t.ExportedOp[]> {
|
|
44
130
|
return await this.makeGetReq(
|
|
45
131
|
`${this.url}/${encodeURIComponent(did)}/log/audit`,
|
|
132
|
+
opts,
|
|
46
133
|
)
|
|
47
134
|
}
|
|
48
135
|
|
|
@@ -50,53 +137,74 @@ export class Client {
|
|
|
50
137
|
return `${this.url}/${encodeURIComponent(did)}`
|
|
51
138
|
}
|
|
52
139
|
|
|
53
|
-
async getLastOp(
|
|
140
|
+
async getLastOp(
|
|
141
|
+
did: string,
|
|
142
|
+
opts?: RequestOptions,
|
|
143
|
+
): Promise<t.CompatibleOpOrTombstone> {
|
|
54
144
|
return await this.makeGetReq(
|
|
55
145
|
`${this.url}/${encodeURIComponent(did)}/log/last`,
|
|
146
|
+
opts,
|
|
56
147
|
)
|
|
57
148
|
}
|
|
58
149
|
|
|
59
|
-
async sendOperation(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
150
|
+
async sendOperation(
|
|
151
|
+
did: string,
|
|
152
|
+
op: t.OpOrTombstone,
|
|
153
|
+
opts?: MutationOptions,
|
|
154
|
+
): Promise<void> {
|
|
155
|
+
const headers = new Headers({ 'content-type': 'application/json' })
|
|
156
|
+
if (op.prev !== null) {
|
|
157
|
+
headers.set('if-match', opts?.ignorePrevMismatch ? '*' : `"${op.prev}"`)
|
|
67
158
|
}
|
|
159
|
+
await this.request(
|
|
160
|
+
this.postOpUrl(did),
|
|
161
|
+
{
|
|
162
|
+
method: 'POST',
|
|
163
|
+
headers,
|
|
164
|
+
body: JSON.stringify(op),
|
|
165
|
+
},
|
|
166
|
+
// release the connection without buffering an unused body
|
|
167
|
+
async (res) => {
|
|
168
|
+
await res.body?.cancel()
|
|
169
|
+
},
|
|
170
|
+
opts,
|
|
171
|
+
)
|
|
68
172
|
}
|
|
69
173
|
|
|
70
|
-
async export(
|
|
174
|
+
async export(
|
|
175
|
+
after?: number,
|
|
176
|
+
count?: number,
|
|
177
|
+
opts?: RequestOptions,
|
|
178
|
+
): Promise<t.ExportedOpWithSeq[]> {
|
|
71
179
|
const url = new URL(`${this.url}/export`)
|
|
72
|
-
|
|
73
|
-
url.searchParams.append('after', after)
|
|
74
|
-
}
|
|
180
|
+
url.searchParams.append('after', (after || 0).toString(10))
|
|
75
181
|
if (count !== undefined) {
|
|
76
182
|
url.searchParams.append('count', count.toString(10))
|
|
77
183
|
}
|
|
78
|
-
const
|
|
79
|
-
|
|
184
|
+
const data = await this.request(
|
|
185
|
+
url.toString(),
|
|
186
|
+
{ method: 'GET' },
|
|
187
|
+
(res) => res.text(),
|
|
188
|
+
opts,
|
|
189
|
+
)
|
|
190
|
+
const lines = data.split('\n').filter((l) => l.length > 0)
|
|
80
191
|
return lines.map((l) => JSON.parse(l))
|
|
81
192
|
}
|
|
82
193
|
|
|
83
|
-
async createDid(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
rotationKeys: string[]
|
|
88
|
-
signer: Keypair
|
|
89
|
-
}): Promise<string> {
|
|
194
|
+
async createDid(
|
|
195
|
+
opts: AtprotoOpOpts & { signer: Keypair },
|
|
196
|
+
reqOpts?: RequestOptions,
|
|
197
|
+
): Promise<string> {
|
|
90
198
|
const op = await atprotoOp({ ...opts, prev: null })
|
|
91
199
|
const did = await didForCreateOp(op)
|
|
92
|
-
await this.sendOperation(did, op)
|
|
200
|
+
await this.sendOperation(did, op, reqOpts)
|
|
93
201
|
return did
|
|
94
202
|
}
|
|
95
203
|
|
|
96
|
-
async ensureLastOp(did) {
|
|
97
|
-
const lastOp = await this.getLastOp(did)
|
|
204
|
+
async ensureLastOp(did: string, opts?: RequestOptions) {
|
|
205
|
+
const lastOp = await this.getLastOp(did, opts)
|
|
98
206
|
if (check.is(lastOp, t.def.tombstone)) {
|
|
99
|
-
throw new
|
|
207
|
+
throw new TombstonedDidError(did)
|
|
100
208
|
}
|
|
101
209
|
return lastOp
|
|
102
210
|
}
|
|
@@ -105,64 +213,167 @@ export class Client {
|
|
|
105
213
|
did: string,
|
|
106
214
|
signer: Keypair,
|
|
107
215
|
fn: (lastOp: t.UnsignedOperation) => Omit<t.UnsignedOperation, 'prev'>,
|
|
216
|
+
opts?: MutationOptions,
|
|
108
217
|
) {
|
|
109
|
-
const lastOp = await this.ensureLastOp(did)
|
|
218
|
+
const lastOp = await this.ensureLastOp(did, opts)
|
|
110
219
|
const op = await createUpdateOp(lastOp, signer, fn)
|
|
111
|
-
await this.sendOperation(did, op)
|
|
220
|
+
await this.sendOperation(did, op, opts)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async updateAtprotoSigningKey(
|
|
224
|
+
did: string,
|
|
225
|
+
signer: Keypair,
|
|
226
|
+
signingKey: string,
|
|
227
|
+
opts?: MutationOptions,
|
|
228
|
+
) {
|
|
229
|
+
const lastOp = await this.ensureLastOp(did, opts)
|
|
230
|
+
const op = await updateAtprotoSigningKeyOp(lastOp, signer, signingKey)
|
|
231
|
+
await this.sendOperation(did, op, opts)
|
|
112
232
|
}
|
|
113
233
|
|
|
114
|
-
async
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
234
|
+
async updateAtprotoKemKey(
|
|
235
|
+
did: string,
|
|
236
|
+
signer: Keypair,
|
|
237
|
+
kemKey: string,
|
|
238
|
+
opts?: MutationOptions,
|
|
239
|
+
) {
|
|
240
|
+
const lastOp = await this.ensureLastOp(did, opts)
|
|
241
|
+
const op = await updateAtprotoKemKeyOp(lastOp, signer, kemKey)
|
|
242
|
+
await this.sendOperation(did, op, opts)
|
|
118
243
|
}
|
|
119
244
|
|
|
120
|
-
async updateHandle(
|
|
121
|
-
|
|
245
|
+
async updateHandle(
|
|
246
|
+
did: string,
|
|
247
|
+
signer: Keypair,
|
|
248
|
+
handle: string,
|
|
249
|
+
opts?: MutationOptions,
|
|
250
|
+
) {
|
|
251
|
+
const lastOp = await this.ensureLastOp(did, opts)
|
|
122
252
|
const op = await updateHandleOp(lastOp, signer, handle)
|
|
123
|
-
await this.sendOperation(did, op)
|
|
253
|
+
await this.sendOperation(did, op, opts)
|
|
124
254
|
}
|
|
125
255
|
|
|
126
|
-
async updatePds(
|
|
127
|
-
|
|
256
|
+
async updatePds(
|
|
257
|
+
did: string,
|
|
258
|
+
signer: Keypair,
|
|
259
|
+
endpoint: string,
|
|
260
|
+
opts?: MutationOptions,
|
|
261
|
+
) {
|
|
262
|
+
const lastOp = await this.ensureLastOp(did, opts)
|
|
128
263
|
const op = await updatePdsOp(lastOp, signer, endpoint)
|
|
129
|
-
await this.sendOperation(did, op)
|
|
264
|
+
await this.sendOperation(did, op, opts)
|
|
130
265
|
}
|
|
131
266
|
|
|
132
|
-
async updateRotationKeys(
|
|
133
|
-
|
|
267
|
+
async updateRotationKeys(
|
|
268
|
+
did: string,
|
|
269
|
+
signer: Keypair,
|
|
270
|
+
keys: string[],
|
|
271
|
+
opts?: MutationOptions,
|
|
272
|
+
) {
|
|
273
|
+
const lastOp = await this.ensureLastOp(did, opts)
|
|
134
274
|
const op = await updateRotationKeysOp(lastOp, signer, keys)
|
|
135
|
-
await this.sendOperation(did, op)
|
|
275
|
+
await this.sendOperation(did, op, opts)
|
|
136
276
|
}
|
|
137
277
|
|
|
138
|
-
async tombstone(did: string, signer: Keypair) {
|
|
139
|
-
const lastOp = await this.ensureLastOp(did)
|
|
278
|
+
async tombstone(did: string, signer: Keypair, opts?: MutationOptions) {
|
|
279
|
+
const lastOp = await this.ensureLastOp(did, opts)
|
|
140
280
|
const prev = await cidForCbor(lastOp)
|
|
141
281
|
const op = await tombstoneOp(prev, signer)
|
|
142
|
-
await this.sendOperation(did, op)
|
|
282
|
+
await this.sendOperation(did, op, opts)
|
|
143
283
|
}
|
|
144
284
|
|
|
145
|
-
|
|
146
|
-
|
|
285
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
286
|
+
async health(opts?: RequestOptions): Promise<any> {
|
|
287
|
+
return await this.makeGetReq(`${this.url}/_health`, opts)
|
|
147
288
|
}
|
|
148
289
|
}
|
|
149
290
|
|
|
291
|
+
/**
|
|
292
|
+
* A non-2xx response from the PLC directory. `status` is the HTTP status of
|
|
293
|
+
* the response. Network failures are not wrapped: they reject with the error
|
|
294
|
+
* from `fetch`.
|
|
295
|
+
*/
|
|
150
296
|
export class PlcClientError extends Error {
|
|
297
|
+
override name: string = 'PlcClientError'
|
|
298
|
+
/** Response headers. */
|
|
299
|
+
public headers: Headers
|
|
300
|
+
|
|
151
301
|
constructor(
|
|
152
302
|
public status: number,
|
|
153
303
|
public data: unknown,
|
|
154
304
|
public message: string,
|
|
305
|
+
opts?: { headers?: Headers; cause?: unknown },
|
|
155
306
|
) {
|
|
156
|
-
super(message)
|
|
307
|
+
super(message, { cause: opts?.cause })
|
|
308
|
+
this.headers = opts?.headers ?? new Headers()
|
|
157
309
|
}
|
|
310
|
+
}
|
|
158
311
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
312
|
+
/**
|
|
313
|
+
* The directory rejected the operation with 409: another operation for the
|
|
314
|
+
* DID was committed concurrently, so the op's `prev` is no longer the latest.
|
|
315
|
+
* Re-reading the last op and re-signing is the usual remedy.
|
|
316
|
+
*/
|
|
317
|
+
export class PlcConflictError extends PlcClientError {
|
|
318
|
+
override name: string = 'PlcConflictError'
|
|
319
|
+
|
|
320
|
+
constructor(
|
|
321
|
+
data: unknown,
|
|
322
|
+
message: string,
|
|
323
|
+
opts?: { headers?: Headers; cause?: unknown },
|
|
324
|
+
) {
|
|
325
|
+
super(409, data, message, opts)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* The directory rejected the request with 412 because `If-Match` did not
|
|
331
|
+
* match the DID's latest operation, or the DID does not exist.
|
|
332
|
+
*/
|
|
333
|
+
export class PlcPrevMismatchError extends PlcClientError {
|
|
334
|
+
override name: string = 'PlcPrevMismatchError'
|
|
335
|
+
|
|
336
|
+
constructor(
|
|
337
|
+
data: unknown,
|
|
338
|
+
message: string,
|
|
339
|
+
opts?: { headers?: Headers; cause?: unknown },
|
|
340
|
+
) {
|
|
341
|
+
super(412, data, message, opts)
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** The DID's latest operation is a tombstone, so no op can follow it. */
|
|
346
|
+
export class TombstonedDidError extends Error {
|
|
347
|
+
override name: string = 'TombstonedDidError'
|
|
348
|
+
|
|
349
|
+
constructor(public did: string) {
|
|
350
|
+
super(`Cannot apply op to tombstone: ${did}`)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const errorFromResponse = async (res: Response): Promise<PlcClientError> => {
|
|
355
|
+
const headers = res.headers
|
|
356
|
+
const text = await res.text()
|
|
357
|
+
let data: unknown = text
|
|
358
|
+
if (headers.get('content-type')?.includes('json')) {
|
|
359
|
+
try {
|
|
360
|
+
data = JSON.parse(text)
|
|
361
|
+
} catch {
|
|
362
|
+
// keep the raw text
|
|
363
|
+
}
|
|
165
364
|
}
|
|
365
|
+
const plcMessage =
|
|
366
|
+
data && typeof data === 'object' && 'message' in data
|
|
367
|
+
? data.message
|
|
368
|
+
: undefined
|
|
369
|
+
const message =
|
|
370
|
+
typeof plcMessage === 'string' && plcMessage
|
|
371
|
+
? plcMessage
|
|
372
|
+
: `Request failed with status code ${res.status}`
|
|
373
|
+
const opts = { headers }
|
|
374
|
+
if (res.status === 409) return new PlcConflictError(data, message, opts)
|
|
375
|
+
if (res.status === 412) return new PlcPrevMismatchError(data, message, opts)
|
|
376
|
+
return new PlcClientError(res.status, data, message, opts)
|
|
166
377
|
}
|
|
167
378
|
|
|
168
379
|
export default Client
|