Fail with error 'ECDSA: invalid signature length'

Hello everyone I tried several times to sign a message but I end up with this error Fail with error 'ECDSA: invalid signature length' within the transaction on the BSC Scan (https://testnet.bscscan.com/tx/0xfda2c59841fdf3401499cf304bcb8d158fb4e08c58369fdbd5f0781b5a1e88e5). Here is the code I use to sign the transaction:

private static async Task<byte[]> signMessage(string ethAddress, int nonce, int finalScore)
        {
            try
            {
                string message = ethAddress + nonce + finalScore;
               string signature = await WalletConnect.ActiveSession.EthPersonalSign(ethAddress, message);
                return Encoding.UTF8.GetBytes(signature);
            }
            catch (Exception e)
            {
                // await WalletConnect.ActiveSession.Disconnect();
                throw new Exception(e.Message);
            }
        }

Discussion, suggestions and corrections are all welcomed.

string signature = await WalletConnect.ActiveSession.EthPersonalSign(ethAddress, message);
return Encoding.UTF8.GetBytes(signature);

Which documentation and/or tutorial did you get this code from? E.g. the Moralis docs gives signature = await _walletConnect.Session.EthPersonalSign(address, signMessage);.

Maybe it’s due to how it’s returned.

To make the discussion clear, I want to sign a custom message within the unity and metamask. That’s how I ended with that chunk of code. Is there an effective way to sign a custom message in unity using moralis sdk?

You can try to validate that signature directly before sending it to the contract too, to see if everything seems ok.

Does moralis support that because if I do custom validation I need signer’s private key which actually in real sense no wallet will make private key inaccessible. I’m right?

you can validate the signature without knowing the private key, that is how you will validate it in the smart contract too

My bad I misinterpreted validation and verification. Let me try this out. But I think I will not be able to send a transaction, why, because EthPersonalSign returns a string which i convert it to byte array whose length is 132 but if am not wrong it needs to be 130. Since the string is in hex it is prefixed with 0x which when removed still I get the same error. Hence, is there another method to sign a custom message?

what do you want to do? it is a difference in signing a transaction and signing a message with personal sign

I want to do this in C# using moralis sdk.

this may be a little more complicated, to use that web3.eth.accounts.sign, it may trigger a warning in metamask when trying to sign a message that way

you are using personal sign that it is a different thing, you can check in a contract the result from personal sign too

I know it triggers a warning. I Just want to use this web3.eth.accounts.sign how can I achieve that.

I don’t know how to do it in C#, it should be possible

Ooh! Okay, thank you though for your clarity.

@alex, do have any idea how I can implement this in C#?

Don’t know sorry, you may get some answers on our Discord server in the #unity-help channel if you’re not there already.

Did you check the length of the signature from _walletConnect.Session.EthPersonalSign(address, signMessage); and find out what it needs to be for the transaction to be valid? You can try to find out what’s happening behind the scenes in the base SDK.