WIF { public static byte[] getPrivateKeyFromWIF(String wif) { if (wif == null) { throw new NullPointerException(); } byte[] data = Base58.decode(wif); if (data.length != 38 || data[0] != (byte) 0x80 || data[33] != 0x01) { throw new IllegalArgumentException("Incorrect WIF format."); } byte[] checksum = hash256(data, 0, data.length - 4); for (int i = 0; i < 4; i++) { if (data[data.length - 4 + i] != checksum[i]) { throw new IllegalArgumentException("Incorrect WIF checksum."); } } byte[] privateKey = new byte[32]; System.arraycopy(data, 1, privateKey, 0, privateKey.length); Arrays.fill(data, (byte) 0); return privateKey; }  static byte[] getPrivateKeyFromWIF(String wif); static String getWIFFromPrivateKey(byte[] key);  }
@Test public void testWIF() { final String privateKey = "9117f4bf9be717c9a90994326897f4243503accd06712162267e77f18b49c3a3"; byte[] privateKeyFromWIF = WIF.getPrivateKeyFromWIF("L25kgAQJXNHnhc7Sx9bomxxwVSMsZdkaNQ3m2VfHrnLzKWMLP13A"); assertThat(Numeric.toHexStringNoPrefix(privateKeyFromWIF), is(privateKey)); }
@Test(expected = IllegalArgumentException.class) public void testWIFLargerThan38() { WIF.getPrivateKeyFromWIF("L25kgAQJXNHnhc7Sx9bomxxwVSMsZdkaNQ3m2VfHrnLzKWMLP13A000"); }
@Test(expected = IllegalArgumentException.class) public void testWIFFirstByteDifferentThan0x80() { byte[] wifBytes = Base58.decode("L25kgAQJXNHnhc7Sx9bomxxwVSMsZdkaNQ3m2VfHrnLzKWMLP13A"); wifBytes[0] = (byte) 0x81; String wifString = Base58.encode(wifBytes); WIF.getPrivateKeyFromWIF(wifString); }
@Test(expected = IllegalArgumentException.class) public void testWIF33ByteDifferentThan0x01() { byte[] wifBytes = Base58.decode("L25kgAQJXNHnhc7Sx9bomxxwVSMsZdkaNQ3m2VfHrnLzKWMLP13A"); wifBytes[33] = (byte) 0x00; String wifString = Base58.encode(wifBytes); WIF.getPrivateKeyFromWIF(wifString); }
@Test(expected = NullPointerException.class) public void testWIFNull() { WIF.getPrivateKeyFromWIF(null); }