@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
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
|
}
|
|
@@ -110,21 +113,34 @@ export const validateOperationLog = async (
|
|
|
110
113
|
throw new MisorderedOperationError()
|
|
111
114
|
}
|
|
112
115
|
await assureValidSig(doc.rotationKeys, op)
|
|
113
|
-
|
|
116
|
+
const data = opToData(did, op)
|
|
117
|
+
// if tombstone & last op, return null. else throw
|
|
118
|
+
if (data === null) {
|
|
114
119
|
if (i === rest.length - 1) {
|
|
115
120
|
return null
|
|
116
121
|
} else {
|
|
117
122
|
throw new MisorderedOperationError()
|
|
118
123
|
}
|
|
119
124
|
}
|
|
120
|
-
|
|
121
|
-
doc = { did, verificationMethods, rotationKeys, alsoKnownAs, services }
|
|
125
|
+
doc = data
|
|
122
126
|
prev = await cidForCbor(op)
|
|
123
127
|
}
|
|
124
128
|
|
|
125
129
|
return doc
|
|
126
130
|
}
|
|
127
131
|
|
|
132
|
+
export const opToData = (
|
|
133
|
+
did: string,
|
|
134
|
+
op: t.CompatibleOpOrTombstone,
|
|
135
|
+
): t.DocumentData | null => {
|
|
136
|
+
if (check.is(op, t.def.tombstone)) {
|
|
137
|
+
return null
|
|
138
|
+
}
|
|
139
|
+
const { verificationMethods, rotationKeys, alsoKnownAs, services } =
|
|
140
|
+
normalizeOp(op)
|
|
141
|
+
return { did, verificationMethods, rotationKeys, alsoKnownAs, services }
|
|
142
|
+
}
|
|
143
|
+
|
|
128
144
|
export const getLastOpWithCid = async (
|
|
129
145
|
ops: t.CompatibleOpOrTombstone[],
|
|
130
146
|
): Promise<{ op: t.CompatibleOpOrTombstone; cid: CID }> => {
|
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(
|
|
@@ -91,19 +111,20 @@ export const createUpdateOp = async (
|
|
|
91
111
|
export const createAtprotoUpdateOp = async (
|
|
92
112
|
lastOp: t.CompatibleOp,
|
|
93
113
|
signer: Keypair,
|
|
94
|
-
opts: Partial<
|
|
95
|
-
signingKey: string
|
|
96
|
-
handle: string
|
|
97
|
-
pds: string
|
|
98
|
-
rotationKeys: string[]
|
|
99
|
-
}>,
|
|
114
|
+
opts: Partial<AtprotoOpOpts>,
|
|
100
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
|
+
})
|
|
101
122
|
return createUpdateOp(lastOp, signer, (normalized) => {
|
|
102
123
|
const updated = { ...normalized }
|
|
103
|
-
if (
|
|
124
|
+
if (Object.keys(verificationMethods).length > 0) {
|
|
104
125
|
updated.verificationMethods = {
|
|
105
126
|
...normalized.verificationMethods,
|
|
106
|
-
|
|
127
|
+
...verificationMethods,
|
|
107
128
|
}
|
|
108
129
|
}
|
|
109
130
|
if (opts.handle) {
|
|
@@ -138,7 +159,7 @@ export const createAtprotoUpdateOp = async (
|
|
|
138
159
|
})
|
|
139
160
|
}
|
|
140
161
|
|
|
141
|
-
export const
|
|
162
|
+
export const updateAtprotoSigningKeyOp = async (
|
|
142
163
|
lastOp: t.CompatibleOp,
|
|
143
164
|
signer: Keypair,
|
|
144
165
|
signingKey: string,
|
|
@@ -146,6 +167,14 @@ export const updateAtprotoKeyOp = async (
|
|
|
146
167
|
return createAtprotoUpdateOp(lastOp, signer, { signingKey })
|
|
147
168
|
}
|
|
148
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 })
|
|
176
|
+
}
|
|
177
|
+
|
|
149
178
|
export const updateHandleOp = async (
|
|
150
179
|
lastOp: t.CompatibleOp,
|
|
151
180
|
signer: Keypair,
|
|
@@ -240,28 +269,6 @@ export const normalizeOp = (op: t.CompatibleOp): t.Operation => {
|
|
|
240
269
|
// Verifying operations/signatures
|
|
241
270
|
// ---------------------------
|
|
242
271
|
|
|
243
|
-
export const assureValidOp = async (op: t.OpOrTombstone) => {
|
|
244
|
-
if (check.is(op, t.def.tombstone)) {
|
|
245
|
-
return true
|
|
246
|
-
}
|
|
247
|
-
// ensure we support the op's keys
|
|
248
|
-
const keys = [...Object.values(op.verificationMethods), ...op.rotationKeys]
|
|
249
|
-
await Promise.all(
|
|
250
|
-
keys.map(async (k) => {
|
|
251
|
-
try {
|
|
252
|
-
parseDidKey(k)
|
|
253
|
-
} catch (err) {
|
|
254
|
-
throw new UnsupportedKeyError(k, err)
|
|
255
|
-
}
|
|
256
|
-
}),
|
|
257
|
-
)
|
|
258
|
-
if (op.rotationKeys.length > 5) {
|
|
259
|
-
throw new ImproperOperationError('too many rotation keys', op)
|
|
260
|
-
} else if (op.rotationKeys.length < 1) {
|
|
261
|
-
throw new ImproperOperationError('need at least one rotation key', op)
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
272
|
export const assureValidCreationOp = async (
|
|
266
273
|
did: string,
|
|
267
274
|
op: t.CompatibleOpOrTombstone,
|
|
@@ -270,14 +277,9 @@ export const assureValidCreationOp = async (
|
|
|
270
277
|
throw new MisorderedOperationError()
|
|
271
278
|
}
|
|
272
279
|
const normalized = normalizeOp(op)
|
|
273
|
-
await assureValidOp(normalized)
|
|
274
280
|
await assureValidSig(normalized.rotationKeys, op)
|
|
275
|
-
const expectedDid = await didForCreateOp(op
|
|
276
|
-
|
|
277
|
-
if (did.length < 32) {
|
|
278
|
-
throw new ImproperlyFormattedDidError('too short')
|
|
279
|
-
}
|
|
280
|
-
if (!expectedDid.startsWith(did)) {
|
|
281
|
+
const expectedDid = await didForCreateOp(op)
|
|
282
|
+
if (expectedDid !== did) {
|
|
281
283
|
throw new GenesisHashError(expectedDid)
|
|
282
284
|
}
|
|
283
285
|
if (op.prev !== null) {
|
|
@@ -289,16 +291,19 @@ export const assureValidCreationOp = async (
|
|
|
289
291
|
}
|
|
290
292
|
|
|
291
293
|
export const assureValidSig = async (
|
|
292
|
-
|
|
294
|
+
allowedDidKeys: string[],
|
|
293
295
|
op: t.CompatibleOpOrTombstone,
|
|
294
296
|
): Promise<string> => {
|
|
295
297
|
const { sig, ...opData } = op
|
|
298
|
+
if (sig.endsWith('=')) {
|
|
299
|
+
throw new InvalidSignatureError(op)
|
|
300
|
+
}
|
|
296
301
|
const sigBytes = uint8arrays.fromString(sig, 'base64url')
|
|
297
302
|
const dataBytes = new Uint8Array(cbor.encode(opData))
|
|
298
|
-
for (const
|
|
299
|
-
const isValid = await verifySignature(
|
|
303
|
+
for (const didKey of allowedDidKeys) {
|
|
304
|
+
const isValid = await verifySignature(didKey, dataBytes, sigBytes)
|
|
300
305
|
if (isValid) {
|
|
301
|
-
return
|
|
306
|
+
return didKey
|
|
302
307
|
}
|
|
303
308
|
}
|
|
304
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
|
-
})
|