org.bitcoinj.wallet.Protos.Wallet.EncryptionType Java Examples

The following examples show how to use org.bitcoinj.wallet.Protos.Wallet.EncryptionType. 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: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void encryptionDecryptionAESBasic() throws Exception {
    Wallet encryptedWallet = new Wallet(UNITTEST);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
    KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1);

    assertEquals(EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType());
    assertTrue(encryptedWallet.checkPassword(PASSWORD1));
    assertTrue(encryptedWallet.checkAESKey(aesKey));
    assertFalse(encryptedWallet.checkPassword(WRONG_PASSWORD));
    assertNotNull("The keyCrypter is missing but should not be", keyCrypter);
    encryptedWallet.decrypt(aesKey);

    // Wallet should now be unencrypted.
    assertNull("Wallet is not an unencrypted wallet", encryptedWallet.getKeyCrypter());
    try {
        encryptedWallet.checkPassword(PASSWORD1);
        fail();
    } catch (IllegalStateException e) {
    }
}
 
Example #2
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void encryptionDecryptionBadPassword() throws Exception {
    Wallet encryptedWallet = new Wallet(UNITTEST);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
    KeyParameter wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD);

    // Check the wallet is currently encrypted
    assertEquals("Wallet is not an encrypted wallet", EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType());
    assertFalse(encryptedWallet.checkAESKey(wrongAesKey));

    // Check that the wrong password does not decrypt the wallet.
    try {
        encryptedWallet.decrypt(wrongAesKey);
        fail("Incorrectly decoded wallet with wrong password");
    } catch (KeyCrypterException ede) {
        // Expected.
    }
}
 
Example #3
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void encryptionDecryptionAESBasic() throws Exception {
    Wallet encryptedWallet = new Wallet(PARAMS);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
    KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1);

    assertEquals(EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType());
    assertTrue(encryptedWallet.checkPassword(PASSWORD1));
    assertTrue(encryptedWallet.checkAESKey(aesKey));
    assertFalse(encryptedWallet.checkPassword(WRONG_PASSWORD));
    assertNotNull("The keyCrypter is missing but should not be", keyCrypter);
    encryptedWallet.decrypt(aesKey);

    // Wallet should now be unencrypted.
    assertNull("Wallet is not an unencrypted wallet", encryptedWallet.getKeyCrypter());
    try {
        encryptedWallet.checkPassword(PASSWORD1);
        fail();
    } catch (IllegalStateException e) {
    }
}
 
Example #4
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void encryptionDecryptionBadPassword() throws Exception {
    Wallet encryptedWallet = new Wallet(PARAMS);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
    KeyParameter wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD);

    // Check the wallet is currently encrypted
    assertEquals("Wallet is not an encrypted wallet", EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType());
    assertFalse(encryptedWallet.checkAESKey(wrongAesKey));

    // Check that the wrong password does not decrypt the wallet.
    try {
        encryptedWallet.decrypt(wrongAesKey);
        fail("Incorrectly decoded wallet with wrong password");
    } catch (KeyCrypterException ede) {
        // Expected.
    }
}
 
Example #5
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void encryptionDecryptionAESBasic() throws Exception {
    Wallet encryptedWallet = new Wallet(PARAMS);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
    KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1);

    assertEquals(EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType());
    assertTrue(encryptedWallet.checkPassword(PASSWORD1));
    assertTrue(encryptedWallet.checkAESKey(aesKey));
    assertFalse(encryptedWallet.checkPassword(WRONG_PASSWORD));
    assertNotNull("The keyCrypter is missing but should not be", keyCrypter);
    encryptedWallet.decrypt(aesKey);

    // Wallet should now be unencrypted.
    assertNull("Wallet is not an unencrypted wallet", encryptedWallet.getKeyCrypter());
    try {
        encryptedWallet.checkPassword(PASSWORD1);
        fail();
    } catch (IllegalStateException e) {
    }
}
 
