Exemplo n.º 1
0
        /// <summary>
        /// Network address.
        /// </summary>
        /// <returns>The address.</returns>
        /// <param name="coin">Coin.</param>
        /// <param name="networkApi">Network API.</param>
        public byte[] NetworkAddress(CoinDto coin, NetworkApiMethod networkApi = null)
        {
            Guard.Argument(coin, nameof(coin)).NotNull();

            //TODO: Will remove the need to format to and from base64..
            try
            { coin = coin.FormatCoinFromBase64(); }
            catch (FormatException) { }

            byte[] address = new byte[33];

            string env = networkApi == null ? environment : networkApi.ToString();

            address[0] = env == Constant.Mainnet ? (byte)0x1 : (byte)74;

            var hash = Cryptography.GenericHashWithKey(
                $"{coin.Envelope.Commitment}" +
                $" {coin.Envelope.Proof}" +
                $" {coin.Envelope.PublicKey}" +
                $" {coin.Envelope.Signature}" +
                $" {coin.Hash}" +
                $" {coin.Hint}" +
                $" {coin.Keeper}" +
                $" {coin.Principle}" +
                $" {coin.Stamp}" +
                $" {coin.Version}",
                coin.Principle.FromHex());

            Array.Copy(hash, 0, address, 1, 32);

            return(Encoding.UTF8.GetBytes(Base58.Bitcoin.Encode(address)));
        }
