Ejemplo n.º 1
0
        public override async Task <string> SendCapitulationRequest(GameTypes type, string address, string password = "")
        {
            if (!Players.ContainsKey(address))
            {
                return(await Task.FromResult("Player address is not in the list of players!"));
            }

            try
            {
                var dto = new ChessGameDto()
                {
                    GameId              = Id.ToString(),
                    GameState           = string.Empty,
                    Type                = GameDtoTypes.ChessGame,
                    LastGameRecordTxId  = string.Empty,
                    CapitulationRequest = true,
                };

                dto.Players = new Dictionary <string, ChessPlayer>();

                if (Players.TryGetValue(Player1Address, out var pl1))
                {
                    pl1.FigureType = FigureTypes.White;
                }

                if (Players.TryGetValue(Player1Address, out var pl2))
                {
                    pl2.FigureType = FigureTypes.Black;
                }

                foreach (var pl in Players)
                {
                    dto.Players.TryAdd(pl.Key, pl.Value);
                }

                var moveString = JsonConvert.SerializeObject(dto);

                var tkData = new SendTokenTxData()
                {
                    Amount          = 1,
                    Symbol          = TokenSymbol,
                    SenderAddress   = Player1Address,
                    ReceiverAddress = Player2Address,
                    Id = TokenId
                };

                tkData.Metadata.TryAdd("GameId", Id.ToString());
                tkData.Metadata.TryAdd("GameData", moveString);
                tkData.Password = password;

                var res = await NeblioTransactionHelpers.SendNTP1TokenAPI(tkData, 30000);

                return(await Task.FromResult(res));
            }
            catch (Exception ex)
            {
                log.Error("Chess Game - Cannot write move!", ex);
                return(await Task.FromResult("ERROR"));
            }
        }
        private async Task <string> processOrder(Utxos utxo, ITransaction txinfo, IShopItem mainItem)
        {
            var amountOfTokens = (int)((mainItem.Price / (utxo.Value / neblio.FromSatToMainRatio)) * mainItem.Lot);

            if (amountOfTokens != 0)
            {
                try
                {
                    var meta = new Dictionary <string, string>();
                    meta.Add("Token Order", "true");
                    meta.Add("PaymentTxId", utxo.Txid);

                    var sutxs = await NeblioTransactionHelpers.FindUtxoForMintNFT(Address, TokenId, amountOfTokens - 1);

                    if (sutxs.Count != 0)
                    {
                        var sendutxos = new List <string>();
                        foreach (var s in sutxs)
                        {
                            sendutxos.Add(s.Txid + ":" + s.Index);
                        }

                        var dto = new SendTokenTxData()
                        {
                            Amount          = amountOfTokens,
                            Id              = TokenId,
                            Metadata        = meta,
                            Password        = string.Empty, // shop must be unlocked
                            SenderAddress   = Address,
                            ReceiverAddress = txinfo.From[0],
                            sendUtxo        = sendutxos,
                            NeblUtxo        = utxo.Txid,
                            UseRPCPrimarily = false
                        };

                        var resp = await NeblioTransactionHelpers.SendNTP1TokenAPI(dto, isItMintNFT : true);

                        return(resp);
                    }
                }
                catch (Exception ex)
                {
                    log.Error("Cannot send ordered tokens!", ex);
                }
            }

            return(string.Empty);
        }
