Class: Ed25519Keypair

Inherits:
Object
  • Object
show all
Defined in:
lib/calimero/types/keypair.rb

Constant Summary collapse

PROTOBUF_PREFIX =

Expected protobuf prefix for Ed25519 keypair (type: 1, data length: 64) The implementation is used for compatibility with Ed25519 keypair from [libp2p_identity/keypair](github.com/libp2p/rust-libp2p/blob/88f7875ad1a3e240aa2d9b9fb6f6c5354f1a62eb/identity/src/keypair.rs#L262)

"\x08\x01\x12\x40".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base58_keypair) ⇒ Ed25519Keypair

Initialize with a Base58-encoded keypair string

Raises:



17
18
19
20
21
22
23
24
# File 'lib/calimero/types/keypair.rb', line 17

def initialize(base58_keypair)
  raise KeypairError, "Base58 keypair cannot be nil" if base58_keypair.nil?
  @key_bytes = decode_base58(base58_keypair)
  validate_keypair_length
  validate_protobuf_prefix
  extract_keys
  initialize_signing_key
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



9
10
11
# File 'lib/calimero/types/keypair.rb', line 9

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



9
10
11
# File 'lib/calimero/types/keypair.rb', line 9

def public_key
  @public_key
end

#stored_public_keyObject (readonly)

Returns the value of attribute stored_public_key.



9
10
11
# File 'lib/calimero/types/keypair.rb', line 9

def stored_public_key
  @stored_public_key
end

Instance Method Details

#sign(message) ⇒ Object

Sign a message (as raw bytes)



27
28
29
# File 'lib/calimero/types/keypair.rb', line 27

def sign(message)
  @signing_key.sign(message)
end

#validate_protobuf_prefixObject



50
51
52
53
54
55
# File 'lib/calimero/types/keypair.rb', line 50

def validate_protobuf_prefix
  prefix = @key_bytes[0..3]
  unless prefix == PROTOBUF_PREFIX
    raise KeypairError, "Invalid protobuf prefix: #{prefix.unpack1('H*')} (expected #{PROTOBUF_PREFIX.unpack1('H*')})"
  end
end

#verify(signature, message) ⇒ Object

Verify a signature against a message



32
33
34
35
36
37
# File 'lib/calimero/types/keypair.rb', line 32

def verify(signature, message)
  @verify_key.verify(signature, message)
  true
rescue Ed25519::VerifyError
  false
end