UNPKG

@gleif-it/did-webs-ts

Version:
54 lines (53 loc) 2.49 kB
import { generateCapabilityDelegationBlock } from './generateCapabilityDelegationBlock.js'; import { generateConditionalProofBlock } from './generateConditionalProofBlock.js'; import { generateKeyVerificationBlock } from './generateKeyVerificationBlock.js'; import { calculateThreshold } from '../document/calculateThreshold.js'; import { generateDidWeb } from './generateDidWeb.js'; import { generateDidKeri } from './generateDidKeri.js'; export const generateDocument = (controllerDid, controller, delegatorDid, delegator) => { // make sure both delegate paramers are provided or none if (delegatorDid && !delegator) { throw new Error('Delegator did provided without delegator keys'); } if (delegator && !delegatorDid) { throw new Error('Delegator keys provided without delegator did'); } // various helpful values and objects const controllerIdentifier = controller.identifier; const controllerKeyState = controller.keyState; const controllerThreshold = calculateThreshold(controllerKeyState.kt); // const delagatorKeyState = hasDelegator ? delegator?.keyState : undefined; // const delegatorThresholdIsFractional = hasDelegator // ? isFractionalThreshold(delagatorKeyState?.kt) // : undefined; // const delegatorThreshold = hasDelegator // ? calcutaleThreshold(delagatorKeyState?.kt) // : undefined; // some pregenerated blocks const conditionalProofBlock = generateConditionalProofBlock(controller, controllerDid); const keyVerificationBlock = generateKeyVerificationBlock(controller, controllerDid); const optionalCapabilityDelegationBlock = generateCapabilityDelegationBlock(delegator, delegatorDid); // use pregenerated values to create the document return { id: controllerDid, verificationMethod: controllerThreshold > 1 ? [conditionalProofBlock, ...keyVerificationBlock] : keyVerificationBlock, authentication: [ controllerThreshold > 1 ? `#${controllerIdentifier}` : `#${controllerKeyState.k[0]}`, ], assertionMethod: [ controllerThreshold > 1 ? `#${controllerIdentifier}` : `#${controllerKeyState.k[0]}`, ], ...optionalCapabilityDelegationBlock, service: [], alsoKnownAs: [ generateDidWeb(controllerDid), generateDidKeri(controllerDid), ], }; };