@did-plc/lib 0.0.4 → 1.0.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/LICENSE +5 -0
- package/README.md +64 -1
- package/dist/client.d.ts +79 -25
- 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 +3 -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 -33182
- package/dist/index.js.map +1 -7
- package/dist/operations.d.ts +29 -20
- 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 -25
- package/src/client.ts +283 -72
- package/src/data.ts +15 -12
- package/src/document.ts +31 -18
- package/src/error.ts +14 -4
- package/src/index.ts +6 -6
- package/src/operations.ts +124 -111
- 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
package/src/data.ts
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
1
|
import { CID } from 'multiformats/cid'
|
|
2
2
|
import { check, cidForCbor, HOUR } from '@atproto/common'
|
|
3
|
-
import * as t from './types'
|
|
3
|
+
import * as t from './types.js'
|
|
4
4
|
import {
|
|
5
5
|
assureValidCreationOp,
|
|
6
|
-
assureValidOp,
|
|
7
6
|
assureValidSig,
|
|
8
7
|
normalizeOp,
|
|
9
|
-
} from './operations'
|
|
8
|
+
} from './operations.js'
|
|
10
9
|
import {
|
|
11
10
|
ImproperOperationError,
|
|
12
11
|
LateRecoveryError,
|
|
13
12
|
MisorderedOperationError,
|
|
14
|
-
} from './error'
|
|
13
|
+
} from './error.js'
|
|
15
14
|
|
|
16
15
|
export const assureValidNextOp = async (
|
|
17
16
|
did: string,
|
|
18
17
|
ops: t.IndexedOperation[],
|
|
19
18
|
proposed: t.CompatibleOpOrTombstone,
|
|
19
|
+
proposedDate: Date,
|
|
20
20
|
): Promise<{ nullified: CID[]; prev: CID | null }> => {
|
|
21
|
-
if (check.is(proposed, t.def.createOpV1)) {
|
|
22
|
-
const normalized = normalizeOp(proposed)
|
|
23
|
-
await assureValidOp(normalized)
|
|
24
|
-
} else {
|
|
25
|
-
await assureValidOp(proposed)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
21
|
// special case if account creation
|
|
29
22
|
if (ops.length === 0) {
|
|
30
23
|
await assureValidCreationOp(did, proposed)
|
|
@@ -70,10 +63,20 @@ export const assureValidNextOp = async (
|
|
|
70
63
|
|
|
71
64
|
await assureValidSig(morePowerfulKeys, proposed)
|
|
72
65
|
|
|
66
|
+
// timestamps must increase monotonically
|
|
67
|
+
const mostRecentOp = ops.at(-1) // ops.length is always >0 here
|
|
68
|
+
if (
|
|
69
|
+
mostRecentOp &&
|
|
70
|
+
proposedDate.getTime() <= mostRecentOp.createdAt.getTime()
|
|
71
|
+
) {
|
|
72
|
+
throw new MisorderedOperationError()
|
|
73
|
+
}
|
|
74
|
+
|
|
73
75
|
// recovery key gets a 72hr window to do historical re-wrties
|
|
74
76
|
if (nullified.length > 0) {
|
|
75
77
|
const RECOVERY_WINDOW = 72 * HOUR
|
|
76
|
-
const timeLapsed =
|
|
78
|
+
const timeLapsed =
|
|
79
|
+
proposedDate.getTime() - firstNullified.createdAt.getTime()
|
|
77
80
|
if (timeLapsed > RECOVERY_WINDOW) {
|
|
78
81
|
throw new LateRecoveryError(timeLapsed)
|
|
79
82
|
}
|
package/src/document.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import * as uint8arrays from 'uint8arrays'
|
|
2
1
|
import * as crypto from '@atproto/crypto'
|
|
3
|
-
import * as t from './types'
|
|
4
|
-
import {
|
|
5
|
-
import { ParsedDidKey } from '@atproto/crypto'
|
|
2
|
+
import type * as t from './types.js'
|
|
3
|
+
import type { ParsedMultikey } from '@atproto/crypto'
|
|
6
4
|
|
|
7
5
|
export const formatDidDoc = (data: t.DocumentData): t.DidDocument => {
|
|
8
|
-
const context = [
|
|
6
|
+
const context = [
|
|
7
|
+
'https://www.w3.org/ns/did/v1',
|
|
8
|
+
'https://w3id.org/security/multikey/v1',
|
|
9
|
+
]
|
|
9
10
|
|
|
10
11
|
const verificationMethods: VerificationMethod[] = []
|
|
11
12
|
for (const [keyid, key] of Object.entries(data.verificationMethods)) {
|
|
12
13
|
const info = formatKeyAndContext(key)
|
|
13
|
-
if (!context.includes(info.context)) {
|
|
14
|
+
if (info.context && !context.includes(info.context)) {
|
|
14
15
|
context.push(info.context)
|
|
15
16
|
}
|
|
16
17
|
verificationMethods.push({
|
|
17
|
-
id:
|
|
18
|
+
id: `${data.did}#${keyid}`,
|
|
18
19
|
type: info.type,
|
|
19
20
|
controller: data.did,
|
|
20
21
|
publicKeyMultibase: info.publicKeyMultibase,
|
|
@@ -53,32 +54,44 @@ type Service = {
|
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
type KeyAndContext = {
|
|
56
|
-
context
|
|
57
|
+
context?: string
|
|
57
58
|
type: string
|
|
58
|
-
publicKeyMultibase
|
|
59
|
+
publicKeyMultibase: string
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
const formatKeyAndContext = (key: string): KeyAndContext => {
|
|
62
|
-
let keyInfo:
|
|
63
|
+
let keyInfo: ParsedMultikey
|
|
63
64
|
try {
|
|
64
65
|
keyInfo = crypto.parseDidKey(key)
|
|
65
|
-
} catch
|
|
66
|
-
|
|
66
|
+
} catch {
|
|
67
|
+
return {
|
|
68
|
+
// we can't specify a context for a key type we don't recognize
|
|
69
|
+
type: 'Multikey',
|
|
70
|
+
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
|
|
71
|
+
}
|
|
67
72
|
}
|
|
68
|
-
const { jwtAlg
|
|
73
|
+
const { jwtAlg } = keyInfo
|
|
69
74
|
|
|
70
75
|
if (jwtAlg === crypto.P256_JWT_ALG) {
|
|
71
76
|
return {
|
|
72
77
|
context: 'https://w3id.org/security/suites/ecdsa-2019/v1',
|
|
73
|
-
type: '
|
|
74
|
-
publicKeyMultibase:
|
|
78
|
+
type: 'Multikey',
|
|
79
|
+
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
|
|
75
80
|
}
|
|
76
81
|
} else if (jwtAlg === crypto.SECP256K1_JWT_ALG) {
|
|
77
82
|
return {
|
|
78
83
|
context: 'https://w3id.org/security/suites/secp256k1-2019/v1',
|
|
79
|
-
type: '
|
|
80
|
-
publicKeyMultibase:
|
|
84
|
+
type: 'Multikey',
|
|
85
|
+
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
|
|
81
86
|
}
|
|
82
87
|
}
|
|
83
|
-
|
|
88
|
+
|
|
89
|
+
// this codepath might seem unreachable/redundant, but it's possible
|
|
90
|
+
// parseDidKey() supports more key formats in future, before this function
|
|
91
|
+
// can be updated likewise
|
|
92
|
+
return {
|
|
93
|
+
// we can't specify a context for a key type we don't recognize
|
|
94
|
+
type: 'Multikey',
|
|
95
|
+
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
|
|
96
|
+
}
|
|
84
97
|
}
|
package/src/error.ts
CHANGED
|
@@ -5,14 +5,21 @@ export class PlcError extends Error {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
static is(obj: unknown): obj is PlcError {
|
|
8
|
-
if (
|
|
8
|
+
if (
|
|
9
|
+
obj &&
|
|
10
|
+
typeof obj === 'object' &&
|
|
11
|
+
(obj as Record<string, unknown>)['plcError'] === true
|
|
12
|
+
) {
|
|
9
13
|
return true
|
|
10
14
|
}
|
|
11
15
|
return false
|
|
12
16
|
}
|
|
13
17
|
}
|
|
14
18
|
export class ImproperOperationError extends PlcError {
|
|
15
|
-
constructor(
|
|
19
|
+
constructor(
|
|
20
|
+
public reason: string,
|
|
21
|
+
public op: unknown,
|
|
22
|
+
) {
|
|
16
23
|
super(`Improperly formatted operation, ${reason}: ${op}`)
|
|
17
24
|
}
|
|
18
25
|
}
|
|
@@ -26,7 +33,7 @@ export class MisorderedOperationError extends PlcError {
|
|
|
26
33
|
export class LateRecoveryError extends PlcError {
|
|
27
34
|
constructor(public timeLapsed: number) {
|
|
28
35
|
super(
|
|
29
|
-
`Recovery operation
|
|
36
|
+
`Recovery operation occurred outside of the allowed 72 hr recovery window. Time lapsed: ${timeLapsed}`,
|
|
30
37
|
)
|
|
31
38
|
}
|
|
32
39
|
}
|
|
@@ -46,7 +53,10 @@ export class InvalidSignatureError extends PlcError {
|
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
export class UnsupportedKeyError extends PlcError {
|
|
49
|
-
constructor(
|
|
56
|
+
constructor(
|
|
57
|
+
public key: string,
|
|
58
|
+
public err: unknown,
|
|
59
|
+
) {
|
|
50
60
|
super(`Unsupported key type ${key}: ${err}`)
|
|
51
61
|
}
|
|
52
62
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './client'
|
|
2
|
-
export * from './data'
|
|
3
|
-
export * from './document'
|
|
4
|
-
export * from './error'
|
|
5
|
-
export * from './operations'
|
|
6
|
-
export * from './types'
|
|
1
|
+
export * from './client.js'
|
|
2
|
+
export * from './data.js'
|
|
3
|
+
export * from './document.js'
|
|
4
|
+
export * from './error.js'
|
|
5
|
+
export * from './operations.js'
|
|
6
|
+
export * from './types.js'
|
package/src/operations.ts
CHANGED
|
@@ -1,40 +1,68 @@
|
|
|
1
1
|
import * as cbor from '@ipld/dag-cbor'
|
|
2
|
-
import { CID } from 'multiformats/cid'
|
|
2
|
+
import type { CID } from 'multiformats/cid'
|
|
3
3
|
import * as uint8arrays from 'uint8arrays'
|
|
4
|
-
import { Keypair
|
|
4
|
+
import type { Keypair } from '@atproto/crypto'
|
|
5
|
+
import { sha256, verifySignature } from '@atproto/crypto'
|
|
5
6
|
import { check, cidForCbor } from '@atproto/common'
|
|
6
|
-
import * as t from './types'
|
|
7
|
+
import * as t from './types.js'
|
|
7
8
|
import {
|
|
8
9
|
GenesisHashError,
|
|
9
|
-
ImproperlyFormattedDidError,
|
|
10
10
|
ImproperOperationError,
|
|
11
11
|
InvalidSignatureError,
|
|
12
12
|
MisorderedOperationError,
|
|
13
|
-
|
|
14
|
-
} from './error'
|
|
13
|
+
} from './error.js'
|
|
15
14
|
|
|
16
|
-
export const didForCreateOp = async (op: t.CompatibleOp
|
|
15
|
+
export const didForCreateOp = async (op: t.CompatibleOp) => {
|
|
17
16
|
const hashOfGenesis = await sha256(cbor.encode(op))
|
|
18
17
|
const hashB32 = uint8arrays.toString(hashOfGenesis, 'base32')
|
|
19
|
-
const truncated = hashB32.slice(0,
|
|
18
|
+
const truncated = hashB32.slice(0, 24)
|
|
20
19
|
return `did:plc:${truncated}`
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
// Operations formatting
|
|
24
23
|
// ---------------------------
|
|
25
24
|
|
|
26
|
-
export
|
|
25
|
+
export type AtprotoOpOpts = {
|
|
26
|
+
/** did:key written to `verificationMethods.atproto` */
|
|
27
27
|
signingKey: string
|
|
28
|
+
/** did:key written to `verificationMethods.atproto_kem` */
|
|
29
|
+
kemKey?: string
|
|
30
|
+
/**
|
|
31
|
+
* Further verification methods, by id. Must not name an id that
|
|
32
|
+
* `signingKey` or `kemKey` also sets.
|
|
33
|
+
*/
|
|
34
|
+
verificationMethods?: Record<string, string>
|
|
28
35
|
handle: string
|
|
29
36
|
pds: string
|
|
30
37
|
rotationKeys: string[]
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Combines the explicitly-named atproto keys with any further verification
|
|
41
|
+
// methods, refusing to let one silently override the other.
|
|
42
|
+
const atprotoVerificationMethods = (opts: {
|
|
43
|
+
signingKey?: string
|
|
44
|
+
kemKey?: string
|
|
45
|
+
verificationMethods?: Record<string, string>
|
|
46
|
+
}): Record<string, string> => {
|
|
47
|
+
const named: Record<string, string> = {}
|
|
48
|
+
if (opts.signingKey !== undefined) named.atproto = opts.signingKey
|
|
49
|
+
if (opts.kemKey !== undefined) named.atproto_kem = opts.kemKey
|
|
50
|
+
for (const id of Object.keys(named)) {
|
|
51
|
+
if (opts.verificationMethods && id in opts.verificationMethods) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`verificationMethods.${id} conflicts with the ${id === 'atproto' ? 'signingKey' : 'kemKey'} option`,
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { ...opts.verificationMethods, ...named }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const formatAtprotoOp = (
|
|
61
|
+
opts: AtprotoOpOpts & { prev: CID | null },
|
|
62
|
+
): t.UnsignedOperation => {
|
|
33
63
|
return {
|
|
34
64
|
type: 'plc_operation',
|
|
35
|
-
verificationMethods:
|
|
36
|
-
atproto: opts.signingKey,
|
|
37
|
-
},
|
|
65
|
+
verificationMethods: atprotoVerificationMethods(opts),
|
|
38
66
|
rotationKeys: opts.rotationKeys,
|
|
39
67
|
alsoKnownAs: [ensureAtprotoPrefix(opts.handle)],
|
|
40
68
|
services: {
|
|
@@ -47,24 +75,15 @@ export const formatAtprotoOp = (opts: {
|
|
|
47
75
|
}
|
|
48
76
|
}
|
|
49
77
|
|
|
50
|
-
export const atprotoOp = async (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
pds: string
|
|
54
|
-
rotationKeys: string[]
|
|
55
|
-
prev: CID | null
|
|
56
|
-
signer: Keypair
|
|
57
|
-
}) => {
|
|
78
|
+
export const atprotoOp = async (
|
|
79
|
+
opts: AtprotoOpOpts & { prev: CID | null; signer: Keypair },
|
|
80
|
+
) => {
|
|
58
81
|
return addSignature(formatAtprotoOp(opts), opts.signer)
|
|
59
82
|
}
|
|
60
83
|
|
|
61
|
-
export const createOp = async (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
pds: string
|
|
65
|
-
rotationKeys: string[]
|
|
66
|
-
signer: Keypair
|
|
67
|
-
}): Promise<{ op: t.Operation; did: string }> => {
|
|
84
|
+
export const createOp = async (
|
|
85
|
+
opts: AtprotoOpOpts & { signer: Keypair },
|
|
86
|
+
): Promise<{ op: t.Operation; did: string }> => {
|
|
68
87
|
const op = await atprotoOp({ ...opts, prev: null })
|
|
69
88
|
const did = await didForCreateOp(op)
|
|
70
89
|
return { op, did }
|
|
@@ -77,6 +96,7 @@ export const createUpdateOp = async (
|
|
|
77
96
|
): Promise<t.Operation> => {
|
|
78
97
|
const prev = await cidForCbor(lastOp)
|
|
79
98
|
// omit sig so it doesn't accidentally make its way into the next operation
|
|
99
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
80
100
|
const { sig, ...normalized } = normalizeOp(lastOp)
|
|
81
101
|
const unsigned = await fn(normalized)
|
|
82
102
|
return addSignature(
|
|
@@ -88,18 +108,71 @@ export const createUpdateOp = async (
|
|
|
88
108
|
)
|
|
89
109
|
}
|
|
90
110
|
|
|
91
|
-
export const
|
|
111
|
+
export const createAtprotoUpdateOp = async (
|
|
92
112
|
lastOp: t.CompatibleOp,
|
|
93
113
|
signer: Keypair,
|
|
94
|
-
|
|
114
|
+
opts: Partial<AtprotoOpOpts>,
|
|
115
|
+
) => {
|
|
116
|
+
// validate before signing anything
|
|
117
|
+
const verificationMethods = atprotoVerificationMethods({
|
|
118
|
+
signingKey: opts.signingKey || undefined,
|
|
119
|
+
kemKey: opts.kemKey || undefined,
|
|
120
|
+
verificationMethods: opts.verificationMethods,
|
|
121
|
+
})
|
|
122
|
+
return createUpdateOp(lastOp, signer, (normalized) => {
|
|
123
|
+
const updated = { ...normalized }
|
|
124
|
+
if (Object.keys(verificationMethods).length > 0) {
|
|
125
|
+
updated.verificationMethods = {
|
|
126
|
+
...normalized.verificationMethods,
|
|
127
|
+
...verificationMethods,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (opts.handle) {
|
|
131
|
+
const formatted = ensureAtprotoPrefix(opts.handle)
|
|
132
|
+
const handleI = normalized.alsoKnownAs.findIndex((h) =>
|
|
133
|
+
h.startsWith('at://'),
|
|
134
|
+
)
|
|
135
|
+
if (handleI < 0) {
|
|
136
|
+
updated.alsoKnownAs = [formatted, ...normalized.alsoKnownAs]
|
|
137
|
+
} else {
|
|
138
|
+
updated.alsoKnownAs = [
|
|
139
|
+
...normalized.alsoKnownAs.slice(0, handleI),
|
|
140
|
+
formatted,
|
|
141
|
+
...normalized.alsoKnownAs.slice(handleI + 1),
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (opts.pds) {
|
|
146
|
+
const formatted = ensureHttpPrefix(opts.pds)
|
|
147
|
+
updated.services = {
|
|
148
|
+
...normalized.services,
|
|
149
|
+
atproto_pds: {
|
|
150
|
+
type: 'AtprotoPersonalDataServer',
|
|
151
|
+
endpoint: formatted,
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (opts.rotationKeys) {
|
|
156
|
+
updated.rotationKeys = opts.rotationKeys
|
|
157
|
+
}
|
|
158
|
+
return updated
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export const updateAtprotoSigningKeyOp = async (
|
|
163
|
+
lastOp: t.CompatibleOp,
|
|
164
|
+
signer: Keypair,
|
|
165
|
+
signingKey: string,
|
|
95
166
|
): Promise<t.Operation> => {
|
|
96
|
-
return
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
167
|
+
return createAtprotoUpdateOp(lastOp, signer, { signingKey })
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export const updateAtprotoKemKeyOp = async (
|
|
171
|
+
lastOp: t.CompatibleOp,
|
|
172
|
+
signer: Keypair,
|
|
173
|
+
kemKey: string,
|
|
174
|
+
): Promise<t.Operation> => {
|
|
175
|
+
return createAtprotoUpdateOp(lastOp, signer, { kemKey })
|
|
103
176
|
}
|
|
104
177
|
|
|
105
178
|
export const updateHandleOp = async (
|
|
@@ -107,46 +180,15 @@ export const updateHandleOp = async (
|
|
|
107
180
|
signer: Keypair,
|
|
108
181
|
handle: string,
|
|
109
182
|
): Promise<t.Operation> => {
|
|
110
|
-
|
|
111
|
-
return createUpdateOp(lastOp, signer, (normalized) => {
|
|
112
|
-
const handleI = normalized.alsoKnownAs.findIndex((h) =>
|
|
113
|
-
h.startsWith('at://'),
|
|
114
|
-
)
|
|
115
|
-
let aka: string[]
|
|
116
|
-
if (handleI < 0) {
|
|
117
|
-
aka = [formatted, ...normalized.alsoKnownAs]
|
|
118
|
-
} else {
|
|
119
|
-
aka = [
|
|
120
|
-
...normalized.alsoKnownAs.slice(0, handleI),
|
|
121
|
-
formatted,
|
|
122
|
-
...normalized.alsoKnownAs.slice(handleI + 1),
|
|
123
|
-
]
|
|
124
|
-
}
|
|
125
|
-
return {
|
|
126
|
-
...normalized,
|
|
127
|
-
alsoKnownAs: aka,
|
|
128
|
-
}
|
|
129
|
-
})
|
|
183
|
+
return createAtprotoUpdateOp(lastOp, signer, { handle })
|
|
130
184
|
}
|
|
131
185
|
|
|
132
186
|
export const updatePdsOp = async (
|
|
133
187
|
lastOp: t.CompatibleOp,
|
|
134
188
|
signer: Keypair,
|
|
135
|
-
|
|
189
|
+
pds: string,
|
|
136
190
|
): Promise<t.Operation> => {
|
|
137
|
-
|
|
138
|
-
return createUpdateOp(lastOp, signer, (normalized) => {
|
|
139
|
-
return {
|
|
140
|
-
...normalized,
|
|
141
|
-
services: {
|
|
142
|
-
...normalized.services,
|
|
143
|
-
atproto_pds: {
|
|
144
|
-
type: 'AtprotoPersonalDataServer',
|
|
145
|
-
endpoint: formatted,
|
|
146
|
-
},
|
|
147
|
-
},
|
|
148
|
-
}
|
|
149
|
-
})
|
|
191
|
+
return createAtprotoUpdateOp(lastOp, signer, { pds })
|
|
150
192
|
}
|
|
151
193
|
|
|
152
194
|
export const updateRotationKeysOp = async (
|
|
@@ -154,12 +196,7 @@ export const updateRotationKeysOp = async (
|
|
|
154
196
|
signer: Keypair,
|
|
155
197
|
rotationKeys: string[],
|
|
156
198
|
): Promise<t.Operation> => {
|
|
157
|
-
return
|
|
158
|
-
return {
|
|
159
|
-
...normalized,
|
|
160
|
-
rotationKeys,
|
|
161
|
-
}
|
|
162
|
-
})
|
|
199
|
+
return createAtprotoUpdateOp(lastOp, signer, { rotationKeys })
|
|
163
200
|
}
|
|
164
201
|
|
|
165
202
|
export const tombstoneOp = async (
|
|
@@ -232,28 +269,6 @@ export const normalizeOp = (op: t.CompatibleOp): t.Operation => {
|
|
|
232
269
|
// Verifying operations/signatures
|
|
233
270
|
// ---------------------------
|
|
234
271
|
|
|
235
|
-
export const assureValidOp = async (op: t.OpOrTombstone) => {
|
|
236
|
-
if (check.is(op, t.def.tombstone)) {
|
|
237
|
-
return true
|
|
238
|
-
}
|
|
239
|
-
// ensure we support the op's keys
|
|
240
|
-
const keys = [...Object.values(op.verificationMethods), ...op.rotationKeys]
|
|
241
|
-
await Promise.all(
|
|
242
|
-
keys.map(async (k) => {
|
|
243
|
-
try {
|
|
244
|
-
parseDidKey(k)
|
|
245
|
-
} catch (err) {
|
|
246
|
-
throw new UnsupportedKeyError(k, err)
|
|
247
|
-
}
|
|
248
|
-
}),
|
|
249
|
-
)
|
|
250
|
-
if (op.rotationKeys.length > 5) {
|
|
251
|
-
throw new ImproperOperationError('too many rotation keys', op)
|
|
252
|
-
} else if (op.rotationKeys.length < 1) {
|
|
253
|
-
throw new ImproperOperationError('need at least one rotation key', op)
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
272
|
export const assureValidCreationOp = async (
|
|
258
273
|
did: string,
|
|
259
274
|
op: t.CompatibleOpOrTombstone,
|
|
@@ -262,14 +277,9 @@ export const assureValidCreationOp = async (
|
|
|
262
277
|
throw new MisorderedOperationError()
|
|
263
278
|
}
|
|
264
279
|
const normalized = normalizeOp(op)
|
|
265
|
-
await assureValidOp(normalized)
|
|
266
280
|
await assureValidSig(normalized.rotationKeys, op)
|
|
267
|
-
const expectedDid = await didForCreateOp(op
|
|
268
|
-
|
|
269
|
-
if (did.length < 32) {
|
|
270
|
-
throw new ImproperlyFormattedDidError('too short')
|
|
271
|
-
}
|
|
272
|
-
if (!expectedDid.startsWith(did)) {
|
|
281
|
+
const expectedDid = await didForCreateOp(op)
|
|
282
|
+
if (expectedDid !== did) {
|
|
273
283
|
throw new GenesisHashError(expectedDid)
|
|
274
284
|
}
|
|
275
285
|
if (op.prev !== null) {
|
|
@@ -281,16 +291,19 @@ export const assureValidCreationOp = async (
|
|
|
281
291
|
}
|
|
282
292
|
|
|
283
293
|
export const assureValidSig = async (
|
|
284
|
-
|
|
294
|
+
allowedDidKeys: string[],
|
|
285
295
|
op: t.CompatibleOpOrTombstone,
|
|
286
296
|
): Promise<string> => {
|
|
287
297
|
const { sig, ...opData } = op
|
|
298
|
+
if (sig.endsWith('=')) {
|
|
299
|
+
throw new InvalidSignatureError(op)
|
|
300
|
+
}
|
|
288
301
|
const sigBytes = uint8arrays.fromString(sig, 'base64url')
|
|
289
302
|
const dataBytes = new Uint8Array(cbor.encode(opData))
|
|
290
|
-
for (const
|
|
291
|
-
const isValid = await verifySignature(
|
|
303
|
+
for (const didKey of allowedDidKeys) {
|
|
304
|
+
const isValid = await verifySignature(didKey, dataBytes, sigBytes)
|
|
292
305
|
if (isValid) {
|
|
293
|
-
return
|
|
306
|
+
return didKey
|
|
294
307
|
}
|
|
295
308
|
}
|
|
296
309
|
throw new InvalidSignatureError(op)
|
package/src/types.ts
CHANGED
|
@@ -8,10 +8,12 @@ const cid = z
|
|
|
8
8
|
})
|
|
9
9
|
.transform((obj: unknown) => mf.CID.asCID(obj) as mf.CID)
|
|
10
10
|
|
|
11
|
-
const service = z
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const service = z
|
|
12
|
+
.object({
|
|
13
|
+
type: z.string(),
|
|
14
|
+
endpoint: z.string(),
|
|
15
|
+
})
|
|
16
|
+
.strict()
|
|
15
17
|
|
|
16
18
|
const documentData = z.object({
|
|
17
19
|
did: z.string(),
|
|
@@ -22,36 +24,42 @@ const documentData = z.object({
|
|
|
22
24
|
})
|
|
23
25
|
export type DocumentData = z.infer<typeof documentData>
|
|
24
26
|
|
|
25
|
-
const unsignedCreateOpV1 = z
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
const unsignedCreateOpV1 = z
|
|
28
|
+
.object({
|
|
29
|
+
type: z.literal('create'),
|
|
30
|
+
signingKey: z.string(),
|
|
31
|
+
recoveryKey: z.string(),
|
|
32
|
+
handle: z.string(),
|
|
33
|
+
service: z.string(),
|
|
34
|
+
prev: z.null(),
|
|
35
|
+
})
|
|
36
|
+
.strict()
|
|
33
37
|
export type UnsignedCreateOpV1 = z.infer<typeof unsignedCreateOpV1>
|
|
34
|
-
const createOpV1 = unsignedCreateOpV1.extend({ sig: z.string() })
|
|
38
|
+
const createOpV1 = unsignedCreateOpV1.extend({ sig: z.string() }).strict()
|
|
35
39
|
export type CreateOpV1 = z.infer<typeof createOpV1>
|
|
36
40
|
|
|
37
|
-
const unsignedOperation = z
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
const unsignedOperation = z
|
|
42
|
+
.object({
|
|
43
|
+
type: z.literal('plc_operation'),
|
|
44
|
+
rotationKeys: z.array(z.string()),
|
|
45
|
+
verificationMethods: z.record(z.string()),
|
|
46
|
+
alsoKnownAs: z.array(z.string()),
|
|
47
|
+
services: z.record(service),
|
|
48
|
+
prev: z.string().nullable(),
|
|
49
|
+
})
|
|
50
|
+
.strict()
|
|
45
51
|
export type UnsignedOperation = z.infer<typeof unsignedOperation>
|
|
46
|
-
const operation = unsignedOperation.extend({ sig: z.string() })
|
|
52
|
+
const operation = unsignedOperation.extend({ sig: z.string() }).strict()
|
|
47
53
|
export type Operation = z.infer<typeof operation>
|
|
48
54
|
|
|
49
|
-
const unsignedTombstone = z
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
const unsignedTombstone = z
|
|
56
|
+
.object({
|
|
57
|
+
type: z.literal('plc_tombstone'),
|
|
58
|
+
prev: z.string(),
|
|
59
|
+
})
|
|
60
|
+
.strict()
|
|
53
61
|
export type UnsignedTombstone = z.infer<typeof unsignedTombstone>
|
|
54
|
-
const tombstone = unsignedTombstone.extend({ sig: z.string() })
|
|
62
|
+
const tombstone = unsignedTombstone.extend({ sig: z.string() }).strict()
|
|
55
63
|
export type Tombstone = z.infer<typeof tombstone>
|
|
56
64
|
|
|
57
65
|
const opOrTombstone = z.union([operation, tombstone])
|
|
@@ -79,6 +87,16 @@ export const exportedOp = z.object({
|
|
|
79
87
|
})
|
|
80
88
|
export type ExportedOp = z.infer<typeof exportedOp>
|
|
81
89
|
|
|
90
|
+
export const exportedOpWithSeq = z.object({
|
|
91
|
+
did: z.string(),
|
|
92
|
+
operation: compatibleOpOrTombstone,
|
|
93
|
+
cid: z.string(),
|
|
94
|
+
createdAt: z.string(),
|
|
95
|
+
seq: z.number(),
|
|
96
|
+
type: z.literal('sequenced_op'),
|
|
97
|
+
})
|
|
98
|
+
export type ExportedOpWithSeq = z.infer<typeof exportedOpWithSeq>
|
|
99
|
+
|
|
82
100
|
export const didDocVerificationMethod = z.object({
|
|
83
101
|
id: z.string(),
|
|
84
102
|
type: z.string(),
|
|
@@ -112,5 +130,6 @@ export const def = {
|
|
|
112
130
|
compatibleOpOrTombstone,
|
|
113
131
|
indexedOperation,
|
|
114
132
|
exportedOp,
|
|
133
|
+
exportedOpWithSeq,
|
|
115
134
|
didDocument,
|
|
116
135
|
}
|
package/build.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const pkgJson = require('@npmcli/package-json')
|
|
2
|
-
const { nodeExternalsPlugin } = require('esbuild-node-externals')
|
|
3
|
-
|
|
4
|
-
const buildShallow =
|
|
5
|
-
process.argv.includes('--shallow') || process.env.PLC_BUILD_SHALLOW === 'true'
|
|
6
|
-
|
|
7
|
-
if (process.argv.includes('--update-main-to-dist')) {
|
|
8
|
-
return pkgJson
|
|
9
|
-
.load(__dirname)
|
|
10
|
-
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
|
|
11
|
-
.then((pkg) => pkg.save())
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
require('esbuild').build({
|
|
15
|
-
logLevel: 'info',
|
|
16
|
-
entryPoints: [
|
|
17
|
-
'src/index.ts',
|
|
18
|
-
],
|
|
19
|
-
bundle: true,
|
|
20
|
-
sourcemap: true,
|
|
21
|
-
treeShaking: true,
|
|
22
|
-
outdir: 'dist',
|
|
23
|
-
platform: 'node',
|
|
24
|
-
assetNames: 'src/static',
|
|
25
|
-
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
|
|
26
|
-
})
|