@civic/sol-did-client
Version:
A powerful DID-method on Solana
140 lines • 4.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerificationMethodFlags = exports.VerificationMethod = exports.DidSolDataAccount = void 0;
const types_1 = require("./types");
const anchor_1 = require("@coral-xyz/anchor");
const utils_1 = require("./utils");
const DidSolIdentifier_1 = require("../DidSolIdentifier");
const web3_js_1 = require("@solana/web3.js");
const const_1 = require("./const");
/**
* A class representing the on-chain data for a SOL DID
*/
class DidSolDataAccount {
constructor(_rawDidDataAccount, _cluster) {
this._rawDidDataAccount = _rawDidDataAccount;
this._cluster = _cluster;
this._identifier = DidSolIdentifier_1.DidSolIdentifier.create(new web3_js_1.PublicKey(this._rawDidDataAccount.initialVerificationMethod.keyData), this._cluster);
}
static from(rawDidDataAccount, cluster) {
return new DidSolDataAccount(rawDidDataAccount, cluster);
}
static generative(identifier) {
if (!identifier.clusterType) {
throw new Error(`Cannot create generative DID from unknown cluster: ${identifier.toString()}`);
}
// calculate bump
const [_, bump] = (0, utils_1.findProgramAddress)(identifier.authority);
return new DidSolDataAccount({
version: 0,
bump,
nonce: new anchor_1.BN(0),
initialVerificationMethod: {
fragment: const_1.DEFAULT_KEY_ID,
methodType: types_1.VerificationMethodType.Ed25519VerificationKey2018,
keyData: identifier.authority.toBuffer(),
flags: types_1.BitwiseVerificationMethodFlag.CapabilityInvocation |
types_1.BitwiseVerificationMethodFlag.OwnershipProof,
},
verificationMethods: [],
services: [],
nativeControllers: [],
otherControllers: [],
}, identifier.clusterType);
}
get identifier() {
return this._identifier;
}
get raw() {
return this._rawDidDataAccount;
}
get version() {
return this._rawDidDataAccount.version;
}
get bump() {
return this._rawDidDataAccount.bump;
}
get nonce() {
return this._rawDidDataAccount.nonce;
}
get verificationMethods() {
return [
this._rawDidDataAccount.initialVerificationMethod,
...this._rawDidDataAccount.verificationMethods,
].map(VerificationMethod.from);
}
get services() {
return this._rawDidDataAccount.services;
}
get controllers() {
return (0, utils_1.mapControllers)(this._rawDidDataAccount.nativeControllers, this._rawDidDataAccount.otherControllers, this._cluster);
}
}
exports.DidSolDataAccount = DidSolDataAccount;
class VerificationMethod {
constructor(_rawVerificationMethod) {
this._rawVerificationMethod = _rawVerificationMethod;
}
get raw() {
return this._rawVerificationMethod;
}
static from(rawVerificationMethod) {
return new VerificationMethod(rawVerificationMethod);
}
toParams() {
return {
fragment: this.fragment,
keyData: this.keyData,
methodType: this.methodType,
flags: this.flags.array,
};
}
get fragment() {
return this._rawVerificationMethod.fragment;
}
get keyData() {
return this._rawVerificationMethod.keyData;
}
get methodType() {
return this._rawVerificationMethod.methodType;
}
get flags() {
return new VerificationMethodFlags(this._rawVerificationMethod.flags);
}
}
exports.VerificationMethod = VerificationMethod;
class VerificationMethodFlags {
constructor(_flags) {
this._flags = _flags;
}
static none() {
return new VerificationMethodFlags(0);
}
static of(flags) {
return new VerificationMethodFlags(flags);
}
get raw() {
return this._flags;
}
static ofArray(flags) {
return flags.reduce((acc, flag) => acc.set(flag), VerificationMethodFlags.none());
}
get array() {
return Object.keys(types_1.BitwiseVerificationMethodFlag)
.map((i) => parseInt(i))
.filter((i) => !isNaN(i) && this.has(i));
}
has(flag) {
return (this._flags & flag) === flag;
}
set(flag) {
this._flags |= flag;
return this;
}
clear(flag) {
this._flags &= ~flag;
return this;
}
}
exports.VerificationMethodFlags = VerificationMethodFlags;
//# sourceMappingURL=wrappers.js.map