Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit a21078b

Browse files
feat: add custom transaction schema to formatTransaction (#7227)
* feat: add custom transaction schema to formatTransaction * docs: add entries to changelogs * tests: initial tests * fix: unused vars * tests: lint issues * refactor: pr review * fix: type errors * fix: dependency cycle * refactor: revert whitespaces changes * fix: types * fix: types * test: fix web3-eth-personal tests * refactor: remove config from manager * fix: types * fix: build issue * fix: CustomTransactionSchema type
1 parent b3cb1b7 commit a21078b

21 files changed

+207
-25
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,3 +2702,24 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
27022702
- The callback function provided to the static `Web3.onNewProviderDiscovered` function expects a parameter of type `EIP6963ProvidersMapUpdateEvent` as opposed to `EIP6963AnnounceProviderEvent`. (#7242)
27032703

27042704
## [Unreleased]
2705+
2706+
### Added
2707+
2708+
#### web3-core
2709+
2710+
- Adds a new property (`customTransactionSchema`) to `Web3ConfigOptions`
2711+
- Adds a new property (`config`) to `Web3RequestManager`
2712+
2713+
#### web3-eth
2714+
2715+
- Adds the same `{transactionSchema?: ValidationSchemaInput}` that exists in `formatTransaction` to `validateTransactionForSigning`
2716+
2717+
### Changed
2718+
2719+
#### web3-eth
2720+
2721+
- Forwards the new `web3Context.config.customTransactionSchema` to `formatTransaction`
2722+
2723+
#### web3-eth-personal
2724+
2725+
- Forwards the new `web3Context.config.customTransactionSchema` to `formatTransaction`

docs/docs/guides/web3_config/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ There is list of configuration params that can be set for modifying behavior of
2828
- [defaultCommon](/guides/web3_config/#defaultcommon)
2929
- [defaultTransactionType](/guides/web3_config/#defaulttransactiontype)
3030
- [defaultMaxPriorityFeePerGas](/guides/web3_config/#defaultmaxpriorityfeepergas)
31+
- [customTransactionSchema](/guides/web3_config/#customTransactionSchema)
3132
- [defaultReturnFormat](/guides/web3_config/#defaultreturnformat)
3233

3334
## Global level Config
@@ -411,6 +412,9 @@ The `defaultMaxPriorityFeePerGas` option is used to set the [`defaultMaxPriority
411412

412413
The default value of `defaultMaxPriorityFeePerGas` is 2500000000 (2.5gwei) in hexstring format.
413414

415+
### [customTransactionSchema](/api/web3-core/class/Web3Config#customTransactionSchema)
416+
The `customTransactionSchema` option is used to allow [`formatTransaction`](/api/web3-eth/function/formatTransaction) to accept a custom schema to validate transactions. A use-case could be: your chain has an extra field in its transactions and you want to write a plugin that makes sending these transactions easier.
417+
414418
### [defaultReturnFormat](/api/web3-core/class/Web3Config#defaultReturnFormat)
415419
The `defaultReturnFormat` option allows users to specify the format in which certain types of data should be returned by default. It is a configuration parameter that can be set at the global level and affects how data is returned across the entire library.
416420
```ts

packages/web3-core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,8 @@ Documentation:
234234
- `setConfig()` fix for `setMaxListenerWarningThreshold` fix (#5079)
235235

236236
## [Unreleased]
237+
238+
### Added
239+
240+
- Adds a new property (`customTransactionSchema`) to `Web3ConfigOptions`
241+
- Adds a new property (`config`) to `Web3RequestManager`

packages/web3-core/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
Web3APIMethod,
2424
Web3APIReturnType,
2525
} from 'web3-types';
26+
import { Schema } from 'web3-validator';
2627

2728
export type TransactionTypeParser = (transaction: Transaction) => HexString | undefined;
2829

@@ -50,3 +51,8 @@ export interface RequestManagerMiddleware<API> {
5051
options?: { [key: string]: unknown },
5152
): Promise<JsonRpcResponse<ResponseType>>;
5253
}
54+
55+
export type CustomTransactionSchema = {
56+
type: string;
57+
properties: Record<string, Schema>;
58+
};

packages/web3-core/src/web3_config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from 'web3-types';
2626
import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors';
2727
import { isNullish, toHex } from 'web3-utils';
28-
import { TransactionTypeParser } from './types.js';
28+
import { CustomTransactionSchema, TransactionTypeParser } from './types.js';
2929
// eslint-disable-next-line import/no-cycle
3030
import { TransactionBuilder } from './web3_context.js';
3131
import { Web3EventEmitter } from './web3_event_emitter.js';
@@ -59,6 +59,7 @@ export interface Web3ConfigOptions {
5959
};
6060
transactionBuilder?: TransactionBuilder;
6161
transactionTypeParser?: TransactionTypeParser;
62+
customTransactionSchema?: CustomTransactionSchema;
6263
defaultReturnFormat: DataFormat;
6364
}
6465

@@ -101,6 +102,7 @@ export abstract class Web3Config
101102
},
102103
transactionBuilder: undefined,
103104
transactionTypeParser: undefined,
105+
customTransactionSchema: undefined,
104106
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
105107
};
106108

@@ -520,6 +522,15 @@ export abstract class Web3Config
520522
this.config.transactionTypeParser = val;
521523
}
522524

525+
public get customTransactionSchema(): CustomTransactionSchema | undefined {
526+
return this.config.customTransactionSchema;
527+
}
528+
529+
public set customTransactionSchema(schema: CustomTransactionSchema | undefined) {
530+
this._triggerConfigChange('customTransactionSchema', schema);
531+
this.config.customTransactionSchema = schema;
532+
}
533+
523534
private _triggerConfigChange<K extends keyof Web3ConfigOptions>(
524535
config: K,
525536
newValue: Web3ConfigOptions[K],

packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ exports[`Web3Context getContextObject should return correct context object 1`] =
66
"config": {
77
"blockHeaderTimeout": 10,
88
"contractDataInputFill": "data",
9+
"customTransactionSchema": undefined,
910
"defaultAccount": undefined,
1011
"defaultBlock": "latest",
1112
"defaultChain": "mainnet",

packages/web3-core/test/unit/web3_config.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const defaultConfig = {
4949
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
5050
transactionBuilder: undefined,
5151
transactionTypeParser: undefined,
52+
customTransactionSchema: undefined,
5253
};
5354
const setValue = {
5455
string: 'newValue',

packages/web3-eth-personal/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,7 @@ Documentation:
148148
- Dependencies updated
149149

150150
## [Unreleased]
151+
152+
### Changed
153+
154+
- Forwards the new `web3Context.config.customTransactionSchema` to `formatTransaction`

packages/web3-eth-personal/src/personal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export class Personal extends Web3Context<EthPersonalAPI> {
159159
* ```
160160
*/
161161
public async sendTransaction(tx: Transaction, passphrase: string) {
162-
return rpcWrappers.sendTransaction(this.requestManager, tx, passphrase);
162+
return rpcWrappers.sendTransaction(this.requestManager, tx, passphrase, this.config);
163163
}
164164
/**
165165
* Signs a transaction. This account needs to be unlocked.
@@ -204,7 +204,7 @@ export class Personal extends Web3Context<EthPersonalAPI> {
204204
* ```
205205
*/
206206
public async signTransaction(tx: Transaction, passphrase: string) {
207-
return rpcWrappers.signTransaction(this.requestManager, tx, passphrase);
207+
return rpcWrappers.signTransaction(this.requestManager, tx, passphrase, this.config);
208208
}
209209
/**
210210
* Calculates an Ethereum specific signature with:

packages/web3-eth-personal/src/rpc_method_wrappers.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GNU Lesser General Public License for more details.
1414
You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17-
import { Web3RequestManager } from 'web3-core';
17+
import { Web3RequestManager, Web3ConfigOptions } from 'web3-core';
1818
import { toChecksumAddress, utf8ToHex } from 'web3-utils';
1919
import { formatTransaction } from 'web3-eth';
2020
import { Address, EthPersonalAPI, ETH_DATA_FORMAT, HexString, Transaction } from 'web3-types';
@@ -72,8 +72,11 @@ export const sendTransaction = async (
7272
requestManager: Web3RequestManager<EthPersonalAPI>,
7373
tx: Transaction,
7474
passphrase: string,
75+
config?: Web3ConfigOptions,
7576
) => {
76-
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);
77+
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, {
78+
transactionSchema: config?.customTransactionSchema,
79+
});
7780

7881
return personalRpcMethods.sendTransaction(requestManager, formattedTx, passphrase);
7982
};
@@ -82,8 +85,11 @@ export const signTransaction = async (
8285
requestManager: Web3RequestManager<EthPersonalAPI>,
8386
tx: Transaction,
8487
passphrase: string,
88+
config?: Web3ConfigOptions,
8589
) => {
86-
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT);
90+
const formattedTx = formatTransaction(tx, ETH_DATA_FORMAT, {
91+
transactionSchema: config?.customTransactionSchema,
92+
});
8793

8894
return personalRpcMethods.signTransaction(requestManager, formattedTx, passphrase);
8995
};

0 commit comments

Comments
 (0)