···183183 return "z" + base58.Encode(kbytes)
184184}
185185186186-// Returns the DID cryptographic suite string which would be included in the `type` field of a `verificationMethod`.
187187-func (k *PublicKeyK256) LegacyDidDocSuite() string {
188188- // NOTE: this is not a W3C standard suite, and will probably be replaced with "Multikey"
189189- return "EcdsaSecp256k1VerificationKey2019"
190190-}
191191-192186// Returns a did:key string encoding of the public key, as would be encoded in a DID PLC operation:
193187//
194188// - compressed / compacted binary representation
···199193func (k *PublicKeyK256) DidKey() string {
200194 return "did:key:" + k.Multibase()
201195}
202202-203203-// Returns multibase string encoding of the public key, as would be included in an older DID Document "verificationMethod" section:
204204-//
205205-// - non-compressed / non-compacted binary representation
206206-// - encode bytes with base58btc
207207-// - prefix "z" (lower-case) to indicate encoding
208208-func (k *PublicKeyK256) LegacyMultibase() string {
209209- kbytes := k.UncompressedBytes()
210210- return "z" + base58.Encode(kbytes)
211211-}
-32
atproto/crypto/keys.go
···6464 // For curves with no compressed/uncompressed distinction, returns the same
6565 // value as Bytes().
6666 UncompressedBytes() []byte
6767-6868- // Outputs a DID cryptographic suite type name for this curve, as used in
6969- // some DID documents.
7070- // This method may be removed in the future.
7171- LegacyDidDocSuite() string
7272-7373- // Helper to serialize in a format used in older DID documents:
7474- // uncompressed byte encoding, no multicodec prefix, base58btc multibase
7575- // string encoding ("z" prefix)
7676- // This method may be removed in the future.
7777- LegacyMultibase() string
7867}
79688069// Parses a public key from multibase encoding, with multicodec indicating the key type.
···11099 mb := strings.TrimPrefix(didKey, "did:key:")
111100 return ParsePublicMultibase(mb)
112101}
113113-114114-// Parses a public key in multibase encoding, as would be found in a older DID Document `verificationMethod` section: uncompressed binary, no multicodec prefix, and base58btc multibase string encoding.
115115-//
116116-// This function is likely to be deprecated and removed.
117117-func ParsePublicLegacyMultibase(encoded string, didDocSuite string) (PublicKey, error) {
118118- if len(encoded) < 2 || encoded[0] != 'z' {
119119- return nil, fmt.Errorf("crypto: not a multibase base58btc string")
120120- }
121121- data, err := base58.Decode(encoded[1:])
122122- if err != nil {
123123- return nil, fmt.Errorf("crypto: not a multibase base58btc string")
124124- }
125125- switch didDocSuite {
126126- case "EcdsaSecp256r1VerificationKey2019":
127127- return ParsePublicUncompressedBytesP256(data)
128128- case "EcdsaSecp256k1VerificationKey2019":
129129- return ParsePublicUncompressedBytesK256(data)
130130- default:
131131- return nil, fmt.Errorf("unhandled legacy crypto suite: %s", didDocSuite)
132132- }
133133-}
-5
atproto/crypto/keys_test.go
···4747 assert.NoError(err)
4848 assert.True(pubP256.Equal(pubP256FromUncompBytes))
49495050- pubP256LegacyMultibaseString := pubP256.LegacyMultibase()
5151- pubP256LMB, err := ParsePublicLegacyMultibase(pubP256LegacyMultibaseString, "EcdsaSecp256r1VerificationKey2019")
5252- assert.NoError(err)
5353- assert.True(pubP256.Equal(pubP256LMB))
5454-5550 both := []PrivateKey{privP256, privK256}
5651 for _, priv := range both {
5752 pub, err := priv.Public()
-15
atproto/crypto/p256.go
···240240 return "z" + base58.Encode(kbytes)
241241}
242242243243-// Returns the DID cryptographic suite string which would be included in the `type` field of a `verificationMethod`.
244244-func (k *PublicKeyP256) LegacyDidDocSuite() string {
245245- return "EcdsaSecp256r1VerificationKey2019"
246246-}
247247-248243// Returns a did:key string encoding of the public key, as would be encoded in a DID PLC operation:
249244//
250245// - compressed / compacted binary representation
···255250func (k *PublicKeyP256) DidKey() string {
256251 return "did:key:" + k.Multibase()
257252}
258258-259259-// Returns multibase string encoding of the public key, as would be included in an older DID Document "verificationMethod" section:
260260-//
261261-// - non-compressed / non-compacted binary representation
262262-// - encode bytes with base58btc
263263-// - prefix "z" (lower-case) to indicate encoding
264264-func (k *PublicKeyP256) LegacyMultibase() string {
265265- kbytes := k.UncompressedBytes()
266266- return "z" + base58.Encode(kbytes)
267267-}