Class: BSV::Wallet::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Crypto, Identity, Network, Transaction, Interface::BRC100
Defined in:
lib/bsv/wallet/client.rb,
lib/bsv/wallet/client/brc100/crypto.rb,
lib/bsv/wallet/client/brc100/network.rb,
lib/bsv/wallet/client/brc100/identity.rb,
lib/bsv/wallet/client/brc100/transaction.rb,
lib/bsv/wallet/client/brc100/authentication.rb

Overview

BRC-100 wallet implementation.

All 28 BRC-100 methods are implemented directly — the 9 crypto operations are inlined here, with substrate delegation wired at the top of each method. No inheritance; behaviour is fully self-contained.

Examples:

Create a simple transaction

client = BSV::Wallet::Client.new(private_key)
result = client.create_action({
  description: 'Pay invoice',
  outputs: [{ locking_script: '76a914...88ac', satoshis: 1000,
              output_description: 'Payment' }]
})

Defined Under Namespace

Modules: Authentication, Crypto, Identity, Network, Transaction

Constant Summary

Constants included from Transaction

Transaction::ANCESTOR_DEPTH_CAP, Transaction::STALE_CHECK_INTERVAL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transaction

#abort_action, #create_action, #internalize_action, #list_actions, #list_outputs, #relinquish_output, #sign_action

Methods included from Network

#get_header_for_height, #get_height, #get_network, #get_version

Methods included from Identity

#acquire_certificate, #discover_by_attributes, #discover_by_identity_key, #list_certificates, #prove_certificate, #relinquish_certificate

Methods included from Crypto

#create_hmac, #create_signature, #decrypt, #encrypt, #get_public_key, #reveal_counterparty_key_linkage, #reveal_specific_key_linkage, #verify_hmac, #verify_signature

Methods included from Authentication

#is_authenticated, #wait_for_authentication

Methods included from Interface::BRC100

#abort_action, #acquire_certificate, #create_action, #create_hmac, #create_signature, #decrypt, #discover_by_attributes, #discover_by_identity_key, #encrypt, #get_header_for_height, #get_height, #get_network, #get_public_key, #get_version, #internalize_action, #is_authenticated, #list_actions, #list_certificates, #list_outputs, #prove_certificate, #relinquish_certificate, #relinquish_output, #reveal_counterparty_key_linkage, #reveal_specific_key_linkage, #sign_action, #verify_hmac, #verify_signature, #wait_for_authentication

Constructor Details

