Skip to content

Commit

Permalink
Merge pull request #11 from aviarytech/prerotate-test
Browse files Browse the repository at this point in the history
Add prerotation test into happy path
  • Loading branch information
brianorwhatever authored Jun 7, 2024
2 parents fcc9f33 + ce5db1d commit be9bbd9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ export const documentStateIsValid = async (doc: any, proofs: any[], updateKeys:
return true;
}

export const newKeysAreValid = (updateKeys: string[], previousnextKeyHashes: string[], nextKeyHashes: string[], previousPrerotate: boolean, prerotate: boolean) => {
export const newKeysAreValid = (updateKeys: string[], previousNextKeyHashes: string[], nextKeyHashes: string[], previousPrerotate: boolean, prerotate: boolean) => {
if (prerotate && nextKeyHashes.length === 0) {
throw new Error(`nextKeyHashes are required if prerotation enabled`);
}
if(previousPrerotate) {
const innextKeyHashes = updateKeys.reduce((result, key) => {
const inNextKeyHashes = updateKeys.reduce((result, key) => {
const hashedKey = deriveHash(key);
return result && previousnextKeyHashes.includes(hashedKey);
return result && previousNextKeyHashes.includes(hashedKey);
}, true);
if (!innextKeyHashes) {
if (!inNextKeyHashes) {
throw new Error(`invalid updateKeys ${updateKeys}`);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { resolveDID, createDID, updateDID } from './method';
export { createSigner } from './signing';
export { resolveDID, createDID, updateDID } from './method';
8 changes: 5 additions & 3 deletions src/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const createDID = async (options: CreateDIDInterface): Promise<{did: stri
meta: {
versionId: 1,
created: initialLogEntry[2],
updated: initialLogEntry[2]
updated: initialLogEntry[2],
...(options.prerotate ? {prerotate: true, nextKeyHashes: options.nextKeyHashes} : {})
},
log: [
initialLogEntry
Expand Down Expand Up @@ -115,7 +116,7 @@ export const resolveDID = async (log: DIDLog, options: {versionId?: number, vers
if (entry[3].prerotate === true && (!entry[3].nextKeyHashes || entry[3].nextKeyHashes.length === 0)) {
throw new Error("prerotate enabled without nextKeyHashes");
}
newKeysAreValid(entry[3].updateKeys, nextKeyHashes, entry[3].nextKeyHashes ?? [], prerotate, entry[3].prerotate === true);
newKeysAreValid(entry[3].updateKeys ?? [], nextKeyHashes, entry[3].nextKeyHashes ?? [], prerotate, entry[3].prerotate === true);
const logEntryHash = deriveHash([
previousLogEntryHash,
entry[1],
Expand Down Expand Up @@ -221,7 +222,8 @@ export const updateDID = async (options: UpdateDIDInterface): Promise<{did: stri
versionId: meta.versionId,
created: meta.created,
updated: meta.updated,
previousLogEntryHash: meta.previousLogEntryHash
previousLogEntryHash: meta.previousLogEntryHash,
...(prerotate ? {prerotate: true, nextKeyHashes} : {})
},
log: [
...clone(log),
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/not-authorized.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
["wxfyrzu835m75kukwgcjym035c0a",1,"2024-06-06T21:16:29Z",{"method":"did:tdw:1","scid":"wxfyrzu835m75kukwgcjym035c0a","updateKeys":["did:key:z6Mkw1KSvGWNAwSwWbcpwPgFARX4vKPa1xvcDMsJ5b48Zj6B"]},{"value":{"@context":["https://www.w3.org/ns/did/v1","https://w3id.org/security/multikey/v1"],"id":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a","controller":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a","authentication":["did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#5b48Zj6B"],"assertionMethod":["did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#CpsUrTh3"],"verificationMethod":[{"id":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#5b48Zj6B","controller":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a","type":"Multikey","publicKeyMultibase":"z6Mkw1KSvGWNAwSwWbcpwPgFARX4vKPa1xvcDMsJ5b48Zj6B"},{"id":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#CpsUrTh3","controller":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a","type":"Multikey","publicKeyMultibase":"z6MkuocstfAaHsJgRnQgfQdtJiECuWEMDAbQV61aCpsUrTh3"}]}},[{"type":"DataIntegrityProof","cryptosuite":"eddsa-jcs-2022","verificationMethod":"did:key:z6Mkw1KSvGWNAwSwWbcpwPgFARX4vKPa1xvcDMsJ5b48Zj6B","created":"2024-06-06T21:16:29Z","proofPurpose":"authentication","challenge":"8c8g64vzhyv72z41md4rrwc986500adh3qkgn9xxpqrtcwv7vgkg","proofValue":"zBKncD6Edq8vPZntWQerPyzKhBgzc8Xgv3Qs2s4YxUxTpqtK5MQRcf22WiqNGTgx56ZNHxhij5NSTRYtDiXiY373"}]]
["jyynj9jmbez302nm4fpjmnpubp4hax7e05z8v2m7uexvprdr2vc0",2,"2024-06-06T21:16:29Z",{},{"patch":[{"op":"replace","path":"/verificationMethod/1/publicKeyMultibase","value":"z6Mkq7Qoa2LCvLCUiq9W2J9vXH1ooDqSX2ehWGUzCZSeAWLE"},{"op":"replace","path":"/verificationMethod/1/id","value":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#CZSeAWLE"},{"op":"replace","path":"/verificationMethod/0/publicKeyMultibase","value":"z6MksNvDtgMutKMzjezRqj8JbYuCHNjHR5HBaF2ogJqvqaYD"},{"op":"replace","path":"/verificationMethod/0/id","value":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#gJqvqaYD"},{"op":"replace","path":"/assertionMethod/0","value":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#CZSeAWLE"},{"op":"replace","path":"/authentication/0","value":"did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a#gJqvqaYD"},{"op":"replace","path":"/controller","value":["did:tdw:example.com:wxfyrzu835m75kukwgcjym035c0a"]}]},[{"type":"DataIntegrityProof","cryptosuite":"eddsa-jcs-2022","verificationMethod":"did:key:z6MkuocstfAaHsJgRnQgfQdtJiECuWEMDAbQV61aCpsUrTh3","created":"2024-06-06T21:16:29Z","proofPurpose":"authentication","challenge":"jyynj9jmbez302nm4fpjmnpubp4hax7e05z8v2m7uexvprdr2vc0","proofValue":"z25iTrGQc3oytL7dfhTbZ4Skad86c1SUQ17jpvurKoEmSkXU6f45iG4hqQK7mUBf7F1n27VitgRAkU4gP9rsM9HDM"}]]
["hnwg7aeq1dwu68vbw7ncyh2eqf20",1,"2024-06-07T06:20:56Z",{"method":"did:tdw:1","scid":"hnwg7aeq1dwu68vbw7ncyh2eqf20","updateKeys":["did:key:z6Mkw1KSvGWNAwSwWbcpwPgFARX4vKPa1xvcDMsJ5b48Zj6B"]},{"value":{"@context":["https://www.w3.org/ns/did/v1","https://w3id.org/security/multikey/v1"],"id":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20","controller":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20","authentication":["did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#5b48Zj6B"],"assertionMethod":["did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#CpsUrTh3"],"verificationMethod":[{"id":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#5b48Zj6B","controller":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20","type":"Multikey","publicKeyMultibase":"z6Mkw1KSvGWNAwSwWbcpwPgFARX4vKPa1xvcDMsJ5b48Zj6B"},{"id":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#CpsUrTh3","controller":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20","type":"Multikey","publicKeyMultibase":"z6MkuocstfAaHsJgRnQgfQdtJiECuWEMDAbQV61aCpsUrTh3"}]}},[{"type":"DataIntegrityProof","cryptosuite":"eddsa-jcs-2022","verificationMethod":"did:key:z6Mkw1KSvGWNAwSwWbcpwPgFARX4vKPa1xvcDMsJ5b48Zj6B","created":"2024-06-07T06:20:56Z","proofPurpose":"authentication","challenge":"zhn9485uaunxvay9xfftfrrquhrnr08rpxangnbzrx5mz1dw86n0","proofValue":"z3NS6qD14oxnPwCq3neFHpLWpoWustv6eCc2futpR1X9KXe17p4Gzk934FizW56mXHAurH5e6PgGJxPkcVewwDYFz"}]]
["hyz4v5h3fcykwfp4vpqubfwafkjpz2uzyd1r0f9t5j96j3nra0gg",2,"2024-06-07T06:20:56Z",{},{"patch":[{"op":"replace","path":"/verificationMethod/1/publicKeyMultibase","value":"z6Mkq7Qoa2LCvLCUiq9W2J9vXH1ooDqSX2ehWGUzCZSeAWLE"},{"op":"replace","path":"/verificationMethod/1/id","value":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#CZSeAWLE"},{"op":"replace","path":"/verificationMethod/0/publicKeyMultibase","value":"z6MksNvDtgMutKMzjezRqj8JbYuCHNjHR5HBaF2ogJqvqaYD"},{"op":"replace","path":"/verificationMethod/0/id","value":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#gJqvqaYD"},{"op":"replace","path":"/assertionMethod/0","value":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#CZSeAWLE"},{"op":"replace","path":"/authentication/0","value":"did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20#gJqvqaYD"},{"op":"replace","path":"/controller","value":["did:tdw:example.com:hnwg7aeq1dwu68vbw7ncyh2eqf20"]}]},[{"type":"DataIntegrityProof","cryptosuite":"eddsa-jcs-2022","verificationMethod":"did:key:z6MkuocstfAaHsJgRnQgfQdtJiECuWEMDAbQV61aCpsUrTh3","created":"2024-06-07T06:20:56Z","proofPurpose":"authentication","challenge":"hyz4v5h3fcykwfp4vpqubfwafkjpz2uzyd1r0f9t5j96j3nra0gg","proofValue":"z21usj5WzobaUeNDs1KAzzFWVSQad7ScrZTmLxeD4QJiu2pShD4nn4wpjjdPV4F4XanVLM1y2sd2Yh7WQWSkSfbhM"}]]
53 changes: 48 additions & 5 deletions test/happy-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect, beforeAll } from "bun:test";
import { createDID, deactivateDID, resolveDID, updateDID } from "../src/method";
import fs from 'node:fs';
import { readLogFromDisk, readKeysFromDisk } from "./utils";
import { createVMID } from "../src/utils";
import { createVMID, deriveHash } from "../src/utils";
import { METHOD } from "../src/constants";
import { createSigner } from "../src/signing";

Expand Down Expand Up @@ -249,6 +249,49 @@ test("Resolve DID version 5", async () => {
await testResolveVersion(5);
});

test("Update DID (enable prerotate)", async () => {
let didLog = readLogFromDisk(logFile);
const {doc} = await resolveDID(didLog);
if (availableKeys.ed25519.length === 0) {
const {keys} = readKeysFromDisk();
availableKeys = JSON.parse(keys);
}

const nextAuthKey = {type: 'authentication' as const, ...availableKeys.ed25519.shift()};
const nextNextAuthKey = {type: 'authentication' as const, ...availableKeys.ed25519.shift()};
const nextNextKeyHash = deriveHash(`did:key:${nextNextAuthKey.publicKeyMultibase}`);
const {did: updatedDID, doc: updatedDoc, meta, log: updatedLog} =
await updateDID({
log: didLog,
signer: createSigner(currentAuthKey!),
updateKeys: [
`did:key:${nextAuthKey.publicKeyMultibase}`
],
prerotate: true,
nextKeyHashes: [nextNextKeyHash],
context: doc['@context'],
verificationMethods: [
nextAuthKey
],
services: doc.service,
alsoKnownAs: ['did:web:example.com']
});
didLog = [...updatedLog];
expect(updatedDID).toBe(did);
expect(updatedDoc.controller).toContain(did)
expect(meta.prerotate).toBe(true);
expect(meta.nextKeyHashes).toContain(nextNextKeyHash);

expect(meta.versionId).toBe(6);

writeFilesToDisk(updatedLog, updatedDoc, 6);
currentAuthKey = nextAuthKey;
});

test("Resolve DID version 6", async () => {
await testResolveVersion(6);
});

// ADD ANY NEW TESTS HERE AND BUMP VERSION NUMBER AT END OF FILE

test("Deactivate DID", async () => {
Expand All @@ -271,11 +314,11 @@ test("Deactivate DID", async () => {
expect(updatedDoc.verificationMethod.length).toBe(0);
expect(meta.deactivated).toBe(true);

expect(meta.versionId).toBe(6);
expect(meta.versionId).toBe(7);

writeFilesToDisk(updatedLog, updatedDoc, 6);
writeFilesToDisk(updatedLog, updatedDoc, 7);
});

test("Resolve DID version 6", async () => {
await testResolveVersion(6);
test("Resolve DID version 7", async () => {
await testResolveVersion(7);
});

0 comments on commit be9bbd9

Please sign in to comment.