Fork of github.com/did-method-plc/did-method-plc
1import { cidForCbor, DAY } from '@atproto/common'
2import { Secp256k1Keypair } from '@atproto/crypto'
3import {
4 assureValidNextOp,
5 CreateOpV1,
6 deprecatedSignCreate,
7 didForCreateOp,
8 normalizeOp,
9 updateRotationKeysOp,
10 updateAtprotoKeyOp,
11 validateOperationLog,
12} from '../src'
13
14describe('compatibility', () => {
15 let signingKey: Secp256k1Keypair
16 let recoveryKey: Secp256k1Keypair
17 const handle = 'alice.test'
18 const service = 'https://example.com'
19 let did: string
20
21 let legacyOp: CreateOpV1
22
23 beforeAll(async () => {
24 signingKey = await Secp256k1Keypair.create()
25 recoveryKey = await Secp256k1Keypair.create()
26 })
27
28 it('normalizes legacy create ops', async () => {
29 legacyOp = await deprecatedSignCreate(
30 {
31 type: 'create',
32 signingKey: signingKey.did(),
33 recoveryKey: recoveryKey.did(),
34 handle,
35 service,
36 prev: null,
37 },
38 signingKey,
39 )
40
41 did = await didForCreateOp(legacyOp)
42
43 const normalized = normalizeOp(legacyOp)
44 expect(normalized).toEqual({
45 type: 'plc_operation',
46 verificationMethods: {
47 atproto: signingKey.did(),
48 },
49 rotationKeys: [recoveryKey.did(), signingKey.did()],
50 alsoKnownAs: [`at://${handle}`],
51 services: {
52 atproto_pds: {
53 type: 'AtprotoPersonalDataServer',
54 endpoint: service,
55 },
56 },
57 prev: null,
58 sig: legacyOp.sig,
59 })
60 })
61
62 it('validates a log with a legacy create op', async () => {
63 const legacyCid = await cidForCbor(legacyOp)
64 const newSigner = await Secp256k1Keypair.create()
65 const newRotater = await Secp256k1Keypair.create()
66 const nextOp = await updateAtprotoKeyOp(
67 legacyOp,
68 signingKey,
69 newSigner.did(),
70 )
71 const anotherOp = await updateRotationKeysOp(nextOp, signingKey, [
72 newRotater.did(),
73 ])
74 await validateOperationLog(did, [legacyOp, nextOp])
75 await validateOperationLog(did, [legacyOp, nextOp, anotherOp])
76
77 const indexedLegacy = {
78 did,
79 operation: legacyOp,
80 cid: legacyCid,
81 nullified: false,
82 createdAt: new Date(Date.now() - 7 * DAY),
83 }
84
85 const result = await assureValidNextOp(did, [indexedLegacy], nextOp)
86 expect(result.nullified.length).toBe(0)
87 expect(result.prev?.equals(legacyCid)).toBeTruthy()
88 })
89})