Class: Klay::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/klay/key.rb

Overview

The Key class to handle Secp256k1 private/public key-pairs.

Defined Under Namespace

Classes: Decrypter, Encrypter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(priv: nil) ⇒ Key

Constructor of the Klay::Key class. Creates a new random key-pair if no priv key is provided.

Parameters:

  • priv (String) (defaults to: nil)

    binary string of private key data.



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_keyObject (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_keyObject (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

#addressKlay::Address

Exports the checksummed public address.

Returns:

  • (Klay::Address)

    compressed address as packed hex prefixed string.



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

Parameters:

  • message (String)

    the message string to be prefixed and signed.

  • chain_id (Integer) (defaults to: nil)

    the chain id the signature should be generated on.

Returns:

  • (String)

    an EIP-191 conform, hexa-decimal signature.



94
95
96
97
98
# File 'lib/klay/key.rb', line 94

def personal_sign(message, chain_id = nil)
  prefixed_message = Signature.prefix_message message
  hashed_message = Util.keccak256 prefixed_message
  sign hashed_message, chain_id
end

#private_bytesString

Exports the private key bytes in a wrapper function to maintain backward-compatibility with older versions of Klay::Key.

Returns:

  • (String)

    private key as packed byte-string.



125
126
127
# File 'lib/klay/key.rb', line 125

def private_bytes
  @private_key.data
end

#private_hexString

Converts the private key data into a hexa-decimal string.

Returns:

  • (String)

    private key as 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_bytesString

Exports the uncompressed public key bytes in a wrapper function to maintain backward-compatibility with older versions of Klay::Key.

Returns:

  • (String)

    uncompressed public key as packed byte-string.



149
150
151
# File 'lib/klay/key.rb', line 149

def public_bytes
  @public_key.uncompressed
end

#public_bytes_compressedString

Exports the compressed public key bytes.

Returns:

  • (String)

    compressed public key as packed byte-string.



156
157
158
# File 'lib/klay/key.rb', line 156

def public_bytes_compressed
  @public_key.compressed
end

#public_hexString

Converts the public key data into an uncompressed hexa-decimal string.

Returns:

  • (String)

    public key as 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_compressedString

Converts the public key data into an compressed hexa-decimal string.

Returns:

  • (String)

    public key as 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.

Parameters:

  • blob (Object)

    that arbitrary data to be signed.

  • chain_id (Integer) (defaults to: nil)

    the chain id the signature should be generated on.

Returns:

  • (String)

    a hexa-decimal signature.



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

Parameters:

  • typed_data (Array)

    all the data in the typed data structure to be signed.

  • chain_id (Integer) (defaults to: nil)

    the chain id the signature should be generated on.

Returns:

  • (String)

    an EIP-712 conform, hexa-decimal signature.



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