Example #6
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void encryptionDecryptionBadPassword() throws Exception {
    Wallet encryptedWallet = new Wallet(PARAMS);
    encryptedWallet.encrypt(PASSWORD1);
    KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
    KeyParameter wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD);

    // Check the wallet is currently encrypted
    assertEquals("Wallet is not an encrypted wallet", EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType());
    assertFalse(encryptedWallet.checkAESKey(wrongAesKey));

    // Check that the wrong password does not decrypt the wallet.
    try {
        encryptedWallet.decrypt(wrongAesKey);
        fail("Incorrectly decoded wallet with wrong password");
    } catch (KeyCrypterException ede) {
        // Expected.
    }
}
 
Example #7
Source File: KeyCrypterScrypt.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter
 * can understand.
 */
@Override
public EncryptionType getUnderstoodEncryptionType() {
    return EncryptionType.ENCRYPTED_SCRYPT_AES;
}
 
Example #8
Source File: WalletProtobufSerializer.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Converts the given wallet to the object representation of the protocol buffers. This can be modified, or
 * additional data fields set, before serialization takes place.
 */
public Protos.Wallet walletToProto(Wallet wallet) {
    Protos.Wallet.Builder walletBuilder = Protos.Wallet.newBuilder();
    walletBuilder.setNetworkIdentifier(wallet.getNetworkParameters().getId());
    if (wallet.getDescription() != null) {
        walletBuilder.setDescription(wallet.getDescription());
    }

    for (WalletTransaction wtx : wallet.getWalletTransactions()) {
        Protos.Transaction txProto = makeTxProto(wtx);
        walletBuilder.addTransaction(txProto);
    }

    walletBuilder.addAllKey(wallet.serializeKeyChainGroupToProtobuf());

    for (Script script : wallet.getWatchedScripts()) {
        Protos.Script protoScript =
                Protos.Script.newBuilder()
                        .setProgram(ByteString.copyFrom(script.getProgram()))
                        .setCreationTimestamp(script.getCreationTimeSeconds() * 1000)
                        .build();

        walletBuilder.addWatchedScript(protoScript);
    }

    // Populate the lastSeenBlockHash field.
    Sha256Hash lastSeenBlockHash = wallet.getLastBlockSeenHash();
    if (lastSeenBlockHash != null) {
        walletBuilder.setLastSeenBlockHash(hashToByteString(lastSeenBlockHash));
        walletBuilder.setLastSeenBlockHeight(wallet.getLastBlockSeenHeight());
    }
    if (wallet.getLastBlockSeenTimeSecs() > 0)
        walletBuilder.setLastSeenBlockTimeSecs(wallet.getLastBlockSeenTimeSecs());

    // Populate the scrypt parameters.
    KeyCrypter keyCrypter = wallet.getKeyCrypter();
    if (keyCrypter == null) {
        // The wallet is unencrypted.
        walletBuilder.setEncryptionType(EncryptionType.UNENCRYPTED);
    } else {
        // The wallet is encrypted.
        walletBuilder.setEncryptionType(keyCrypter.getUnderstoodEncryptionType());
        if (keyCrypter instanceof KeyCrypterScrypt) {
            KeyCrypterScrypt keyCrypterScrypt = (KeyCrypterScrypt) keyCrypter;
            walletBuilder.setEncryptionParameters(keyCrypterScrypt.getScryptParameters());
        } else {
            // Some other form of encryption has been specified that we do not know how to persist.
            throw new RuntimeException("The wallet has encryption of type '" + keyCrypter.getUnderstoodEncryptionType() + "' but this WalletProtobufSerializer does not know how to persist this.");
        }
    }

    if (wallet.getKeyRotationTime() != null) {
        long timeSecs = wallet.getKeyRotationTime().getTime() / 1000;
        walletBuilder.setKeyRotationTime(timeSecs);
    }

    populateExtensions(wallet, walletBuilder);

    for (Map.Entry<String, ByteString> entry : wallet.getTags().entrySet()) {
        Protos.Tag.Builder tag = Protos.Tag.newBuilder().setTag(entry.getKey()).setData(entry.getValue());
        walletBuilder.addTags(tag);
    }

    // Populate the wallet version.
    walletBuilder.setVersion(wallet.getVersion());

    return walletBuilder.build();
}
 
Example #9
Source File: KeyCrypterScrypt.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter
 * can understand.
 */
@Override
public EncryptionType getUnderstoodEncryptionType() {
    return EncryptionType.ENCRYPTED_SCRYPT_AES;
}
 
