public void ClearAllData() { KeyValues.RemoveRange(KeyValues); Workflows.RemoveRange(Workflows); Timestamps.RemoveRange(Timestamps); Proofs.RemoveRange(Proofs); Claims.RemoveRange(Claims); Packages.RemoveRange(Packages); SaveChanges(); }
public static SafeGetResponse Call(ImmuService.ImmuServiceClient immuS, RootService rs, SafeGetOptions request) { var root = rs.GetRoot(); var index = new Immudb.Schema.Index { Index_ = root.Index }; var protoReq = new SafeGetOptions(request) { RootIndex = index }; var msg = immuS.SafeGet(protoReq); var verified = Proofs.Verify(msg.Proof, ItemUtils.GetHash(msg.Item), root); if (verified) { var toCache = new Root { Index = msg.Proof.At, Root_ = msg.Proof.Root }; try { rs.SetRoot(toCache); } catch (Exception e) { throw e; } } var i = msg.Item; var timestampBytes = i.Value.Take(8).ToArray(); if (BitConverter.IsLittleEndian) { Array.Reverse(timestampBytes); } return(new SafeGetResponse { Index = i.Index, Key = i.Key, Value = ByteString.CopyFrom(i.Value.Skip(8).ToArray()), Timestamp = BitConverter.ToInt64(timestampBytes), Verified = verified }); }
public DictionaryObject GetJson() { var json = new DictionaryObject { { "amount", AmountAsset.AmountToLong(Amount) }, { "price", Asset.PriceToLong(AmountAsset, PriceAsset, Price) }, { "timestamp", Timestamp.ToLong() }, { "expiration", Expiration.ToLong() }, { "senderPublicKey", SenderPublicKey.ToBase58() }, { "matcherPublicKey", MatcherPublicKey.ToBase58() }, { "matcherFee", Assets.BCT.AmountToLong(MatcherFee) }, { "assetPair", new DictionaryObject { { "amountAsset", AmountAsset.IdOrNull }, { "priceAsset", PriceAsset.IdOrNull } } }, { "orderType", Side.ToString().ToLower() }, { "version", Version } }; var proofs = Proofs .Take(Array.FindLastIndex(Proofs, p => p != null && p.Length > 0) + 1) .Select(p => p == null ? "" : p.ToBase58()) .ToArray(); if (SupportsProofs()) { json.Add("proofs", proofs); } else { if (proofs.Length == 0) { throw new InvalidOperationException("Order is not signed"); } if (proofs.Length > 1) { throw new InvalidOperationException("Order version doesn't support multiple proofs"); } json.Add("signature", proofs.Single()); } return(json); }
public byte[] GetProofsBytes() { var stream = new MemoryStream(); var writer = new BinaryWriter(stream); var proofs = Proofs .Take(Array.FindLastIndex(Proofs, p => p != null && p.Length > 0) + 1) .Select(p => p ?? (new byte[0])) .ToArray(); writer.WriteByte(1); writer.WriteShort(proofs.Count()); foreach (var proof in proofs) { writer.WriteShort(proof.Length); writer.Write(proof); } return(stream.ToArray()); }
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 }); }