Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast fails when data contains Digital Signature #43

Open
marius-yknotocs-com opened this issue May 25, 2020 · 2 comments
Open

Broadcast fails when data contains Digital Signature #43

marius-yknotocs-com opened this issue May 25, 2020 · 2 comments

Comments

@marius-yknotocs-com
Copy link

string signature = "SIG_K1_JyngtE5o5zqFGxNQZfQ7CWsvG4wfPfrxs8Z7YDTNtEGHos8Pso9z2p4EHBoNhd3UcBu7zn5iTxASfPfbAbcAdJCdoLxbAS";

var MyAction1 = new EosSharp.Core.Api.v1.Action() {
                    account = "zar",
                    authorization = new List<PermissionLevel>() { new PermissionLevel() {
                      actor = "mycontract", permission = "active" }
                    },
                    name = "dummyaction",
                    data = new {
                        sign = signature
                    }
                };

Packed data for above

1f9dcb5e92bf8736e53c0000000001000000000000aef900267519192fa54e01000000000000aef90000b86ae1696ede41001f22a5ee328c54421bddb73110f6ddce3258e214c611aac33a1532affb3adcc2bd3f3276e5223189fa58cdd0a9cb054d1360e26d2a24047e6628392a68d9dd9900

Transcoding the signature to hex

1f22a5ee328c54421bddb73110f6ddce3258e214c611aac33a1532affb3adcc2bd3f3276e5223189fa58cdd0a9cb054d1360e26d2a24047e6628392a68d9dd9920

The last byte is 0x00, should be 0x20

Confirm using commandline (cleos)

{
"expiration": "2020-05-25T10:13:38",
"ref_block_num": 47374,
"ref_block_prefix": 2492603937,
"max_net_usage_words": 0,
"max_cpu_usage_ms": 0,
"delay_sec": 0,
"context_free_actions": [],
"actions": [{
"account": "zar",
"name": "dummyaction",
"authorization": [{
"actor": "mycontract",
"permission": "active"
}
],
"data": "001f22a5ee328c54421bddb73110f6ddce3258e214c611aac33a1532affb3adcc2bd3f3276e5223189fa58cdd0a9cb054d1360e26d2a24047e6628392a68d9dd9920"
}
],
"transaction_extensions": [],
"signatures": [],
"context_free_data": []
}

It appears that packing of the transaction is dropping the last byte of the signature?

I use the following method for signing a hash of data (the resulting sig seems to be valid):

        public static string SignHash(byte[] hash, string privateKey) {
            var sign = Secp256K1Manager.SignCompressedCompact(hash, EosSharp.Core.Helpers.CryptoHelper.GetPrivateKeyBytesWithoutCheckSum(privateKey));
            var check = new List<byte[]>() { sign, Encoding.UTF8.GetBytes("K1") };
            var checksum = Ripemd160Manager.GetHash(EosSharp.Core.Helpers.SerializationHelper.Combine(check)).Take(4).ToArray();
            return "SIG_K1_" + Base58.Encode(EosSharp.Core.Helpers.SerializationHelper.Combine(new List<byte[]>() { sign, checksum }));
        }
@marius-yknotocs-com
Copy link
Author

SignStringToBytes (CryptoHelper.cs) writes out 64 bytes (SIGN_KEY_DATA_SIZE = 64) but the signature = 65 bytes - is that maybe the cause?

@marius-yknotocs-com
Copy link
Author

marius-yknotocs-com commented May 26, 2020

As a test, I changed the WriteSignaturemethod in AbiSerilizationProvider.cs as follows (CryptoHelper.SIGN_KEY_DATA_SIZE + 1) and that seems to fix the issue ... but will probably break for other stuff (r1 ?)

    private static void WriteSignature(MemoryStream ms, object value)
    {
        var s = (string)value;
        var signBytes = CryptoHelper.SignStringToBytes(s);
        
        if (s.StartsWith("SIG_K1_"))
            WriteByte(ms, KeyType.k1);
        else if (s.StartsWith("SIG_R1_"))
            WriteByte(ms, KeyType.r1);

        ms.Write(signBytes, 0, CryptoHelper.SIGN_KEY_DATA_SIZE + 1);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant