Exemplo n.º 1
0
  @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 testRoundTripNormalWallet() throws Exception {
   Wallet wallet1 = roundTrip(myWallet);
   assertEquals(0, wallet1.getTransactions(true).size());
   assertEquals(Coin.ZERO, wallet1.getBalance());
   assertArrayEquals(
       myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
   assertArrayEquals(
       myKey.getPrivKeyBytes(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   assertEquals(
       myKey.getCreationTimeSeconds(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getCreationTimeSeconds());
 }
Exemplo n.º 3
0
 @Test
 public void bloom() throws Exception {
   ECKey key1 = new ECKey();
   ECKey key2 = new ECKey();
   chain.importKeys(key1, key2);
   assertEquals(2, chain.numKeys());
   assertEquals(4, chain.numBloomFilterEntries());
   BloomFilter filter = chain.getFilter(4, 0.001, 100);
   assertTrue(filter.contains(key1.getPubKey()));
   assertTrue(filter.contains(key1.getPubKeyHash()));
   assertTrue(filter.contains(key2.getPubKey()));
   assertTrue(filter.contains(key2.getPubKeyHash()));
   ECKey key3 = new ECKey();
   assertFalse(filter.contains(key3.getPubKey()));
 }
 @Test
 public void testKeys() throws Exception {
   for (int i = 0; i < 20; i++) {
     myKey = new ECKey();
     myAddress = myKey.toAddress(params);
     myWallet = new Wallet(params);
     myWallet.importKey(myKey);
     Wallet wallet1 = roundTrip(myWallet);
     assertArrayEquals(
         myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
     assertArrayEquals(
         myKey.getPrivKeyBytes(),
         wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   }
 }
 @Test
 public void empty() throws Exception {
   // Check the base case of a wallet with one key and no transactions.
   Wallet wallet1 = roundTrip(myWallet);
   assertEquals(0, wallet1.getTransactions(true).size());
   assertEquals(Coin.ZERO, wallet1.getBalance());
   assertArrayEquals(
       myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
   assertArrayEquals(
       myKey.getPrivKeyBytes(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   assertEquals(
       myKey.getCreationTimeSeconds(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getCreationTimeSeconds());
   assertEquals(mScriptCreationTime, wallet1.getWatchedScripts().get(0).getCreationTimeSeconds());
   assertEquals(1, wallet1.getWatchedScripts().size());
   assertEquals(
       ScriptBuilder.createOutputScript(myWatchedKey.toAddress(params)),
       wallet1.getWatchedScripts().get(0));
   assertEquals(WALLET_DESCRIPTION, wallet1.getDescription());
 }
  public String createAddress() {

    ECKey ecKey = new ECKey(new SecureRandom());
    MainNetParams mainNetParams = new MainNetParams();
    try {
      Address address = new Address(mainNetParams, 0, ecKey.getPubKeyHash());

      return address.toString();
    } catch (WrongNetworkException e) {

    }
    return "";
  }