···
import { cidForCbor } from '@atproto/common'
import { AxiosError } from 'axios'
import { Database } from '../src'
7
-
import { didForCreateOp } from '@did-plc/lib'
7
+
import { signOperation } from '@did-plc/lib'
describe('PLC server', () => {
let handle = 'alice.example.com'
···
it('registers a did', async () => {
43
-
const op = await plc.signOperation(
43
+
did = await client.create(
signingKey: signingKey.did(),
rotationKeys: [rotationKey1.did(), rotationKey2.did()],
···
55
-
did = await didForCreateOp(op)
56
-
await client.sendOperation(did, op)
it('retrieves did doc data', async () => {
···
await client.applyPartialOp(
{ rotationKeys: [newRotationKey.did(), rotationKey2.did()] },
rotationKey1 = newRotationKey
···
expect(doc.services).toEqual({ atpPds })
100
-
// it('does not allow key types that we do not support', async () => {
101
-
// // an ed25519 key which we don't yet support
102
-
// const newSigningKey =
103
-
// 'did:key:z6MkjwbBXZnFqL8su24wGL2Fdjti6GSLv9SWdYGswfazUPm9'
95
+
it('does not allow key types that we do not support', async () => {
96
+
// an ed25519 key which we don't yet support
97
+
const newSigningKey =
98
+
'did:key:z6MkjwbBXZnFqL8su24wGL2Fdjti6GSLv9SWdYGswfazUPm9'
105
-
// const promise = client.rotateSigningKey(did, newSigningKey, signingKey)
106
-
// await expect(promise).rejects.toThrow(AxiosError)
100
+
const promise = client.applyPartialOp(
102
+
{ signingKey: newSigningKey },
105
+
await expect(promise).rejects.toThrow(AxiosError)
109
-
// it('retrieves the operation log', async () => {
110
-
// const doc = await client.getDocumentData(did)
111
-
// const ops = await client.getOperationLog(did)
112
-
// const computedDoc = await document.validateOperationLog(did, ops)
113
-
// expect(computedDoc).toEqual(doc)
108
+
it('retrieves the operation log', async () => {
109
+
const doc = await client.getDocumentData(did)
110
+
const ops = await client.getOperationLog(did)
111
+
const computedDoc = await plc.validateOperationLog(did, ops)
112
+
expect(computedDoc).toEqual(doc)
116
-
// it('rejects on bad updates', async () => {
117
-
// const newKey = await EcdsaKeypair.create()
118
-
// const operation = client.rotateRecoveryKey(did, newKey.did(), newKey)
119
-
// await expect(operation).rejects.toThrow()
115
+
it('rejects on bad updates', async () => {
116
+
const newKey = await EcdsaKeypair.create()
117
+
const operation = client.applyPartialOp(
119
+
{ signingKey: newKey.did() },
122
+
await expect(operation).rejects.toThrow()
122
-
// it('allows for recovery through a forked history', async () => {
123
-
// const attackerKey = await EcdsaKeypair.create()
124
-
// await client.rotateSigningKey(did, attackerKey.did(), signingKey)
125
-
// await client.rotateRecoveryKey(did, attackerKey.did(), attackerKey)
125
+
it('allows for recovery through a forked history', async () => {
126
+
const attackerKey = await EcdsaKeypair.create()
127
+
await client.applyPartialOp(
129
+
{ signingKey: attackerKey.did(), rotationKeys: [attackerKey.did()] },
127
-
// const newKey = await EcdsaKeypair.create()
128
-
// const ops = await client.getOperationLog(did)
129
-
// const forkPoint = ops[ops.length - 3]
130
-
// const forkCid = await cidForCbor(forkPoint)
131
-
// await client.rotateSigningKey(did, newKey.did(), recoveryKey, forkCid)
132
-
// signingKey = newKey
133
+
const newKey = await EcdsaKeypair.create()
134
+
const ops = await client.getOperationLog(did)
135
+
const forkPoint = ops.at(-2)
137
+
throw new Error('Could not find fork point')
139
+
const forkCid = await cidForCbor(forkPoint)
140
+
const op = await signOperation(
142
+
signingKey: signingKey.did(),
143
+
rotationKeys: [newKey.did()],
144
+
handles: forkPoint.handles,
145
+
services: forkPoint.services,
146
+
prev: forkCid.toString(),
150
+
await client.sendOperation(did, op)
134
-
// const doc = await client.getDocumentData(did)
135
-
// expect(doc.did).toEqual(did)
136
-
// expect(doc.signingKey).toEqual(signingKey.did())
137
-
// expect(doc.recoveryKey).toEqual(recoveryKey.did())
138
-
// expect(doc.handle).toEqual(handle)
139
-
// expect(doc.atpPds).toEqual(atpPds)
152
+
rotationKey1 = newKey
142
-
// it('retrieves the did doc', async () => {
143
-
// const data = await client.getDocumentData(did)
144
-
// const doc = await client.getDocument(did)
145
-
// expect(doc).toEqual(document.formatDidDoc(data))
154
+
const doc = await client.getDocumentData(did)
155
+
expect(doc.did).toEqual(did)
156
+
expect(doc.signingKey).toEqual(signingKey.did())
157
+
expect(doc.rotationKeys).toEqual([newKey.did()])
158
+
expect(doc.handles).toEqual([handle])
159
+
expect(doc.services).toEqual({ atpPds })
148
-
// it('handles concurrent requests to many docs', async () => {
149
-
// const COUNT = 100
150
-
// const keys: EcdsaKeypair[] = []
151
-
// for (let i = 0; i < COUNT; i++) {
152
-
// keys.push(await EcdsaKeypair.create())
154
-
// await Promise.all(
155
-
// keys.map(async (key, index) => {
156
-
// await client.createDid(key, key.did(), `user${index}`, `example.com`)
162
+
it('retrieves the did doc', async () => {
163
+
const data = await client.getDocumentData(did)
164
+
const doc = await client.getDocument(did)
165
+
expect(doc).toEqual(plc.formatDidDoc(data))
161
-
// it('resolves races into a coherent history with no forks', async () => {
162
-
// const COUNT = 100
163
-
// const keys: EcdsaKeypair[] = []
164
-
// for (let i = 0; i < COUNT; i++) {
165
-
// keys.push(await EcdsaKeypair.create())
167
-
// const prev = await client.getPrev(did)
168
+
it('handles concurrent requests to many docs', async () => {
170
+
const keys: EcdsaKeypair[] = []
171
+
for (let i = 0; i < COUNT; i++) {
172
+
keys.push(await EcdsaKeypair.create())
175
+
keys.map(async (key, index) => {
176
+
await client.create(
178
+
signingKey: key.did(),
179
+
rotationKeys: [key.did()],
180
+
handles: [`user${index}`],
182
+
atpPds: `example.com`,
191
+
it('resolves races into a coherent history with no forks', async () => {
193
+
const keys: EcdsaKeypair[] = []
194
+
for (let i = 0; i < COUNT; i++) {
195
+
keys.push(await EcdsaKeypair.create())
197
+
// const prev = await client.getPrev(did)
169
-
// let successes = 0
170
-
// let failures = 0
171
-
// await Promise.all(
172
-
// keys.map(async (key) => {
174
-
// await client.rotateSigningKey(did, key.did(), signingKey, prev)
181
-
// expect(successes).toBe(1)
182
-
// expect(failures).toBe(99)
202
+
keys.map(async (key) => {
204
+
await client.applyPartialOp(
206
+
{ signingKey: key.did() },
215
+
expect(successes).toBe(1)
216
+
expect(failures).toBe(99)
184
-
// const ops = await client.getOperationLog(did)
185
-
// await document.validateOperationLog(did, ops)
218
+
const ops = await client.getOperationLog(did)
219
+
await plc.validateOperationLog(did, ops)
188
-
// it('healthcheck succeeds when database is available.', async () => {
189
-
// const { data, status } = await client.health()
190
-
// expect(status).toEqual(200)
191
-
// expect(data).toEqual({ version: '0.0.0' })
222
+
it('healthcheck succeeds when database is available.', async () => {
223
+
const { data, status } = await client.health()
224
+
expect(status).toEqual(200)
225
+
expect(data).toEqual({ version: '0.0.0' })
194
-
// it('healthcheck fails when database is unavailable.', async () => {
195
-
// await db.db.destroy()
196
-
// let error: AxiosError
198
-
// await client.health()
199
-
// throw new Error('Healthcheck should have failed')
201
-
// if (err instanceof AxiosError) {
207
-
// expect(error.response?.status).toEqual(503)
208
-
// expect(error.response?.data).toEqual({
209
-
// version: '0.0.0',
210
-
// error: 'Service Unavailable',
228
+
it('healthcheck fails when database is unavailable.', async () => {
229
+
await db.db.destroy()
230
+
let error: AxiosError
232
+
await client.health()
233
+
throw new Error('Healthcheck should have failed')
235
+
if (err instanceof AxiosError) {
241
+
expect(error.response?.status).toEqual(503)
242
+
expect(error.response?.data).toEqual({
244
+
error: 'Service Unavailable',