Repository navigation
feat(thirdweb): add DACS-Standard (§9.5.7) pre-flight spend mandate guardrail to sendTransaction #9076
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
base: main
Are you sure you want to change the base?
feat(thirdweb): add DACS-Standard (§9.5.7) pre-flight spend mandate guardrail to sendTransaction #9076
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,53 @@ import type { GaslessOptions } from "./gasless/types.js"; | |
| import { toSerializableTransaction } from "./to-serializable-transaction.js"; | ||
| import type { WaitForReceiptOptions } from "./wait-for-tx-receipt.js"; | ||
|
|
||
| // ─── DACS-Standard Guardrail (§9.5.7 & §14.4) ───────────────────────────── | ||
|
|
||
| export interface DacsMandateMeta { | ||
| /** Signed DACS mandate identifier or URI. */ | ||
| dacsMandate?: string; | ||
| /** Active DACS session identifier. */ | ||
| dacsSessionId?: string; | ||
| /** Hard authorized spend ceiling in atomic units (wei). */ | ||
| dacsMaxAmount?: bigint | string | number; | ||
| } | ||
|
|
||
| export function assertDacsSpendMandate( | ||
| requestedAmount: bigint | string | number, | ||
| meta?: DacsMandateMeta, | ||
| ): void { | ||
| if (!meta || (!meta.dacsMandate && !meta.dacsSessionId)) { | ||
| return; | ||
| } | ||
|
|
||
| if (meta.dacsMaxAmount !== undefined) { | ||
| let ceiling: bigint; | ||
| let requested: bigint; | ||
| try { | ||
| ceiling = BigInt(meta.dacsMaxAmount); | ||
| requested = BigInt(requestedAmount); | ||
|
Comment on lines
+32
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject unsafe numeric amounts before conversion. A numeric ceiling written as Based on learnings, preserve exact atomic amounts rather than accepting precision loss through JavaScript numbers.
|
||
| } catch { | ||
| throw new Error( | ||
| `[DACS-Standard §9.5.7] Invalid numeric amount or dacsMaxAmount ceiling.`, | ||
| ); | ||
| } | ||
|
|
||
| if (ceiling < 0n) { | ||
| throw new Error( | ||
| `[DACS-Standard §9.5.7] Invalid negative dacsMaxAmount ceiling: "${meta.dacsMaxAmount}".`, | ||
| ); | ||
| } | ||
|
|
||
| if (requested > ceiling) { | ||
| throw new Error( | ||
| `[DACS-Standard §9.5.7] sendTransaction execution aborted: requested value (${requested}) exceeds authorized mandate ceiling (${ceiling}).`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Send transaction options */ | ||
| export interface SendTransactionOptions { | ||
| export interface SendTransactionOptions extends DacsMandateMeta { | ||
| /** | ||
| * The account to send the transaction with | ||
| */ | ||
|
|
@@ -76,7 +121,7 @@ export interface SendTransactionOptions { | |
| * nft: { | ||
| * name: "NFT Name", | ||
| * description: "NFT Description", | ||
| * image: "https://example.com/image.png", | ||
| * image: "[https://example.com/image.png](https://example.com/image.png)", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep Markdown syntax outside executable URL strings. These examples pass Markdown text as runtime values. The image field no longer contains the plain image URL. The Engine provider passes Also applies to: 196-196
|
||
| * }, | ||
| * }); | ||
| * | ||
|
|
@@ -148,7 +193,7 @@ export interface SendTransactionOptions { | |
| * transaction, | ||
| * gasless: { | ||
| * provider: "engine", | ||
| * relayerUrl: "https://thirdweb.engine-***.thirdweb.com/relayer/***", | ||
| * relayerUrl: "[https://thirdweb.engine-](https://thirdweb.engine-)***[.thirdweb.com/relayer/](https://.thirdweb.com/relayer/)***", | ||
| * relayerForwarderAddress: "0x...", | ||
| * } | ||
| * }); | ||
|
|
@@ -171,6 +216,18 @@ export async function sendTransaction( | |
| options: SendTransactionOptions, | ||
| ): Promise<WaitForReceiptOptions> { | ||
| let { account, transaction, gasless } = options; | ||
| const dacsMeta: DacsMandateMeta | undefined = | ||
| options.dacsMandate || options.dacsSessionId | ||
| ? { | ||
| dacsMandate: options.dacsMandate, | ||
| dacsSessionId: options.dacsSessionId, | ||
| dacsMaxAmount: options.dacsMaxAmount, | ||
| } | ||
| : undefined; | ||
|
|
||
| if (typeof transaction.value === "bigint") { | ||
| assertDacsSpendMandate(transaction.value, dacsMeta); | ||
| } | ||
|
|
||
| const decorator = getTransactionDecorator(); | ||
| if (decorator) { | ||
|
|
@@ -186,6 +243,10 @@ export async function sendTransaction( | |
|
|
||
| // if zksync transaction params are set, send with eip712 | ||
| if (transaction.eip712) { | ||
| assertDacsSpendMandate( | ||
| typeof transaction.value === "bigint" ? transaction.value : 0n, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the resolved EIP-712 value instead of zero. With
|
||
| dacsMeta, | ||
| ); | ||
| const { sendEip712Transaction } = await import( | ||
| "./zksync/send-eip712-transaction.js" | ||
| ); | ||
|
|
@@ -199,6 +260,10 @@ export async function sendTransaction( | |
| from: account, | ||
| transaction: transaction, | ||
| }); | ||
|
|
||
| // DACS-Standard (§9.5.7 & §14.4) pre-flight spend mandate ceiling check | ||
| assertDacsSpendMandate(serializableTransaction.value ?? 0n, dacsMeta); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve and check the transaction value before serialization runs signing thunks. The spend guard runs after Resolve
|
||
|
|
||
| // branch for gasless transactions | ||
| if (gasless) { | ||
| // lazy load the gasless tx function because it's only needed for gasless transactions | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expose the new public symbols through the package entrypoints.
Neither
thirdwebnorthirdweb/transactionre-exportsassertDacsSpendMandateorDacsMandateMeta. Both entrypoints export only the existing send function and options from this module. Add the helper export and metadata type export to the appropriate feature barrels. (raw.githubusercontent.com)As per coding guidelines, “Export everything via the exports/ directory, grouped by feature.”
Source: Coding guidelines