Class: Solace::Keypair
- Inherits:
-
Object
- Object
- Solace::Keypair
- Defined in:
- lib/solace/keypair.rb
Constant Summary collapse
- SECRET_LENGTH =
!@const SECRET_LENGTH
The length of a Solana secret key in bytes 64- SEED_LENGTH =
!@const SEED_LENGTH
The length of a Solana seed in bytes (borrowed from RbNaCl) RbNaCl::Signatures::Ed25519::SEEDBYTES
- SigningKey =
!@const SigningKey
The RbNaCl signing key RbNaCl::Signatures::Ed25519::SigningKey
Instance Attribute Summary collapse
-
#keypair_bytes ⇒ Object
readonly
!@attribute [r] keypair_bytes @return [Array<Integer>] The keypair bytes.
Class Method Summary collapse
-
.from_secret_key(secret_key) ⇒ Keypair
Create a keypair from a 64-byte secret key.
-
.from_seed(seed) ⇒ Keypair
Create a keypair from a 32-byte seed.
-
.generate ⇒ Keypair
Generate a new random keypair.
Instance Method Summary collapse
-
#address ⇒ String
Returns the public key as a Base58 string.
-
#initialize(keypair_bytes) ⇒ Keypair
constructor
Initialize a new keypair.
-
#private_key ⇒ String
Returns the private key.
-
#public_key ⇒ PublicKey
Returns the public key.
-
#public_key_bytes ⇒ String
Returns the public key bytes.
-
#sign(message) ⇒ Binary
Signs a message (string or binary).
-
#signing_key ⇒ RbNaCl::Signatures::Ed25519::SigningKey
Returns the signing key.
Constructor Details
#initialize(keypair_bytes) ⇒ Keypair
Initialize a new keypair
68 69 70 71 72 |
# File 'lib/solace/keypair.rb', line 68 def initialize(keypair_bytes) raise ArgumentError, 'Keypair must be 64 bytes' unless keypair_bytes.length == SECRET_LENGTH @keypair_bytes = keypair_bytes end |
Instance Attribute Details
#keypair_bytes ⇒ Object (readonly)
!@attribute [r] keypair_bytes
@return [Array<Integer>] The keypair bytes
33 34 35 |
# File 'lib/solace/keypair.rb', line 33 def keypair_bytes @keypair_bytes end |
Class Method Details
.from_secret_key(secret_key) ⇒ Keypair
Create a keypair from a 64-byte secret key
57 58 59 60 61 |
# File 'lib/solace/keypair.rb', line 57 def from_secret_key(secret_key) raise ArgumentError, 'Secret key must be 64 bytes' unless secret_key.length == SECRET_LENGTH new(SigningKey.new(secret_key[0..31]).keypair_bytes.bytes) end |
.from_seed(seed) ⇒ Keypair
Create a keypair from a 32-byte seed
47 48 49 50 51 |
# File 'lib/solace/keypair.rb', line 47 def from_seed(seed) raise ArgumentError, 'Seed must be 32 bytes' unless seed.length == SEED_LENGTH new(SigningKey.new(seed).keypair_bytes.bytes) end |
.generate ⇒ Keypair
Generate a new random keypair
39 40 41 |
# File 'lib/solace/keypair.rb', line 39 def generate from_seed(RbNaCl::Random.random_bytes(SEED_LENGTH)) end |
Instance Method Details
#address ⇒ String
Returns the public key as a Base58 string
109 110 111 |
# File 'lib/solace/keypair.rb', line 109 def address public_key.to_base58 end |
#private_key ⇒ String
Returns the private key
The private key is the first 32 bytes of the keypair
102 103 104 |
# File 'lib/solace/keypair.rb', line 102 def private_key keypair_bytes[0..31].pack('C*') end |
#public_key ⇒ PublicKey
Returns the public key
77 78 79 |
# File 'lib/solace/keypair.rb', line 77 def public_key @public_key ||= Solace::PublicKey.new(public_key_bytes) end |
#public_key_bytes ⇒ String
Returns the public key bytes
The public key bytes are the last 32 bytes of the keypair
93 94 95 |
# File 'lib/solace/keypair.rb', line 93 def public_key_bytes keypair_bytes[32..63] end |
#sign(message) ⇒ Binary
Signs a message (string or binary)
117 118 119 |
# File 'lib/solace/keypair.rb', line 117 def sign() signing_key.sign() end |
#signing_key ⇒ RbNaCl::Signatures::Ed25519::SigningKey
Returns the signing key
84 85 86 |
# File 'lib/solace/keypair.rb', line 84 def signing_key @signing_key ||= SigningKey.new(private_key) end |