org.bitcoinj.wallet.BasicKeyChain Java Examples

The following examples show how to use org.bitcoinj.wallet.BasicKeyChain. 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: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void serializationUnencrypted() throws UnreadableWalletException {
    Utils.setMockClock();
    Date now = Utils.now();
    final ECKey key1 = new ECKey();
    Utils.rollMockClock(5000);
    final ECKey key2 = new ECKey();
    chain.importKeys(ImmutableList.of(key1, key2));
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(2, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertArrayEquals(key2.getPubKey(), keys.get(1).getPublicKey().toByteArray());
    assertArrayEquals(key1.getPrivKeyBytes(), keys.get(0).getSecretBytes().toByteArray());
    assertArrayEquals(key2.getPrivKeyBytes(), keys.get(1).getSecretBytes().toByteArray());
    long normTime = (long) (Math.floor(now.getTime() / 1000) * 1000);
    assertEquals(normTime, keys.get(0).getCreationTimestamp());
    assertEquals(normTime + 5000 * 1000, keys.get(1).getCreationTimestamp());

    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(2, chain.getKeys().size());
    assertEquals(key1, chain.getKeys().get(0));
    assertEquals(key2, chain.getKeys().get(1));
}
 
Example #2
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void serializationUnencrypted() throws UnreadableWalletException {
    Utils.setMockClock();
    Date now = Utils.now();
    final ECKey key1 = new ECKey();
    Utils.rollMockClock(5000);
    final ECKey key2 = new ECKey();
    chain.importKeys(ImmutableList.of(key1, key2));
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(2, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertArrayEquals(key2.getPubKey(), keys.get(1).getPublicKey().toByteArray());
    assertArrayEquals(key1.getPrivKeyBytes(), keys.get(0).getSecretBytes().toByteArray());
    assertArrayEquals(key2.getPrivKeyBytes(), keys.get(1).getSecretBytes().toByteArray());
    long normTime = (long) (Math.floor(now.getTime() / 1000) * 1000);
    assertEquals(normTime, keys.get(0).getCreationTimestamp());
    assertEquals(normTime + 5000 * 1000, keys.get(1).getCreationTimestamp());

    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(2, chain.getKeys().size());
    assertEquals(key1, chain.getKeys().get(0));
    assertEquals(key2, chain.getKeys().get(1));
}
 
Example #3
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void serializationUnencrypted() throws UnreadableWalletException {
    Utils.setMockClock();
    Date now = Utils.now();
    final ECKey key1 = new ECKey();
    Utils.rollMockClock(5000);
    final ECKey key2 = new ECKey();
    chain.importKeys(ImmutableList.of(key1, key2));
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(2, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertArrayEquals(key2.getPubKey(), keys.get(1).getPublicKey().toByteArray());
    assertArrayEquals(key1.getPrivKeyBytes(), keys.get(0).getSecretBytes().toByteArray());
    assertArrayEquals(key2.getPrivKeyBytes(), keys.get(1).getSecretBytes().toByteArray());
    long normTime = (long) (Math.floor(now.getTime() / 1000) * 1000);
    assertEquals(normTime, keys.get(0).getCreationTimestamp());
    assertEquals(normTime + 5000 * 1000, keys.get(1).getCreationTimestamp());

    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(2, chain.getKeys().size());
    assertEquals(key1, chain.getKeys().get(0));
    assertEquals(key2, chain.getKeys().get(1));
}
 
Example #4
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setup() {
    chain = new BasicKeyChain();
    onKeysAdded = new AtomicReference<>();
    onKeysAddedRan = new AtomicBoolean();
    chain.addEventListener(new AbstractKeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> keys2) {
            onKeysAdded.set(keys2);
            onKeysAddedRan.set(true);
        }
    }, Threading.SAME_THREAD);
}
 
Example #5
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotImportEncryptedKey() {
    final ECKey key1 = new ECKey();
    chain.importKeys(ImmutableList.of(key1));
    chain = chain.toEncrypted("foobar");
    ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertTrue(encryptedKey.isEncrypted());

    BasicKeyChain chain2 = new BasicKeyChain();
    chain2.importKeys(ImmutableList.of(encryptedKey));
}
 
Example #6
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void serializationEncrypted() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    chain.importKeys(key1);
    chain = chain.toEncrypted("foo bar");
    key1 = chain.getKeys().get(0);
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertFalse(keys.get(0).hasSecretBytes());
    assertTrue(keys.get(0).hasEncryptedData());
    chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter()));
    assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey());
    assertTrue(chain.checkPassword("foo bar"));
}
 
