···195195 }
196196}
197197198198-// Loads a [PublicKey] of the indicated curve type from raw bytes, as exported by the [PublicKey.CompressedBytes] method.
198198+// Loads a [PublicKey] of the indicated curve type from raw bytes, as exported by the [PublicKey.Bytes] method. This is the "compressed" curve format.
199199//
200200// Calling code needs to know the key type ahead of time, and must remove any string encoding (hex encoding, base64, etc) before calling this function.
201201-func ParsePublicCompressedBytes(data []byte, kt KeyType) (*PublicKey, error) {
201201+func ParsePublicBytes(data []byte, kt KeyType) (*PublicKey, error) {
202202 switch kt {
203203 case P256:
204204 curve := elliptic.P256()
···240240 }
241241}
242242243243-// Loads a [PublicKey] of the indicated curve type from raw bytes, as exported by the [PublicKey.CompressedBytes] method.
243243+// Loads a [PublicKey] of the indicated curve type from raw bytes, as exported by the [PublicKey.UncompressedBytes] method.
244244//
245245// Calling code needs to know the key type ahead of time, and must remove any string encoding (hex encoding, base64, etc) before calling this function.
246246func ParsePublicUncompressedBytes(data []byte, kt KeyType) (*PublicKey, error) {
···277277 }
278278}
279279280280-// Parses a public key in multibase encoding, as would be found in a DID Document `verificationMethod` section.
280280+// Parses a public key in multibase encoding, as would be found in a older DID Document `verificationMethod` section.
281281//
282282// This implementation does not handle the many possible multibase encodings (eg, base32), only the base58btc encoding that would be found in a DID Document.
283283-func ParsePublicMultibase(encoded string, kt KeyType) (*PublicKey, error) {
283283+func ParsePublicLegacyMultibase(encoded string, kt KeyType) (*PublicKey, error) {
284284 if len(encoded) < 2 || encoded[0] != 'z' {
285285 return nil, fmt.Errorf("crypto: not a multibase base58btc string")
286286 }
···291291 return ParsePublicUncompressedBytes(data, kt)
292292}
293293294294-// Parses a public key in a variant of multibase encoding, with no key type indicator (unlike did:key), but with key compression (unlike `verificationMethod` in a DID Document).
295295-func ParsePublicCompressedMultibase(encoded string, kt KeyType) (*PublicKey, error) {
294294+// Parses a public key from multibase encoding, with multicodec indicating the key type.
295295+func ParsePublicMultibase(encoded string) (*PublicKey, error) {
296296 if len(encoded) < 2 || encoded[0] != 'z' {
297297 return nil, fmt.Errorf("crypto: not a multibase base58btc string")
298298 }
···300300 if err != nil {
301301 return nil, fmt.Errorf("crypto: not a multibase base58btc string")
302302 }
303303- return ParsePublicCompressedBytes(data, kt)
303303+ if data[0] == 0x80 && data[1] == 0x24 {
304304+ // multicodec p256-pub, code 0x1200, varint-encoded bytes: [0x80, 0x24]
305305+ return ParsePublicBytes(data[2:], P256)
306306+ } else if data[0] == 0xE7 && data[1] == 0x01 {
307307+ // multicodec secp256k1-pub, code 0xE7, varint bytes: [0xE7, 0x01]
308308+ return ParsePublicBytes(data[2:], K256)
309309+ } else {
310310+ return nil, fmt.Errorf("unexpected did:key multicode value")
311311+ }
304312}
305313306314// Loads a [PublicKey] from did:key string serialization.
···310318 if !strings.HasPrefix(didKey, "did:key:z") {
311319 return nil, fmt.Errorf("string is not a DID key: %s", didKey)
312320 }
313313- mb := strings.TrimPrefix(didKey, "did:key:z")
314314- data, err := base58.Decode(mb)
315315- if err != nil || len(data) < 2 {
316316- return nil, fmt.Errorf("crypto: not a multibase base58btc string")
317317- }
318318- if data[0] == 0x80 && data[1] == 0x24 {
319319- // multicodec p256-pub, code 0x1200, varint-encoded bytes: [0x80, 0x24]
320320- return ParsePublicCompressedBytes(data[2:], P256)
321321- } else if data[0] == 0xE7 && data[1] == 0x01 {
322322- // multicodec secp256k1-pub, code 0xE7, varint bytes: [0xE7, 0x01]
323323- return ParsePublicCompressedBytes(data[2:], K256)
324324- } else {
325325- return nil, fmt.Errorf("unexpected did:key multicode value")
326326- }
321321+ mb := strings.TrimPrefix(didKey, "did:key:")
322322+ return ParsePublicMultibase(mb)
327323}
328324329325// Serializes the [PublicKey] in to "uncompressed" binary format.
···348344}
349345350346// Serializes the [PublicKey] in to "compressed" binary format.
351351-func (k *PublicKey) CompressedBytes() []byte {
347347+func (k *PublicKey) Bytes() []byte {
352348 switch k.keyType {
353349 case P256:
354350 if !k.pubP256.Curve.IsOnCurve(k.pubP256.X, k.pubP256.Y) {
···414410// - add "z" prefix to indicate encoding
415411// - add "did:key:" prefix
416412func (k *PublicKey) DidKey() string {
417417- kbytes := k.CompressedBytes()
413413+ return "did:key:" + k.Multibase()
414414+}
415415+416416+// Returns a multibased string encoding of the public key, including a multicodec indicator and compressed curve bytes serialization
417417+func (k *PublicKey) Multibase() string {
418418+ kbytes := k.Bytes()
418419 switch k.keyType {
419420 case P256:
420421 // multicodec p256-pub, code 0x1200, varint-encoded bytes: [0x80, 0x24]
···425426 default:
426427 panic("unexpected crypto KeyType")
427428 }
428428- return "did:key:z" + base58.Encode(kbytes)
429429+ return "z" + base58.Encode(kbytes)
429430}
430431431431-// Returns multibase string encoding of the public key, as would be included in a DID Document "verificationMethod" section:
432432+// Returns multibase string encoding of the public key, as would be included in an older DID Document "verificationMethod" section:
432433//
433434// - non-compressed / non-compacted binary representation
434435// - encode bytes with base58btc
435436// - prefix "z" (lower-case) to indicate encoding
436436-func (k *PublicKey) Multibase() string {
437437+func (k *PublicKey) LegacyMultibase() string {
437438 kbytes := k.UncompressedBytes()
438439 return "z" + base58.Encode(kbytes)
439440}
440441441441-// Variant of Multibase() which outputs compressed key format.
442442-func (k *PublicKey) CompressedMultibase() string {
443443- kbytes := k.CompressedBytes()
444444- return "z" + base58.Encode(kbytes)
445445-}
446446-447442func (k *PublicKey) KeyType() KeyType {
448443 return k.keyType
449444}
450445451446// Returns the DID cryptographic suite string which would be included in the `type` field of a `verificationMethod`.
452452-func (k *PublicKey) DidDocSuite() string {
447447+func (k *PublicKey) LegacyDidDocSuite() string {
453448 switch k.keyType {
454449 case P256:
455450 return "EcdsaSecp256r1VerificationKey2019"