Ejemplo n.º 1
0
  @Test
  public void deterministicUpgradeUnencrypted() throws Exception {
    // Check that a group that contains only random keys has its HD chain created using the private
    // key bytes of
    // the oldest random key, so upgrading the same wallet twice gives the same outcome.
    group = new KeyChainGroup(params);
    group.setLookaheadSize(LOOKAHEAD_SIZE); // Don't want slow tests.
    ECKey key1 = new ECKey();
    Utils.rollMockClock(86400);
    ECKey key2 = new ECKey();
    group.importKeys(key2, key1);

    List<Protos.Key> protobufs = group.serializeToProtobuf();
    group.upgradeToDeterministic(0, null);
    assertFalse(group.isDeterministicUpgradeRequired());
    DeterministicKey dkey1 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    DeterministicSeed seed1 = group.getActiveKeyChain().getSeed();
    assertNotNull(seed1);

    group = KeyChainGroup.fromProtobufUnencrypted(params, protobufs);
    group.upgradeToDeterministic(0, null); // Should give same result as last time.
    DeterministicKey dkey2 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    DeterministicSeed seed2 = group.getActiveKeyChain().getSeed();
    assertEquals(seed1, seed2);
    assertEquals(dkey1, dkey2);

    // Check we used the right (oldest) key despite backwards import order.
    byte[] truncatedBytes = Arrays.copyOfRange(key1.getSecretBytes(), 0, 16);
    assertArrayEquals(seed1.getEntropyBytes(), truncatedBytes);
  }
Ejemplo n.º 2
0
 @Test
 public void constructFromSeed() throws Exception {
   ECKey key1 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
   final DeterministicSeed seed = checkNotNull(group.getActiveKeyChain().getSeed());
   KeyChainGroup group2 = new KeyChainGroup(params, seed);
   group2.setLookaheadSize(5);
   ECKey key2 = group2.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
   assertEquals(key1, key2);
 }
Ejemplo n.º 3
0
 private KeyChainGroup createMarriedKeyChainGroup() {
   byte[] entropy =
       Sha256Hash.create("don't use a seed like this in real life".getBytes()).getBytes();
   DeterministicSeed seed =
       new DeterministicSeed(entropy, "", MnemonicCode.BIP39_STANDARDISATION_TIME_SECS);
   KeyChainGroup group = new KeyChainGroup(params, seed, ImmutableList.of(watchingAccountKey));
   group.setLookaheadSize(LOOKAHEAD_SIZE);
   group.getActiveKeyChain();
   return group;
 }
Ejemplo n.º 4
0
  @Before
  public void setup() {
    BriefLogFormatter.init();
    Utils.setMockClock();
    group = new KeyChainGroup(params);
    group.setLookaheadSize(LOOKAHEAD_SIZE); // Don't want slow tests.
    group.getActiveKeyChain(); // Force create a chain.

    watchingAccountKey = DeterministicKey.deserializeB58(null, XPUB);
  }
Ejemplo n.º 5
0
 @Test
 public void encryptionWhilstEmpty() throws Exception {
   group = new KeyChainGroup(params);
   group.setLookaheadSize(5);
   KeyCrypterScrypt scrypt = new KeyCrypterScrypt(2);
   final KeyParameter aesKey = scrypt.deriveKey("password");
   group.encrypt(scrypt, aesKey);
   assertTrue(group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS).isEncrypted());
   final ECKey key = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
   group.decrypt(aesKey);
   assertFalse(checkNotNull(group.findKeyFromPubKey(key.getPubKey())).isEncrypted());
 }
Ejemplo n.º 6
0
 @Test
 public void serializeWatching() throws Exception {
   group = new KeyChainGroup(params, watchingAccountKey);
   group.setLookaheadSize(LOOKAHEAD_SIZE);
   group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
   group.freshKey(KeyChain.KeyPurpose.CHANGE);
   group.getBloomFilterElementCount(); // Force lookahead.
   List<Protos.Key> protoKeys1 = group.serializeToProtobuf();
   assertEquals(
       3 + (group.getLookaheadSize() + group.getLookaheadThreshold() + 1) * 2, protoKeys1.size());
   group = KeyChainGroup.fromProtobufUnencrypted(params, protoKeys1);
   assertEquals(
       3 + (group.getLookaheadSize() + group.getLookaheadThreshold() + 1) * 2,
       group.serializeToProtobuf().size());
 }
Ejemplo n.º 7
0
 @Test
 public void deterministicUpgradeRotating() throws Exception {
   group = new KeyChainGroup(params);
   group.setLookaheadSize(LOOKAHEAD_SIZE); // Don't want slow tests.
   long now = Utils.currentTimeSeconds();
   ECKey key1 = new ECKey();
   Utils.rollMockClock(86400);
   ECKey key2 = new ECKey();
   Utils.rollMockClock(86400);
   ECKey key3 = new ECKey();
   group.importKeys(key2, key1, key3);
   group.upgradeToDeterministic(now + 10, null);
   DeterministicSeed seed = group.getActiveKeyChain().getSeed();
   assertNotNull(seed);
   // Check we used the right key: oldest non rotating.
   byte[] truncatedBytes = Arrays.copyOfRange(key2.getSecretBytes(), 0, 16);
   assertArrayEquals(seed.getEntropyBytes(), truncatedBytes);
 }