Class: Klay::Key
- Inherits:
-
Object
- Object
- Klay::Key
- Defined in:
- lib/klay/key.rb
Overview
The Key class to handle Secp256k1 private/public key-pairs.
Defined Under Namespace
Instance Attribute Summary collapse
-
#private_key ⇒ Object
readonly
The
Secp256k1::PrivateKey
of the Key pair. -
#public_key ⇒ Object
readonly
The
Secp256k1::PublicKey
of the Key pair.
Instance Method Summary collapse
-
#address ⇒ Klay::Address
Exports the checksummed public address.
-
#initialize(priv: nil) ⇒ Key
constructor
Constructor of the Key class.
-
#personal_sign(message, chain_id = nil) ⇒ String
Prefixes a message with
\u0019Klaytn Signed Message:
and signs it in the common way used by many web3 wallets. -
#private_bytes ⇒ String
Exports the private key bytes in a wrapper function to maintain backward-compatibility with older versions of Key.
-
#private_hex ⇒ String
Converts the private key data into a hexa-decimal string.
-
#public_bytes ⇒ String
Exports the uncompressed public key bytes in a wrapper function to maintain backward-compatibility with older versions of Key.
-
#public_bytes_compressed ⇒ String
Exports the compressed public key bytes.
-
#public_hex ⇒ String
Converts the public key data into an uncompressed hexa-decimal string.
-
#public_hex_compressed ⇒ String
Converts the public key data into an compressed hexa-decimal string.
-
#sign(blob, chain_id = nil) ⇒ String
Signs arbitrary data without validation.
-
#sign_typed_data(typed_data, chain_id = nil) ⇒ String
Prefixes, hashes, and signes a typed data structure in the common way used by many web3 wallets.
Constructor Details
#initialize(priv: nil) ⇒ Key
Constructor of the Klay::Key class. Creates a new random key-pair
if no priv
key is provided.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/klay/key.rb', line 43 def initialize(priv: nil) # Creates a new, randomized libsecp256k1 context. ctx = Secp256k1::Context.new context_randomization_bytes: SecureRandom.random_bytes(32) # Creates a new random key pair (public, private). key = ctx.generate_key_pair unless priv.nil? # Converts hex private keys to binary strings. priv = Util.hex_to_bin priv if Util.is_hex? priv # Creates a keypair from existing private key data. key = ctx.key_pair_from_private_key priv end # Sets the attributes. @private_key = key.private_key @public_key = key.public_key end |
Instance Attribute Details
#private_key ⇒ Object (readonly)
The Secp256k1::PrivateKey
of the Klay::Key pair.
34 35 36 |
# File 'lib/klay/key.rb', line 34 def private_key @private_key end |
#public_key ⇒ Object (readonly)
The Secp256k1::PublicKey
of the Klay::Key pair.
37 38 39 |
# File 'lib/klay/key.rb', line 37 def public_key @public_key end |
Instance Method Details
#address ⇒ Klay::Address
Exports the checksummed public address.
163 164 165 |
# File 'lib/klay/key.rb', line 163 def address Util.public_key_to_address public_bytes end |
#personal_sign(message, chain_id = nil) ⇒ String
Prefixes a message with \u0019Klaytn Signed Message:
and signs
it in the common way used by many web3 wallets. Complies with
EIP-191 prefix 0x19
and version byte 0x45
(E
). See also
Signature#personal_recover.
Ref: https://eips.ethereum.org/EIPS/eip-191
94 95 96 97 98 |
# File 'lib/klay/key.rb', line 94 def personal_sign(, chain_id = nil) = Signature. = Util.keccak256 sign , chain_id end |
#private_bytes ⇒ String
Exports the private key bytes in a wrapper function to maintain backward-compatibility with older versions of Klay::Key.
125 126 127 |
# File 'lib/klay/key.rb', line 125 def private_bytes @private_key.data end |
#private_hex ⇒ String
Converts the private key data into a hexa-decimal string.
117 118 119 |
# File 'lib/klay/key.rb', line 117 def private_hex Util.bin_to_hex @private_key.data end |
#public_bytes ⇒ String
Exports the uncompressed public key bytes in a wrapper function to maintain backward-compatibility with older versions of Klay::Key.
149 150 151 |
# File 'lib/klay/key.rb', line 149 def public_bytes @public_key.uncompressed end |
#public_bytes_compressed ⇒ String
Exports the compressed public key bytes.
156 157 158 |
# File 'lib/klay/key.rb', line 156 def public_bytes_compressed @public_key.compressed end |
#public_hex ⇒ String
Converts the public key data into an uncompressed hexa-decimal string.
133 134 135 |
# File 'lib/klay/key.rb', line 133 def public_hex Util.bin_to_hex @public_key.uncompressed end |
#public_hex_compressed ⇒ String
Converts the public key data into an compressed hexa-decimal string.
141 142 143 |
# File 'lib/klay/key.rb', line 141 def public_hex_compressed Util.bin_to_hex @public_key.compressed end |
#sign(blob, chain_id = nil) ⇒ String
Signs arbitrary data without validation. Should not be used unless really desired. See also: #personal_sign, #sign_typed_data, and Signature#recover.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/klay/key.rb', line 72 def sign(blob, chain_id = nil) context = Secp256k1::Context.new compact, recovery_id = context.sign_recoverable(@private_key, blob).compact signature = compact.bytes v = Chain.to_v recovery_id, chain_id is_leading_zero = true [v].pack("N").unpack("C*").each do |byte| is_leading_zero = false if byte > 0 and is_leading_zero signature.append byte unless is_leading_zero and byte === 0 end Util.bin_to_hex signature.pack "c*" end |
#sign_typed_data(typed_data, chain_id = nil) ⇒ String
Prefixes, hashes, and signes a typed data structure in the common
way used by many web3 wallets. Complies with EIP-191 prefix 0x19
and EIP-712 version byte 0x01
. Supports V3
, V4
. See also
Signature#recover_typed_data.
Ref: https://eips.ethereum.org/EIPS/eip-712
109 110 111 112 |
# File 'lib/klay/key.rb', line 109 def sign_typed_data(typed_data, chain_id = nil) hash_to_sign = Eip712.hash typed_data sign hash_to_sign, chain_id end |