-
Notifications
You must be signed in to change notification settings - Fork 604
[SDK] Add verifyPayment() backend utility for arbitrary chain x402 payments #8091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joaquim-verges
merged 1 commit into
main
from
_SDK_Add_verifyPayment_backend_utility_for_arbitrary_chain_x402_payments
Sep 22, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"thirdweb": minor | ||
--- | ||
|
||
Accept arbitrary chain ids for x402 payments with new verifyPayment() backend utility |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
export { decodePayment, encodePayment } from "../x402/encode.js"; | ||
export { | ||
facilitator, | ||
type ThirdwebX402FacilitatorConfig, | ||
} from "../x402/facilitator.js"; | ||
export { wrapFetchWithPayment } from "../x402/fetchWithPayment.js"; | ||
export { | ||
type VerifyPaymentArgs, | ||
type VerifyPaymentResult, | ||
verifyPayment, | ||
} from "../x402/verify-payment.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { ExactEvmPayload } from "x402/types"; | ||
import { | ||
type RequestedPaymentPayload, | ||
RequestedPaymentPayloadSchema, | ||
} from "./schemas.js"; | ||
|
||
/** | ||
* Encodes a payment payload into a base64 string, ensuring bigint values are properly stringified | ||
* | ||
* @param payment - The payment payload to encode | ||
* @returns A base64 encoded string representation of the payment payload | ||
*/ | ||
export function encodePayment(payment: RequestedPaymentPayload): string { | ||
let safe: RequestedPaymentPayload; | ||
|
||
// evm | ||
const evmPayload = payment.payload as ExactEvmPayload; | ||
safe = { | ||
...payment, | ||
payload: { | ||
...evmPayload, | ||
authorization: Object.fromEntries( | ||
Object.entries(evmPayload.authorization).map(([key, value]) => [ | ||
key, | ||
typeof value === "bigint" ? (value as bigint).toString() : value, | ||
]), | ||
) as ExactEvmPayload["authorization"], | ||
}, | ||
}; | ||
return safeBase64Encode(JSON.stringify(safe)); | ||
} | ||
|
||
/** | ||
* Decodes a base64 encoded payment string back into a PaymentPayload object | ||
* | ||
* @param payment - The base64 encoded payment string to decode | ||
* @returns The decoded and validated PaymentPayload object | ||
*/ | ||
export function decodePayment(payment: string): RequestedPaymentPayload { | ||
const decoded = safeBase64Decode(payment); | ||
const parsed = JSON.parse(decoded); | ||
|
||
const obj: RequestedPaymentPayload = { | ||
...parsed, | ||
payload: parsed.payload as ExactEvmPayload, | ||
}; | ||
const validated = RequestedPaymentPayloadSchema.parse(obj); | ||
return validated; | ||
} | ||
|
||
/** | ||
* Encodes a string to base64 format | ||
* | ||
* @param data - The string to be encoded to base64 | ||
* @returns The base64 encoded string | ||
*/ | ||
export function safeBase64Encode(data: string): string { | ||
if ( | ||
typeof globalThis !== "undefined" && | ||
typeof globalThis.btoa === "function" | ||
) { | ||
return globalThis.btoa(data); | ||
} | ||
return Buffer.from(data).toString("base64"); | ||
} | ||
joaquim-verges marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Decodes a base64 string back to its original format | ||
* | ||
* @param data - The base64 encoded string to be decoded | ||
* @returns The decoded string in UTF-8 format | ||
*/ | ||
function safeBase64Decode(data: string): string { | ||
if ( | ||
typeof globalThis !== "undefined" && | ||
typeof globalThis.atob === "function" | ||
) { | ||
return globalThis.atob(data); | ||
} | ||
return Buffer.from(data, "base64").toString("utf-8"); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.