com.google.bitcoin.crypto.KeyCrypter

Here are the examples of the java api class com.google.bitcoin.crypto.KeyCrypter taken from open source projects.

1. FileHandlerTest#testDefaultScryptParameters()

Project: multibit
Source File: FileHandlerTest.java
View license
@Test
public void testDefaultScryptParameters() throws Exception {
    // Create an encrypted wallet with default scrypt parameters.
    File temporaryWallet = File.createTempFile(TEST_SCRYPT_PARAMETERS + "1", ".wallet");
    temporaryWallet.deleteOnExit();
    String newWalletFilename = temporaryWallet.getAbsolutePath();
    KeyCrypter testKeyCrypter = new KeyCrypterScrypt();
    Wallet newWallet = new Wallet(NetworkParameters.prodNet(), testKeyCrypter);
    ECKey newKey = new ECKey();
    newKey = newKey.encrypt(newWallet.getKeyCrypter(), newWallet.getKeyCrypter().deriveKey(WALLET_PASSWORD));
    newWallet.addKey(newKey);
    WalletData perWalletModelData = new WalletData();
    WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, newWallet, MultiBitWalletVersion.PROTOBUF_ENCRYPTED);
    perWalletModelData.setWalletInfo(walletInfo);
    perWalletModelData.setWallet(newWallet);
    perWalletModelData.setWalletFilename(newWalletFilename);
    perWalletModelData.setWalletDescription(TEST_SCRYPT_PARAMETERS);
    // Save the wallet and read it back in again.
    controller.getFileHandler().savePerWalletModelData(perWalletModelData, true);
    // Check the wallet and wallet info file exists.
    File newWalletFile = new File(newWalletFilename);
    String walletInfoFileAsString = WalletInfoData.createWalletInfoFilename(newWalletFilename);
    File walletInfoFile = new File(walletInfoFileAsString);
    // Load the wallet and check the default scrypt parameters
    WalletData perWalletModelDataReborn = fileHandler.loadFromFile(newWalletFile);
    assertNotNull(perWalletModelDataReborn);
    KeyCrypter rebornEncrypterDecrypter = perWalletModelDataReborn.getWallet().getKeyCrypter();
    assertNotNull("There was no encrypterDecrypter after round trip", rebornEncrypterDecrypter);
    assertTrue("EncrypterDecrypter was not an EncrypterDecrypterScrypt", rebornEncrypterDecrypter instanceof KeyCrypterScrypt);
    KeyCrypterScrypt rebornEncrypterDecrypterScrypt = (KeyCrypterScrypt) rebornEncrypterDecrypter;
    assertEquals("Wrong N parameter", 16384, rebornEncrypterDecrypterScrypt.getScryptParameters().getN());
    assertEquals("Wrong R parameter", 8, rebornEncrypterDecrypterScrypt.getScryptParameters().getR());
    assertEquals("Wrong P parameter", 1, rebornEncrypterDecrypterScrypt.getScryptParameters().getP());
    deleteWalletAndCheckDeleted(perWalletModelDataReborn, newWalletFile, walletInfoFile);
}

2. FileHandlerTest#testNonDefaultScryptParameters()

