我正在使用web3js,并且我试图正确地签署一个元组。
下面您可以看到用于验证的可靠代码。我用的是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))
)));
}前端构建在ReactJs, web3js上。下面的代码
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那么,我应该如何更改前端代码以通过验证呢?如果您需要其他信息,请告诉我。欢迎任何帮助。谢谢你✌
发布于 2022-06-06 09:10:10
下面是解决方案,伙计们:
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);:)
https://ethereum.stackexchange.com/questions/118928
复制相似问题