Class: Bitcoin::Wallet::Base
- Inherits:
-
Object
- Object
- Bitcoin::Wallet::Base
- Defined in:
- lib/bitcoin/wallet/base.rb
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, purpose = Account::PURPOSE_TYPE[:native_segwit]) ⇒ Bitcoin::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) ⇒ Bitcoin::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) ⇒ Bitcoin::Wallet::Account
create new account.
-
#decrypt(passphrase) ⇒ Object
decrypt wallet.
-
#encrypt(passphrase) ⇒ Object
encrypt wallet.
-
#generate_new_address(account_name) ⇒ String
create new bitcoin address for receiving payments.
-
#get_balance(account) ⇒ Object
get wallet balance.
-
#master_key ⇒ Bitcoin::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.
8 9 10 |
# File 'lib/bitcoin/wallet/base.rb', line 8 def db @db end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/bitcoin/wallet/base.rb', line 9 def path @path end |
#wallet_id ⇒ Object
Returns the value of attribute wallet_id.
7 8 9 |
# File 'lib/bitcoin/wallet/base.rb', line 7 def wallet_id @wallet_id end |
Class Method Details
.create(wallet_id = 1, path_prefix = default_path_prefix, purpose = Account::PURPOSE_TYPE[:native_segwit]) ⇒ Bitcoin::Wallet::Base
Create new wallet. If wallet already exist, throw error. The wallet generates a seed using SecureRandom and store to db at initialization.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/bitcoin/wallet/base.rb', line 23 def self.create(wallet_id = 1, path_prefix = default_path_prefix, purpose = Account::PURPOSE_TYPE[:native_segwit]) 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 = Bitcoin::Wallet::MasterKey.generate w.db.register_master_key(master) w.create_account(purpose, 'Default') w end |
.current_wallet(path_prefix = default_path_prefix) ⇒ Object
get current wallet
48 49 50 51 52 53 54 |
# File 'lib/bitcoin/wallet/base.rb', line 48 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
14 15 16 |
# File 'lib/bitcoin/wallet/base.rb', line 14 def self.default_path_prefix "#{Bitcoin.base_dir}/db/wallet/" end |
.load(wallet_id, path_prefix = default_path_prefix) ⇒ Bitcoin::Wallet::Base
load wallet with specified wallet_id
36 37 38 39 |
# File 'lib/bitcoin/wallet/base.rb', line 36 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
43 44 45 |
# File 'lib/bitcoin/wallet/base.rb', line 43 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
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bitcoin/wallet/base.rb', line 57 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
105 106 107 |
# File 'lib/bitcoin/wallet/base.rb', line 105 def close db.close end |
#create_account(purpose = , name) ⇒ Bitcoin::Wallet::Account
create new account
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bitcoin/wallet/base.rb', line 72 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}'/#{Bitcoin.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
124 125 126 |
# File 'lib/bitcoin/wallet/base.rb', line 124 def decrypt(passphrase) end |
#encrypt(passphrase) ⇒ Object
encrypt wallet
117 118 119 120 |
# File 'lib/bitcoin/wallet/base.rb', line 117 def encrypt(passphrase) master_key.encrypt(passphrase) db.register_master_key(master_key) end |
#generate_new_address(account_name) ⇒ String
create new bitcoin address for receiving payments.
93 94 95 96 97 |
# File 'lib/bitcoin/wallet/base.rb', line 93 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.
85 86 87 88 |
# File 'lib/bitcoin/wallet/base.rb', line 85 def get_balance(account) # TODO get from utxo db. 0.00000000 end |
#master_key ⇒ Bitcoin::Wallet::MasterKey
get master key
111 112 113 |
# File 'lib/bitcoin/wallet/base.rb', line 111 def master_key db.master_key end |
#to_h ⇒ Object
wallet information
129 130 131 132 |
# File 'lib/bitcoin/wallet/base.rb', line 129 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.
100 101 102 |
# File 'lib/bitcoin/wallet/base.rb', line 100 def version db.version end |