Exemplo n.º 2
0
        public CoinDto Insert(CoinInsertDto dto)
        {
            CoinDto coinDto = null;

            try
            {
                var coin = Mapper.Map <CoinInsertDto, Coin>(dto);
                coin.CreatedBy = _appSession.GetUserName();
                coin.IsEnabled = true;
                _unitOfWork.CreateTransaction();

                _unitOfWork.GenericRepository <Coin>().Insert(coin);

                CreateBranchCashForAllBranches(coin);
                CreateTreasuryCashForAllTreasures(coin);
                CreateCompanyCashForAllCompanies(coin);
                CreateClientCashForAllClients(coin);

                _unitOfWork.Save();

                _unitOfWork.Commit();

                coinDto = Mapper.Map <Coin, CoinDto>(coin);
            }
            catch (Exception ex)
            {
                Tracing.SaveException(ex);
                _unitOfWork.Rollback();
            }

            return(coinDto);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Network address.
        /// </summary>
        /// <returns>The address.</returns>
        /// <param name="coin">Coin.</param>
        /// <param name="networkApi">Network API.</param>
        public byte[] NetworkAddress(CoinDto coin, NetworkApiMethod networkApi = null)
        {
            Guard.Argument(coin, nameof(coin)).NotNull();

            string env = string.Empty;

            byte[] address = new byte[33];

            env        = networkApi == null ? environment : networkApi.ToString();
            address[0] = env == Constant.Mainnet ? (byte)0x1 : (byte)74;

            var hash = Cryptography.GenericHashWithKey(
                $"{coin.Envelope.Commitment}" +
                $" {coin.Envelope.Proof}" +
                $" {coin.Envelope.PublicKey}" +
                $" {coin.Envelope.Signature}" +
                $" {coin.Hash}" +
                $" {coin.Hint}" +
                $" {coin.Keeper}" +
                $" {coin.Principle}" +
                $" {coin.Stamp}" +
                $" {coin.Version}",
                Encoding.UTF8.GetBytes(coin.Principle));

            Array.Copy(hash, 0, address, 1, 32);

            return(Encoding.UTF8.GetBytes(Base58.Bitcoin.Encode(address)));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add the Wallet transaction.
        /// </summary>
        /// <returns>The Wallet transaction.</returns>
        /// <param name="coin">Coin.</param>
        /// <param name="transactionType">Transaction type.</param>
        private async Task <bool> AddWalletTransaction(Guid sessionId, CoinDto coin, ulong total, string memoText, byte[] blind, byte[] salt, TransactionType transactionType)
        {
            Guard.Argument(coin, nameof(coin)).NotNull();
            Guard.Argument(total, nameof(total)).NotNegative();
            CoinDto formattedCoin;

            try
            { formattedCoin = coin.FormatCoinFromBase64(); }
            catch (FormatException)
            { formattedCoin = coin; }

            var txn = new TransactionDto
            {
                TransactionId   = Guid.NewGuid().ToString(),
                Amount          = total,
                Blind           = blind == null ? string.Empty : blind.ToHex(),
                Commitment      = formattedCoin.Envelope.Commitment,
                Hash            = formattedCoin.Hash,
                Salt            = salt == null ? string.Empty : salt.ToHex(),
                Stamp           = formattedCoin.Stamp,
                Version         = formattedCoin.Version,
                TransactionType = transactionType,
                Memo            = memoText,
                DateTime        = DateTime.Now
            };

            var session = GetSession(sessionId);
            var addTxn  = await unitOfWork.GetTransactionRepository().Put(session, txn);

            return(addTxn.Success);
        }
Exemplo n.º 5
0
        /// <summary>
        ///  Checks if the coin equals the mode.
        /// </summary>
        /// <returns>The pass.</returns>
        /// <param name="swap">Swap.</param>
        /// <param name="mode">Mode.</param>
        private async Task <bool> CoinPass(SecureString secret, SecureString salt, CoinDto swap, int mode)
        {
            Guard.Argument(swap, nameof(swap)).NotNull();
            Guard.Argument(mode, nameof(mode)).NotNegative();

            var canPass = false;
            var coin    = coinService.DeriveCoin(swap, secret, salt);
            var status  = coinService.VerifyCoin(swap, coin);

            coin.Hash = coinService.Hash(coin).ToHex();

            //TODO.... remove
            coin.Network             = walletService.NetworkAddress(coin).ToHex();
            coin.Envelope.RangeProof = walletService.NetworkAddress(coin).ToHex();

            if (status.Equals(mode))
            {
                var returnCoin = await Client.AddAsync(coin.FormatCoinToBase64(), RestApiMethod.PostCoin);

                if (returnCoin.Result != null)
                {
                    canPass = true;
                }
            }

            return(canPass);
        }
Exemplo n.º 6
0
 public void UpdateCoin(Guid collectionId, CoinDto coinDto)
 {
     if (CoinService.Exists(collectionId, coinDto.Id))
     {
         CoinService.UpdateCoin(collectionId, Mapper.Map <CoinModel>(coinDto));
     }
     else
     {
         throw new NotFoundException();
     }
 }
        public async Task <JsonResult> Add(
            [FromServices] IMediator mediator,
            [FromBody] CoinDto coin)
        {
            if (coin is null)
            {
                throw new System.ArgumentNullException(nameof(coin));
            }

            var           command = new AddCoinCommand(coin);
            AddCoinResult data    = await mediator.Send(command);

            return(new JsonResult(data.Coin));
        }
Exemplo n.º 8
0
        public async Task PostCoin()
        {
            CoinDto coinDto = new CoinDto {
                amount = 5
            };
            var content = new StringContent(JsonSerializer.Serialize(coinDto), Encoding.UTF8, "application/json");

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = await _client.PostAsync("/CoinJar", content);

            dynamic o = JObject.Parse(await response.Content.ReadAsStringAsync());

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.True(o.amount == coinDto.amount);
        }
Exemplo n.º 9
0
        public CoinDto GetById(int id)
        {
            CoinDto coinDto = null;

            try
            {
                var coin = _unitOfWork.GenericRepository <Coin>().GetById(id);
                if (coin != null)
                {
                    coinDto = Mapper.Map <Coin, CoinDto>(coin);
                }
            }
            catch (Exception ex)
            {
                Tracing.SaveException(ex);
            }

            return(coinDto);
        }
Exemplo n.º 10
0
        public IActionResult Update(int Id, [FromBody] CoinDto coinDto)
        {
            // map dto to entity and set id
            var coin = _mapper.Map <Coin>(coinDto);

            if (ModelState.IsValid)
            {
                try
                {
                    // save
                    _coinRepository.Update(coin);
                    return(Ok());
                }
                catch (AppException ex)
                {
                    // return error message if there was an exception
                    return(BadRequest(ex.Message));
                }
            }
            return(BadRequest(ModelState));
        }
Exemplo n.º 11
0
        public static CoinDto FormatCoinFromBase64(this CoinDto coin)
        {
            var formattedCoin = new CoinDto
            {
                Envelope = new EnvelopeDto()
                {
                    Commitment = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Envelope.Commitment)),
                    Proof      = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Envelope.Proof)),
                    PublicKey  = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Envelope.PublicKey)),
                    Signature  = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Envelope.Signature))
                }
            };

            formattedCoin.Hash   = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Hash));
            formattedCoin.Hint   = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Hint));
            formattedCoin.Keeper = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Keeper));
            // formattedCoin.Network = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Network));
            formattedCoin.Principle = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Principle));
            formattedCoin.Stamp     = Encoding.UTF8.GetString(Convert.FromBase64String(coin.Stamp));
            formattedCoin.Version   = coin.Version;

            return(formattedCoin);
        }
