Class: Bitcoin::Wallet::Base

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

Constant Summary collapse

DEFAULT_PATH_PREFIX =
"#{Bitcoin.base_dir}/db/wallet/"
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.



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

def db
  @db
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#wallet_idObject

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.

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)


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.('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

Returns:

Raises:

  • (ArgumentError)


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

Returns:

  • (Array)

    Array of paths for each wallet dir.



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

#closeObject

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

Parameters:

  • purpose (Integer) (defaults to: )

    BIP44’s purpose.

  • name (String)

    a account name.

Returns:

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
# File 'lib/bitcoin/wallet/base.rb', line 67

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

#decrypt(passphrase) ⇒ Object

decrypt wallet

Parameters:

  • passphrase (String)

    the wallet passphrase



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

def decrypt(passphrase)

end

#encrypt(passphrase) ⇒ Object

encrypt wallet

Parameters:

  • passphrase (String)

    the wallet passphrase



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.

Parameters:

  • account_name (String)

    an account name.

Returns:

  • (String)

    generated address.

Raises:

  • (ArgumentError)


88
89
90
91
92
# File 'lib/bitcoin/wallet/base.rb', line 88

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

#get_balance(account) ⇒ Object

get wallet balance.

Parameters:



80
81
82
83
# File 'lib/bitcoin/wallet/base.rb', line 80

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

#master_keyBitcoin::Wallet::MasterKey

get master key



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

def master_key
  db.master_key
end

#to_hObject

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

#versionObject

get wallet version.



95
96
97
# File 'lib/bitcoin/wallet/base.rb', line 95

def version
  db.version
end

#watch_targetsArray[String]

get data elements tobe monitored with Bloom Filter.

Returns:



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

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