Class: X402::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/x402/configuration.rb', line 9

def initialize
  @wallet_address = ENV.fetch("X402_WALLET_ADDRESS", nil)
  @facilitator = ENV.fetch("X402_FACILITATOR_URL", "https://www.x402.org/facilitator")
  @chain = ENV.fetch("X402_CHAIN", "base-sepolia")
  @currency = ENV.fetch("X402_CURRENCY", "USDC")
  @optimistic = ENV.fetch("X402_OPTIMISTIC", "false") == "true"
  @version = ENV.fetch("X402_VERSION", "2").to_i
  @fee_payer = nil
  @custom_chains = {}
  @custom_tokens = {}
  @accepted_payments = []
  # Coinbase CDP facilitator credentials (same env names as CDP's own SDKs).
  # Only used when the facilitator host is api.cdp.coinbase.com.
  @cdp_api_key_id = ENV.fetch("CDP_API_KEY_ID", nil)
  @cdp_api_key_secret = ENV.fetch("CDP_API_KEY_SECRET", nil)
end

Instance Attribute Details

#accepted_paymentsObject

Returns the value of attribute accepted_payments.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def accepted_payments
  @accepted_payments
end

#cdp_api_key_idObject

Returns the value of attribute cdp_api_key_id.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def cdp_api_key_id
  @cdp_api_key_id
end

#cdp_api_key_secretObject

Returns the value of attribute cdp_api_key_secret.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def cdp_api_key_secret
  @cdp_api_key_secret
end

#chainObject

Returns the value of attribute chain.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def chain
  @chain
end

#currencyObject

Returns the value of attribute currency.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def currency
  @currency
end

#custom_chainsObject

Returns the value of attribute custom_chains.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def custom_chains
  @custom_chains
end

#custom_tokensObject

Returns the value of attribute custom_tokens.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def custom_tokens
  @custom_tokens
end

#facilitatorObject

Returns the value of attribute facilitator.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def facilitator
  @facilitator
end

#fee_payerObject

Returns the value of attribute fee_payer.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def fee_payer
  @fee_payer
end

#optimisticObject

Returns the value of attribute optimistic.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def optimistic
  @optimistic
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def version
  @version
end

#wallet_addressObject

Returns the value of attribute wallet_address.



5
6
7
# File 'lib/x402/configuration.rb', line 5

def wallet_address
  @wallet_address
end

Instance Method Details

#accept(chain:, currency: "USDC", wallet_address: nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/x402/configuration.rb', line 26

def accept(chain:, currency: "USDC", wallet_address: nil)
  @accepted_payments << {
    chain: chain,
    currency: currency,
    wallet_address: wallet_address
  }
end

#chain_config(name) ⇒ Object



68
69
70
# File 'lib/x402/configuration.rb', line 68

def chain_config(name)
  @custom_chains[name]
end

#effective_accepted_paymentsObject



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

def effective_accepted_payments
  if @accepted_payments.empty?
    [{ chain: @chain, currency: @currency, wallet_address: nil }]
  else
    @accepted_payments
  end
end

#register_chain(name:, chain_id:, standard: "eip155") ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/x402/configuration.rb', line 42

def register_chain(name:, chain_id:, standard: "eip155")
  unless standard == "eip155"
    raise ConfigurationError, "Only eip155 (EVM) chains are supported for custom registration"
  end

  @custom_chains[name] = {
    chain_id: chain_id,
    standard: standard
  }
end

#register_token(chain:, symbol:, address:, decimals:, name:, version: "1") ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/x402/configuration.rb', line 53

def register_token(chain:, symbol:, address:, decimals:, name:, version: "1")
  if chain.to_s.start_with?("solana")
    raise ConfigurationError, "Custom tokens are only supported on EVM chains"
  end

  key = "#{chain}:#{symbol}"
  @custom_tokens[key] = {
    symbol: symbol,
    address: address,
    decimals: decimals,
    name: name,
    version: version
  }
end

#token_config(chain, symbol) ⇒ Object



72
73
74
75
# File 'lib/x402/configuration.rb', line 72

def token_config(chain, symbol)
  key = "#{chain}:#{symbol}"
  @custom_tokens[key]
end

#validate!Object

Raises:



77
78
79
80
81
82
83
# File 'lib/x402/configuration.rb', line 77

def validate!
  raise ConfigurationError, "wallet_address is required" if wallet_address.nil? || wallet_address.empty?
  raise ConfigurationError, "facilitator URL is required" if facilitator.nil? || facilitator.empty?
  raise ConfigurationError, "chain is required" if chain.nil? || chain.empty?
  raise ConfigurationError, "currency is required" if currency.nil? || currency.empty?
  raise ConfigurationError, "version must be 1 or 2" unless [1, 2].include?(version)
end