Skip to content

Commit

Permalink
Add a new method to broadcast bitcoin transactions directly to bitcoi…
Browse files Browse the repository at this point in the history
…n nodes in a decentralized way avoiding third parties.
  • Loading branch information
Coding-Enthusiast committed Jan 17, 2022
1 parent dcd98c2 commit c88cf3e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions SharpPusher/Services/P2P.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SharpPusher
// Copyright (c) 2017 Coding Enthusiast
// Distributed under the MIT software license, see the accompanying
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.

using Autarkysoft.Bitcoin;
using Autarkysoft.Bitcoin.Blockchain.Transactions;
using Autarkysoft.Bitcoin.Clients;
using Autarkysoft.Bitcoin.P2PNetwork.Messages;
using Autarkysoft.Bitcoin.P2PNetwork.Messages.MessagePayloads;
using System.Threading.Tasks;

namespace SharpPusher.Services
{
public class P2P : Api
{
public P2P(bool isMainNet)
{
netType = isMainNet ? NetworkType.MainNet : NetworkType.TestNet;
}


private readonly NetworkType netType;

public static readonly string[] DnsMain = new string[]
{
"seed.bitcoin.sipa.be", // Pieter Wuille, only supports x1, x5, x9, and xd
"dnsseed.bluematt.me", // Matt Corallo, only supports x9
"dnsseed.bitcoin.dashjr.org", // Luke Dashjr
"seed.bitcoinstats.com", // Christian Decker, supports x1 - xf
"seed.bitcoin.jonasschnelli.ch", // Jonas Schnelli, only supports x1, x5, x9, and xd
"seed.btc.petertodd.org", // Peter Todd, only supports x1, x5, x9, and xd
"seed.bitcoin.sprovoost.nl", // Sjors Provoost
"dnsseed.emzy.de", // Stephan Oeste
"seed.bitcoin.wiz.biz", // Jason Maurice
};

public static readonly string[] DnsTest = new string[]
{
"testnet-seed.bitcoin.jonasschnelli.ch",
"seed.tbtc.petertodd.org",
"seed.testnet.bitcoin.sprovoost.nl",
"testnet-seed.bluematt.me",
};


public override string ApiName => "P2P";

public override async Task<Response<string>> PushTx(string txHex)
{
Transaction tx = new(txHex);
Message msg = new(new TxPayload(tx), netType);

MinimalClientSettings settings = new(netType, 4, null)
{
DnsSeeds = netType == NetworkType.MainNet ? DnsMain : DnsTest
};
MinimalClient client = new(settings);
client.Start();
await Task.Delay(TimeConstants.OneSecond_Milliseconds * 3);
client.Send(msg);

return new Response<string>()
{
Result = $"Transaction was sent to {settings.MaxConnectionCount} nodes. " +
$"Transaction ID: {tx.GetTransactionId()}"
};
}
}
}
2 changes: 2 additions & 0 deletions SharpPusher/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private void SetApiList()
new Blockchair(Blockchair.Chain.BTC),
new Smartbit(),
new BlockCypher(),
new P2P(true)
};
break;
case Networks.BitcoinCash:
Expand Down Expand Up @@ -128,6 +129,7 @@ private void SetApiList()
ApiList = new ObservableCollection<Api>()
{
new Blockchair(Blockchair.Chain.TBTC),
new P2P(false)
};
break;
case Networks.BitcoinSV:
Expand Down

0 comments on commit c88cf3e

Please sign in to comment.