Ejemplo n.º 1
0
 /// <summary>
 /// Parses the given private key as created by the "dumpprivkey" BitCoin C++ RPC.
 /// </summary>
 /// <param name="networkParams">The expected network parameters of the key. If you don't care, provide null.</param>
 /// <param name="encoded">The base58 encoded string.</param>
 /// <exception cref="AddressFormatException">If the string is invalid or the header byte doesn't match the network params.</exception>
 public DumpedPrivateKey(NetworkParameters networkParams, string encoded)
     : base(encoded)
 {
     if (networkParams != null && Version != networkParams.DumpedPrivateKeyHeader)
         throw new AddressFormatException("Mismatched version number, trying to cross networks? " + Version +
                                          " vs " + networkParams.DumpedPrivateKeyHeader);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Construct an address from parameters and the standard "human readable" form.
 /// </summary>
 /// <remarks>
 /// Example:<p/>
 /// <pre>new Address(NetworkParameters.prodNet(), "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");</pre>
 /// </remarks>
 /// <exception cref="AddressFormatException"/>
 public Address(NetworkParameters networkParams, string address)
     : base(address)
 {
     if (Version != networkParams.AddressHeader)
         throw new AddressFormatException("Mismatched version number, trying to cross networks? " + Version +
                                          " vs " + networkParams.AddressHeader);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Used only in creation of the genesis block.
 /// </summary>
 internal TransactionInput(NetworkParameters networkParams, Transaction parentTransaction, byte[] scriptBytes)
     : base(networkParams)
 {
     ScriptBytes = scriptBytes;
     Outpoint = new TransactionOutPoint(networkParams, -1, null);
     _sequence = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
Ejemplo n.º 4
0
 internal TransactionOutput(NetworkParameters networkParams, Transaction parent, ulong value, Address to)
     : base(networkParams)
 {
     _value = value;
     _scriptBytes = Script.CreateOutputScript(to);
     ParentTransaction = parent;
     _availableForSpending = true;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Used only in creation of the genesis blocks and in unit tests.
 /// </summary>
 internal TransactionOutput(NetworkParameters networkParams, Transaction parent, byte[] scriptBytes)
     : base(networkParams)
 {
     _scriptBytes = scriptBytes;
     _value = Utils.ToNanoCoins(50, 0);
     ParentTransaction = parent;
     _availableForSpending = true;
 }
Ejemplo n.º 6
0
 internal Transaction(NetworkParameters networkParams)
     : base(networkParams)
 {
     _version = 1;
     _inputs = new List<TransactionInput>();
     _outputs = new List<TransactionOutput>();
     // We don't initialize appearsIn deliberately as it's only useful for transactions stored in the wallet.
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Creates an UNSIGNED input that links to the given output
 /// </summary>
 internal TransactionInput(NetworkParameters networkParams, Transaction parentTransaction, TransactionOutput output)
     : base(networkParams)
 {
     var outputIndex = output.Index;
     Outpoint = new TransactionOutPoint(networkParams, outputIndex, output.ParentTransaction);
     ScriptBytes = EmptyArray;
     _sequence = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a new, empty wallet with no keys and no transactions. If you want to restore a wallet from disk instead,
 /// see loadFromFile.
 /// </summary>
 public Wallet(NetworkParameters networkParams)
 {
     _params = networkParams;
     Keychain = new List<EcKey>();
     Unspent = new Dictionary<Sha256Hash, Transaction>();
     Spent = new Dictionary<Sha256Hash, Transaction>();
     _inactive = new Dictionary<Sha256Hash, Transaction>();
     Pending = new Dictionary<Sha256Hash, Transaction>();
     _dead = new Dictionary<Sha256Hash, Transaction>();
 }
Ejemplo n.º 9
0
 public VersionMessage(NetworkParameters networkParams, uint newBestHeight)
     : base(networkParams)
 {
     ClientVersion = NetworkParameters.ProtocolVersion;
     LocalServices = 0;
     Time = UnixTime.ToUnixTime(DateTime.UtcNow);
     // Note that the official client doesn't do anything with these, and finding out your own external IP address
     // is kind of tricky anyway, so we just put nonsense here for now.
     MyAddr = new PeerAddress(IPAddress.Loopback, networkParams.Port, 0);
     TheirAddr = new PeerAddress(IPAddress.Loopback, networkParams.Port, 0);
     SubVer = "BitCoinSharp 0.3-SNAPSHOT";
     BestHeight = newBestHeight;
 }
Ejemplo n.º 10
0
 internal TransactionOutPoint(NetworkParameters networkParams, int index, Transaction fromTx)
     : base(networkParams)
 {
     Index = index;
     if (fromTx != null)
     {
         Hash = fromTx.Hash;
         FromTx = fromTx;
     }
     else
     {
         // This happens when constructing the genesis block.
         Hash = Sha256Hash.ZeroHash;
     }
 }
Ejemplo n.º 11
0
 /// <exception cref="ProtocolException"/>
 internal Message(NetworkParameters networkParams, byte[] msg, int offset, uint protocolVersion = NetworkParameters.ProtocolVersion)
 {
     ProtocolVersion = protocolVersion;
     Params = networkParams;
     Bytes = msg;
     Cursor = Offset = offset;
     Parse();
     #if SELF_CHECK
     // Useful to ensure serialize/deserialize are consistent with each other.
     if (GetType() != typeof (VersionMessage))
     {
         var msgbytes = new byte[Cursor - offset];
         Array.Copy(msg, offset, msgbytes, 0, Cursor - offset);
         var reserialized = BitcoinSerialize();
         if (!reserialized.SequenceEqual(msgbytes))
             throw new Exception("Serialization is wrong: " + Environment.NewLine +
                                 Utils.BytesToHexString(reserialized) + " vs " + Environment.NewLine +
                                 Utils.BytesToHexString(msgbytes));
     }
     #endif
     Bytes = null;
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Connect to the given IP address using the port specified as part of the network parameters. Once construction
        /// is complete a functioning network channel is set up and running.
        /// </summary>
        /// <param name="peerAddress">IP address to connect to. IPv6 is not currently supported by BitCoin. If port is not positive the default port from params is used.</param>
        /// <param name="networkParams">Defines which network to connect to and details of the protocol.</param>
        /// <param name="bestHeight">How many blocks are in our best chain</param>
        /// <param name="connectTimeout">Timeout in milliseconds when initially connecting to peer</param>
        /// <exception cref="IOException">If there is a network related failure.</exception>
        /// <exception cref="ProtocolException">If the version negotiation failed.</exception>
        public NetworkConnection(PeerAddress peerAddress, NetworkParameters networkParams, uint bestHeight, int connectTimeout)
        {
            _params = networkParams;
            remoteIp = peerAddress.Addr;

            var port = (peerAddress.Port > 0) ? peerAddress.Port : networkParams.Port;

            var address = new IPEndPoint(remoteIp, port);
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Connect(address);
            _socket.SendTimeout = _socket.ReceiveTimeout = connectTimeout;

            _out = new NetworkStream(_socket, FileAccess.Write);
            _in = new NetworkStream(_socket, FileAccess.Read);

            // the version message never uses check-summing. Update check-summing property after version is read.
            serializer = new BitcoinSerializer(networkParams, true);  // SDL: Checksuming now ALWAYS necessary

            versionMessage = Handshake(networkParams, bestHeight);

            // newer clients use check-summing
            serializer.UseChecksumming(versionMessage.ClientVersion >= 209);
            // Handshake is done!
        }
Ejemplo n.º 13
0
 /// <exception cref="ProtocolException"/>
 protected ListMessage(NetworkParameters networkParams, byte[] bytes)
     : base(networkParams, bytes, 0)
 {
 }
Ejemplo n.º 14
0
 internal Message(NetworkParameters networkParams)
 {
     Params = networkParams;
 }
Ejemplo n.º 15
0
 /// <exception cref="ProtocolException"/>
 internal AddressMessage(NetworkParameters networkParams, byte[] payload)
     : base(networkParams, payload, 0)
 {
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Special case constructor, used for the genesis node, cloneAsHeader and unit tests.
 /// </summary>
 internal Block(NetworkParameters networkParams)
     : base(networkParams)
 {
     // Set up a few basic things. We are not complete after this though.
     _version = 1;
     _difficultyTarget = 0x1d07fff8;
     _time = (uint) UnixTime.ToUnixTime(DateTime.UtcNow);
     _prevBlockHash = Sha256Hash.ZeroHash;
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Deserializes the message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionOutPoint(NetworkParameters networkParams, byte[] payload, int offset)
     : base(networkParams, payload, offset)
 {
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Deserializes a transaction output message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionOutput(NetworkParameters networkParams, Transaction parent, byte[] payload, int offset)
     : base(networkParams, payload, offset)
 {
     ParentTransaction     = parent;
     _availableForSpending = true;
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Construct a peer that handles the given network connection and reads/writes from the given block chain. Note that
 /// communication won't occur until you call connect().
 /// </summary>
 public Peer(NetworkParameters networkParams, PeerAddress address, BlockChain blockChain)
     : this(networkParams, address, 0, blockChain)
 {
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Constructs a BlockChain connected to the given list of wallets and a store.
 /// </summary>
 /// <exception cref="BlockStoreException"/>
 public BlockChain(NetworkParameters networkParams, IEnumerable<Wallet> wallets, IBlockStore blockStore)
 {
     _blockStore = blockStore;
     _chainHead = blockStore.GetChainHead();
     Log.InfoFormat("chain head is:{0}{1}", Environment.NewLine, _chainHead.Header);
     _params = networkParams;
     _wallets = new List<Wallet>(wallets);
 }
Ejemplo n.º 21
0
 internal Message(NetworkParameters networkParams)
 {
     Params = networkParams;
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Constructs a BlockChain that has no wallet at all. This is helpful when you don't actually care about sending
 /// and receiving coins but rather, just want to explore the network data structures.
 /// </summary>
 /// <exception cref="BlockStoreException"/>
 public BlockChain(NetworkParameters networkParams, IBlockStore blockStore)
     : this(networkParams, new List<Wallet>(), blockStore)
 {
 }
Ejemplo n.º 23
0
 public GetBlocksMessage(NetworkParameters networkParams, IList<Sha256Hash> locator, Sha256Hash stopHash)
     : base(networkParams)
 {
     _locator = locator;
     _stopHash = stopHash;
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Construct a peer address from a serialized payload.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public PeerAddress(NetworkParameters networkParams, byte[] payload, int offset, uint protocolVersion)
     : base(networkParams, payload, offset, protocolVersion)
 {
 }
Ejemplo n.º 25
0
 protected ListMessage(NetworkParameters networkParams)
     : base(networkParams)
 {
     _items = new List<InventoryItem>();
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Returns the address that corresponds to the public part of this ECKey. Note that an address is derived from
        /// the RIPEMD-160 hash of the public key and is not the public key itself (which is too large to be convenient).
        /// </summary>
        public Address ToAddress(NetworkParameters networkParams)
        {
            var hash160 = Utils.Sha256Hash160(_pub);

            return(new Address(networkParams, hash160));
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Construct a peer that handles the given network connection and reads/writes from the given block chain. Note that
 /// communication won't occur until you call Connect().
 /// </summary>
 /// <param name="bestHeight">Our current best chain height, to facilitate downloading.</param>
 public Peer(NetworkParameters networkParams, PeerAddress address, uint bestHeight, BlockChain blockChain)
 {
     _params = networkParams;
     _address = address;
     _bestHeight = bestHeight;
     _blockChain = blockChain;
     _pendingGetBlockFutures = new List<GetDataFuture<Block>>();
 }
Ejemplo n.º 28
0
 /// <exception cref="ProtocolException"/>
 public UnknownMessage(NetworkParameters networkParams, string name, byte[] payloadBytes)
     : base(networkParams, payloadBytes, 0)
 {
     _name = name;
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Deserializes a transaction output message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionOutput(NetworkParameters networkParams, Transaction parent, byte[] payload, int offset)
     : base(networkParams, payload, offset)
 {
     ParentTransaction = parent;
     _availableForSpending = true;
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Constructs a BitcoinSerializer with the given behavior.
 /// </summary>
 /// <param name="networkParams">MetworkParams used to create Messages instances and determining packetMagic</param>
 /// <param name="usesChecksumming">Set to true if checksums should be included and expected in headers</param>
 public BitcoinSerializer(NetworkParameters networkParams, bool usesChecksumming)
 {
     _params = networkParams;
     _usesChecksumming = usesChecksumming;
 }
Ejemplo n.º 31
0
 /// <exception cref="ProtocolException"/>
 public VersionMessage(NetworkParameters networkParams, byte[] msg)
     : base(networkParams, msg, 0)
 {
 }
Ejemplo n.º 32
0
 /// <exception cref="ProtocolException"/>
 internal AddressMessage(NetworkParameters networkParams, byte[] payload, int offset)
     : base(networkParams, payload, offset)
 {
 }
Ejemplo n.º 33
0
 /// <summary>
 /// Constructs a BlockChain connected to the given wallet and store. To obtain a <see cref="Wallet"/> you can construct
 /// one from scratch, or you can deserialize a saved wallet from disk using <see cref="Wallet.LoadFromFile"/>.
 /// </summary>
 /// <remarks>
 /// For the store you can use a <see cref="MemoryBlockStore"/> if you don't care about saving the downloaded data, or a
 /// <see cref="BoundedOverheadBlockStore"/> if you'd like to ensure fast start-up the next time you run the program.
 /// </remarks>
 /// <exception cref="BlockStoreException"/>
 public BlockChain(NetworkParameters networkParams, Wallet wallet, IBlockStore blockStore)
     : this(networkParams, new List<Wallet>(), blockStore)
 {
     if (wallet != null)
         AddWallet(wallet);
 }
Ejemplo n.º 34
0
 /// <summary>
 /// Exports the private key in the form used by the Satoshi client "dumpprivkey" and "importprivkey" commands. Use
 /// the <see cref="DumpedPrivateKey.ToString"/> method to get the string.
 /// </summary>
 /// <param name="networkParams">The network this key is intended for use on.</param>
 /// <returns>Private key bytes as a <see cref="DumpedPrivateKey"/>.</returns>
 public DumpedPrivateKey GetPrivateKeyEncoded(NetworkParameters networkParams)
 {
     return(new DumpedPrivateKey(networkParams, GetPrivKeyBytes()));
 }
Ejemplo n.º 35
0
 /// <exception cref="ProtocolException"/>
 public VersionMessage(NetworkParameters networkParams, byte[] msg)
     : base(networkParams, msg, 0)
 {
 }
Ejemplo n.º 36
0
 /// <summary>
 /// Construct a peer address from a serialized payload.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public PeerAddress(NetworkParameters networkParams, byte[] payload, int offset, uint protocolVersion)
     : base(networkParams, payload, offset, protocolVersion)
 {
 }