public async Task <String> Get(Int64 amount, String address, Int64 minersFee = 1000) { //Helper method to create a buy in transaction //TODO: WRAP AND INJECT REPO NBitcoin.BlockrTransactionRepository repo = new NBitcoin.BlockrTransactionRepository(NBitcoin.Network.TestNet); var utxos = await repo.GetUnspentAsync(base.LocalBitcoinAddress.ToString()); NBitcoin.Coin[] coins = utxos.OrderByDescending(u => u.Amount).Select(u => new NBitcoin.Coin(u.Outpoint, u.TxOut)).ToArray(); NBitcoin.Money fee = new NBitcoin.Money(minersFee); NBitcoin.IDestination destination = NBitcoin.BitcoinAddress.Create(address, base.Network); var txBuilder = new NBitcoin.TransactionBuilder(); var tx = txBuilder .AddCoins(coins) .AddKeys(base.Secret) .Send(destination, new NBitcoin.Money(amount)) .SendFees(minersFee) .SetChange(base.LocalBitcoinAddress) .BuildTransaction(true); Debug.Assert(txBuilder.Verify(tx)); //check fully signed return(tx.ToHex()); }
internal static void ProcessBitcoinTransaction(JObject blockchainTransaction) { /* Format: * * { * "op": "utx", * "x": { * "lock_time": 0, * "ver": 1, * "size": 192, * "inputs": [ * { * "sequence": 4294967295, * "prev_out": { * "spent": true, * "tx_index": 99005468, * "type": 0, * "addr": "1BwGf3z7n2fHk6NoVJNkV32qwyAYsMhkWf", * "value": 65574000, * "n": 0, * "script": "76a91477f4c9ee75e449a74c21a4decfb50519cbc245b388ac" * }, * "script": "483045022100e4ff962c292705f051c2c2fc519fa775a4d8955bce1a3e29884b2785277999ed02200b537ebd22a9f25fbbbcc9113c69c1389400703ef2017d80959ef0f1d685756c012102618e08e0c8fd4c5fe539184a30fe35a2f5fccf7ad62054cad29360d871f8187d" * } * ], * "time": 1440086763, * "tx_index": 99006637, * "vin_sz": 1, * "hash": "0857b9de1884eec314ecf67c040a2657b8e083e1f95e31d0b5ba3d328841fc7f", * "vout_sz": 1, * "relayed_by": "127.0.0.1", * "out": [ * { * "spent": false, * "tx_index": 99006637, * "type": 0, * "addr": "1A828tTnkVFJfSvLCqF42ohZ51ksS3jJgX", * "value": 65564000, * "n": 0, * "script": "76a914640cfdf7b79d94d1c980133e3587bd6053f091f388ac" * } * ] * } * } */ Console.WriteLine(" - transaction received"); if (_transactionCache == null) { _transactionCache = new SerializableDictionary <string, JObject>(); } string txHash = (string)blockchainTransaction["x"]["hash"]; _transactionCache[txHash] = (JObject)blockchainTransaction["x"]; foreach (JObject outpoint in blockchainTransaction["x"]["out"]) { Satoshis satoshis = Int64.Parse((string)outpoint["value"]); string addressString = (string)outpoint["addr"]; HotBitcoinAddress hotAddress = null; try { hotAddress = HotBitcoinAddress.FromAddress(BitcoinChain.Cash, addressString); } catch (ArgumentException) { // Ignore this - it means the addressString isn't ours continue; } if (hotAddress != null) { JObject json = new JObject(); json["MessageType"] = "BitcoinReceived"; json["Address"] = addressString; json["Hash"] = txHash; Currency currency = hotAddress.Organization.Currency; json["OrganizationId"] = hotAddress.OrganizationId.ToString(); json["Currency"] = currency.Code; Swarmops.Logic.Financial.Money organizationCents = new Money(satoshis, Currency.BitcoinCore).ToCurrency(currency); json["Satoshis"] = satoshis.ToString(); json["Cents"] = organizationCents.Cents.ToString(); json["CentsFormatted"] = String.Format("{0:N2}", organizationCents.Cents / 100.0); _socketServer.WebSocketServices.Broadcast(json.ToString()); // TODO: Examine what address this is, handle accordingly } } }