作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正尝试在前端使用 _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);
我将凭证和签名存储在 mongo 数据库中,我在 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
)
)
);
}
引用以上_hashTypedDataV4
最佳答案
这是erc20permit的例子,希望对你有帮助
function permit(address owner, address spender, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public virtual override {
require(deadline >= block.timestamp, "ERC20Permit: expired deadline");
bytes32 hashStruct = keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
owner,
spender,
amount,
nonce[owner],
deadline
)
);
bytes32 hash = keccak256(
abi.encodePacked(
'\x19\x01',
keccak256(abi.encode( keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name_)),
keccak256(bytes(version())),
chainId,
address(this)
),
hashStruct
)
);
address signer = ecrecover(hash, v, r, s);
require(
signer != address(0) && signer == owner,
"ERC20Permit: invalid signature"
);
nonces[owner]++;
_approve(owner, spender, amount);
}
关于ethereum - 在智能合约中使用 _hashTypedDataV4 并在前端使用 _signTypedData(etherjs) 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71732049/
我正尝试在前端使用 _signTypedData(etherjs) 对 nft 数据进行编码,如下所示 const domain = { name: "og-nft", versio
我正在尝试在 React-Native 环境中使用 Ether.js 来发送以太币。我遵循了 Etherjs 指南中的示例。 这是我的代码: let privateKey = 'walletPriva
我是一名优秀的程序员,十分优秀!