Exemplo n.º 12
0
        public static CoinDto FormatCoinToBase64(this CoinDto coin)
        {
            var formattedCoin = new CoinDto
            {
                Envelope = new EnvelopeDto()
                {
                    Commitment = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Envelope.Commitment)),
                    Proof      = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Envelope.Proof)),
                    PublicKey  = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Envelope.PublicKey)),
                    Signature  = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Envelope.Signature)),
                    RangeProof = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Envelope.RangeProof)),
                }
            };

            formattedCoin.Hash      = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Hash));
            formattedCoin.Hint      = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Hint));
            formattedCoin.Keeper    = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Keeper));
            formattedCoin.Network   = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Network));
            formattedCoin.Principle = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Principle));
            formattedCoin.Stamp     = Convert.ToBase64String(Encoding.UTF8.GetBytes(coin.Stamp));
            formattedCoin.Version   = coin.Version;

            return(formattedCoin);
        }
Exemplo n.º 13
0
        public CoinDto Update(CoinUpdateDto dto)
        {
            CoinDto coinDto = null;

            try
            {
                var coin = _unitOfWork.GenericRepository <Coin>().GetById(dto.Id);
                Mapper.Map <CoinUpdateDto, Coin>(dto, coin);
                coin.ModifiedBy = _appSession.GetUserName();
                _unitOfWork.CreateTransaction();

                _unitOfWork.GenericRepository <Coin>().Update(coin);
                _unitOfWork.Save();

                _unitOfWork.Commit();

                coinDto = Mapper.Map <Coin, CoinDto>(coin);
            }
            catch (Exception ex)
            {
                Tracing.SaveException(ex);
            }
            return(coinDto);
        }
Exemplo n.º 14
0
        private CoinDto SendCoin(CoinDto coin)
        {
            spinner.Text = "Sending printed coin ;)";

            var coinResult = actorService.Client.AddAsync(coin.FormatCoinToBase64(), RestApiMethod.PostCoin).GetAwaiter().GetResult();

            if (coinResult == null)
            {
                for (int i = 0; i < 10; i++)
                {
                    spinner.Text = $"Retrying sending coin {i} of 10";
                    coinResult   = actorService.Client.AddAsync(coin.FormatCoinToBase64(), RestApiMethod.PostCoin).GetAwaiter().GetResult();

                    Task.Delay(100).Wait();

                    if (coinResult != null)
                    {
                        break;
                    }
                }
            }

            return(coinResult.Result);
        }
Exemplo n.º 15
0
 public AddCoinCommand(CoinDto coin)
 {
     CoinDto = coin;
 }
Exemplo n.º 16
0
 public void CreateCoin(Guid collectionId, CoinDto coinDto)
 {
     CoinService.CreateCoin(collectionId, Mapper.Map <CoinModel>(coinDto));
 }