Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
creating a did:key and document, also see multibase vs base64url_universal (npm) vs base64url (npm)
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')
const base64url = require('base64url'); // this converts a text string to base64url, it does not take a uint8 array??
const base64url_universal = require('base64url-universal');
const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'
const didKey = publicHexKeyToDIDKey(publicKeyHex);
console.log(didKey);
const KeyBase58 = publickeyToBase58btc(publicKeyHex);
console.log(KeyBase58);
publicKeyToXY(publicKeyHex);
console.log(base64url.encode("hello"));
console.log(base64url_universal.encode("hello"));
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 publickeyToBase58btc(publicKeyHex) {
const publicKey = fromString(publicKeyHex,'base16');
const buf = multibase.encode('base58btc',publicKey)
const buftoString = toString(buf);
return buftoString;
}
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');
// console.log('the xOctet is:'+toString(xOctet)+'\n');
// console.log('the yOctet is:'+toString(yOctet)+'\n');
xm = toString(multibase.encode('base64url',xOctet));
ym = toString(multibase.encode('base64url',yOctet));
xu = base64url_universal.encode(xOctet);
yu = base64url_universal.encode(yOctet);
x = base64url.encode(xOctet);
y = base64url.encode(yOctet);
console.log('base64url using multibase: '+xm);
console.log('base64url using b64urluni: '+xu);
console.log('base64url using b64url : '+x);
console.log('base64url using multibase: '+ym);
console.log('base64url using b64urluni: '+yu);
console.log('base64url using b64url : '+y);
}
@bshambaugh

This comment has been minimized.

Copy link
Owner Author

@bshambaugh bshambaugh commented Mar 12, 2021

output:

did🔑zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ
z5zdNaYFHfgJ5tMm6BReioHKbpZBMxGW1znc1vnWd3P78R3rPcqwUpHx5wRqTJa94xyxJryZUAH5yyGRJZLCkUFNE
128
f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb6
1cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
base64url using multibase: u-cNviWRiM3i9wGjUvOB-0XyPpIb5rAwmE8o8jDBte7Y
base64url using b64urluni: -cNviWRiM3i9wGjUvOB-0XyPpIb5rAwmE8o8jDBte7Y
base64url using multibase: uHNNnF7isXk_qitI9yNB4PCMY7krXqA224AJq0LByok8
base64url using b64urluni: HNNnF7isXk_qitI9yNB4PCMY7krXqA224AJq0LByok8

@bshambaugh

This comment has been minimized.

Copy link
Owner Author

@bshambaugh bshambaugh commented Mar 19, 2021

did🔑zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ

const doc = await ceramic.createDocument({
metadata: {
controllers: ['did🔑zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ'],
// secp256r1 did:key
family: '3id'
},
content: {
publicKeys: {
" ": " ", // secp256r1 public key
// The value of this property should be an object where the value is a multicodec + multibase(base58btc) encoded public key, the key for any // given key should be the last 15 characters of the encoded public key.
}
},
deterministic: true
})

const didString = did:3:${doc.id}

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