/// <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); }
// 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"); }
/// <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; }
/// <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); }
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. }
/// <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; }
public TransactionOutput(NetworkParameters networkParameters, Transaction parentTransaction, ulong value, Address to) : base(networkParameters) { Value = value; _scriptBytes = Script.CreateOutputScript(to); ParentTransaction = parentTransaction; _availableForSpending = true; }
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); }
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); }
/// <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>(); }
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; }
// 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; }
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; }
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; } }
/// <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); } }
/// <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; }
/// <exception cref="ProtocolException"/> public VersionMessage(NetworkParameters networkParameters, byte[] byteMessage) : base(networkParameters, byteMessage, 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); }
/// <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; }
/// <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; }
/// <exception cref="ProtocolException"/> protected ListMessage(NetworkParameters networkParameters, byte[] bytes) : base(networkParameters, bytes, 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; }
/// <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; }
/// <summary> /// The test chain created by Gavin. /// </summary> public static NetworkParameters TestNet() { var networkParameters = new NetworkParameters(); return CreateTestNet(networkParameters); }
/// <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; }
public GetDataMessage(NetworkParameters networkParameters) : base(networkParameters) { }
protected ListMessage(NetworkParameters networkParameters) : base(networkParameters) { Items = new List<InventoryItem>(); }
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); }
/// <exception cref="BitcoinSharp.ProtocolException"/> public GetDataMessage(NetworkParameters networkParameters, byte[] payloadBytes) : base(networkParameters, payloadBytes) { }
/// <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; }