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

feat: Add EIP-712 management in prepareMessageToSign #892

Merged
merged 22 commits into from
Sep 7, 2022

Conversation

sprohaszka-ledger
Copy link
Contributor

@sprohaszka-ledger sprohaszka-ledger commented Aug 9, 2022

Signed-off-by: Stéphane Prohaszka [email protected]

📝 Description

Add EIP-712 support on WebPlatformPlayer.
Rework some unused properties in TypesMessageData and update impacted WalletConnect current implementation.

❓ Context

  • Impacted projects: common ledgerjs-eth
  • Linked resource(s): LIVE-3317

✅ Checklist

  • Test coverage
  • Atomic delivery
  • No breaking changes

📸 Demo

🚀 Expectations to reach

The goal, beside Platform capacity to manage EIP-712 call, is to remove unused code and keep eth logic inside dedicated modules (i.e. ledgerjs/package/hw-app/eth and common/families/etherereum).
Also, leverage as much as possible type to use typescript validation build system to check for unwanted behavior.

It can be tested by running LLD with EXPERIMENTAL_EIP712=1 and using the ethereum app available on provider 3

@vercel
Copy link

vercel bot commented Aug 9, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
live-common-tools ✅ Ready (Inspect) Visit Preview Sep 6, 2022 at 1:34PM (UTC)
3 Ignored Deployments
Name Status Preview Updated
ledger-live-github-bot ⬜️ Ignored (Inspect) Sep 6, 2022 at 1:34PM (UTC)
native-ui-storybook ⬜️ Ignored (Inspect) Sep 6, 2022 at 1:34PM (UTC)
react-ui-storybook ⬜️ Ignored (Inspect) Sep 6, 2022 at 1:34PM (UTC)

@changeset-bot
Copy link

changeset-bot bot commented Aug 9, 2022

🦋 Changeset detected

Latest commit: 909fef0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@ledgerhq/live-common Minor
@ledgerhq/hw-app-eth Patch
@ledgerhq/live-cli Patch
ledger-live-desktop Patch
live-mobile Patch
live-common-tools Patch

Not sure what this means? Click here to learn what changesets are.

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

@github-actions github-actions bot added common Has changes in live-common ledgerjs Has changes in the ledgerjs open source libs labels Aug 9, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 9, 2022

@sprohaszka-ledger

Screenshots: ✅

There are no changes in the screenshots for this PR. If this is expected, you are good to go.

Copy link
Contributor

@chabroA chabroA left a comment

Choose a reason for hiding this comment

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

No strong opinion on this rework overall

@chabroA
Copy link
Contributor

chabroA commented Aug 11, 2022

There seem to be an issue with the update in ledgerjs.
Looks like LLD is not able to import the isEIP712Message exported by the hw-app-eth package in ledgerjs

Uncaught SyntaxError: The requested module '[...]/ledger-live/libs/ledgerjs/packages/hw-app-eth/lib/modules/EIP712/index.js' does not provide an export named 'isEIP712Message'

@codecov
Copy link

codecov bot commented Aug 12, 2022

Codecov Report

Merging #892 (2fcda44) into develop (8fe91aa) will increase coverage by 0.03%.
The diff coverage is n/a.

❗ Current head 2fcda44 differs from pull request most recent head 909fef0. Consider uploading reports for the commit 909fef0 to get more accurate results

@@             Coverage Diff             @@
##           develop     #892      +/-   ##
===========================================
+ Coverage    47.82%   47.86%   +0.03%     
===========================================
  Files          650      650              
  Lines        29114    29146      +32     
  Branches      7529     7541      +12     
===========================================
+ Hits         13925    13951      +26     
- Misses       15131    15137       +6     
  Partials        58       58              
Flag Coverage Δ
test 47.86% <ø> (+0.03%) ⬆️

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

