Class: DIDKit::PublicKey
- Inherits:
-
Object
- Object
- DIDKit::PublicKey
- Defined in:
- lib/didkit/public_key.rb
Overview
A public signing key of an ATProto account, decoded from its DID document or PLC operation.
Both k256 (secp256k1) and p256 (secp256r1, prime256v1) keys are supported. Keys from
legacy DID documents (before August 2023) without a did:key: prefix are intentionally not supported.
Related documentation:
Constant Summary collapse
- K256_PREFIX =
Multicodec varint prefix for a compressed
k256public key. "\xE7\x01".b.freeze
- P256_PREFIX =
Multicodec varint prefix for a compressed
p256public key. "\x80\x24".b.freeze
- K256_GROUP_NAME =
OpenSSL group name for the K-256 curve.
'secp256k1'- P256_GROUP_NAME =
OpenSSL group name for the P-256 curve.
'prime256v1'
Instance Attribute Summary collapse
-
#compressed_key ⇒ String
readonly
A 33-byte binary string containing the raw compressed public key.
-
#multibase ⇒ String
readonly
The original multikey value, Base58-encoded with a
zmultibase prefix.
Instance Method Summary collapse
-
#ec_group ⇒ OpenSSL::PKey::EC::Group
Returns the OpenSSL elliptic curve group representing the given key's type.
-
#ec_point ⇒ OpenSSL::PKey::EC::Point
Returns the OpenSSL object representing the key's point on the elliptic curve.
-
#initialize(string) ⇒ PublicKey
constructor
Decodes and validates an ATProto account public key.
-
#type ⇒ Symbol
Returns a short name of the key's elliptic curve.
Constructor Details
#initialize(string) ⇒ PublicKey
Decodes and validates an ATProto account public key.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/didkit/public_key.rb', line 48 def initialize(string) raise ArgumentError, "Expected a string argument" unless string.is_a?(String) # both supported curve prefixes produce 48 Base58 characters, plus the multibase prefix; # skip base58 decoding if the passed data is obviously wrong or too long raise PublicKeyError, "Unsupported key encoding: #{string.inspect}" unless string.start_with?('z') raise PublicKeyError, "Invalid encoded key length (#{string.bytesize})" unless string.bytesize == 49 @multibase = string begin key_decoded = Base58.base58_to_binary(string[1..-1], :bitcoin) rescue ArgumentError raise PublicKeyError, "Invalid Base58 encoding in key data" end prefix = key_decoded.byteslice(0, 2) case prefix when K256_PREFIX @key_type = :k256 when P256_PREFIX @key_type = :p256 else raise PublicKeyError, "Unsupported key type: #{string.inspect}" end raise PublicKeyError, "Invalid key length (#{key_decoded.bytesize})" unless key_decoded.bytesize == 35 @compressed_key = key_decoded.byteslice(2, 33) validate_ec_point end |
Instance Attribute Details
#compressed_key ⇒ String (readonly)
Returns a 33-byte binary string containing the raw compressed public key.
36 37 38 |
# File 'lib/didkit/public_key.rb', line 36 def compressed_key @compressed_key end |
#multibase ⇒ String (readonly)
Returns the original multikey value, Base58-encoded with a z multibase prefix.
39 40 41 |
# File 'lib/didkit/public_key.rb', line 39 def multibase @multibase end |
Instance Method Details
#ec_group ⇒ OpenSSL::PKey::EC::Group
Returns the OpenSSL elliptic curve group representing the given key's type.
92 93 94 |
# File 'lib/didkit/public_key.rb', line 92 def ec_group @ec_group ||= OpenSSL::PKey::EC::Group.new(@key_type == :k256 ? K256_GROUP_NAME : P256_GROUP_NAME) end |
#ec_point ⇒ OpenSSL::PKey::EC::Point
Returns the OpenSSL object representing the key's point on the elliptic curve.
100 101 102 |
# File 'lib/didkit/public_key.rb', line 100 def ec_point @ec_point ||= OpenSSL::PKey::EC::Point.new(ec_group, @compressed_key) end |
#type ⇒ Symbol
Returns a short name of the key's elliptic curve.
85 86 87 |
# File 'lib/didkit/public_key.rb', line 85 def type @key_type end |