#initialize(key, storage: Store::File.new, network: 'mainnet', proof_store: nil, http_client: nil, fee_estimator: nil, coin_selector: nil, change_generator: nil, broadcaster: nil, broadcast_queue: nil, substrate: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • key (BSV::Primitives::PrivateKey, String, KeyDeriver)

    signing key

  • storage (Store) (defaults to: Store::File.new)

    persistence adapter (default: Store::File.new). Use storage: Store::Memory.new for tests.

  • network (String) (defaults to: 'mainnet')

    ‘mainnet’ (default) or ‘testnet’

  • proof_store (ProofStore, nil) (defaults to: nil)

    merkle proof store (default: LocalProofStore backed by storage)

  • http_client (#request, nil) (defaults to: nil)

    injectable HTTP client for certificate issuance

  • fee_estimator (FeeEstimator, nil) (defaults to: nil)

    optional fee estimator

  • coin_selector (CoinSelector, nil) (defaults to: nil)

    optional coin selector

  • change_generator (ChangeGenerator, nil) (defaults to: nil)

    optional change generator

  • broadcaster (#broadcast, nil) (defaults to: nil)

    optional broadcaster

  • broadcast_queue (BroadcastQueue, nil) (defaults to: nil)

    optional broadcast queue; defaults to BroadcastQueue::Inline

  • substrate (Interface, nil) (defaults to: nil)

    optional remote wallet substrate



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bsv/wallet/client.rb', line 65

def initialize(
  key,
  storage: Store::File.new,
  network: 'mainnet',
  proof_store: nil,
  http_client: nil,
  fee_estimator: nil,
  coin_selector: nil,
  change_generator: nil,
  broadcaster: nil,
  broadcast_queue: nil,
  substrate: nil
)
  @key_deriver = key.is_a?(KeyDeriver) ? key : KeyDeriver.new(key)
  @substrate = substrate
  @storage = storage
  @network = network
  @proof_store = proof_store || LocalProofStore.new(storage)
  @http_client = http_client
  @broadcaster = broadcaster
  @pending = {}
  @pending_by_txid = {}
  @injected_fee_estimator    = fee_estimator
  @injected_coin_selector    = coin_selector
  @injected_change_generator = change_generator
  @broadcast_queue = broadcast_queue || BroadcastQueue::Inline.new(
    storage: @storage,
    broadcaster: @broadcaster
  )
end

Instance Attribute Details

#broadcast_queueBroadcastQueue (readonly)

Returns the broadcast queue used to dispatch transactions.

Returns:

  • (BroadcastQueue)

    the broadcast queue used to dispatch transactions



48
49
50
# File 'lib/bsv/wallet/client.rb', line 48

def broadcast_queue
  @broadcast_queue
end

#broadcaster#broadcast? (readonly)

Returns the optional broadcaster (responds to #broadcast(tx)).

Returns:

  • (#broadcast, nil)

    the optional broadcaster (responds to #broadcast(tx))



45
46
47
# File 'lib/bsv/wallet/client.rb', line 45

def broadcaster
  @broadcaster
end

#key_deriverKeyDeriver (readonly)

Returns the underlying key deriver.

Returns:



33
34
35
# File 'lib/bsv/wallet/client.rb', line 33

def key_deriver
  @key_deriver
end

#networkString (readonly)

Returns the network (‘mainnet’ or ‘testnet’).

Returns:

  • (String)

    the network (‘mainnet’ or ‘testnet’)



39
40
41
# File 'lib/bsv/wallet/client.rb', line 39

def network
  @network
end

#proof_storeProofStore (readonly)

Returns the merkle proof persistence store.

Returns:

  • (ProofStore)

    the merkle proof persistence store



42
43
44
# File 'lib/bsv/wallet/client.rb', line 42

def proof_store
  @proof_store
end

#storageStore (readonly)

Returns the underlying persistence adapter.

Returns:

  • (Store)

    the underlying persistence adapter



36
37
38
# File 'lib/bsv/wallet/client.rb', line 36

def storage
  @storage
end

#substrateInterface? (readonly)

Returns the optional substrate for remote wallet delegation.

Returns:

  • (Interface, nil)

    the optional substrate for remote wallet delegation



51
52
53
# File 'lib/bsv/wallet/client.rb', line 51

def substrate
  @substrate
end

Instance Method Details

#balance(basket: nil) ⇒ Integer

Returns the total spendable satoshis across all baskets (or a named basket).

Parameters:

  • basket (String, nil) (defaults to: nil)

    the basket to total, or nil for all baskets

Returns:

  • (Integer)

    sum of all spendable output values



112
113
114
# File 'lib/bsv/wallet/client.rb', line 112

def balance(basket: nil)
  @storage.find_spendable_outputs(basket: basket).sum { |o| o[:satoshis].to_i }
end

#broadcast_enabled?Boolean

Returns true when broadcast is available.

Returns:

  • (Boolean)


97
98
99
# File 'lib/bsv/wallet/client.rb', line 97

def broadcast_enabled?
  @broadcast_queue.broadcast_enabled?
end

#set_wallet_change_params(count:, satoshis:) ⇒ Object

Configures the target UTXO pool parameters for change generation.

Parameters:

  • count (Integer)

    desired number of spendable UTXOs in ‘default’ basket

  • satoshis (Integer)

    desired average value per UTXO in satoshis

Raises:



130
131
132
133
134
135
# File 'lib/bsv/wallet/client.rb', line 130

def set_wallet_change_params(count:, satoshis:)
  raise InvalidParameterError.new('count', 'a positive Integer') unless count.is_a?(Integer) && count.positive?
  raise InvalidParameterError.new('satoshis', 'a positive Integer') unless satoshis.is_a?(Integer) && satoshis.positive?

  @storage.store_setting('change_params', { count: count, satoshis: satoshis })
end

#spendable_balance(basket: nil) ⇒ Integer

Returns the total satoshis of outputs the wallet can automatically spend.

Parameters:

  • basket (String, nil) (defaults to: nil)

    restrict to a named basket, or nil for all

Returns:

  • (Integer)

    total auto-spendable satoshis



120
121
122
123
124
# File 'lib/bsv/wallet/client.rb', line 120

def spendable_balance(basket: nil)
  @storage.find_spendable_outputs(basket: basket)
          .select { |o| (o[:derivation_prefix] && o[:derivation_suffix] && o[:sender_identity_key]) || o[:derivation_type]&.to_s == 'identity' }
          .sum { |o| o[:satoshis].to_i }
end

#sync_utxosObject



102
103
104
# File 'lib/bsv/wallet/client.rb', line 102

def sync_utxos
  raise UnsupportedActionError, 'sync_utxos requires a remote substrate or custom integration'
end

#utxo_pool(name:, target_count: 20, target_satoshis: 10_000, low_water_mark: 0.5) ⇒ LocalPool

Creates a UTXO pool for high-frequency transaction pre-allocation.

Parameters:

  • name (String)

    pool identifier (basket will be “pool:<name>”)

  • target_count (Integer) (defaults to: 20)

    desired number of UTXOs (default 20)

  • target_satoshis (Integer) (defaults to: 10_000)

    desired satoshis per UTXO (default 10_000)

  • low_water_mark (Float) (defaults to: 0.5)

    replenishment trigger fraction (default 0.5)

Returns:

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/bsv/wallet/client.rb', line 144

def utxo_pool(name:, target_count: 20, target_satoshis: 10_000, low_water_mark: 0.5)
  basket = "pool:#{name}"
  Validators.validate_basket!(basket)
  raise WalletError, 'utxo_pool requires a broadcaster for replenishment' unless broadcast_enabled?

  threshold = (target_count * low_water_mark).ceil
  pool = LocalPool.new(
    name: name,
    storage: @storage,
    wallet_client: self,
    target_count: target_count,
    target_satoshis: target_satoshis,
    low_water_mark: threshold
  )
  worker = ReplenishmentWorker.new(
    pool: pool,
    wallet_client: self
  )
  pool.replenisher = worker
  worker.start
  pool
end