2 people like it.

BTC transfers with NBitcoin

Bitcoin transfers. I'm not responsible if your money gets lost. Have fun!

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
//Add reference to NBitcoin
#if INTERACTIVE
#I "./../packages/NBitcoin.3.0.1.6/lib/net45/"
#I "./../packages/Newtonsoft.Json.9.0.1/lib/net45"
#r "NBitcoin.dll"
#r "Newtonsoft.Json.dll"
#endif

open System
open NBitcoin
open NBitcoin.Protocol

// --------------- CREATING A TRANSACTION --------------- // 

let network = 
    // Select your network
    Network.Main
    // Network.Test

/// Generate a new wallet private key for a new user as byte array
let getNewPrivateKey() = Key().ToBytes()

/// Create BitcoinSecret from byte array
let getSecret(bytes:byte[]) = BitcoinSecret(Key(bytes), network)

// Create a new wallet:
let newWallet() = Key()
let alice = BitcoinSecret(newWallet(), network)
let bob = BitcoinSecret(newWallet(), network)
    
// You can also use existing wallets.
// to test you can create wallets e.g. in https://rushwallet.com/
let tuomas = BitcoinSecret("5J3RWPbfuR3W8sQkPto15CBWgc99SiMBNvP7KYb9xdAUW35L2qh", network)

let bobPublic = 
    //bob.GetAddress()
    // or use existing:
    BitcoinPubKeyAddress("1PnfqAxqmH9r5ADc9xCoaXJRjcuFs2xh1H", network)
    
let transaction =
    let sum = Money.Coins 0.008m
    let fee = Money.Coins 0.0004m
    let coin = 
        Coin(
            OutPoint(
                // Previous Transaction-ID, get it from your wallet.
                new uint256("70c3a3925d6a6d652f9a2f4c5b0a93c93c783c9939d701e3a1b24fce6b2244b9"), 
                // Coin-order-number
                0 // (Inside a transaction can be many operations, you have to spot your coin.)
            ), TxOut(sum, tuomas.ScriptPubKey)
        ) :> ICoin
    let builder = TransactionBuilder()
    let tx = 
        builder
            .AddCoins([|coin|])
            .AddKeys(tuomas)
            .Send(bobPublic, (sum - fee))
            .SendFees(fee)
            .SetChange(tuomas.GetAddress())
            .BuildTransaction(true)

    let ok, errs = builder.Verify tx
    match ok with
    | true -> tx
    | false -> failwith(String.Join(",", errs))

// --------------- SENDING TRANSACTION TO SERVER --------------- // 

// Do the trade
let sendSync() =
    /// Connect to public Bitcoin network:
    // get a bit-node server address, e.g. from: https://bitnodes.21.co/
    // Not all the nodes work.
    use node = Node.Connect(network, "81.17.27.134:8333")
    node.VersionHandshake()
    // Notify server
    node.SendMessage(InvPayload(transaction)) 
    System.Threading.Thread.Sleep 1000
    // Send transaction
    node.SendMessage(TxPayload(transaction))
    System.Threading.Thread.Sleep 5000
    node.Disconnect()
    transaction.GetHash()
//sendSync()

//let sendAsync() =
//    async {
//        use node = Node.Connect(network, "81.17.27.134:8333")
//        node.VersionHandshake()
//        do! node.SendMessageAsync(InvPayload(transaction)) 
//            |> Async.AwaitTask
//        do! Async.Sleep 1000
//        do! node.SendMessageAsync(TxPayload(transaction)) 
//            |> Async.AwaitTask
//        do! Async.Sleep 5000
//        node.DisconnectAsync()
//        Console.WriteLine (transaction.GetHash())
//    }
//sendAsync() |> Async.Start

// Instead of sending, you can broadcast the 
// transaction manually e.g. from http://blockr.io
let transactionToBroadcast = transaction.ToHex()

// You can search the transaction 
// e.g. from: https://blockchain.info/
let hash = transaction.GetHash()

// Some resources:
// Open book: https://programmingblockchain.gitbooks.io/programmingblockchain/content/
// A video: https://www.youtube.com/watch?v=X4ZwRWIF49w
// Article: https://www.codeproject.com/articles/835098/nbitcoin-build-them-all
namespace System
val network : obj

Full name: Script.network
val getNewPrivateKey : unit -> 'a

Full name: Script.getNewPrivateKey


 Generate a new wallet private key for a new user as byte array
val getSecret : bytes:byte [] -> 'a

Full name: Script.getSecret


 Create BitcoinSecret from byte array
val bytes : byte []
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = Byte

Full name: Microsoft.FSharp.Core.byte
val newWallet : unit -> 'a

Full name: Script.newWallet
val alice : obj

Full name: Script.alice
val bob : obj

Full name: Script.bob
val tuomas : obj

Full name: Script.tuomas
val bobPublic : obj

Full name: Script.bobPublic
val transaction : obj

Full name: Script.transaction
val sum : obj
val fee : obj
val coin : obj
val builder : obj
val tx : obj
val ok : bool
val errs : obj
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.Join(separator: string, values: Collections.Generic.IEnumerable<string>) : string
String.Join<'T>(separator: string, values: Collections.Generic.IEnumerable<'T>) : string
String.Join(separator: string, [<ParamArray>] values: obj []) : string
String.Join(separator: string, [<ParamArray>] value: string []) : string
String.Join(separator: string, value: string [], startIndex: int, count: int) : string
val sendSync : unit -> 'a

Full name: Script.sendSync
val node : IDisposable


 Connect to public Bitcoin network:
namespace System.Threading
Multiple items
type Thread =
  inherit CriticalFinalizerObject
  new : start:ThreadStart -> Thread + 3 overloads
  member Abort : unit -> unit + 1 overload
  member ApartmentState : ApartmentState with get, set
  member CurrentCulture : CultureInfo with get, set
  member CurrentUICulture : CultureInfo with get, set
  member DisableComObjectEagerCleanup : unit -> unit
  member ExecutionContext : ExecutionContext
  member GetApartmentState : unit -> ApartmentState
  member GetCompressedStack : unit -> CompressedStack
  member GetHashCode : unit -> int
  ...

Full name: System.Threading.Thread

--------------------
Threading.Thread(start: Threading.ThreadStart) : unit
Threading.Thread(start: Threading.ParameterizedThreadStart) : unit
Threading.Thread(start: Threading.ThreadStart, maxStackSize: int) : unit
Threading.Thread(start: Threading.ParameterizedThreadStart, maxStackSize: int) : unit
Threading.Thread.Sleep(timeout: TimeSpan) : unit
Threading.Thread.Sleep(millisecondsTimeout: int) : unit
val transactionToBroadcast : obj

Full name: Script.transactionToBroadcast
val hash : obj

Full name: Script.hash

More information

Link:http://fssnip.net/7RT
Posted:5 years ago
Author:Tuomas Hietanen
Tags: btc , bitcoin , blockchain