Class: Bitcoin::Wallet::Base
Constant Summary collapse
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) ⇒ Bitcoin::Wallet::Base
Create new wallet.
-
.current_wallet(path_prefix = DEFAULT_PATH_PREFIX) ⇒ Object
get current wallet.
-
.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) ⇒ 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.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bitcoin/wallet/base.rb', line 19 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 = Bitcoin::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
44 45 46 47 48 49 |
# File 'lib/bitcoin/wallet/base.rb', line 44 def self.current_wallet(path_prefix = DEFAULT_PATH_PREFIX) path = wallet_paths.first # TODO default wallet selection return nil unless path wallet_id = path.delete(path_prefix + '/wallet').delete('/').to_i self.load(wallet_id, path_prefix) end |
.load(wallet_id, path_prefix = DEFAULT_PATH_PREFIX) ⇒ Bitcoin::Wallet::Base
load wallet with specified wallet_id
32 33 34 35 |
# File 'lib/bitcoin/wallet/base.rb', line 32 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
39 40 41 |
# File 'lib/bitcoin/wallet/base.rb', line 39 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
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bitcoin/wallet/base.rb', line 52 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
100 101 102 |
# File 'lib/bitcoin/wallet/base.rb', line 100 def close db.close end |
#create_account(purpose = , name) ⇒ Bitcoin::Wallet::Account
create new account
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bitcoin/wallet/base.rb', line 67 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
119 120 121 |
# File 'lib/bitcoin/wallet/base.rb', line 119 def decrypt(passphrase) end |
#encrypt(passphrase) ⇒ Object
encrypt wallet
112 113 114 115 |
# File 'lib/bitcoin/wallet/base.rb', line 112 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.
88 89 90 91 92 |
# File 'lib/bitcoin/wallet/base.rb', line 88 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.
80 81 82 83 |
# File 'lib/bitcoin/wallet/base.rb', line 80 def get_balance(account) # TODO get from utxo db. 0.00000000 end |
#master_key ⇒ Bitcoin::Wallet::MasterKey
get master key
106 107 108 |
# File 'lib/bitcoin/wallet/base.rb', line 106 def master_key db.master_key end |
#to_h ⇒ Object
wallet information
124 125 126 127 |
# File 'lib/bitcoin/wallet/base.rb', line 124 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.
95 96 97 |
# File 'lib/bitcoin/wallet/base.rb', line 95 def version db.version end |
#watch_targets ⇒ Array[String]
get data elements tobe monitored with Bloom Filter.
131 132 133 |
# File 'lib/bitcoin/wallet/base.rb', line 131 def watch_targets accounts.map(&:watch_targets).flatten end |