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

improved eip712 support #149

Merged
merged 11 commits into from
Dec 13, 2024
Merged

improved eip712 support #149

merged 11 commits into from
Dec 13, 2024

Conversation

pkieltyka
Copy link
Member

No description provided.

@pkieltyka
Copy link
Member Author

import { ethers } from 'ethers'

const mnemonic = 'dose weasel clever culture letter volume endorse used harvest ripple circle install'

async function main() {
  const wallet = ethers.Wallet.fromPhrase(mnemonic)
  console.log('wallet address:', wallet.address)

  const typedData = {
    "types": {
      // "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"},
        {"name": "count", "type": "uint8"}
      ]
    },
    "primaryType": "Person",
    "domain": {
      "name": "Ether Mail",
      "version": "1",
      "chainId": 1,
      "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
    },
    "message": {
      "name": "Bob",
      "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
      "count": 4
    }
  }

  const signature = await wallet.signTypedData(typedData.domain, typedData.types, typedData.message)
  console.log('signature:', signature)

  // verify signature
  const recoveredAddress = ethers.verifyTypedData(typedData.domain, typedData.types, typedData.message, signature)
  console.log('recovered address:', recoveredAddress)
}

main()

output:

wallet address: 0xb59ba5A13f0fb106EA6094a1F69786AA69be1424
signature: 0x296c98bed8f3fd7ea96f55ca8148b4d092cbada953c8d9205b2fff759461ab4e6d6db0b78833b954684900530caeee9aaef8e42dfd8439a3fa107e910b57e2cc1b
recovered address: 0xb59ba5A13f0fb106EA6094a1F69786AA69be1424

comparison test to confirm encoding and signature compute with a ethcoder typed_data_test.go

@pkieltyka pkieltyka merged commit 404855b into master Dec 13, 2024
8 checks passed
@pkieltyka pkieltyka deleted the typed-data-improvements branch December 13, 2024 18:20
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.

1 participant