Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
creating a did:key document
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')
const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'
const didKey = publicHexKeyToDIDKey(publicKeyHex);
console.log(didKey);
publicKeyToXY(publicKeyHex);
function publicHexKeyToDIDKey(publicKeyHex) {
const publicKey = fromString(publicKeyHex,'base16');
const publicKeywPrefix = multicodec.addPrefix('p256-pub',publicKey)
const buf = multibase.encode('base58btc',publicKeywPrefix)
const bufAsString = toString(buf)
return `did:key:${bufAsString}`
}
function publicKeyToXY(publicKeyHex) {
console.log(publicKeyHex.length);
const xHex = publicKeyHex.slice(0,publicKeyHex.length/2);
const yHex = publicKeyHex.slice(publicKeyHex.length/2,publicKeyHex.length);
console.log(publicKeyHex);
console.log(xHex);
console.log(yHex);
xOctet = fromString(xHex,'base16');
yOctet = fromString(yHex,'base16');
x = toString(multibase.encode('base64url',xOctet));
y = toString(multibase.encode('base64url',yOctet));
console.log(x);
console.log(y);
}
{
"@context": "https://w3id.org/did/v1",
"id": "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ",
"publicKey": [{
"id": "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ#zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ",
"type": "JsonWebKey2020",
"controller": "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ",
"publicKeyJwk": {
"kty": "EC",
"crv": "P-256",
"x": "u-cNviWRiM3i9wGjUvOB-0XyPpIb5rAwmE8o8jDBte7Y",
"y": "uHNNnF7isXk_qitI9yNB4PCMY7krXqA224AJq0LByok8"
}
}],
"authentication": [ "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ#zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ" ],
"assertionMethod": [ "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ#zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ" ],
"capabilityInvocation": [ "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ#zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ" ],
"keyAgreement": [{
"id": "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ#zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ", //
"type": "JsonWebKey2020",
"controller": "did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ",
"publicKeyJwk": {
"kty": "EC",
"crv": "P-256",
"x": "u-cNviWRiM3i9wGjUvOB-0XyPpIb5rAwmE8o8jDBte7Y",
"y": "uHNNnF7isXk_qitI9yNB4PCMY7krXqA224AJq0LByok8"
}
}]
}
@bshambaugh

This comment has been minimized.

Copy link
Owner Author

@bshambaugh bshambaugh commented Mar 12, 2021

In the did:key document, output from function publicHexKeyToDIDKey is used.

in the did:key document coordinates "x" and "y" come from console.log(x) and console.log(y) in function publicKeyToXY.

@bshambaugh

This comment has been minimized.

Copy link
Owner Author

@bshambaugh bshambaugh commented Apr 10, 2021

const publicKey = 'zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ';
const buf = multibase.decode(publicKey)
const bufnoPrefix = multicodec.rmPrefix(buf)
const bbf = toString(bufnoPrefix,'base16')
console.log(bbf)

@bshambaugh

This comment has been minimized.

Copy link
Owner Author

@bshambaugh bshambaugh commented Apr 10, 2021

console.log(bbf) yields:

f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment