Skip to content
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

Implement EIP712 RPC Wrapper Methods and getEncodedEip712Data Util #6286

Merged
merged 15 commits into from
Jul 20, 2023

Conversation

spacesailor24
Copy link
Contributor

@spacesailor24 spacesailor24 commented Jul 16, 2023

This PR

  • Adds RPC method wrappers for eth_signTypedData and eth_signTypedData_v4 to web3-eth and web3-rpc-methods packages
  • A new util method, getEncodedEip712Message was added to web3-eth-abi
    • The implementation of this code was copied from this repo which is a more defined and type safe implementation of the example code provided in EIP-712
      • I've removed a lot of code we don't need and modified some of the code to use standards established in 4.x
    • I've only exported the single util method to give users the ability to encode EIP-712 data and sign it without having to use signTypedData RPC call incase their connected provider doesn't support it (e.g. Geth). The util method requires the use of an abi encoder, so it's using the already imported ethersAbiCoder from ethers_abi_coder.js in web3-eth-abi package. This util methods API is pretty basic so it shouldn't be a problem avoiding a breaking change when web3.js migrates from using Ether's ABI encoder
const getMessage = (typedData: Eip712TypedData, hash?: boolean): string

closes #5927

@spacesailor24 spacesailor24 added the 4.x 4.0 related label Jul 16, 2023
@spacesailor24 spacesailor24 self-assigned this Jul 16, 2023
@github-actions
Copy link

github-actions bot commented Jul 16, 2023

Bundle Stats

Hey there, this message comes from a github action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Asset Old size New size Diff Diff %
Total 649 KB 651 KB 2.2 KB 0.34%
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset Old size New size Diff Diff %
web3.min.js 631 KB 634 KB 2.2 KB 0.35%
../lib/commonjs/index.d.ts 8.56 KB 8.56 KB 0 0.00%
../lib/commonjs/accounts.d.ts 3.67 KB 3.67 KB 0 0.00%
../lib/commonjs/types.d.ts 2.45 KB 2.45 KB 0 0.00%
../lib/commonjs/web3.d.ts 1.14 KB 1.14 KB 0 0.00%
../lib/commonjs/abi.d.ts 1000 bytes 1000 bytes 0 0.00%
../lib/commonjs/eth.exports.d.ts 280 bytes 280 bytes 0 0.00%
../lib/commonjs/providers.exports.d.ts 148 bytes 148 bytes 0 0.00%
../lib/commonjs/version.d.ts 60 bytes 60 bytes 0 0.00%

@codecov
Copy link

codecov bot commented Jul 16, 2023

Codecov Report

Merging #6286 (d80a590) into 4.x (7d0a91d) will increase coverage by 0.16%.
The diff coverage is 96.05%.

Additional details and impacted files
@@            Coverage Diff             @@
##              4.x    #6286      +/-   ##
==========================================
+ Coverage   88.72%   88.88%   +0.16%     
==========================================
  Files         198      199       +1     
  Lines        7609     7685      +76     
  Branches     2094     2110      +16     
