我正在与EIP712签署订单,并且在签署uint256时实现了恢复数据,只要我使用uint256[],它就会恢复到错误的地址。
有人能帮忙吗?
下面是创建签名(Web3js)的javascript方面的内容:
var domain = [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
{ name: "salt", type: "bytes32" }
];
var sellOrders = [
{ name: "id", type: "uint256[]" },
{ name: "tokenId", type: "uint256[]" },
{ name: "price", type: "uint256[]" },
{ name: "proto", type: "uint256[]" },
{ name: "purity", type: "uint256[]" },
{ name: "seller", type: "address" }
];
const domainData = {
name: "app",
version: "1",
chainId: 3,
verifyingContract: cardExchangeContract,
salt: "0xa222082684812afae4e093416fff16bc218b569abe4db590b6a058e1f2c1cd3e"
};
var message = {
id: [1],
tokenId: [1],
price: [1],
proto: [1],
purity: [1],
seller: address
};
var data = JSON.stringify({
types: {
EIP712Domain: domain,
SellOrders: sellOrders,
},
domain: domainData,
primaryType: "SellOrders",
message: message
});
window.web3.currentProvider.sendAsync({
method: "eth_signTypedData_v4",
params: [address, data],
from: address
}, function(error, result) {
if (error) {
errorCallback();
} else {
const signature = result.result.substring(2);
const r = "0x" + signature.substring(0, 64);
const s = "0x" + signature.substring(64, 128);
const v = parseInt(signature.substring(128, 130), 16);
successCallback(signature, r, s, v);
}
});这是事物坚实的一面:
string private constant domain = "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)";
bytes32 public constant domainTypeHash = keccak256(abi.encodePacked(domain));
string private constant sellOrdersType = "SellOrders(uint256[] id,uint256[] tokenId,uint256[] price,uint256[] proto,uint256[] purity,address seller)";
bytes32 public constant sellOrdersTypeHash = keccak256(abi.encodePacked(sellOrdersType));
uint256 constant chainId = 3;
bytes32 constant salt = 0xa222082684812afae4e093416fff16bc218b569abe4db590b6a058e1f2c1cd3e;
bytes32 private domainSeparator;
struct SellOrders {
uint256[] id;
uint256[] tokenId;
uint256[] price;
uint256[] proto;
uint256[] purity;
address payable seller;
}
constructor() public {
domainSeparator = keccak256(abi.encode(
domainTypeHash,
keccak256("app"),
keccak256("1"), // version
chainId,
this,
salt
));
}
function recover(uint256[] calldata id, uint256[] calldata tokenId, uint256[] calldata price, uint256[] calldata proto, uint256[] calldata purity, address seller, uint8 v, bytes32 r, bytes32 s) external view returns (address) {
return _recover(id, tokenId, price, proto, purity, seller, v, r, s);
}
function _recover(uint256[] memory id, uint256[] memory tokenId, uint256[] memory price, uint256[] memory proto, uint256[] memory purity, address seller, uint8 v, bytes32 r, bytes32 s) private view returns (address) {
return ecrecover(hashSellOrders(id, tokenId, price, proto, purity, seller), v, r, s);
}
function hashSellOrders(uint256[] memory id, uint256[] memory tokenId, uint256[] memory price, uint256[] memory proto, uint256[] memory purity, address seller) private view returns (bytes32){
return keccak256(abi.encodePacked(
"\x19\x01",
domainSeparator,
keccak256(abi.encode(
sellOrdersTypeHash,
id,
tokenId,
price,
proto,
purity,
seller
))
));
}当我将sellOrders属性更改为uint256而不是uint256[]并提供uint256s而不是数组时,它将按预期工作,并恢复正确的地址。当我将其设置为uint256[]并提供数组时,它会恢复到错误的地址。
编辑/添加
当使用uint256 (没有数组)时,实际工作的代码片段:
Javascript (web3js/Metamask):
var sellOrders = [
{ name: "id", type: "uint256" },
{ name: "tokenId", type: "uint256" },
{ name: "price", type: "uint256" },
{ name: "proto", type: "uint256" },
{ name: "purity", type: "uint256" },
{ name: "seller", type: "address" }
];
var message = {
id: 1,
tokenId: 1,
price: 1,
proto: 1,
purity: 1,
seller: address
};稳固性:
string private constant sellOrdersType = "SellOrders(uint256 id,uint256 tokenId,uint256 price,uint256 proto,uint256 purity,address seller)";
struct SellOrders {
uint256 id;
uint256 tokenId;
uint256 price;
uint256 proto;
uint256 purity;
address payable seller;
}
function recover(uint256 id, uint256 tokenId, uint256 price, uint256 proto, uint256 purity, address seller, uint8 v, bytes32 r, bytes32 s) external view returns (address) {
return _recover(id, tokenId, price, proto, purity, seller, v, r, s);
}
function _recover(uint256 id, uint256 tokenId, uint256 price, uint256 proto, uint256 purity, address seller, uint8 v, bytes32 r, bytes32 s) private view returns (address) {
return ecrecover(hashSellOrders(id, tokenId, price, proto, purity, seller), v, r, s);
}
function hashSellOrders(uint256 id, uint256 tokenId, uint256 price, uint256 proto, uint256 purity, address seller) private view returns (bytes32){
return keccak256(abi.encodePacked(
"\x19\x01",
domainSeparator,
keccak256(abi.encode(
sellOrdersTypeHash,
id,
tokenId,
price,
proto,
purity,
seller
))
));
}添加了2 --我上一次的研究让我了解了Metamask在客户端实现编码/散列的方式,但是为了正确地恢复它,我仍然无法在实体契约中再现它。
发布于 2019-10-07 11:25:21
来自https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md#definition-of-encodedata
数组值被编码为其内容的级联
encodeData的encodeData散列(即SomeType[5]的编码与包含五个类型SomeType成员的结构的编码相同)。
因此,我认为这应该是可行的(完全未经测试):
function hashSellOrders(uint256[] memory id, uint256[] memory tokenId, uint256[] memory price, uint256[] memory proto, uint256[] memory purity, address seller) private view returns (bytes32){
return keccak256(abi.encodePacked(
"\x19\x01",
domainSeparator,
keccak256(abi.encode(
sellOrdersTypeHash,
keccak256(abi.encodePacked(id)),
keccak256(abi.encodePacked(tokenId)),
keccak256(abi.encodePacked(price)),
keccak256(abi.encodePacked(proto)),
keccak256(abi.encodePacked(purity)),
seller
))
));
}发布于 2022-01-18 21:25:37
若要添加所选的答案,
如果您有一个任意大小的数据数组,您必须首先使用keccack256,即下面的数据是bytes[]
bytes32[] memory keccakData = new bytes32[](data.length);
for (uint256 i = 0; i < data.length; i++) {
keccakData[i] = keccak256(data[i]);
}然后,您可以使用其余的编码来执行keccak256(abi.encodePacked(keccakData))。
https://stackoverflow.com/questions/58257459
复制相似问题