/** * * @param networkParameters NetworkParameters object. * @param msg Bitcoin protocol formatted byte array containing message content. * @param offset The location of the first msg byte within the array. * @param protocolVersion Bitcoin protocol version. * @param parseLazy Whether to perform a full parse immediately or delay until a read is requested. * @param parseRetain Whether to retain the backing byte array for quick reserialization. * If true and the backing byte array is invalidated due to modification of a field then * the cached bytes may be repopulated and retained if the message is serialized again in the future. * @param Length The Length of message if known. Usually this is provided when deserializing of the wire * as the Length will be provided as part of the header. If unknown then set to Message.UNKNOWN_LENGTH * @ */ Message(NetworkParameters networkParameters, byte[] msg, int offset, int protocolVersion, bool parseLazy, bool parseRetain, int Length) { this.parseLazy = parseLazy; this.parseRetain = parseRetain; this.protocolVersion = protocolVersion; this.networkParameters = networkParameters; this.bytes = msg; this.cursor = this.offset = offset; this.Length = Length; if (parseLazy) { parseLite(); } else { parseLite(); parse(); parsed = true; } Assumes.True(this.Length != UNKNOWN_LENGTH, "Length field has not been set in constructor for %s after %s parse. " + "Refer to Message.parseLite() for detail of required Length field contract.", this.GetType().Name, parseLazy ? "lite" : "full"); if (SELF_CHECK) { selfCheck(msg, offset); } if (parseRetain || !parsed) return; this.bytes = null; }
/** * Construct an address from parameters and the standard "human readable" form. Example:<p> * * <pre>new Address(NetworkParameters.prodNet(), "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");</pre><p> * * @param networkParameters The expected NetworkParameters or null if you don't want validation. * @param address The textual form of the address, such as "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL" * @throws AddressFormatException if the given address doesn't parse or the checksum is invalid * @throws WrongNetworkException if the given address is valid but for a different chain (eg testnet vs prodnet) */ public Address(NetworkParameters networkParameters, string address) : base(address) { if (networkParameters != null) { bool found = false; foreach (int v in networkParameters.acceptableAddressCodes) { if (this.Version == v) { found = true; break; } } if (!found) { throw new WrongNetworkException(this.Version, networkParameters.acceptableAddressCodes); } } }
/** The primary BitCoin chain created by Satoshi. */ public static NetworkParameters prodNet() { NetworkParameters n = new NetworkParameters(); n.proofOfWorkLimit = Utils.decodeCompactBits(0x1d00ffffL); n.port = 8333; n.packetMagic = 0xf9beb4d9L; n.addressHeader = 0; n.acceptableAddressCodes = new int[] { 0 }; n.dumpedPrivateKeyHeader = 128; n.interval = INTERVAL; n.targetTimespan = TARGET_TIMESPAN; n.alertSigningKey = SATOSHI_KEY; n.genesisBlock = createGenesis(n); n.genesisBlock.setDifficultyTarget(0x1d00ffffL); n.genesisBlock.setTime(1231006505L); n.genesisBlock.setNonce(2083236893); n.id = ID_PRODNET; String genesisHash = n.genesisBlock.getHashAsString(); Assumes.True(String.Equals(genesisHash, "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", StringComparison.OrdinalIgnoreCase)); return n; }
/** The test chain created by Gavin. */ public static NetworkParameters testNet() { NetworkParameters n = new NetworkParameters(); return createTestNet(n); }
/** Sets up the given NetworkParameters with testnet values. */ private static NetworkParameters createTestNet(NetworkParameters n) { // Genesis hash is 0000000224b1593e3ff16a0e3b61285bbc393a39f78c8aa48c456142671f7110 n.proofOfWorkLimit = Utils.decodeCompactBits(0x1d0fffffL); n.packetMagic = 0xfabfb5daL; n.port = 18333; n.addressHeader = 111; n.acceptableAddressCodes = new int[] { 111 }; n.dumpedPrivateKeyHeader = 239; n.interval = INTERVAL; n.targetTimespan = TARGET_TIMESPAN; n.alertSigningKey = SATOSHI_KEY; n.genesisBlock = createGenesis(n); n.genesisBlock.setTime(1296688602L); n.genesisBlock.setDifficultyTarget(0x1d07fff8L); n.genesisBlock.setNonce(384568319); n.id = ID_TESTNET; String genesisHash = n.genesisBlock.getHashAsString(); Assumes.True(String.Equals(genesisHash, "00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008", StringComparison.OrdinalIgnoreCase)); return n; }
Message(NetworkParameters networkParameters, byte[] msg, int offset, int protocolVersion) : this(networkParameters, msg, offset, protocolVersion, false, false, UNKNOWN_LENGTH) { }
Message(NetworkParameters networkParameters) { this.networkParameters = networkParameters; parsed = true; parseLazy = false; parseRetain = false; }
public TransactionOutput(NetworkParameters n, Transaction t, byte[] p) { // TODO: Complete member initialization this.n = n; this.t = t; this.p = p; }
Message(NetworkParameters networkParameters, byte[] msg, int offset) : this(networkParameters, msg, offset, NetworkParameters.PROTOCOL_VERSION, false, false, UNKNOWN_LENGTH) { }
public Block(NetworkParameters n) { // TODO: Complete member initialization this.n = n; }
/** * Examines the version byte of the address and attempts to find a matching NetworkParameters. If you aren't sure * which network the address is intended for (eg, it was provided by a user), you can use this to decide if it is * compatible with the current wallet. You should be able to handle a null response from this method. Note that the * parameters returned is not necessarily the same as the one the Address was created with. * * @return a NetworkParameters representing the network the address is intended for, or null if unknown. */ public NetworkParameters getParameters() { // TODO: There should be a more generic way to get all supported networks. NetworkParameters[] networks = new NetworkParameters[] { NetworkParameters.testNet(), NetworkParameters.prodNet() }; foreach (NetworkParameters networkParameters in networks) { NetworkParameters nested = networkParameters; if (networkParameters.acceptableAddressCodes == null) { // Old Java-serialized wallet. This code can eventually be deleted. if (networkParameters.getId().Equals(NetworkParameters.ID_PRODNET)) nested = NetworkParameters.prodNet(); else if (networkParameters.getId().Equals(NetworkParameters.ID_TESTNET)) nested = NetworkParameters.testNet(); } foreach (int code in nested.acceptableAddressCodes) { if (code == this.Version) { return nested; } } } return null; }
/** * Construct an address from parameters and the hash160 form. Example:<p> * * <pre>new Address(NetworkParameters.prodNet(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));</pre> */ public Address(NetworkParameters networkParameters, byte[] hash160) : base(networkParameters.addressHeader, hash160) { if (hash160.Length != 20) { // 160 = 8 * 20 throw new ArgumentException("Addresses are 160-bit hashes, so you must provide 20 bytes"); } }
public Transaction(NetworkParameters n) { // TODO: Complete member initialization this.n = n; }
public TransactionInput(NetworkParameters n, Transaction t, byte[] bytes) { // TODO: Complete member initialization this.n = n; this.t = t; this.bytes = bytes; }
/** Returns a testnet params modified to allow any difficulty target. */ public static NetworkParameters unitTests() { NetworkParameters n = new NetworkParameters(); n = createTestNet(n); n.proofOfWorkLimit = new BigInteger(Hex.Decode("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); n.genesisBlock.setNonce(2); // Make this pass the difficulty test n.genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); n.interval = 10; n.targetTimespan = 200000000; // 6 years. Just a very big number. n.id = "com.google.bitcoin.unittest"; return n; }
Message(NetworkParameters networkParameters, byte[] msg, int offset, bool parseLazy, bool parseRetain, int Length) : this(networkParameters, msg, offset, NetworkParameters.PROTOCOL_VERSION, parseLazy, parseRetain, Length) { }
private static Block createGenesis(NetworkParameters n) { Block genesisBlock = new Block(n); Transaction t = new Transaction(n); // A script containing the difficulty bits and the following message: // // "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" byte[] bytes = Hex.Decode ("04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73"); t.addInput(new TransactionInput(n, t, bytes)); var stream = new MemoryStream(); var scriptPubKeyBytes = new BinaryWriter(stream); Script.writeBytes(scriptPubKeyBytes, Hex.Decode ("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")); scriptPubKeyBytes.Write(Script.OP_CHECKSIG); scriptPubKeyBytes.Flush(); t.addOutput(new TransactionOutput(n, t, stream.ToArray())); genesisBlock.addTransaction(t); return genesisBlock; }
public Block(NetworkParameters n) { // TODO: Complete member initialization this.n = n; }