0

I am stumped. I am trying to create a signature using ECSDA / keccak256. In Solidity the recover method requires 65 characters. My signatures from my integrating code (.net C#). I don't know what is wrong my logic. Any thoughts? I wrote a unit test to help me test some various things but it's always 132 characters. Any suggestions?

    [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);

    }
3
  • I apologize for lack of clarity. Have Covid while trying to figure this out. I can share snippets of other code as needed.
    – 0xElGato
    Jan 6, 2022 at 2:51
  • 2
    In solidity a signature is 65 bytes, if you wrote those bytes as hexadecimal string you will have 2 characters per byte so 130 characters, plus 2 for the '0x' prefix. So 132 hexadecimal characters for a signature are equivalent to 65 bytes in solidity.
    – Ismael
    Jan 6, 2022 at 4:30
  • Thank you! Having hard time processing things this morning pre-caffeine and with this Covid. Let me process this and get back to you!
    – 0xElGato
    Jan 6, 2022 at 14:02

1 Answer 1

1

For future people, Ismael in the comments was correct. I also had encoding and hashing problems. This solution depends on the .net Nethereum library. Item of note - my data type was the sender address, so the ABIValue TYPE was set to "address". This made a huge difference.

    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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.