Module: X402::Wallet

Defined in:
lib/x402/wallet.rb

Overview

Helpers for loading and persisting the server wallet key.

The gem uses +BSV::Wallet::WalletClient+ as the concrete BRC-100 wallet. This module provides a loader that resolves the signing WIF from either the +SERVER_WIF+ environment variable or an on-disk +wallet.key+ file, and constructs a +WalletClient+ with a +FileStore+ pointed at the same directory.

See +lib/tasks/x402.rake+ for the interactive +rake x402:wallet:setup+ task which creates or restores the on-disk +wallet.key+.

Constant Summary collapse

DEFAULT_DIR =
File.expand_path("~/.bsv-wallet")
KEY_FILENAME =
"wallet.key"

Class Method Summary collapse

Class Method Details

.key_path(dir: nil) ⇒ String

Returns the canonical path to the on-disk wallet key file for the given directory.

Parameters:

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

    optional override

Returns:

  • (String)

    absolute path



47
48
49
# File 'lib/x402/wallet.rb', line 47

def self.key_path(dir: nil)
  File.join(dir || ENV.fetch("BSV_WALLET_DIR", DEFAULT_DIR), KEY_FILENAME)
end

.load(dir: nil) ⇒ BSV::Wallet::WalletClient

Resolve the signing WIF and construct a +BSV::Wallet::WalletClient+.

Resolution order (locked per HLR #104):

  1. +SERVER_WIF+ environment variable (wins if set)
  2. +/wallet.key+ file (default: +~/.bsv-wallet/wallet.key+, or +BSV_WALLET_DIR+ if set)
  3. Raises +ConfigurationError+ with a hint to run the setup task

Parameters:

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

    override the wallet directory (rarely needed — prefer +BSV_WALLET_DIR+ env var for process-wide config)

Returns:

  • (BSV::Wallet::WalletClient)

Raises:



32
33
34
35
36
37
38
39
40
# File 'lib/x402/wallet.rb', line 32

def self.load(dir: nil)
  require "bsv-wallet"

  resolved_dir = dir || ENV.fetch("BSV_WALLET_DIR", DEFAULT_DIR)
  wif = resolve_wif(resolved_dir)
  key = ::BSV::Primitives::PrivateKey.from_wif(wif)
  storage = ::BSV::Wallet::FileStore.new(dir: resolved_dir)
  ::BSV::Wallet::WalletClient.new(key, storage: storage)
end