Ejemplo n.º 1
0
 /// <summary>
 ///     Construct an address from parameters and the standard "human readable" form.
 /// </summary>
 /// <remarks>
 ///     Example:<p />
 ///     <pre>new ipAddress(NetworkParameters.prodNet(), "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");</pre>
 /// </remarks>
 /// <exception cref="AddressFormatException" />
 public Address(NetworkParameters networkParameters, string address)
     : base(address)
 {
     if (Version != networkParameters.AddressHeader)
         throw new AddressFormatException("Mismatched version number, trying to cross networks? " + Version +
                                          " vs " + networkParameters.AddressHeader);
 }
Ejemplo n.º 2
0
 // Used by EcKey.PrivateKeyEncoded
 internal DumpedPrivateKey(NetworkParameters networkParameters, byte[] keyBytes)
     : base(networkParameters.DumpedPrivateKeyHeader, keyBytes)
 {
     if (keyBytes.Length != 32) // 256 bit keys
         throw new ArgumentException(
             "Keys are 256 bits, so you must provide 32 bytes, got " + keyBytes.Length + " bytes", "keyBytes");
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Deserializes a transaction output message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionOutput(NetworkParameters networkParameters, Transaction parentTransaction, byte[] payload,
     int offset)
     : base(networkParameters, payload, offset)
 {
     ParentTransaction = parentTransaction;
     _availableForSpending = true;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Parses the given private key as created by the "dumpprivkey" BitCoin C++ RPC.
 /// </summary>
 /// <param name="networkParameters">The expected network parameters of the key. If you don't care, provide null.</param>
 /// <param name="base58EncodedString">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 networkParameters, string base58EncodedString)
     : base(base58EncodedString)
 {
     if (networkParameters != null && Version != networkParameters.DumpedPrivateKeyHeader)
         throw new AddressFormatException("Mismatched version number, trying to cross networks? " + Version +
                                          " vs " + networkParameters.DumpedPrivateKeyHeader);
 }
Ejemplo n.º 5
0
 public Transaction(NetworkParameters networkParameters)
     : base(networkParameters)
 {
     _version = 1;
     _transactionInputs = new List<TransactionInput>();
     _transactionOutputs = new List<TransactionOutput>();
     // We don't initialize appearsIn deliberately as it's only useful for transactions stored in the wallet.
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Used only in creation of the genesis block.
 /// </summary>
 internal TransactionInput(NetworkParameters networkParameters, Transaction parentTransaction, byte[] scriptBytes)
     : base(networkParameters)
 {
     ScriptBytes = scriptBytes;
     Outpoint = new TransactionOutPoint(networkParameters, -1, null);
     _sequence = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
Ejemplo n.º 7
0
 public TransactionOutput(NetworkParameters networkParameters, Transaction parentTransaction, ulong value,
     Address to)
     : base(networkParameters)
 {
     Value = value;
     _scriptBytes = Script.CreateOutputScript(to);
     ParentTransaction = parentTransaction;
     _availableForSpending = true;
 }
Ejemplo n.º 8
0
 public MemoryBlockStore(NetworkParameters @params)
 {
     _blockMap = new Dictionary<Sha256Hash, StoredBlock>();
     // Insert the genesis block.
     var genesisHeader = @params.GenesisBlock.CloneAsHeader();
     var storedGenesis = new StoredBlock(genesisHeader, genesisHeader.GetWork(), 0);
     Put(storedGenesis);
     SetChainHead(storedGenesis);
 }
Ejemplo n.º 9
0
 public void SetUp()
 {
     _unitTestParams = NetworkParameters.UnitTests();
     _defaultWallet = new DefaultWallet(_unitTestParams);
     _defaultWallet.AddKey(new EcKey());
     _chainBlockStore = new MemoryBlockStore(_unitTestParams);
     _chain = new BlockChain(_unitTestParams, _defaultWallet, _chainBlockStore);
     _coinbaseTo = _defaultWallet.Keychain[0].ToAddress(_unitTestParams);
     _someOtherGuy = new EcKey().ToAddress(_unitTestParams);
 }
Ejemplo n.º 10
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 DefaultWallet(NetworkParameters networkParameters)
 {
     _networkParameters = networkParameters;
     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.º 11
0
 public VersionMessage(NetworkParameters networkParameters, uint newBestHeight)
     : base(networkParameters)
 {
     ClientVersion = NetworkParameters.ProtocolVersion;
     LocalServices = 0;
     Time = SystemTime.UnixNow();
     // 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.
     MyAddress = new PeerAddress(IPAddress.Loopback, networkParameters.Port, 0);
     TheirAddress = new PeerAddress(IPAddress.Loopback, networkParameters.Port, 0);
     SubVersion = LIBRARY_SUBVER;
     BestHeight = newBestHeight;
 }
Ejemplo n.º 12
0
 // Emulates receiving a valid block that builds on top of the chain.
 public static BlockPair CreateFakeBlock(NetworkParameters @params, IBlockStore blockStore, params Transaction[] transactions)
 {
     var b = MakeTestBlock(@params, blockStore);
     // Coinbase tx was already added.
     foreach (var tx in transactions)
         b.AddTransaction(tx);
     b.Solve();
     var pair = new BlockPair();
     pair.Block = b;
     pair.StoredBlock = blockStore.GetChainHead().Build(b);
     blockStore.Put(pair.StoredBlock);
     blockStore.SetChainHead(pair.StoredBlock);
     return pair;
 }
Ejemplo n.º 13
0
 public static Transaction CreateFakeTx(NetworkParameters @params, ulong nanocoins, Address to)
 {
     var t = new Transaction(@params);
     var o1 = new TransactionOutput(@params, t, nanocoins, to);
     t.AddOutput(o1);
     // Make a previous tx simply to send us sufficient coins. This prev tx is not really valid but it doesn't
     // matter for our purposes.
     var prevTx = new Transaction(@params);
     var prevOut = new TransactionOutput(@params, prevTx, nanocoins, to);
     prevTx.AddOutput(prevOut);
     // Connect it.
     t.AddInput(prevOut);
     return t;
 }
Ejemplo n.º 14
0
 internal TransactionOutPoint(NetworkParameters networkParameters, int index, Transaction fromTransaction)
     : base(networkParameters)
 {
     Index = index;
     if (fromTransaction != null)
     {
         Hash = fromTransaction.Hash;
         FromTransaction = fromTransaction;
     }
     else
     {
         // This happens when constructing the genesis block.
         Hash = Sha256Hash.ZeroHash;
     }
 }
Ejemplo n.º 15
0
 /// <exception cref="BlockStoreException"/>
 public DiskBlockStore(NetworkParameters @params, FileInfo file)
 {
     _params = @params;
     _blockMap = new Dictionary<Sha256Hash, StoredBlock>();
     try
     {
         Load(file);
         if (_stream != null)
         {
             _stream.Dispose();
         }
         _stream = file.Open(FileMode.Append, FileAccess.Write); // Do append.
     }
     catch (IOException e)
     {
         _log.Error("failed to load block store from file", e);
         CreateNewStore(@params, file);
     }
 }
Ejemplo n.º 16
0
 /// <exception cref="ProtocolException"/>
 internal AbstractMessage(NetworkParameters networkParameters, byte[] byteMessage, int offset,
     uint protocolVersion = NetworkParameters.ProtocolVersion)
 {
     ProtocolVersion = protocolVersion;
     NetworkParameters = networkParameters;
     Bytes = byteMessage;
     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(byteMessage, 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.º 17
0
 /// <exception cref="ProtocolException"/>
 public VersionMessage(NetworkParameters networkParameters, byte[] byteMessage)
     : base(networkParameters, byteMessage, 0)
 {
 }
Ejemplo n.º 18
0
        public void SetUp()
        {
            _testNetChainBlockStore = new MemoryBlockStore(_testNet);
            _testNetChain = new BlockChain(_testNet, new DefaultWallet(_testNet), _testNetChainBlockStore);
            _unitTestParams = NetworkParameters.UnitTests();
            _defaultWallet = new DefaultWallet(_unitTestParams);
            _defaultWallet.AddKey(new EcKey());

            ResetBlockStore();
            _chain = new BlockChain(_unitTestParams, _defaultWallet, _blockStore);

            _coinbaseTo = _defaultWallet.Keychain[0].ToAddress(_unitTestParams);
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Creates an UNSIGNED input that links to the given output
 /// </summary>
 internal TransactionInput(NetworkParameters networkParameters, Transaction parentTransaction,
     TransactionOutput output)
     : base(networkParameters)
 {
     var outputIndex = output.Index;
     Outpoint = new TransactionOutPoint(networkParameters, outputIndex, output.ParentTransaction);
     ScriptBytes = EmptyArray;
     _sequence = uint.MaxValue;
     ParentTransaction = parentTransaction;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Deserializes an input message. This is usually part of a transaction message.
 /// </summary>
 /// <exception cref="ProtocolException"/>
 public TransactionInput(NetworkParameters networkParameters, Transaction parentTransaction, byte[] payload,
     int offset)
     : base(networkParameters, payload, offset)
 {
     ParentTransaction = parentTransaction;
 }
Ejemplo n.º 21
0
 /// <exception cref="ProtocolException"/>
 protected ListMessage(NetworkParameters networkParameters, byte[] bytes)
     : base(networkParameters, bytes, 0)
 {
 }
Ejemplo n.º 22
0
 private static Block CreateGenesis(NetworkParameters networkParameters)
 {
     var genesisBlock = new Block(networkParameters);
     var transaction = new Transaction(networkParameters);
     // A script containing the difficulty bits and the following message:
     //   "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
     var bytes = Hex.Decode("04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73");
     transaction.AddInput(new TransactionInput(networkParameters, transaction, bytes));
     using (var scriptPubKeyBytes = new MemoryStream())
     {
         Script.WriteBytes(scriptPubKeyBytes,Hex.Decode("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"));
         scriptPubKeyBytes.Write(Script.OpCheckSig);
         transaction.AddOutput(new TransactionOutput(networkParameters, transaction, scriptPubKeyBytes.ToArray()));
     }
     genesisBlock.AddTransaction(transaction);
     return genesisBlock;
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Returns a testnet params modified to allow any difficulty target.
 /// </summary>
 public static NetworkParameters UnitTests()
 {
     var networkParameters = new NetworkParameters();
     networkParameters = CreateTestNet(networkParameters);
     networkParameters.ProofOfWorkLimit =
         new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
     networkParameters.GenesisBlock.TargetDifficulty = Block.EasiestDifficultyTarget;
     networkParameters.Interval = 10;
     networkParameters.TargetTimespan = 200000000; // 6 years. Just a very big number.
     return networkParameters;
 }
Ejemplo n.º 24
0
 /// <summary>
 /// The test chain created by Gavin.
 /// </summary>
 public static NetworkParameters TestNet()
 {
     var networkParameters = new NetworkParameters();
     return CreateTestNet(networkParameters);
 }
Ejemplo n.º 25
0
        /// <summary>
        /// The primary BitCoin chain created by Satoshi.
        /// </summary>
        public static NetworkParameters ProdNet()
        {
            var networkParameters = new NetworkParameters
            {
                ProofOfWorkLimit = new BigInteger("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16),
                Port = 8333,
                PacketMagic = 0xf9beb4d9,
                AddressHeader = 0,
                DumpedPrivateKeyHeader = 128,
                Interval = _interval,
                TargetTimespan = _targetTimespan,

            };
            networkParameters.GenesisBlock = CreateGenesis(networkParameters);
            networkParameters.GenesisBlock.TargetDifficulty = 0x1d00ffff;
            networkParameters.GenesisBlock.TimeSeconds = 1231006505;
            networkParameters.GenesisBlock.Nonce = 2083236893;
            networkParameters.DnsSeeds = new[]
            {
                //Well known discovery host names on the production network.
                "seed.bitcoin.sipa.be",
                "dnsseed.bluematt.me",
                "dnsseed.bitcoin.dashjr.org",
                "bitseed.xf2.org"
            };
            var genesisHash = networkParameters.GenesisBlock.HashAsString;
            //TODO: If the genesis block must be created why don't we throw if it's invalid?
            Debug.Assert(genesisHash.Equals("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"),
                genesisHash);
            return networkParameters;
        }
Ejemplo n.º 26
0
 public GetDataMessage(NetworkParameters networkParameters)
     : base(networkParameters)
 {
 }
Ejemplo n.º 27
0
 protected ListMessage(NetworkParameters networkParameters)
     : base(networkParameters)
 {
     Items = new List<InventoryItem>();
 }
Ejemplo n.º 28
0
        private byte[] _programCopy; // TODO: remove this

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Construct a Script using the given network parameters and a range of the programBytes array.
        /// </summary>
        /// <param name="params">Network parameters.</param>
        /// <param name="programBytes">Array of program bytes from a transaction.</param>
        /// <param name="offset">How many bytes into programBytes to start reading from.</param>
        /// <param name="length">How many bytes to read.</param>
        /// <exception cref="ScriptException"/>
        public Script(NetworkParameters @params, byte[] programBytes, int offset, int length)
        {
            _params = @params;
            Parse(programBytes, offset, length);
        }
Ejemplo n.º 29
0
 /// <exception cref="BitcoinSharp.ProtocolException"/>
 public GetDataMessage(NetworkParameters networkParameters, byte[] payloadBytes)
     : base(networkParameters, payloadBytes)
 {
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Sets up the given NetworkParameters with testnet values.
 /// </summary>
 private static NetworkParameters CreateTestNet(NetworkParameters networkParameters)
 {
     // Genesis hash is 0000000224b1593e3ff16a0e3b61285bbc393a39f78c8aa48c456142671f7110
     // The proof of work limit has to start with 00, as otherwise the value will be interpreted as negative.
     networkParameters.ProofOfWorkLimit = new BigInteger("0000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
     networkParameters.Port = 18333;
     networkParameters.PacketMagic = 0x0b110907;
     networkParameters.AddressHeader = 111;
     networkParameters.DumpedPrivateKeyHeader = 239;
     networkParameters.Interval = _interval;
     networkParameters.TargetTimespan = _targetTimespan;
     networkParameters.GenesisBlock = CreateGenesis(networkParameters);
     //TODO: Should this use SystemTime.UnixTime()?
     networkParameters.GenesisBlock.TimeSeconds = 1296688602;
     networkParameters.GenesisBlock.TargetDifficulty = 0x1d07fff8;
     networkParameters.GenesisBlock.Nonce = 384568319;
     networkParameters.DnsSeeds = new[]
     {
         "testnet-seed.bitcoin.petertodd.org",
         "testnet-seed.bluematt.me"
     };
     var genesisHash = networkParameters.GenesisBlock.HashAsString;
     Debug.Assert(genesisHash.Equals("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"),genesisHash);
     return networkParameters;
 }