Java Code Examples for org.bitcoinj.params.MainNetParams#get()

The following examples show how to use org.bitcoinj.params.MainNetParams#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LevelDB.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    /*
     * This is just a test runner that will download blockchain till block
     * 390000 then exit.
     */
    FullPrunedBlockStore store = new LevelDBFullPrunedBlockStore(
            MainNetParams.get(), args[0], 1000, 100 * 1024 * 1024l,
            10 * 1024 * 1024, 100000, true, 390000);

    FullPrunedBlockChain vChain = new FullPrunedBlockChain(
            MainNetParams.get(), store);
    vChain.setRunScripts(false);

    PeerGroup vPeerGroup = new PeerGroup(MainNetParams.get(), vChain);
    vPeerGroup.setUseLocalhostPeerWhenPossible(true);
    vPeerGroup.addAddress(InetAddress.getLocalHost());

    vPeerGroup.start();
    vPeerGroup.downloadBlockChain();
}
 
Example 2
Source File: BIP32Test.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private void testVector(int testCase) {
    log.info("=======  Test vector {}", testCase);
    HDWTestVector tv = tvs[testCase];
    NetworkParameters params = MainNetParams.get();
    DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(HEX.decode(tv.seed));
    assertEquals(testEncode(tv.priv), testEncode(masterPrivateKey.serializePrivB58(params)));
    assertEquals(testEncode(tv.pub), testEncode(masterPrivateKey.serializePubB58(params)));
    DeterministicHierarchy dh = new DeterministicHierarchy(masterPrivateKey);
    for (int i = 0; i < tv.derived.size(); i++) {
        HDWTestVector.DerivedTestCase tc = tv.derived.get(i);
        log.info("{}", tc.name);
        assertEquals(tc.name, String.format(Locale.US, "Test%d %s", testCase + 1, tc.getPathDescription()));
        int depth = tc.path.length - 1;
        DeterministicKey ehkey = dh.deriveChild(Arrays.asList(tc.path).subList(0, depth), false, true, tc.path[depth]);
        assertEquals(testEncode(tc.priv), testEncode(ehkey.serializePrivB58(params)));
        assertEquals(testEncode(tc.pub), testEncode(ehkey.serializePubB58(params)));
    }
}
 
Example 3
Source File: BitcoinSerializerTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testAddr() throws Exception {
    final NetworkParameters params = MainNetParams.get();
    MessageSerializer serializer = params.getDefaultSerializer();
    // the actual data from https://en.bitcoin.it/wiki/Protocol_specification#addr
    AddressMessage addressMessage = (AddressMessage) serializer.deserialize(ByteBuffer.wrap(ADDRESS_MESSAGE_BYTES));
    assertEquals(1, addressMessage.getAddresses().size());
    PeerAddress peerAddress = addressMessage.getAddresses().get(0);
    assertEquals(8333, peerAddress.getPort());
    assertEquals("10.0.0.1", peerAddress.getAddr().getHostAddress());
    ByteArrayOutputStream bos = new ByteArrayOutputStream(ADDRESS_MESSAGE_BYTES.length);
    serializer.serialize(addressMessage, bos);

    assertEquals(31, addressMessage.getMessageSize());
    addressMessage.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
    assertEquals(61, addressMessage.getMessageSize());
    addressMessage.removeAddress(0);
    assertEquals(31, addressMessage.getMessageSize());

    //this wont be true due to dynamic timestamps.
    //assertTrue(LazyParseByteCacheTest.arrayContains(bos.toByteArray(), addrMessage));
}
 
Example 4
Source File: AbstractFullPrunedBlockChainTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testFirst100KBlocks() throws Exception {
    NetworkParameters params = MainNetParams.get();
    Context context = new Context(params);
    File blockFile = new File(getClass().getResource("first-100k-blocks.dat").getFile());
    BlockFileLoader loader = new BlockFileLoader(params, Arrays.asList(blockFile));
    
    store = createStore(params, 10);
    resetStore(store);
    chain = new FullPrunedBlockChain(context, store);
    for (Block block : loader)
        chain.add(block);
    try {
        store.close();
    } catch (Exception e) {}
}
 
