No description
| examples | ||
| spec | ||
| src | ||
| EXAMPLES.md | ||
| mise.toml | ||
| README.md | ||
| shard.yml | ||
| test.cr | ||
| test_implementation.cr | ||
| tvm_test.cr | ||
x402 Crystal Implementation
Core Crystal implementation of the x402 Payment Protocol. This package provides the foundational types, schemas, and utilities that power all x402 integrations in Crystal.
Installation
Add this to your application's shard.yml:
dependencies:
x402:
github: your-username/x402-crystal
version: ~> 0.1.0
Overview
The x402 Crystal package provides the core building blocks for implementing the x402 Payment Protocol in Crystal. It's designed to be used by:
- Server applications requiring payment enforcement
- Client-side payment handlers
- Custom integrations
Usage
Basic Client Usage
require "x402"
# Create payment requirements
network = X402::Types::Shared::Network::Base
price = 10.5
payment_requirements = X402::Types::Verify::PaymentRequirements.new(
"exact",
network,
price
)
# Create a signer
signer = X402::Types::Shared::EvmSigner.new
# Create payment header
payment_header = X402::Client.create_payment_header(
signer,
X402::X402_VERSION,
payment_requirements
)
Server-Side Verification and Settlement
# Verify a payment
verification_result = X402::Facilitator.verify(
client,
payment_payload,
payment_requirements
)
if verification_result.is_valid
# Settle the payment
settlement_result = X402::Facilitator.settle(
signer,
payment_payload,
payment_requirements
)
if settlement_result.success
# Process the user's request
puts "Payment settled successfully: #{settlement_result.transaction}"
end
end
Supported Networks
The implementation supports EVM, SVM, and TVM networks:
EVM Networks:
- Abstract
- Abstract Testnet
- Base
- Base Sepolia
- Avalanche
- Avalanche Fuji
- Iotex
- Sei
- Sei Testnet
- Polygon
- Polygon Amoy
- Peaq
- Story
- Educhain
- Skale Base Sepolia
SVM Networks:
- Solana
- Solana Devnet
TVM Networks:
- Everscale
Architecture
The implementation is organized into several key modules:
X402::Types: Core type definitionsX402::Client: Client-side payment creationX402::Facilitator: Server-side verification and settlementX402::Schemes: Payment scheme implementationsX402::Utils: Utility functions
Payment Schemes
Currently, the implementation supports the "exact" payment scheme:
- Exact: Requires an exact payment amount to be made to a specified address
Future versions may support additional schemes like "minimum" or "range".
API Reference
Types
X402::Types::Shared::Network: Enum for supported blockchain networksX402::Types::Verify::PaymentRequirements: Requirements for a paymentX402::Types::Verify::PaymentPayload: Signed payment dataX402::Types::Verify::VerifyResponse: Result of payment verificationX402::Types::Verify::SettleResponse: Result of payment settlement
Client Methods
X402::Client.create_payment_header: Creates a payment header for a requestX402::Client.select_payment_requirements: Selects appropriate payment requirementsX402::Client.prepare_payment_header: Prepares a payment header (without signing)X402::Client.sign_payment_header: Signs a payment header
Facilitator Methods
X402::Facilitator.verify: Verifies a payment payload against requirementsX402::Facilitator.settle: Settles a verified paymentX402::Facilitator.supported: Returns supported schemes and networks
Configuration
You can configure the x402 implementation with a config object:
config = X402::Types::X402Config.new(
svm_config: X402::Types::SvmConfig.new(rpc_url: "https://your-solana-rpc.com")
)
Contributing
- Fork it (https://github.com/your-github-user/x402-crystal/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- [Your Name] - creator and maintainer