无法在 Solidity 上重新创建链下签名消息
Can't recreate off chain signed message on Solidity
我在我的服务器上使用 Nethereum 对消息进行签名,如下所示:
string amount = "10";
string nonce = "1";
string msg1 = "asdasd";
var privateKey = "0x00000privateKey";
var signer1 = new EthereumMessageSigner();
var abiEnconde = new ABIEncode();
var byteArray = abiEnconde.GetABIEncodedPacked(msg1,amount, nonce);
var messageHashed = "0x" + Convert.ToHexString(byteArray).ToLower();
var signature1 = signer1.Sign(byteArray, new EthECKey(privateKey));
signature
结果很完美,当我尝试重新创建消息时,问题就来了,如下所示:
function VerifyMessage(bytes memory sig,string memory msg1, string memory amount, string memory nonce) public pure returns (address) {
(uint8 _v, bytes32 _r, bytes32 _s) = splitSignature(sig);
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHashMessage = keccak256(abi.encodePacked(prefix,msg1,amount,nonce));
address signer = ecrecover(prefixedHashMessage, _v, _r, _s);
return signer;
}
signer
不是我在链下与 Nethereum 签署的钱包,但是,如果我尝试从服务器传递 messageHashed
而不是重新创建消息,它会完美地工作,所以我认为问题出在 solidity 函数上。
例如:
bytes32 prefixedHashMessage = keccak256(abi.encodePacked(prefix,HASHED-MSG-FROM-NETHEREUM));
这很好用。
这就是我的工作方式
var abiEnconde = new ABIEncode();
var resultHash = abiEnconde.GetSha3ABIEncodedPacked(
new ABIValue("address", msg1),
new ABIValue("uint256", amount),
new ABIValue("uint256", nonce));
var messageHashed = "0x" + Convert.ToHexString(resultHash).ToLower();
var signature1 = signer1.Sign(resultHash, new EthECKey(privateKey));
并且在坚固中:
function getMessageHash(address add,uint256 amount,uint256 nonce ) public pure returns (bytes32) {
return keccak256(abi.encodePacked(add,amount,nonce));
}
并恢复签名者>
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHashMessage = keccak256(abi.encodePacked(prefix,getMessageHash(msg1,amount,nonce)));
address signer = ecrecover(prefixedHashMessage, _v, _r, _s);
我在我的服务器上使用 Nethereum 对消息进行签名,如下所示:
string amount = "10";
string nonce = "1";
string msg1 = "asdasd";
var privateKey = "0x00000privateKey";
var signer1 = new EthereumMessageSigner();
var abiEnconde = new ABIEncode();
var byteArray = abiEnconde.GetABIEncodedPacked(msg1,amount, nonce);
var messageHashed = "0x" + Convert.ToHexString(byteArray).ToLower();
var signature1 = signer1.Sign(byteArray, new EthECKey(privateKey));
signature
结果很完美,当我尝试重新创建消息时,问题就来了,如下所示:
function VerifyMessage(bytes memory sig,string memory msg1, string memory amount, string memory nonce) public pure returns (address) {
(uint8 _v, bytes32 _r, bytes32 _s) = splitSignature(sig);
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHashMessage = keccak256(abi.encodePacked(prefix,msg1,amount,nonce));
address signer = ecrecover(prefixedHashMessage, _v, _r, _s);
return signer;
}
signer
不是我在链下与 Nethereum 签署的钱包,但是,如果我尝试从服务器传递 messageHashed
而不是重新创建消息,它会完美地工作,所以我认为问题出在 solidity 函数上。
例如:
bytes32 prefixedHashMessage = keccak256(abi.encodePacked(prefix,HASHED-MSG-FROM-NETHEREUM));
这很好用。
这就是我的工作方式
var abiEnconde = new ABIEncode();
var resultHash = abiEnconde.GetSha3ABIEncodedPacked(
new ABIValue("address", msg1),
new ABIValue("uint256", amount),
new ABIValue("uint256", nonce));
var messageHashed = "0x" + Convert.ToHexString(resultHash).ToLower();
var signature1 = signer1.Sign(resultHash, new EthECKey(privateKey));
并且在坚固中:
function getMessageHash(address add,uint256 amount,uint256 nonce ) public pure returns (bytes32) {
return keccak256(abi.encodePacked(add,amount,nonce));
}
并恢复签名者>
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHashMessage = keccak256(abi.encodePacked(prefix,getMessageHash(msg1,amount,nonce)));
address signer = ecrecover(prefixedHashMessage, _v, _r, _s);