Java Code Examples for org.bitcoinj.core.Address#fromBase58()

The following examples show how to use org.bitcoinj.core.Address#fromBase58() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BtcAddressValidator.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
private ValidationResult validateBtcAddress(String input) {
    try {
        Address.fromBase58(BisqEnvironment.getParameters(), input);
        return new ValidationResult(true);
    } catch (AddressFormatException e) {
        return new ValidationResult(false, Res.get("validation.btc.invalidFormat"));
    }
}
 
Example 2
Source File: BtcAddressValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private ValidationResult validateBtcAddress(String input) {
    try {
        Address.fromBase58(Config.baseCurrencyNetworkParameters(), input);
        return new ValidationResult(true);
    } catch (AddressFormatException e) {
        return new ValidationResult(false, Res.get("validation.btc.invalidFormat"));
    }
}
 
Example 3
Source File: Base58BitcoinAddressValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public AddressValidationResult validate(String address) {
    try {
        Address.fromBase58(networkParameters, address);
    } catch (AddressFormatException ex) {
        return AddressValidationResult.invalidAddress(ex);
    }

    return AddressValidationResult.validAddress();
}
 
Example 4
Source File: BitcoinAddressValidator.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private boolean testAddr(String text) {
    try {
        Address.fromBase58(params, text);
        return true;
    } catch (AddressFormatException e) {
        return false;
    }
}
 
