class Tapyrus::Wallet::Account

the account in BIP-44

Constants

PURPOSE_TYPE

Attributes

account_key[R]
change_depth[RW]
index[R]
lookahead[RW]
name[R]
purpose[R]
receive_depth[RW]
wallet[RW]

Public Class Methods

new(account_key, purpose = PURPOSE_TYPE[:native_segwit], index = 0, name = '') click to toggle source
# File lib/tapyrus/wallet/account.rb, line 18
def initialize(account_key, purpose = PURPOSE_TYPE[:native_segwit], index = 0, name = '')
  validate_params!(account_key, purpose, index)
  @purpose = purpose
  @index = index
  @name = name
  @receive_depth = 0
  @change_depth = 0
  @lookahead = 10
  @account_key = account_key
end
parse_from_payload(payload) click to toggle source
# File lib/tapyrus/wallet/account.rb, line 29
def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  account_key = Tapyrus::ExtPubkey.parse_from_payload(buf.read(78))
  payload = buf.read
  name, payload = Tapyrus.unpack_var_string(payload)
  name = name.force_encoding('utf-8')
  purpose, index, receive_depth, change_depth, lookahead = payload.unpack('I*')
  a = Account.new(account_key, purpose, index, name)
  a.receive_depth = receive_depth
  a.change_depth = change_depth
  a.lookahead = lookahead
  a
end

Public Instance Methods

create_change() click to toggle source

create new change key @return [Tapyrus::ExtPubkey]

# File lib/tapyrus/wallet/account.rb, line 65
def create_change
  @change_depth += 1
  save
  save_key(1, @change_depth, derive_key(1, @change_depth))
end
create_receive() click to toggle source

create new receive key @return [Tapyrus::ExtPubkey]

# File lib/tapyrus/wallet/account.rb, line 57
def create_receive
  @receive_depth += 1
  save
  save_key(0, @receive_depth, derive_key(0, @receive_depth))
end
derived_change_keys() click to toggle source

get the list of derived keys for change key. @return [Array]

# File lib/tapyrus/wallet/account.rb, line 93
def derived_change_keys
  (change_depth + 1).times.map { |i| derive_key(1, i) }
end
derived_receive_keys() click to toggle source

get the list of derived keys for receive key. @return [Array]

# File lib/tapyrus/wallet/account.rb, line 87
def derived_receive_keys
  (receive_depth + 1).times.map { |i| derive_key(0, i) }
end
path() click to toggle source

account derivation path

# File lib/tapyrus/wallet/account.rb, line 112
def path
  "m/#{purpose}'/#{Tapyrus.chain_params.bip44_coin_type}'/#{index}'"
end
save() click to toggle source

save this account payload to database.

# File lib/tapyrus/wallet/account.rb, line 72
def save
  wallet.db.save_account(self)
  save_key(0, receive_depth, derive_key(0, receive_depth)) if receive_depth.zero?
  save_key(1, change_depth, derive_key(1, change_depth)) if change_depth.zero?
end
save_key(purpose, index, key) click to toggle source

@param purpose 0:recieve, 1:change @param index receive_depth or change_depth @param key the key to be saved

# File lib/tapyrus/wallet/account.rb, line 81
def save_key(purpose, index, key)
  wallet.db.save_key(self, purpose, index, key)
end
to_h() click to toggle source
# File lib/tapyrus/wallet/account.rb, line 126
def to_h
  {
    name: name,
    type: type,
    index: index,
    receive_depth: receive_depth,
    change_depth: change_depth,
    look_ahead: lookahead,
    receive_address: derive_key(0, receive_depth).addr,
    change_address: derive_key(1, change_depth).addr,
    account_key: account_key.to_base58,
    path: path,
    watch_only: watch_only
  }
end
to_payload() click to toggle source
# File lib/tapyrus/wallet/account.rb, line 43
def to_payload
  payload = account_key.to_payload
  payload << Tapyrus.pack_var_string(name.unpack('H*').first.htb)
  payload << [purpose, index, receive_depth, change_depth, lookahead].pack('I*')
  payload
end
type() click to toggle source

get account type label.

# File lib/tapyrus/wallet/account.rb, line 98
def type
  case purpose
  when PURPOSE_TYPE[:legacy]
    'pubkeyhash'
  when PURPOSE_TYPE[:nested_witness]
    'p2wpkh-p2sh'
  when PURPOSE_TYPE[:native_segwit]
    'p2wpkh'
  else
    'unknown'
  end
end
watch_only() click to toggle source
# File lib/tapyrus/wallet/account.rb, line 116
def watch_only
  false # TODO implements import watch only address.
end
watch_targets() click to toggle source

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

# File lib/tapyrus/wallet/account.rb, line 122
def watch_targets
  wallet.db.get_keys(self).map { |key| Tapyrus.hash160(key) }
end
witness?() click to toggle source

whether support witness

# File lib/tapyrus/wallet/account.rb, line 51
def witness?
  [PURPOSE_TYPE[:nested_witness], PURPOSE_TYPE[:native_segwit]].include?(purpose)
end

Private Instance Methods

derive_key(branch, address_index) click to toggle source
# File lib/tapyrus/wallet/account.rb, line 144
def derive_key(branch, address_index)
  account_key.derive(branch).derive(address_index)
end
validate_params!(account_key, purpose, index) click to toggle source
# File lib/tapyrus/wallet/account.rb, line 148
def validate_params!(account_key, purpose, index)
  raise 'account_key must be an instance of Tapyrus::ExtPubkey.' unless account_key.is_a?(Tapyrus::ExtPubkey)
  raise 'Account key and index does not match.' unless account_key.number == (index + Tapyrus::HARDENED_THRESHOLD)
  version_bytes = Tapyrus::ExtPubkey.version_from_purpose(purpose + Tapyrus::HARDENED_THRESHOLD)
  raise 'The purpose and the account key do not match.' unless account_key.version == version_bytes
end