npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

did-key-creator

v1.2.0

Published

This is a library for converting public keys to the did:key format

Downloads

271

Readme

did:key creator

This is a library for converting public keys to the did:key format

Reference

This has been tested to create did:keys from the P-256,P-384, and P-521 curves specified in https://github.com/w3c-ccg/did-method-key and https://w3c-ccg.github.io/did-method-key/ .

This code is available as the did-key-creator package on npm.

For other cryptographic curves, follow a similar pattern by feeding the correct multicodecName and corresponding public key, and check the result (or form) of creating a did:key against the test vectors in the specification. Try reverting the did:key to get the original public key. The encodeDIDfromBytes function in the library should be the most generic. Public keys that create did:keys should revert to the same public keys. The did_key_utils functions provided may or may not work with the desired key. Since did:keys are specified to involve compressed public keys, a decompression function will be needed to get the key back into a raw or uncompressed form if that allows for the best comparisons or use based on the output or desired inputs of the utilized cryptographic library, etc. At least the raw form (just the x,y bytes) and the uncompressed form with a '04' prefix concatenated to the x,y bytes was appropriate for the NIST (P-*) curves. If it looks different for different curves, this is to be determined. Of course when comparing the compressed to raw form for NIST, look at them serialized as a hex string with x,y points as big endian. Decompression functions for NIST expressed as a ECDecompress function can be found in the key-did-resolver npm package github src folder for secp256r1, secp384r1, and secp521r1 (https://github.com/ceramicnetwork/js-ceramic/). multicodecNames can be found here: https://github.com/multiformats/multicodec/blob/master/table.csv .

Standard Use:

Note:

One way to get import in the code to work is to add "type":"module" in package.json .

Compressed 33 Byte Key code example for P-256 key (using elliptic npm module):

import { rawKeyInHexfromUncompressed, compressedKeyInHexfromRaw, encodeDIDfromHexString, encodeDIDfromBytes } from 'did-key-creator'

import { fromString } from 'uint8arrays/from-string'
import elliptic from 'elliptic'

const ecurve = new elliptic.ec('p256')
const  key = ecurve.genKeyPair();
const  pubPoint = key.getPublic('hex');
const multicodecName = 'p256-pub';

const rawKey = rawKeyInHexfromUncompressed(pubPoint);
const compressedKey = compressedKeyInHexfromRaw(rawKey);

const publicKey2 = fromString(compressedKey,'base16');
console.log(encodeDIDfromHexString(multicodecName,compressedKey));
console.log(encodeDIDfromBytes(multicodecName,publicKey2));

output

did:key:zDnaeqYWNxcFqy5DcJm91BMTeWv5hjs1VL5medk9n8dDUC67T
did:key:zDnaeqYWNxcFqy5DcJm91BMTeWv5hjs1VL5medk9n8dDUC67T

Compressed 49 Byte Key code example for P-384 key (using elliptic npm module):

import { rawKeyInHexfromUncompressed, compressedKeyInHexfromRaw, encodeDIDfromHexString, encodeDIDfromBytes } from 'did-key-creator'
import elliptic from 'elliptic'

const ecurve = new elliptic.ec('p384')
const  key = ecurve.genKeyPair();
const  pubPoint = key.getPublic('hex');
const multicodecName = 'p384-pub';

const rawKey = rawKeyInHexfromUncompressed(pubPoint);
const compressedKey = compressedKeyInHexfromRaw(rawKey);

console.log(encodeDIDfromHexString(multicodecName,compressedKey));

output

did:key:z82Lkz6GT5oNPzQowVWaYysnFPT1NAMsXayELmNjme3FhRErkTkij9ywuYWukxcLfNdW6Cw

Compressed 67 Byte Key code example for P-521 key (using elliptic npm module):

import { rawKeyInHexfromUncompressed, compressedKeyInHexfromRaw, encodeDIDfromHexString, encodeDIDfromBytes } from 'did-key-creator'

import { fromString } from 'uint8arrays/from-string'
import elliptic from 'elliptic'

const ecurve = new elliptic.ec('p521')
const  key = ecurve.genKeyPair();
const  pubPoint = key.getPublic('hex');
const multicodecName = 'p521-pub';

const rawKey = rawKeyInHexfromUncompressed(pubPoint);
const compressedKey = compressedKeyInHexfromRaw(rawKey);

console.log(encodeDIDfromHexString(multicodecName,compressedKey));

output

did:key:z2J9gaYmUxgiF1VDutBWwC4KVdpKfjnRkyV3t4kysx49eHz1wkYh1KHBPqbNdVH5GTgY2KLXtJPYTwFDkhQxuTWxK3K5HSKu

Other Possible Uses (non-standard and will hurt interoperability):

Raw 64 Byte Public Key code Example for P-256 Key:

import { encodeDIDfromHexString, encodeDIDfromBytes } from 'did-key-creator'
import { fromString } from 'uint8arrays/from-string'

const multicodecName = 'p256-pub';
const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f';
const publicKey = fromString(publicKeyHex,'base16');

console.log(encodeDIDfromHexString(multicodecName,publicKeyHex));
console.log(encodeDIDfromBytes(multicodecName,publicKey));

output

did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ
did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ

Raw 64 Byte Public Key code Example for P-256 key (using elliptic npm module):

import { rawKeyInHexfromUncompressed, compressedKeyInHexfromRaw, encodeDIDfromHexString, encodeDIDfromBytes } from 'did-key-creator'
import { fromString } from 'uint8arrays/from-string'
import elliptic from 'elliptic'

const ecurve = new elliptic.ec('p256')
const  key = ecurve.genKeyPair();
const  pubPoint = key.getPublic('hex');
const multicodecName = 'p256-pub';

const rawKey = rawKeyInHexfromUncompressed(pubPoint);

const publicKey2 = fromString(rawKey,'base16');
console.log(encodeDIDfromHexString(multicodecName,rawKey));
console.log(encodeDIDfromBytes(multicodecName,publicKey2));

output

did:key:zruqvMb8L2VWcghzg2Bt9QYwHnDfwixx9gKKU6Sy4pizM24cB98kFKefB8S7jYNvzyUFT5aRF1q7zEuMwR2RdszUDDc
did:key:zruqvMb8L2VWcghzg2Bt9QYwHnDfwixx9gKKU6Sy4pizM24cB98kFKefB8S7jYNvzyUFT5aRF1q7zEuMwR2RdszUDDc

Uncompressed 65 Byte Key code example for P-256 key (using elliptic npm module):

import { rawKeyInHexfromUncompressed, compressedKeyInHexfromRaw, encodeDIDfromHexString, encodeDIDfromBytes } from 'did-key-creator'
import { fromString } from 'uint8arrays/from-string'
import elliptic from 'elliptic'

const ecurve = new elliptic.ec('p256')
const  key = ecurve.genKeyPair();
const  pubPoint = key.getPublic('hex');
const multicodecName = 'p256-pub'

const publicKey3 = fromString(pubPoint,'base16');
console.log(encodeDIDfromHexString(multicodecName,pubPoint));
console.log(encodeDIDfromBytes(multicodecName,publicKey3));

output

did:key:z4oJ8bWfFMefRfFuNtDtu69bQyvaCpYh62thpGA9TWNZs614qDUfCdZ2KuZMEAFKXGACSo3Ws9FLZwscDGtLwThKFev44
did:key:z4oJ8bWfFMefRfFuNtDtu69bQyvaCpYh62thpGA9TWNZs614qDUfCdZ2KuZMEAFKXGACSo3Ws9FLZwscDGtLwThKFev44

License

Apache-2.0 OR MIT