==========================================
+ Hits         6751     6831      +80     
+ Misses        858      854       -4     
Flag Coverage Δ
UnitTests 88.88% <96.05%> (+0.16%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
web3 ∅ <ø> (∅)
web3-core ∅ <ø> (∅)
web3-errors ∅ <ø> (∅)
web3-eth ∅ <ø> (∅)
web3-eth-abi ∅ <ø> (∅)
web3-eth-accounts ∅ <ø> (∅)
web3-eth-contract ∅ <ø> (∅)
web3-eth-ens ∅ <ø> (∅)
web3-eth-iban ∅ <ø> (∅)
web3-eth-personal ∅ <ø> (∅)
web3-net ∅ <ø> (∅)
web3-providers-http ∅ <ø> (∅)
web3-providers-ipc ∅ <ø> (∅)
web3-providers-ws ∅ <ø> (∅)
web3-rpc-methods ∅ <ø> (∅)
web3-utils ∅ <ø> (∅)
web3-validator ∅ <ø> (∅)

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 16, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: d80a590
Status: ✅  Deploy successful!
Preview URL: https://8f397582.web3-js-docs.pages.dev
Branch Preview URL: https://wyatt-4-x-5927-eip-712.web3-js-docs.pages.dev

View logs

@spacesailor24
Copy link
Contributor Author

This is the only documented limitation of the EIP-712 library I copied code from. I've tested it without implementation and it still throws an error, but I've also test with ethers and it throws an error for the same reason:

eip712-ethers/node_modules/@ethersproject/logger/lib/index.js:238
        var error = new Error(message);
                    ^

Error: circular type reference to "Mail" (argument="types", value={"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"},{"name":"replyTo","type":"Mail"}]}, code=INVALID_ARGUMENT, version=hash/5.7.0)
    at Logger.makeError (eip712-ethers/node_modules/@ethersproject/logger/lib/index.js:238:21)
    at Logger.throwError (eip712-ethers/node_modules/@ethersproject/logger/lib/index.js:247:20)
    at Logger.throwArgumentError (eip712-ethers/node_modules/@ethersproject/logger/lib/index.js:250:21)
    at eip712-ethers/node_modules/@ethersproject/hash/lib/typed-data.js:199:28
    at Array.forEach (<anonymous>)
    at _loop_1 (eip712-ethers/node_modules/@ethersproject/hash/lib/typed-data.js:190:27)
    at new TypedDataEncoder (eip712-ethers/node_modules/@ethersproject/hash/lib/typed-data.js:215:13)
    at TypedDataEncoder.from (eip712-ethers/node_modules/@ethersproject/hash/lib/typed-data.js:358:16)
    at TypedDataEncoder.encode (eip712-ethers/node_modules/@ethersproject/hash/lib/typed-data.js:384:30)
    at eip712-ethers/index.js:118:49 {
  reason: 'circular type reference to "Mail"',
  code: 'INVALID_ARGUMENT',
  argument: 'types',
  value: {
    EIP712Domain: [
      { name: 'name', type: 'string' },
      { name: 'version', type: 'string' },
      { name: 'chainId', type: 'uint256' },
      { name: 'verifyingContract', type: 'address' }
    ],
    Person: [
      { name: 'name', type: 'string' },
      { name: 'wallet', type: 'address' }
    ],
    Mail: [
      { name: 'from', type: 'Person' },
      { name: 'to', type: 'Person' },
      { name: 'contents', type: 'string' },
      { name: 'replyTo', type: 'Mail' }
    ]
  }
}

Node.js v18.16.0

@spacesailor24 spacesailor24 marked this pull request as ready for review July 18, 2023 05:31
@spacesailor24 spacesailor24 changed the title Implement EIP712 RPC Wrapper Methods and getEncodedEip712Message Util Implement EIP712 RPC Wrapper Methods and getEncodedEip712Data Util Jul 18, 2023
@spacesailor24
Copy link
Contributor Author

Failing Build / Integration IPC (18, geth, ipc) (pull_request) test is unrelated to changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has a couple places where new Error('...') is used, I've opted not to wrap these as Web3Errors as done in the rest of the codebase. I'm choosing to do this so there's some flexibility when this code gets refactored when we implement on own ABI encoder to replace the use of ethers'. I've created #6291 to track this

@spacesailor24 spacesailor24 merged commit 933ef51 into 4.x Jul 20, 2023
@spacesailor24 spacesailor24 deleted the wyatt/4.x/5927-eip-712 branch July 20, 2023 00:20
This was referenced Aug 3, 2023
sanyu1225 pushed a commit to blocto/web3.js that referenced this pull request Aug 30, 2023
…eb3#6286)

* Add Eip712TypeDetails and Eip712TypedData interfaces

* Init signTypedData rpc method and test

* Init getEncodedEip712Message and test

* Init signTypedData methods and tests

* Add return type to signTypedData

* Add eth_signTypedData and eth_signTypedData_v4 to web3_eth_execution_api

* Add itIf to signTypedData integration test

* Replace use of Buffer.from with bytesToHex

* Add additional test cases

* Rename getEncodedEip712Message to getEncodedEip712Data

* Update CHANGELOGs and renamed some files

* Update packages/web3-rpc-methods/test/unit/eth_rpc_methods/sign_typed_data.test.ts
sanyu1225 pushed a commit to blocto/web3.js that referenced this pull request Aug 30, 2023
…eb3#6286)

* Add Eip712TypeDetails and Eip712TypedData interfaces

* Init signTypedData rpc method and test

* Init getEncodedEip712Message and test

* Init signTypedData methods and tests

* Add return type to signTypedData

* Add eth_signTypedData and eth_signTypedData_v4 to web3_eth_execution_api

* Add itIf to signTypedData integration test

* Replace use of Buffer.from with bytesToHex

* Add additional test cases

* Rename getEncodedEip712Message to getEncodedEip712Data

* Update CHANGELOGs and renamed some files

* Update packages/web3-rpc-methods/test/unit/eth_rpc_methods/sign_typed_data.test.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EIP 712 method eth_signTypedData not found in web3.js
3 participants