Class: Trx::Key
- Inherits:
-
Object
- Object
- Trx::Key
- Defined in:
- lib/trx/key.rb
Instance Attribute Summary collapse
-
#private_key ⇒ Object
readonly
Returns the value of attribute private_key.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
Instance Method Summary collapse
- #address ⇒ Object
-
#initialize(priv: nil) ⇒ Key
constructor
A new instance of Key.
- #personal_sign(message) ⇒ Object
- #private_bytes ⇒ Object
- #private_hex ⇒ Object
- #public_bytes ⇒ Object
- #public_bytes_compressed ⇒ Object
- #public_hex ⇒ Object
- #public_hex_compressed ⇒ Object
- #sign(blob) ⇒ Object
Constructor Details
#initialize(priv: nil) ⇒ Key
Returns a new instance of Key.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/trx/key.rb', line 7 def initialize(priv: nil) # Creates a new, randomized libsecp256k1 context. ctx = Secp256k1::Context.new context_randomization_bytes: SecureRandom.random_bytes(32) key = if priv.nil? # Creates a new random key pair (public, private). ctx.generate_key_pair else # Converts hex private keys to binary strings. priv = Utils.hex_to_bin priv if Utils.is_hex? priv # Creates a keypair from existing private key data. 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)
Returns the value of attribute private_key.
5 6 7 |
# File 'lib/trx/key.rb', line 5 def private_key @private_key end |
#public_key ⇒ Object (readonly)
Returns the value of attribute public_key.
5 6 7 |
# File 'lib/trx/key.rb', line 5 def public_key @public_key end |
Instance Method Details
#address ⇒ Object
53 54 55 |
# File 'lib/trx/key.rb', line 53 def address Address.from_public_hex public_hex end |
#personal_sign(message) ⇒ Object
76 77 78 79 80 |
# File 'lib/trx/key.rb', line 76 def personal_sign() = Signature. = Utils.keccak256 sign end |
#private_bytes ⇒ Object
33 34 35 |
# File 'lib/trx/key.rb', line 33 def private_bytes @private_key.data end |
#private_hex ⇒ Object
29 30 31 |
# File 'lib/trx/key.rb', line 29 def private_hex Utils.bin_to_hex @private_key.data end |
#public_bytes ⇒ Object
45 46 47 |
# File 'lib/trx/key.rb', line 45 def public_bytes @public_key.uncompressed end |
#public_bytes_compressed ⇒ Object
49 50 51 |
# File 'lib/trx/key.rb', line 49 def public_bytes_compressed @public_key.compressed end |
#public_hex ⇒ Object
37 38 39 |
# File 'lib/trx/key.rb', line 37 def public_hex Utils.bin_to_hex @public_key.uncompressed end |
#public_hex_compressed ⇒ Object
41 42 43 |
# File 'lib/trx/key.rb', line 41 def public_hex_compressed Utils.bin_to_hex @public_key.compressed end |
#sign(blob) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/trx/key.rb', line 57 def sign(blob) context = Secp256k1::Context.new compact, recovery_id = context.sign_recoverable(@private_key, blob).compact signature = compact.bytes is_leading_zero = true signature.append recovery_id # TODO: WTF? It is necessary? # [recovery_id].pack("N").unpack("C*").each do |byte| # is_leading_zero = false if byte > 0 and is_leading_zero # unless is_leading_zero and byte === 0 # signature.append recovery_id # end # end Utils.bin_to_hex signature.pack "c*" end |