Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@digitalbazaar/did-method-key
Advanced tools
A DID (Decentralized Identifier) method driver for the
did-io
library and for standalone use
See also (related specs):
A did:key
method driver for the did-io
client library and for standalone use.
The did:key
method is used to express public keys in a way that doesn't
require a DID Registry of any kind. Its general format is:
did:key:<multibase encoded, multicodec identified, public key>
So, for example, the following DID would be derived from a base-58 encoded ed25519 public key:
did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH
That DID would correspond to the following DID Document:
{
"@context": [
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/suites/ed25519-2020/v1",
"https://w3id.org/security/suites/x25519-2020/v1"
],
"id": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv",
"verificationMethod": [
{
"id": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv",
"type": "Ed25519VerificationKey2020",
"controller": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv",
"publicKeyMultibase": "zFj5p9C2Sfqth6g6DEXtw5dWFqrtpFn4TCBBPJHGnwKzY"
}
],
"authentication": [
"did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv"
],
"assertionMethod": [
"did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv"
],
"capabilityDelegation": [
"did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv"
],
"capabilityInvocation": [
"did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv"
],
"keyAgreement": [
{
"id": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6LSeYpKdHsRV4rCh3r6yo7moGbeRm1rmBbptBcAP3HCu4jC",
"type": "X25519KeyAgreementKey2020",
"controller": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv",
"publicKeyMultibase": "z3seA6z4ZPc8TbfULT9bpUgPAacUk4aRg1CtUtadgBgxS"
}
]
}
The keyAgreement
key is a Curve25519 public key (suitable for
Diffie-Hellman key exchange) that is deterministically derived from the source
Ed25519 key, using ed2curve-js
.
Note that this derived key is optional -- there's currently no proof that this is safe to do.
Requires Node.js 12+
To install from npm
:
npm install --save @digitalbazaar/did-method-key
To install locally (for development):
git clone https://github.com/digitalbazaar/did-method-key-js.git
cd did-method-key-js
npm install
generate()
To generate a new key and get its corresponding did:key
method DID Document:
const didKeyDriver = require('@digitalbazaar/did-method-key').driver();
// generate did:key using Ed25519 key type by default
const {
didDocument, keyPairs, methodFor
} = await didKeyDriver.generate();
// print the DID Document above
console.log(JSON.stringify(didDocument, null, 2));
// keyPairs will be set like so ->
Map(2) {
'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv' => Ed25519VerificationKey2020 {
id: 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv',
controller: 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv',
type: 'Ed25519VerificationKey2020',
publicKeyMultibase: 'zFj5p9C2Sfqth6g6DEXtw5dWFqrtpFn4TCBBPJHGnwKzY',
privateKeyMultibase: 'z3zDo1wXuXGcFkJa9SPE7VYpdutmHq8gJsvFRMKJckTWMykoHsAjWNbHXqzrZ8qa7aWdDTjmJNJ1amYEG2mCvZZeY'
},
'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6LSeYpKdHsRV4rCh3r6yo7moGbeRm1rmBbptBcAP3HCu4jC' => X25519KeyAgreementKey2020 {
id: 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6LSeYpKdHsRV4rCh3r6yo7moGbeRm1rmBbptBcAP3HCu4jC',
controller: 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv',
type: 'X25519KeyAgreementKey2020',
publicKeyMultibase: 'z3seA6z4ZPc8TbfULT9bpUgPAacUk4aRg1CtUtadgBgxS',
privateKeyMultibase: 'z8YKTeHC5WzYNV7k8Nq7Mbv5cVrWDoyp7RqGMWaaYfvHM'
}
}
methodFor
is a convenience function that returns a public/private key pair
instance for a given purpose. For example, a verification key (containing a
signer()
and verifier()
functions) are frequently useful for
jsonld-signatures
or
vc-js
operations. After generating
a new did:key DID, you can do:
// For signing Verifiable Credentials
const assertionKeyPair = methodFor({purpose: 'assertionMethod'});
// For Authorization Capabilities (zCaps)
const invocationKeyPair = methodFor({purpose: 'capabilityInvocation'});
// For Encryption using `@digitalbazaar/minimal-cipher`
const keyAgreementPair = methodFor({purpose: 'keyAgreement'});
Note that methodFor
returns a key pair that contains both a public and private
key pair (since it has access to the keyPairs
map from generate()
).
This makes it useful for signing and encrypting operations (unlike the
publicMethodFor
that's returned by get()
, below).
publicKeyToDidDoc()
If you already have an Ed25519VerificationKey2020
public key object (as an
LDKeyPair instance, or a plain key description object), you can turn it into
a DID Document:
const {didDocument} = await didKeyDriver.publicKeyToDidDoc({publicKeyDescription});
get()
did:key
DIDTo get a DID Document for an existing did:key
DID:
const did = 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv';
const didDocument = await didKeyDriver.get({did});
(Results in the example DID Doc above).
You can also use a .get()
to retrieve an individual key, if you know it's id
already (this is useful for constructing documentLoader
s for JSON-LD Signature
libs, and the resulting key does include the appropriate @context
).
const verificationKeyId = 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv';
await didKeyDriver.get({url: keyAgreementKeyId});
// ->
{
"@context": "https://w3id.org/security/suites/ed25519-2020/v1",
"id": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv",
"type": "Ed25519VerificationKey2020",
"controller": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv",
"publicKeyMultibase": "zFj5p9C2Sfqth6g6DEXtw5dWFqrtpFn4TCBBPJHGnwKzY"
}
const keyAgreementKeyId = 'did:key:z6MkfJh6Ks3xDo3PMGUS1KWVNWPgpVjGT9BpjWNuAPeXwmop#z6LSmNXTNXTkUPL6UHaBCDJhtvNTTzCgcUQ6kM6S7zngPFPj';
await didKeyDriver.get({url: keyAgreementKeyId});
// ->
{
"@context": "https://w3id.org/security/suites/x25519-2020/v1",
"id": "did:key:z6MkfJh6Ks3xDo3PMGUS1KWVNWPgpVjGT9BpjWNuAPeXwmop#z6LSmNXTNXTkUPL6UHaBCDJhtvNTTzCgcUQ6kM6S7zngPFPj",
"type": "X25519KeyAgreementKey2020",
"controller": "did:key:z6MkfJh6Ks3xDo3PMGUS1KWVNWPgpVjGT9BpjWNuAPeXwmop",
"publicKeyMultibase": "zAhMHrDetNvcMNuCQfZnkaL9ycqfZusDwsNNkdY99fscy"
}
publicMethodFor()
Often, you have just a did:key
DID, and you need to get a key for a
particular purpose from it, such as an assertionMethod
key to verify a
VC signature, or a keyAgreement
key to encrypt a document for that DID's
controller.
For that purpose, you can use a combination of get()
and publicMethodFor
:
// Start with the DID
const didDocument = await didKeyDriver.get({did});
// This lets you use `publicMethodFor()` to get a key for a specific purpose
const keyAgreementData = didKeyDriver.publicMethodFor({
didDocument, purpose: 'keyAgreement'
});
const assertionMethodData = 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);
const {verify} = assertionMethodPublicKey.verifier();
publicMethodFor
will return undefined
if no key is found for a given
purpose.
See the contribute file!
PRs accepted.
If editing the Readme, please conform to the standard-readme specification.
Commercial support for this library is available upon request from Digital Bazaar: support@digitalbazaar.com
New BSD License (3-clause) © Digital Bazaar
1.1.0 - 2021-05-04
didKeyDriver.publicKeyToDidDoc({keyPair})
method. (This used to be
the keyToDidDoc()
method, in <= v0.7.0
, removed in v1.0 and brought back
by popular demand.)FAQs
A did:key method resolver.
The npm package @digitalbazaar/did-method-key receives a total of 5,209 weekly downloads. As such, @digitalbazaar/did-method-key popularity was classified as popular.
We found that @digitalbazaar/did-method-key demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.