@Override public ECKey deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken token = p.getCurrentToken(); switch (token) { case VALUE_STRING: try { return DumpedPrivateKey.fromBase58(null, p.getValueAsString()).getKey(); } catch (AddressFormatException e) { throw new InvalidFormatException(p, "Invalid Key", p.getValueAsString(), ECKey.class); } default: return (ECKey) ctxt.handleUnexpectedToken(ECKey.class, p); } } }
public String getPrivateKeyAsWiF(NetworkParameters params) { return getPrivateKeyEncoded(params).toString(); }
/** * Construct a private key from its Base58 representation. * @param params * The expected NetworkParameters or null if you don't want validation. * @param base58 * The textual form of the private key. * @throws AddressFormatException * if the given base58 doesn't parse or the checksum is invalid * @throws WrongNetworkException * if the given private key is valid but for a different chain (eg testnet vs mainnet) */ public static DumpedPrivateKey fromBase58(@Nullable NetworkParameters params,String base58) throws AddressFormatException { return new DumpedPrivateKey(params, base58); }
@Test public void base58Encoding_leadingZero() throws Exception { String privkey = "91axuYLa8xK796DnBXXsMbjuc8pDYxYgJyQMvFzrZ6UfXaGYuqL"; ECKey key = DumpedPrivateKey.fromBase58(TestNet3Params.get(), privkey).getKey(); assertEquals(privkey, key.getPrivateKeyEncoded(TestNet3Params.get()).toString()); assertEquals(0, key.getPrivKeyBytes()[0]); }
@Test public void roundtripBase58() throws Exception { String base58 = "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk"; assertEquals(base58, DumpedPrivateKey.fromBase58(null, base58).toBase58()); } }
@Test public void checkNetwork() throws Exception { DumpedPrivateKey.fromBase58(MAINNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk"); }
DumpedPrivateKey(NetworkParameters params, byte[] keyBytes, boolean compressed) { super(params.getDumpedPrivateKeyHeader(), encode(keyBytes, compressed)); this.compressed = compressed; }
@Test public void cloning() throws Exception { DumpedPrivateKey a = new DumpedPrivateKey(MAINNET, new ECKey().getPrivKeyBytes(), true); // TODO: Consider overriding clone() in DumpedPrivateKey to narrow the type DumpedPrivateKey b = (DumpedPrivateKey) a.clone(); assertEquals(a, b); assertNotSame(a, b); }
@Test public void base58Encoding_stress() throws Exception { // Replace the loop bound with 1000 to get some keys with leading zero byte for (int i = 0 ; i < 20 ; i++) { ECKey key = new ECKey(); ECKey key1 = DumpedPrivateKey.fromBase58(TestNet3Params.get(), key.getPrivateKeyEncoded(TestNet3Params.get()).toString()).getKey(); assertEquals(Utils.HEX.encode(key.getPrivKeyBytes()), Utils.HEX.encode(key1.getPrivKeyBytes())); } }
@Test(expected = WrongNetworkException.class) public void checkNetworkWrong() throws Exception { DumpedPrivateKey.fromBase58(TESTNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk"); }
DumpedPrivateKey(NetworkParameters params, byte[] keyBytes, boolean compressed) { super(params.getDumpedPrivateKeyHeader(), encode(keyBytes, compressed)); this.compressed = compressed; }
public ECKey getKey(String format, String data) throws Exception { switch (format) { case WIF_UNCOMPRESSED: case WIF_COMPRESSED: DumpedPrivateKey pk = DumpedPrivateKey.fromBase58(PersistentUrls.getInstance().getCurrentNetworkParams(), data); return pk.getKey(); case BASE58: return decodeBase58PK(data); case BASE64: return decodeBase64PK(data); case HEX: return determineKey(data); case MINI: return decodeMiniKey(data); default: throw new Exception("Unknown key format: " + format); } }
@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()); }
public String getPrivateKeyAsWiF(NetworkParameters params) { return getPrivateKeyEncoded(params).toString(); }
/** * Construct a private key from its Base58 representation. * @param params * The expected NetworkParameters or null if you don't want validation. * @param base58 * The textual form of the private key. * @throws AddressFormatException * if the given base58 doesn't parse or the checksum is invalid * @throws WrongNetworkException * if the given private key is valid but for a different chain (eg testnet vs mainnet) */ public static DumpedPrivateKey fromBase58(@Nullable NetworkParameters params,String base58) throws AddressFormatException { return new DumpedPrivateKey(params, base58); }
DumpedPrivateKey(NetworkParameters params, byte[] keyBytes, boolean compressed) { super(params.getDumpedPrivateKeyHeader(), encode(keyBytes, compressed)); this.compressed = compressed; }
DumpedPrivateKey dpk = DumpedPrivateKey.fromBase58(null, this.wif); ECKey key = dpk.getKey();
@Test public void roundTripDumpedPrivKey() throws Exception { ECKey key = new ECKey(); assertTrue(key.isCompressed()); NetworkParameters params = UnitTestParams.get(); String base58 = key.getPrivateKeyEncoded(params).toString(); ECKey key2 = DumpedPrivateKey.fromBase58(params, base58).getKey(); assertTrue(key2.isCompressed()); assertTrue(Arrays.equals(key.getPrivKeyBytes(), key2.getPrivKeyBytes())); assertTrue(Arrays.equals(key.getPubKey(), key2.getPubKey())); }
public String getPrivateKeyAsWiF(NetworkParameters params) { return getPrivateKeyEncoded(params).toString(); }
/** * Construct a private key from its Base58 representation. * @param params * The expected NetworkParameters or null if you don't want validation. * @param base58 * The textual form of the private key. * @throws AddressFormatException * if the given base58 doesn't parse or the checksum is invalid * @throws WrongNetworkException * if the given private key is valid but for a different chain (eg testnet vs mainnet) */ public static DumpedPrivateKey fromBase58(@Nullable NetworkParameters params,String base58) throws AddressFormatException { return new DumpedPrivateKey(params, base58); }