beowulf-ruby-testnet 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 051760a12035fd7cec40d4580668e0a83d417f185df42bad78af8f182c6c4cfc
4
- data.tar.gz: 26b1c49130ebe45f5dfe568b47b91526409ccfdfabcf7d87c5afd7da1ce29589
3
+ metadata.gz: 27e0e138da847a7dc047b8fa71ab61c3e0ae9155e6b17b32deafc481ea6e6b16
4
+ data.tar.gz: 63fcc9da2cd8ddc534c21eaa7d7563d1a8e0387abce1f418ab950235469c9891
5
5
  SHA512:
6
- metadata.gz: a1727b552c1ad5a1bca03d122720f6dab9f307422bc8521f4a40f16f7625b6a1b33d049f1c7be67fcf8f5a2cc134c1043d20c374f76ea09f4e78653ec66f5948
7
- data.tar.gz: 20fd619bf000ebe4ff86e254c8506fc5302ca9bbc1b465e9c0629ca8d89177015efd346230b390ac26623d85ed1ec8fb483df20312732f74c387b2a4f4c3080e
6
+ metadata.gz: 3371235a6ee5ef9b49958c551f8d4263d968ad9239cd5cf8294a7c7a2ff0bdf162f31428b4b75a641969c59e3c98255ff0ad927b168a83e164203be0587551c4
7
+ data.tar.gz: 7d9777a57077c4504ccf009f35bf539b8c06a0e3d7b778e078d10e38b45867de09596bc6dd7ae56e20638c0403e8a26ce6bc7b805d3d66dadd49c573fb1b6a94
@@ -34,7 +34,7 @@ module Beowulf
34
34
  bytes
35
35
  end
36
36
 
37
- def to_json(options)
37
+ def to_json(options = {})
38
38
  JSON.dump ({
39
39
  :weight_threshold => @weight_threshold,
40
40
  :account_auths => @account_auths,
@@ -35,7 +35,7 @@ module Beowulf
35
35
  bytes
36
36
  end
37
37
 
38
- def to_json(options)
38
+ def to_json(options = {})
39
39
  JSON.dump ({
40
40
  :weight_threshold => @weight_threshold,
41
41
  :account_auths => @account_auths,
@@ -1,4 +1,4 @@
1
1
  module Beowulf
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  AGENT_ID = "beowulf/#{VERSION}"
4
4
  end
@@ -8,6 +8,8 @@ module Beowulf
8
8
  include ChainConfig
9
9
  include Utils
10
10
 
11
+ attr_accessor :name, :private_key, :public_key, :cipher_keys, :cipher_type, :salt
12
+
11
13
  def initialize(options = {})
12
14
  @name = options[:name] || ''
13
15
  @private_key = options[:private_key] || ''
@@ -17,16 +19,15 @@ module Beowulf
17
19
  @salt = options[:salt] || ''
18
20
  end
19
21
 
20
- def name
21
- @name
22
- end
23
-
24
- def private_key
25
- @private_key
26
- end
27
-
28
- def public_key
29
- @public_key
22
+ def to_json(options = {})
23
+ JSON.dump ({
24
+ :name => @name,
25
+ :private_key => @private_key,
26
+ :public_key => @public_key,
27
+ :cipher_keys => @cipher_keys,
28
+ :cipher_type => @cipher_type,
29
+ :salt => @salt
30
+ })
30
31
  end
31
32
 
32
33
  def gen_keys
@@ -142,6 +143,41 @@ module Beowulf
142
143
  File.open(file_path, "w") { |file| file.puts wallet_json}
143
144
  end
144
145
 
146
+ def encode_wallet(password)
147
+ # Validate
148
+ if @name == nil || @name.length == 0
149
+ puts "name is not empty."
150
+ raise WalletError, "name is not empty."
151
+ end
152
+
153
+ # Encrypt data
154
+ # Create checksum
155
+ @salt = SecureRandom.alphanumeric(16)
156
+ new_password = password + @salt
157
+ # puts "new_password:", new_password
158
+ pw_bytes = pakStr(new_password)
159
+ checksum = Digest::SHA512.digest(pw_bytes)
160
+ chs = hexlify(checksum)
161
+ # puts "chs", chs
162
+ # Create plain_data
163
+ keys = {@public_key => @private_key}
164
+ # puts "keys:", keys
165
+ plain_keys = {"checksum" => chs, "keys" => keys}
166
+ plain_data = JSON.dump(plain_keys)
167
+ # puts "plain_data:", plain_data
168
+
169
+ # Encrypt AES
170
+ msg_encrypt = encrypt(chs, plain_data)
171
+ msg_hex = hexlify(msg_encrypt)
172
+ # puts "msg_hex:", msg_hex
173
+ @cipher_keys = msg_hex
174
+
175
+ # Create data save.
176
+ wallet_data = {"cipher_keys" => @cipher_keys, "cipher_type" => @cipher_type, "salt" => @salt, "name" => @name}
177
+ wallet_json = JSON.dump(wallet_data)
178
+ wallet_json
179
+ end
180
+
145
181
  # https://gist.github.com/andrekandore/3140554
146
182
  def encrypt(key, data)
147
183
  block = key[0..31]
@@ -202,6 +238,45 @@ module Beowulf
202
238
  end
203
239
  end
204
240
 
241
+ def decode_wallet(wallet_json_string, password)
242
+ # Validate inputs
243
+ if wallet_json_string == nil || wallet_json_string.length == 0
244
+ puts "Wallet json is not empty."
245
+ raise WalletError, "Wallet json is not empty."
246
+ end
247
+ wallet_json = JSON.parse(wallet_json_string)
248
+ # Init properties wallet
249
+ @name = wallet_json['name']
250
+ @cipher_keys = wallet_json['cipher_keys']
251
+ # puts "decode_wallet.cipher_keys:", @cipher_keys
252
+ @salt = wallet_json['salt']
253
+ # Create checksum
254
+ new_password = password + @salt
255
+ # puts "new_password:", new_password
256
+ pw_bytes = pakStr(new_password)
257
+ checksum = Digest::SHA512.digest(pw_bytes)
258
+ chs = hexlify(checksum)
259
+ # puts "chs", chs
260
+ # Decrypt data
261
+ data_byte = unhexlify(@cipher_keys)
262
+ # Decrypt AES
263
+ msg_decrypt = decrypt(chs, data_byte)
264
+ # puts "msg_decrypt:", msg_decrypt
265
+ msg_json = JSON.parse(msg_decrypt)
266
+ # puts "msg_json:", msg_json
267
+ dechs = msg_json['checksum']
268
+ if !(chs.eql? dechs)
269
+ puts "Password wrong."
270
+ raise WalletError, "Password wrong."
271
+ end
272
+ keys = msg_json['keys']
273
+ keys.each do |key, value|
274
+ # puts "#{key} is #{value}"
275
+ @public_key = key
276
+ @private_key = value
277
+ end
278
+ end
279
+
205
280
  # https://gist.github.com/andrekandore/3140554
206
281
  def decrypt(key, data)
207
282
  block = key[0..31]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beowulf-ruby-testnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - NghiaTC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-11 00:00:00.000000000 Z
11
+ date: 2020-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler