Class: BSV::Wallet::Wallet

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

Overview

Holds a private key, sources UTXOs via a chain data provider, and funds and signs P2PKH transactions.

The provider is duck-typed — any object responding to #fetch_utxos(address) and #fetch_transaction(txid) qualifies.

Constant Summary collapse

DUST_THRESHOLD =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key:, provider:) ⇒ Wallet

Returns a new instance of Wallet.



15
16
17
18
# File 'lib/bsv/wallet/wallet.rb', line 15

def initialize(private_key:, provider:)
  @private_key = private_key
  @provider = provider
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



13
14
15
# File 'lib/bsv/wallet/wallet.rb', line 13

def private_key
  @private_key
end

#providerObject (readonly)

Returns the value of attribute provider.



13
14
15
# File 'lib/bsv/wallet/wallet.rb', line 13

def provider
  @provider
end

Instance Method Details

#address(network: :mainnet) ⇒ Object



20
21
22
# File 'lib/bsv/wallet/wallet.rb', line 20

def address(network: :mainnet)
  @private_key.public_key.address(network: network)
end

#balance(network: :mainnet) ⇒ Object



24
25
26
# File 'lib/bsv/wallet/wallet.rb', line 24

def balance(network: :mainnet)
  @provider.fetch_utxos(address(network: network)).sum(&:satoshis)
end

#fund(tx, network: :mainnet, satoshis_per_byte: 0.1) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bsv/wallet/wallet.rb', line 28

def fund(tx, network: :mainnet, satoshis_per_byte: 0.1)
  utxos = @provider.fetch_utxos(address(network: network))
  output_total = tx.total_output_satoshis

  # Add a dummy change output so fee estimation accounts for its size
  dummy_change = BSV::Transaction::TransactionOutput.new(
    satoshis: 0, locking_script: locking_script
  )
  tx.add_output(dummy_change)

  input_total = tx.total_input_satoshis
  funded = false

  utxos.each do |utxo|
    tx.add_input(build_input(utxo))
    input_total += utxo.satoshis

    fee = tx.estimated_fee(satoshis_per_byte: satoshis_per_byte)
    if input_total >= output_total + fee
      funded = true
      break
    end
  end

  # Remove the dummy change output
  tx.outputs.delete(dummy_change)

  unless funded
    fee = tx.estimated_fee(satoshis_per_byte: satoshis_per_byte)
    raise InsufficientFundsError.new(required: output_total + fee, available: input_total)
  end

  add_change_if_needed(tx, input_total, output_total, satoshis_per_byte)

  tx
end

#fund_and_sign(tx, network: :mainnet, satoshis_per_byte: 0.1) ⇒ Object



69
70
71
72
# File 'lib/bsv/wallet/wallet.rb', line 69

def fund_and_sign(tx, network: :mainnet, satoshis_per_byte: 0.1)
  fund(tx, network: network, satoshis_per_byte: satoshis_per_byte)
  sign(tx)
end

#sign(tx) ⇒ Object



65
66
67
# File 'lib/bsv/wallet/wallet.rb', line 65

def sign(tx)
  tx.sign_all(@private_key)
end