Class: Trx::Key

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#addressObject



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(message)
  prefixed_message = Signature.prefix_message message
  hashed_message = Utils.keccak256 prefixed_message
  sign hashed_message
end

#private_bytesObject



33
34
35
# File 'lib/trx/key.rb', line 33

def private_bytes
  @private_key.data
end

#private_hexObject



29
30
31
# File 'lib/trx/key.rb', line 29

def private_hex
  Utils.bin_to_hex @private_key.data
end

#public_bytesObject



45
46
47
# File 'lib/trx/key.rb', line 45

def public_bytes
  @public_key.uncompressed
end

#public_bytes_compressedObject



49
50
51
# File 'lib/trx/key.rb', line 49

def public_bytes_compressed
  @public_key.compressed
end

#public_hexObject



37
38
39
# File 'lib/trx/key.rb', line 37

def public_hex
  Utils.bin_to_hex @public_key.uncompressed
end

#public_hex_compressedObject



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