Project: multibit
Source File: FileHandlerTest.java
View license
@Test
public void testNonDefaultScryptParameters() throws Exception {
    // Non default scrypt parameters.
    int n = 32768;
    int r = 8;
    int p = 3;
    byte[] salt = new byte[KeyCrypterScrypt.SALT_LENGTH];
    secureRandom.nextBytes(salt);
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder().setSalt(ByteString.copyFrom(salt)).setN(n).setR(r).setP(p);
    ScryptParameters scryptParameters = scryptParametersBuilder.build();
    KeyCrypter testKeyCrypter = new KeyCrypterScrypt(scryptParameters);
    // Create an encrypted wallet with nondefault scrypt parameters.
    File temporaryWallet = File.createTempFile(TEST_SCRYPT_PARAMETERS + "2", ".wallet");
    temporaryWallet.deleteOnExit();
    String newWalletFilename = temporaryWallet.getAbsolutePath();
    Wallet newWallet = new Wallet(NetworkParameters.prodNet(), testKeyCrypter);
    ECKey newKey = new ECKey();
    newKey = newKey.encrypt(newWallet.getKeyCrypter(), newWallet.getKeyCrypter().deriveKey(WALLET_PASSWORD));
    newWallet.addKey(newKey);
    WalletData perWalletModelData = new WalletData();
    WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, newWallet, MultiBitWalletVersion.PROTOBUF_ENCRYPTED);
    perWalletModelData.setWalletInfo(walletInfo);
    perWalletModelData.setWallet(newWallet);
    perWalletModelData.setWalletFilename(newWalletFilename);
    perWalletModelData.setWalletDescription(TEST_SCRYPT_PARAMETERS + "2");
    // Save the wallet and read it back in again.
    controller.getFileHandler().savePerWalletModelData(perWalletModelData, true);
    // Check the wallet and wallet info file exists.
    File newWalletFile = new File(newWalletFilename);
    String walletInfoFileAsString = WalletInfoData.createWalletInfoFilename(newWalletFilename);
    File walletInfoFile = new File(walletInfoFileAsString);
    // Load the wallet and check the default scrypt parameters
    WalletData perWalletModelDataReborn = fileHandler.loadFromFile(newWalletFile);
    assertNotNull(perWalletModelDataReborn);
    KeyCrypter rebornKeyCrypter = perWalletModelDataReborn.getWallet().getKeyCrypter();
    assertNotNull("There was no keyCrypter after round trip", rebornKeyCrypter);
    assertTrue("EncrypterDecrypter was not an KeyCrypterScrypt", rebornKeyCrypter instanceof KeyCrypterScrypt);
    KeyCrypterScrypt rebornKeyCrypterScrypt = (KeyCrypterScrypt) rebornKeyCrypter;
    assertEquals("Wrong N parameter", n, rebornKeyCrypterScrypt.getScryptParameters().getN());
    assertEquals("Wrong R parameter", r, rebornKeyCrypterScrypt.getScryptParameters().getR());
    assertEquals("Wrong P parameter", p, rebornKeyCrypterScrypt.getScryptParameters().getP());
    deleteWalletAndCheckDeleted(perWalletModelDataReborn, newWalletFile, walletInfoFile);
}

3. WalletTest#encryptionDecryptionHomogenousKeys()

Project: multibit
Source File: WalletTest.java
View license
@Test
public void encryptionDecryptionHomogenousKeys() throws Exception {
    // Check the wallet is currently encrypted
    assertTrue("Wallet is not an encrypted wallet", encryptedWallet.getEncryptionType() == EncryptionType.ENCRYPTED_SCRYPT_AES);
    // Try added an ECKey that was encrypted with a differenct ScryptParameters (i.e. a non-homogenous key).
    // This is not allowed as the ScryptParameters is stored at the Wallet level.
    byte[] salt = new byte[KeyCrypterScrypt.SALT_LENGTH];
    secureRandom.nextBytes(salt);
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder().setSalt(ByteString.copyFrom(salt));
    ScryptParameters scryptParameters = scryptParametersBuilder.build();
    KeyCrypter keyCrypterDifferent = new KeyCrypterScrypt(scryptParameters);
    ECKey ecKeyDifferent = new ECKey();
    ecKeyDifferent = ecKeyDifferent.encrypt(keyCrypterDifferent, aesKey);
    Iterable<ECKey> keys = encryptedWallet.getKeys();
    Iterator iterator = keys.iterator();
    boolean oneKey = iterator.hasNext();
    iterator.next();
    assertTrue("Wrong number of keys in wallet before key addition", oneKey && !iterator.hasNext());
    try {
        encryptedWallet.addKey(ecKeyDifferent);
        fail("AddKey should have thrown an EncrypterDecrypterException but did not.");
    } catch (KeyCrypterException ede) {
    }
    keys = encryptedWallet.getKeys();
    iterator = keys.iterator();
    oneKey = iterator.hasNext();
    iterator.next();
    assertTrue("Wrong number of keys in wallet after key addition", oneKey && !iterator.hasNext());
}

4. BackupManagerTest#testBackupWalletEncrypted()

