Skip to main content

Crate khodpay_signing

Crate khodpay_signing 

Source
Expand description

§Khodpay Signing

EVM transaction signing library for BSC (BNB Smart Chain) and other EVM-compatible chains.

This crate provides EIP-1559 transaction signing, EIP-712 typed data signing, and ERC-4337 (Account Abstraction) PackedUserOperation support — all integrating with khodpay-bip44 for HD wallet key derivation.

§Modules

ModuleStandardDescription
(root)EIP-1559Type-2 transaction building and signing
eip712EIP-712Generic typed structured data signing
erc4337ERC-4337 v0.7PackedUserOperation build / hash / sign

§Features

  • EIP-1559 Transactions: Modern fee market transactions for EOA wallets
  • EIP-712 Typed Data: Generic, protocol-agnostic structured data signing
  • ERC-4337 Account Abstraction: PackedUserOperation v0.7 for gasless smart wallets
  • BSC Support: Native support for BNB Smart Chain (mainnet/testnet)
  • BIP-44 Integration: Seamless key derivation from HD wallets
  • BEP-20 Helpers: Token transfer encoding utilities

§Quick Start — EIP-1559 (EOA Wallet)

use khodpay_signing::{Eip1559Transaction, ChainId, Wei, Bip44Signer};
use khodpay_bip44::{Wallet, Purpose, CoinType};
use khodpay_bip32::Network;

let mut wallet = Wallet::from_english_mnemonic(mnemonic, "", Network::BitcoinMainnet)?;
let account = wallet.get_account(Purpose::BIP44, CoinType::Ethereum, 0)?;
let signer = Bip44Signer::new(account, 0)?;

let tx = Eip1559Transaction::builder()
    .chain_id(ChainId::BscMainnet)
    .nonce(0)
    .to("0x742d35Cc6634C0532925a3b844Bc454e4438f44e".parse()?)
    .value(Wei::from_ether(1))
    .gas_limit(21_000)
    .max_fee_per_gas(Wei::from_gwei(5))
    .max_priority_fee_per_gas(Wei::from_gwei(1))
    .build()?;

let signature = signer.sign_transaction(&tx)?;
let signed_tx = khodpay_signing::SignedTransaction::new(tx, signature);
let raw_tx = signed_tx.to_raw_transaction(); // "0x02..."

§Quick Start — EIP-712 Typed Data

use khodpay_signing::eip712::{
    Eip712Domain, Eip712Type, encode_address, encode_uint64, sign_typed_data,
};

struct PaymentIntent { business: Address, amount: u64, nonce: u64 }

impl Eip712Type for PaymentIntent {
    fn type_string() -> &'static str {
        "PaymentIntent(address business,uint64 amount,uint64 nonce)"
    }
    fn encode_data(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        buf.extend_from_slice(&encode_address(&self.business));
        buf.extend_from_slice(&encode_uint64(self.amount));
        buf.extend_from_slice(&encode_uint64(self.nonce));
        buf
    }
}

let domain = Eip712Domain::new("MyApp", "1", 56, gateway_address);
let sig = sign_typed_data(&signer, &domain, &intent)?;

§Quick Start — ERC-4337 Smart Wallet

use khodpay_signing::erc4337::{PackedUserOperation, sign_user_operation, ENTRY_POINT_V07};

let user_op = PackedUserOperation::builder()
    .sender(smart_account_address)
    .nonce(0)
    .call_data(encoded_calldata)
    .account_gas_limits(150_000, 300_000)
    .pre_verification_gas(50_000)
    .gas_fees(1_000_000_000, 5_000_000_000)
    .paymaster(paymaster_address, vec![])
    .build()?;

let entry_point: Address = ENTRY_POINT_V07.parse().unwrap();
let sig = sign_user_operation(&signer, &user_op, entry_point, 56)?;
let mut signed_op = user_op;
signed_op.signature = sig.to_bytes().to_vec();

Modules§

eip712
Generic EIP-712 typed data signing.
erc4337
Generic ERC-4337 (Account Abstraction) UserOperation support.

Structs§

AccessListItem
An access list item specifying an address and its storage keys.
Address
A 20-byte EVM address.
Bip44Signer
A transaction signer using BIP-44 derived keys.
Eip1559Transaction
EIP-1559 (Type 2) transaction.
Eip1559TransactionBuilder
Builder for constructing EIP-1559 transactions.
Signature
An ECDSA signature with recovery ID.
SignedTransaction
A signed EIP-1559 transaction ready for broadcast.
Wei
A value in wei (smallest EVM currency unit).

Enums§

ChainId
EVM chain identifier for transaction replay protection.
Error
Errors that can occur during transaction signing operations.

Constants§

ETHER
The number of wei in one ether/BNB (10^18).
GWEI
The number of wei in one gwei (10^9).
TOKEN_TRANSFER_GAS
Typical gas limit for a BEP-20/ERC-20 token transfer.
TRANSFER_GAS
Gas limit for a standard ETH/BNB transfer.

Functions§

recover_signer
Recovers the signer’s address from a signature and message hash.

Type Aliases§

AccessList
A list of access list items.
Result
Result type alias for signing operations.