Created
May 24, 2024 16:28
-
-
Save dlongley/6a34fa5315adf9c59e7de7cfbe6212e3 to your computer and use it in GitHub Desktop.
WebCrypto Ed25519 raw format to multikey
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
// generate an Ed25519 key pair | |
> kp = await globalThis.crypto.subtle.generateKey({name: 'Ed25519'}, true, ['sign', 'verify']) | |
[Object: null prototype] { | |
privateKey: CryptoKey { | |
type: 'private', | |
extractable: true, | |
algorithm: { name: 'Ed25519' }, | |
usages: [ 'sign' ] | |
}, | |
publicKey: CryptoKey { | |
type: 'public', | |
extractable: true, | |
algorithm: { name: 'Ed25519' }, | |
usages: [ 'verify' ] | |
} | |
} | |
// output raw formatted public key | |
> raw = new Uint8Array(await subtle.exportKey('raw', kp.publicKey)) | |
Uint8Array(32) [ | |
4, 132, 125, 217, 225, 9, 89, 61, | |
203, 128, 6, 137, 43, 72, 115, 208, | |
56, 245, 236, 116, 227, 241, 86, 181, | |
144, 34, 118, 152, 87, 133, 3, 209 | |
] | |
// add Ed25519 public key multikey header | |
> mk = new Uint8Array([0xed, 0x01, ...raw]) | |
Uint8Array(34) [ | |
237, 1, 4, 132, 125, 217, 225, 9, | |
89, 61, 203, 128, 6, 137, 43, 72, | |
115, 208, 56, 245, 236, 116, 227, 241, | |
86, 181, 144, 34, 118, 152, 87, 133, | |
3, 209 | |
] | |
// then base-encode `mk` and prefix multibase header identifying the base encoding. | |
// e.g., 'z' + base58encode(mk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment