@civic/sol-did-client
Version:
A powerful DID-method on Solana
148 lines • 6.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DidSolDocument = void 0;
const const_1 = require("./lib/const");
const types_1 = require("./lib/types");
const DidSolIdentifier_1 = require("./DidSolIdentifier");
const utils_1 = require("./lib/utils");
const wrappers_1 = require("./lib/wrappers");
/**
* A class representing a did:sol document
* The document is less permissive than the DIDDocument specification that it implements.
*/
class DidSolDocument {
constructor(identifier) {
// private identifier: DidSolIdentifier;
this['@context'] = DidSolDocument.defaultContext();
// public alsoKnownAs?: string[];
this.controller = [];
this.verificationMethod = [];
this.authentication = [];
this.assertionMethod = [];
this.keyAgreement = [];
this.capabilityInvocation = [];
this.capabilityDelegation = [];
this.service = [];
this.id = identifier.toString();
// default to generative case
Object.assign(this, (0, utils_1.mapVerificationMethodsToDidComponents)([(0, utils_1.defaultVerificationMethod)(identifier.authority)], identifier));
}
static defaultContext(version = '2.0') {
return [const_1.W3ID_CONTEXT, (0, const_1.getSolContextPrefix)(version)];
}
static sparse(identifier) {
return new DidSolDocument(identifier);
}
static from(account) {
const doc = DidSolDocument.sparse(account.identifier);
// VM related
Object.assign(doc, (0, utils_1.mapVerificationMethodsToDidComponents)(account.verificationMethods, account.identifier));
// Services
doc.service = (0, utils_1.mapServices)(account.services, account.identifier);
// Controllers
doc.controller = account.controllers;
return doc;
}
static fromDoc(document) {
const didSolDocument = new DidSolDocument(DidSolIdentifier_1.DidSolIdentifier.parse(document.id));
// check requirements
if (document.controller && !Array.isArray(document.controller)) {
throw new Error('DIDDocument.controller must be an string array');
}
if (document.authentication &&
!document.authentication.every((id) => typeof id === 'string')) {
throw new Error('DIDDocument.authentication must be an string array');
}
if (document.assertionMethod &&
!document.assertionMethod.every((id) => typeof id === 'string')) {
throw new Error('DIDDocument.assertionMethod must be an string array');
}
if (document.keyAgreement &&
!document.keyAgreement.every((id) => typeof id === 'string')) {
throw new Error('DIDDocument.keyAgreement must be an string array');
}
if (document.capabilityInvocation &&
!document.capabilityInvocation.every((id) => typeof id === 'string')) {
throw new Error('DIDDocument.capabilityInvocation must be an string array');
}
if (document.capabilityDelegation &&
!document.capabilityDelegation.every((id) => typeof id === 'string')) {
throw new Error('DIDDocument.capabilityDelegation must be an string array');
}
Object.assign(didSolDocument, document);
return didSolDocument;
}
getDocUpdateArgs() {
const args = {
controllerDIDs: [],
verificationMethods: [],
services: [],
};
const didSolIdentifier = DidSolIdentifier_1.DidSolIdentifier.parse(this.id);
// Controllers
if (this.controller) {
args.controllerDIDs = this.controller;
}
// Services
if (this.service) {
args.services = this.service.map((service) => {
return {
fragment: didSolIdentifier.parseFragmentFromId(service.id),
serviceType: service.type,
serviceEndpoint: service.serviceEndpoint,
};
});
}
// Verification Methods
if (this.verificationMethod) {
args.verificationMethods = this.verificationMethod.map((vm) => this.mapVerificationMethod(vm));
}
return args;
}
getFlagsFromVerificationMethod(fragment) {
let flags = 0;
if (this.authentication &&
this.authentication.find((id) => id.endsWith(`#${fragment}`))) {
flags |= types_1.BitwiseVerificationMethodFlag.Authentication;
}
if (this.assertionMethod &&
this.assertionMethod.find((id) => id.endsWith(`#${fragment}`))) {
flags |= types_1.BitwiseVerificationMethodFlag.Assertion;
}
if (this.keyAgreement &&
this.keyAgreement.find((id) => id.endsWith(`#${fragment}`))) {
flags |= types_1.BitwiseVerificationMethodFlag.KeyAgreement;
}
if (this.capabilityInvocation &&
this.capabilityInvocation.find((id) => id.endsWith(`#${fragment}`))) {
flags |= types_1.BitwiseVerificationMethodFlag.CapabilityInvocation;
}
if (this.capabilityDelegation &&
this.capabilityDelegation.find((id) => id.endsWith(`#${fragment}`))) {
flags |= types_1.BitwiseVerificationMethodFlag.CapabilityDelegation;
}
return flags;
}
/**
* Map a DidVerificationMethod to a compressed did:sol VerificationMethod with flags.
* @param vm DidVerificationMethod to map
*/
mapVerificationMethod(vm) {
const id = DidSolIdentifier_1.DidSolIdentifier.parse(this.id);
const methodType = types_1.VerificationMethodType[vm.type];
if (methodType === undefined) {
throw new Error(`Unknown verification method type '${vm.type}'`);
}
const fragment = id.parseFragmentFromId(vm.id);
const flags = this.getFlagsFromVerificationMethod(fragment);
const keyData = (0, utils_1.getKeyDataFromVerificationMethod)(vm);
return {
fragment,
methodType,
flags: wrappers_1.VerificationMethodFlags.of(flags).array,
keyData,
};
}
}
exports.DidSolDocument = DidSolDocument;
//# sourceMappingURL=DidSolDocument.js.map