示例#1
0
        public async Task <ProtocolTransactionResponse> RevealTransaction(string transactionId, List <Common.RandomModels.RevealItem> items)
        {
            try
            {
                var rs = await api.ApiTransactionRevealPostWithHttpMessagesAsync(new ProtocolTransaction()
                {
                    Type  = "AggregatedReveal",
                    Items = items.Select(x => new Sp8de.Protocol.Client.Models.SignedItem()
                    {
                        Type   = x.Type.ToString(),
                        Nonce  = x.Nonce,
                        PubKey = x.PubKey,
                        Seed   = x.Seed,
                        Sign   = x.Sign,
                    }).ToList(),
                    DependsOn = transactionId
                }, AuthHeaders);

                var result = (rs.Body as Sp8deTransaction);

                if (result != null)
                {
                    var protocolTransactionResponse = new ProtocolTransactionResponse()
                    {
                        Id    = result.Id,
                        Items = result.InternalTransactions.Select(x => new Common.RandomModels.SignedItem()
                        {
                            Type   = MapType(x.Type),
                            Nonce  = x.Nonce,
                            PubKey = x.FromProperty,
                            Seed   = x.Data,
                            Sign   = x.Sign,
                        }).ToList(),
                    };

                    var external = result.Anchors.FirstOrDefault();
                    if (external != null)
                    {
                        protocolTransactionResponse.Anchor = new Common.BlockModels.Anchor()
                        {
                            Data      = external.Data,
                            Timestamp = external.Timestamp ?? 0,
                            Type      = external.Type,
                        };
                    }
                    return(protocolTransactionResponse);
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(null);
        }
示例#2
0
        private async Task AddAnchors(ProtocolTransactionResponse tx)
        {
            if (ipfs.IsActive)
            {
                var rs = await ipfs.Add(tx.Id, tx);

                tx.Anchor = new Anchor()
                {
                    Type      = "ipfs",
                    Data      = rs.Id,
                    Timestamp = DateConverter.UtcNow
                };
            }
        }
示例#3
0
        public async Task <ProtocolTransactionResponse> CreateTransaction(List <SignedItem> items, ChaosProtocolSettings settings)
        {
            var id = TxIdHelper.GenerateId();

            var revealItem = new RevealItem()
            {
                Type   = UserType.Validator,
                Seed   = random.NextLong().ToString(),
                Nonce  = DateTime.UtcNow.Ticks.ToString(),
                PubKey = keySecret.PublicAddress.ToLowerInvariant()
            };

            revealItem.Sign = cryptoService.SignMessage(revealItem.ToString(), keySecret.PrivateKey);

            await storage.Add(revealItem.Sign, revealItem);

            var commitItem = revealItem.ToCommitItem();

            items.Add(new SignedItem()
            {
                Type   = commitItem.Type,
                Nonce  = commitItem.Nonce,
                PubKey = commitItem.PubKey,
                Sign   = commitItem.Sign,
            });

            var tx = new ProtocolTransactionResponse()
            {
                Id     = id,
                Signer = keySecret.PublicAddress.ToLowerInvariant(),
                Items  = items
            };

            await AddAnchors(tx);

            await storage.Add(id, tx);

            return(tx);
        }
示例#4
0
        public async Task <ProtocolTransactionResponse> RevealTransaction(string transactionId, List <RevealItem> items)
        {
            var tx = await storage.Get <ProtocolTransactionResponse>(transactionId);

            var commitItem = tx.Items.First(x => x.PubKey == tx.Signer);

            items.Add(await storage.Get <RevealItem>(commitItem.Sign));

            foreach (var revealItem in items)
            {
                if (!cryptoService.VerifySignature(revealItem.ToString(), revealItem.Sign, revealItem.PubKey))
                {
                    throw new ArgumentException($"Invalid signature for {revealItem.PubKey}");
                }
                ;
            }

            var finishTx = new ProtocolTransactionResponse()
            {
                Id        = TxIdHelper.GenerateId(),
                DependsOn = transactionId,
                Signer    = keySecret.PublicAddress.ToLowerInvariant(),
                Items     = items.Select(x => new SignedItem()
                {
                    Type   = x.Type,
                    PubKey = x.PubKey,
                    Seed   = x.Seed,
                    Sign   = x.Sign,
                    Nonce  = x.Nonce,
                }).ToList()
            };

            await AddAnchors(finishTx);

            await storage.Add(finishTx.Id, finishTx);

            return(finishTx);
        }