Implement SendTransactionAsync()
MODIFY
MagicalCryptoWallet.Tests/RegTests.cs
MagicalCryptoWallet.Tests/RegTests.cs
@@ -708,6 +708,10 @@ namespace MagicalCryptoWallet.Tests
Assert.Equal(key.GetP2wpkhScript(), res2.SpentCoins.Single().ScriptPubKey);
Assert.Equal(new Money(0.1m, MoneyUnit.BTC), res2.SpentCoins.Single().Amount);
Assert.False(res2.SpendsUnconfirmed);
+
+ await wallet.SendTransactionAsync(res2.Transaction);
+
+ Assert.Contains(res2.InternalOutputs.Single(), wallet.Coins);
}
finally
{

MODIFY
MagicalCryptoWallet/Services/WalletService.cs
MagicalCryptoWallet/Services/WalletService.cs
@@ -737,6 +737,25 @@ namespace MagicalCryptoWallet.Services
return haveEnough;
}
+ public async Task SendTransactionAsync(SmartTransaction transaction)
+ {
+ using (var torClient = new TorHttpClient(IndexDownloader.Client.DestinationUri, IndexDownloader.Client.TorSocks5EndPoint, isolateStream: true))
+ using (var content = new StringContent($"'{transaction.Transaction.ToHex()}'", Encoding.UTF8, "application/json"))
+ using (var response = await torClient.SendAsync(HttpMethod.Post, "/api/v1/btc/Blockchain/broadcast", content))
+ {
+ if(response.StatusCode == HttpStatusCode.BadRequest)
+ {
+ throw new HttpRequestException($"Couldn't broadcast transaction. Reason: {await response.Content.ReadAsStringAsync()}");
+ }
+ if (response.StatusCode != HttpStatusCode.OK) // Try again.
+ {
+ throw new HttpRequestException($"Couldn't broadcast transaction. Reason: {response.StatusCode.ToReasonString()}");
+ }
+ }
+
+ ProcessTransaction(new SmartTransaction(transaction.Transaction, Height.MemPool));
+ }
+
#region IDisposable Support
private volatile bool _disposedValue = false; // To detect redundant calls

Implement SendTransactionAsync()