Ejemplo n.º 3
0
        public async Task <string> WriteMove(string stateString, string onMoveAddress, string player2Address, string password = "")
        {
            GameState state = null;

            if (!string.IsNullOrEmpty(stateString))
            {
                state = GetGameStateFromString(stateString);
            }
            else
            {
                return(await Task.FromResult("Cannot load state String. It is empty!"));
            }

            if (!Players.ContainsKey(onMoveAddress) || !Players.ContainsKey(player2Address))
            {
                return(await Task.FromResult("Player address is not in the list of players!"));
            }

            try
            {
                state.LastMovePlayer = onMoveAddress;
                var dto = new ChessGameDto()
                {
                    GameId             = Id.ToString(),
                    GameState          = JsonConvert.SerializeObject(state),
                    Type               = GameDtoTypes.ChessGame,
                    LastGameRecordTxId = ActualStateTxId
                };

                dto.Players = new Dictionary <string, ChessPlayer>();

                foreach (var pl in Players)
                {
                    dto.Players.TryAdd(pl.Key, pl.Value);
                }

                GameHistory.Add(dto);

                var moveString = JsonConvert.SerializeObject(dto);

                var tkData = new SendTokenTxData()
                {
                    Amount          = 1,
                    Symbol          = TokenSymbol,
                    ReceiverAddress = player2Address,
                    SenderAddress   = onMoveAddress,
                    Id = TokenId
                };

                tkData.Metadata.TryAdd("GameId", Id.ToString());
                tkData.Metadata.TryAdd("GameData", moveString);
                tkData.Password = password;

                var res = await NeblioTransactionHelpers.SendNTP1TokenAPI(tkData, 30000);

                if (!string.IsNullOrEmpty(res))
                {
                    ActualStateTxId = res;
                }

                return(await Task.FromResult(res));
            }
            catch (Exception ex)
            {
                log.Error("Chess Game - Cannot write move!", ex);
                return(await Task.FromResult("ERROR"));
            }
        }
        public static async Task <string> SendMessage(SendMessageDto dto)
        {
            if (EconomyMainContext.Wallets.TryGetValue(dto.WalletId, out var wallet))
            {
                if (wallet.Accounts.TryGetValue(dto.SenderAddress, out var account))
                {
                    var txdto = new SendTokenTxData();

                    txdto.Amount = 1;
                    txdto.Id     = TokenId;     // token ID - todo MSGT
                    txdto.Symbol = TokenSymbol; // token symbol - todo MSGT

                    txdto.UseRPCPrimarily = false;
                    txdto.Password        = dto.AccountPassword;
                    txdto.SenderAddress   = dto.SenderAddress;
                    txdto.ReceiverAddress = dto.ReceiverAddress;


                    ///////////////////////////////
                    // prepare the message to send

                    if (dto.InitMessage)
                    {
                        // for init message lets create new uid
                        txdto.Metadata.Add("MessageStreamUID", Guid.NewGuid().ToString());
                    }
                    else
                    {
                        // for not init message message stream uid must be filled
                        if (!string.IsNullOrEmpty(dto.MessageStreamUID))
                        {
                            txdto.Metadata.Add("MessageStreamUID", dto.MessageStreamUID.ToString());
                        }
                        else
                        {
                            throw new Exception("Cannot send message - this is not set as init message but no MessageStreamUID was provided!");
                        }
                    }

                    var msg         = string.Empty;
                    var prevmsgdata = string.Empty;
                    var prevmsg     = string.Empty;
                    var prevtxid    = string.Empty;

                    if (!dto.InitMessage)
                    {
                        var pretoken = await NeblioTransactionHelpers.TokenMetadataAsync(TokenTypes.NTP1, TokenId, dto.TokenTxId);

                        if (string.IsNullOrEmpty(pretoken.TxId))
                        {
                            throw new Exception("Cannot send message - Cannot find previous message token metadata!");
                        }

                        if (pretoken.MetadataAvailable)
                        {
                            if (pretoken.Metadata.TryGetValue("PrevTxId", out var prevtxidfrommeta))
                            {
                                prevtxid = prevtxidfrommeta;
                            }

                            IToken prepretoken = null;
                            if (pretoken.Metadata.TryGetValue("InitMessage", out var prevmsginit))
                            {
                                if (prevmsginit != "true")
                                {
                                    prepretoken = await NeblioTransactionHelpers.TokenMetadataAsync(TokenTypes.NTP1, TokenId, prevtxid);

                                    if (string.IsNullOrEmpty(prepretoken.TxId))
                                    {
                                        throw new Exception("Cannot send message - Cannot find pre-previous message token metadata and previous message is not init message!");
                                    }
                                }
                            }

                            if (pretoken.Metadata.TryGetValue("MessageData", out var prevmsgdatafromMeta))
                            {
                                prevmsgdata = prevmsgdatafromMeta;

                                if (prepretoken != null)
                                {
                                    if (prepretoken.MetadataAvailable)
                                    {
                                        if (prepretoken.Metadata.TryGetValue("SenderPubKey", out var preprevtokenReceiverKey))
                                        {
                                            var accEncKey = account.AccountKeys.FirstOrDefault(k => k.PublicKey == preprevtokenReceiverKey);
                                            if (accEncKey != null)
                                            {
                                                try
                                                {
                                                    // dencrypt my prevoious message just when I know that it will be encrypt before send
                                                    // if this will not happen, the prevmsgdata will contain original data from token metadata which can be encrypted
                                                    if (pretoken.Metadata.ContainsKey("SenderPubKey"))
                                                    {
                                                        var decprevmsg = AsymmetricProvider.DecryptString(prevmsgdata, accEncKey.GetEncryptedKey(dto.Password));

                                                        prevmsgdata = decprevmsg;
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    throw new Exception("Cannot send message - Cannot decrypt prevouis message, requeired keuy is not correct format!");
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            // if sender sent in previous message pubkey he wants to encrypt the message before sending
                            if (pretoken.Metadata.TryGetValue("SenderPubKey", out var receiverpubkey))
                            {
                                if (!string.IsNullOrEmpty(receiverpubkey))
                                {
                                    try
                                    {
                                        // encrypt new message
                                        msg = AsymmetricProvider.EncryptString(dto.Message, receiverpubkey);
                                        // encrypt prevoius message
                                        prevmsg = AsymmetricProvider.EncryptString(prevmsgdata, receiverpubkey);
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("Cannot send message - Cannot encrypt the message, probably wrong Key");
                                    }
                                }
                                else
                                {
                                    throw new Exception("Cannot send message - Required key is not correct format!");
                                }
                            }
                            else
                            {
                                // if he dont want to encrypt message just copy
                                msg = dto.Message;
                                // if he dont want encrypted message copy previous message too
                                // but if that message was encrypted before witout his new encryption request it will not be provided in decrypted form and he cannot read it
                                // this is because i dont want to publish my encrypted previous message if this transfer will not be encrypted
                                prevmsg = prevmsgdata;
                            }
                        }
                    }

                    var senderPUB = string.Empty;
                    if (dto.Encrypt)
                    {
                        if (!string.IsNullOrEmpty(dto.KeyId))
                        {
                            var key = account.AccountKeys.FirstOrDefault(k => k.Id.ToString() == dto.KeyId);
                            if (key != null)
                            {
                                senderPUB = key.PublicKey;
                                // if encryption is required token metadata will contains sender pub key
                                txdto.Metadata.Add("SenderPubKey", senderPUB);
                            }
                        }
                    }

                    if (msg == null)
                    {
                        msg = string.Empty;
                    }

                    if (dto.InitMessage)
                    {
                        txdto.Metadata.Add("InitMessage", "true");
                        msg = dto.Message;
                    }
                    else
                    {
                        txdto.Metadata.Add("InitMessage", "false");
                    }

                    txdto.Metadata.Add("PrevTxId", dto.TokenTxId);
                    txdto.Metadata.Add("PreviousMessage", prevmsg);
                    txdto.Metadata.Add("MessageData", msg);

                    var res = await NeblioTransactionHelpers.SendNTP1TokenAPI(txdto, 50000);

                    return(res);
                }
                else
                {
                    throw new Exception("Cannot send message - Cannot find the Account!");
                }
            }
            else
            {
                throw new Exception("Cannot send message - Cannot find the wallet!");
            }
        }