/// <summary> /// Returns the public based on the passed private key and password /// </summary> /// <param name="privateKey"></param> /// <param name="password"></param> /// <returns>Empty string if unresolvable, otherwise public key</returns> public string ResolvePrivateKey(string encryptedPrivateKey, string password) { if (!String.IsNullOrEmpty(encryptedPrivateKey) && !String.IsNullOrEmpty(password)) { Byte[] privateKeyAsBytes = Convert.FromBase64String(encryptedPrivateKey.Trim()); Byte[] passwordAsBytes = Convert.FromBase64String(password.Trim()); String decryptedWIFPrivateKey = DC.Common.Security.DecryptStringFromBytes_Aes(privateKeyAsBytes, passwordAsBytes); Org.BouncyCastle.Math.BigInteger key = DC.Common.BitcoinHelper.DecodeWif(decryptedWIFPrivateKey); var sourceAddress = new Common.Models.Address(key.ToString()); return(sourceAddress.BTCAddress); } else { throw new ArgumentException("Private Key or password cannot be null"); } }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "address/{postalCode}/{houseNumber}")] HttpRequest req, string postalCode, string houseNumber, ILogger log) { var mapsKey = _configuration.GetValue <string>("mapsKey"); var mapsClientId = _configuration.GetValue <string>("46630e98-fea4-4749-9050-bf8dd62d9c9c"); var client = _httpClientFactory.CreateClient(); client.DefaultRequestHeaders.Add("x-ms-client-id", mapsClientId); client.BaseAddress = new Uri("https://atlas.microsoft.com/search/address/structured/"); var responseMessage = await client.GetAsync($"json?countryCode=NL&api-version=1.0&subscription-key={mapsKey}&postalCode={postalCode.Replace(" ", "")}&streetNumber={houseNumber}"); var getAddress = JsonConvert.DeserializeObject <SearchAddressRepsonse>( await responseMessage.Content.ReadAsStringAsync(), new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); var streetAddress = getAddress.Results.OrderByDescending(ga => ga.Score).FirstOrDefault(); if (streetAddress != null) { var address = new Common.Models.Address { City = streetAddress.Address.LocalName, HouseNumber = streetAddress.Address.StreetNumber, Latitude = streetAddress.Position.Lat, Longitude = streetAddress.Position.Lon, Street = streetAddress.Address.StreetName, Zipcode = streetAddress.Address.ExtendedPostalCode }; return(new OkObjectResult(address)); } return(new NotFoundResult()); }
/// <summary> /// Sign and send bitcoins using bitcoin core /// </summary> /// <param name="privateKey">Private key as decimal</param> /// <param name="sourceAddress">Bitcoin address to receive change</param> /// <param name="destinationAddress"></param> /// <param name="amount"></param> private String SignAndTransfer(String privateKey, String destinationAddress, Decimal amount) { DC.Common.Models.Address sourceAddress = new Common.Models.Address(privateKey); DC.Common.Models.UnspentResponse utxoResponse = Helpers.Factory.GetTransactionProvider().GetUnspentTxns (sourceAddress.BTCAddress); Decimal fee = Convert.ToDecimal(ConfigurationManager.AppSettings["fee"]); fee = 0.0001M; //Get unspent txs, largest to smallest IEnumerable <DC.Common.Models.UnspentOutput> outputs = (from u in utxoResponse.UnspentOutputs where u.Confirmations > 0 select u).OrderByDescending(u => u.Value); //There needs to be sufficient balance for the amount plus the fee Int64 target = Convert.ToInt64((amount + fee) * DC.Common.Math.SATOSHI); if (outputs.Count() > 0) { bool sufficientFunds = false; IList <DC.Common.Models.UnspentOutput> outputsToSpend = new List <Common.Models.UnspentOutput>(); foreach (DC.Common.Models.UnspentOutput output in outputs) { outputsToSpend.Add(output); Int64 i = outputsToSpend.Sum(o => o.Value); if (i >= target) { sufficientFunds = true; break; } } if (!sufficientFunds) { string message; if (outputsToSpend.Sum(o => o.Value) == amount * DC.Common.Math.SATOSHI) { message = "There are insufficient funds to cover the transaction amount plus the bitcoin miner fee of 0.0001 Bitcoin"; } else { message = "There are insufficient funds to complete this transaction"; } throw new InvalidOperationException(message); } DC.Providers.IAddress provider = new DC.Providers.BlockChainInfo(); String destinationHash160 = provider.GetHash160(destinationAddress); Helpers.Transaction Transaction = new Helpers.Transaction(outputsToSpend, sourceAddress, destinationAddress, destinationHash160, amount, fee); Byte[] signedTransaction = Transaction.CreateAndSignTransaction(sourceAddress, destinationAddress, destinationHash160, amount, fee); String signature = DC.Common.StringHelper.ByteArrayToHexString(signedTransaction); // Providers.IWallet wallet = Helpers.Factory.GetWallet(); Providers.IWallet wallet = new Providers.BlockChainInfo() { MerchantId = "4ea10592-e7cb-4170-9b5b-6e94e3236bb1", Password = "******" }; //ADD YOUR BLOCK CHAIN INFO CREDS HERE! String txnHash = wallet.SendRawTransaction(signature); return(txnHash); } else { throw new ArgumentException("The address does not have any unspent transactions"); } }