Project: multibit
Source File: BackupManagerTest.java
View license
@Test
public void testBackupWalletEncrypted() throws IOException {
    // Create MultiBit controller.
    final CreateControllers.Controllers controllers = CreateControllers.createControllers();
    controller = controllers.bitcoinController;
    File temporaryWallet = File.createTempFile(TEST_BACKUP_WALLET_ENCRYPTED, ".wallet");
    temporaryWallet.deleteOnExit();
    String newWalletFilename = temporaryWallet.getAbsolutePath();
    // Create a new protobuf wallet - encrypted.
    byte[] salt = new byte[KeyCrypterScrypt.SALT_LENGTH];
    SecureRandom secureRandom = new SecureRandom();
    secureRandom.nextBytes(salt);
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder().setSalt(ByteString.copyFrom(salt));
    ScryptParameters scryptParameters = scryptParametersBuilder.build();
    KeyCrypter keyCrypter = new KeyCrypterScrypt(scryptParameters);
    Wallet encryptedWallet = new Wallet(NetworkParameters.prodNet(), keyCrypter);
    WalletData perWalletModelData = new WalletData();
    WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, encryptedWallet, MultiBitWalletVersion.PROTOBUF);
    perWalletModelData.setWalletInfo(walletInfo);
    perWalletModelData.setWallet(encryptedWallet);
    perWalletModelData.setWalletFilename(newWalletFilename);
    perWalletModelData.setWalletDescription(TEST_BACKUP_WALLET_ENCRYPTED);
    // Check there are initially no backup wallets.
    List<File> backupWallets = BackupManager.INSTANCE.getWalletsInBackupDirectory(newWalletFilename, "wallet-backup");
    assertNotNull("Null backupWallets list returned", backupWallets);
    assertEquals("Wring number of backup wallets", 0, backupWallets.size());
    // Save the wallet.
    controller.getFileHandler().savePerWalletModelData(perWalletModelData, true);
    // Backup the wallet.
    BackupManager.INSTANCE.backupPerWalletModelData(controller.getFileHandler(), perWalletModelData);
    // Check that a backup copy has been saved in the data/wallet-backup directory
    backupWallets = BackupManager.INSTANCE.getWalletsInBackupDirectory(newWalletFilename, "wallet-backup");
    assertNotNull("Null backupWallets list returned", backupWallets);
    assertEquals("Wring number of backup wallets", 1, backupWallets.size());
    // Read the originally saved wallet back in.
    byte[] originalBytes = FileHandler.read(temporaryWallet);
    // Read the backup wallet back in.
    byte[] backupBytes = FileHandler.read(backupWallets.get(0));
    assertNotNull("The originally saved wallet was not read back in ok.1", originalBytes);
    assertTrue("The originally saved wallet was not read back in ok.2", originalBytes.length > 0);
    assertEquals("Wrong length of file after backup", originalBytes.length, backupBytes.length);
    assertTrue("The wallet after the backup has changed", Arrays.areEqual(originalBytes, backupBytes));
}

5. ActionTestUtils#createNewActiveWallet()

Project: multibit
Source File: ActionTestUtils.java
View license
public static void createNewActiveWallet(BitcoinController controller, String descriptor, boolean encrypt, CharSequence walletPassword) throws Exception {
    if (secureRandom == null) {
        secureRandom = new SecureRandom();
    }
    byte[] salt = new byte[KeyCrypterScrypt.SALT_LENGTH];
    secureRandom.nextBytes(salt);
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder().setSalt(ByteString.copyFrom(salt));
    ScryptParameters scryptParameters = scryptParametersBuilder.build();
    KeyCrypter keyCrypter = new KeyCrypterScrypt(scryptParameters);
    Wallet wallet;
    ECKey ecKey;
    if (encrypt) {
        wallet = new Wallet(NetworkParameters.prodNet(), keyCrypter);
        ecKey = (new ECKey()).encrypt(keyCrypter, keyCrypter.deriveKey(walletPassword));
        wallet.addKey(ecKey);
    } else {
        wallet = new Wallet(NetworkParameters.prodNet());
        ecKey = new ECKey();
        wallet.addKey(ecKey);
    }
    WalletData perWalletModelData = new WalletData();
    perWalletModelData.setWallet(wallet);
    // Save the wallet to a temporary directory.
    File multiBitDirectory = FileHandler.createTempDirectory("CreateAndDeleteWalletsTest");
    String multiBitDirectoryPath = multiBitDirectory.getAbsolutePath();
    String walletFile = multiBitDirectoryPath + File.separator + descriptor + ".wallet";
    // Put the wallet in the model as the active wallet.
    WalletInfoData walletInfoData = new WalletInfoData(walletFile, wallet, MultiBitWalletVersion.PROTOBUF_ENCRYPTED);
    walletInfoData.addReceivingAddress(new WalletAddressBookData(LABEL_OF_ADDRESS_ADDED, ecKey.toAddress(NetworkParameters.prodNet()).toString()), false);
    perWalletModelData.setWalletInfo(walletInfoData);
    perWalletModelData.setWalletFilename(walletFile);
    perWalletModelData.setWalletDescription(descriptor);
    // Save the wallet and load it up again, making it the active wallet.
    // This also sets the timestamp fields used in file change detection.
    FileHandler fileHandler = new FileHandler(controller);
    fileHandler.savePerWalletModelData(perWalletModelData, true);
    WalletData loadedPerWalletModelData = fileHandler.loadFromFile(new File(walletFile));
    controller.getModel().setActiveWalletByFilename(loadedPerWalletModelData.getWalletFilename());
}