Class: X402::Payments::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = X402::Payments.configuration) ⇒ Generator

Returns a new instance of Generator.



14
15
16
# File 'lib/x402/payments/generator.rb', line 14

def initialize(config = X402::Payments.configuration)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/x402/payments/generator.rb', line 12

def config
  @config
end

Instance Method Details

#generate_header(amount:, resource:, description: nil, network: nil, private_key: nil, pay_to: nil, extra: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/x402/payments/generator.rb', line 18

def generate_header(amount:, resource:, description: nil, network: nil, private_key: nil, pay_to: nil, extra: nil)
  config.validate!

  chain_name = network || config.chain
  key = private_key || config.private_key
  recipient = pay_to || config.default_pay_to

  chain_config = X402::Payments.chain_config(chain_name)
  currency_config = X402::Payments.currency_config_for_chain(chain_name)

  # Convert amount to atomic units
  atomic_amount = convert_to_atomic(amount, currency_config[:decimals])

  # Get the Ethereum account from private key
   = Eth::Key.new(priv: key)
  sender_address = .address.to_s

  # Create nonce (32 random bytes)
  nonce = SecureRandom.random_bytes(32)

  # Build payment requirements
  payment_requirements = {
    scheme: "exact",
    network: chain_name,
    max_amount_required: atomic_amount.to_s,
    asset: chain_config[:usdc_address],
    pay_to: recipient,
    resource: resource,
    description: description || "Payment required for #{resource}",
    max_timeout_seconds: config.max_timeout_seconds,
    mime_type: "application/json",
    extra: extra || {
      name: currency_config[:name],
      version: currency_config[:version]
    }
  }

  # Prepare unsigned payment header
  valid_after = (Time.now.to_i - 60).to_s
  valid_before = (Time.now.to_i + config.max_timeout_seconds).to_s

  header = {
    x402Version: 1,
    scheme: "exact",
    network: chain_name,
    payload: {
      signature: nil,
      authorization: {
        from: sender_address,
        to: recipient,
        value: atomic_amount.to_s,
        validAfter: valid_after,
        validBefore: valid_before,
        nonce: "0x#{nonce.unpack1('H*')}"
      }
    }
  }

  # Sign the payment header
  signature = sign_payment(, header, payment_requirements, nonce)
  header[:payload][:signature] = signature

  # Encode to base64
  encode_payment(header)
end


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/x402/payments/generator.rb', line 84

def generate_link(amount:, resource:, description: nil, network: nil, private_key: nil, pay_to: nil, extra: nil)
  header = generate_header(
    amount: amount,
    resource: resource,
    description: description,
    network: network,
    private_key: private_key,
    pay_to: pay_to,
    extra: extra
  )

  {
    payment_header: header,
    curl_command: "curl -s -H \"X-PAYMENT: #{header}\" #{resource} | jq ."
  }
end