/** * @return The contact name if present, otherwise the Bitcoin address */ public String getSummary() { if (contact.isPresent()) { return contact.get().getName(); } // The Base58 representation of this address return bitcoinAddress.toString(); } }
@Override public void run() { Coin balance = wallet.getBalance(Wallet.BalanceType.ESTIMATED); mAddressText.setText(address.toString()); String balanceString = String.valueOf(balance.value / 100000) + " mBTC"; mBalanceText.setText(balanceString); BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap); bitmapDrawable.setFilterBitmap(false); mQrImageView.setImageDrawable(bitmapDrawable); } });
/** * Simple Bitcoin URI builder using known good fields. * * @param address The Bitcoin address * @param amount The amount * @param label A label * @param message A message * @return A String containing the Bitcoin URI */ public static String convertToBitcoinURI(Address address, Coin amount, String label, String message) { return convertToBitcoinURI(address.getParameters(), address.toString(), amount, label, message); }
/** * Simple Bitcoin URI builder using known good fields. * * @param address The Bitcoin address * @param amount The amount * @param label A label * @param message A message * @return A String containing the Bitcoin URI */ public static String convertToBitcoinURI(Address address, Coin amount, String label, String message) { return convertToBitcoinURI(address.getParameters(), address.toString(), amount, label, message); }
private void selectNameOrBitcoinAddress(Optional<Recipient> recipient) { if (recipient.get().getContact().isPresent()) { editor.setText(recipient.get().getContact().get().getName()); } else { editor.setText(recipient.get().getBitcoinAddress().toString()); } }
public ECKey decrypt(String passphrase) throws BadPassphraseException { String normalizedPassphrase = Normalizer.normalize(passphrase, Normalizer.Form.NFC); ECKey key = ecMultiply ? decryptEC(normalizedPassphrase) : decryptNoEC(normalizedPassphrase); Sha256Hash hash = Sha256Hash.twiceOf(key.toAddress(params).toString().getBytes(Charsets.US_ASCII)); byte[] actualAddressHash = Arrays.copyOfRange(hash.getBytes(), 0, 4); if (!Arrays.equals(actualAddressHash, addressHash)) throw new BadPassphraseException(); return key; }
public ECKey decrypt(String passphrase) throws BadPassphraseException { String normalizedPassphrase = Normalizer.normalize(passphrase, Normalizer.Form.NFC); ECKey key = ecMultiply ? decryptEC(normalizedPassphrase) : decryptNoEC(normalizedPassphrase); Sha256Hash hash = Sha256Hash.twiceOf(key.toAddress(params).toString().getBytes(Charsets.US_ASCII)); byte[] actualAddressHash = Arrays.copyOfRange(hash.getBytes(), 0, 4); if (!Arrays.equals(actualAddressHash, addressHash)) throw new BadPassphraseException(); return key; }
public ECKey decrypt(String passphrase) throws BadPassphraseException { String normalizedPassphrase = Normalizer.normalize(passphrase, Normalizer.Form.NFC); ECKey key = ecMultiply ? decryptEC(normalizedPassphrase) : decryptNoEC(normalizedPassphrase); Sha256Hash hash = Sha256Hash.twiceOf(key.toAddress(params).toString().getBytes(Charsets.US_ASCII)); byte[] actualAddressHash = Arrays.copyOfRange(hash.getBytes(), 0, 4); if (!Arrays.equals(actualAddressHash, addressHash)) throw new BadPassphraseException(); return key; }
@Test public void comparisonBytesVsString() throws Exception { // TODO: To properly test this we need a much larger data set Address a = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX"); Address b = Address.fromBase58(mainParams, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P"); int resultBytes = a.compareTo(b); int resultsString = a.toString().compareTo(b.toString()); assertTrue( resultBytes < 0 ); assertTrue( resultsString < 0 ); } }
@Test public void stringification() throws Exception { // Test a testnet address. Address a = new Address(testParams, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString()); assertFalse(a.isP2SHAddress()); Address b = new Address(mainParams, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a")); assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString()); assertFalse(b.isP2SHAddress()); }
@Test public void testSend() throws Exception { WalletAppKit nomKit = bitcoinCryptoNoP.getKit(); Wallet wallet = nomKit.wallet(); System.out.println("Current Receive Address: " + wallet.currentReceiveAddress().toString() + "\nIssued Receive Addresses: \n" + wallet.getIssuedReceiveAddresses().toString() + "\nMnemonic: " + wallet.getActiveKeychain().getMnemonicCode().toString() + "\nWallets Balance: " + wallet.getBalance().toPlainString() + " BTC"); // Get a ready to send TX in its Raw HEX format System.out.println("Raw TX HEX: " + bitcoinCryptoNoP.sendOffline("n2ooxjPCQ19f56ivrCBq93DM6a71TA89bc", 10000)); // Create and send transaciton using the wallets broadcast org.bitcoinj.core.Transaction sentTransaction = bitcoinCryptoNoP.send("n2ooxjPCQ19f56ivrCBq93DM6a71TA89bc", 10000); System.out.println("Transaction sent. Find txid: " + sentTransaction.getHashAsString()); }
@Test public void testScriptSig() throws Exception { byte[] sigProgBytes = HEX.decode(sigProg); Script script = new Script(sigProgBytes); // Test we can extract the from address. byte[] hash160 = Utils.sha256hash160(script.getPubKey()); Address a = new Address(PARAMS, hash160); assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", a.toString()); }
@Test public void testScriptPubKey() throws Exception { // Check we can extract the to address byte[] pubkeyBytes = HEX.decode(pubkeyProg); Script pubkey = new Script(pubkeyBytes); assertEquals("DUP HASH160 PUSHDATA(20)[33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString()); Address toAddr = new Address(PARAMS, pubkey.getPubKeyHash()); assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr.toString()); }
@Test public void testMini() throws Exception { SerializedKey serializedKey = new SerializedKey(BTC_MINI_PRIV); assertFalse(serializedKey.isEncrypted()); SerializedKey.TypedKey key = serializedKey.getKey(); assertTrue(key.possibleType.contains(BTC)); assertEquals(BTC_MINI_ADDR, key.key.toAddress(BTC).toString()); } }
@Test public void testWif() throws Exception { SerializedKey serializedKey = new SerializedKey(BTC_WIF_PRIV); assertFalse(serializedKey.isEncrypted()); SerializedKey.TypedKey key = serializedKey.getKey(); assertTrue(key.possibleType.contains(BTC)); assertEquals(BTC_WIF_ADDR, key.key.toAddress(BTC).toString()); }
@Test public void testMini() throws Exception { SerializedKey serializedKey = new SerializedKey(BTC_MINI_PRIV); assertFalse(serializedKey.isEncrypted()); SerializedKey.TypedKey key = serializedKey.getKey(); assertTrue(key.possibleType.contains(BTC)); assertEquals(BTC_MINI_ADDR, key.key.toAddress(BTC).toString()); } }
@Test public void testWif() throws Exception { SerializedKey serializedKey = new SerializedKey(BTC_WIF_PRIV); assertFalse(serializedKey.isEncrypted()); SerializedKey.TypedKey key = serializedKey.getKey(); assertTrue(key.possibleType.contains(BTC)); assertEquals(BTC_WIF_ADDR, key.key.toAddress(BTC).toString()); }
@Test public void brokenURIs() throws BitcoinURIParseException { // Check we can parse the incorrectly formatted URIs produced by blockchain.info and its iPhone app. String str = "bitcoin://1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH?amount=0.01000000"; BitcoinURI uri = new BitcoinURI(str); assertEquals("1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH", uri.getAddress().toString()); assertEquals(CENT, uri.getAmount()); }
@Test public void base58Encoding() throws Exception { String addr = "mqAJmaxMcG5pPHHc3H3NtyXzY7kGbJLuMF"; String privkey = "92shANodC6Y4evT5kFzjNFQAdjqTtHAnDTLzqBBq4BbKUPyx6CD"; ECKey key = DumpedPrivateKey.fromBase58(TestNet3Params.get(), privkey).getKey(); assertEquals(privkey, key.getPrivateKeyEncoded(TestNet3Params.get()).toString()); assertEquals(addr, key.toAddress(TestNet3Params.get()).toString()); }
@Test public void testP2SHOutputScript() throws Exception { String P2SHAddressString = "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU"; Address P2SHAddress = Address.fromBase58(MainNetParams.get(), P2SHAddressString); Script script = ScriptBuilder.createOutputScript(P2SHAddress); Transaction tx = new Transaction(MainNetParams.get()); tx.addOutput(Coin.COIN, script); assertEquals(P2SHAddressString, tx.getOutput(0).getAddressFromP2SH(MainNetParams.get()).toString()); }