private TypedKey getFromWifKey() { ImmutableList.Builder<CoinType> builder = ImmutableList.builder(); for (CoinType type : CoinID.getSupportedCoins()) { if (version == type.getDumpedPrivateKeyHeader()) { builder.add(type); } } final ECKey key = compressed ? ECKey.fromPrivate(content) : ECKey.fromPrivate(content).decompress(); return new TypedKey(builder.build(), key); }
private TypedKey getFromMiniKey() { final ECKey key = ECKey.fromPrivate(content).decompress(); return new TypedKey(CoinID.getSupportedCoins(), key); } }
/** * Tries to parse the addressStr as a Bitcoin style address and find potential compatible coin types * @param addressStr possible bitcoin type address * @param builder for the types list */ private static void tryBitcoinFamilyAddresses(final String addressStr, ImmutableList.Builder<CoinType> builder) { VersionedChecksummedBytes parsed; try { parsed = new VersionedChecksummedBytes(addressStr) { }; } catch (AddressFormatException e) { return; } int version = parsed.getVersion(); for (CoinType type : CoinID.getSupportedCoins()) { if (type.getAcceptableAddressCodes() == null) continue; for (int addressCode : type.getAcceptableAddressCodes()) { if (addressCode == version) { builder.add(type); break; } } } }
public TypedKey decryptBip38(String passphrase) throws BadPassphraseException { String normalizedPassphrase = Normalizer.normalize(passphrase, Normalizer.Form.NFC); ECKey key = ecMultiply ? decryptBip38EC(normalizedPassphrase) : decryptBip38NoEC(normalizedPassphrase); String address = null; for (CoinType type : CoinID.getSupportedCoins()) { String possibleAddress = key.toAddress(type).toString(); Sha256Hash hash = Sha256Hash.createDouble(possibleAddress.getBytes(Charsets.US_ASCII)); byte[] actualAddressHash = Arrays.copyOfRange(hash.getBytes(), 0, 4); if (Arrays.equals(actualAddressHash, addressHash)) { address = possibleAddress; } } if (address == null) { throw new BadPassphraseException(); } try { return new TypedKey(GenericUtils.getPossibleTypes(address), key); } catch (AddressMalformedException e) { throw new RuntimeException(e); // Should not happen } }
private void parseWif(String key) throws KeyFormatException { byte[] keyBytes = parseBase58(key); // Check if compatible boolean isCompatible = false; for (CoinType type : CoinID.getSupportedCoins()) { if (version == type.getDumpedPrivateKeyHeader()) { isCompatible = true; } } if (!isCompatible) { clearDataAndThrow(keyBytes, "No coin with private key version: " + version); } if (keyBytes.length == 33 && keyBytes[32] == 1) { compressed = true; content = Arrays.copyOf(keyBytes, 32); // Chop off the additional marker byte. clearData(keyBytes); } else if (keyBytes.length == 32) { compressed = false; content = keyBytes; } else { clearDataAndThrow(keyBytes, "Wrong number of bytes for a private key, not 32 or 33"); } }