@Override public NxtFamilyKey toDecrypted(KeyParameter aesKey) { checkState(isEncrypted(), "Key is not encrypted"); checkNotNull(getKeyCrypter(), "Key chain not encrypted"); DeterministicKey entropyDecrypted = entropy.decrypt(getKeyCrypter(), aesKey); return new NxtFamilyKey(entropyDecrypted, publicKey); }
/** * Returns the key chain found in the given list of keys. Used for unencrypted chains */ public static NxtFamilyKey fromProtobuf(List<Protos.Key> keys) throws UnreadableWalletException { return fromProtobuf(keys, null); }
public void signTransaction(NxtSendRequest request) { checkArgument(request.isCompleted(), "Send request is not completed"); checkArgument(request.tx != null, "No transaction found in send request"); Transaction tx = request.tx.getRawTransaction(); byte[] privateKey; if (rootKey.isEncrypted()) { checkArgument(request.aesKey != null, "Wallet is encrypted but no decryption key provided"); privateKey = rootKey.toDecrypted(request.aesKey).getPrivateKey(); } else { privateKey = rootKey.getPrivateKey(); } tx.sign(privateKey); Arrays.fill(privateKey, (byte) 0); // clear private key }
@Override public boolean checkPassword(CharSequence password) { checkNotNull(password, "Password is null"); checkState(getKeyCrypter() != null, "Key chain not encrypted"); return checkAESKey(getKeyCrypter().deriveKey(password)); }
@Override public NxtFamilyKey toDecrypted(CharSequence password) { checkNotNull(password, "Attempt to decrypt with a null password."); checkArgument(password.length() > 0, "Attempt to decrypt with an empty password."); KeyCrypter crypter = getKeyCrypter(); checkState(crypter != null, "Chain not encrypted"); KeyParameter derivedKey = crypter.deriveKey(password); return toDecrypted(derivedKey); }
@Test public void testHDAccountNxt() throws MnemonicException, UnreadableWalletException { DeterministicSeed seed = new DeterministicSeed(recoveryPhrase, null, "", 0); DeterministicKey masterKey = HDKeyDerivation.createMasterPrivateKey(seed.getSeedBytes()); DeterministicHierarchy hierarchy = new DeterministicHierarchy(masterKey); DeterministicKey entropy = hierarchy.get(NxtMain.get().getBip44Path(0), false, true); NxtFamilyKey nxtKey = new NxtFamilyKey(entropy, null, null); byte[] privateKey = nxtKey.getPrivateKey(); byte[] publicKey = nxtKey.getPublicKey(); NxtAddress address = new NxtAddress(NxtMain.get(), publicKey); assertArrayEquals(nxtPrivateKey, privateKey); assertArrayEquals(nxtPublicKey, publicKey); assertEquals(nxtRsAddress, address.toString()); assertEquals(nxtAccountId, address.getAccountId()); }
public NxtFamilyWallet(DeterministicKey entropy, CoinType type, @Nullable KeyCrypter keyCrypter, @Nullable KeyParameter key) { this(new NxtFamilyKey(entropy, keyCrypter, key), type); }
@Override public byte[] getPublicKey() { return rootKey.getPublicKey(); }
@Test public void testHDAccountNxt() throws MnemonicException { byte[] privateKey = nxtAccount.rootKey.getPrivateKey(); byte[] publicKey = nxtAccount.getPublicKey(); assertArrayEquals(nxtPrivateKey, privateKey); assertEquals(nxtRsAddress, nxtAccount.getPublicKeyMnemonic()); assertArrayEquals(nxtPublicKey, publicKey); NxtAddress address = (NxtAddress) nxtAccount.getReceiveAddress(); assertEquals(nxtRsAddress, address.toString()); assertEquals(nxtAccountId, address.getAccountId()); }
@Override public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkNotNull(keyCrypter); checkNotNull(aesKey); lock.lock(); try { this.rootKey = this.rootKey.toEncrypted(keyCrypter, aesKey); } finally { lock.unlock(); } }
@Override public KeyCrypter getKeyCrypter() { lock.lock(); try { return rootKey.getKeyCrypter(); } finally { lock.unlock(); } }
@Override public boolean isEncrypted() { lock.lock(); try { return rootKey.isEncrypted(); } finally { lock.unlock(); } }
List<Protos.Key> toProtobuf() { LinkedList<Protos.Key> entries = newLinkedList(); List<Protos.Key.Builder> protos = toEditableProtobuf(); for (Protos.Key.Builder proto : protos) { entries.add(proto.build()); } return entries; }
@Override public NxtFamilyKey toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) { checkState(!entropy.isEncrypted(), "Attempt to encrypt a key that is already encrypted."); return new NxtFamilyKey(entropy.encrypt(keyCrypter, aesKey, null), publicKey); }
public NxtFamilyWallet(NxtFamilyKey key, CoinType type) { this(KeyUtils.getPublicKeyId(type, key.getPublicKey()), key, type); }
@Override public NxtFamilyKey toEncrypted(CharSequence password) { checkNotNull(password, "Attempt to encrypt with a null password."); checkArgument(password.length() > 0, "Attempt to encrypt with an empty password."); checkState(!entropy.isEncrypted(), "Attempt to encrypt a key that is already encrypted."); KeyCrypter scrypt = new KeyCrypterScrypt(); KeyParameter derivedKey = scrypt.deriveKey(password); return toEncrypted(scrypt, derivedKey); }
@Override public boolean checkAESKey(KeyParameter aesKey) { checkNotNull(aesKey, "Cannot check null KeyParameter"); checkNotNull(getKeyCrypter(), "Key not encrypted"); try { return Arrays.equals(publicKey, Crypto.getPublicKey(entropy.decrypt(aesKey).getPrivKeyBytes())); } catch (KeyCrypterException e) { return false; } }
@Test public void serializeUnencryptedNormal() throws Exception { List<Protos.Key> keys = nxtAccount.serializeKeychainToProtobuf(); NxtFamilyKey nxtKey = NxtFamilyKey.fromProtobuf(keys); NxtFamilyWallet newWallet; newWallet = new NxtFamilyWallet(nxtKey, NXT); }
/** * Returns the key chain found in the given list of keys. */ public static NxtFamilyKey fromProtobuf(List<Protos.Key> keys, @Nullable KeyCrypter crypter) throws UnreadableWalletException { if (keys.size() != 2) { throw new UnreadableWalletException("Expected 2 keys, NXT secret and Curve25519 " + "pub/priv pair, instead got: " + keys.size()); } Protos.Key entropyProto = keys.get(0); DeterministicKey entropyKey = KeyUtils.getDeterministicKey(entropyProto, null, crypter); Protos.Key publicKeyProto = keys.get(1); if (publicKeyProto.getType() != Protos.Key.Type.ORIGINAL) { throw new UnreadableWalletException("Unexpected type for NXT public key: " + publicKeyProto.getType()); } byte[] publicKeyBytes = publicKeyProto.getPublicKey().toByteArray(); return new NxtFamilyKey(entropyKey, publicKeyBytes); }
public NxtFamilyWallet(String id, NxtFamilyKey key, CoinType type) { super(type, id); rootKey = key; address = new NxtAddress(type, key.getPublicKey()); balance = type.value(0); addressesStatus = new HashMap<>(); addressesSubscribed = new ArrayList<>(); addressesPendingSubscription = new ArrayList<>(); statusPendingUpdates = new HashMap<>(); //fetchingTransactions = new HashSet<>(); rawtransactions = new HashMap<>(); listeners = new CopyOnWriteArrayList<>(); }