@aviarytech/did-peer
Advanced tools
Comparing version 0.0.21 to 0.0.22
import type { IDIDDocumentServiceDescriptor, IDIDDocumentVerificationMethod } from "./interfaces.js"; | ||
export declare const create: (numalgo: number, authenticationKeys: IDIDDocumentVerificationMethod[], encryptionKeys?: IDIDDocumentVerificationMethod[], service?: IDIDDocumentServiceDescriptor) => Promise<string>; | ||
export declare const create: (numalgo: number, authenticationKeys: IDIDDocumentVerificationMethod[], encryptionKeys?: IDIDDocumentVerificationMethod[], service?: IDIDDocumentServiceDescriptor | IDIDDocumentServiceDescriptor[]) => Promise<string>; | ||
export declare const createNumAlgo0: (authenticationKey: IDIDDocumentVerificationMethod) => Promise<string>; | ||
export declare const createNumAlgo1: () => Promise<string>; | ||
export declare const createNumAlgo2: (authenticationKeys: IDIDDocumentVerificationMethod[], encryptionKeys?: IDIDDocumentVerificationMethod[], service?: IDIDDocumentServiceDescriptor) => Promise<string>; | ||
export declare const createNumAlgo2: (authenticationKeys: IDIDDocumentVerificationMethod[], encryptionKeys?: IDIDDocumentVerificationMethod[], service?: IDIDDocumentServiceDescriptor[]) => Promise<string>; |
@@ -5,2 +5,5 @@ import { Numalgo2Prefixes } from "./constants.js"; | ||
export const create = async (numalgo, authenticationKeys, encryptionKeys, service) => { | ||
if (service && !Array.isArray(service)) { | ||
service = [service]; | ||
} | ||
switch (numalgo) { | ||
@@ -29,4 +32,4 @@ case 0: | ||
const enc = encryptionKeys ? encryptionKeys.map(k => `.${Numalgo2Prefixes.KeyAgreement}${k.publicKeyMultibase}`) : ''; | ||
const serv = service ? encodeService(service) : ''; | ||
const serv = service ? service?.map(s => encodeService(s)).join("") : ''; | ||
return `did:peer:2${auth}${enc}${serv}`; | ||
}; |
{ | ||
"name": "@aviarytech/did-peer", | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "devDependencies": { |
@@ -34,3 +34,4 @@ import { Numalgo2Prefixes } from "./constants.js"; | ||
let keys = did.split('.'); | ||
let serviceIndex = 0; | ||
let serviceMetadata = { index: 0 }; | ||
let keyIndex = 1; | ||
delete keys[0]; | ||
@@ -41,5 +42,5 @@ keys.forEach(k => { | ||
authKeys.push({ | ||
id: `${did}#${k.slice(2)}`, | ||
id: `#key-${keyIndex++}`, | ||
controller: did, | ||
type: 'Ed25519VerificationKey2020', | ||
type: 'Multikey', | ||
publicKeyMultibase: k.slice(1) | ||
@@ -50,5 +51,5 @@ }); | ||
encKeys.push({ | ||
id: `${did}#${k.slice(2)}`, | ||
id: `#key-${keyIndex++}`, | ||
controller: did, | ||
type: 'X25519KeyAgreementKey2020', | ||
type: 'Multikey', | ||
publicKeyMultibase: k.slice(1) | ||
@@ -58,4 +59,3 @@ }); | ||
case Numalgo2Prefixes.Service: | ||
services.push(decodeService(did, k.slice(1), serviceIndex)); | ||
serviceIndex++; | ||
services.push(decodeService(did, k.slice(1), serviceMetadata)); | ||
break; | ||
@@ -62,0 +62,0 @@ } |
@@ -16,4 +16,4 @@ import type { IDIDDocumentServiceDescriptor, IDIDDocumentVerificationMethod } from "./interfaces.js"; | ||
export declare const encodeService: (service: IDIDDocumentServiceDescriptor) => string; | ||
export declare const decodeService: (did: string, service: string, index: number) => IDIDDocumentServiceDescriptor; | ||
export declare const decodeService: (did: string, service: string, metadata: Record<string, any>) => IDIDDocumentServiceDescriptor; | ||
export declare const isPeerDID: (did: string) => boolean; | ||
export declare const createDIDDocument: (did: string, authKeys: IDIDDocumentVerificationMethod[], encKeys: IDIDDocumentVerificationMethod[], services: IDIDDocumentServiceDescriptor[]) => any; |
32
utils.js
@@ -42,3 +42,3 @@ import { Buffer } from 'buffer/index.js'; | ||
}; | ||
export const decodeService = (did, service, index) => { | ||
export const decodeService = (did, service, metadata) => { | ||
const val = JSON.parse(utf8.decode(base64url.decode(service))); | ||
@@ -72,17 +72,29 @@ if (val.s) { | ||
val.type = 'DIDCommMessaging'; | ||
val.id = `#didcommmessaging-${index}`; | ||
} | ||
else { | ||
val.type = val.t; | ||
val.id = `#service-${index}`; | ||
} | ||
delete val['t']; | ||
} | ||
if (!val.id) { | ||
if (metadata.index === 0) { | ||
val.id = `#service`; | ||
} | ||
else { | ||
val.id = `#service-${metadata.index}`; | ||
} | ||
metadata.index++; | ||
} | ||
return val; | ||
}; | ||
export const isPeerDID = (did) => { | ||
return new RegExp('^did:peer:(([01](z)([1-9a-km-zA-HJ-NP-Z]*))|(2((\.[AEVID](z)([1-9a-km-zA-HJ-NP-Z]*))+(\.(S)[0-9a-zA-Z=]*)?)))$').test(did); | ||
return new RegExp('^did:peer:(([01](z)([1-9a-km-zA-HJ-NP-Z]*))|(2((\.[AEVID](z)([1-9a-km-zA-HJ-NP-Z]*))+(\.(S)[0-9a-zA-Z=]*)*)))$').test(did); | ||
}; | ||
export const createDIDDocument = (did, authKeys, encKeys, services) => { | ||
let contexts = ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1"]; | ||
let contexts = ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/multikey/v1", { "@base": did }]; | ||
const prefix = "did:peer:"; | ||
const didPeerNumalgo = parseInt(did.slice(prefix.length, prefix.length + 1)); | ||
if (didPeerNumalgo < 2) { | ||
contexts = ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1"]; | ||
} | ||
const auth = authKeys.map(k => k.id); | ||
@@ -100,9 +112,13 @@ const enc = encKeys.map(k => k.id); | ||
authentication: auth, | ||
capabilityDelegation: auth, | ||
capabilityInvocation: auth, | ||
verificationMethod: ver, | ||
}; | ||
if (didPeerNumalgo < 2) { | ||
doc["capabilityDelegation"] = auth; | ||
doc["capabilityInvocation"] = auth; | ||
} | ||
if (enc.length > 0) { | ||
doc['keyAgreement'] = enc; | ||
contexts.push("https://w3id.org/security/suites/x25519-2020/v1"); | ||
if (didPeerNumalgo < 2) { | ||
contexts.push("https://w3id.org/security/suites/x25519-2020/v1"); | ||
} | ||
} | ||
@@ -109,0 +125,0 @@ if (services.length > 0) { |
17233
393