Class: X402::Tasks::WalletSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/x402/tasks/wallet_setup.rb

Overview

Interactive wallet setup task — backing implementation for +rake x402:wallet:setup+.

Behaviour:

  • Checks BSV_WALLET_DIR/wallet.key (default ~/.bsv-wallet/wallet.key)
  • If exists: prints identity key, reports dir, exits cleanly. Never overwrites. Set FORCE=1 to replace an existing wallet.
  • If absent: offers [1] create new / [2] restore from WIF / [3] cancel
  • New wallets get a random PrivateKey. WIF written with mode 0600.
  • Parent directory created with mode 0700.

Designed for dependency injection in tests: pass custom +stdin+, +stdout+, +dir+, and +random_wif+ to the constructor.

Constant Summary collapse

FORCE_ENV_VAR =
"FORCE"

Instance Method Summary collapse

Constructor Details

#initialize(stdin: $stdin, stdout: $stdout, dir: nil, random_wif: nil) ⇒ WalletSetup

Returns a new instance of WalletSetup.

Parameters:

  • stdin (IO) (defaults to: $stdin)

    input stream (default +$stdin+)

  • stdout (IO) (defaults to: $stdout)

    output stream (default +$stdout+)

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

    wallet directory override (default +BSV_WALLET_DIR+ env or +~/.bsv-wallet+)

  • random_wif (#call, nil) (defaults to: nil)

    lambda returning a WIF string. Injectable for tests. Defaults to +BSV::Primitives::PrivateKey.generate.to_wif+.



33
34
35
36
37
38
39
# File 'lib/x402/tasks/wallet_setup.rb', line 33

def initialize(stdin: $stdin, stdout: $stdout, dir: nil, random_wif: nil)
  require "bsv-sdk"
  @stdin = stdin
  @stdout = stdout
  @dir = dir || ENV.fetch("BSV_WALLET_DIR", X402::Wallet::DEFAULT_DIR)
  @random_wif = random_wif || -> { ::BSV::Primitives::PrivateKey.generate.to_wif }
end

Instance Method Details

#runObject



41
42
43
44
45
46
47
48
# File 'lib/x402/tasks/wallet_setup.rb', line 41

def run
  ensure_dir!
  if File.exist?(key_path)
    handle_existing
  else
    handle_fresh
  end
end