@Test public void importKeys() { long now = Utils.currentTimeSeconds(); Utils.setMockClock(now); final ECKey key1 = new ECKey(); Utils.rollMockClock(86400); final ECKey key2 = new ECKey(); final ArrayList<ECKey> keys = Lists.newArrayList(key1, key2); // Import two keys, check the event is correct. assertEquals(2, chain.importKeys(keys)); assertEquals(2, chain.numKeys()); assertTrue(onKeysAddedRan.getAndSet(false)); assertArrayEquals(keys.toArray(), onKeysAdded.get().toArray()); assertEquals(now, chain.getEarliestKeyCreationTime()); // Check we ignore duplicates. final ECKey newKey = new ECKey(); keys.add(newKey); assertEquals(1, chain.importKeys(keys)); assertTrue(onKeysAddedRan.getAndSet(false)); assertEquals(newKey, onKeysAdded.getAndSet(null).get(0)); assertEquals(0, chain.importKeys(keys)); assertFalse(onKeysAddedRan.getAndSet(false)); assertNull(onKeysAdded.get()); assertTrue(chain.hasKey(key1)); assertTrue(chain.hasKey(key2)); assertEquals(key1, chain.findKeyFromPubHash(key1.getPubKeyHash())); assertEquals(key2, chain.findKeyFromPubKey(key2.getPubKey())); assertNull(chain.findKeyFromPubKey(key2.getPubKeyHash())); }
@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()); }
@Test public void encryptDecrypt() { final ECKey key1 = new ECKey(); chain.importKeys(key1, new ECKey()); final String PASSWORD = "******"; chain = chain.toEncrypted(PASSWORD); final KeyCrypter keyCrypter = chain.getKeyCrypter(); assertNotNull(keyCrypter); assertTrue(keyCrypter instanceof KeyCrypterScrypt); assertTrue(chain.checkPassword(PASSWORD)); assertFalse(chain.checkPassword("wrong")); ECKey key = chain.findKeyFromPubKey(key1.getPubKey()); assertTrue(key.isEncrypted()); assertTrue(key.isPubKeyOnly()); assertFalse(key.isWatching()); assertNull(key.getSecretBytes()); try { // Don't allow import of an unencrypted key. chain.importKeys(new ECKey()); fail(); } catch (KeyCrypterException e) { } try { chain.toDecrypted(keyCrypter.deriveKey("wrong")); fail(); } catch (KeyCrypterException e) { } chain = chain.toDecrypted(PASSWORD); key = chain.findKeyFromPubKey(key1.getPubKey()); assertFalse(key.isEncrypted()); assertFalse(key.isPubKeyOnly()); assertFalse(key.isWatching()); key.getPrivKeyBytes(); }