Solidity
簽名總是 132 個字元而不是 65 個字元
我難住了。我正在嘗試使用 ECSDA / keccak256 創建簽名。在 Solidity 中,恢復方法需要 65 個字元。我的集成程式碼 (.net C#) 中的簽名。我不知道我的邏輯有什麼問題。有什麼想法嗎?我寫了一個單元測試來幫助我測試一些不同的東西,但它總是 132 個字元。有什麼建議麼?
[Fact] public void AbleToSignAndVerify() { // Arrange var abiEncode = new ABIEncode(); var privateKey = "0x0000000000000000000000000000000000000000"; var publicKey = "0x0000000000000000000000000000000000000000"; var senderAddress = "0x0000000000000000000000000000000000000000"; var textToHash = "\x19Ethereum Signed Message:\n" + senderAddress.Length + senderAddress; //var msgBytes = Encoding.UTF8.GetBytes(textToHash); var encodedValue = abiEncode.GetABIEncodedPacked(textToHash); byte[] msgHash = new Sha3Keccack().CalculateHash(encodedValue); var signer1 = new EthereumMessageSigner(); var signer2 = new EthereumMessageSigner(); // Act var signature1 = signer1.Sign(msgHash, new EthECKey(privateKey)); var length = signature1.Length; var addressRec2 = signer2.EcRecover(msgHash, signature1); var signatureByteArray = Encoding.UTF8.GetBytes(signature1); var baLength = signatureByteArray.Length; var signatureHex = signatureByteArray.ToHex(); var shLength = signatureHex.Length; var signatureHexCompact = signatureByteArray.ToHexCompact(); var shcLength = signatureHexCompact.Length; // Assert Assert.Equal(publicKey, addressRec2); }
對於未來的人,評論中的伊斯梅爾是正確的。我也有編碼和散列問題。此解決方案取決於 .net Nethereum 庫。注意事項 - 我的數據類型是發件人地址,因此 ABIValue TYPE 設置為“地址”。這產生了巨大的變化。
private byte[] GetSignature(string privateKey, string textToHash) { var abiEncode = new ABIEncode(); var abiValue = new ABIValue("address", textToHash); var encodedValue = abiEncode.GetABIEncodedPacked(abiValue); var hashing = new Sha3Keccack(); byte[] msgHash = hashing.CalculateHash(encodedValue); var key = new EthECKey(privateKey); var signer = new EthereumMessageSigner(); var signature = signer.SignAndCalculateV(msgHash, key); var signatureByteArray = signature.To64ByteArray(); return signatureByteArray; }