Here are the examples of the java api class com.google.bitcoin.core.ECKey taken from open source projects.
1. FileHandlerTest#testWalletVersion2b()
Project: multibit
File: FileHandlerTest.java
File: FileHandlerTest.java
@Test public void testWalletVersion2b() throws IOException { // Create MultiBit controller. final CreateControllers.Controllers controllers = CreateControllers.createControllers(); controller = controllers.bitcoinController; File temporaryWallet = File.createTempFile(TEST_WALLET_VERSION_2_PREFIX, ".wallet"); temporaryWallet.deleteOnExit(); String newWalletFilename = temporaryWallet.getAbsolutePath(); // Create a new protobuf wallet. Wallet newWallet = new Wallet(NetworkParameters.prodNet()); ECKey newKey = new ECKey(); newWallet.getKeychain().add(newKey); newKey = new ECKey(); newWallet.getKeychain().add(newKey); WalletData perWalletModelData = new WalletData(); // The wallet info incorrectly states it is encrypted but the wallet is not encrypted. WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, newWallet, MultiBitWalletVersion.PROTOBUF_ENCRYPTED); perWalletModelData.setWalletInfo(walletInfo); perWalletModelData.setWallet(newWallet); perWalletModelData.setWalletFilename(newWalletFilename); perWalletModelData.setWalletDescription(TEST_WALLET_VERSION_2_PREFIX); // Save the wallet controller.getFileHandler().savePerWalletModelData(perWalletModelData, true); // Check the version gets round tripped. WalletData perWalletModelDataReborn = fileHandler.loadFromFile(new File(newWalletFilename)); assertNotNull(perWalletModelDataReborn); WalletInfoData rebornWalletInfo = perWalletModelDataReborn.getWalletInfo(); assertEquals("Wallet version was incorrect.", MultiBitWalletVersion.PROTOBUF, rebornWalletInfo.getWalletVersion()); ; }
2. FileHandlerTest#testWalletVersion2a()
Project: multibit
File: FileHandlerTest.java
File: FileHandlerTest.java
@Test public void testWalletVersion2a() throws IOException { // Create MultiBit controller. final CreateControllers.Controllers controllers = CreateControllers.createControllers(); controller = controllers.bitcoinController; File temporaryWallet = File.createTempFile(TEST_WALLET_VERSION_2_PREFIX, ".wallet"); temporaryWallet.deleteOnExit(); String newWalletFilename = temporaryWallet.getAbsolutePath(); // Create a new protobuf wallet. Wallet newWallet = new Wallet(NetworkParameters.prodNet()); ECKey newKey = new ECKey(); newWallet.getKeychain().add(newKey); newKey = new ECKey(); newWallet.getKeychain().add(newKey); WalletData perWalletModelData = new WalletData(); WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, newWallet, MultiBitWalletVersion.PROTOBUF); perWalletModelData.setWalletInfo(walletInfo); perWalletModelData.setWallet(newWallet); perWalletModelData.setWalletFilename(newWalletFilename); perWalletModelData.setWalletDescription(TEST_WALLET_VERSION_2_PREFIX); // Save the wallet controller.getFileHandler().savePerWalletModelData(perWalletModelData, true); // Check the version gets round tripped. WalletData perWalletModelDataReborn = fileHandler.loadFromFile(new File(newWalletFilename)); assertNotNull(perWalletModelDataReborn); WalletInfoData rebornWalletInfo = perWalletModelDataReborn.getWalletInfo(); assertEquals("Wallet version was not roundtripped", MultiBitWalletVersion.PROTOBUF, rebornWalletInfo.getWalletVersion()); ; }
3. FileHandlerTest#testCannotLoadOrSaveFutureWalletVersions()
Project: multibit
File: FileHandlerTest.java
File: FileHandlerTest.java
@Test public void testCannotLoadOrSaveFutureWalletVersions() throws IOException { // Create MultiBit controller. final CreateControllers.Controllers controllers = CreateControllers.createControllers(); controller = controllers.bitcoinController; File temporaryWallet = File.createTempFile(TEST_WALLET_VERSION_PREFIX, ".wallet"); temporaryWallet.deleteOnExit(); String newWalletFilename = temporaryWallet.getAbsolutePath(); // Create a new protobuf wallet with a future wallet version Wallet newWallet = new Wallet(NetworkParameters.prodNet()); ECKey newKey = new ECKey(); newWallet.getKeychain().add(newKey); newKey = new ECKey(); newWallet.getKeychain().add(newKey); WalletData perWalletModelData = new WalletData(); WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, newWallet, MultiBitWalletVersion.FUTURE); perWalletModelData.setWalletInfo(walletInfo); perWalletModelData.setWallet(newWallet); perWalletModelData.setWalletFilename(newWalletFilename); perWalletModelData.setWalletDescription(TEST_CREATE_PROTOBUF_PREFIX); // Should not be able to save a wallet version from the future. try { controller.getFileHandler().savePerWalletModelData(perWalletModelData, true); fail("Could save a wallet version from the future but should not be able to"); } catch (WalletVersionException wve) { } // Check a wallet from the future cannot be loaded. File directory = new File("."); String currentPath = directory.getAbsolutePath(); String futureWalletName = currentPath + File.separator + Constants.TESTDATA_DIRECTORY + File.separator + WALLET_TESTDATA_DIRECTORY + File.separator + WALLET_FUTURE; try { File futureWalletFile = new File(futureWalletName); fileHandler.loadFromFile(futureWalletFile); fail("Could load a wallet version from the future but should not be able to"); } catch (WalletVersionException wve) { } }
4. FileHandlerTest#testCreateProtobufUnencryptedWallet()
Project: multibit
File: FileHandlerTest.java
File: FileHandlerTest.java
@Test public void testCreateProtobufUnencryptedWallet() throws IOException { File temporaryWallet = File.createTempFile(TEST_CREATE_UNENCRYPTED_PROTOBUF_PREFIX, ".wallet"); temporaryWallet.deleteOnExit(); String newWalletFilename = temporaryWallet.getAbsolutePath(); // Create a new unencrypted (vanilla) protobuf wallet. Wallet newWallet = new Wallet(NetworkParameters.prodNet()); ECKey newKey = new ECKey(); newWallet.getKeychain().add(newKey); newKey = new ECKey(); newWallet.getKeychain().add(newKey); WalletData perWalletModelData = new WalletData(); WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, newWallet, MultiBitWalletVersion.PROTOBUF); perWalletModelData.setWalletInfo(walletInfo); perWalletModelData.setWallet(newWallet); perWalletModelData.setWalletFilename(newWalletFilename); perWalletModelData.setWalletDescription(TEST_CREATE_UNENCRYPTED_PROTOBUF_PREFIX); // Check the wallet status before it is written out and reborn. assertEquals(MultiBitWalletVersion.PROTOBUF, perWalletModelData.getWalletInfo().getWalletVersion()); assertTrue("Wallet is not UNENCRYPTED when it should be", perWalletModelData.getWallet().getEncryptionType() == EncryptionType.UNENCRYPTED); // Save the wallet and then read it back in. controller.getFileHandler().savePerWalletModelData(perWalletModelData, true); // Check the wallet and wallet info file exists. File newWalletFile = new File(newWalletFilename); assertTrue(newWalletFile.exists()); String walletInfoFileAsString = WalletInfoData.createWalletInfoFilename(newWalletFilename); File walletInfoFile = new File(walletInfoFileAsString); assertTrue(walletInfoFile.exists()); // Check wallet can be loaded and is still protobuf and unencrypted. // Note - when reborn it is reborn as an EncryptableWallet. WalletData perWalletModelDataReborn = fileHandler.loadFromFile(newWalletFile); assertNotNull(perWalletModelDataReborn); assertEquals(BigInteger.ZERO, perWalletModelDataReborn.getWallet().getBalance()); assertEquals(TEST_CREATE_UNENCRYPTED_PROTOBUF_PREFIX, perWalletModelDataReborn.getWalletDescription()); assertEquals(2, perWalletModelDataReborn.getWallet().getKeychain().size()); assertEquals(MultiBitWalletVersion.PROTOBUF, perWalletModelDataReborn.getWalletInfo().getWalletVersion()); assertTrue("Wallet is not UNENCRYPTED when it should be", perWalletModelDataReborn.getWallet().getEncryptionType() == EncryptionType.UNENCRYPTED); deleteWalletAndCheckDeleted(perWalletModelDataReborn, newWalletFile, walletInfoFile); }
5. BackupManagerTest#testBackupWalletUnencrypted()
Project: multibit
File: BackupManagerTest.java
File: BackupManagerTest.java
@Test public void testBackupWalletUnencrypted() throws IOException { // Create MultiBit controller. final CreateControllers.Controllers controllers = CreateControllers.createControllers(); controller = controllers.bitcoinController; File temporaryWallet = File.createTempFile(TEST_BACKUP_WALLET_UNENCRYPTED, ".wallet"); temporaryWallet.deleteOnExit(); String newWalletFilename = temporaryWallet.getAbsolutePath(); // Create a new protobuf wallet - unencrypted. Wallet newWallet = new Wallet(NetworkParameters.prodNet()); ECKey newKey = new ECKey(); newWallet.getKeychain().add(newKey); newKey = new ECKey(); newWallet.getKeychain().add(newKey); WalletData perWalletModelData = new WalletData(); WalletInfoData walletInfo = new WalletInfoData(newWalletFilename, newWallet, MultiBitWalletVersion.PROTOBUF); perWalletModelData.setWalletInfo(walletInfo); perWalletModelData.setWallet(newWallet); perWalletModelData.setWalletFilename(newWalletFilename); perWalletModelData.setWalletDescription(TEST_BACKUP_WALLET_UNENCRYPTED); // Check there are initially no backup wallets. List<File> backupWallets = BackupManager.INSTANCE.getWalletsInBackupDirectory(newWalletFilename, "wallet-unenc-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-unenc-backup directory backupWallets = BackupManager.INSTANCE.getWalletsInBackupDirectory(newWalletFilename, "wallet-unenc-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)); }
6. BackupManagerTest#testFileCopyAndEncrypt()
Project: multibit
File: BackupManagerTest.java
File: BackupManagerTest.java
@Test public void testFileCopyAndEncrypt() throws IOException { // Create MultiBit controller. final CreateControllers.Controllers controllers = CreateControllers.createControllers(); controller = controllers.bitcoinController; File temporaryWallet = File.createTempFile(TEST_FILE_COPY_AND_ENCRYPT, ".wallet"); temporaryWallet.deleteOnExit(); File temporaryWalletCopy = File.createTempFile(TEST_FILE_COPY_AND_ENCRYPT, ".wallet.cipher"); temporaryWalletCopy.deleteOnExit(); temporaryWalletCopy.delete(); String newWalletFilename = temporaryWallet.getAbsolutePath(); // Create a new protobuf wallet. Wallet newWallet = new Wallet(NetworkParameters.prodNet()); ECKey newKey = new ECKey(); newWallet.getKeychain().add(newKey); newKey = new ECKey(); newWallet.getKeychain().add(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_FILE_COPY_AND_ENCRYPT); // Save the wallet controller.getFileHandler().savePerWalletModelData(perWalletModelData, true); // Copy the wallet and encrypt the whole file. BackupManager.INSTANCE.copyFileAndEncrypt(temporaryWallet, temporaryWalletCopy, WALLET_PASSWORD); // Read the file back and decrypt it. byte[] decryptedWalletBytes = BackupManager.INSTANCE.readFileAndDecrypt(temporaryWalletCopy, WALLET_PASSWORD); // The decrypted bytes should match what is in the temporaryWallet file. byte[] sourceBytes = FileHandler.read(temporaryWallet); assertEquals("Wrong length of file after encrypt save roundtrip", sourceBytes.length, decryptedWalletBytes.length); assertTrue("The wallet after the encrypt save roundtrip has changed", Arrays.areEqual(sourceBytes, decryptedWalletBytes)); }
7. ActionTestUtils#createNewActiveWallet()
Project: multibit
File: ActionTestUtils.java
File: ActionTestUtils.java
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()); }
8. FileHandlerTest#testNonDefaultScryptParameters()
Project: multibit
File: FileHandlerTest.java
File: FileHandlerTest.java
@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); }
9. FileHandlerTest#testDefaultScryptParameters()
Project: multibit
File: FileHandlerTest.java
File: FileHandlerTest.java
@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); }
10. ReceiveMoney#onCreate()
Project: bitcoin-android
File: ReceiveMoney.java
File: ReceiveMoney.java
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.receive_money); appState = (ApplicationState) getApplication(); // prevent keyboard from opening getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); dimension = getScreenWidth() * 7 / 8; ECKey key = appState.wallet.keychain.get(0); address = key.toAddress(appState.params); TextView addressField = (TextView) findViewById(R.id.address); addressField.setText(address.toString()); generateQRCode(generateBitCoinURI()); amountField = (EditText) findViewById(R.id.receive_amount); amountField.setWidth(dimension); amountLabel = (TextView) findViewById(R.id.receive_amount_label); amountLabel.setWidth(dimension); amountField.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { if (amount != s.toString()) { amount = s.toString(); generateQRCode(generateBitCoinURI()); } } public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // do nothing } public void onTextChanged(CharSequence s, int start, int before, int count) { // do nothing } }); Button emailButton = (Button) this.findViewById(R.id.email_button); emailButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("text/plain"); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.pay_request_received_from) + address); StringBuilder message = new StringBuilder(getString(R.string.pay_request)); message.append("\n\n").append(getString(R.string.pay_request_address_label)).append(address); if (!TextUtils.isEmpty(amount)) { message.append("\n").append(R.string.pay_request_amount_label).append(amount).append(" BTC"); } message.append("\n\n").append(getString(R.string.pay_request_thank_you)); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message.toString()); ReceiveMoney.this.startActivity(Intent.createChooser(emailIntent, "Send Request Via...")); } }); Button copyButton = (Button) this.findViewById(R.id.copy_button); copyButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(address.toString()); Toast.makeText(ReceiveMoney.this, "Copied address to clipboard!", Toast.LENGTH_SHORT).show(); } }); }