Saturday, February 27, 2021

DocID, CID, and VarInt Explorations

On February 20th I read:
https://proto.school/anatomy-of-a-cid
The code:

========

bshambaugh02/22/2021
II looked at the documentation a little before and I have been having trouble getting a match between part of the did:key in example 2 for CIP-79  and the public key.  I have a gist.
hittps://gist.github.com/bshambaugh/f18d26259ecdfccf199fa984c3637d50#gistcomment-3641554
I do suspect that it follows the did.key method, also mentioned in the gist.
Maybe I don't understand this fully.
"did-key-format := did:key:MULTIBASE(base58-btc, MULTICODEC(public-key-type, raw-public-key-bytes))"
II'll give it a go again tomorrow.
mMaybe I need to convert everything after e7 to a uint8array and then back to base58 or something.

{removed} 02/23/2021
Can you explain what you are trying to do exactly?
You might find the DocID spec interesting: https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-59/CIP-59.md
As well as the did:key spec: https://w3c-ccg.github.io/did-method-key/

bshambaugh02/23/2021
Presently I'm trying to figure out how to get a P-256 key from a did:key .

Code:
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')

const key = 'zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY'
const multibaseDecoded = multibase.decode(key)
console.info(toString(Uint8Array.from(multibaseDecoded), 'base16'))

Output:
1200017f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

Comment:
Perhaps the form is
<multicodec 'p256-pub'><multicodec 'cidv1'><publicKey(64)>

1200 01 7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

key source: https://w3c-ccg.github.io/did-method-key/#p-256
Well actually I'm trying to get a did:key from a P-256 public key in order to form a DocID, but one step at a time.
https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-79/CIP-79.md. I'm going backwards first to see how the did:key is made.
I'll admit that I thought the did key spec would have a key that decoded to having just one prefix 0x1200, rather than 0x1200 and 0x01 but I guess it is how it is. If you put the other key zXwpBzzzpUofYSorhHTeGwKWCKTBvUkNEkgHtCivNyQTixsyGmHiQfkjGtum3SxJxsetka9rp3VB2gkKF5zchhwHNEfC in place in the code the same prefix string, '1200 01' is there.
It was not clear to me from the spec, but you've got to pop off the first 3 bytes to get a 64 byte uint8, which should be correct for P-256.

to avoid clutter, I think there is a problem with the library js-multicodec. At this point I believe the library should give just 1200 as the prefix, not 120001. Everything up to 0x7c in the multicodec table just concatenates the hex to the front of the public key hex. Everything after adds a 01 or the prefix is something else entirely. This somehow sneaks past the tests.
https://gist.github.com/bshambaugh/3c3e3d2591a5b0f14726ba13df0c384c

Edit: the prefix is the varint rather than the code. If the prefix was the code, then something along what I described would happen. I think I was incorrect. Will double check. The library may be right, but I still don't know why the public key from the did key looks the way it does. If it is varint it should be:
8024 7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

=========

On February 23rd in my exploration I explored CIDs through js-cid:
https://github.com/multiformats/js-cid

