Class: Beowulf::Wallet
Constant Summary
Constants included
from ChainConfig
ChainConfig::EXPIRE_IN_SECS, ChainConfig::EXPIRE_IN_SECS_PROPOSAL, ChainConfig::NETWORKS_BEOWULF_ADDRESS_PREFIX, ChainConfig::NETWORKS_BEOWULF_BWF_ASSET, ChainConfig::NETWORKS_BEOWULF_CHAIN_ID, ChainConfig::NETWORKS_BEOWULF_DEFAULT_NODE, ChainConfig::NETWORKS_BEOWULF_VEST_ASSET, ChainConfig::NETWORKS_BEOWULF_W_ASSET, ChainConfig::NETWORK_CHAIN_IDS
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakPubKey, #pakQ, #pakS, #pakStr, #pakc, #pakq, #paks, #send_log, #unhexlify, #varint, #warning
Constructor Details
#initialize(options = {}) ⇒ Wallet
Returns a new instance of Wallet.
13
14
15
16
17
18
19
20
|
# File 'lib/beowulf/wallet.rb', line 13
def initialize(options = {})
@name = options[:name] || ''
@private_key = options[:private_key] || ''
@public_key = options[:public_key] || ''
@cipher_keys = options[:cipher_keys] || ''
@cipher_type = 'aes-256-cbc'
@salt = options[:salt] || ''
end
|
Instance Attribute Details
#cipher_keys ⇒ Object
Returns the value of attribute cipher_keys.
11
12
13
|
# File 'lib/beowulf/wallet.rb', line 11
def cipher_keys
@cipher_keys
end
|
#cipher_type ⇒ Object
Returns the value of attribute cipher_type.
11
12
13
|
# File 'lib/beowulf/wallet.rb', line 11
def cipher_type
@cipher_type
end
|
#name ⇒ Object
Returns the value of attribute name.
11
12
13
|
# File 'lib/beowulf/wallet.rb', line 11
def name
@name
end
|
#private_key ⇒ Object
Returns the value of attribute private_key.
11
12
13
|
# File 'lib/beowulf/wallet.rb', line 11
def private_key
@private_key
end
|
#public_key ⇒ Object
Returns the value of attribute public_key.
11
12
13
|
# File 'lib/beowulf/wallet.rb', line 11
def public_key
@public_key
end
|
#salt ⇒ Object
Returns the value of attribute salt.
11
12
13
|
# File 'lib/beowulf/wallet.rb', line 11
def salt
@salt
end
|
Instance Method Details
#decode_wallet(wallet_json_string, password) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/beowulf/wallet.rb', line 211
def decode_wallet(wallet_json_string, password)
if wallet_json_string == nil || wallet_json_string.length == 0
puts "Wallet json is not empty."
raise WalletError, "Wallet json is not empty."
end
wallet_json = JSON.parse(wallet_json_string)
@name = wallet_json['name']
@cipher_keys = wallet_json['cipher_keys']
@salt = wallet_json['salt']
new_password = password + @salt
pw_bytes = pakStr(new_password)
checksum = Digest::SHA512.digest(pw_bytes)
chs = hexlify(checksum)
data_byte = unhexlify(@cipher_keys)
msg_decrypt = decrypt(chs, data_byte)
msg_json = JSON.parse(msg_decrypt)
dechs = msg_json['checksum']
if !(chs.eql? dechs)
puts "Password wrong."
raise WalletError, "Password wrong."
end
keys = msg_json['keys']
keys.each do |key, value|
@public_key = key
@private_key = value
end
end
|
#decrypt(key, data) ⇒ Object
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/beowulf/wallet.rb', line 245
def decrypt(key, data)
block = key[0..31]
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.decrypt
last = 31 + aes.block_size
aes.iv = key[32..last]
aes.key = block
final_msg = aes.update(data) + aes.final
final_msg
end
|
#encode_wallet(password) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/beowulf/wallet.rb', line 130
def encode_wallet(password)
if @name == nil || @name.length == 0
puts "name is not empty."
raise WalletError, "name is not empty."
end
@salt = SecureRandom.alphanumeric(16)
new_password = password + @salt
pw_bytes = pakStr(new_password)
checksum = Digest::SHA512.digest(pw_bytes)
chs = hexlify(checksum)
keys = {@public_key => @private_key}
plain_keys = {"checksum" => chs, "keys" => keys}
plain_data = JSON.dump(plain_keys)
msg_encrypt = encrypt(chs, plain_data)
msg_hex = hexlify(msg_encrypt)
@cipher_keys = msg_hex
wallet_data = {"cipher_keys" => @cipher_keys, "cipher_type" => @cipher_type, "salt" => @salt, "name" => @name}
wallet_json = JSON.dump(wallet_data)
wallet_json
end
|
#encrypt(key, data) ⇒ Object
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/beowulf/wallet.rb', line 161
def encrypt(key, data)
block = key[0..31]
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.encrypt
last = 31 + aes.block_size
aes.iv = key[32..last]
aes.key = block
final_msg = aes.update(data) + aes.final
final_msg
end
|
#gen_keys ⇒ Object
33
34
35
36
|
# File 'lib/beowulf/wallet.rb', line 33
def gen_keys
@private_key = gen_priv
@public_key = gen_pub
end
|
#gen_priv ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/beowulf/wallet.rb', line 38
def gen_priv
rd = SecureRandom.alphanumeric(128)
seed = @name + "owner" + rd
bytes = pakStr(seed)
hashSha256 = Digest::SHA256.digest(bytes)
pk = unhexlify('80')
pk << hashSha256
chs = Digest::SHA256.digest(pk)
chs = Digest::SHA256.digest(chs)
b58 = pk
b58 << chs[0..3]
priv_key = Base58.binary_to_base58(b58, :bitcoin)
priv_key
end
|
#gen_pub ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/beowulf/wallet.rb', line 55
def gen_pub
if @private_key.length > 0
b58 = Base58.base58_to_binary(@private_key, :bitcoin)
lb58 = b58.length-4
chs = b58[lb58..-1]
hb58 = lb58-1
tpk = b58[0..hb58]
nchs = Digest::SHA256.digest(tpk)
nchs = Digest::SHA256.digest(nchs)
achs = hexlify(chs)
bnchs = hexlify(nchs[0..3])
if !(achs.eql? bnchs)
puts achs, bnchs
puts 'Invalid private key (checksum miss-match)'
raise WalletError, "Invalid private key (checksum miss-match): #{achs} != #{bnchs}"
end
bk = Bitcoin::Key.new(hexlify(tpk[1..-1]), nil, {compressed: true})
public_key_hex = bk.pub
public_key_byte = unhexlify(public_key_hex)
checksum = OpenSSL::Digest::RIPEMD160.digest(public_key_byte)
public_key = NETWORKS_BEOWULF_ADDRESS_PREFIX + Base58.binary_to_base58(public_key_byte + checksum[0..3], :bitcoin)
public_key
end
end
|
#read_wallet_file(wallet_path_file, password) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/beowulf/wallet.rb', line 172
def read_wallet_file(wallet_path_file, password)
if wallet_path_file == nil || wallet_path_file.length == 0
puts "Path file wallet is not empty."
raise WalletError, "Path file wallet is not empty."
end
if password == nil || password.length == 0
puts "Password is not empty."
raise WalletError, "Password is not empty."
end
file = File.read(wallet_path_file)
wallet_json = JSON.parse(file)
@name = wallet_json['name']
@cipher_keys = wallet_json['cipher_keys']
@salt = wallet_json['salt']
new_password = password + @salt
pw_bytes = pakStr(new_password)
checksum = Digest::SHA512.digest(pw_bytes)
chs = hexlify(checksum)
data_byte = unhexlify(@cipher_keys)
msg_decrypt = decrypt(chs, data_byte)
msg_json = JSON.parse(msg_decrypt)
dechs = msg_json['checksum']
if !(chs.eql? dechs)
puts "Password wrong."
raise WalletError, "Password wrong."
end
keys = msg_json['keys']
keys.each do |key, value|
@public_key = key
@private_key = value
end
end
|
#save_wallet_file(wallet_path, wallet_filename, password) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/beowulf/wallet.rb', line 84
def save_wallet_file(wallet_path, wallet_filename, password)
if password == nil || password.length == 0
puts "Password is not empty."
raise WalletError, "Password is not empty."
end
if password.length < 8
puts "Password length >= 8 character."
raise WalletError, "Password length >= 8 character."
end
if @name == nil || @name.length == 0
puts "name is not empty."
raise WalletError, "name is not empty."
end
@salt = SecureRandom.alphanumeric(16)
new_password = password + @salt
pw_bytes = pakStr(new_password)
checksum = Digest::SHA512.digest(pw_bytes)
chs = hexlify(checksum)
keys = {@public_key => @private_key}
plain_keys = {"checksum" => chs, "keys" => keys}
plain_data = JSON.dump(plain_keys)
msg_encrypt = encrypt(chs, plain_data)
msg_hex = hexlify(msg_encrypt)
@cipher_keys = msg_hex
file_path = wallet_filename
if wallet_filename.length == 0
file_path = @name + "-wallet.json"
end
if wallet_path.length > 0
file_path = File.join(wallet_path, file_path)
end
wallet_data = {"cipher_keys" => @cipher_keys, "cipher_type" => @cipher_type, "salt" => @salt, "name" => @name}
wallet_json = JSON.dump(wallet_data)
File.open(file_path, "w") { |file| file.puts wallet_json}
end
|
#to_json(options = {}) ⇒ Object
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/beowulf/wallet.rb', line 22
def to_json(options = {})
JSON.dump ({
:name => @name,
:private_key => @private_key,
:public_key => @public_key,
:cipher_keys => @cipher_keys,
:cipher_type => @cipher_type,
:salt => @salt
})
end
|