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

account.signTypedData() for @celo/viem-account-ledger #505

Conversation

jbrower95
Copy link
Contributor

@jbrower95 jbrower95 commented Jan 12, 2025

Description

  • Needed this for my own work. viem-account-ledger was the nicest wrapper for Viem I could find, and everything else is Ethers or lower-level.
  • Ledger requires the domain seperator to be passed in explicitly, so you need to hash things independently (i.e you cannot use Viem's hashTypedData()).

Tested

  • Tested this onchain and got valid signatures back. I'm using it in my own work to sign a Gnosis transaction hash, and submit onchain to approve the transaction. I successfully approved a transaction hash using this.
  • If you guys want to copy this branch and add unit tests that's totally fine. Just wanted to make this easier for ya'll to integrate.

PR-Codex overview

This PR enhances the ledgerToAccount functionality by implementing the signTypedData method for EIP-712 typed data signing, incorporating new utility functions from viem and defining an interface for message properties.

Detailed summary

  • Added imports for getTypesForEIP712Domain, hashDomain, hashStruct, HashTypedDataParameters from viem.
  • Defined a new interface MessageTypeProperty.
  • Implemented the signTypedData method to handle EIP-712 signing.
  • Utilized hashDomain and hashStruct to compute hashes for the signing process.
  • Used serializeSignature to format the signature before returning.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

@jbrower95 jbrower95 requested a review from a team as a code owner January 12, 2025 20:36
Copy link

changeset-bot bot commented Jan 12, 2025

⚠️ No Changeset found

Latest commit: df1e07c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@jbrower95 jbrower95 changed the title Typed data support for viem-account-ledger account.signTypedData() for @celo/viem-account-ledger Jan 12, 2025
@aaronmgdr
Copy link
Member

thanks for the contribution. will look at it closely soon!

@aaronmgdr aaronmgdr added this to the 5 - Celo MVP Mainnet milestone Jan 13, 2025
@aaronmgdr aaronmgdr self-assigned this Jan 13, 2025
@nicolasbrugneaux nicolasbrugneaux self-assigned this Jan 21, 2025
@nicolasbrugneaux nicolasbrugneaux changed the base branch from master to nicolasbrugneaux/sign-typed-data January 21, 2025 09:57
@nicolasbrugneaux nicolasbrugneaux merged commit 3495cad into celo-org:nicolasbrugneaux/sign-typed-data Jan 21, 2025
3 checks passed
@jbrower95
Copy link
Contributor Author

can you comment / post which version on npm this goes out in? Would love to delete my local code and use this instead. Thanks

nicolasbrugneaux added a commit that referenced this pull request Jan 24, 2025
> [!NOTE]
>  Originally opened by @jbrower95
---

### Description
- Needed this for my own work. `viem-account-ledger` was the nicest
wrapper for Viem I could find, and everything else is Ethers or
lower-level.
- Ledger requires the domain seperator to be passed in explicitly, so
you need to hash things independently (i.e you cannot use Viem's
`hashTypedData()`).

### Tested

- Tested this onchain and got valid signatures back. I'm using it in my
own work to sign a Gnosis transaction hash, and submit onchain to
approve the transaction. I successfully approved a transaction hash
using this.
- If you guys want to copy this branch and add unit tests that's totally
fine. Just wanted to make this easier for ya'll to integrate.

<!-- start pr-codex -->

---

## PR-Codex overview
This PR primarily focuses on enhancing the `LedgerSigner` and
`LedgerWallet` classes by adding an `appName` property to their
configurations and implementing logic to handle different ledger
applications (Celo and Ethereum) effectively.

### Detailed summary
- Added `appName` property to `appConfiguration` in `LedgerSigner`.
- Updated constructor to initialize `appName` with a default value.
- Implemented validation logic in `signTypedData` and `signTransaction`
methods based on `appName`.
- Created `readAppName` function to fetch the app name from the ledger
device.
- Introduced tests for `readAppName` and various transaction signing
scenarios.
- Updated documentation to include new `test-utils` module and its
contents.

> The following files were skipped due to too many changes:
`packages/viem-account-ledger/src/ledger-to-account.test.ts`

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->

### Description

_Tell us why these changes are being made_

#### Other changes

_Describe any minor or "drive-by" changes here. If none delete this
section_

### Tested

_An explanation of how the changes were tested or an explanation as to
why they don't need to be._


### How to QA

_List of steps required to QA the change if applicable_

### Related issues

- Fixes #[issue number here]


<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on enhancing the `ledgerToAccount` functionality by
implementing the `signTypedData` method, which allows for signing
EIP-712 typed data using the Ledger device. It also adds new utility
functions for handling EIP-712 domains and structures.

### Detailed summary
- Added imports for `getTypesForEIP712Domain`, `hashDomain`,
`hashStruct`, and `HashTypedDataParameters` from `viem`.
- Introduced `MessageTypeProperty` interface for type safety.
- Implemented the `signTypedData` method to sign EIP-712 typed data.
- Utilized `hashDomain` and `hashStruct` for message hashing.
- Updated the return statement to serialize the signature.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

---------

Co-authored-by: Jay <jbrower95@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants