Ejemplo n.º 1
0
        public async Task SafeSetRawAsync(string key, byte[] value)
        {
            var root = this.getActiveDatabaseRoot();

            var request = new SafeSetOptions()
            {
                Kv = new KeyValue()
                {
                    Key   = ByteString.CopyFromUtf8(key),
                    Value = ByteString.CopyFrom(value)
                },

                RootIndex = new Index()
                {
                    Index_ = root.Index
                }
            };

            var proof = await this.client.SafeSetAsync(request, this.getSecurityHeader());

            var item = new Item()
            {
                Index = proof.Index,
                Key   = request.Kv.Key,
                Value = request.Kv.Value
            };

            CryptoUtils.Verify(proof, item, root);

            this.rootHolder.SetRoot(this.activeDatabaseName, new Root()
            {
                Root_ = proof.Root, Index = proof.At
            });
        }
Ejemplo n.º 2
0
 public SafeSetResponse SafeSet(SafeSetOptions request)
 {
     return(Handler.SafeSet.Call(this.instance, this.rootService, request));
 }
Ejemplo n.º 3
0
        public static SafeSetResponse Call(ImmuService.ImmuServiceClient immuS, RootService rs, SafeSetOptions request)
        {
            var root = rs.GetRoot();

            var index = new Immudb.Schema.Index
            {
                Index_ = root.Index
            };

            var valueB        = new byte[8 + request.Kv.Value.Length];
            var buffTimestamp = BitConverter.GetBytes(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(buffTimestamp);
            }

            buffTimestamp.CopyTo(valueB, 0);
            request.Kv.Value.CopyTo(valueB, 8);

            var sso = new SafeSetOptions
            {
                Kv = new KeyValue
                {
                    Key   = request.Kv.Key,
                    Value = ByteString.CopyFrom(valueB)
                },
                RootIndex = index
            };

            var msg = immuS.SafeSet(sso);

            var item = new Item
            {
                Key   = sso.Kv.Key,
                Value = sso.Kv.Value,
                Index = msg.Index
            };

            if (!ItemUtils.GetHash(item).SequenceEqual(msg.Leaf.ToByteArray()))
            {
                throw new Exception("Proof does not match the given item.");
            }

            bool verified = Proofs.Verify(msg, msg.Leaf.ToByteArray(), root);

            if (verified)
            {
                var toCache = new Root
                {
                    Index = msg.Index,
                    Root_ = msg.Root
                };

                try
                {
                    rs.SetRoot(toCache);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return(new SafeSetResponse
            {
                Index = msg.Index,
                Leaf = msg.Leaf,
                Root = msg.Root,
                At = msg.At,
                InclusionPath = msg.InclusionPath,
                ConsistencyPath = msg.ConsistencyPath,
                Verified = verified
            });
        }