Skip to content

Instantly share code, notes, and snippets.

@bumblefudge
Created February 20, 2024 21:43
Show Gist options
  • Save bumblefudge/8a28608777699034e893aa4874d1ea31 to your computer and use it in GitHub Desktop.
Save bumblefudge/8a28608777699034e893aa4874d1ea31 to your computer and use it in GitHub Desktop.
Expressing public keys and blockchain addresses as "street-legal DIDs"
DID syntax can be kind of confusing and arbitrary-seeming but it's basically just like URNs, an heirarchy of arbitrary schemata. Here's a tutorial on expressing public keys (or their hashed "address" forms popular in cryto networks) "as street-legal DIDs".
If it's a public key, the most common syntax is a `did:key`[^1]:
```
did:key:<base58btc multibase(<pub key bytes appropriately multicodeced>)>
```
The magic number in this case is 0xe7 [^2], which you'll want to prepend to the raw bytes.
Note that if you've never used `multiformats` before and you're not using IPFS/Protocol-Labs tooling, there's a step some people miss: you have to pad the raw bytes into an "unsigned varint"[^3] before base58btc-encoding but after prepending the magic number.
One nice sideeffect of this padding + prefacing with a byte or two of sigil is that once ASCII-encoded, there ends up being a one-character prefix that is constant for each major key type to date [^4].
If, instead of a bare public key, it's a hash or a public key otherwise transformed to serve as an address on a public crypto network, the most common syntax (for networks that have already profiled and entered into the registry) is `did:pkh` [^5]:
```
did:pkh:<caip-2, i.e. 'eip155:1' for eth mainnet>:<address>
```
If you want to express an address as a `did:pkh` but don't see the network listed, get the network included in the CASA namespaces project [^6] and I'll PR it into the `did:pkh` specification!
[^1]: https://w3c-ccg.github.io/did-method-key/#format
[^2]: https://github.com/multiformats/multicodec/blob/master/table.csv#L92
[^3]: https://github.com/multiformats/unsigned-varint
[^4]: https://w3c-ccg.github.io/did-method-key/#secp256k1
[^5]: https://github.com/w3c-ccg/did-pkh/blob/main/did-pkh-method-draft.md#syntax-and-interpretation
[^6]: https://namespaces.chainagnostic.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment