Java Code Examples for org.bitcoinj.core.Base58#decode()

The following examples show how to use org.bitcoinj.core.Base58#decode() . 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: types.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public public_key_type(String strBase58) throws NoSuchAlgorithmException {
    String strPrefix = GRAPHENE_ADDRESS_PREFIX;
    byte[] byteKeyData = Base58.decode(strBase58.substring(strPrefix.length()));
    binary_key binaryKey = new binary_key(byteKeyData);

    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(binaryKey.data, 0, binaryKey.data.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);

    byte[] byteOut = new byte[4];
    System.arraycopy(out, 0, byteOut, 0, byteOut.length);
    int nByteOut = ByteBuffer.wrap(byteOut).getInt();

    if (nByteOut != binaryKey.check) {
        throw new RuntimeException("Public key is not valid");
    }
    key_data = binaryKey.data;
}
 
Example 2
Source File: EOSWalletTest.java    From token-core-android with Apache License 2.0 6 votes vote down vote up
@Test
public void generatePrvPubKey() {

  byte[] prvWIF = Base58.decode(WIF);
  // have omitted the checksum verification
  prvWIF = Arrays.copyOfRange(prvWIF, 1, prvWIF.length - 4);

  // use the privateKey to calculate the compressed public key directly
  ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, prvWIF));
  byte[] pubKeyData = ecKey.getPubKey();
  RIPEMD160Digest digest = new RIPEMD160Digest();
  digest.update(pubKeyData, 0, pubKeyData.length);
  byte[] out = new byte[20];
  digest.doFinal(out, 0);
  byte[] checksumBytes = Arrays.copyOfRange(out, 0, 4);

  pubKeyData = ByteUtil.concat(pubKeyData, checksumBytes);
  String eosPK = "EOS" + Base58.encode(pubKeyData);
  Assert.assertEquals(PUBLIC_KEY, eosPK);
}
 
Example 3
Source File: ContractBuilder.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
private String convertParameter(ContractMethodParameter parameter) {
    String _value = parameter.getValue();
    if (!parameterIsArray(parameter)) {
        if (parameter.getType().contains(TYPE_INT)) {
            return appendNumericPattern(convertToByteCode(new BigInteger(_value)));
        } else if (parameter.getType().contains(TYPE_STRING)) {
            return getStringOffset(parameter);
        } else if (parameter.getType().contains(TYPE_ADDRESS) && _value.length() == 34) {
            byte[] decode = Base58.decode(_value);
            String toHexString = Hex.toHexString(decode);
            String substring = toHexString.substring(2, 42);
            return appendAddressPattern(substring);
        } else if (parameter.getType().contains(TYPE_ADDRESS)) {
            return getStringOffset(parameter);
        } else if (parameter.getType().contains(TYPE_BOOL)) {
            return appendBoolean(_value);
        }
    } else {
        return getStringOffset(parameter);
    }
    return "";
}
 
Example 4
Source File: types.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
public private_key_type(String strBase58) throws KeyInvalideException, AddressFormatException {
    byte wif_bytes[] = Base58.decode(strBase58);

    if (wif_bytes.length < key_data.length) {
        throw new KeyInvalideException("Private key is not valid");
    }
    System.arraycopy(wif_bytes, 1, key_data, 0, key_data.length);
    SHA256Digest digest = new SHA256Digest();
    digest.update(wif_bytes, 0, wif_bytes.length - 4);
    byte[] hashCheck = new byte[32];
    digest.doFinal(hashCheck, 0);

    byte[] hashCheck2 = new byte[32];
    digest.update(hashCheck, 0, hashCheck.length);
    digest.doFinal(hashCheck2, 0);

    byte check[] = new byte[4];
    System.arraycopy(wif_bytes, wif_bytes.length - check.length, check, 0, check.length);

    byte[] check1 = new byte[4];
    byte[] check2 = new byte[4];
    System.arraycopy(hashCheck, 0, check1, 0, check1.length);
    System.arraycopy(hashCheck2, 0, check2, 0, check2.length);
    if (!Arrays.equals(check1, check) && !Arrays.equals(check2, check)) {
        throw new KeyInvalideException("Private key is not valid");
    }
}
 
