Skip to content

Instantly share code, notes, and snippets.

@dlongley
Created May 24, 2024 16:28
Show Gist options
  • Save dlongley/6a34fa5315adf9c59e7de7cfbe6212e3 to your computer and use it in GitHub Desktop.
Save dlongley/6a34fa5315adf9c59e7de7cfbe6212e3 to your computer and use it in GitHub Desktop.
WebCrypto Ed25519 raw format to multikey
// 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