Java Code Examples for org.bitcoinj.core.ECKey#fromEncrypted()

The following examples show how to use org.bitcoinj.core.ECKey#fromEncrypted() . 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: BasicKeyChain.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void deserializeFromProtobuf(List<Protos.Key> keys) throws UnreadableWalletException {
    lock.lock();
    try {
        checkState(hashToKeys.isEmpty(), "Tried to deserialize into a non-empty chain");
        for (Protos.Key key : keys) {
            if (key.getType() != Protos.Key.Type.ORIGINAL && key.getType() != Protos.Key.Type.ENCRYPTED_SCRYPT_AES)
                continue;
            boolean encrypted = key.getType() == Protos.Key.Type.ENCRYPTED_SCRYPT_AES;
            byte[] priv = key.hasSecretBytes() ? key.getSecretBytes().toByteArray() : null;
            if (!key.hasPublicKey())
                throw new UnreadableWalletException("Public key missing");
            byte[] pub = key.getPublicKey().toByteArray();
            ECKey ecKey;
            if (encrypted) {
                checkState(keyCrypter != null, "This wallet is encrypted but encrypt() was not called prior to deserialization");
                if (!key.hasEncryptedData())
                    throw new UnreadableWalletException("Encrypted private key data missing");
                Protos.EncryptedData proto = key.getEncryptedData();
                EncryptedData e = new EncryptedData(proto.getInitialisationVector().toByteArray(),
                        proto.getEncryptedPrivateKey().toByteArray());
                ecKey = ECKey.fromEncrypted(e, keyCrypter, pub);
            } else {
                if (priv != null)
                    ecKey = ECKey.fromPrivateAndPrecalculatedPublic(priv, pub);
                else
                    ecKey = ECKey.fromPublicOnly(pub);
            }
            ecKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000);
            importKeyLocked(ecKey);
        }
    } finally {
        lock.unlock();
    }
}
 
Example 2
Source File: BasicKeyChain.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void deserializeFromProtobuf(List<Protos.Key> keys) throws UnreadableWalletException {
    lock.lock();
    try {
        checkState(hashToKeys.isEmpty(), "Tried to deserialize into a non-empty chain");
        for (Protos.Key key : keys) {
            if (key.getType() != Protos.Key.Type.ORIGINAL && key.getType() != Protos.Key.Type.ENCRYPTED_SCRYPT_AES)
                continue;
            boolean encrypted = key.getType() == Protos.Key.Type.ENCRYPTED_SCRYPT_AES;
            byte[] priv = key.hasSecretBytes() ? key.getSecretBytes().toByteArray() : null;
            if (!key.hasPublicKey())
                throw new UnreadableWalletException("Public key missing");
            byte[] pub = key.getPublicKey().toByteArray();
            ECKey ecKey;
            if (encrypted) {
                checkState(keyCrypter != null, "This wallet is encrypted but encrypt() was not called prior to deserialization");
                if (!key.hasEncryptedData())
                    throw new UnreadableWalletException("Encrypted private key data missing");
                Protos.EncryptedData proto = key.getEncryptedData();
                EncryptedData e = new EncryptedData(proto.getInitialisationVector().toByteArray(),
                        proto.getEncryptedPrivateKey().toByteArray());
                ecKey = ECKey.fromEncrypted(e, keyCrypter, pub);
            } else {
                if (priv != null)
                    ecKey = ECKey.fromPrivateAndPrecalculatedPublic(priv, pub);
                else
                    ecKey = ECKey.fromPublicOnly(pub);
            }
            ecKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000);
            importKeyLocked(ecKey);
        }
    } finally {
        lock.unlock();
    }
}
 
Example 3
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void deserializeFromProtobuf(List<Protos.Key> keys) throws UnreadableWalletException {
    lock.lock();
    try {
        checkState(hashToKeys.isEmpty(), "Tried to deserialize into a non-empty chain");
        for (Protos.Key key : keys) {
            if (key.getType() != Protos.Key.Type.ORIGINAL && key.getType() != Protos.Key.Type.ENCRYPTED_SCRYPT_AES)
                continue;
            boolean encrypted = key.getType() == Protos.Key.Type.ENCRYPTED_SCRYPT_AES;
            byte[] priv = key.hasSecretBytes() ? key.getSecretBytes().toByteArray() : null;
            if (!key.hasPublicKey())
                throw new UnreadableWalletException("Public key missing");
            byte[] pub = key.getPublicKey().toByteArray();
            ECKey ecKey;
            if (encrypted) {
                checkState(keyCrypter != null, "This wallet is encrypted but encrypt() was not called prior to deserialization");
                if (!key.hasEncryptedData())
                    throw new UnreadableWalletException("Encrypted private key data missing");
                Protos.EncryptedData proto = key.getEncryptedData();
                EncryptedData e = new EncryptedData(proto.getInitialisationVector().toByteArray(),
                        proto.getEncryptedPrivateKey().toByteArray());
                ecKey = ECKey.fromEncrypted(e, keyCrypter, pub);
            } else {
                if (priv != null)
                    ecKey = ECKey.fromPrivateAndPrecalculatedPublic(priv, pub);
                else
                    ecKey = ECKey.fromPublicOnly(pub);
            }
            ecKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000);
            importKeyLocked(ecKey);
        }
    } finally {
        lock.unlock();
    }
}