diff --git a/docs/js/README.md b/docs/js/README.md index 1da9c1c..4fe1507 100644 --- a/docs/js/README.md +++ b/docs/js/README.md @@ -21,7 +21,7 @@ This repository is a comprehensive toolkit designed to streamline and optimize t - **[Configuration and Management](https://github.com/1inch/solidity-utils/blob/master/docs/js/modules/hardhat_setup.md)**: Tools for setting up Hardhat network configurations and managing project settings efficiently. -- **[Solidity Contracts and Libraries](https://github.com/1inch/solidity-utils/blob/master/docs/contracts/SUMMARY.md)**: Frequently used smart contracts, libraries, and interfaces aimed at providing reusable code for common use cases, alongside optimizations to improve contract efficiency. +- **[Solidity Contracts and Libraries](https://github.com/1inch/solidity-utils/blob/master/docs/SUMMARY.md)**: Frequently used smart contracts, libraries, and interfaces aimed at providing reusable code for common use cases, alongside optimizations to improve contract efficiency. ## Contributing diff --git a/docs/js/classes/hardhat_setup.Networks.md b/docs/js/classes/hardhat_setup.Networks.md index 77c22f6..2598db4 100644 --- a/docs/js/classes/hardhat_setup.Networks.md +++ b/docs/js/classes/hardhat_setup.Networks.md @@ -47,7 +47,7 @@ See the [README](https://github.com/1inch/solidity-utils/tree/master/hardhat-set #### Defined in -[hardhat-setup/networks.ts:75](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L75) +[hardhat-setup/networks.ts:75](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L75) ## Properties @@ -57,7 +57,7 @@ See the [README](https://github.com/1inch/solidity-utils/tree/master/hardhat-set #### Defined in -[hardhat-setup/networks.ts:73](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L73) +[hardhat-setup/networks.ts:73](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L73) ___ @@ -67,7 +67,7 @@ ___ #### Defined in -[hardhat-setup/networks.ts:72](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L72) +[hardhat-setup/networks.ts:72](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L72) ## Methods @@ -93,7 +93,7 @@ ___ #### Defined in -[hardhat-setup/networks.ts:96](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L96) +[hardhat-setup/networks.ts:96](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L96) ___ @@ -112,7 +112,7 @@ ___ #### Defined in -[hardhat-setup/networks.ts:139](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L139) +[hardhat-setup/networks.ts:139](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L139) ___ @@ -139,7 +139,7 @@ ___ #### Defined in -[hardhat-setup/networks.ts:113](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L113) +[hardhat-setup/networks.ts:113](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L113) ___ @@ -165,4 +165,4 @@ ___ #### Defined in -[hardhat-setup/networks.ts:120](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L120) +[hardhat-setup/networks.ts:120](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L120) diff --git a/docs/js/enums/src.NonceType.md b/docs/js/enums/src.NonceType.md new file mode 100644 index 0000000..75c0584 --- /dev/null +++ b/docs/js/enums/src.NonceType.md @@ -0,0 +1,45 @@ +[@1inch/solidity-utils](../README.md) / [Modules](../modules.md) / [src](../modules/src.md) / NonceType + +# Enumeration: NonceType + +[src](../modules/src.md).NonceType + +Enum defining types of nonces. + +## Table of contents + +### Enumeration Members + +- [Account](src.NonceType.md#account) +- [Selector](src.NonceType.md#selector) +- [Unique](src.NonceType.md#unique) + +## Enumeration Members + +### Account + +• **Account** = ``0`` + +#### Defined in + +[src/bySig.ts:10](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L10) + +___ + +### Selector + +• **Selector** = ``1`` + +#### Defined in + +[src/bySig.ts:11](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L11) + +___ + +### Unique + +• **Unique** = ``2`` + +#### Defined in + +[src/bySig.ts:12](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L12) diff --git a/docs/js/interfaces/src.DeployContractOptions.md b/docs/js/interfaces/src.DeployContractOptions.md index c6acf6d..469a18c 100644 --- a/docs/js/interfaces/src.DeployContractOptions.md +++ b/docs/js/interfaces/src.DeployContractOptions.md @@ -81,7 +81,7 @@ Number of confirmations to wait based on network. Ussually it's need for waiting #### Defined in -[src/utils.ts:29](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L29) +[src/utils.ts:29](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L29) ___ @@ -91,7 +91,7 @@ ___ #### Defined in -[src/utils.ts:27](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L27) +[src/utils.ts:27](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L27) ___ @@ -101,7 +101,7 @@ ___ #### Defined in -[src/utils.ts:31](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L31) +[src/utils.ts:31](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L31) ___ @@ -111,7 +111,7 @@ ___ #### Defined in -[src/utils.ts:32](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L32) +[src/utils.ts:32](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L32) ___ @@ -127,7 +127,7 @@ ___ #### Defined in -[src/utils.ts:30](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L30) +[src/utils.ts:30](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L30) ___ @@ -137,7 +137,7 @@ ___ #### Defined in -[src/utils.ts:35](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L35) +[src/utils.ts:35](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L35) ___ @@ -147,7 +147,7 @@ ___ #### Defined in -[src/utils.ts:38](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L38) +[src/utils.ts:38](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L38) ___ @@ -157,7 +157,7 @@ ___ #### Defined in -[src/utils.ts:37](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L37) +[src/utils.ts:37](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L37) ___ @@ -167,7 +167,7 @@ ___ #### Defined in -[src/utils.ts:36](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L36) +[src/utils.ts:36](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L36) ___ @@ -177,7 +177,7 @@ ___ #### Defined in -[src/utils.ts:34](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L34) +[src/utils.ts:34](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L34) ___ @@ -187,7 +187,7 @@ ___ #### Defined in -[src/utils.ts:33](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L33) +[src/utils.ts:33](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L33) ___ @@ -197,4 +197,4 @@ ___ #### Defined in -[src/utils.ts:39](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L39) +[src/utils.ts:39](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L39) diff --git a/docs/js/interfaces/src.SignedCallStruct.md b/docs/js/interfaces/src.SignedCallStruct.md new file mode 100644 index 0000000..5c4843c --- /dev/null +++ b/docs/js/interfaces/src.SignedCallStruct.md @@ -0,0 +1,32 @@ +[@1inch/solidity-utils](../README.md) / [Modules](../modules.md) / [src](../modules/src.md) / SignedCallStruct + +# Interface: SignedCallStruct + +[src](../modules/src.md).SignedCallStruct + +## Table of contents + +### Properties + +- [data](src.SignedCallStruct.md#data) +- [traits](src.SignedCallStruct.md#traits) + +## Properties + +### data + +• **data**: `string` + +#### Defined in + +[src/bySig.ts:51](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L51) + +___ + +### traits + +• **traits**: `bigint` + +#### Defined in + +[src/bySig.ts:50](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L50) diff --git a/docs/js/modules/hardhat_setup.md b/docs/js/modules/hardhat_setup.md index 652fe0c..8f1a013 100644 --- a/docs/js/modules/hardhat_setup.md +++ b/docs/js/modules/hardhat_setup.md @@ -45,7 +45,7 @@ Array of custom blockchain network configurations. #### Defined in -[hardhat-setup/networks.ts:11](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L11) +[hardhat-setup/networks.ts:11](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L11) ___ @@ -65,7 +65,7 @@ A helper method to get the network name from the command line arguments. #### Defined in -[hardhat-setup/networks.ts:21](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L21) +[hardhat-setup/networks.ts:21](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L21) ___ @@ -96,7 +96,7 @@ A helper method to parse RPC configuration strings. Checks that the string is in #### Defined in -[hardhat-setup/networks.ts:32](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L32) +[hardhat-setup/networks.ts:32](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L32) ___ @@ -121,4 +121,4 @@ A helper method to reset the Hardhat network to the local network or to a fork. #### Defined in -[hardhat-setup/networks.ts:46](https://github.com/1inch/solidity-utils/blob/cdb7d95/hardhat-setup/networks.ts#L46) +[hardhat-setup/networks.ts:46](https://github.com/1inch/solidity-utils/blob/dc69769/hardhat-setup/networks.ts#L46) diff --git a/docs/js/modules/src.md b/docs/js/modules/src.md index fba0a34..bcb2715 100644 --- a/docs/js/modules/src.md +++ b/docs/js/modules/src.md @@ -4,9 +4,14 @@ ## Table of contents +### Enumerations + +- [NonceType](../enums/src.NonceType.md) + ### Interfaces - [DeployContractOptions](../interfaces/src.DeployContractOptions.md) +- [SignedCallStruct](../interfaces/src.SignedCallStruct.md) ### Type Aliases @@ -27,6 +32,7 @@ ### Functions - [assertRoughlyEqualValues](src.md#assertroughlyequalvalues) +- [buildBySigTraits](src.md#buildbysigtraits) - [buildData](src.md#builddata) - [buildDataLikeDai](src.md#builddatalikedai) - [compressPermit](src.md#compresspermit) @@ -45,9 +51,11 @@ - [getPermit2](src.md#getpermit2) - [getPermitLikeDai](src.md#getpermitlikedai) - [getPermitLikeUSDC](src.md#getpermitlikeusdc) +- [hashBySig](src.md#hashbysig) - [permit2Contract](src.md#permit2contract) - [profileEVM](src.md#profileevm) - [signMessage](src.md#signmessage) +- [signSignedCall](src.md#signsignedcall) - [timeIncreaseTo](src.md#timeincreaseto) - [trackReceivedTokenAndTx](src.md#trackreceivedtokenandtx) - [trim0x](src.md#trim0x) @@ -61,7 +69,7 @@ #### Defined in -[src/permit.ts:30](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L30) +[src/permit.ts:30](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L30) ___ @@ -71,7 +79,7 @@ ___ #### Defined in -[src/permit.ts:15](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L15) +[src/permit.ts:15](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L15) ___ @@ -81,7 +89,7 @@ ___ #### Defined in -[src/permit.ts:22](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L22) +[src/permit.ts:22](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L22) ___ @@ -91,7 +99,7 @@ ___ #### Defined in -[src/permit.ts:11](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L11) +[src/permit.ts:11](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L11) ___ @@ -116,7 +124,7 @@ ___ #### Defined in -[src/prelude.ts:4](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/prelude.ts#L4) +[src/prelude.ts:4](https://github.com/1inch/solidity-utils/blob/dc69769/src/prelude.ts#L4) ___ @@ -126,7 +134,7 @@ ___ #### Defined in -[src/permit.ts:12](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L12) +[src/permit.ts:12](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L12) ___ @@ -136,7 +144,96 @@ ___ #### Defined in -[src/permit.ts:13](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L13) +[src/permit.ts:13](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L13) + +___ + +### buildBySigTraits + +▸ **buildBySigTraits**(`«destructured»?`): `bigint` + +Builds traits for {bySig} contract by combining params. + +#### Parameters + +| Name | Type | Default value | +| :------ | :------ | :------ | +| `«destructured»` | `Object` | `{}` | +| › `deadline` | `undefined` \| `number` | `0` | +| › `nonce` | `undefined` \| `number` | `0` | +| › `nonceType` | `undefined` \| [`NonceType`](../enums/src.NonceType.md) | `NonceType.Account` | +| › `relayer` | `undefined` \| `string` | `undefined` | + +#### Returns + +`bigint` + +A bigint representing the combined traits. + +**`Throws`** + +Error if provided with invalid parameters. + +#### Defined in + +[src/bySig.ts:24](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L24) + +___ + +### hashBySig + +▸ **hashBySig**(`name`, `version`, `chainId`, `verifyingContract`, `sig`): `string` + +Computes the EIP-712 hash for a given bySig call. + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `name` | `string` | The user readable name of EIP-712 domain. | +| `version` | `string` | The version of the EIP-712 domain. | +| `chainId` | `bigint` | The unique identifier for the blockchain network. | +| `verifyingContract` | `string` | The Ethereum address of the contract that will verify the signature. This ties the signature to a specific contract. | +| `sig` | [`SignedCallStruct`](../interfaces/src.SignedCallStruct.md) | The data to be signed. | + +#### Returns + +`string` + +The EIP-712 hash of the fully encoded data. + +#### Defined in + +[src/bySig.ts:63](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L63) + +___ + +### signSignedCall + +▸ **signSignedCall**(`name`, `version`, `chainId`, `verifyingContract`, `signer`, `signedCall`): `Promise`\<`string`\> + +Signs a given data for {bySig} contract call using EIP-712 standard. + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `name` | `string` | The user readable name of EIP-712 domain. | +| `version` | `string` | The version of the EIP-712 domain. | +| `chainId` | `string` \| `bigint` | The unique identifier for the blockchain network. | +| `verifyingContract` | `string` | The Ethereum address of the contract that will verify the signature. This ties the signature to a specific contract. | +| `signer` | `Wallet` \| `HardhatEthersSigner` | The wallet or signer to sign the data. | +| `signedCall` | [`SignedCallStruct`](../interfaces/src.SignedCallStruct.md) | The call data to be signed, consisting of traits and data. | + +#### Returns + +`Promise`\<`string`\> + +A Promise that resolves to the signature. + +#### Defined in + +[src/bySig.ts:84](https://github.com/1inch/solidity-utils/blob/dc69769/src/bySig.ts#L84) ## expect @@ -168,7 +265,7 @@ or if the actual value deviates from the expected by more than the specified rel #### Defined in -[src/expect.ts:15](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/expect.ts#L15) +[src/expect.ts:15](https://github.com/1inch/solidity-utils/blob/dc69769/src/expect.ts#L15) ## permit @@ -216,7 +313,7 @@ Constructs structured data for EIP-2612 permit function, including types, domain #### Defined in -[src/permit.ts:97](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L97) +[src/permit.ts:97](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L97) ___ @@ -266,7 +363,7 @@ Prepares structured data similar to the Dai permit function, including types, do #### Defined in -[src/permit.ts:129](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L129) +[src/permit.ts:129](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L129) ___ @@ -295,7 +392,7 @@ Compresses a permit function call to a shorter format based on its type. #### Defined in -[src/permit.ts:352](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L352) +[src/permit.ts:352](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L352) ___ @@ -321,7 +418,7 @@ Trims the method selector from transaction data, removing the first 8 characters #### Defined in -[src/permit.ts:58](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L58) +[src/permit.ts:58](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L58) ___ @@ -350,7 +447,7 @@ Decompresses a compressed permit function call back to its original full format. #### Defined in -[src/permit.ts:401](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L401) +[src/permit.ts:401](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L401) ___ @@ -379,7 +476,7 @@ Generates a domain separator for EIP-712 structured data using the provided para #### Defined in -[src/permit.ts:72](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L72) +[src/permit.ts:72](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L72) ___ @@ -412,7 +509,7 @@ Generates a permit signature for ERC20 tokens with EIP-2612 standard. #### Defined in -[src/permit.ts:172](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L172) +[src/permit.ts:172](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L172) ___ @@ -445,7 +542,7 @@ Creates a permit for spending tokens on Permit2 standard contracts. #### Defined in -[src/permit.ts:214](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L214) +[src/permit.ts:214](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L214) ___ @@ -478,7 +575,7 @@ Generates a Dai-like permit signature for tokens. #### Defined in -[src/permit.ts:256](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L256) +[src/permit.ts:256](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L256) ___ @@ -511,7 +608,7 @@ Generates a ERC-7597 permit signature for tokens. #### Defined in -[src/permit.ts:301](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L301) +[src/permit.ts:301](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L301) ___ @@ -531,7 +628,7 @@ Ensures contract code is set for a given address and returns a contract instance #### Defined in -[src/permit.ts:152](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L152) +[src/permit.ts:152](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L152) ___ @@ -557,7 +654,7 @@ Removes the '0x' prefix from a string. If no '0x' prefix is found, returns the o #### Defined in -[src/permit.ts:44](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L44) +[src/permit.ts:44](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L44) ___ @@ -584,7 +681,7 @@ Concatenates a target address with data, trimming the '0x' prefix from the data. #### Defined in -[src/permit.ts:339](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/permit.ts#L339) +[src/permit.ts:339](https://github.com/1inch/solidity-utils/blob/dc69769/src/permit.ts#L339) ## prelude @@ -610,7 +707,7 @@ Converts an Ether amount represented as a string into its Wei equivalent as a bi #### Defined in -[src/prelude.ts:26](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/prelude.ts#L26) +[src/prelude.ts:26](https://github.com/1inch/solidity-utils/blob/dc69769/src/prelude.ts#L26) ## profileEVM @@ -632,7 +729,7 @@ Default configuration options for the `gasspectEVM` function to analyze gas usag #### Defined in -[src/profileEVM.ts:11](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/profileEVM.ts#L11) +[src/profileEVM.ts:11](https://github.com/1inch/solidity-utils/blob/dc69769/src/profileEVM.ts#L11) ___ @@ -662,7 +759,7 @@ Analyzes gas usage by operations within a transaction, applying filters and form #### Defined in -[src/profileEVM.ts:141](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/profileEVM.ts#L141) +[src/profileEVM.ts:141](https://github.com/1inch/solidity-utils/blob/dc69769/src/profileEVM.ts#L141) ___ @@ -691,7 +788,7 @@ Profiles EVM execution by counting occurrences of specified instructions in a tr #### Defined in -[src/profileEVM.ts:112](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/profileEVM.ts#L112) +[src/profileEVM.ts:112](https://github.com/1inch/solidity-utils/blob/dc69769/src/profileEVM.ts#L112) ## utils @@ -702,14 +799,14 @@ Profiles EVM execution by counting occurrences of specified instructions in a tr **`Notice`** Represents a tuple containing a token quantity and either a transaction receipt or a recursive instance of the same tuple type. -This type is used in `trackReceivedTokenAndTx` method to track token transfers and their transaction receipts in a nested structure, +This type is used in `trackReceivedTokenAndTx` method to track token transfers and their transaction receipts in a nested structure, allowing for handling of complex scenarios like chained or batched transactions and tracking several tokens. - result[0]: The amount of the token received. - result[1]: The transaction receipt or another nested token tracking result. #### Defined in -[src/utils.ts:164](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L164) +[src/utils.ts:164](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L164) ___ @@ -737,7 +834,7 @@ Counts the occurrences of specified EVM instructions in a transaction's executio #### Defined in -[src/utils.ts:238](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L238) +[src/utils.ts:238](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L238) ___ @@ -763,7 +860,7 @@ Deploys a contract with optional Etherscan verification. #### Defined in -[src/utils.ts:53](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L53) +[src/utils.ts:53](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L53) ___ @@ -790,7 +887,7 @@ Deploys a contract given a name and optional constructor parameters. #### Defined in -[src/utils.ts:120](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L120) +[src/utils.ts:120](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L120) ___ @@ -819,7 +916,7 @@ Deploys a contract from bytecode, useful for testing and deployment of minimal p #### Defined in -[src/utils.ts:137](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L137) +[src/utils.ts:137](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L137) ___ @@ -845,7 +942,7 @@ Corrects the ECDSA signature 'v' value according to Ethereum's standard. #### Defined in -[src/utils.ts:204](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L204) +[src/utils.ts:204](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L204) ___ @@ -875,7 +972,7 @@ important part of test. #### Defined in -[src/utils.ts:263](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L263) +[src/utils.ts:263](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L263) ___ @@ -902,7 +999,7 @@ Signs a message with a given signer and fixes the signature format. #### Defined in -[src/utils.ts:223](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L223) +[src/utils.ts:223](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L223) ___ @@ -926,7 +1023,7 @@ Advances the blockchain time to a specific timestamp for testing purposes. #### Defined in -[src/utils.ts:107](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L107) +[src/utils.ts:107](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L107) ___ @@ -963,7 +1060,7 @@ It could be used recursively for multiple tokens via specific `txPromise` functi #### Defined in -[src/utils.ts:177](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L177) +[src/utils.ts:177](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L177) ## utils Represents the interface for a token, providing methods to fetch its balance and address. @@ -990,4 +1087,4 @@ Method which retrieves the token contract's address. #### Defined in -[src/utils.ts:151](https://github.com/1inch/solidity-utils/blob/cdb7d95/src/utils.ts#L151) +[src/utils.ts:151](https://github.com/1inch/solidity-utils/blob/dc69769/src/utils.ts#L151) diff --git a/package.json b/package.json index 2a3514a..3dd2dc0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@1inch/solidity-utils", - "version": "5.0.0", + "version": "5.1.0", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", "exports": { diff --git a/src/bySig.ts b/src/bySig.ts new file mode 100644 index 0000000..7e289c9 --- /dev/null +++ b/src/bySig.ts @@ -0,0 +1,97 @@ +import { ethers } from 'hardhat'; +import { constants } from './prelude'; +import { Wallet } from 'ethers'; +import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers'; + +/** + * Enum defining types of nonces. + */ +export enum NonceType { + Account, // Nonce for account + Selector, // Nonce for selector + Unique, // Nonce for unique +} + +/** + * Builds traits for {bySig} contract by combining params. + * @param nonceType The type of nonce to use. Default is NonceType.Account. + * @param deadline The deadline for the message. Default is 0. + * @param relayer The relayer address. Default is the zero address. + * @param nonce The nonce. Default is 0. + * @returns A bigint representing the combined traits. + * @throws Error if provided with invalid parameters. + */ +export function buildBySigTraits({ + nonceType = NonceType.Account, + deadline = 0, + relayer = constants.ZERO_ADDRESS.toString(), + nonce = 0, +} = {}): bigint { + if (nonceType > 3) { + throw new Error('Wrong nonce type, it should be less than 4'); + } + if (deadline > 0xffffffffff) { + throw new Error('Wrong deadline, it should be less than 0xffffffff'); + } + if (relayer.length > 42) { + throw new Error('Wrong relayer address, it should be less than 42 symbols'); + } + if (nonce > 0xffffffffffffffffffffffffffffffffn) { + throw new Error('Wrong nonce, it should not be more than 128 bits'); + } + + return (BigInt(nonceType) << 254n) + + (BigInt(deadline) << 208n) + + ((BigInt(relayer) & 0xffffffffffffffffffffn) << 128n) + + BigInt(nonce); +} + +export interface SignedCallStruct { + traits: bigint; + data: string; +} + +/** + * Computes the EIP-712 hash for a given bySig call. + * @param name The user readable name of EIP-712 domain. + * @param version The version of the EIP-712 domain. + * @param chainId The unique identifier for the blockchain network. + * @param verifyingContract The Ethereum address of the contract that will verify the signature. This ties the signature to a specific contract. + * @param sig The data to be signed. + * @returns The EIP-712 hash of the fully encoded data. + */ +export function hashBySig(name: string, version: string, chainId: bigint, verifyingContract: string, sig: SignedCallStruct): string { + const domain = { name, version, chainId, verifyingContract }; + const types = { + SignedCall: [ + { name: 'traits', type: 'uint256' }, + { name: 'data', type: 'bytes' }, + ], + }; + return ethers.TypedDataEncoder.hash(domain, types, sig); +} + +/** + * Signs a given data for {bySig} contract call using EIP-712 standard. + * @param name The user readable name of EIP-712 domain. + * @param version The version of the EIP-712 domain. + * @param chainId The unique identifier for the blockchain network. + * @param verifyingContract The Ethereum address of the contract that will verify the signature. This ties the signature to a specific contract. + * @param signer The wallet or signer to sign the data. + * @param signedCall The call data to be signed, consisting of traits and data. + * @returns A Promise that resolves to the signature. + */ +export function signSignedCall( + name: string, + version: string, + chainId: bigint | string, + verifyingContract: string, + signer: Wallet | SignerWithAddress, + signedCall: SignedCallStruct, +): Promise { + return signer.signTypedData( + { name, version, chainId, verifyingContract }, + { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, + signedCall + ); +} diff --git a/src/index.ts b/src/index.ts index 61b2286..9053865 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ // created from 'create-ts-index' +export * from './bySig'; export * from './expect'; export * from './permit'; export * from './prelude'; diff --git a/test/contracts/BySig.test.ts b/test/contracts/BySig.test.ts index d2809ee..1170fda 100644 --- a/test/contracts/BySig.test.ts +++ b/test/contracts/BySig.test.ts @@ -2,34 +2,19 @@ import { constants } from '../../src/prelude'; import { expect } from '../../src/expect'; import { loadFixture } from '@nomicfoundation/hardhat-network-helpers'; import { ethers } from 'hardhat'; -import { NonceType, buildBySigTraits } from './BySigTraits.test'; - -interface SignedCallStruct { - traits: bigint; - data: string; -} - -function hashBySig(name: string, version: string, chainId: bigint, verifyingContract: string, sig: SignedCallStruct): string { - const domain = { name, version, chainId, verifyingContract }; - const types = { - SignedCall: [ - { name: 'traits', type: 'uint256' }, - { name: 'data', type: 'bytes' }, - ], - }; - return ethers.TypedDataEncoder.hash(domain, types, sig); -} +import { NonceType, buildBySigTraits, hashBySig, signSignedCall } from '../../src/bySig'; describe('BySig', function () { async function deployAddressArrayMock() { const [alice, bob, carol] = await ethers.getSigners(); - const eip712Version = '1'; + const version = '1'; + const name = 'Token'; const TokenWithBySig = await ethers.getContractFactory('TokenWithBySig'); - const token = await TokenWithBySig.deploy('Token', 'TKN', eip712Version); + const token = await TokenWithBySig.deploy(name, 'TKN', version); await token.mint(bob.address, 1000); - return { addrs: { alice, bob, carol }, token, eip712Version }; + return { addrs: { alice, bob, carol }, token, eip712: { name, version } }; } describe('bySigAccountNonces and useBySigAccountNonce', function () { @@ -83,12 +68,12 @@ describe('BySig', function () { describe('hashBySig', function () { it('should return correct hash', async function () { - const { token, eip712Version } = await loadFixture(deployAddressArrayMock); + const { token, eip712: { name, version } } = await loadFixture(deployAddressArrayMock); const sig = { traits: buildBySigTraits(), data: '0x', }; - expect(await token.hashBySig(sig)).to.be.equal(hashBySig(await token.name(), eip712Version, await token.getChainId(), await token.getAddress(), sig)); + expect(await token.hashBySig(sig)).to.be.equal(hashBySig(name, version, await token.getChainId(), await token.getAddress(), sig)); }); }); @@ -152,30 +137,22 @@ describe('BySig', function () { }); it('should revert with wrong signature', async function () { - const { addrs: { alice, bob }, token } = await loadFixture(deployAddressArrayMock); + const { addrs: { alice, bob }, token, eip712: { name, version } } = await loadFixture(deployAddressArrayMock); const signedCall = { traits: buildBySigTraits({ deadline: 0xffffffffff, nonceType: NonceType.Selector, nonce: 0 }), data: token.interface.encodeFunctionData('transfer', [alice.address, 100]), }; - const signature = await alice.signTypedData( - { name: 'Token', version: '1', chainId: await token.getChainId(), verifyingContract: await token.getAddress() }, - { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, - signedCall - ); + const signature = await signSignedCall(name, version, await token.getChainId(), await token.getAddress(), alice, signedCall); await expect(token.bySig(bob, signedCall, signature)).to.be.revertedWithCustomError(token, 'WrongSignature'); }); it('should work for transfer method', async function () { - const { addrs: { alice, bob }, token } = await loadFixture(deployAddressArrayMock); + const { addrs: { alice, bob }, token, eip712: { name, version } } = await loadFixture(deployAddressArrayMock); const signedCall = { traits: buildBySigTraits({ deadline: 0xffffffffff, nonceType: NonceType.Selector, nonce: 0 }), data: token.interface.encodeFunctionData('transfer', [alice.address, 100]), }; - const signature = await bob.signTypedData( - { name: 'Token', version: '1', chainId: await token.getChainId(), verifyingContract: await token.getAddress() }, - { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, - signedCall - ); + const signature = await signSignedCall(name, version, await token.getChainId(), await token.getAddress(), bob, signedCall); await expect(token.bySig(bob, signedCall, signature)) .to.emit(token, 'Transfer') .withArgs(bob.address, alice.address, 100); @@ -184,17 +161,13 @@ describe('BySig', function () { }); it('should make approve for sponsored call', async function () { - const { addrs: { alice, bob }, token } = await loadFixture(deployAddressArrayMock); + const { addrs: { alice, bob }, token, eip712: { name, version } } = await loadFixture(deployAddressArrayMock); const approveData = token.interface.encodeFunctionData('approve', [alice.address, 100]); const signedCall = { traits: buildBySigTraits({ deadline: 0xffffffffff, nonceType: NonceType.Selector, nonce: 0 }), data: token.interface.encodeFunctionData('sponsoredCall', [await token.getAddress(), '0', approveData, '0x']), }; - const signature = await bob.signTypedData( - { name: 'Token', version: '1', chainId: await token.getChainId(), verifyingContract: await token.getAddress() }, - { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, - signedCall - ); + const signature = await signSignedCall(name, version, await token.getChainId(), await token.getAddress(), bob, signedCall); expect(await token.allowance(bob.address, alice.address)).to.be.equal(0); await expect(token.bySig(bob, signedCall, signature)) .to.emit(token, 'ChargedSigner') @@ -203,29 +176,21 @@ describe('BySig', function () { }); it('should work recursively', async function () { - const { addrs: { alice, bob, carol }, token } = await loadFixture(deployAddressArrayMock); + const { addrs: { alice, bob, carol }, token, eip712: { name, version } } = await loadFixture(deployAddressArrayMock); // Bob sign for Carol const bobSignedCall = { traits: buildBySigTraits({ relayer: carol.address, deadline: 0xffffffffff, nonceType: NonceType.Selector, nonce: 0 }), data: token.interface.encodeFunctionData('transfer', [carol.address, 100]), }; - const bobSignature = await bob.signTypedData( - { name: 'Token', version: '1', chainId: await token.getChainId(), verifyingContract: await token.getAddress() }, - { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, - bobSignedCall - ); + const bobSignature = await signSignedCall(name, version, await token.getChainId(), await token.getAddress(), bob, bobSignedCall); // Carol sign for Alice const carolSignedCall = { traits: buildBySigTraits({ relayer: alice.address, deadline: 0xffffffffff, nonceType: NonceType.Selector, nonce: 0 }), data: token.interface.encodeFunctionData('bySig', [bob.address, bobSignedCall, bobSignature]), }; - const carolSignature = await carol.signTypedData( - { name: 'Token', version: '1', chainId: await token.getChainId(), verifyingContract: await token.getAddress() }, - { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, - carolSignedCall - ); + const carolSignature = await signSignedCall(name, version, await token.getChainId(), await token.getAddress(), carol, carolSignedCall); await expect(token.bySig(carol, carolSignedCall, carolSignature)) .to.emit(token, 'Transfer') @@ -235,7 +200,7 @@ describe('BySig', function () { }); it('should work recursively for sponsored call', async function () { - const { addrs: { alice, bob, carol }, token } = await loadFixture(deployAddressArrayMock); + const { addrs: { alice, bob, carol }, token, eip712: { name, version } } = await loadFixture(deployAddressArrayMock); // Bob sign for Carol const approveData = token.interface.encodeFunctionData('approve', [carol.address, 100]); @@ -243,11 +208,7 @@ describe('BySig', function () { traits: buildBySigTraits({ relayer: carol.address, deadline: 0xffffffffff, nonceType: NonceType.Selector, nonce: 0 }), data: token.interface.encodeFunctionData('sponsoredCall', [await token.getAddress(), '0', approveData, '0x']), }; - const bobSignature = await bob.signTypedData( - { name: 'Token', version: '1', chainId: await token.getChainId(), verifyingContract: await token.getAddress() }, - { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, - bobSignedCall - ); + const bobSignature = await signSignedCall(name, version, await token.getChainId(), await token.getAddress(), bob, bobSignedCall); // Carol sign for Alice const carolSigByData = token.interface.encodeFunctionData('bySig', [bob.address, bobSignedCall, bobSignature]); @@ -255,11 +216,7 @@ describe('BySig', function () { traits: buildBySigTraits({ relayer: alice.address, deadline: 0xffffffffff, nonceType: NonceType.Selector, nonce: 0 }), data: token.interface.encodeFunctionData('sponsoredCall', [await token.getAddress(), '0', carolSigByData, '0x']), }; - const carolSignature = await carol.signTypedData( - { name: 'Token', version: '1', chainId: await token.getChainId(), verifyingContract: await token.getAddress() }, - { SignedCall: [{ name: 'traits', type: 'uint256' }, { name: 'data', type: 'bytes' }] }, - carolSignedCall - ); + const carolSignature = await signSignedCall(name, version, await token.getChainId(), await token.getAddress(), carol, carolSignedCall); expect(await token.allowance(bob.address, carol.address)).to.be.equal(0); await expect(token.bySig(carol, carolSignedCall, carolSignature)) diff --git a/test/contracts/BySigTraits.test.ts b/test/contracts/BySigTraits.test.ts index 0d62253..94cc634 100644 --- a/test/contracts/BySigTraits.test.ts +++ b/test/contracts/BySigTraits.test.ts @@ -2,37 +2,7 @@ import { constants } from '../../src/prelude'; import { expect } from '../../src/expect'; import { loadFixture } from '@nomicfoundation/hardhat-network-helpers'; import { ethers } from 'hardhat'; - -export const NonceType = { - Account: 0, - Selector: 1, - Unique: 2, -}; - -export function buildBySigTraits({ - nonceType = 0, - deadline = 0, - relayer = constants.ZERO_ADDRESS.toString(), - nonce = 0, -} = {}): bigint { - if (nonceType > 3) { - throw new Error('Wrong nonce type, it should be less than 4'); - } - if (deadline > 0xffffffffff) { - throw new Error('Wrong deadline, it should be less than 0xffffffff'); - } - if (relayer.length > 42) { - throw new Error('Wrong relayer address, it should be less than 42 symbols'); - } - if (nonce > 0xffffffffffffffffffffffffffffffffn) { - throw new Error('Wrong nonce, it should not be more than 128 bits'); - } - - return (BigInt(nonceType) << 254n) + - (BigInt(deadline) << 208n) + - ((BigInt(relayer) & 0xffffffffffffffffffffn) << 128n) + - BigInt(nonce); -} +import { NonceType, buildBySigTraits } from '../../src/bySig'; describe('BySigTraits', function () { async function deployAddressArrayMock() { @@ -62,7 +32,7 @@ describe('BySigTraits', function () { it('should revert with unsupported nonce', async function () { const { bySigTraitsMock } = await loadFixture(deployAddressArrayMock); - const value = buildBySigTraits({ nonceType: 3 }); + const value = buildBySigTraits({ nonceType: 3 as NonceType }); await expect(bySigTraitsMock.nonceType(value)).to.be.revertedWithCustomError(bySigTraitsMock, 'WrongNonceType'); }); });