Fork of github.com/did-method-plc/did-method-plc

fix up tests

dholms 1fc7679f a99dbe07

Changed files
+62 -133
packages
lib
tests
+62 -133
packages/lib/tests/data.test.ts
···
import { check, cidForCbor } from '@atproto/common'
-
import { EcdsaKeypair, Keypair, Secp256k1Keypair } from '@atproto/crypto'
+
import { EcdsaKeypair, Secp256k1Keypair } from '@atproto/crypto'
import {
GenesisHashError,
ImproperOperationError,
···
let rotationKey1: Secp256k1Keypair
let rotationKey2: EcdsaKeypair
let did: string
-
let handle = 'alice.example.com'
+
let handle = 'at://alice.example.com'
let atpPds = 'https://example.com'
let oldRotationKey1: Secp256k1Keypair
···
rotationKey2 = await EcdsaKeypair.create()
})
-
const makeNextOp = async (
-
changes: Partial<t.Operation>,
-
key: Keypair,
-
): Promise<t.Operation> => {
+
const lastOp = () => {
const lastOp = ops.at(-1)
if (!lastOp) {
throw new Error('expected an op on log')
}
-
const prev = await cidForCbor(lastOp)
-
return operations.signOperation(
-
{
-
type: 'plc_operation',
-
verificationMethods: lastOp.verificationMethods,
-
rotationKeys: lastOp.rotationKeys,
-
alsoKnownAs: lastOp.alsoKnownAs,
-
services: lastOp.services,
-
prev: prev.toString(),
-
...changes,
+
return lastOp
+
}
+
+
const verifyDoc = (doc: t.DocumentData | null) => {
+
if (!doc) {
+
throw new Error('expected doc')
+
}
+
expect(doc.did).toEqual(did)
+
expect(doc.verificationMethods).toEqual({ atproto: signingKey.did() })
+
expect(doc.rotationKeys).toEqual([rotationKey1.did(), rotationKey2.did()])
+
expect(doc.alsoKnownAs).toEqual([handle])
+
expect(doc.services).toEqual({
+
atproto_pds: {
+
type: 'AtprotoPersonalDataServer',
+
endpoint: atpPds,
},
-
key,
-
)
+
})
}
it('creates a valid create op', async () => {
-
const createOp = await operations.signOperation(
-
{
-
type: 'plc_operation',
-
verificationMethods: {
-
atproto: signingKey.did(),
-
},
-
rotationKeys: [rotationKey1.did(), rotationKey2.did()],
-
alsoKnownAs: [handle],
-
services: {
-
atpPds,
-
},
-
prev: null,
-
},
-
rotationKey1,
-
)
+
const createOp = await operations.atprotoOp({
+
signingKey: signingKey.did(),
+
rotationKeys: [rotationKey1.did(), rotationKey2.did()],
+
handle,
+
pds: atpPds,
+
prev: null,
+
signer: rotationKey1,
+
})
const isValid = check.is(createOp, t.def.operation)
expect(isValid).toBeTruthy()
ops.push(createOp)
···
it('parses an operation log with no updates', async () => {
const doc = await data.validateOperationLog(did, ops)
-
-
if (!doc) {
-
throw new Error('expected doc')
-
}
-
expect(doc.did).toEqual(did)
-
expect(doc.verificationMethods).toEqual({ atproto: signingKey.did() })
-
expect(doc.rotationKeys).toEqual([rotationKey1.did(), rotationKey2.did()])
-
expect(doc.alsoKnownAs).toEqual([handle])
-
expect(doc.services).toEqual({ atpPds })
+
verifyDoc(doc)
})
it('updates handle', async () => {
-
handle = 'ali.example2.com'
-
const op = await makeNextOp({ alsoKnownAs: [handle] }, rotationKey1)
+
handle = 'at://ali.example2.com'
+
const op = await operations.updateHandleOp(lastOp(), rotationKey1, handle)
ops.push(op)
const doc = await data.validateOperationLog(did, ops)
-
if (!doc) {
-
throw new Error('expected doc')
-
}
-
expect(doc.did).toEqual(did)
-
expect(doc.verificationMethods).toEqual({ atproto: signingKey.did() })
-
expect(doc.rotationKeys).toEqual([rotationKey1.did(), rotationKey2.did()])
-
expect(doc.alsoKnownAs).toEqual([handle])
-
expect(doc.services).toEqual({ atpPds })
+
verifyDoc(doc)
})
it('updates atpPds', async () => {
atpPds = 'https://example2.com'
-
const op = await makeNextOp(
-
{
-
services: {
-
atpPds,
-
},
-
},
-
rotationKey1,
-
)
+
const op = await operations.updatePdsOp(lastOp(), rotationKey1, atpPds)
ops.push(op)
const doc = await data.validateOperationLog(did, ops)
-
if (!doc) {
-
throw new Error('expected doc')
-
}
-
expect(doc.did).toEqual(did)
-
expect(doc.verificationMethods).toEqual({ atproto: signingKey.did() })
-
expect(doc.rotationKeys).toEqual([rotationKey1.did(), rotationKey2.did()])
-
expect(doc.alsoKnownAs).toEqual([handle])
-
expect(doc.services).toEqual({ atpPds })
+
verifyDoc(doc)
})
it('rotates signingKey', async () => {
const newSigningKey = await Secp256k1Keypair.create()
-
const op = await makeNextOp(
-
{
-
verificationMethods: {
-
atproto: newSigningKey.did(),
-
},
-
},
+
const op = await operations.updateAtprotoKeyOp(
+
lastOp(),
rotationKey1,
+
newSigningKey.did(),
)
ops.push(op)
signingKey = newSigningKey
const doc = await data.validateOperationLog(did, ops)
-
if (!doc) {
-
throw new Error('expected doc')
-
}
-
expect(doc.did).toEqual(did)
-
expect(doc.verificationMethods).toEqual({ atproto: signingKey.did() })
-
expect(doc.rotationKeys).toEqual([rotationKey1.did(), rotationKey2.did()])
-
expect(doc.alsoKnownAs).toEqual([handle])
-
expect(doc.services).toEqual({ atpPds })
+
verifyDoc(doc)
})
it('rotates rotation keys', async () => {
const newRotationKey = await Secp256k1Keypair.create()
-
const op = await makeNextOp(
-
{
-
rotationKeys: [newRotationKey.did(), rotationKey2.did()],
-
},
-
rotationKey1,
-
)
+
const op = await operations.updateRotationkeysOp(lastOp(), rotationKey1, [
+
newRotationKey.did(),
+
rotationKey2.did(),
+
])
ops.push(op)
oldRotationKey1 = rotationKey1
rotationKey1 = newRotationKey
const doc = await data.validateOperationLog(did, ops)
-
if (!doc) {
-
throw new Error('expected doc')
-
}
-
-
expect(doc.did).toEqual(did)
-
expect(doc.verificationMethods).toEqual({ atproto: signingKey.did() })
-
expect(doc.rotationKeys).toEqual([rotationKey1.did(), rotationKey2.did()])
-
expect(doc.alsoKnownAs).toEqual([handle])
-
expect(doc.services).toEqual({ atpPds })
+
verifyDoc(doc)
})
it('no longer allows operations from old rotation key', async () => {
-
const op = await makeNextOp(
-
{
-
alsoKnownAs: ['bob'],
-
},
+
const op = await operations.updateHandleOp(
+
lastOp(),
oldRotationKey1,
+
'at://bob',
)
expect(data.validateOperationLog(did, [...ops, op])).rejects.toThrow(
InvalidSignatureError,
···
})
it('does not allow operations from the signingKey', async () => {
-
const op = await makeNextOp(
-
{
-
alsoKnownAs: ['bob'],
-
},
-
signingKey,
-
)
+
const op = await operations.updateHandleOp(lastOp(), signingKey, 'at://bob')
expect(data.validateOperationLog(did, [...ops, op])).rejects.toThrow(
InvalidSignatureError,
)
})
it('allows for operations from either rotation key', async () => {
-
const newHandle = 'ali.example.com'
-
const op = await makeNextOp(
-
{
-
alsoKnownAs: [newHandle],
-
},
+
const newHandle = 'at://ali.example.com'
+
const op = await operations.updateHandleOp(
+
lastOp(),
rotationKey2,
+
newHandle,
)
ops.push(op)
handle = newHandle
const doc = await data.validateOperationLog(did, ops)
-
if (!doc) {
-
throw new Error('expected doc')
-
}
-
expect(doc.did).toEqual(did)
-
expect(doc.verificationMethods).toEqual({ atproto: signingKey.did() })
-
expect(doc.rotationKeys).toEqual([rotationKey1.did(), rotationKey2.did()])
-
expect(doc.alsoKnownAs).toEqual([handle])
-
expect(doc.services).toEqual({ atpPds })
+
verifyDoc(doc)
})
it('allows tombstoning a DID', async () => {
···
})
it('requires operations to be in order', async () => {
-
const prev = await cidForCbor(ops[ops.length - 2])
-
const op = await makeNextOp(
-
{
-
alsoKnownAs: ['bob.test'],
-
prev: prev.toString(),
-
},
+
const op = await operations.updateHandleOp(
+
ops[ops.length - 2],
rotationKey1,
+
'at://bob.test',
)
expect(data.validateOperationLog(did, [...ops, op])).rejects.toThrow(
MisorderedOperationError,
···
})
it('does not allow a create operation in the middle of the log', async () => {
-
const op = await makeNextOp(
-
{
-
alsoKnownAs: ['bob.test'],
-
prev: null,
-
},
-
rotationKey1,
-
)
+
const op = await operations.atprotoOp({
+
signingKey: signingKey.did(),
+
rotationKeys: [rotationKey1.did(), rotationKey2.did()],
+
handle,
+
pds: atpPds,
+
prev: null,
+
signer: rotationKey1,
+
})
expect(data.validateOperationLog(did, [...ops, op])).rejects.toThrow(
MisorderedOperationError,
)