Class: BSV::Wallet::Client
- Inherits:
-
Object
- Object
- BSV::Wallet::Client
- 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.
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
-
#broadcast_queue ⇒ BroadcastQueue
readonly
The broadcast queue used to dispatch transactions.
-
#broadcaster ⇒ #broadcast?
readonly
The optional broadcaster (responds to #broadcast(tx)).
-
#key_deriver ⇒ KeyDeriver
readonly
The underlying key deriver.
-
#network ⇒ String
readonly
The network (‘mainnet’ or ‘testnet’).
-
#proof_store ⇒ ProofStore
readonly
The merkle proof persistence store.
-
#storage ⇒ Store
readonly
The underlying persistence adapter.
-
#substrate ⇒ Interface?
readonly
The optional substrate for remote wallet delegation.
Instance Method Summary collapse
-
#balance(basket: nil) ⇒ Integer
Returns the total spendable satoshis across all baskets (or a named basket).
-
#broadcast_enabled? ⇒ Boolean
Returns
truewhen broadcast is available. -
#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
constructor
A new instance of Client.
-
#set_wallet_change_params(count:, satoshis:) ⇒ Object
Configures the target UTXO pool parameters for change generation.
-
#spendable_balance(basket: nil) ⇒ Integer
Returns the total satoshis of outputs the wallet can automatically spend.
-
#sync_utxos ⇒ Object
Raises UnsupportedActionError.
-
#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.
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.
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_queue ⇒ BroadcastQueue (readonly)
Returns 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)).
45 46 47 |
# File 'lib/bsv/wallet/client.rb', line 45 def broadcaster @broadcaster end |
#key_deriver ⇒ KeyDeriver (readonly)
Returns the underlying key deriver.
33 34 35 |
# File 'lib/bsv/wallet/client.rb', line 33 def key_deriver @key_deriver end |
#network ⇒ String (readonly)
Returns the network (‘mainnet’ or ‘testnet’).
39 40 41 |
# File 'lib/bsv/wallet/client.rb', line 39 def network @network end |
#proof_store ⇒ ProofStore (readonly)
Returns the merkle proof persistence store.
42 43 44 |
# File 'lib/bsv/wallet/client.rb', line 42 def proof_store @proof_store end |
#storage ⇒ Store (readonly)
Returns the underlying persistence adapter.
36 37 38 |
# File 'lib/bsv/wallet/client.rb', line 36 def storage @storage end |
#substrate ⇒ Interface? (readonly)
Returns 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).
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.
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.
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.
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_utxos ⇒ Object
Raises UnsupportedActionError.
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.
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 |