org.bitcoinj.crypto.KeyCrypter Java Examples

The following examples show how to use org.bitcoinj.crypto.KeyCrypter. 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: Network.java    From cate with MIT License 6 votes vote down vote up
/**
 * Queue a request to decrypt this wallet. This returns immediately, as the
 * actual work is done on the network thread in order to ensure the thread
 * context is correct. Unhandled errors are reported back to Network.
 *
 * @param password password to decrypt the wallet with
 * @param onSuccess callback on success
 * @param onWalletNotEncrypted callback if the wallet is not encrypted
 * @param onCrypterError callback in case of an error in the key crypter
 * @param timeout timeout on queueing the work request
 * @param timeUnit time unit for the timeout
 */
public void decrypt(String password, Consumer<Object> onSuccess,
        Consumer<Object> onWalletNotEncrypted,
        Consumer<KeyCrypterException> onCrypterError,
        final long timeout, final TimeUnit timeUnit) {
    this.networkExecutor.execute((Runnable) () -> {
        final Wallet wallet = wallet();
        if (!wallet.isEncrypted()) {
            onCrypterError.accept(null);
        } else {
            final KeyCrypter keyCrypter = wallet().getKeyCrypter();

            if (keyCrypter == null) {
                throw new IllegalStateException("Wallet is encrypted but has no key crypter.");
            } else {
                try {
                    wallet().decrypt(keyCrypter.deriveKey(password));
                    encrypted.set(false);
                    onSuccess.accept(null);
                } catch (KeyCrypterException ex) {
                    onCrypterError.accept(ex);
                }
            }
        }
    });
}
 
Example #2
Source File: Network.java    From cate with MIT License 5 votes vote down vote up
/**
 * Get a key parameter derived from the given password. This only works if
 * the wallet is, or previously has been, encrypted.
 *
 * @param password the password to derive an AES key from.
 * @return the derived AES key.
 * @throws IllegalStateException if the wallet is not encrypted
 */
public KeyParameter getKeyFromPassword(String password) throws IllegalStateException {
    final KeyCrypter keyCrypter = wallet().getKeyCrypter();
    if (keyCrypter != null) {
        return keyCrypter.deriveKey(password);
    } else {
        throw new IllegalStateException("Wallet does not have a key crypter.");
    }
}
 
Example #3
Source File: WalletsManager.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public DeterministicSeed getDecryptedSeed(KeyParameter aesKey, DeterministicSeed keyChainSeed, KeyCrypter keyCrypter) {
    if (keyCrypter != null) {
        return keyChainSeed.decrypt(keyCrypter, "", aesKey);
    } else {
        log.warn("keyCrypter is null");
        return null;
    }
}
 
Example #4
Source File: WalletsManager.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public DeterministicSeed getDecryptedSeed(KeyParameter aesKey, DeterministicSeed keyChainSeed, KeyCrypter keyCrypter) {
    if (keyCrypter != null) {
        return keyChainSeed.decrypt(keyCrypter, "", aesKey);
    } else {
        log.warn("keyCrypter is null");
        return null;
    }
}
 
Example #5
Source File: WalletService.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Nullable
public KeyCrypter getKeyCrypter() {
    return wallet.getKeyCrypter();
}
 
Example #6
Source File: BtcDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
protected BtcDeterministicKeyChain(KeyCrypter crypter, KeyParameter aesKey, DeterministicKeyChain chain) {
    super(crypter, aesKey, chain);
}
 
Example #7
Source File: BtcDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeterministicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
    return new BtcDeterministicKeyChain(keyCrypter, aesKey, this);
}
 
Example #8
Source File: BtcDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public BtcDeterministicKeyChain(DeterministicSeed seed, KeyCrypter crypter) {
    super(seed, crypter);
}
 
Example #9
Source File: MarriedKeyChain.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
MarriedKeyChain(DeterministicSeed seed, KeyCrypter crypter) {
    super(seed, crypter);
}
 
Example #10
Source File: BisqDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
protected BisqDeterministicKeyChain(KeyCrypter crypter, KeyParameter aesKey, DeterministicKeyChain chain) {
    super(crypter, aesKey, chain);
}
 