Example 5
Source File: DeterministicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void spendingChainAccountTwo() throws UnreadableWalletException {
    Utils.setMockClock();
    long secs = 1389353062L;
    chain = new AccountTwoChain(ENTROPY, "", secs);
    DeterministicKey firstReceiveKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    DeterministicKey secondReceiveKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    DeterministicKey firstChangeKey = chain.getKey(KeyChain.KeyPurpose.CHANGE);
    DeterministicKey secondChangeKey = chain.getKey(KeyChain.KeyPurpose.CHANGE);

    NetworkParameters params = MainNetParams.get();
    DeterministicKey watchingKey = chain.getWatchingKey();

    final String prv58 = watchingKey.serializePrivB58(params);
    assertEquals("xprv9vL4k9HYXonmzR7UC1ngJ3hTjxkmjLLUo3RexSfUGSWcACHzghWBLJAwW6xzs59XeFizQxFQWtscoTfrF9PSXrUgAtBgr13Nuojax8xTBRz", prv58);
    watchingKey = DeterministicKey.deserializeB58(null, prv58, params);
    watchingKey.setCreationTimeSeconds(secs);
    chain = DeterministicKeyChain.spend(watchingKey);
    assertEquals(secs, chain.getEarliestKeyCreationTime());
    chain.setLookaheadSize(10);
    chain.maybeLookAhead();

    verifySpendableKeyChain(firstReceiveKey, secondReceiveKey, firstChangeKey, secondChangeKey, chain, "spending-wallet-account-two-serialization.txt");
}
 
Example 6
Source File: GaService.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public static String getBech32Prefix(final NetworkParameters params) {
    if (params == MainNetParams.get())
        return "bc";
    if (params == TestNet3Params.get())
        return "tb";
    return "bcrt";
}
 
Example 7
Source File: PrintPeers.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private static void printDNS() throws PeerDiscoveryException {
    long start = System.currentTimeMillis();
    DnsDiscovery dns = new DnsDiscovery(MainNetParams.get());
    dnsPeers = dns.getPeers(0, 10, TimeUnit.SECONDS);
    printPeers(dnsPeers);
    printElapsed(start);
}
 
Example 8
Source File: NetworkData.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@JsonIgnore
public NetworkParameters getNetworkParameters() {
    switch (network == null ? "mainnet" : network) {
    case "mainnet":
        return MainNetParams.get();
    case "testnet":
        return TestNet3Params.get();
    case "regtest":
    case "localtest":
        return RegTestParams.get();
    default:
        return null;
    }
}
 
Example 9
Source File: TransactionOutputTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testP2SHOutputScript() throws Exception {
    String P2SHAddressString = "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU";
    Address P2SHAddress = Address.fromBase58(MainNetParams.get(), P2SHAddressString);
    Script script = ScriptBuilder.createOutputScript(P2SHAddress);
    Transaction tx = new Transaction(MainNetParams.get());
    tx.addOutput(Coin.COIN, script);
    assertEquals(P2SHAddressString, tx.getOutput(0).getAddressFromP2SH(MainNetParams.get()).toString());
}
 
Example 10
Source File: DeterministicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void watchingChain() throws UnreadableWalletException {
    Utils.setMockClock();
    DeterministicKey key1 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    DeterministicKey key2 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    DeterministicKey key3 = chain.getKey(KeyChain.KeyPurpose.CHANGE);
    DeterministicKey key4 = chain.getKey(KeyChain.KeyPurpose.CHANGE);

    NetworkParameters params = MainNetParams.get();
    DeterministicKey watchingKey = chain.getWatchingKey();
    final String pub58 = watchingKey.serializePubB58(params);
    assertEquals("xpub69KR9epSNBM59KLuasxMU5CyKytMJjBP5HEZ5p8YoGUCpM6cM9hqxB9DDPCpUUtqmw5duTckvPfwpoWGQUFPmRLpxs5jYiTf2u6xRMcdhDf", pub58);
    watchingKey = DeterministicKey.deserializeB58(null, pub58, params);
    watchingKey.setCreationTimeSeconds(100000);
    chain = DeterministicKeyChain.watch(watchingKey);
    assertEquals(100000, chain.getEarliestKeyCreationTime());
    chain.setLookaheadSize(10);
    chain.maybeLookAhead();

    assertEquals(key1.getPubKeyPoint(), chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS).getPubKeyPoint());
    assertEquals(key2.getPubKeyPoint(), chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS).getPubKeyPoint());
    final DeterministicKey key = chain.getKey(KeyChain.KeyPurpose.CHANGE);
    assertEquals(key3.getPubKeyPoint(), key.getPubKeyPoint());
    try {
        // Can't sign with a key from a watching chain.
        key.sign(Sha256Hash.ZERO_HASH);
        fail();
    } catch (ECKey.MissingPrivateKeyException e) {
        // Ignored.
    }
    // Test we can serialize and deserialize a watching chain OK.
    List<Protos.Key> serialization = chain.serializeToProtobuf();
    checkSerialization(serialization, "watching-wallet-serialization.txt");
    chain = DeterministicKeyChain.fromProtobuf(serialization, null).get(0);
    final DeterministicKey rekey4 = chain.getKey(KeyChain.KeyPurpose.CHANGE);
    assertEquals(key4.getPubKeyPoint(), rekey4.getPubKeyPoint());
}
 
Example 11
Source File: Util.java    From jelectrum with MIT License 5 votes vote down vote up
public static NetworkParameters getNetworkParameters(Config config)
{
  NetworkParameters network_params = MainNetParams.get();
  if (config.getBoolean("testnet"))
  { 
    network_params = TestNet3Params.get();
  }
  return network_params;

}
 
