@bit-gpt/h402
Version:
BitGPT's 402 open protocol for blockchain-native payments
77 lines • 3.23 kB
JavaScript
/**
* Export all facilitator implementations
* This file serves as a central export point for all blockchain-specific facilitators
* and provides a unified interface for easier use
*/
// Import blockchain-specific facilitators
import * as evmFacilitator from "./evm/facilitator.js";
import * as solanaFacilitator from "./solana/facilitator.js";
// Re-export the specific implementations
export * as evm from "./evm/facilitator.js";
export * as solana from "./solana/facilitator.js";
import { createWalletClient, http, publicActions } from "viem";
import { getPublicClient } from "../types/shared/evm/wallet.js";
import { privateKeyToAccount } from "viem/accounts";
import { getChain } from "../shared/evm/chainUtils.js";
/**
* Verifies a payment payload against the required payment details
* Automatically detects the appropriate blockchain implementation
*
* @param payload - The signed payment payload
* @param paymentRequirements - The payment requirements that the payload must satisfy
* @param client - Optional client used for blockchain interactions (required for EVM)
* @returns A VerifyResponse indicating if the payment is valid and any invalidation reason
*/
export async function verify(payload, paymentRequirements) {
// Route to the appropriate blockchain implementation based on namespace
if (paymentRequirements.namespace === "solana") {
return solanaFacilitator.verify(payload, paymentRequirements);
}
else {
const client = getPublicClient(paymentRequirements.networkId);
return evmFacilitator.verify(client, payload, paymentRequirements);
}
}
/**
* Settles a payment payload against the required payment details
* Automatically detects the appropriate blockchain implementation
*
* @param payload - The signed payment payload
* @param paymentRequirements - The payment requirements that the payload must satisfy
* @param privateKeyOrClient - Private key (for EVM) or client (for Solana)
* @returns A SettleResponse indicating if the payment is settled and any settlement reason
*/
export async function settle(payload, paymentRequirements, privateKeyOrClient) {
// Route to the appropriate blockchain implementation based on namespace
if (paymentRequirements.namespace === "solana") {
return solanaFacilitator.settle(privateKeyOrClient, payload, paymentRequirements);
}
else {
const account = privateKeyToAccount(privateKeyOrClient);
const chain = getChain(paymentRequirements.networkId);
const client = createWalletClient({
account,
transport: http(),
chain,
}).extend(publicActions);
return evmFacilitator.settle(client, payload, paymentRequirements);
}
}
/**
* Get supported payment schemes and networks for all implementations
*
* @returns Object containing supported schemes and networks for each blockchain
*/
export function getSupported() {
return {
evm: {
h402Version: 1,
kind: [{ scheme: "exact", networkId: "evm" }],
},
solana: {
h402Version: 1,
kind: [{ scheme: "exact", networkId: "solana" }],
},
};
}
//# sourceMappingURL=index.js.map