Class: Tapyrus::Wallet::DB

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path = "#{Tapyrus.base_dir}/db/wallet") ⇒ DB

Returns a new instance of DB.



14
15
16
17
# File 'lib/tapyrus/wallet/db.rb', line 14

def initialize(path = "#{Tapyrus.base_dir}/db/wallet")
  FileUtils.mkdir_p(path)
  @level_db = ::LevelDBNative::DB.new(path)
end

Instance Attribute Details

#level_dbObject (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_keyObject

get master_key



55
56
57
# File 'lib/tapyrus/wallet/db.rb', line 55

def master_key
  @master_key
end

Instance Method Details

#accountsObject

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

#closeObject

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()
  id = [.purpose, .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

Parameters:



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.

Returns:

  • (Boolean)


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 ()
  level_db.batch do
    id = [.purpose, .index].pack("I*").bth
    key = KEY_PREFIX[:account] + id
    level_db.put(key, .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(, purpose, index, key)
  pubkey = key.pub
  id = [.purpose, .index, purpose, index].pack("I*").bth
  k = KEY_PREFIX[:key] + id
  level_db.put(k, pubkey)
  key
end

#versionObject

wallet version



73
74
75
# File 'lib/tapyrus/wallet/db.rb', line 73

def version
  level_db.get(KEY_PREFIX[:version]).to_i
end