在智能合约中使用_hashtypeddatav4和_signtypeddata(Etherjs)在前端不起作用
我正在尝试使用_signtypeddata(Etherjs)在前端进行编码NFT数据,如下
const domain = {
name: "og-nft",
version: "1",
};
const types = {
Nft: [
{ name: "URI", type: "string" },
{ name: "price", type: "uint256" },
],
};
// The data to sign
const [voucher, setVoucher] = useState({
URI: "",
price: '1',
});
const signature = await signer._signTypedData(domain, types, voucher);
我在蒙古签名和签名数据库,我已经在Hardhat上部署了智能合约,并且通过使用ECDSA.Recover凝视签名签名者来验证签名的真实性,
function verifyVoucher(NFTVoucher calldata voucher, bytes memory signature)
public
view
returns (address)
{
require(voucher.price > 0, "Price must be greater than 0");
// require(voucher.tokenId > 0, "Token ID must be greater than 0");
bytes32 hash = _hash(voucher);
//string memory hash="";
return ECDSA.recover(hash, signature);
}
但结果与实际签名者不匹配。我认为我在上面使用的哈希功能中犯了一些错误。
0xe8c795f9168269940b31a470ad82e89a453e88b9 signer
0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 owner
以下是哈希函数。
function _hash(NFTVoucher calldata voucher)
internal
view
returns (bytes32)
{
return
_hashTypedDataV4(
keccak256(
abi.encode(
keccak256(
"Nft(string URI,uint256 price)"
),
keccak256(bytes(voucher.URI)),
voucher.price
)
)
);
}
引用上述
i am trying to encode the nft data using _signTypedData(etherjs) in frontend as follows
const domain = {
name: "og-nft",
version: "1",
};
const types = {
Nft: [
{ name: "URI", type: "string" },
{ name: "price", type: "uint256" },
],
};
// The data to sign
const [voucher, setVoucher] = useState({
URI: "",
price: '1',
});
const signature = await signer._signTypedData(domain, types, voucher);
reference to above
_signTypedData in docs
I am storing the voucher and signature in the mongo database, I have deployed smart contract on hardhat and I am verifying the authenticity of signature by peering out the signer of the voucher using ECDSA.recover
function verifyVoucher(NFTVoucher calldata voucher, bytes memory signature)
public
view
returns (address)
{
require(voucher.price > 0, "Price must be greater than 0");
// require(voucher.tokenId > 0, "Token ID must be greater than 0");
bytes32 hash = _hash(voucher);
//string memory hash="";
return ECDSA.recover(hash, signature);
}
but the result of this is not matching with actual signer. i think I am making some mistake in the hash function above used.
0xe8c795f9168269940b31a470ad82e89a453e88b9 signer
0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 owner
below is the hash function.
function _hash(NFTVoucher calldata voucher)
internal
view
returns (bytes32)
{
return
_hashTypedDataV4(
keccak256(
abi.encode(
keccak256(
"Nft(string URI,uint256 price)"
),
keccak256(bytes(voucher.URI)),
voucher.price
)
)
);
}
reference to above _hashTypedDataV4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是erc20permit的例子,希望对你有帮助
this is erc20permit example,I hope it can help you