Exemplo n.º 1
0
        //private async Task<bool> Authenticated() {
        //    if (_registered) return true;

        //    var stringContent = new StringContent(JsonConvert.SerializeObject(new AuthenticationRequest(_orkId,_private.GetPublic().ToString())), Encoding.UTF8, "application/json");
        //    var response = (await _client.PostAsync("Authentication", stringContent));

        //    _registered = response.IsSuccessStatusCode;
        //    if (!_registered) Console.Write($"Ork was not authorized to write transaction. Error: {await response.Content.ReadAsStringAsync()}");

        //    return _registered;
        //}

        public async Task <(bool success, string error)> Post(string contract, string table, string scope, string index, object payload)
        {
            //if (!await Authenticated()) return (false,"not authenticated");
            var blockData = new Transaction(contract, table, scope, index, _orkId, payload);

            var serializedPayload = JsonConvert.SerializeObject(blockData.Data);

            blockData.Sign = _private.Sign(Encoding.UTF8.GetBytes(serializedPayload));

            var stringContent = new StringContent(JsonConvert.SerializeObject(blockData), Encoding.UTF8, "application/json");
            var response      = (await _client.PostAsync("Simulator", stringContent));

            if (response.IsSuccessStatusCode)
            {
                return(true, null);
            }
            return(false, await response.Content.ReadAsStringAsync());
        }
Exemplo n.º 2
0
        public static byte[] Encrypt(byte[] buffer, ulong tag, C25519Key key)
        {
            var toAsymmetricEncrypt = Pad32(buffer);
            var bufferSymmetric     = new byte[0];

            if (buffer.Length > 32)
            {
                var secret = new AesSherableKey();
                bufferSymmetric     = secret.Encrypt(buffer);
                toAsymmetricEncrypt = secret.ToByteArray();
            }

            var bufferAsymmetric = key.Encrypt(toAsymmetricEncrypt).ToByteArray();
            var tagBuffer        = BitConverter.GetBytes(tag);
            var signature        = key.Sign(bufferAsymmetric.Concat(tagBuffer).ToArray()).PadLeft(32 * 3);

            var size =
                bufferAsymmetric.Length +
                tagBuffer.Length +
                signature.Length +
                bufferSymmetric.Length;
            var dimension = DimensionBuffer(size);

            var all = new byte[1 + dimension.Length + size];

            all[0] = 1; // version #

            dimension.CopyTo(all, 1);
            var step = dimension.Length + 1;

            bufferAsymmetric.CopyTo(all, step);
            step += bufferAsymmetric.Length;

            tagBuffer.CopyTo(all, step);
            step += tagBuffer.Length;

            signature.CopyTo(all, step);
            step += signature.Length;

            bufferSymmetric.CopyTo(all, step);
            return(all);
        }