Example 5
Source File: Utils.java    From evt4j with MIT License 5 votes vote down vote up
public static byte[] base58CheckDecode(String key, @Nullable String keyType) throws Base58CheckException {
    byte[] decoded;

    try {
        // base58 decode
        decoded = Base58.decode(key);
    } catch (AddressFormatException ex) {
        throw new Base58CheckException(ex.getMessage(), ex);
    }
    // split the byte slice
    byte[] data = ArrayUtils.subarray(decoded, 0, decoded.length - 4);
    byte[] checksum = ArrayUtils.subarray(decoded, decoded.length - 4, decoded.length);

    if (keyType != null) {
        data = ArrayUtils.addAll(data, keyType.getBytes());
    }

    // ripemd160 input, sign 4 bytes to compare
    byte[] hash = ripemd160(data);

    // if pass, return data, otherwise throw ex
    // compare two checksum
    boolean isEqual = true;

    for (int i = 0; i < checksum.length; i++) {
        if (hash[i] != checksum[i]) {
            isEqual = false;
        }
    }

    if (!isEqual) {
        throw new Base58CheckException();
    }

    if (keyType != null) {
        return ArrayUtils.subarray(data, 0, data.length - keyType.getBytes().length);
    }

    return data;
}
 
Example 6
Source File: Mile.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public AddressValidationResult validate(String address) {
    byte[] decoded;

    try {
        decoded = Base58.decode(address);
    } catch (AddressFormatException e) {
        return AddressValidationResult.invalidAddress(e.getMessage());
    }
    if (decoded.length != 32 + 4)
        return AddressValidationResult.invalidAddress("Invalid address");

    byte[] data = Arrays.copyOfRange(decoded, 0, decoded.length - 4);
    byte[] addrChecksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length);

    Checksum checksum = new CRC32();
    checksum.update(data, 0, data.length);
    long checksumValue = checksum.getValue();

    if ((byte)(checksumValue & 0xff) != addrChecksum[0] ||
            (byte)((checksumValue >> 8) & 0xff) != addrChecksum[1] ||
            (byte)((checksumValue >> 16) & 0xff) != addrChecksum[2] ||
            (byte)((checksumValue >> 24) & 0xff) != addrChecksum[3])
    {
        return AddressValidationResult.invalidAddress("Invalid address checksum");
    }

    return AddressValidationResult.validAddress();
}
 
Example 7
Source File: Ergo.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public AddressValidationResult validate(String address) {
    try {
        byte[] decoded  = Base58.decode(address);
        if (decoded.length < 4) {
            return AddressValidationResult.invalidAddress("Input too short: " + decoded.length);
        }
        if (decoded[0] != 1 && decoded[0] != 2 && decoded[0] != 3) {
            return AddressValidationResult.invalidAddress("Invalid prefix");
        }
    } catch (AddressFormatException e) {
        return AddressValidationResult.invalidAddress(e);
    }
    return AddressValidationResult.validAddress();
}
 
Example 8
Source File: EOSFormatter.java    From eosio-java with MIT License 4 votes vote down vote up
/**
 * This method Base58 decodes the private key and validates its checksum.
 *
 * @param strKey Base58 value of the key
 * @param keyType key type
 * @return Base58 decoded key minus checksum
 * @throws Base58ManipulationError if private key decoding fails.
 */
@NotNull
private static byte[] decodePrivateKey(@NotNull String strKey, AlgorithmEmployed keyType)
        throws Base58ManipulationError {
    if (strKey.isEmpty()) {
        throw new IllegalArgumentException(ErrorConstants.BASE58_EMPTY_KEY);
    }

    byte[] decodedKey;

    try {
        byte[] base58Decoded = Base58.decode(strKey);
        byte[] firstCheckSum = Arrays
                .copyOfRange(base58Decoded, base58Decoded.length - CHECKSUM_BYTES,
                        base58Decoded.length);
        decodedKey = Arrays
                .copyOfRange(base58Decoded, 0, base58Decoded.length - CHECKSUM_BYTES);

        switch (keyType) {
            case SECP256R1:
                byte[] secp256r1Suffix = SECP256R1_AND_PRIME256V1_CHECKSUM_VALIDATION_SUFFIX
                        .getBytes();
                if (invalidRipeMD160CheckSum(decodedKey, firstCheckSum, secp256r1Suffix)) {
                    throw new IllegalArgumentException(ErrorConstants.BASE58_INVALID_CHECKSUM);
                }
                break;
            case PRIME256V1:
                byte[] prime256v1Suffix = SECP256R1_AND_PRIME256V1_CHECKSUM_VALIDATION_SUFFIX
                        .getBytes();
                if (invalidRipeMD160CheckSum(decodedKey, firstCheckSum, prime256v1Suffix)) {
                    throw new IllegalArgumentException(ErrorConstants.BASE58_INVALID_CHECKSUM);
                }
                break;
            case SECP256K1:
                if (invalidSha256x2CheckSum(decodedKey, firstCheckSum)) {
                    throw new IllegalArgumentException(ErrorConstants.BASE58_INVALID_CHECKSUM);
                }
                break;
            default:
                throw new Base58ManipulationError(ErrorConstants.UNSUPPORTED_ALGORITHM);

        }

        // trim 0x80 out if the key size is more than 32 bytes
        // this code apply for key has more than 32 byte and non R1 key
        if (decodedKey.length > STANDARD_KEY_LENGTH && keyType != AlgorithmEmployed.SECP256R1) {
            // Slice out the first byte
            decodedKey = Arrays.copyOfRange(decodedKey, 1, decodedKey.length);
            if (decodedKey.length > STANDARD_KEY_LENGTH
                    && decodedKey[STANDARD_KEY_LENGTH] == ((Integer) 1).byteValue()) {
                // Slice out last byte
                decodedKey = Arrays.copyOfRange(decodedKey, 0, decodedKey.length - 1);
            }
        }
    } catch (Exception ex) {
        throw new Base58ManipulationError(ErrorConstants.BASE58_DECODING_ERROR, ex);
    }

    return decodedKey;
}
 