Example 12
Source File: PeerAddressTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testPeerAddressRoundtrip() throws Exception {
    // copied verbatim from https://en.bitcoin.it/wiki/Protocol_specification#Network_address
    String fromSpec = "010000000000000000000000000000000000ffff0a000001208d";
    PeerAddress pa = new PeerAddress(MainNetParams.get(),
            HEX.decode(fromSpec), 0, 0);
    String reserialized = Utils.HEX.encode(pa.unsafeBitcoinSerialize());
    assertEquals(reserialized,fromSpec );
}
 
Example 13
Source File: TransactionOutputTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testP2SHOutputScript() throws Exception {
    String P2SHAddressString = "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU";
    Address P2SHAddress = Address.fromBase58(MainNetParams.get(), P2SHAddressString);
    Script script = ScriptBuilder.createOutputScript(P2SHAddress);
    Transaction tx = new Transaction(MainNetParams.get());
    tx.addOutput(Coin.COIN, script);
    assertEquals(P2SHAddressString, tx.getOutput(0).getAddressFromP2SH(MainNetParams.get()).toString());
}
 
Example 14
Source File: PeerAddressTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testPeerAddressRoundtrip() throws Exception {
    // copied verbatim from https://en.bitcoin.it/wiki/Protocol_specification#Network_address
    String fromSpec = "010000000000000000000000000000000000ffff0a000001208d";
    PeerAddress pa = new PeerAddress(MainNetParams.get(),
            HEX.decode(fromSpec), 0, 0);
    String reserialized = Utils.HEX.encode(pa.unsafeBitcoinSerialize());
    assertEquals(reserialized,fromSpec );
}
 
Example 15
Source File: PeerAddressTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testBitcoinSerialize() throws Exception {
    PeerAddress pa = new PeerAddress(MainNetParams.get(), InetAddress.getByName(null), 8333, 0, BigInteger.ZERO);
    assertEquals("000000000000000000000000000000000000ffff7f000001208d",
            Utils.HEX.encode(pa.bitcoinSerialize()));
}
 
Example 16
Source File: BitcoinConfig.java    From consensusj with Apache License 2.0 4 votes vote down vote up
@Bean
public NetworkParameters networkParameters() {
    return MainNetParams.get();
}
 
Example 17
Source File: BSQ.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public Mainnet() {
    super(Network.MAINNET, MainNetParams.get());
}
 
Example 18
Source File: PrivateKey.java    From evt4j with MIT License 4 votes vote down vote up
public String toWif() {
    ECKey decompressedKey = key.decompress();
    NetworkParameters networkParameters = MainNetParams.get();
    return decompressedKey.getPrivateKeyAsWiF(networkParameters);
}
 
Example 19
Source File: BlockImporter.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws BlockStoreException, VerificationException, PrunedException {
    System.out.println("USAGE: BlockImporter (prod|test) (H2|Disk|MemFull|Mem|SPV) [blockStore]");
    System.out.println("       blockStore is required unless type is Mem or MemFull");
    System.out.println("       eg BlockImporter prod H2 /home/user/bitcoinj.h2store");
    System.out.println("       Does full verification if the store supports it");
    Preconditions.checkArgument(args.length == 2 || args.length == 3);
    
    NetworkParameters params;
    if (args[0].equals("test"))
        params = TestNet3Params.get();
    else
        params = MainNetParams.get();
    
    BlockStore store;
    if (args[1].equals("H2")) {
        Preconditions.checkArgument(args.length == 3);
        store = new H2FullPrunedBlockStore(params, args[2], 100);
    } else if (args[1].equals("MemFull")) {
        Preconditions.checkArgument(args.length == 2);
        store = new MemoryFullPrunedBlockStore(params, 100);
    } else if (args[1].equals("Mem")) {
        Preconditions.checkArgument(args.length == 2);
        store = new MemoryBlockStore(params);
    } else if (args[1].equals("SPV")) {
        Preconditions.checkArgument(args.length == 3);
        store = new SPVBlockStore(params, new File(args[2]));
    } else {
        System.err.println("Unknown store " + args[1]);
        return;
    }
    
    AbstractBlockChain chain = null;
    if (store instanceof FullPrunedBlockStore)
        chain = new FullPrunedBlockChain(params, (FullPrunedBlockStore) store);
    else
        chain = new BlockChain(params, store);
    
    BlockFileLoader loader = new BlockFileLoader(params, BlockFileLoader.getReferenceClientBlockFileList());
    
    for (Block block : loader)
        chain.add(block);
}
 
Example 20
Source File: SeedPeersTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void getPeer_one() throws Exception{
    SeedPeers seedPeers = new SeedPeers(MainNetParams.get());
    assertThat(seedPeers.getPeer(), notNullValue());
}