Example #10
Source File: WalletProtobufSerializer.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Converts the given wallet to the object representation of the protocol buffers. This can be modified, or
 * additional data fields set, before serialization takes place.
 */
public Protos.Wallet walletToProto(Wallet wallet) {
    Protos.Wallet.Builder walletBuilder = Protos.Wallet.newBuilder();
    walletBuilder.setNetworkIdentifier(wallet.getNetworkParameters().getId());
    if (wallet.getDescription() != null) {
        walletBuilder.setDescription(wallet.getDescription());
    }

    for (WalletTransaction wtx : wallet.getWalletTransactions()) {
        Protos.Transaction txProto = makeTxProto(wtx);
        walletBuilder.addTransaction(txProto);
    }

    walletBuilder.addAllKey(wallet.serializeKeyChainGroupToProtobuf());

    for (Script script : wallet.getWatchedScripts()) {
        Protos.Script protoScript =
                Protos.Script.newBuilder()
                        .setProgram(ByteString.copyFrom(script.getProgram()))
                        .setCreationTimestamp(script.getCreationTimeSeconds() * 1000)
                        .build();

        walletBuilder.addWatchedScript(protoScript);
    }

    // Populate the lastSeenBlockHash field.
    Sha256Hash lastSeenBlockHash = wallet.getLastBlockSeenHash();
    if (lastSeenBlockHash != null) {
        walletBuilder.setLastSeenBlockHash(hashToByteString(lastSeenBlockHash));
        walletBuilder.setLastSeenBlockHeight(wallet.getLastBlockSeenHeight());
    }
    if (wallet.getLastBlockSeenTimeSecs() > 0)
        walletBuilder.setLastSeenBlockTimeSecs(wallet.getLastBlockSeenTimeSecs());

    // Populate the scrypt parameters.
    KeyCrypter keyCrypter = wallet.getKeyCrypter();
    if (keyCrypter == null) {
        // The wallet is unencrypted.
        walletBuilder.setEncryptionType(EncryptionType.UNENCRYPTED);
    } else {
        // The wallet is encrypted.
        walletBuilder.setEncryptionType(keyCrypter.getUnderstoodEncryptionType());
        if (keyCrypter instanceof KeyCrypterScrypt) {
            KeyCrypterScrypt keyCrypterScrypt = (KeyCrypterScrypt) keyCrypter;
            walletBuilder.setEncryptionParameters(keyCrypterScrypt.getScryptParameters());
        } else {
            // Some other form of encryption has been specified that we do not know how to persist.
            throw new RuntimeException("The wallet has encryption of type '" + keyCrypter.getUnderstoodEncryptionType() + "' but this WalletProtobufSerializer does not know how to persist this.");
        }
    }

    if (wallet.getKeyRotationTime() != null) {
        long timeSecs = wallet.getKeyRotationTime().getTime() / 1000;
        walletBuilder.setKeyRotationTime(timeSecs);
    }

    populateExtensions(wallet, walletBuilder);

    for (Map.Entry<String, ByteString> entry : wallet.getTags().entrySet()) {
        Protos.Tag.Builder tag = Protos.Tag.newBuilder().setTag(entry.getKey()).setData(entry.getValue());
        walletBuilder.addTags(tag);
    }

    for (TransactionSigner signer : wallet.getTransactionSigners()) {
        // do not serialize LocalTransactionSigner as it's being added implicitly
        if (signer instanceof LocalTransactionSigner)
            continue;
        Protos.TransactionSigner.Builder protoSigner = Protos.TransactionSigner.newBuilder();
        protoSigner.setClassName(signer.getClass().getName());
        protoSigner.setData(ByteString.copyFrom(signer.serialize()));
        walletBuilder.addTransactionSigners(protoSigner);
    }

    // Populate the wallet version.
    walletBuilder.setVersion(wallet.getVersion());

    return walletBuilder.build();
}
 
Example #11
Source File: KeyCrypterScrypt.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter
 * can understand.
 */
@Override
public EncryptionType getUnderstoodEncryptionType() {
    return EncryptionType.ENCRYPTED_SCRYPT_AES;
}
 
Example #12
Source File: WalletProtobufSerializer.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Converts the given wallet to the object representation of the protocol buffers. This can be modified, or
 * additional data fields set, before serialization takes place.
 */
