Class: Platon::Key
- Inherits:
-
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.
48
49
50
51
|
# File 'lib/platon/key.rb', line 48
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_key ⇒ Object
Returns the value of attribute private_key.
6
7
8
|
# File 'lib/platon/key.rb', line 6
def private_key
@private_key
end
|
#public_key ⇒ Object
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, options = {}) ⇒ Object
8
9
10
11
12
|
# File 'lib/platon/key.rb', line 8
def self.encrypt(key, password,options={})
key = new(priv: key) unless key.is_a?(Key)
Encrypter.perform key.private_hex, password ,options
end
|
.encrypt_and_save(key, password, options = {}) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/platon/key.rb', line 19
def self.encrypt_and_save(key,password,options={})
encrypted_key_info = encrypt(key,password,options)
keypath = options[:keypath]
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(options = {}) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/platon/key.rb', line 34
def self.list_wallets(options={})
keypath = options[:keypath]
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
Instance Method Details
#address ⇒ Object
Also known as:
to_address
#bech32_address(hrp: "lat") ⇒ Object
69
70
71
|
# File 'lib/platon/key.rb', line 69
def bech32_address(hrp:"lat")
Utils.to_bech32_address(hrp,address)
end
|
#personal_sign(message) ⇒ Object
#private_hex ⇒ Object
53
54
55
|
# File 'lib/platon/key.rb', line 53
def private_hex
private_key.to_hex
end
|
#public_bytes ⇒ Object
57
58
59
|
# File 'lib/platon/key.rb', line 57
def public_bytes
public_key.to_bytes
end
|
#public_hex ⇒ Object
61
62
63
|
# File 'lib/platon/key.rb', line 61
def public_hex
public_key.to_hex
end
|
#sign(message) ⇒ Object
74
75
76
|
# File 'lib/platon/key.rb', line 74
def sign(message)
sign_hash message_hash(message)
end
|
#sign_hash(hash) ⇒ Object
78
79
80
81
82
83
|
# File 'lib/platon/key.rb', line 78
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
85
86
87
88
|
# File 'lib/platon/key.rb', line 85
def verify_signature(message, signature)
hash = message_hash(message)
public_hex == OpenSsl.recover_compact(hash, signature)
end
|