Example #11
Source File: BisqDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeterministicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
    return new BisqDeterministicKeyChain(keyCrypter, aesKey, this);
}
 
Example #12
Source File: BisqDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public BisqDeterministicKeyChain(DeterministicSeed seed, KeyCrypter crypter) {
    super(seed, crypter);
}
 
Example #13
Source File: BisqKeyChainFactory.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeterministicKeyChain makeKeyChain(Protos.Key key, Protos.Key firstSubKey, DeterministicSeed seed, KeyCrypter crypter, boolean isMarried) {
    return useBitcoinDeterministicKeyChain ? new BtcDeterministicKeyChain(seed, crypter) : new BisqDeterministicKeyChain(seed, crypter);
}
 
Example #14
Source File: EncryptableKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/** Returns the key crypter used by this key chain, or null if it's not encrypted. */
@Nullable
KeyCrypter getKeyCrypter();
 
Example #15
Source File: MarriedKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
MarriedKeyChain(DeterministicSeed seed, KeyCrypter crypter) {
    super(seed, crypter);
}
 
Example #16
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 #17
Source File: EncryptableKeyChain.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/** Returns the key crypter used by this key chain, or null if it's not encrypted. */
@Nullable
KeyCrypter getKeyCrypter();
 
Example #18
Source File: BisqDeterministicKeyChain.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
protected BisqDeterministicKeyChain(KeyCrypter crypter, KeyParameter aesKey, DeterministicKeyChain chain) {
    super(crypter, aesKey, chain);
}
 
Example #19
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 #20
Source File: BisqKeyChainFactory.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeterministicKeyChain makeKeyChain(Protos.Key key, Protos.Key firstSubKey, DeterministicSeed seed, KeyCrypter crypter, boolean isMarried) {
    return useBitcoinDeterministicKeyChain ? new BtcDeterministicKeyChain(seed, crypter) : new BisqDeterministicKeyChain(seed, crypter);
}
 
Example #21
Source File: WalletService.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@Nullable
public KeyCrypter getKeyCrypter() {
    return wallet.getKeyCrypter();
}
 
Example #22
Source File: BisqDeterministicKeyChain.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public BisqDeterministicKeyChain(DeterministicSeed seed, KeyCrypter crypter) {
    super(seed, crypter);
}
 
Example #23
Source File: BisqDeterministicKeyChain.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeterministicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
    return new BisqDeterministicKeyChain(keyCrypter, aesKey, this);
}
 
Example #24
Source File: BtcDeterministicKeyChain.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public BtcDeterministicKeyChain(DeterministicSeed seed, KeyCrypter crypter) {
    super(seed, crypter);
}
 
Example #25
Source File: BtcDeterministicKeyChain.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeterministicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
    return new BtcDeterministicKeyChain(keyCrypter, aesKey, this);
}
 
Example #26
Source File: BtcDeterministicKeyChain.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
protected BtcDeterministicKeyChain(KeyCrypter crypter, KeyParameter aesKey, DeterministicKeyChain chain) {
    super(crypter, aesKey, chain);
}
 
Example #27
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 #28
Source File: MarriedKeyChain.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
MarriedKeyChain(DeterministicSeed seed, KeyCrypter crypter) {
    super(seed, crypter);
}
 
Example #29
Source File: EncryptableKeyChain.java    From GreenBits with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns a new keychain holding identical/cloned keys to this chain, but encrypted under the given key.
 * Old keys and keychains remain valid and so you should ensure you don't accidentally hold references to them.
 */
EncryptableKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey);
 
Example #30
Source File: KeyChainFactory.java    From green_android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Make a keychain (but not a watching one).
 *
 * @param key the protobuf for the root key
 * @param firstSubKey the protobuf for the first child key (normally the parent of the external subchain)
 * @param seed the seed
 * @param crypter the encrypted/decrypter
 * @param isMarried whether the keychain is leading in a marriage
 */
DeterministicKeyChain makeKeyChain(Protos.Key key, Protos.Key firstSubKey, DeterministicSeed seed, KeyCrypter crypter, boolean isMarried);