public Protos.Wallet walletToProto(Wallet wallet) {
    Protos.Wallet.Builder walletBuilder = Protos.Wallet.newBuilder();
    walletBuilder.setNetworkIdentifier(wallet.getNetworkParameters().getId());
    if (wallet.getDescription() != null) {
        walletBuilder.setDescription(wallet.getDescription());
    }

    for (WalletTransaction wtx : wallet.getWalletTransactions()) {
        Protos.Transaction txProto = makeTxProto(wtx);
        walletBuilder.addTransaction(txProto);
    }

    walletBuilder.addAllKey(wallet.serializeKeyChainGroupToProtobuf());

    for (Script script : wallet.getWatchedScripts()) {
        Protos.Script protoScript =
                Protos.Script.newBuilder()
                        .setProgram(ByteString.copyFrom(script.getProgram()))
                        .setCreationTimestamp(script.getCreationTimeSeconds() * 1000)
                        .build();

        walletBuilder.addWatchedScript(protoScript);
    }

    // Populate the lastSeenBlockHash field.
    Sha256Hash lastSeenBlockHash = wallet.getLastBlockSeenHash();
    if (lastSeenBlockHash != null) {
        walletBuilder.setLastSeenBlockHash(hashToByteString(lastSeenBlockHash));
        walletBuilder.setLastSeenBlockHeight(wallet.getLastBlockSeenHeight());
    }
    if (wallet.getLastBlockSeenTimeSecs() > 0)
        walletBuilder.setLastSeenBlockTimeSecs(wallet.getLastBlockSeenTimeSecs());

    // Populate the scrypt parameters.
    KeyCrypter keyCrypter = wallet.getKeyCrypter();
    if (keyCrypter == null) {
        // The wallet is unencrypted.
        walletBuilder.setEncryptionType(EncryptionType.UNENCRYPTED);
    } else {
        // The wallet is encrypted.
        walletBuilder.setEncryptionType(keyCrypter.getUnderstoodEncryptionType());
        if (keyCrypter instanceof KeyCrypterScrypt) {
            KeyCrypterScrypt keyCrypterScrypt = (KeyCrypterScrypt) keyCrypter;
            walletBuilder.setEncryptionParameters(keyCrypterScrypt.getScryptParameters());
        } else {
            // Some other form of encryption has been specified that we do not know how to persist.
            throw new RuntimeException("The wallet has encryption of type '" + keyCrypter.getUnderstoodEncryptionType() + "' but this WalletProtobufSerializer does not know how to persist this.");
        }
    }

    if (wallet.getKeyRotationTime() != null) {
        long timeSecs = wallet.getKeyRotationTime().getTime() / 1000;
        walletBuilder.setKeyRotationTime(timeSecs);
    }

    populateExtensions(wallet, walletBuilder);

    for (Map.Entry<String, ByteString> entry : wallet.getTags().entrySet()) {
        Protos.Tag.Builder tag = Protos.Tag.newBuilder().setTag(entry.getKey()).setData(entry.getValue());
        walletBuilder.addTags(tag);
    }

    for (TransactionSigner signer : wallet.getTransactionSigners()) {
        // do not serialize LocalTransactionSigner as it's being added implicitly
        if (signer instanceof LocalTransactionSigner)
            continue;
        Protos.TransactionSigner.Builder protoSigner = Protos.TransactionSigner.newBuilder();
        protoSigner.setClassName(signer.getClass().getName());
        protoSigner.setData(ByteString.copyFrom(signer.serialize()));
        walletBuilder.addTransactionSigners(protoSigner);
    }

    // Populate the wallet version.
    walletBuilder.setVersion(wallet.getVersion());

    return walletBuilder.build();
}
 
Example #13
Source File: KeyCrypter.java    From bcm-android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter
 * can understand.
 */
EncryptionType getUnderstoodEncryptionType();
 
Example #14
Source File: KeyCrypter.java    From green_android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter
 * can understand.
 */
EncryptionType getUnderstoodEncryptionType();
 
Example #15
Source File: KeyCrypter.java    From GreenBits with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter
 * can understand.
 */
EncryptionType getUnderstoodEncryptionType();