@veramo/did-provider-peer
Version:
Veramo plugin that can enable creation and control of did:peer identifiers.
129 lines • 5.42 kB
JavaScript
import { AbstractIdentifierProvider } from '@veramo/did-manager';
import { bytesToBase64url, bytesToMultibase, hexToBytes, stringToUtf8Bytes, importOrCreateKey } from '@veramo/utils';
import Debug from 'debug';
const debug = Debug('veramo:did-peer:identifier-provider');
const ServiceReplacements = {
type: 't',
DIDCommMessaging: 'dm',
serviceEndpoint: 's',
routingKeys: 'r',
accept: 'a',
};
const encodeService = (service) => {
let encoded = JSON.stringify(service);
Object.values(ServiceReplacements).forEach((v, idx) => {
encoded = encoded.replace(Object.keys(ServiceReplacements)[idx], v);
});
return bytesToBase64url(stringToUtf8Bytes(encoded));
};
/**
* {@link @veramo/did-manager#DIDManager} identifier provider for `did:key` identifiers
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
export class PeerDIDProvider extends AbstractIdentifierProvider {
defaultKms;
constructor(options) {
super();
this.defaultKms = options.defaultKms;
}
async createIdentifier({ kms, options }, context) {
options = options ?? { num_algo: 0 };
if (options.service) {
options.num_algo = 2;
}
const authPrivateKeyHex = options?.key?.privateKeyHex || options?.authPrivateKeyHex;
const agreementPrivateKeyHex = options?.agreementKey?.privateKeyHex || options?.agreementPrivateKeyHex;
let key;
let agreementKey;
// Exit early so we don't create a key if we can't continue
if (![0, 2].includes(options.num_algo)) {
throw new Error(`not_supported: PeerDIDProvider num algo ${options.num_algo} not supported yet.`);
}
if (options.agreementKeyRef) {
agreementKey = await context.agent.keyManagerGet({ kid: options.agreementKeyRef });
if (agreementKey.type !== 'X25519') {
throw new Error('not_supported: Key type must be X25519');
}
}
if (options.keyRef) {
key = await context.agent.keyManagerGet({ kid: options.keyRef });
if (key.type !== 'Ed25519') {
throw new Error('not_supported: Key type must be Ed25519');
}
}
else {
key = await importOrCreateKey({
kms: kms || this.defaultKms,
options: {
...(options?.key ?? {}),
type: 'Ed25519',
privateKeyHex: authPrivateKeyHex,
}
}, context);
}
switch (options.num_algo) {
case 0: {
const methodSpecificId = bytesToMultibase(hexToBytes(key.publicKeyHex), 'base58btc', 'ed25519-pub');
const identifier = {
did: 'did:peer:0' + methodSpecificId,
controllerKeyId: key.kid,
keys: [key],
services: [],
};
debug('Created', identifier.did);
return identifier;
}
case 2: {
if (!agreementKey) {
agreementKey = await importOrCreateKey({
kms: kms || this.defaultKms,
options: {
...(options?.agreementKey ?? {}),
type: 'X25519',
privateKeyHex: agreementPrivateKeyHex,
}
}, context);
}
const authKeyText = bytesToMultibase(hexToBytes(key.publicKeyHex), 'base58btc', 'ed25519-pub');
const agreementKeyText = bytesToMultibase(hexToBytes(agreementKey.publicKeyHex), 'base58btc', 'x25519-pub');
let serviceString = '';
if (options.service) {
serviceString = `.S${encodeService(options.service)}`;
}
const identifier = {
did: `did:peer:2.E${agreementKeyText}.V${authKeyText}${serviceString}`,
controllerKeyId: key.kid,
keys: [key, agreementKey],
services: options.service ? [options.service] : [],
};
debug('Created', identifier.did);
return identifier;
}
default:
throw new Error(`'not_supported: PeerDIDProvider num algo ${options.num_algo} not supported yet.'`);
}
}
async updateIdentifier(args, context) {
throw new Error('not_supported: PeerDIDProvider updateIdentifier not supported yet.');
}
async deleteIdentifier(identifier, context) {
for (const { kid } of identifier.keys) {
await context.agent.keyManagerDelete({ kid });
}
return true;
}
async addKey({ identifier, key, options }, context) {
throw Error('not_supported: PeerDIDProvider addKey not supported');
}
async addService({ identifier, service, options }, context) {
throw Error('not_supported: PeerDIDProvider addService not supported');
}
async removeKey(args, context) {
throw Error('not_supported: PeerDIDProvider removeKey not supported');
}
async removeService(args, context) {
throw Error('not_supported: PeerDIDProvider removeService not supported');
}
}
//# sourceMappingURL=peer-did-provider.js.map