Class: Tapyrus::Wallet::Base
Constant Summary collapse
- VERSION =
1
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#wallet_id ⇒ Object
Returns the value of attribute wallet_id.
Class Method Summary collapse
-
.create(wallet_id = 1, path_prefix = default_path_prefix) ⇒ Tapyrus::Wallet::Base
Create new wallet.
-
.current_wallet(path_prefix = default_path_prefix) ⇒ Object
get current wallet.
-
.default_path_prefix ⇒ Object
get wallet dir path.
-
.load(wallet_id, path_prefix = default_path_prefix) ⇒ Tapyrus::Wallet::Base
load wallet with specified
wallet_id. -
.wallet_paths(path_prefix = default_path_prefix) ⇒ Array
get wallets path.
Instance Method Summary collapse
-
#accounts(purpose = nil) ⇒ Object
get account list based on BIP-44.
-
#close ⇒ Object
close database wallet.
-
#create_account(purpose = , name) ⇒ Tapyrus::Wallet::Account
create new account.
-
#decrypt(passphrase) ⇒ Object
decrypt wallet.
-
#encrypt(passphrase) ⇒ Object
encrypt wallet.
-
#generate_new_address(account_name) ⇒ String
create new tapyrus address for receiving payments.
-
#get_balance(account) ⇒ Object
get wallet balance.
-
#master_key ⇒ Tapyrus::Wallet::MasterKey
get master key.
-
#to_h ⇒ Object
wallet information.
-
#version ⇒ Object
get wallet version.
-
#watch_targets ⇒ Array[String]
get data elements tobe monitored with Bloom Filter.
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
9 10 11 |
# File 'lib/tapyrus/wallet/base.rb', line 9 def db @db end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/tapyrus/wallet/base.rb', line 10 def path @path end |
#wallet_id ⇒ Object
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.
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.create_account('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_prefix ⇒ Object
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
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
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 = Account.parse_from_payload(raw) next if purpose && purpose != a.purpose a.wallet = self list << a end list end |
#close ⇒ Object
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
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/tapyrus/wallet/base.rb', line 73 def create_account(purpose = Account::PURPOSE_TYPE[:native_segwit], name) raise ArgumentError.new('Account already exists.') if find_account(name, purpose) index = accounts.size path = "m/#{purpose}'/#{Tapyrus.chain_params.bip44_coin_type}'/#{index}'" account_key = master_key.derive(path).ext_pubkey account = Account.new(account_key, purpose, index, name) account.wallet = self account.save account end |
#decrypt(passphrase) ⇒ Object
decrypt wallet
125 126 127 |
# File 'lib/tapyrus/wallet/base.rb', line 125 def decrypt(passphrase) end |
#encrypt(passphrase) ⇒ Object
encrypt wallet
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.
94 95 96 97 98 |
# File 'lib/tapyrus/wallet/base.rb', line 94 def generate_new_address(account_name) account = find_account(account_name) raise ArgumentError.new('Account does not exist.') unless account account.create_receive.addr end |
#get_balance(account) ⇒ Object
get wallet balance.
86 87 88 89 |
# File 'lib/tapyrus/wallet/base.rb', line 86 def get_balance(account) # TODO get from utxo db. 0.00000000 end |
#master_key ⇒ Tapyrus::Wallet::MasterKey
get master key
112 113 114 |
# File 'lib/tapyrus/wallet/base.rb', line 112 def master_key db.master_key end |
#to_h ⇒ Object
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 |
#version ⇒ Object
get wallet version.
101 102 103 |
# File 'lib/tapyrus/wallet/base.rb', line 101 def version db.version end |
#watch_targets ⇒ Array[String]
get data elements tobe monitored with Bloom Filter.
137 138 139 |
# File 'lib/tapyrus/wallet/base.rb', line 137 def watch_targets accounts.map(&:watch_targets).flatten end |