~/Documents/exploringCIDs/js-cid/brents_experiments$ cat testTwo.js
function toHexString(byteArray) {
  return Array.prototype.map.call(byteArray, function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('');
}
function toByteArray(hexString) {
  var result = [];
  for (var i = 0; i < hexString.length; i += 2) {
    result.push(parseInt(hexString.substr(i, 2), 16));
  }
  return result;
}

const CID = require('../src/')

const mhStr = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
const cid = new CID(mhStr)

console.log(cid.codec)
console.log(cid.code)
console.log(cid.version)
console.log(cid.multihash)
console.log(toHexString(cid.multihash))
console.log(cid.multibaseName)
console.log(cid)

const hash = '1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'

const cidTwo = new CID(0, 'dag-pb', cid.multihash)

console.log(cidTwo.toString())
// https://bitcoin.stackexchange.com/questions/52727/byte-array-to-hexadecimal-and-back-again-in-javascript

The result:
dag-pb
112
0
Uint8Array(34) [
   18,  32, 185,  77,  39, 185, 147,  77,
   62,   8, 165,  46,  82, 215, 218, 125,
  171, 250, 196, 132, 239, 227, 122,  83,
  128, 238, 144, 136, 247, 172, 226, 239,
  205, 233
]
1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
base58btc
CID(QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4)
QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4


On February 23rd in my exploration I decoded a ceramic doc DocID available from the gateway at https://gateway-clay.ceramic.network/api/v0/documents/kjzl6cwe1jw149tlplc4bgnpn1v4uwk9rg9jkvijx0u0zmfa97t69dnqibqa2as. The code was:

~/Documents/exploringCIDs/js-multibase/brents_experiments$ cat testtwo.js
const { decodeText, encodeText } = require('../src/util')
const multibase = require('../src')
const constants = require('../src/constants.js')

const bytes = 'kjzl6cwe1jw149tlplc4bgnpn1v4uwk9rg9jkvijx0u0zmfa97t69dnqibqa2as'
const decodedBytes = multibase.decode(bytes)
console.log(decodedBytes.toString())

console.log(toHexString(decodedBytes))

// Source ::: https://bitcoin.stackexchange.com/questions/52727/byte-array-to-hexadecimal-and-back-again-in-javascript

function toHexString(byteArray) {
  return Array.prototype.map.call(byteArray, function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('');
}
function toByteArray(hexString) {
  var result = [];
  for (var i = 0; i < hexString.length; i += 2) {
    result.push(parseInt(hexString.substr(i, 2), 16));
  }
  return result;
}

The result was:
206,1,0,1,133,1,18,32,182,119,151,186,131,111,67,32,150,167,228,131,184,82,184,8,194,19,187,54,100,63,60,169,194,251,128,148,54,3,48,36
ce01000185011220b67797ba836f432096a7e483b852b808c213bb36643f3ca9c2fb809436033024

are different than the parts of a a DocID github.com/ceramicnetwork , but there are some commonalities like github.com/multiformats/m (js-multibase) and github.com/multiformats/m
(js-multicodec) to form w3c-ccg.github.io/did-method-key/ for example.

did:key is used in CIP-79 (ceramic network to construct a DocID) I believe:
" did-key-format := did:key:MULTIBASE(base58-btc, MULTICODEC(public-key-type, raw-public-key-bytes)) " w3c-ccg.github.io/did-method-key/ multibase.encode('base58btc',multicodec.addPrefix('p256-pub', {raw public key bytes as uint8Array})) https://github.com/multiformats/js-multibase

multicodec.rmPrefix(multicodec.addPrefix('p256-pub', {raw public key bytes as uint8Array})) == {raw public key bytes as uint8Array} ??
https://github.com/multiformats/js-multicodec


multicodec.rmPrefix(multibase.decode(multibase.encode('base58btc',multicodec.addPrefix('p256-pub', {raw public key bytes as uint8Array})))) == {raw public key bytes as uint8Array} ???

Messing with https://github.com/multiformats/js-multibase

Code Used:
~/Documents/exploringCIDs/js-multibase+multicodecTest$ cat index2.js
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')

const key = 'zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY'

console.log('the key before decoding: '+key)
// to decode we need the prefix
const multibaseDecoded = multibase.decode(key)
console.log('the decoded data')
console.log(multibaseDecoded)
console.log('the decoded data converted to utf8:  '+toString(multibaseDecoded, encoding = 'utf8'))

const multibaseRecode = multibase.encode('base58btc',multibaseDecoded)

console.log('the decoded data reencoded')
console.log(multibaseRecode)
console.log('the decoded data rencoded and converted to utf8:  '+toString(multibaseRecode, encoding = 'utf8'))

console.log('the decoded data reencoded decoded')
console.log(multibase.decode(multibaseRecode))

Results:
~/Documents/exploringCIDs/js-multibase+multicodecTest$ node index2.js
the key before decoding: zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY
the decoded data
Uint8Array(67) [
   18,   0,   1, 127,  35,  88,  48, 221,  61, 239, 167,
   34, 239,  26, 162,  73, 214, 160, 221, 187, 164, 249,
  144, 176, 129, 117,  56, 147,  63,  87,  54,  64, 101,
   53,  66, 133, 109, 168, 141,  51,  95,  31, 178,  91,
  139, 207, 190,   8, 149,  40, 220, 224, 155,  31, 124,
  185, 159, 221,  96, 248, 131,   0, 244, 194, 204, 109,
   53
]
the decoded data converted to utf8:  #X0�=�"��I֠ݻ�����u8�?W6@e5B�m��3_�[��(���|���`�����m5
the decoded data reencoded
Uint8Array(92) [
  122,  88, 119, 112,  66, 110,  77, 100,  67, 109,  49,  99,
   76, 109,  75, 117, 122, 103,  69,  83, 110,  50,  57, 110,
  113, 110, 111, 110, 112,  49, 105, 111, 113, 114,  81,  77,
   82,  72,  78, 115, 109, 106,  77, 121, 112, 112, 122, 120,
   56, 120,  66,  50, 112, 118,  55,  99, 119,  56, 113,  49,
   80, 100,  68,  97,  99,  83, 114, 100,  87,  69,  51, 100,
  116,  66,  57, 102,  55,  78, 120, 107,  56,  56,  54, 109,
  100, 122,  78,  70, 111,  80, 116,  89
]
the decoded data rencoded and converted to utf8:  zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY
the decoded data reencoded decoded
Uint8Array(67) [
   18,   0,   1, 127,  35,  88,  48, 221,  61, 239, 167,
   34, 239,  26, 162,  73, 214, 160, 221, 187, 164, 249,
  144, 176, 129, 117,  56, 147,  63,  87,  54,  64, 101,
   53,  66, 133, 109, 168, 141,  51,  95,  31, 178,  91,
  139, 207, 190,   8, 149,  40, 220, 224, 155,  31, 124,
  185, 159, 221,  96, 248, 131,   0, 244, 194, 204, 109,
   53
]

So the public key for did:key:zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY must be in hex: 17f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35 ?
Document that I tried to produce:


Code:
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')

const key = 'zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY'
//const key = 'zXwpBzzzpUofYSorhHTeGwKWCKTBvUkNEkgHtCivNyQTixsyGmHiQfkjGtum3SxJxsetka9rp3VB2gkKF5zchhwHNEfC'

console.log('the key before decoding: '+key)
// to decode we need the prefix
const multibaseDecoded = multibase.decode(key)
console.log('the decoded data')
console.log(multibaseDecoded)
console.log('the length is : '+multibaseDecoded.length)
console.log('the decoded data converted to utf8:  '+toString(multibaseDecoded, encoding = 'utf8'))
console.log('the decoded data converted to hex '+toHexString(multibaseDecoded))
const woPrefixDecodedHex = '7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35'
const woPrefixDecodedByteArray = toByteArray(woPrefixDecodedHex)
console.log(woPrefixDecodedByteArray)
console.log('the length is: '+woPrefixDecodedByteArray.length)

const multibaseRecode = multibase.encode('base58btc',multibaseDecoded)

console.log('the decoded data reencoded')
console.log(multibaseRecode)
console.log('the decoded data rencoded and converted to utf8:  '+toString(multibaseRecode, encoding = 'utf8'))

console.log('the decoded data reencoded decoded')
console.log(multibase.decode(multibaseRecode))

// Source ::: https://bitcoin.stackexchange.com/questions/52727/byte-array-to-hexadecimal-and-back-again-in-javascript

function toHexString(byteArray) {
return Array.prototype.map.call(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
function toByteArray(hexString) {
var result = [];
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
}
return result;
}

Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest$ node index3.js
the key before decoding: zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY
the decoded data
Uint8Array(67) [
   18,   0,   1, 127,  35,  88,  48, 221,  61, 239, 167,
   34, 239,  26, 162,  73, 214, 160, 221, 187, 164, 249,
  144, 176, 129, 117,  56, 147,  63,  87,  54,  64, 101,
   53,  66, 133, 109, 168, 141,  51,  95,  31, 178,  91,
  139, 207, 190,   8, 149,  40, 220, 224, 155,  31, 124,
  185, 159, 221,  96, 248, 131,   0, 244, 194, 204, 109,
   53
]
the length is : 67
the decoded data converted to utf8:  #X0�=�"��I֠ݻ�����u8�?W6@e5B�m��3_�[��(���|���`�����m5
the decoded data converted to hex 1200017f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35
[
  127,  35,  88,  48, 221,  61, 239, 167,  34, 239,  26,
  162,  73, 214, 160, 221, 187, 164, 249, 144, 176, 129,
  117,  56, 147,  63,  87,  54,  64, 101,  53,  66, 133,
  109, 168, 141,  51,  95,  31, 178,  91, 139, 207, 190,
    8, 149,  40, 220, 224, 155,  31, 124, 185, 159, 221,
   96, 248, 131,   0, 244, 194, 204, 109,  53
]
the length is: 64
the decoded data reencoded
Uint8Array(92) [
  122,  88, 119, 112,  66, 110,  77, 100,  67, 109,  49,  99,
   76, 109,  75, 117, 122, 103,  69,  83, 110,  50,  57, 110,
  113, 110, 111, 110, 112,  49, 105, 111, 113, 114,  81,  77,
   82,  72,  78, 115, 109, 106,  77, 121, 112, 112, 122, 120,
   56, 120,  66,  50, 112, 118,  55,  99, 119,  56, 113,  49,
   80, 100,  68,  97,  99,  83, 114, 100,  87,  69,  51, 100,
  116,  66,  57, 102,  55,  78, 120, 107,  56,  56,  54, 109,
  100, 122,  78,  70, 111,  80, 116,  89
]
the decoded data rencoded and converted to utf8:  zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY
the decoded data reencoded decoded
Uint8Array(67) [
   18,   0,   1, 127,  35,  88,  48, 221,  61, 239, 167,
   34, 239,  26, 162,  73, 214, 160, 221, 187, 164, 249,
  144, 176, 129, 117,  56, 147,  63,  87,  54,  64, 101,
   53,  66, 133, 109, 168, 141,  51,  95,  31, 178,  91,
  139, 207, 190,   8, 149,  40, 220, 224, 155,  31, 124,
  185, 159, 221,  96, 248, 131,   0, 244, 194, 204, 109,
   53
]

reference: https://github.com/multiformats/multicodec , https://github.com/multiformats/multicodec/blob/master/table.csv

actually, I think the hex should be broken down to: <multicodec><version><publicKey(64)> == 1200017f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35 to 1200 + 01 + <publicKey(64)>

bshambaugh02/23/2021
Presently I'm trying to figure out how to get a P-256 key from a did:key .

Code:
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')

const key = 'zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY'
const multibaseDecoded = multibase.decode(key)
console.info(toString(Uint8Array.from(multibaseDecoded), 'base16'))

Output:
1200017f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

Comment:
Perhaps the form is
<multicodec 'p256-pub'><multicodec 'cidv1'><publicKey(64)>

1200 01 7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

key source: https://w3c-ccg.github.io/did-method-key/#p-256
Well actually I'm trying to get a did:key from a P-256 public key in order to form a DocID, but one step at a time.
https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-79/CIP-79.md. I'm going backwards first to see how the did:key is made.
I'll admit that I thought the did key spec would have a key that decoded to having just one prefix 0x1200, rather than 0x1200 and 0x01 but I guess it is how it is. If you put the other key zXwpBzzzpUofYSorhHTeGwKWCKTBvUkNEkgHtCivNyQTixsyGmHiQfkjGtum3SxJxsetka9rp3VB2gkKF5zchhwHNEfC in place in the code the same prefix string, '1200 01' is there.
It was not clear to me from the spec, but you've got to pop off the first 3 bytes to get a 64 byte uint8, which should be correct for P-256.

to avoid clutter, I think there is a problem with the library js-multicodec. At this point I believe the library should give just 1200 as the prefix, not 120001. Everything up to 0x7c in the multicodec table just concatenates the hex to the front of the public key hex. Everything after adds a 01 or the prefix is something else entirely. This somehow sneaks past the tests.
https://gist.github.com/bshambaugh/3c3e3d2591a5b0f14726ba13df0c384c

Edit: the prefix is the varint rather than the code. If the prefix was the code, then something along what I described would happen. I think I was incorrect. Will double check. The library may be right, but I still don't know why the public key from the did key looks the way it does. If it is varint it should be:
8024 7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

actually, I think the hex should be broken down to: <multicodec><version><publicKey(64)> == 1200017f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35 to 1200 + 01 + <publicKey(64)>

So now that I have the public key, how about reversing things and try to make a did:key. First I will add the multicodec.

Code:
~/Documents/exploringCIDs/js-multibase+multicodecTest$ cat index3_rev.js
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

const publicKey = '7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35'
console.log('the public key is'+publicKey)
const publicKeyByte = toByteArray(publicKey)

const publicKeyByte2 = fromString(publicKey, 'base16')

console.log(publicKeyByte)
console.log(publicKeyByte2)

if (publicKeyByte === publicKeyByte2) {
   console.log('true')
}

console.log(toString((fromString('1200', 'base16')),'base16'))

const publickKeyBytewPrefix = multicodec.addPrefix(fromString('1200', 'base16'), publicKeyByte)
// const publickKeyBytewPrefix = multicodec.addPrefix('p256-pub', publicKeyByte)
console.log(publickKeyBytewPrefix)

console.log(toHexString(publickKeyBytewPrefix))
console.info(toString(Uint8Array.from(publickKeyBytewPrefix), 'base16'))

const base58encodedString = multibase.encode('base58btc',publickKeyBytewPrefix)

console.log(base58encodedString)
console.info(toString(Uint8Array.from(base58encodedString), 'base16'))

function toHexString(byteArray) {
return Array.prototype.map.call(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
function toByteArray(hexString) {
var result = [];
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
}
return result;
}

Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest$ node index3_rev.js
the public key is7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35
[
  127,  35,  88,  48, 221,  61, 239, 167,  34, 239,  26,
  162,  73, 214, 160, 221, 187, 164, 249, 144, 176, 129,
  117,  56, 147,  63,  87,  54,  64, 101,  53,  66, 133,
  109, 168, 141,  51,  95,  31, 178,  91, 139, 207, 190,
    8, 149,  40, 220, 224, 155,  31, 124, 185, 159, 221,
   96, 248, 131,   0, 244, 194, 204, 109,  53
]
Uint8Array(64) [
  127,  35,  88,  48, 221,  61, 239, 167,  34, 239,  26,
  162,  73, 214, 160, 221, 187, 164, 249, 144, 176, 129,
  117,  56, 147,  63,  87,  54,  64, 101,  53,  66, 133,
  109, 168, 141,  51,  95,  31, 178,  91, 139, 207, 190,
    8, 149,  40, 220, 224, 155,  31, 124, 185, 159, 221,
   96, 248, 131,   0, 244, 194, 204, 109,  53
]
1200
Uint8Array(66) [
  128,  36, 127,  35,  88,  48, 221,  61, 239, 167,  34,
  239,  26, 162,  73, 214, 160, 221, 187, 164, 249, 144,
  176, 129, 117,  56, 147,  63,  87,  54,  64, 101,  53,
   66, 133, 109, 168, 141,  51,  95,  31, 178,  91, 139,
  207, 190,   8, 149,  40, 220, 224, 155,  31, 124, 185,
  159, 221,  96, 248, 131,   0, 244, 194, 204, 109,  53
]
80247f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35
80247f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35
Uint8Array(91) [
  122, 114, 117, 114, 119,  99,  74,  90, 115, 115,  52, 114,
  117, 101, 112,  86,  78, 117,  49,  72,  51, 120, 109,  83,
  105, 114, 118,  78,  98, 122, 103,  66, 107,  57, 113, 114,
   67, 107, 116,  66,  54, 107,  97, 101, 119,  88, 110,  74,
   65, 104,  89,  87, 119, 116,  80,  51,  98, 120,  65,  67,
  113,  66, 112, 122, 106,  90, 100,  78,  55,  84, 121,  72,
   78, 122, 122,  71,  71,  83,  83,  72,  53, 113, 118,  90,
  115,  83,  68, 105, 114,  57, 122
]
7a72757277634a5a73733472756570564e75314833786d536972764e627a67426b397172436b7442366b616577586e4a4168595777745033627841437142707a6a5a644e375479484e7a7a47475353483571765a7353446972397a

Here is the original getting the public key from the did:key

Code:
~/Documents/exploringCIDs/js-multibase+multicodecTest$ cat index3.js
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')

const key = 'zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY'
//const key = 'zXwpBzzzpUofYSorhHTeGwKWCKTBvUkNEkgHtCivNyQTixsyGmHiQfkjGtum3SxJxsetka9rp3VB2gkKF5zchhwHNEfC'

console.log('the key before decoding: '+key)
// to decode we need the prefix
const multibaseDecoded = multibase.decode(key)
console.log('the decoded data')
console.log(multibaseDecoded)
console.log('the length is : '+multibaseDecoded.length)
console.log('the decoded data converted to utf8:  '+toString(multibaseDecoded, encoding = 'utf8'))
console.log('the decoded data converted to hex '+toHexString(multibaseDecoded))
const woPrefixDecodedHex = '7f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35'
const woPrefixDecodedByteArray = toByteArray(woPrefixDecodedHex)
console.log(woPrefixDecodedByteArray)
console.log('the length is: '+woPrefixDecodedByteArray.length)

const multibaseRecode = multibase.encode('base58btc',multibaseDecoded)

console.log('the decoded data reencoded')
console.log(multibaseRecode)
console.log('the decoded data rencoded and converted to utf8:  '+toString(multibaseRecode, encoding = 'utf8'))

console.log('the decoded data reencoded decoded')
console.log(multibase.decode(multibaseRecode))

// Source ::: https://bitcoin.stackexchange.com/questions/52727/byte-array-to-hexadecimal-and-back-again-in-javascript

function toHexString(byteArray) {
return Array.prototype.map.call(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
function toByteArray(hexString) {
var result = [];
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
}
return result;
}

Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest$ node index3.js
the key before decoding: zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY
the decoded data
Uint8Array(67) [
   18,   0,   1, 127,  35,  88,  48, 221,  61, 239, 167,
   34, 239,  26, 162,  73, 214, 160, 221, 187, 164, 249,
  144, 176, 129, 117,  56, 147,  63,  87,  54,  64, 101,
   53,  66, 133, 109, 168, 141,  51,  95,  31, 178,  91,
  139, 207, 190,   8, 149,  40, 220, 224, 155,  31, 124,
  185, 159, 221,  96, 248, 131,   0, 244, 194, 204, 109,
   53
]
the length is : 67
the decoded data converted to utf8:  #X0�=�"��I֠ݻ�����u8�?W6@e5B�m��3_�[��(���|���`�����m5
the decoded data converted to hex 1200017f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35
[
  127,  35,  88,  48, 221,  61, 239, 167,  34, 239,  26,
  162,  73, 214, 160, 221, 187, 164, 249, 144, 176, 129,
  117,  56, 147,  63,  87,  54,  64, 101,  53,  66, 133,
  109, 168, 141,  51,  95,  31, 178,  91, 139, 207, 190,
    8, 149,  40, 220, 224, 155,  31, 124, 185, 159, 221,
   96, 248, 131,   0, 244, 194, 204, 109,  53
]
the length is: 64
the decoded data reencoded
Uint8Array(92) [
  122,  88, 119, 112,  66, 110,  77, 100,  67, 109,  49,  99,
   76, 109,  75, 117, 122, 103,  69,  83, 110,  50,  57, 110,
  113, 110, 111, 110, 112,  49, 105, 111, 113, 114,  81,  77,
   82,  72,  78, 115, 109, 106,  77, 121, 112, 112, 122, 120,
   56, 120,  66,  50, 112, 118,  55,  99, 119,  56, 113,  49,
   80, 100,  68,  97,  99,  83, 114, 100,  87,  69,  51, 100,
  116,  66,  57, 102,  55,  78, 120, 107,  56,  56,  54, 109,
  100, 122,  78,  70, 111,  80, 116,  89
]
the decoded data rencoded and converted to utf8:  zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY
the decoded data reencoded decoded
Uint8Array(67) [
   18,   0,   1, 127,  35,  88,  48, 221,  61, 239, 167,
   34, 239,  26, 162,  73, 214, 160, 221, 187, 164, 249,
  144, 176, 129, 117,  56, 147,  63,  87,  54,  64, 101,
   53,  66, 133, 109, 168, 141,  51,  95,  31, 178,  91,
  139, 207, 190,   8, 149,  40, 220, 224, 155,  31, 124,
  185, 159, 221,  96, 248, 131,   0, 244, 194, 204, 109,
   53
]

It seems like the work of a sophomore, but I got some part of it working. Combined with yesterday's stuff, maybe I'm done with the next objective.

Code:
const { decodeText, encodeText } = require('../src/util')
const multibase = require('../src')
const constants = require('../src/constants.js')
const toString = require('uint8arrays/to-string')

const someHex = '01022c04648fac78e1cd4a35c8097d40846db37db294f0a48b9be2ddf90e1566fa20'
console.log(someHex)

const bytesThree = toByteArray(someHex)

const buf = multibase.encode('base58btc', bytesThree)
const bufAsString = toString(Uint8Array.from(buf))
console.log(bufAsString)
console.log('2KhCYSqYpZ7RGPMQ4PZqwipW4vLLz86nZC4t2P3Q6iptAb')

const decoded = multibase.decode(bufAsString)
console.log(decoded)

// the bottom two console outputs should be equivalent
const decodedAsHex = toHexString(decoded)

console.log(decodedAsHex)

console.info(toString(decoded, 'base16'))

// console.log(toHexString2)

//console.log(buf)
//console.log(toHexString(buf))

/**
 const buf = encode(input)
        const encodedBuf = encode(base.encode(buf))
        const multibasedBuf = multibase(base.code, encodedBuf)
        expect(decodeText(multibasedBuf)).to.equal(output)
*/

// Source ::: https://bitcoin.stackexchange.com/questions/52727/byte-array-to-hexadecimal-and-back-again-in-javascript

function toHexString(byteArray) {
  return Array.prototype.map.call(byteArray, function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('');
}
function toByteArray(hexString) {
  var result = [];
  for (var i = 0; i < hexString.length; i += 2) {
    result.push(parseInt(hexString.substr(i, 2), 16));
  }
  return result;
}

Result:
~/Documents/exploringCIDs/js-multibase/brents_experiments$ node test5.js
01022c04648fac78e1cd4a35c8097d40846db37db294f0a48b9be2ddf90e1566fa20
z2KhCYSqYpZ7RGPMQ4PZqwipW4vLLz86nZC4t2P3Q6iptAb
2KhCYSqYpZ7RGPMQ4PZqwipW4vLLz86nZC4t2P3Q6iptAb
Uint8Array(34) [
    1,   2,  44,   4, 100, 143, 172, 120,
  225, 205,  74,  53, 200,   9, 125,  64,
  132, 109, 179, 125, 178, 148, 240, 164,
  139, 155, 226, 221, 249,  14,  21, 102,
  250,  32
]
01022c04648fac78e1cd4a35c8097d40846db37db294f0a48b9be2ddf90e1566fa20
01022c04648fac78e1cd4a35c8097d40846db37db294f0a48b9be2ddf90e1566fa20

I am not sure why my public key prefix does not match 'zXwpB' in w3c-ccg.github.io/did-method-key . Something is Inconsistent.



Code:

const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'

// convert the hex to a Uint8tArray
const publicKey = fromString(publicKeyHex,'base16')

const publicKeyBytewPrefix = multicodec.addPrefix('p256-pub',publicKey)

// convert the byte array to hex and display here...
const publicKeyBytewPrefixAsHex = toString(publicKeyBytewPrefix,'base16')

console.log(publicKeyBytewPrefixAsHex)

// encode as base58
const buf = multibase.encode('base58btc',publicKeyBytewPrefix)

// convert the base58 encoded Uint8Array to text and display
const bufAsString = toString(buf)

console.log(bufAsString)

// reverse the damage
const bufRev = multicodec.rmPrefix(multibase.decode(buf))

// convert it to hex and display
const bufRevAsString = toString(bufRev,'base16')

console.log(bufRevAsString)

// encode the public key as base58btc
const publicKeyBase58Btc = multibase.encode('base58btc',publicKey)

console.log(toString(publicKeyBase58Btc))

const publicKeyBase58BtcNoPrefix = multicodec.rmPrefix(publicKeyBase58Btc)

console.log(toString(publicKeyBase58BtcNoPrefix))
console.log('z6LSqKWh3XQ7AfsJuE2KR23cozEut8D5CXQCT2xJHsdY6iJH')

console.log(toString(multibase.decode(publicKeyBase58Btc),'base16'))

Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index4_e.js
8024f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ
f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
z5zdNaYFHfgJ5tMm6BReioHKbpZBMxGW1znc1vnWd3P78R3rPcqwUpHx5wRqTJa94xyxJryZUAH5yyGRJZLCkUFNE
5zdNaYFHfgJ5tMm6BReioHKbpZBMxGW1znc1vnWd3P78R3rPcqwUpHx5wRqTJa94xyxJryZUAH5yyGRJZLCkUFNE
z6LSqKWh3XQ7AfsJuE2KR23cozEut8D5CXQCT2xJHsdY6iJH
f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Oh look, I got a did:key from a public key. I've been trying to make this work for awhile.


Code:
const { base58_to_binary } = require('base58-js')
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

console.log("I started with the public key Ddfn4DtWhqX79zSFmiS6QBZwFrSH9indi3zAgCgRMYNq")
const bin = base58_to_binary('Ddfn4DtWhqX79zSFmiS6QBZwFrSH9indi3zAgCgRMYNq')
console.log(bin)
const publicKeyBytewPrefix = multicodec.addPrefix('ed25519-pub',bin)
const buf = multibase.encode('base58btc',publicKeyBytewPrefix)

const bufAsString = toString(buf)

console.log('did:key:'+bufAsString+' derived from Ddfn4DtWhqX79zSFmiS6QBZwFrSH9indi3zAgCgRMYNq')
console.log('did:key:z6Mks5vpeU8x3P1aGVGxTHPwFH7w5Ri8Zc2zQ4u6WUeSGmAD')

Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index8.js
I started with the public key Ddfn4DtWhqX79zSFmiS6QBZwFrSH9indi3zAgCgRMYNq
Uint8Array(32) [
  187, 176, 255,  81,  55,  14, 10, 211,
  212, 145, 123, 111, 160, 228, 59,  76,
  165, 126,  95, 222,  49,  91,  6, 115,
  214, 230,  65, 238, 168, 245, 77, 142
]
did:key:z6Mks5vpeU8x3P1aGVGxTHPwFH7w5Ri8Zc2zQ4u6WUeSGmAD derived from Ddfn4DtWhqX79zSFmiS6QBZwFrSH9indi3zAgCgRMYNq
did:key:z6Mks5vpeU8x3P1aGVGxTHPwFH7w5Ri8Zc2zQ4u6WUeSGmAD

I got the did:key from:
https://github.com/digitalbazaar/did-method-key-js

Feb 25
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

https://github.com/digitalbazaar/did-method-key-js
progress

Code:
const didKeyDriver = require('did-method-key').driver();

//console.log(didKeyDriver)

async function f1() {
  const didDocument = await didKeyDriver.generate(); // Ed25519 key type by default
  console.log(JSON.stringify(didDocument, null, 2)); // see DID Document above
  console.log(didDocument.keys)
}

f1()

Result:
~/Documents/exploringCIDs/did-method-key-js-experiments$ node index.js
{
  "@context": [
    "https://w3id.org/did/v0.11"
  ],
  "id": "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX",
  "publicKey": [
    {
      "id": "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX",
      "type": "Ed25519VerificationKey2018",
      "controller": "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX",
      "publicKeyBase58": "8vhgarHrPAUMa5Ef4z82VEyG7eEZxfDE9hkXh9hEhdu9"
    }
  ],
  "authentication": [
    "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX"
  ],
  "assertionMethod": [
    "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX"
  ],
  "capabilityDelegation": [
    "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX"
  ],
  "capabilityInvocation": [
    "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX"
  ],
  "keyAgreement": [
    {
      "id": "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6LSrQruY3KFXK5BQx8Qz2Q5c2MM5nV4PNF4ThWMy1ZxiL4q",
      "type": "X25519KeyAgreementKey2019",
      "controller": "did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX",
      "publicKeyBase58": "Fjgk1jWPRrMSKZkeTNt8HS8sEdwwgm4uaingUYvRzxJ5"
    }
  ]
}
{
  'did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX': Ed25519KeyPair {
    passphrase: null,
    id: undefined,
    controller: 'did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX',
    owner: undefined,
    type: 'Ed25519VerificationKey2018',
    privateKeyBase58: '4LbVy1H62B5tE5YxxC9jxD52D7UeUujH8jGeKzhW2WpbKwSRBBy12ZMj3PXboQkYrtrBxLy3FCEitUYSH9r1wphu',
    publicKeyBase58: '8vhgarHrPAUMa5Ef4z82VEyG7eEZxfDE9hkXh9hEhdu9'
  },
  'did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6LSrQruY3KFXK5BQx8Qz2Q5c2MM5nV4PNF4ThWMy1ZxiL4q': X25519KeyPair {
    passphrase: null,
    id: 'did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX#z6LSrQruY3KFXK5BQx8Qz2Q5c2MM5nV4PNF4ThWMy1ZxiL4q',
    controller: 'did:key:z6MknNxjB6YHihxpga5MkZ5sLLXFwDWRNYTaqifTXRfFcrgX',
    owner: undefined,
    type: 'X25519KeyAgreementKey2019',
    publicKeyBase58: 'Fjgk1jWPRrMSKZkeTNt8HS8sEdwwgm4uaingUYvRzxJ5',
    privateKeyBase58: '81k2Pch2g1PQD6TGtpMCccFnERYgpxheXb46cTkTQZyJ'
  }
}

Inconsistencies in multicodec (did:key
https://github.com/multiformats/multicodec/blob/master/table.csv &&
https://github.com/multiformats/js-multicodec/blob/master/src/index.js#L29 &&
vs. ceramic (https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-79/CIP-79.md#example-2 ,
https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-59/CIP-59.md,https://github.com/ceramicnetwork/key-did-provider-secp256k1/blob/master/src/index.ts#L21) vs. digitalbazaar et. al spec
https://w3c-ccg.github.io/did-method-key/#p-256 )
https://gist.github.com/bshambaugh/f18d26259ecdfccf199fa984c3637d50)

Multicodec uses varint

 I used the method for varints from the google developers documentation [1]. I got that 0x1200 as a multicodec [2] as a code equals 0x8024 as a varint. (for end)

[1] https://developers.google.com/protocol-buffers/docs/encoding#varints
[2] https://github.com/multiformats/multicodec/blob/master/table.csv

Instead of prefixing things with 0x1200, the prefix is 0x8024.  Source [2] shows how to get this. Here is some of my scratch work to get from 0x1200 to 0x8024 .


Check example with: https://www.calculator.net/binary-calculator.html?b2dnumber1=100101100&calctype=b2d&x=51&y=22#binary2decimal

The ceramic protocol and the digital  bazaar et. al spec should use 0x1200 0x01 in front instead of 0x8024 (check this)

I believe that all three are the same up to code:
https://github.com/multiformats/multicodec/blob/master/table.csv#L47 i.e. 0x7c
I noticed a change between this, and the next value 0x81.

Converting 0x7c to decimal is 124
https://www.rapidtables.com/convert/number/hex-to-binary.html?x=0x7c
I noticed a change between this, and the next value 0x81.
Note: https://www.rapidtables.com/convert/number/hex-to-binary.html?x=0x7f is 128 in decimal.

0x7f = 1111111     (127)
0x80 =10000000   (128)

I was inspired and encouraged along to show the conversion to decimal by dlongley's comment:
https://github.com/w3c-ccg/did-method-key/issues/29#issuecomment-786039356

Links::
=================
https://developers.google.com/protocol-buffers/docs/encoding#varints
(what, why)
https://www.calculator.net/binary-calculator.html?b2dnumber1=100101100&calctype=b2d&x=51&y=22#binary2decimal
https://www.rapidtables.com/convert/number/hex-to-binary.html?x=0x1200
https://github.com/digitalbazaar/did-method-key-js
https://learnmeabitcoin.com/technical/varint  (what, why)
https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-79/CIP-79.md#example-2
https://incoherency.co.uk/base58/
https://w3c-ccg.github.io/did-method-key/#p-256
===================

Some experiments. 1) Experiment with addPrefix with js-multicodec. Transition from 0x7f to 0x80 .

Code:
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'

console.log('before adding prefix: '+publicKeyHex)

// convert the hex to a Uint8tArray
const publicKey = fromString(publicKeyHex,'base16')

const publicKeywPrefix = multicodec.addPrefix('torrent-file',publicKey)

// convert it to hex and display
const bufRevAsString = toString(publicKeywPrefix,'base16')

console.log('after adding prefix: '+bufRevAsString)

Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: 7cf9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Result when replacing 'torrent-file' with 'git-raw' (code 0x78)
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix:
78f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Result when replacing 'torrent-file' with 'leofcoin-block' (code 0x81)
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: 8101f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Result when replacing 'torrent-file' with 'secp256k1-pub' (code 0xe7)
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: e701f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Result when replacing 'torrent-file' with 'ed25519-pub' (code 0xed)
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: ed01f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Result when replacing 'torrent-file' with 'ed25519-pub' (code 0x1200)
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: 8024f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

codes from https://github.com/multiformats/multicodec/blob/master/table.csv

Question 2: See how the #0356 code for did-method-key works to convert  keys to Hex:
"

4.5 P-256

This section is non-normative.

These DID always start with zXwpB.

did:key:zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY
did:key:zXwpBzzzpUofYSorhHTeGwKWCKTBvUkNEkgHtCivNyQTixsyGmHiQfkjGtum3SxJxsetka9rp3VB2gkKF5zchhwHNEfC
"

code for conversion of the 1st key:

const publicKey = 'zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY'

// encode as base58
const buf = multibase.decode(publicKey)

const buftoHex = toString(buf,'base16')

console.log(buftoHex)

output:

~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index11.js
1200017f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

output for the 2nd key:
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index11.js
1200018a0ac59a2d3086e8a12a78fd4773a6d52a0ca61ef6c1419e15a05bcc6dafce7b79fb17e5bd74c7cca3cab8f89f2de919f2dc63b5dbcb52b382a39daa7b2b2483

4.3 Secp256k1  (https://w3c-ccg.github.io/did-method-key/#secp256k1)

This section is non-normative.

These DID always start with zQ3s.

did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme did:key:zQ3shtxV1FrJfhqE1dvxYRcCknWNjHc3c5X1y3ZSoPDi2aur2 did:key:zQ3shZc2QzApp2oymGvQbzP8eKheVshBHbU4ZYjeXqwSKEn6N


result for conversion of the 1st key:
e70103874c15c7fda20e539c6e5ba573c139884c351188799f5458b4b41f7924f235cd


result for conversion of the 2nd key:
e70103d4b8cf1953bd38ea9f79a51c4fd4be325ff9c103b26db490d121a03201bd5343


result for conversion of the 3rd key:
e70102b52d132694fdf9452926f8cc654c80d02d2823d97b556f1dd800e9b5844955d3



4.1 Ed25519 / X25519

This section is non-normative.

These DID always start with z6Mk.

did:key:z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp did:key:z6MkjchhfUsD6mmvni8mCdXHw216Xrm9bQe2mBH1P5RDjVJG did:key:z6MknGc3ocHs3zdPiJbnaaqDi58NGb4pk1Sp9WxWufuXSdxf

result for conversion of the 1st key:
ed013b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29

result for conversion of the 2nd key:
ed014cb5abf6ad79fbf5abbccafcc269d85cd2651ed4b885b5869f241aedf0a5ba29


result for conversion of the 3rd key:
ed017422b9887598068e32c4448a949adb290d0f4e35b9e01b0ee5f1a1e600fe2674

Question 3: Convert Keys in the ceramic doc to hex (secp256k1):

did:key:zQ3shQNcackrTByiYaPGso1Nt7b6r1gSMg4XXBmavzvTMqX1h

result for conversion of the 1st key:
e701022c04648fac78e1cd4a35c8097d40846db37db294f0a48b9be2ddf90e1566fa20


In js-multicodec:

code:
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'

console.log('before adding prefix: '+publicKeyHex)

// convert the hex to a Uint8tArray
const publicKey = fromString(publicKeyHex,'base16')

const publicKeywPrefix = multicodec.addPrefix('ed25519-pub',publicKey)

// convert it to hex and display
const bufRevAsString = toString(publicKeywPrefix,'base16')

console.log('after adding prefix: '+bufRevAsString)


Result:
:~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: ed01f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
code:
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'

console.log('before adding prefix: '+publicKeyHex)

// convert the hex to a Uint8tArray
const publicKey = fromString(publicKeyHex,'base16')

const publicKeywPrefix = multicodec.addPrefix('secp256k1-pub',publicKey)

// convert it to hex and display
const bufRevAsString = toString(publicKeywPrefix,'base16')

console.log('after adding prefix: '+bufRevAsString)


Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: e701f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
code:
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

const publicKeyHex = 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'

console.log('before adding prefix: '+publicKeyHex)

// convert the hex to a Uint8tArray
const publicKey = fromString(publicKeyHex,'base16')

const publicKeywPrefix = multicodec.addPrefix('p256-pub',publicKey)

// convert it to hex and display
const bufRevAsString = toString(publicKeywPrefix,'base16')

console.log('after adding prefix: '+bufRevAsString)

Result:
~/Documents/exploringCIDs/js-multibase+multicodecTest2$ node index9.js
before adding prefix: f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f
after adding prefix: 8024f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f

Question 4: What do Troy's public keys look like for the did-method-key spec issue:
https://github.com/w3c-ccg/did-method-key/issues/29#issuecomment-785521562

did:key:zrurwcJZss4ruepVNu1H3xmSirvNbzgBk9qrCktB6kaewXnJAhYWwtP3bxACqBpzjZdN7TyHNzzGGSSH5qvZsSDir9z 
did:key:zrusAFgBbf84b8mBz8Cmy8UoFWKV52EaeRnK86vnLo4Z5QoRypE6hXVPN2urevZMAMtcTaCDFLWBaE1Q3jmdb1FHgve

Code:
const multicodec = require('multicodec')
const multibase = require('multibase')
const toString = require('uint8arrays/to-string')
const fromString = require('uint8arrays/from-string')

const publicKey = 'zrusAFgBbf84b8mBz8Cmy8UoFWKV52EaeRnK86vnLo4Z5QoRypE6hXVPN2urevZMAMtcTaCDFLWBaE1Q3jmdb1FHgve'
//const publicKey = 'zXwpBzzzpUofYSorhHTeGwKWCKTBvUkNEkgHtCivNyQTixsyGmHiQfkjGtum3SxJxsetka9rp3VB2gkKF5zchhwHNEfC'
// const publicKey = 'zXwpBnMdCm1cLmKuzgESn29nqnonp1ioqrQMRHNsmjMyppzx8xB2pv7cw8q1PdDacSrdWE3dtB9f7Nxk886mdzNFoPtY'

// encode as base58
const buf = multibase.decode(publicKey)

const buftoHex = toString(buf,'base16')

console.log(buftoHex)


Result 1:
80247f235830dd3defa722ef1aa249d6a0ddbba4f990b0817538933f573640653542856da88d335f1fb25b8bcfbe089528dce09b1f7cb99fdd60f88300f4c2cc6d35

Result 2:
80248a0ac59a2d3086e8a12a78fd4773a6d52a0ca61ef6c1419e15a05bcc6dafce7b79fb17e5bd74c7cca3cab8f89f2de919f2dc63b5dbcb52b382a39daa7b2b2483


No comments:

Post a Comment