1

I'm using web3js, and I'm trying to sign a tuple correctly.

Below you can see the solidity code for verification. I'm using draft-EIP712Upgradeable.sol

struct NFTVoucher {
    uint256 tokenId;
    uint256 minPrice; // in wei
    string uri;       // IPFS uri of metadata file
    bytes signature;  // the EIP-712 signature of all other fields in the NFTVoucher struct.
}

function _verify(NFTVoucher calldata voucher) public view returns (address) {
    bytes32 digest = _hash(voucher);
    return ECDSAUpgradeable.recover(digest, voucher.signature);
}

function _hash(NFTVoucher calldata voucher) internal view returns (bytes32) {
     return _hashTypedDataV4(keccak256(abi.encode(
         keccak256("NFTVoucher(uint256 tokenId,uint256 minPrice,string uri)"),
         voucher.tokenId,
         voucher.minPrice,
         keccak256(bytes(voucher.uri))
     )));
}

The frontend is built on ReactJs, web3js. The code you can find below 👇

let newHash = web3.utils.soliditySha3(
  web3.eth.abi.encodeParameters(
    [
      'uint256',
      'uint256',
      'string'
    ], 
    [
      tokenId,
      priceNumber,
      imageURL
    ]
  )
)

let sig = await web3.eth.sign(newHash, account); // "account" is the address of signer

So how should I change in the frontend code to pass the verification? If you need any other information please tell me. Any help is welcome. Thank youu!✌

5

1 Answer 1

1
+50

Here is the solution guys:

let msgHash = web3.utils.soliditySha3(
    { type: "uint256", value: tokenId },
    { type: "uint256", value: priceNumber },
    { type: "string", value: imageURL }
)

let sig = await web3.eth.sign(msgHash, account);

:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.