Created
June 2, 2021 05:02
-
-
Save bshambaugh/60d90a32fd5b3bacd8113c631ba98491 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as name from '../key-did-provider-secp256r1/dist/index.js' | |
import u8a from 'uint8arrays' | |
import multicodec from 'multicodec' | |
import multibase from 'multibase' | |
//import toString from 'uint8arrays/to-string' | |
//import fromString from 'uint8arrays/from-string' | |
import base64url from 'base64url' // this converts a text string to base64url, it does not take a uint8 array?? | |
import base64url_universal from 'base64url-universal' | |
import bs58 from 'bs58' | |
const publicKey = 'zrurwcJZss4ruepVNu1H3xmSirvNbzgBk9qrCktB6kaewXnJAhYWwtP3bxACqBpzjZdN7TyHNzzGGSSH5qvZsSDir9z' | |
//const publicKey = 'zrusAFgBbf84b8mBz8Cmy8UoFWKV52EaeRnK86vnLo4Z5QoRypE6hXVPN2urevZMAMtcTaCDFLWBaE1Q3jmdb1FHgve' | |
//const publicKey = 'zXwpDcwhs3ryJCCM3vYz9nJ7jB7Mbp8ioihWmRkNjy1XPjTqMEn5MvjaH3i1qsegxku36mBUc5fQ5NEW5JfHRr6i3TVf' | |
//const publicKey = 'zJss7xQxaumG3JviQ6XF4MefAeJrYmD2dGk6BaZSueRx2zFeWNg6aCc5biu1mY72rgj7aNbaGkvtYX1sxx2XFn6PFpar3QKpgNKkikEwp6MhP4GaffSadvVxiCzb7XrT6VpK5osS6YmgzksSd7ugNvf8TeUEN2EK2v7rjzVYhg9tEMBHGhq8ByCE2' | |
const buf = multibase.decode(publicKey) | |
console.log(buf); | |
const bb = u8a.toString(buf,'base16') | |
console.log(bb); | |
const bbb = bb.slice(4) | |
console.log('hex key: '+bbb) | |
console.log(bbb.length) | |
// this is the regular length public key | |
console.log(encodeDIDfromHexString(bbb)); | |
// compresedKeyInHex(publicKeyHex: string) | |
const compressedKey = compresedKeyInHex(bbb) | |
console.log(encodeDIDfromHexString(compressedKey)) | |
export function compresedKeyInHex(publicKeyHex) { | |
const xHex = publicKeyHex.slice(0,publicKeyHex.length/2); | |
const yHex = publicKeyHex.slice(publicKeyHex.length/2,publicKeyHex.length); | |
const xOctet = u8a.fromString(xHex,'base16') | |
const yOctet = u8a.fromString(yHex,'base16'); | |
const compressedPoint = ECPointCompress( xOctet , yOctet ); | |
const compressedPointHex = u8a.toString(compressedPoint,'base16'); | |
return compressedPointHex; | |
} | |
// compress a public key with points x,y expressed as UintArrays | |
// source: https://stackoverflow.com/questions/17171542/algorithm-for-elliptic-curve-point-compression | |
export function ECPointCompress( x, y ) | |
{ | |
const out = new Uint8Array( x.length + 1 ); | |
out[0] = 2 + ( y[ y.length-1 ] & 1 ); | |
out.set( x, 1 ); | |
return out; | |
} | |
export function encodeDIDfromHexString(publicKeyHex) { | |
const publicKey = u8a.fromString(publicKeyHex,'base16'); | |
const didKey = encodeDID(publicKey) | |
return didKey; | |
} | |
export function encodeDID(publicKey) { | |
const publicKeywPrefix = multicodec.addPrefix('p256-pub',publicKey) | |
const buf = multibase.encode('base58btc',publicKeywPrefix) | |
const bufAsString = u8a.toString(buf) | |
return `did:key:${bufAsString}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ToDo: Use https://github.com/bshambaugh/did-key-creator to rewrite this.