import * as Domain from "../domain"; import { DIDDocument } from "../domain/models"; type ExtraResolver = new (apollo: Domain.Apollo) => Domain.DIDResolver; /** * Castor is a powerful and flexible library for working with DIDs. Whether you are building a decentralised application * or a more traditional system requiring secure and private identity management, Castor provides the tools and features * you need to easily create, manage, and resolve DIDs. * * @class Castor * @typedef {Castor} */ export default class Castor implements Domain.Castor { private apollo; private resolvers; /** * Creates an instance of Castor as soon as a valid cryptographic interface is provided (Apollo). * * @constructor * @param {Apollo} apollo * @param {ExtraResolver[]} extraResolvers */ constructor(apollo: Domain.Apollo, extraResolvers?: ExtraResolver[]); /** * Parses a string representation of a Decentralized Identifier (DID) into a DID object. * * @example * This function takes a string representation of a DID and returns an instance of `Domain.DID`. It may throw an error if the string is not a valid * DID. * * ```ts * const parsedPrismDid = castor.parseDID( * "did:prism:b6c0c33d701ac1b9a262a14454d1bbde3d127d697a76950963c5fd930605:Cj8KPRI7CgdtYXN0ZXIwEAFKLgoJc2VmsxEiECSTjyV7sUfCr_ArpN9rvCwR9fRMAhcsr_S7ZRiJk4p5k" * ); * ``` * * @param {string} did * @returns {DID} */ parseDID(did: string): Domain.DID; /** * Creates a Prism DID Atala Object, a buffer contained the prism did create operation. * @param {PrivateKey} key * @param {Service[]} services * @param {PublicKey[]} authenticationKeys * @param {PublicKey[]} issuanceKeys * @returns {Promise<{operationHex: string, metadataBody: {v: number, c: string[]}, did: DID}>} */ createPrismDIDAtalaObject(key: Domain.PrivateKey, did: Domain.DID): Promise>; /** * Creates a DID for a prism (a device or server that acts as a DID owner and controller) using a * given master public key and list of services. * * @example * This function creates a new `prism` DID, using a given master Public Key and a list of Services. * The Public Key may be an individual Key or a KeyPair * It may throw an error if the master Public Key or Services are invalid. * * ```ts * const exampleServiceEndpoint = new Domain.DIDDocument.Service("didcomm", ["DIDCommMessaging"], { * uri: "https://example.com/endpoint", * accept: ["didcomm/v2"], * routingKeys: ["did:example:somemediator#somekey"], * }); * const prismDid = await castor.createPrismDID( * keyPairFromCurveSecp256K1.publicKey, * [exampleServiceEndpoint] * ); * ``` * * @async * @param {PrivateKey | KeyPair} masterPublicKey * @param {?(Service[] | undefined)} [services] * @param {?(PublicKey[] | undefined)} [authenticationKeys] * @param {?(PublicKey[] | undefined)} [issuanceKeys] * @returns {Promise} */ createPrismDID(key: Domain.PublicKey | Domain.KeyPair, services?: DIDDocument.Service[] | undefined, authenticationKeys?: (Domain.PublicKey | Domain.KeyPair)[], issuanceKeys?: (Domain.PublicKey | Domain.KeyPair)[]): Promise; /** * Creates a DID for a peer (a device or server that acts as a DID subject) using given key agreement * and authentication key pairs and a list of services. * * @example * This function creates new peer DID, using a given key agreement, authentication key pairs, and a list of services. It may throw an error if the key pairs or services are invalid. * * ```ts * const peerDid = await castor.createPeerDID( * [keyPairFromCurveEd25519, keyPairFromCurveX25519], * [exampleService] * ); * ``` * * @async * @param {PublicKey[]} publicKeys * @param {Service[]} services * @returns {Promise} */ createPeerDID(publicKeys: Domain.PublicKey[], services: DIDDocument.Service[]): Promise; /** * Asynchronously resolves a DID to its corresponding DID Document. This function may throw an error if * the DID is invalid or the document cannot be retrieved. * **Note:** only `prism` and `peer` DID methods are currently supported! * * @example * This function asynchronously resolves a DID to its corresponding DID Document. It may throw an error if the DID is invalid or the document is unretrievable. * * ```ts * const didDocument = await castor.resolveDID("did:prism:123456") * ``` * * @async * @param {string} did * @returns {Promise} */ resolveDID(didstr: Domain.DID | string): Promise; /** * Extracts the verificationMethods from an array of CoreProperties inside a DID Document * * @private * @param {DIDDocumentCoreProperty[]} coreProperties * @returns {DIDDocumentVerificationMethod[]} */ private extractVerificationMethods; /** * Verifies the authenticity of a signature using the corresponding DID Document, challenge, and signature data. * This function returns a boolean value indicating whether the signature is valid or not. This function may throw * an error if the DID Document or signature data are invalid. * * @example * This function verifies the authenticity of a signature using given DID, challenge, and signature data. It returns a boolean value indicating whether the signature is valid or not. It may throw an error if the DID or signature data are invalid. * * ```ts * const message = "data to sign"; * const messageBytes = new TextEncoder().encode(message); * const {mnemonics, seed} = apollo.createRandomSeed(); * const privateKey = apollo.createPublicKey({ * type: KeyTypes.EC, * curve: Curve.SECP256K1, * seed: Buffer.from(seed.value).toString("hex"), * derivationPath: "m/0'/0'/0'" * }); * if (privateKey.isSignable()) { * const signature = privateKey.sign(message); * const did = castor.parseDID("did:prism:123456"); * const challenge = messageBytes * const isValid = castor.verifySignature( * castor.parseDID("did:prism:123456"), * challenge, // Uint8Array * signature // Uint8Array * ); * } * ``` * * @async * @param {DID} did * @param {Uint8Array} challenge * @param {Uint8Array} signature * @returns {Promise} */ verifySignature(did: Domain.DID, challenge: Uint8Array, signature: Uint8Array): Promise; /** * Returns ecnumbasis from a valid DID and its related publicKey * * @param {DID} did * @param {PublicKey} publicKey * @returns {string} */ getEcnumbasis(did: Domain.DID, publicKey: Domain.PublicKey): string; /** * create an identifier for keys within a DID Document * should be unique within the Document * * @param keyUsage - maps to a prefix word * @param index - occurrence of this keyUsage * @returns {string} */ private getUsageId; /** * Return usage from a verification method id * * @param id: string - verification method id string * @returns {Usage} */ private getUsageFromId; private createProtos; private getPrismDIDKeyFromVerificationMethod; } export {};