Example 9
Source File: EOSFormatter.java    From eosio-java with MIT License 4 votes vote down vote up
/**
 * Base58 decodes a public key and validates checksum.
 *
 * @param strKey Base58 encoded public key in string format.
 * @param keyPrefix EOS specific key type prefix (i.e. PUB_R1_, PUB_K1_, or EOS).
 * @return Base58 decoded public key as byte[]
 * @throws Base58ManipulationError if public key decoding fails.
 */
@NotNull
public static byte[] decodePublicKey(@NotNull String strKey, String keyPrefix)
        throws Base58ManipulationError {
    if (strKey.isEmpty()) {
        throw new IllegalArgumentException("Input key to decode can't be empty.");
    }

    byte[] decodedKey = null;

    try {
        byte[] base58Decoded = Base58.decode(strKey);
        byte[] firstCheckSum = Arrays
                .copyOfRange(base58Decoded, base58Decoded.length - CHECKSUM_BYTES,
                        base58Decoded.length);
        decodedKey = Arrays
                .copyOfRange(base58Decoded, 0, base58Decoded.length - CHECKSUM_BYTES);

        switch (keyPrefix) {
            case PATTERN_STRING_EOS_PREFIX_PUB_R1:
                if (invalidRipeMD160CheckSum(decodedKey, firstCheckSum,
                        SECP256R1_AND_PRIME256V1_CHECKSUM_VALIDATION_SUFFIX.getBytes())) {
                    throw new IllegalArgumentException(
                            ErrorConstants.BASE58_INVALID_CHECKSUM);
                }
                break;

            case PATTERN_STRING_EOS_PREFIX_PUB_K1:
                if (invalidRipeMD160CheckSum(decodedKey, firstCheckSum,
                        SECP256K1_CHECKSUM_VALIDATION_SUFFIX.getBytes())) {
                    throw new IllegalArgumentException(
                            ErrorConstants.BASE58_INVALID_CHECKSUM);
                }
                break;

            case PATTERN_STRING_EOS_PREFIX_EOS:
                if (invalidRipeMD160CheckSum(decodedKey, firstCheckSum,
                        LEGACY_CHECKSUM_VALIDATION_SUFFIX.getBytes())) {
                    throw new IllegalArgumentException(
                            ErrorConstants.BASE58_INVALID_CHECKSUM);
                }
                break;

            default:
                break;
        }

    } catch (Exception ex) {
        throw new Base58ManipulationError(ErrorConstants.BASE58_DECODING_ERROR, ex);
    }

    return decodedKey;
}
 
Example 10
Source File: Multihash.java    From token-core-android with Apache License 2.0 4 votes vote down vote up
public static Multihash fromBase58(String base58) {
  return new Multihash(Base58.decode(base58));
}
 
Example 11
Source File: Util.java    From zencash-swing-wallet-ui with MIT License 4 votes vote down vote up
public static String wifToHex(String wifKey) throws Exception {
        byte[] bytes = Base58.decode(wifKey);
        String pk = Util.encodeHexArray(bytes);
        pk = pk.substring(2, pk.length() - 10);
        return pk;
}