|
| 1 | +package main; |
| 2 | + |
| 3 | +import main.crypto.Blake2b; |
| 4 | +import net.i2p.crypto.eddsa.EdDSAPrivateKey; |
| 5 | +import net.i2p.crypto.eddsa.KeyPairGenerator; |
| 6 | +import net.i2p.crypto.eddsa.Utils; |
| 7 | +import org.aion.util.conversions.Hex; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.security.KeyPair; |
| 10 | +import java.security.spec.InvalidKeySpecException; |
| 11 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 12 | +import java.util.Arrays; |
| 13 | + |
| 14 | + |
| 15 | +public final class PrivateKey { |
| 16 | + public static final int SIZE = 32; |
| 17 | + |
| 18 | + private final byte[] key; |
| 19 | + private final byte[] address; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructs a new private key consisting of the provided bytes. |
| 23 | + * |
| 24 | + * @param privateKeyBytes The bytes of the private key. |
| 25 | + */ |
| 26 | + private PrivateKey(byte[] privateKeyBytes) throws InvalidKeySpecException { |
| 27 | + if (privateKeyBytes == null) { |
| 28 | + throw new NullPointerException("private key bytes cannot be null"); |
| 29 | + } |
| 30 | + if (privateKeyBytes.length != SIZE) { |
| 31 | + throw new IllegalArgumentException("bytes of a private key must have a length of " + SIZE); |
| 32 | + } |
| 33 | + this.key = privateKeyBytes.clone(); |
| 34 | + this.address = deriveAddress(this.key); |
| 35 | + } |
| 36 | + |
| 37 | + public static PrivateKey fromBytes(byte[] privateKeyBytes) throws InvalidKeySpecException { |
| 38 | + return new PrivateKey(privateKeyBytes); |
| 39 | + } |
| 40 | + |
| 41 | + public static PrivateKey random() { |
| 42 | + try { |
| 43 | + return new PrivateKey(generatePrivateKey()); |
| 44 | + } catch (InvalidKeySpecException e) { |
| 45 | + // Hiding the checked exception because this should never actually happen here. We have |
| 46 | + // complete control over these bytes and know they are generated in a sound way. |
| 47 | + throw new RuntimeException(e.getMessage()); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public byte[] copyOfUnderlyingBytes() { |
| 52 | + return this.key.clone(); |
| 53 | + } |
| 54 | + |
| 55 | + public byte[] getPublicAionAddress() { |
| 56 | + return this.address; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String toString() { |
| 61 | + return "com.theoan.transactionbuilder.main.PrivateKey { 0x" + Hex.toHexString(this.key) + " }"; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns true only if other is a com.theoan.transactionbuilder.main.PrivateKey with the same underlying bytes. |
| 66 | + * |
| 67 | + * @param other The other whose equality is to be tested. |
| 68 | + * @return whether this is equal to other. |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public boolean equals(Object other) { |
| 72 | + if (!(other instanceof PrivateKey)) { |
| 73 | + return false; |
| 74 | + } else if (other == this) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + return Arrays.equals(this.key, ((PrivateKey) other).key); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int hashCode() { |
| 82 | + return Arrays.hashCode(this.key); |
| 83 | + } |
| 84 | + |
| 85 | + public static byte[] generatePrivateKey() { |
| 86 | + KeyPairGenerator keyPairGenerator = new KeyPairGenerator(); |
| 87 | + KeyPair pair = keyPairGenerator.generateKeyPair(); |
| 88 | + EdDSAPrivateKey privateKey = (EdDSAPrivateKey) pair.getPrivate(); |
| 89 | + return Utils.hexToBytes(Utils.bytesToHex(privateKey.getEncoded()).substring(32, 96)); |
| 90 | + } |
| 91 | + |
| 92 | + private static byte[] deriveAddress(byte[] privateKeyBytes) throws InvalidKeySpecException { |
| 93 | + if (privateKeyBytes == null) { |
| 94 | + throw new NullPointerException("private key cannot be null"); |
| 95 | + } |
| 96 | + |
| 97 | + if (privateKeyBytes.length != 32){ |
| 98 | + throw new IllegalArgumentException("private key mute be 32 bytes"); |
| 99 | + } |
| 100 | + |
| 101 | + EdDSAPrivateKey privateKey = new EdDSAPrivateKey(new PKCS8EncodedKeySpec(addSkPrefix(Utils.bytesToHex(privateKeyBytes)))); |
| 102 | + byte[] publicKeyBytes = privateKey.getAbyte(); |
| 103 | + |
| 104 | + return computeA0Address(publicKeyBytes); |
| 105 | + } |
| 106 | + |
| 107 | + private static byte[] addSkPrefix(String skString){ |
| 108 | + String skEncoded = "302e020100300506032b657004220420" + skString; |
| 109 | + return Utils.hexToBytes(skEncoded); |
| 110 | + } |
| 111 | + |
| 112 | + private static byte[] computeA0Address(byte[] publicKey) { |
| 113 | + byte A0_IDENTIFIER = (byte) 0xa0; |
| 114 | + ByteBuffer buf = ByteBuffer.allocate(32); |
| 115 | + buf.put(A0_IDENTIFIER); |
| 116 | + buf.put(blake256(publicKey), 1, 31); |
| 117 | + return buf.array(); |
| 118 | + } |
| 119 | + |
| 120 | + private static byte[] blake256(byte[] input) { |
| 121 | + Blake2b digest = Blake2b.Digest.newInstance(32); |
| 122 | + digest.update(input); |
| 123 | + return digest.digest(); |
| 124 | + } |
| 125 | +} |
0 commit comments