Class: Mpp::Methods::Evm::EvmMethod
- Inherits:
-
Object
- Object
- Mpp::Methods::Evm::EvmMethod
- Defined in:
- lib/mpp/methods/evm/evm_method.rb
Overview
EVM payment method. Speaks native Payment-auth and x402 exact.
Instance Attribute Summary collapse
-
#authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
#chain_id ⇒ Object
readonly
Returns the value of attribute chain_id.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#decimals ⇒ Object
readonly
Returns the value of attribute decimals.
-
#intents ⇒ Object
Returns the value of attribute intents.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#recipient ⇒ Object
readonly
Returns the value of attribute recipient.
-
#x402 ⇒ Object
readonly
Returns the value of attribute x402.
Instance Method Summary collapse
- #bind_x402_credential(payment_signature, challenge:, request:, url:, body:, http_method: nil) ⇒ Object
- #decorate_challenge(headers, challenge, url: nil, http_method: nil, request: nil) ⇒ Object
- #decorate_receipt(headers, receipt, credential, payment_signature: nil) ⇒ Object
-
#initialize(currency:, recipient:, decimals:, chain_id:, authorization:, x402:) ⇒ EvmMethod
constructor
A new instance of EvmMethod.
- #payment_requirements(request) ⇒ Object
- #transform_request(request, _credential) ⇒ Object
- #x402_matches?(payload, request) ⇒ Boolean
Constructor Details
#initialize(currency:, recipient:, decimals:, chain_id:, authorization:, x402:) ⇒ EvmMethod
Returns a new instance of EvmMethod.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 12 def initialize(currency:, recipient:, decimals:, chain_id:, authorization:, x402:) @name = "evm" @currency = Authorization.checksum_address(currency) @recipient = Authorization.checksum_address(recipient) @decimals = decimals @chain_id = chain_id @authorization = @x402 = x402 @intents = {} end |
Instance Attribute Details
#authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
9 10 11 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 9 def @authorization end |
#chain_id ⇒ Object (readonly)
Returns the value of attribute chain_id.
9 10 11 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 9 def chain_id @chain_id end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
9 10 11 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 9 def currency @currency end |
#decimals ⇒ Object (readonly)
Returns the value of attribute decimals.
9 10 11 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 9 def decimals @decimals end |
#intents ⇒ Object
Returns the value of attribute intents.
10 11 12 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 10 def intents @intents end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 9 def name @name end |
#recipient ⇒ Object (readonly)
Returns the value of attribute recipient.
9 10 11 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 9 def recipient @recipient end |
#x402 ⇒ Object (readonly)
Returns the value of attribute x402.
9 10 11 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 9 def x402 @x402 end |
Instance Method Details
#bind_x402_credential(payment_signature, challenge:, request:, url:, body:, http_method: nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 73 def bind_x402_credential(payment_signature, challenge:, request:, url:, body:, http_method: nil) payment_payload = Mpp::X402::Header.decode_payment_signature(payment_signature) requirements = payment_requirements(request) = Mpp::X402::Server.bind_credential( payment_payload: payment_payload, requirements: requirements, resource_url: url, challenge: challenge, body: body, route_binding: @x402[:route_binding], http_method: http_method ) echo = challenge.to_echo Mpp::Credential.new( challenge: echo, payload: .merge("_x402" => true), source: source_for(["from"]) ) end |
#decorate_challenge(headers, challenge, url: nil, http_method: nil, request: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 43 def decorate_challenge(headers, challenge, url: nil, http_method: nil, request: nil) return headers if url.nil? || url.empty? requirements = payment_requirements(request || challenge.request) extensions = Mpp::X402::Server.route_extensions(challenge, http_method) body = Mpp::X402::Server.payment_required_body( requirements: requirements, resource_url: url, extensions: extensions, error: "#{Mpp::X402::PAYMENT_SIGNATURE_HEADER} header is required" ) headers["PAYMENT-REQUIRED"] = Mpp::X402::Header.encode_payment_required(body) headers end |
#decorate_receipt(headers, receipt, credential, payment_signature: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 58 def decorate_receipt(headers, receipt, credential, payment_signature: nil) return headers if payment_signature.nil? || payment_signature.empty? payload = credential.payload request = decode_request(credential) chain_id = request.dig("methodDetails", "chainId") || @chain_id headers["PAYMENT-RESPONSE"] = Mpp::X402::Header.encode_payment_response({ "network" => "eip155:#{chain_id}", "payer" => payload["from"], "success" => true, "transaction" => receipt.reference }) headers end |
#payment_requirements(request) ⇒ Object
32 33 34 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 32 def payment_requirements(request) charge_intent.payment_requirements(request) end |
#transform_request(request, _credential) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 23 def transform_request(request, _credential) method_details = request.fetch("methodDetails", {}) method_details = {} unless method_details.is_a?(Hash) method_details["chainId"] = @chain_id method_details["credentialTypes"] = ["authorization"] request.merge("methodDetails" => method_details, "currency" => @currency, "recipient" => @recipient) end |
#x402_matches?(payload, request) ⇒ Boolean
36 37 38 39 40 41 |
# File 'lib/mpp/methods/evm/evm_method.rb', line 36 def x402_matches?(payload, request) accepted = payload["accepted"] return false unless accepted.is_a?(Hash) Mpp::X402::Server.canonical_equal?(accepted, payment_requirements(request)) end |