Class: Tapyrus::Wallet::DB
- Inherits:
-
Object
- Object
- Tapyrus::Wallet::DB
- Defined in:
- lib/tapyrus/wallet/db.rb
Constant Summary collapse
- KEY_PREFIX =
{ account: "a", # key: account index, value: Account raw data. master: "m", # value: wallet seed. version: "v", # value: wallet version key: "k" # key: path to the key, value: public key }
Instance Attribute Summary collapse
-
#level_db ⇒ Object
readonly
Returns the value of attribute level_db.
-
#master_key ⇒ Object
get master_key.
Instance Method Summary collapse
-
#accounts ⇒ Object
get accounts raw data.
-
#close ⇒ Object
close database.
- #get_keys(account) ⇒ Object
-
#initialize(path = "#{Tapyrus.base_dir}/db/wallet") ⇒ DB
constructor
A new instance of DB.
-
#register_master_key(master) ⇒ Object
save seed.
-
#registered_master? ⇒ Boolean
whether master key registered.
- #save_account(account) ⇒ Object
- #save_key(account, purpose, index, key) ⇒ Object
-
#version ⇒ Object
wallet version.
Constructor Details
Instance Attribute Details
#level_db ⇒ Object (readonly)
Returns the value of attribute level_db.
11 12 13 |
# File 'lib/tapyrus/wallet/db.rb', line 11 def level_db @level_db end |
#master_key ⇒ Object
get master_key
55 56 57 |
# File 'lib/tapyrus/wallet/db.rb', line 55 def master_key @master_key end |
Instance Method Details
#accounts ⇒ Object
get accounts raw data.
25 26 27 28 29 |
# File 'lib/tapyrus/wallet/db.rb', line 25 def accounts from = KEY_PREFIX[:account] + "00000000" to = KEY_PREFIX[:account] + "ffffffff" level_db.each(from: from, to: to).map { |k, v| v } end |
#close ⇒ Object
close database
20 21 22 |
# File 'lib/tapyrus/wallet/db.rb', line 20 def close level_db.close end |
#get_keys(account) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/tapyrus/wallet/db.rb', line 47 def get_keys(account) id = [account.purpose, account.index].pack("I*").bth from = KEY_PREFIX[:key] + id + "00000000" to = KEY_PREFIX[:key] + id + "ffffffff" level_db.each(from: from, to: to).map { |k, v| v } end |
#register_master_key(master) ⇒ Object
save seed
61 62 63 64 65 |
# File 'lib/tapyrus/wallet/db.rb', line 61 def register_master_key(master) level_db.put(KEY_PREFIX[:master], master.to_payload) level_db.put(KEY_PREFIX[:version], Tapyrus::Wallet::Base::VERSION.to_s) @master_key = master end |
#registered_master? ⇒ Boolean
whether master key registered.
68 69 70 |
# File 'lib/tapyrus/wallet/db.rb', line 68 def registered_master? !level_db.get(KEY_PREFIX[:master]).nil? end |
#save_account(account) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/tapyrus/wallet/db.rb', line 31 def save_account(account) level_db.batch do id = [account.purpose, account.index].pack("I*").bth key = KEY_PREFIX[:account] + id level_db.put(key, account.to_payload) end end |
#save_key(account, purpose, index, key) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/tapyrus/wallet/db.rb', line 39 def save_key(account, purpose, index, key) pubkey = key.pub id = [account.purpose, account.index, purpose, index].pack("I*").bth k = KEY_PREFIX[:key] + id level_db.put(k, pubkey) key end |
#version ⇒ Object
wallet version
73 74 75 |
# File 'lib/tapyrus/wallet/db.rb', line 73 def version level_db.get(KEY_PREFIX[:version]).to_i end |