class Bitcoin::Wallet::Base

Constants

VERSION

Attributes

db[R]
path[R]
wallet_id[RW]

Public Class Methods

create(wallet_id = 1, path_prefix = default_path_prefix, purpose = Account::PURPOSE_TYPE[:native_segwit]) click to toggle source

Create new wallet. If wallet already exist, throw error. The wallet generates a seed using SecureRandom and store to db at initialization. @param [String] wallet_id new wallet id. @param [String] path_prefix wallet file path prefix. @return [Bitcoin::Wallet::Base] the wallet

# 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) click to toggle source

get current wallet

# 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() click to toggle source

get wallet dir path

# 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) click to toggle source

load wallet with specified wallet_id @return [Bitcoin::Wallet::Base] the wallet

# 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
new(wallet_id, path_prefix) click to toggle source
# File lib/bitcoin/wallet/base.rb, line 142
def initialize(wallet_id, path_prefix)
  @path = "#{path_prefix}wallet#{wallet_id}/"
  @db = Bitcoin::Wallet::DB.new(@path)
  @wallet_id = wallet_id
end
wallet_paths(path_prefix = default_path_prefix) click to toggle source

get wallets path @return [Array] Array of paths for each wallet dir.

# File lib/bitcoin/wallet/base.rb, line 43
def self.wallet_paths(path_prefix = default_path_prefix)
  Dir.glob("#{path_prefix}wallet*/").sort
end

Private Class Methods

exist?(wallet_id, path_prefix) click to toggle source
# File lib/bitcoin/wallet/base.rb, line 148
def self.exist?(wallet_id, path_prefix)
  path = "#{path_prefix}wallet#{wallet_id}"
  Dir.exist?(path)
end

Public Instance Methods

accounts(purpose = nil) click to toggle source

get account list based on BIP-44

# 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() click to toggle source

close database wallet

# File lib/bitcoin/wallet/base.rb, line 105
def close
  db.close
end
create_account(purpose = Account::PURPOSE_TYPE[:native_segwit], name) click to toggle source

create new account @param [Integer] purpose BIP44's purpose. @param [String] name a account name. @return [Bitcoin::Wallet::Account]

# 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) click to toggle source

decrypt wallet @param [String] passphrase the wallet passphrase

# File lib/bitcoin/wallet/base.rb, line 124
def decrypt(passphrase)

end
encrypt(passphrase) click to toggle source

encrypt wallet @param [String] passphrase the wallet passphrase

# 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) click to toggle source

create new bitcoin address for receiving payments. @param [String] account_name an account name. @return [String] generated address.

# 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) click to toggle source

get wallet balance. @param [Bitcoin::Wallet::Account] account a account in the wallet.

# File lib/bitcoin/wallet/base.rb, line 85
def get_balance(account)
  # TODO get from utxo db.
  0.00000000
end
master_key() click to toggle source

get master key @return [Bitcoin::Wallet::MasterKey]

# File lib/bitcoin/wallet/base.rb, line 111
def master_key
  db.master_key
end
to_h() click to toggle source

wallet information

# 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() click to toggle source

get wallet version.

# File lib/bitcoin/wallet/base.rb, line 100
def version
  db.version
end
watch_targets() click to toggle source

get data elements tobe monitored with Bloom Filter. @return [Array]

# File lib/bitcoin/wallet/base.rb, line 136
def watch_targets
  accounts.map(&:watch_targets).flatten
end

Private Instance Methods

find_account(account_name, purpose = nil) click to toggle source

find account using account_name

# File lib/bitcoin/wallet/base.rb, line 154
def find_account(account_name, purpose = nil)
  accounts(purpose).find{|a| a.name == account_name}
end