com.google.bitcoin.crypto.KeyCrypterScrypt

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

1. WalletTest#setUp()

Project: multibit
Source File: WalletTest.java
View license
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    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 = new KeyCrypterScrypt(scryptParameters);
    encryptedWallet = new Wallet(params, keyCrypter);
    aesKey = keyCrypter.deriveKey(PASSWORD1);
    wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD);
    ECKey myEncryptedKey = encryptedWallet.addNewEncryptedKey(keyCrypter, aesKey);
    myEncryptedAddress = myEncryptedKey.toAddress(params);
}

2. 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);
}

3. 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);
}