@digitalbazaar/did-method-key
Advanced tools
Comparing version 5.0.0 to 5.1.0
@@ -6,4 +6,4 @@ /*! | ||
import { | ||
getDid, getKey, getKeyAgreementKeyPair, getMultibaseMultikeyHeader, | ||
setVerificationKeyPairId | ||
addKeyAgreementKeyContext, getDid, getKey, getKeyAgreementKeyPair, getKeyPair, | ||
getMultibaseMultikeyHeader, setKeyPairId | ||
} from './helpers.js'; | ||
@@ -149,4 +149,8 @@ | ||
} | ||
const keyPair = await fromMultibase({publicKeyMultibase}); | ||
const {didDocument} = await this._keyPairToDidDocument({keyPair}); | ||
const {keyAgreementKeyPair, keyPair} = await getKeyPair({ | ||
fromMultibase, publicKeyMultibase | ||
}); | ||
const {didDocument} = await this._keyPairToDidDocument({ | ||
keyPair, keyAgreementKeyPair | ||
}); | ||
@@ -175,4 +179,7 @@ if(keyIdFragment) { | ||
async publicKeyToDidDoc({publicKeyDescription} = {}) { | ||
const { | ||
keyPair, keyAgreementKeyPair | ||
} = await getKeyPair({publicKeyDescription}); | ||
const {didDocument} = await this._keyPairToDidDocument({ | ||
keyPair: publicKeyDescription | ||
keyPair, keyAgreementKeyPair | ||
}); | ||
@@ -196,2 +203,20 @@ return {didDocument}; | ||
async _keyPairToDidDocument({keyPair, keyAgreementKeyPair} = {}) { | ||
const keyPairs = new Map(); | ||
let didDocument; | ||
let publicDhKey; | ||
const contexts = [DID_CONTEXT_URL]; | ||
if(!keyPair && keyAgreementKeyPair) { | ||
addKeyAgreementKeyContext({contexts, keyAgreementKeyPair}); | ||
const did = getDid({keyPair: keyAgreementKeyPair}); | ||
keyAgreementKeyPair.controller = did; | ||
setKeyPairId({keyPair: keyAgreementKeyPair, did}); | ||
publicDhKey = await keyAgreementKeyPair.export({publicKey: true}); | ||
keyPairs.set(keyAgreementKeyPair.id, keyAgreementKeyPair); | ||
didDocument = { | ||
'@context': contexts, | ||
id: did, | ||
keyAgreement: [publicDhKey] | ||
}; | ||
return {didDocument, keyPairs}; | ||
} | ||
let {publicKeyMultibase} = keyPair; | ||
@@ -213,6 +238,6 @@ if(!publicKeyMultibase && keyPair.publicKeyBase58) { | ||
const did = getDid({verificationKeyPair}); | ||
const did = getDid({keyPair: verificationKeyPair}); | ||
verificationKeyPair.controller = did; | ||
// Now set the source key's id | ||
setVerificationKeyPairId({verificationKeyPair, did}); | ||
setKeyPairId({keyPair: verificationKeyPair, did}); | ||
// get the public components of verification keypair | ||
@@ -223,3 +248,3 @@ const verificationPublicKey = await verificationKeyPair.export({ | ||
}); | ||
const contexts = [DID_CONTEXT_URL, verificationPublicKey['@context']]; | ||
contexts.push(verificationPublicKey['@context']); | ||
// delete context from verificationPublicKey | ||
@@ -234,5 +259,12 @@ delete verificationPublicKey['@context']; | ||
let publicDhKey; | ||
// get the public components of keyAgreement keypair | ||
if(keyAgreementKeyPair) { | ||
addKeyAgreementKeyContext({contexts, keyAgreementKeyPair}); | ||
const did = getDid({keyPair: keyAgreementKeyPair}); | ||
if(!keyAgreementKeyPair.controller) { | ||
keyAgreementKeyPair.controller = did; | ||
} | ||
if(!keyAgreementKeyPair.id) { | ||
setKeyPairId({keyPair: keyAgreementKeyPair, did}); | ||
} | ||
publicDhKey = await keyAgreementKeyPair.export({publicKey: true}); | ||
@@ -242,3 +274,3 @@ } | ||
// Compose the DID Document | ||
const didDocument = { | ||
didDocument = { | ||
// Note that did:key does not have its own method-specific context, | ||
@@ -258,3 +290,2 @@ // and only uses the general DID Core context, and key-specific contexts. | ||
// create the key pairs map | ||
const keyPairs = new Map(); | ||
keyPairs.set(verificationKeyPair.id, verificationKeyPair); | ||
@@ -261,0 +292,0 @@ if(keyAgreementKeyPair) { |
@@ -40,4 +40,3 @@ /*! | ||
let publicKey; | ||
if(didDocument.verificationMethod[0].id === keyId) { | ||
if(didDocument.verificationMethod?.[0].id === keyId) { | ||
// Return the public key node for the main public key | ||
@@ -56,12 +55,10 @@ publicKey = didDocument.verificationMethod[0]; | ||
export function getDid({verificationKeyPair}) { | ||
return verificationKeyPair.fingerprint ? | ||
`did:key:${verificationKeyPair.fingerprint()}` : | ||
`did:key:${verificationKeyPair.publicKeyMultibase}`; | ||
export function getDid({keyPair}) { | ||
return keyPair.fingerprint ? `did:key:${keyPair.fingerprint()}` : | ||
`did:key:${keyPair.publicKeyMultibase}`; | ||
} | ||
export function setVerificationKeyPairId({verificationKeyPair, did}) { | ||
verificationKeyPair.id = verificationKeyPair.fingerprint ? | ||
`${did}#${verificationKeyPair.fingerprint()}` : | ||
`${did}#${verificationKeyPair.publicKeyMultibase}`; | ||
export function setKeyPairId({keyPair, did}) { | ||
keyPair.id = keyPair.fingerprint ? `${did}#${keyPair.fingerprint()}` : | ||
`${did}#${keyPair.publicKeyMultibase}`; | ||
} | ||
@@ -97,3 +94,3 @@ | ||
} | ||
return {keyAgreementKeyPair, contexts}; | ||
return {keyAgreementKeyPair}; | ||
} | ||
@@ -107,1 +104,41 @@ | ||
} | ||
export function addKeyAgreementKeyContext({contexts, keyAgreementKeyPair}) { | ||
const {type} = keyAgreementKeyPair; | ||
switch(type) { | ||
case 'X25519KeyAgreementKey2019': { | ||
if(!contexts.includes(X25519KeyAgreementKey2019.SUITE_CONTEXT)) { | ||
contexts.push(X25519KeyAgreementKey2019.SUITE_CONTEXT); | ||
} | ||
break; | ||
} | ||
case 'X25519KeyAgreementKey2020': { | ||
if(!contexts.includes(X25519KeyAgreementKey2020.SUITE_CONTEXT)) { | ||
contexts.push(X25519KeyAgreementKey2020.SUITE_CONTEXT); | ||
} | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Unsupported key agreement key type, "${type}".`); | ||
} | ||
} | ||
} | ||
export async function getKeyPair({ | ||
fromMultibase, publicKeyMultibase, publicKeyDescription | ||
} = {}) { | ||
let keyPair; | ||
if(fromMultibase && publicKeyMultibase) { | ||
keyPair = await fromMultibase({publicKeyMultibase}); | ||
} else { | ||
keyPair = publicKeyDescription; | ||
} | ||
const {type} = keyPair; | ||
let keyAgreementKeyPair; | ||
if(type === 'X25519KeyAgreementKey2020' || | ||
type === 'X25519KeyAgreementKey2019') { | ||
keyAgreementKeyPair = keyPair; | ||
keyPair = null; | ||
} | ||
return {keyPair, keyAgreementKeyPair}; | ||
} |
{ | ||
"name": "@digitalbazaar/did-method-key", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "A did:key method resolver.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/digitalbazaar/did-method-key", |
@@ -170,2 +170,4 @@ # did:key method driver _(@digitalbazaar/did-method-key)_ | ||
}); | ||
// or perhaps: | ||
// const verificationKeyPair = await Ed25519VerificationKey2020.generate(); | ||
@@ -273,12 +275,14 @@ const {didDocument, keyPairs, methodFor} = await didKeyDriver.fromKeyPair({ | ||
// This lets you use `publicMethodFor()` to get a key for a specific purpose | ||
const keyAgreementData = didKeyDriver.publicMethodFor({ | ||
const keyAgreementMethod = didKeyDriver.publicMethodFor({ | ||
didDocument, purpose: 'keyAgreement' | ||
}); | ||
const assertionMethodData = didKeyDriver.publicMethodFor({ | ||
const assertionMethod = didKeyDriver.publicMethodFor({ | ||
didDocument, purpose: 'assertionMethod' | ||
}); | ||
// If you're using a `crypto-ld` driver harness, you can create key instances | ||
// which allow you to get access to a `verify()` function. | ||
const assertionMethodPublicKey = await cryptoLd.from(assertionMethodData); | ||
// If you have a known key type, for example, `Ed25519VerificationKey2020`, | ||
// you can create key instances which allow you to get access to a | ||
// `verify()` function. | ||
const assertionMethodPublicKey = await Ed25519VerificationKey2020.from( | ||
assertionMethod); | ||
const {verify} = assertionMethodPublicKey.verifier(); | ||
@@ -285,0 +289,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32127
445
308