Example 5
Source File: BsqFormatter.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public Address getAddressFromBsqAddress(String encoded) {
    if (useBsqAddressFormat)
        encoded = encoded.substring(prefix.length(), encoded.length());

    try {
        return Address.fromBase58(Config.baseCurrencyNetworkParameters(), encoded);
    } catch (AddressFormatException e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: SPV.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public ListenableFuture<Coin>
validateTx(final PreparedTransaction ptx, final String recipientStr, final Coin amount) {
    Address recipient = null;
    try {
        recipient = Address.fromBase58(mService.getNetworkParameters(), recipientStr);
    } catch (final AddressFormatException e) {
    }

    // 1. Find the change output:
    ListenableFuture<List<Boolean>> changeFn = Futures.immediateFuture(null);

    if (ptx.mDecoded.getOutputs().size() == 2) {
        changeFn = Futures.allAsList(Lists.newArrayList(verifyOutputSpendable(ptx, 0),
                                                        verifyOutputSpendable(ptx, 1)));
    }
    else if (ptx.mDecoded.getOutputs().size() > 2)
        throw new IllegalArgumentException("Verification: Wrong number of transaction outputs.");

    // 2. Verify the main output value and address, if available:
    final Address recipientAddr = recipient;
    return Futures.transform(changeFn, new Function<List<Boolean>, Coin>() {
        @Override
        public Coin apply(final List<Boolean> input) {
            return Verifier.verify(mService, mCountedUtxoValues, ptx, recipientAddr, amount, input);
        }
    });
}
 
Example 7
Source File: GaService.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isValidAddress(final String address, final Network network) {
    try {
        if (network.isElements())
            ConfidentialAddress.fromBase58(network.getNetworkParameters(), address);
        else
            Address.fromBase58(network.getNetworkParameters(), address);
        return true;
    } catch (final AddressFormatException e) {
        if (network.isElements())
            return false; // No bech32 for elements yet
        return decodeBech32Address(address, network.getNetworkParameters()) != null;
    }
}
 
Example 8
Source File: BlockChain.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets the amount of voting tokens associated with the given public key.
 * @param pubKey - The Public Key read from the ID of the voter
 * @param mcAsset - The asset (election) that is chosen at app start-up.
 * @return - The amount of voting tokens available
 */
public int getVotingPassAmount(PublicKey pubKey, Asset mcAsset) {
    if(pubKey != null && mcAsset != null) {
        Address mcAddress = Address.fromBase58(params, MultiChainAddressGenerator.getPublicAddress(version, Long.toString(addressChecksum), pubKey));
        return (int) kit.wallet().getAssetBalance(mcAsset, mcAddress).getBalance();
    } else {
        return 0;
    }
}
 
Example 9
Source File: BitcoinAddressValidator.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private boolean testAddr(String text) {
    try {
        Address.fromBase58(params, text);
        return true;
    } catch (AddressFormatException e) {
        return false;
    }
}
 
Example 10
Source File: BitcoinTransaction.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
private void collectPrvKeysAndAddress(String segWit, String password, Wallet wallet) {
  this.network = wallet.getMetadata().isMainNet() ? MainNetParams.get() : TestNet3Params.get();
  if (wallet.getMetadata().getSource().equals(Metadata.FROM_WIF)) {
    changeAddress = Address.fromBase58(network, wallet.getAddress());
    BigInteger prvKey = DumpedPrivateKey.fromBase58(network, wallet.exportPrivateKey(password)).getKey().getPrivKey();
    prvKeys = Collections.singletonList(prvKey);
  } else {
    prvKeys = new ArrayList<>(getOutputs().size());
    String xprv = new String(wallet.decryptMainKey(password), Charset.forName("UTF-8"));
    DeterministicKey xprvKey = DeterministicKey.deserializeB58(xprv, network);
    DeterministicKey changeKey = HDKeyDerivation.deriveChildKey(xprvKey, ChildNumber.ONE);
    DeterministicKey indexKey = HDKeyDerivation.deriveChildKey(changeKey, new ChildNumber(getChangeIdx(), false));
    if (Metadata.P2WPKH.equals(segWit)) {
      changeAddress = new SegWitBitcoinAddressCreator(network).fromPrivateKey(indexKey);
    } else {
      changeAddress = indexKey.toAddress(network);
    }

    for (UTXO output : getOutputs()) {
      String derivedPath = output.getDerivedPath().trim();
      String[] pathIdxs = derivedPath.replace('/', ' ').split(" ");
      int accountIdx = Integer.parseInt(pathIdxs[0]);
      int changeIdx = Integer.parseInt(pathIdxs[1]);

      DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(xprvKey, new ChildNumber(accountIdx, false));
      DeterministicKey externalChangeKey = HDKeyDerivation.deriveChildKey(accountKey, new ChildNumber(changeIdx, false));
      prvKeys.add(externalChangeKey.getPrivKey());
    }
  }
}
 
Example 11
Source File: Instacash.java    From bisq-assets with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public AddressValidationResult validate(String address) {
    if (!address.matches("^[A][a-km-zA-HJ-NP-Z1-9]{25,34}$"))
        return AddressValidationResult.invalidStructure();

    try {
        Address.fromBase58(new InstacashParams(), address);
    } catch (AddressFormatException ex) {
        return AddressValidationResult.invalidAddress(ex);
    }

    return AddressValidationResult.validAddress();
}
 
Example 12
Source File: Base58BitcoinAddressValidator.java    From bisq-assets with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public AddressValidationResult validate(String address) {
    try {
        Address.fromBase58(networkParameters, address);
    } catch (AddressFormatException ex) {
        return AddressValidationResult.invalidAddress(ex);
    }

    return AddressValidationResult.validAddress();
}
 
Example 13
Source File: BsqFormatter.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public Address getAddressFromBsqAddress(String encoded) {
    if (useBsqAddressFormat)
        encoded = encoded.substring(prefix.length(), encoded.length());

    try {
        return Address.fromBase58(BisqEnvironment.getParameters(), encoded);
    } catch (AddressFormatException e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: BitcoinURITest.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testConvertToBitcoinURI() throws Exception {
    Address goodAddress = Address.fromBase58(MAINNET, MAINNET_GOOD_ADDRESS);
    
    // simple example
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", "AMessage"));
    
    // example with spaces, ampersand and plus
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello World", "Mess & age + hope"));

    // no amount, label present, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", "glory"));
    
    // amount present, no label, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), null, "glory"));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), "", "glory"));

    // amount present, label present, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", ""));
          
    // amount present, no label, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), null, null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), "", ""));
    
    // no amount, label present, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", null));
    
    // no amount, no label, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, null, "Agatha"));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, "", "Agatha"));
  
    // no amount, no label, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, null, null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, "", ""));

    // different scheme
    final NetworkParameters alternativeParameters = new MainNetParams() {
        @Override
        public String getUriScheme() {
            return "test";
        }
    };

    assertEquals("test:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage",
         BitcoinURI.convertToBitcoinURI(Address.fromBase58(alternativeParameters, MAINNET_GOOD_ADDRESS), parseCoin("12.34"), "Hello", "AMessage"));
}
 
Example 15
Source File: BitcoinURITest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testConvertToBitcoinURI() throws Exception {
    Address goodAddress = Address.fromBase58(MAINNET, MAINNET_GOOD_ADDRESS);
    
    // simple example
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", "AMessage"));
    
    // example with spaces, ampersand and plus
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello World", "Mess & age + hope"));

    // no amount, label present, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", "glory"));
    
    // amount present, no label, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), null, "glory"));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), "", "glory"));

    // amount present, label present, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", ""));
          
    // amount present, no label, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), null, null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), "", ""));
    
    // no amount, label present, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", null));
    
    // no amount, no label, message present
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, null, "Agatha"));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?message=Agatha", BitcoinURI.convertToBitcoinURI(goodAddress, null, "", "Agatha"));
  
    // no amount, no label, no message
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, null, null));
    assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS, BitcoinURI.convertToBitcoinURI(goodAddress, null, "", ""));

    // different scheme
    final NetworkParameters alternativeParameters = new MainNetParams() {
        @Override
        public String getUriScheme() {
            return "test";
        }
    };

    assertEquals("test:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage",
         BitcoinURI.convertToBitcoinURI(Address.fromBase58(alternativeParameters, MAINNET_GOOD_ADDRESS), parseCoin("12.34"), "Hello", "AMessage"));
}
 
Example 16
Source File: ReimbursementProposal.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public Address getAddress() throws AddressFormatException {
    // Remove leading 'B'
    String underlyingBtcAddress = bsqAddress.substring(1, bsqAddress.length());
    return Address.fromBase58(Config.baseCurrencyNetworkParameters(), underlyingBtcAddress);
}
 
Example 17
Source File: CompensationProposal.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public Address getAddress() throws AddressFormatException {
    // Remove leading 'B'
    String underlyingBtcAddress = bsqAddress.substring(1, bsqAddress.length());
    return Address.fromBase58(Config.baseCurrencyNetworkParameters(), underlyingBtcAddress);
}
 
Example 18
Source File: CompensationProposal.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public Address getAddress() throws AddressFormatException {
    // Remove leading 'B'
    String underlyingBtcAddress = bsqAddress.substring(1, bsqAddress.length());
    return Address.fromBase58(BisqEnvironment.getParameters(), underlyingBtcAddress);
}
 
Example 19
Source File: BlockChain.java    From polling-station-app with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Get the balance of a public key based on the information on the blockchain.
 * @param pubKey
 * @return
 */
public AssetBalance getVotingPassBalance(PublicKey pubKey, Asset asset) {
    Address address = Address.fromBase58(params, MultiChainAddressGenerator.getPublicAddress(version, Long.toString(addressChecksum), pubKey));
    return kit.wallet().getAssetBalance(asset, address);
}
 
Example 20
Source File: BlockChain.java    From polling-station-app with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Returns the address corresponding to the pubkey.
 * @param pubKey
 * @return Address
 */
public Address getAddress(PublicKey pubKey) {
    return Address.fromBase58(params, MultiChainAddressGenerator.getPublicAddress(version, Long.toString(addressChecksum), pubKey));
}