Castor
@hyperledger/identus-sdk v7.0.0
@hyperledger/identus-sdk / overview / Castor
Class: Castor
Defined in: src/castor/Castor.ts:42
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.
Castor
Implements
Constructors
Constructor
new Castor(
apollo:Apollo,extraResolvers:ExtraResolver[],prismResolverEndpoint:string):Castor
Defined in: src/castor/Castor.ts:52
Creates an instance of Castor as soon as a valid cryptographic interface is provided (Apollo).
Parameters
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| apollo | Apollo | undefined | |
| extraResolvers | ExtraResolver[] | [] | |
| prismResolverEndpoint | string | "https://raw.githubusercontent.com/FabioPinheiro/prism-vdr/refs/heads/main/mainnet/diddoc/" | - | 
Returns
Castor
Methods
createPeerDID()
createPeerDID(
publicKeys:PublicKey[],services:Service[]):Promise<DID>
Defined in: src/castor/Castor.ts:241
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.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| publicKeys | PublicKey[] | |
| services | Service[] | 
Returns
Promise<DID>
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.
const peerDid = await castor.createPeerDID(
    [keyPairFromCurveEd25519, keyPairFromCurveX25519],
    [exampleService]
);
Implementation of
createPrismDID()
createPrismDID(
key:PublicKey|KeyPair,services?:Service[],authenticationKeys?: (PublicKey|KeyPair)[],issuanceKeys?: (PublicKey|KeyPair)[]):Promise<DID>
Defined in: src/castor/Castor.ts:165
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.
Parameters
| Parameter | Type | Default value | Description | 
|---|---|---|---|
| key | PublicKey|KeyPair | undefined | |
| services? | Service[] | undefined | |
| authenticationKeys? | ( PublicKey|KeyPair)[] | [] | |
| issuanceKeys? | ( PublicKey|KeyPair)[] | [] | 
Returns
Promise<DID>
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.
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]
);
Implementation of
createPrismDIDAtalaObject()
createPrismDIDAtalaObject(
key:PrivateKey,did:DID):Promise<Uint8Array<ArrayBufferLike>>
Defined in: src/castor/Castor.ts:90
Creates a Prism DID Atala Object, a buffer contained the prism did create operation.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| key | PrivateKey | |
| did | DID | 
Returns
Promise<Uint8Array<ArrayBufferLike>>
getEcnumbasis()
Defined in: src/castor/Castor.ts:445
Returns ecnumbasis from a valid DID and its related publicKey
Parameters
| Parameter | Type | Description | 
|---|---|---|
| did | DID | |
| publicKey | PublicKey | 
Returns
string
Implementation of
parseDID()
parseDID(
did:string):DID
Defined in: src/castor/Castor.ts:80
Parses a string representation of a Decentralized Identifier (DID) into a DID object.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| did | string | 
Returns
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.
const parsedPrismDid = castor.parseDID(
 "did:prism:b6c0c33d701ac1b9a262a14454d1bbde3d127d697a76950963c5fd930605:Cj8KPRI7CgdtYXN0ZXIwEAFKLgoJc2VmsxEiECSTjyV7sUfCr_ArpN9rvCwR9fRMAhcsr_S7ZRiJk4p5k"
);
Implementation of
resolveDID()
resolveDID(
didstr:string|DID):Promise<DIDDocument>
Defined in: src/castor/Castor.ts:266
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!
Parameters
| Parameter | Type | Description | 
|---|---|---|
| didstr | string|DID | 
Returns
Promise<DIDDocument>
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.
const didDocument = await castor.resolveDID("did:prism:123456")
Implementation of
verifySignature()
verifySignature(
did:DID,challenge:Uint8Array,signature:Uint8Array):Promise<boolean>
Defined in: src/castor/Castor.ts:336
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.
Parameters
| Parameter | Type | Description | 
|---|---|---|
| did | DID | |
| challenge | Uint8Array | |
| signature | Uint8Array | 
Returns
Promise<boolean>
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.
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
  );
}