Example #7
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void watching() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    ECKey pub = ECKey.fromPublicOnly(key1.getPubKeyPoint());
    chain.importKeys(pub);
    assertEquals(1, chain.numKeys());
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertTrue(keys.get(0).hasPublicKey());
    assertFalse(keys.get(0).hasSecretBytes());
    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(1, chain.numKeys());
    assertFalse(chain.findKeyFromPubKey(pub.getPubKey()).hasPrivKey());
}
 
Example #8
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setup() {
    chain = new BasicKeyChain();
    onKeysAdded = new AtomicReference<>();
    onKeysAddedRan = new AtomicBoolean();
    chain.addEventListener(new AbstractKeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> keys2) {
            onKeysAdded.set(keys2);
            onKeysAddedRan.set(true);
        }
    }, Threading.SAME_THREAD);
}
 
Example #9
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotImportEncryptedKey() {
    final ECKey key1 = new ECKey();
    chain.importKeys(ImmutableList.of(key1));
    chain = chain.toEncrypted("foobar");
    ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertTrue(encryptedKey.isEncrypted());

    BasicKeyChain chain2 = new BasicKeyChain();
    chain2.importKeys(ImmutableList.of(encryptedKey));
}
 
Example #10
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void serializationEncrypted() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    chain.importKeys(key1);
    chain = chain.toEncrypted("foo bar");
    key1 = chain.getKeys().get(0);
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertFalse(keys.get(0).hasSecretBytes());
    assertTrue(keys.get(0).hasEncryptedData());
    chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter()));
    assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey());
    assertTrue(chain.checkPassword("foo bar"));
}
 
Example #11
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void watching() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    ECKey pub = ECKey.fromPublicOnly(key1.getPubKeyPoint());
    chain.importKeys(pub);
    assertEquals(1, chain.numKeys());
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertTrue(keys.get(0).hasPublicKey());
    assertFalse(keys.get(0).hasSecretBytes());
    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(1, chain.numKeys());
    assertFalse(chain.findKeyFromPubKey(pub.getPubKey()).hasPrivKey());
}
 
Example #12
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setup() {
    chain = new BasicKeyChain();
    onKeysAdded = new AtomicReference<>();
    onKeysAddedRan = new AtomicBoolean();
    chain.addEventListener(new AbstractKeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> keys2) {
            onKeysAdded.set(keys2);
            onKeysAddedRan.set(true);
        }
    }, Threading.SAME_THREAD);
}
 
Example #13
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotImportEncryptedKey() {
    final ECKey key1 = new ECKey();
    chain.importKeys(ImmutableList.of(key1));
    chain = chain.toEncrypted("foobar");
    ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertTrue(encryptedKey.isEncrypted());

    BasicKeyChain chain2 = new BasicKeyChain();
    chain2.importKeys(ImmutableList.of(encryptedKey));
}
 
Example #14
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void serializationEncrypted() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    chain.importKeys(key1);
    chain = chain.toEncrypted("foo bar");
    key1 = chain.getKeys().get(0);
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertFalse(keys.get(0).hasSecretBytes());
    assertTrue(keys.get(0).hasEncryptedData());
    chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter()));
    assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey());
    assertTrue(chain.checkPassword("foo bar"));
}
 
Example #15
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void watching() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    ECKey pub = ECKey.fromPublicOnly(key1.getPubKeyPoint());
    chain.importKeys(pub);
    assertEquals(1, chain.numKeys());
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertTrue(keys.get(0).hasPublicKey());
    assertFalse(keys.get(0).hasSecretBytes());
    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(1, chain.numKeys());
    assertFalse(chain.findKeyFromPubKey(pub.getPubKey()).hasPrivKey());
}