Class: Tapyrus::Wallet::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tapyrus/wallet/base.rb

Constant Summary collapse

VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



9
10
11
# File 'lib/tapyrus/wallet/base.rb', line 9

def db
  @db
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/tapyrus/wallet/base.rb', line 10

def path
  @path
end

#wallet_idObject

Returns the value of attribute wallet_id.



8
9
10
# File 'lib/tapyrus/wallet/base.rb', line 8

def wallet_id
  @wallet_id
end

Class Method Details

.create(wallet_id = 1, path_prefix = default_path_prefix) ⇒ Tapyrus::Wallet::Base

Create new wallet. If wallet already exist, throw error. The wallet generates a seed using SecureRandom and store to db at initialization.

Parameters:

  • wallet_id (String) (defaults to: 1)

    new wallet id.

  • path_prefix (String) (defaults to: default_path_prefix)

    wallet file path prefix.

Returns:

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
# File 'lib/tapyrus/wallet/base.rb', line 24

def self.create(wallet_id = 1, path_prefix = default_path_prefix)
  raise ArgumentError, "wallet_id : #{wallet_id} already exist." if self.exist?(wallet_id, path_prefix)
  w = self.new(wallet_id, path_prefix)
  # generate seed
  raise RuntimeError, 'the seed already exist.' if w.db.registered_master?
  master = Tapyrus::Wallet::MasterKey.generate
  w.db.register_master_key(master)
  w.('Default')
  w
end

.current_wallet(path_prefix = default_path_prefix) ⇒ Object

get current wallet



49
50
51
52
53
54
55
# File 'lib/tapyrus/wallet/base.rb', line 49

def self.current_wallet(path_prefix = default_path_prefix)
  path = wallet_paths(path_prefix).first # TODO default wallet selection
  return nil unless path
  path.slice!(path_prefix + 'wallet')
  path.slice!('/')
  self.load(path.to_i, path_prefix)
end

.default_path_prefixObject

get wallet dir path



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

def self.default_path_prefix
  "#{Tapyrus.base_dir}/db/wallet/"
end

.load(wallet_id, path_prefix = default_path_prefix) ⇒ Tapyrus::Wallet::Base

load wallet with specified wallet_id

Returns:

Raises:

  • (ArgumentError)


37
38
39
40
# File 'lib/tapyrus/wallet/base.rb', line 37

def self.load(wallet_id, path_prefix = default_path_prefix)
  raise ArgumentError, "wallet_id : #{wallet_id} dose not exist." unless self.exist?(wallet_id, path_prefix)
  self.new(wallet_id, path_prefix)
end

.wallet_paths(path_prefix = default_path_prefix) ⇒ Array

get wallets path

Returns:

  • (Array)

    Array of paths for each wallet dir.



44
45
46
# File 'lib/tapyrus/wallet/base.rb', line 44

def self.wallet_paths(path_prefix = default_path_prefix)
  Dir.glob("#{path_prefix}wallet*/").sort
end

Instance Method Details

#accounts(purpose = nil) ⇒ Object

get account list based on BIP-44



58
59
60
61
62
63
64
65
66
67
# File 'lib/tapyrus/wallet/base.rb', line 58

def accounts(purpose = nil)
  list = []
  db.accounts.each do |raw|
    a = .parse_from_payload(raw)
    next if purpose && purpose != a.purpose
    a.wallet = self
    list << a
  end
  list
end

#closeObject

close database wallet



106
107
108
# File 'lib/tapyrus/wallet/base.rb', line 106

def close
  db.close
end

#create_account(purpose = , name) ⇒ Tapyrus::Wallet::Account

create new account

Parameters:

  • purpose (Integer) (defaults to: )

    BIP44’s purpose.

  • name (String)

    a account name.

Returns:

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
# File 'lib/tapyrus/wallet/base.rb', line 73

def (purpose = ::PURPOSE_TYPE[:native_segwit], name)
  raise ArgumentError.new('Account already exists.') if (name, purpose)
  index = accounts.size
  path = "m/#{purpose}'/#{Tapyrus.chain_params.bip44_coin_type}'/#{index}'"
   = master_key.derive(path).ext_pubkey
   = .new(, purpose, index, name)
  .wallet = self
  .save
  
end

#decrypt(passphrase) ⇒ Object

decrypt wallet

Parameters:

  • passphrase (String)

    the wallet passphrase



125
126
127
# File 'lib/tapyrus/wallet/base.rb', line 125

def decrypt(passphrase)

end

#encrypt(passphrase) ⇒ Object

encrypt wallet

Parameters:

  • passphrase (String)

    the wallet passphrase



118
119
120
121
# File 'lib/tapyrus/wallet/base.rb', line 118

def encrypt(passphrase)
  master_key.encrypt(passphrase)
  db.register_master_key(master_key)
end

#generate_new_address(account_name) ⇒ String

create new tapyrus address for receiving payments.

Parameters:

  • account_name (String)

    an account name.

Returns:

  • (String)

    generated address.

Raises:

  • (ArgumentError)


94
95
96
97
98
# File 'lib/tapyrus/wallet/base.rb', line 94

def generate_new_address()
   = ()
  raise ArgumentError.new('Account does not exist.') unless 
  .create_receive.addr
end

#get_balance(account) ⇒ Object

get wallet balance.

Parameters:



86
87
88
89
# File 'lib/tapyrus/wallet/base.rb', line 86

def get_balance()
  # TODO get from utxo db.
  0.00000000
end

#master_keyTapyrus::Wallet::MasterKey

get master key



112
113
114
# File 'lib/tapyrus/wallet/base.rb', line 112

def master_key
  db.master_key
end

#to_hObject

wallet information



130
131
132
133
# File 'lib/tapyrus/wallet/base.rb', line 130

def to_h
  a = accounts.map(&:to_h)
  { wallet_id: wallet_id, version: version, account_depth: a.size, accounts: a, master: {encrypted: master_key.encrypted} }
end

#versionObject

get wallet version.



101
102
103
# File 'lib/tapyrus/wallet/base.rb', line 101

def version
  db.version
end

#watch_targetsArray[String]

get data elements tobe monitored with Bloom Filter.

Returns:



137
138
139
# File 'lib/tapyrus/wallet/base.rb', line 137

def watch_targets
  accounts.map(&:watch_targets).flatten
end