Class: X402::Payments::Generator
- Inherits:
-
Object
- Object
- X402::Payments::Generator
- Defined in:
- lib/x402/payments/generator.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #generate_header(amount:, resource:, description: nil, network: nil, private_key: nil, pay_to: nil, extra: nil, version: nil) ⇒ Object
- #generate_header_for(payment_required, private_key: nil) ⇒ Object
- #generate_link(amount:, resource:, description: nil, network: nil, private_key: nil, pay_to: nil, extra: nil, version: nil) ⇒ Object
-
#initialize(config = X402::Payments.configuration) ⇒ Generator
constructor
A new instance of Generator.
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
#config ⇒ Object (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, version: 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 |
# 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, version: nil) config.validate! protocol_version = X402::Payments.normalize_version(version) || config.protocol_version chain_name = network || config.chain key = private_key || config.private_key recipient = pay_to || config.default_pay_to token_config = X402::Payments.token_config_for(chain_name) asset_address = X402::Payments.asset_address_for(chain_name) atomic_amount = convert_to_atomic(amount, token_config[:decimals]) account = Eth::Key.new(priv: key) sender_address = account.address.to_s nonce_bytes = SecureRandom.random_bytes(32) nonce_hex = "0x#{nonce_bytes.unpack1('H*')}" valid_after = (Time.now.to_i - 60).to_s valid_before = (Time.now.to_i + config.max_timeout_seconds).to_s = { from: sender_address, to: recipient, value: atomic_amount.to_s, valid_after: valid_after, valid_before: valid_before, nonce: nonce_hex } extra_data = extra || { name: token_config[:name], version: token_config[:version] } signature = ( account: account, authorization: , chain_name: chain_name, asset: asset_address, extra: extra_data, nonce_bytes: nonce_bytes ) payload = build_payload( version: protocol_version, authorization: , signature: signature, resource: resource, description: description, chain_name: chain_name, asset_address: asset_address, atomic_amount: atomic_amount, recipient: recipient, extra: extra_data ) encode_payment(payload) end |
#generate_header_for(payment_required, private_key: nil) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/x402/payments/generator.rb', line 79 def generate_header_for(payment_required, private_key: nil) key = private_key || config.private_key raise ConfigurationError, "private_key is required" if key.nil? || key.empty? version = X402::Payments.normalize_version(payment_required[:version]) || 1 accepts = payment_required[:accepts]&.first raise Error, "No payment requirements found" unless accepts resource_info = payment_required[:resource] || {} chain_name = resolve_chain_name(accepts[:network]) token_config = X402::Payments.token_config_for(chain_name) account = Eth::Key.new(priv: key) sender_address = account.address.to_s nonce_bytes = SecureRandom.random_bytes(32) nonce_hex = "0x#{nonce_bytes.unpack1('H*')}" valid_after = (Time.now.to_i - 60).to_s valid_before = (Time.now.to_i + (accepts[:max_timeout_seconds] || config.max_timeout_seconds)).to_s = { from: sender_address, to: accepts[:pay_to], value: accepts[:amount], valid_after: valid_after, valid_before: valid_before, nonce: nonce_hex } extra_data = accepts[:extra] || {} extra_data = { name: extra_data[:name] || extra_data["name"] || token_config[:name], version: extra_data[:version] || extra_data["version"] || token_config[:version] } signature = ( account: account, authorization: , chain_name: chain_name, asset: accepts[:asset], extra: extra_data, nonce_bytes: nonce_bytes ) payload = case version when 2 V2::PayloadBuilder.build( authorization: , signature: signature, resource: { url: resource_info[:url], description: resource_info[:description], mime_type: resource_info[:mime_type] }, accepted: { scheme: accepts[:scheme], network: accepts[:network], amount: accepts[:amount], asset: accepts[:asset], pay_to: accepts[:pay_to], max_timeout_seconds: accepts[:max_timeout_seconds], extra: extra_data }, extensions: payment_required[:extensions] || {} ) else V1::PayloadBuilder.build( authorization: , signature: signature, scheme: accepts[:scheme], network: chain_name ) end encode_payment(payload) end |
#generate_link(amount:, resource:, description: nil, network: nil, private_key: nil, pay_to: nil, extra: nil, version: nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/x402/payments/generator.rb', line 157 def generate_link(amount:, resource:, description: nil, network: nil, private_key: nil, pay_to: nil, extra: nil, version: nil) protocol_version = X402::Payments.normalize_version(version) || config.protocol_version header = generate_header( amount: amount, resource: resource, description: description, network: network, private_key: private_key, pay_to: pay_to, extra: extra, version: protocol_version ) header_name = X402::Payments.payment_header_name(protocol_version) { payment_header: header, header_name: header_name, curl_command: "curl -s -H \"#{header_name}: #{header}\" #{resource} | jq ." } end |