Impacted Files Coverage Δ
...ages/hw-app-eth/src/modules/EIP712/EIP712.utils.ts 93.75% <0.00%> (-6.25%) ⬇️
libs/ledgerjs/packages/hw-app-eth/src/Eth.ts 78.13% <0.00%> (ø)
libs/ledger-live-common/src/walletconnect/index.ts 93.87% <0.00%> (ø)
...r-live-common/src/ble/hooks/useBleDevicePairing.ts 100.00% <0.00%> (ø)
...ive-common/src/families/ethereum/hw-signMessage.ts 100.00% <0.00%> (ø)
...js/packages/hw-app-eth/src/modules/EIP712/index.ts 99.24% <0.00%> (+<0.01%) ⬆️
...er-live-common/src/hw/getOnboardingStatePolling.ts 92.85% <0.00%> (+0.12%) ⬆️
...live-common/src/ble/hooks/useBleDevicesScanning.ts 90.00% <0.00%> (+0.86%) ⬆️
...ibs/ledger-live-common/src/hw/signMessage/index.ts 30.88% <0.00%> (+3.19%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@chabroA
Copy link
Contributor

chabroA commented Aug 12, 2022

There seem to be an issue with the update in ledgerjs. Looks like LLD is not able to import the isEIP712Message exported by the hw-app-eth package in ledgerjs

Uncaught SyntaxError: The requested module '[...]/ledger-live/libs/ledgerjs/packages/hw-app-eth/lib/modules/EIP712/index.js' does not provide an export named 'isEIP712Message'

Fixed in 75cb580

Copy link
Contributor

@chabroA chabroA left a comment

Choose a reason for hiding this comment

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

Is there anything to be done on mobile side as well?

Signed-off-by: Stéphane Prohaszka <[email protected]>
Signed-off-by: Stéphane Prohaszka <[email protected]>
Signed-off-by: Stéphane Prohaszka <[email protected]>
Signed-off-by: Stéphane Prohaszka <[email protected]>
Signed-off-by: Stéphane Prohaszka <[email protected]>
Signed-off-by: Stéphane Prohaszka <[email protected]>
Signed-off-by: Stéphane Prohaszka <[email protected]>
Copy link
Contributor

@lambertkevin lambertkevin left a comment

Choose a reason for hiding this comment

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

Just a few optional comments, otherwise LGTM 🚀

import hwSignMessage from "./hw-signMessage";
import { EIP712Message } from "@ledgerhq/hw-app-eth/lib/modules/EIP712";
import testEIP712Message from "@ledgerhq/hw-app-eth/tests/sample-messages/0.json";
import ethSignMessage, { prepareMessageToSign } from "./hw-signMessage";
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess here we can just

import { signMessage, prepareMessageToSign } from "./hw-signMessage";

And replace all hwSignMessage by signMessage ?

Comment on lines +87 to +90
let parsedMessage = message;
if (typeof message === "string") {
parsedMessage = tryConvertToJSON(message);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this condition necessary ? JSON.parse({}) should throw and be catched and return the original message, so we might be able to update the typings of tryConvertToJSON to allow for more "all-in-one" behaviour ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

message can be of the type string or EIP712Message. In the latter case, we can't and don't want to try to convert it into a JSON.
I added typing info in the method to be clearer.

Copy link
Contributor

@lambertkevin lambertkevin Sep 7, 2022

Choose a reason for hiding this comment

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

What I meant was to potentially change tryConvertToJSON into something like this:

function tryConvertToEIP712(message: string | Record<string, any>): string | EIP712Message {
  const maybeParsedMessage = (() => {
    try {
      return JSON.parse(message as string) as Record<string, any>;
    } catch {
      return message;
    }
  })();
  
  return isEIP712Message(maybeParsedMessage)
    ? maybeParsedMessage 
    : message.toString();
}

So:

  • if message is a random string it would throw on parse and return message as a string
  • if message is a stringified random object it would fail on isEIP712Message and return message as a string
  • if message is an EIP712 stringified object it would return maybeParsedMessage as an EIP712Message
  • if message is an EIP712Message it would throw on parse, succeed on isEIP712Message and return maybeParsedMessage as an EIP712Message
    Only other adjustement to make would to add a type guard to isEIP712Message that the message param is an object first.

But again, I'm nit picking here, and it's just another abstraction, nothing major, feel free to ignore. 👍

Signed-off-by: Stéphane Prohaszka <[email protected]>
@Justkant
Copy link
Contributor

Justkant commented Sep 7, 2022

Works well with wallet-connect and same for EIP 191 messages

@sprohaszka-ledger sprohaszka-ledger merged commit d70bb70 into develop Sep 7, 2022
@sprohaszka-ledger sprohaszka-ledger deleted the feat/LIVE-3180-EIP-712 branch September 7, 2022 13:22
@desirendr desirendr added this to the Ledger Live Mobile 3.8.x milestone Sep 8, 2022
@desirendr desirendr added translations Translation files have been touched and removed translations Translation files have been touched labels Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common Has changes in live-common ledgerjs Has changes in the ledgerjs open source libs mobile Has changes in LLM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants