Class: Platon::Key

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

Defined Under Namespace

Classes: Decrypter, Encrypter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(priv: nil) ⇒ Key

Returns a new instance of Key.



46
47
48
49
# File 'lib/platon/key.rb', line 46

def initialize(priv: nil)
  @private_key = MoneyTree::PrivateKey.new key: priv
  @public_key = MoneyTree::PublicKey.new private_key, compressed: false
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



6
7
8
# File 'lib/platon/key.rb', line 6

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



6
7
8
# File 'lib/platon/key.rb', line 6

def public_key
  @public_key
end

Class Method Details

.decrypt(data, password) ⇒ Object



14
15
16
17
# File 'lib/platon/key.rb', line 14

def self.decrypt(data, password)
  priv = Decrypter.perform data, password
  new priv: priv
end

.encrypt(key, password) ⇒ Object



8
9
10
11
12
# File 'lib/platon/key.rb', line 8

def self.encrypt(key, password)
  key = new(priv: key) unless key.is_a?(Key)

  Encrypter.perform key.private_hex, password
end

.encrypt_and_save(key, password, keypath = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/platon/key.rb', line 19

def self.encrypt_and_save(key,password,keypath=nil)
  encrypted_key_info = encrypt(key,password)
  ## PlatON似乎打算兼容以太地址, 暂时默认以 0x 地址命名 
  if keypath==nil || keypath=="" 
    address = key.address
    dirname = "#{ENV['HOME']}/.platon/keystore/"
    FileUtils.mkdir_p(dirname) unless Dir.exists?(dirname)
    File.write File.join(dirname, "#{key.address}.json") , encrypted_key_info
  else
    File.write keypath , encrypted_key_info
  end
  return encrypted_key_info
end

.list_wallets(keypath = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/platon/key.rb', line 33

def self.list_wallets(keypath=nil)
  if keypath==nil || keypath=="" 
    Dir.glob("#{ENV['HOME']}/.platon/keystore/*").select { |e| File.file? e }
  else
    Dir.glob(keypath + "/*").select { |e| File.file? e }
  end
end

.personal_recover(message, signature) ⇒ Object



41
42
43
44
# File 'lib/platon/key.rb', line 41

def self.personal_recover(message, signature)
  bin_signature = Utils.hex_to_bin(signature).bytes.rotate(-1).pack('c*')
  OpenSsl.recover_compact(Utils.keccak256(Utils.prefix_message(message)), bin_signature)
end

Instance Method Details

#addressObject Also known as: to_address



63
64
65
# File 'lib/platon/key.rb', line 63

def address
  Utils.public_key_to_address public_hex
end

#bech32_address(hrp: "atp") ⇒ Object



67
68
69
# File 'lib/platon/key.rb', line 67

def bech32_address(hrp:"atp")
  Utils.to_bech32_address(hrp,address) ##TODO
end

#personal_sign(message) ⇒ Object



88
89
90
# File 'lib/platon/key.rb', line 88

def personal_sign(message)
  Utils.bin_to_hex(sign(Utils.prefix_message(message)).bytes.rotate(1).pack('c*'))
end

#private_hexObject



51
52
53
# File 'lib/platon/key.rb', line 51

def private_hex
  private_key.to_hex
end

#public_bytesObject



55
56
57
# File 'lib/platon/key.rb', line 55

def public_bytes
  public_key.to_bytes
end

#public_hexObject



59
60
61
# File 'lib/platon/key.rb', line 59

def public_hex
  public_key.to_hex
end

#sign(message) ⇒ Object



72
73
74
# File 'lib/platon/key.rb', line 72

def sign(message)
  sign_hash message_hash(message)
end

#sign_hash(hash) ⇒ Object



76
77
78
79
80
81
# File 'lib/platon/key.rb', line 76

def sign_hash(hash)
  loop do
    signature = OpenSsl.sign_compact hash, private_hex, public_hex
    return signature if valid_s? signature
  end
end

#verify_signature(message, signature) ⇒ Object



83
84
85
86
# File 'lib/platon/key.rb', line 83

def verify_signature(message, signature)
  hash = message_hash(message)
  public_hex == OpenSsl.recover_compact(hash, signature)
end