Java Code Examples for org.bitcoinj.core.LegacyAddress#fromBase58()

The following examples show how to use org.bitcoinj.core.LegacyAddress#fromBase58() . 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: DeterministicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void derive() throws Exception {
    ECKey key1 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertFalse(key1.isPubKeyOnly());
    ECKey key2 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertFalse(key2.isPubKeyOnly());

    final Address address = LegacyAddress.fromBase58(UNITTEST, "n1bQNoEx8uhmCzzA5JPG6sFdtsUQhwiQJV");
    assertEquals(address, LegacyAddress.fromKey(UNITTEST, key1));
    assertEquals("mnHUcqUVvrfi5kAaXJDQzBb9HsWs78b42R", LegacyAddress.fromKey(UNITTEST, key2).toString());
    assertEquals(key1, chain.findKeyFromPubHash(address.getHash()));
    assertEquals(key2, chain.findKeyFromPubKey(key2.getPubKey()));

    key1.sign(Sha256Hash.ZERO_HASH);
    assertFalse(key1.isPubKeyOnly());

    ECKey key3 = chain.getKey(KeyChain.KeyPurpose.CHANGE);
    assertFalse(key3.isPubKeyOnly());
    assertEquals("mqumHgVDqNzuXNrszBmi7A2UpmwaPMx4HQ", LegacyAddress.fromKey(UNITTEST, key3).toString());
    key3.sign(Sha256Hash.ZERO_HASH);
    assertFalse(key3.isPubKeyOnly());
}
 
Example 2
Source File: DeterministicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void deriveAccountOne() throws Exception {
    long secs = 1389353062L;
    DeterministicKeyChain chain1 = new AccountOneChain(ENTROPY, "", secs);
    ECKey key1 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    ECKey key2 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);

    final Address address = LegacyAddress.fromBase58(UNITTEST, "n2nHHRHs7TiZScTuVhZUkzZfTfVgGYwy6X");
    assertEquals(address, LegacyAddress.fromKey(UNITTEST, key1));
    assertEquals("mnp2j9za5zMuz44vNxrJCXXhZsCdh89QXn", LegacyAddress.fromKey(UNITTEST, key2).toString());
    assertEquals(key1, chain1.findKeyFromPubHash(address.getHash()));
    assertEquals(key2, chain1.findKeyFromPubKey(key2.getPubKey()));

    key1.sign(Sha256Hash.ZERO_HASH);

    ECKey key3 = chain1.getKey(KeyChain.KeyPurpose.CHANGE);
    assertEquals("mpjRhk13rvV7vmnszcUQVYVQzy4HLTPTQU", LegacyAddress.fromKey(UNITTEST, key3).toString());
    key3.sign(Sha256Hash.ZERO_HASH);
}
 
Example 3
Source File: LevelDBBlockStoreTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void basics() throws Exception {
    File f = File.createTempFile("leveldbblockstore", null);
    f.delete();

    Context context = new Context(UNITTEST);
    LevelDBBlockStore store = new LevelDBBlockStore(context, f);
    store.reset();

    // Check the first block in a new store is the genesis block.
    StoredBlock genesis = store.getChainHead();
    assertEquals(UNITTEST.getGenesisBlock(), genesis.getHeader());
    assertEquals(0, genesis.getHeight());

    // Build a new block.
    Address to = LegacyAddress.fromBase58(UNITTEST, "mrj2K6txjo2QBcSmuAzHj4nD1oXSEJE1Qo");
    StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
    store.put(b1);
    store.setChainHead(b1);
    store.close();

    // Check we can get it back out again if we rebuild the store object.
    store = new LevelDBBlockStore(context, f);
    try {
        StoredBlock b2 = store.get(b1.getHeader().getHash());
        assertEquals(b1, b2);
        // Check the chain head was stored correctly also.
        StoredBlock chainHead = store.getChainHead();
        assertEquals(b1, chainHead);
    } finally {
        store.close();
        store.destroy();
    }
}
 
Example 4
Source File: BitcoinURITest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testConvertToBitcoinURI() throws Exception {
    Address goodAddress = LegacyAddress.fromBase58(MAINNET, MAINNET_GOOD_ADDRESS);

    // simple example
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", "AMessage"));

    // example with spaces, ampersand and plus
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello World", "Mess & age + hope"));

    // no amount, label present, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", "glory"));

    // amount present, no label, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), null, "glory"));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), "", "glory"));

    // amount present, label present, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", ""));

    // amount present, no label, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), null, null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), "", ""));

    // no amount, label present, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", null));

    // no amount, no label, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, null, "Agatha"));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, "", "Agatha"));

    // no amount, no label, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, null, null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, "", ""));

    // different scheme
    final NetworkParameters alternativeParameters = new MainNetParams() {
        @Override
        public String getUriScheme() {
            return "test";
        }
    };

    assertEquals("test:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage",
            BitcoinURI.convertToBitcoinURI(LegacyAddress.fromBase58(alternativeParameters, MAINNET_GOOD_ADDRESS), parseCoin("12.34"), "Hello", "AMessage"));
}
 
Example 5
Source File: Address.java    From balzac with Apache License 2.0 4 votes vote down vote up
public static Address fromBase58(String wif) {
    LegacyAddress addr = LegacyAddress.fromBase58(null, wif);
    return new AddressImpl(addr.getHash(), NetworkType.from(addr.getParameters()));
}