did-method-key
Advanced tools
Comparing version 0.4.0 to 0.5.0
# did:key driver ChangeLog | ||
## 0.5.0 - 2020-02-24 | ||
### Added | ||
- `driver.get()` can now also resolve individual keys. | ||
### Changed | ||
- **BREAKING**: Undo previous change, using `https://w3id.org/did/v0.11` as | ||
`@context`, apologies for the confusion. | ||
## 0.4.0 - 2020-01-29 | ||
@@ -4,0 +13,0 @@ |
@@ -8,25 +8,77 @@ /*! | ||
const X25519KeyPair = require('x25519-key-pair'); | ||
const {constants: securityConstants} = require('security-context'); | ||
/** | ||
* Returns a `did:key` method DID Document for a given DID. | ||
* Returns a `did:key` method DID Document for a given DID, or a key document | ||
* for a given DID URL (key id). | ||
* Note: This is async only to match the async `get()` signature of other | ||
* `did-io` drivers. | ||
* | ||
* @param {string} did | ||
* @returns {Promise<DidDocument>} | ||
* Either a `did` or `url` param is required: | ||
* @param {string} [did] - DID URL or a key id (either an ed25519 key or an | ||
* x25519 key-agreement key id). | ||
* @param {string} [url] - alias for the `did` url param, supported for better | ||
* readability of invoking code. | ||
* | ||
* Usage: | ||
* | ||
* ``` | ||
* await driver.get({did}); // -> did document | ||
* await driver.get({url: keyId}); // -> public key node | ||
* ``` | ||
* | ||
* @throws Unsupported Fingerprint Type. | ||
* @throws Cannot get - missing DID. | ||
* @returns {Promise<DidDocument|object>} Resolves to a DID Document or a | ||
* public key node with context. | ||
*/ | ||
async function get({did} = {}) { | ||
async function get({did, url} = {}) { | ||
did = did || url; | ||
if(!did) { | ||
throw new Error('Cannot get - missing DID.'); | ||
throw new TypeError('"did" must be a string.'); | ||
} | ||
const fingerprint = did.substr('did:key:'.length); | ||
const [didAuthority, keyIdFragment] = did.split('#'); | ||
const fingerprint = didAuthority.substr('did:key:'.length); | ||
const publicKey = LDKeyPair.fromFingerprint({fingerprint}); | ||
const didDoc = keyToDidDoc(publicKey); | ||
return keyToDidDoc(publicKey); | ||
if(keyIdFragment) { | ||
// Resolve an individual key | ||
return getKey({didDoc, keyIdFragment}); | ||
} | ||
// Resolve the full DID Document | ||
return didDoc; | ||
} | ||
/** | ||
* Returns the public key object for a given key id fragment. | ||
* | ||
* @param {DidDocument} didDoc | ||
* @param {string} keyIdFragment | ||
* | ||
* @returns {object} public key node, with `@context` | ||
*/ | ||
function getKey({didDoc, keyIdFragment}) { | ||
// Determine if the key id fragment belongs to the "main" public key, | ||
// or the keyAgreement key | ||
const keyId = didDoc.id + '#' + keyIdFragment; | ||
const publicKey = didDoc.publicKey[0]; | ||
if(publicKey.id === keyId) { | ||
// Return the public key node for the main public key | ||
return { | ||
'@context': securityConstants.SECURITY_CONTEXT_V2_URL, | ||
...publicKey | ||
} | ||
} | ||
// Return the public key node for the X25519 key-agreement key | ||
return { | ||
'@context': securityConstants.SECURITY_CONTEXT_V2_URL, | ||
...didDoc.keyAgreement[0] | ||
} | ||
} | ||
/** | ||
* Generates a new `did:key` method DID Document for a given key type. | ||
@@ -58,3 +110,3 @@ * | ||
return { | ||
'@context': 'https://www.w3.org/ns/did/v1', | ||
'@context': ['https://w3id.org/did/v0.11'], | ||
id: did, | ||
@@ -61,0 +113,0 @@ publicKey: [{ |
{ | ||
"name": "did-method-key", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A did:key method driver for did-io and standalone use.", | ||
@@ -23,2 +23,3 @@ "homepage": "http://github.com/digitalbazaar/did-method-key", | ||
"crypto-ld": "^3.6.0", | ||
"security-context": "^4.0.0", | ||
"x25519-key-pair": "digitalbazaar/x25519-key-pair#initial" | ||
@@ -25,0 +26,0 @@ }, |
@@ -50,3 +50,3 @@ # did:key method driver _(did-method-key)_ | ||
{ | ||
"@context": "https://www.w3.org/ns/did/v1", | ||
"@context": ["https://w3id.org/did/v0.11"], | ||
"id": "did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH", | ||
@@ -53,0 +53,0 @@ "publicKey": [ |
@@ -14,3 +14,3 @@ /*! | ||
describe('get', () => { | ||
it('should get a did:key DID', async () => { | ||
it('should get the DID Document for a did:key DID', async () => { | ||
const did = 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH'; | ||
@@ -21,3 +21,3 @@ const keyId = 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH#z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH'; | ||
expect(didDoc.id).to.equal(did); | ||
expect(didDoc['@context']).to.equal('https://www.w3.org/ns/did/v1'); | ||
expect(didDoc['@context']).to.eql(['https://w3id.org/did/v0.11']); | ||
expect(didDoc.authentication).to.eql([keyId]); | ||
@@ -42,3 +42,31 @@ expect(didDoc.assertionMethod).to.eql([keyId]); | ||
.equal('JhNWeSVLMYccCk7iopQW4guaSJTojqpMEELgSLhKwRr'); | ||
}) | ||
}); | ||
it('should resolve an individual key within the DID Doc', async () => { | ||
const did = 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH'; | ||
const keyId = did + '#z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH'; | ||
const key = await didKeyDriver.get({did: keyId}); | ||
expect(key).to.eql({ | ||
'@context': 'https://w3id.org/security/v2', | ||
id: 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH#z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH', | ||
type: 'Ed25519VerificationKey2018', | ||
controller: 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH', | ||
publicKeyBase58: 'B12NYF8RrR3h41TDCTJojY59usg3mbtbjnFs7Eud1Y6u' | ||
}); | ||
}); | ||
it('should resolve an individual key agreement key', async () => { | ||
const did = 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH'; | ||
const kakKeyId = did + '#zBzoR5sqFgi6q3iFia8JPNfENCpi7RNSTKF7XNXX96SBY4'; | ||
const key = await didKeyDriver.get({did: kakKeyId}); | ||
expect(key).to.eql({ | ||
'@context': 'https://w3id.org/security/v2', | ||
id: 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH#zBzoR5sqFgi6q3iFia8JPNfENCpi7RNSTKF7XNXX96SBY4', | ||
type: 'X25519KeyAgreementKey2019', | ||
controller: 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH', | ||
publicKeyBase58: 'JhNWeSVLMYccCk7iopQW4guaSJTojqpMEELgSLhKwRr' | ||
}); | ||
}); | ||
}); | ||
@@ -45,0 +73,0 @@ |
Sorry, the diff of this file is not supported yet
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
31563
207
3
+ Addedsecurity-context@^4.0.0
+ Addedsecurity-context@4.0.0(transitive)