Class: DIDKit::PublicKey

Inherits:
Object
  • Object
show all
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 k256 public key.

"\xE7\x01".b.freeze
P256_PREFIX =

Multicodec varint prefix for a compressed p256 public 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

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ PublicKey

Decodes and validates an ATProto account public key.

Parameters:

  • string (String) —

    key in the multikey string form, without the did:key: prefix

Raises:

  • (ArgumentError) —

    when the argument is not a string

  • (PublicKeyError) —

    when the key is invalid, incorrectly encoded, or has unsupported type



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.

Returns:

  • (String) —

    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.

Returns:

  • (String) —

    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.

Returns:

  • (OpenSSL::PKey::EC::Group) —

    the secp256k1 or prime256v1 group



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.

Returns:

  • (OpenSSL::PKey::EC::Point) —

    elliptic curve point for the key



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.

Returns:

  • (Symbol) —

    :k256 or :p256



85
86
87
# File 'lib/didkit/public_key.rb', line 85

def type
  @key_type
end