@civic/sol-did-client
Version:
A powerful DID-method on Solana
155 lines • 4.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapMethodExtension = exports.DidSolIdentifier = void 0;
const web3_js_1 = require("@solana/web3.js");
const utils_1 = require("./lib/utils");
const const_1 = require("./lib/const");
/**
* A class representing a SOL Did
*/
class DidSolIdentifier {
/**
* Creates a new `DecentralizedIdentifier` from its requisite parts.
*
* Use `DecentralizedIdentifier::parse` to obtain this from a direct did address.
*
* @param constructor The construction values
*/
constructor(constructor) {
this.clusterType = constructor.clusterType;
this.authority = constructor.authority;
this.fragment = constructor.fragment;
}
/**
* Get the key to the DID data
*/
dataAccount() {
return (0, utils_1.findProgramAddress)(this.authority);
}
legacyDataAccount() {
return (0, utils_1.findLegacyProgramAddress)(this.authority);
}
/**
* Clones this
*/
clone() {
return new DidSolIdentifier({
clusterType: this.clusterType,
authority: this.authority,
fragment: this.fragment,
});
}
/**
* Returns a new `DecentralizedIdentifier` but with `urlField` swapped to the parameter
* @param urlField The new url field
*/
withUrl(urlField) {
return new DidSolIdentifier(Object.assign(Object.assign({}, this), { fragment: urlField }));
}
get clusterString() {
if (!this.clusterType) {
return 'unknown:';
}
if (this.clusterType === 'mainnet-beta') {
return '';
}
return `${this.clusterType}:`;
}
toString(includeURL = true) {
const path = ''; // TODO
const query = ''; // TODO
const fragment = !this.fragment || this.fragment === '' ? '' : `#${this.fragment}`;
let urlExtension = '';
if (includeURL) {
urlExtension = `${path}${query}${fragment}`;
}
return `${const_1.DID_SOL_PREFIX}:${this.clusterString}${this.authority.toBase58()}${urlExtension}`;
}
/**
* Parses a given did string
* @param did the did string
*/
static parse(did) {
if ((0, utils_1.isStringDID)(did)) {
const matches = DidSolIdentifier.REGEX.exec(did);
if (!matches)
throw new Error('Invalid DID');
const authority = new web3_js_1.PublicKey(matches[2]);
return new DidSolIdentifier({
clusterType: (0, exports.mapMethodExtension)(matches[1]),
authority,
fragment: matches[3],
});
}
else {
throw new Error('Provided DID is not a string');
}
}
parseFragmentFromId(id) {
// fragment self-reference.
if (id.startsWith('#')) {
return id.replace('#', '');
}
const didIdentifierPrefix = this.toString(false) + '#';
if (id.startsWith(didIdentifierPrefix)) {
return id.replace(didIdentifierPrefix, '');
}
throw new Error(`${id} does not conform to the DID spec`);
}
/**
* Returns true if the did is valid
* @param did The did string to check
*/
static valid(did) {
try {
DidSolIdentifier.parse(did);
return true;
}
catch (_a) {
return false;
}
}
/**
* Parses an array of did strings
* @param dids The did strings to parse
*/
static parseMaybeArray(dids) {
return dids ? dids.map((v) => DidSolIdentifier.parse(v)) : [];
}
/**
* Creates a new did
* @param authority The authority and key of the did
* @param clusterType The cluster the did points to
* @param urlField An optional extra field
*/
static create(authority, clusterType, urlField) {
return new DidSolIdentifier({
authority,
clusterType,
fragment: urlField,
});
}
}
exports.DidSolIdentifier = DidSolIdentifier;
// Note fragment is always the last field in the URI.
// https://www.rfc-editor.org/rfc/rfc3986#section-3.5
// TODO: Note, this REGEX is not robust towards URI spec, specifically paths and queries.
// TODO add support for / urls and ? query params
DidSolIdentifier.REGEX = new RegExp(`^${const_1.DID_SOL_PREFIX}:?(\\w*):(\\w+)#?(\\w*)$`);
const mapMethodExtension = (clusterString) => {
switch (clusterString) {
case '':
return 'mainnet-beta';
case 'devnet':
return 'devnet';
case 'testnet':
return 'testnet';
case 'localnet':
return 'localnet';
case 'civicnet':
return 'civicnet';
}
// return undefined if not found
};
exports.mapMethodExtension = mapMethodExtension;
//# sourceMappingURL=DidSolIdentifier.js.map