|
| 1 | +/* |
| 2 | + * Copyright 2019 Web3 Labs Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package org.web3j.crypto; |
| 14 | + |
| 15 | +import java.math.BigInteger; |
| 16 | +import java.security.KeyPair; |
| 17 | +import java.util.Arrays; |
| 18 | + |
| 19 | +import org.bouncycastle.crypto.digests.SHA256Digest; |
| 20 | +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; |
| 21 | +import org.bouncycastle.crypto.signers.ECDSASigner; |
| 22 | +import org.bouncycastle.crypto.signers.HMacDSAKCalculator; |
| 23 | +import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; |
| 24 | +import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; |
| 25 | + |
| 26 | +import org.web3j.utils.Numeric; |
| 27 | + |
| 28 | +/** Elliptic Curve SECP-256k1 generated key pair. */ |
| 29 | +public class ECKeyPair { |
| 30 | + private final BigInteger privateKey; |
| 31 | + private final BigInteger publicKey; |
| 32 | + |
| 33 | + public ECKeyPair(BigInteger privateKey, BigInteger publicKey) { |
| 34 | + this.privateKey = privateKey; |
| 35 | + this.publicKey = publicKey; |
| 36 | + } |
| 37 | + |
| 38 | + public BigInteger getPrivateKey() { |
| 39 | + return privateKey; |
| 40 | + } |
| 41 | + |
| 42 | + public BigInteger getPublicKey() { |
| 43 | + return publicKey; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Sign a hash with the private key of this key pair. |
| 48 | + * |
| 49 | + * @param transactionHash the hash to sign |
| 50 | + * @return An {@link ECDSASignature} of the hash |
| 51 | + */ |
| 52 | + public ECDSASignature sign(byte[] transactionHash) { |
| 53 | + ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); |
| 54 | + |
| 55 | + ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); |
| 56 | + signer.init(true, privKey); |
| 57 | + BigInteger[] components = signer.generateSignature(transactionHash); |
| 58 | + |
| 59 | + return new ECDSASignature(components[0], components[1]).toCanonicalised(); |
| 60 | + } |
| 61 | + |
| 62 | + public static ECKeyPair create(KeyPair keyPair) { |
| 63 | + BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); |
| 64 | + BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); |
| 65 | + |
| 66 | + BigInteger privateKeyValue = privateKey.getD(); |
| 67 | + |
| 68 | + // Ethereum does not use encoded public keys like bitcoin - see |
| 69 | + // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details |
| 70 | + // Additionally, as the first bit is a constant prefix (0x04) we ignore this value |
| 71 | + byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); |
| 72 | + BigInteger publicKeyValue = |
| 73 | + new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); |
| 74 | + |
| 75 | + return new ECKeyPair(privateKeyValue, publicKeyValue); |
| 76 | + } |
| 77 | + |
| 78 | + public static ECKeyPair create(BigInteger privateKey) { |
| 79 | + return new ECKeyPair(privateKey, Sign.publicKeyFromPrivate(privateKey)); |
| 80 | + } |
| 81 | + |
| 82 | + public static ECKeyPair create(byte[] privateKey) { |
| 83 | + return create(Numeric.toBigInt(privateKey)); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean equals(Object o) { |
| 88 | + if (this == o) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + if (o == null || getClass() != o.getClass()) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + ECKeyPair ecKeyPair = (ECKeyPair) o; |
| 96 | + |
| 97 | + if (privateKey != null |
| 98 | + ? !privateKey.equals(ecKeyPair.privateKey) |
| 99 | + : ecKeyPair.privateKey != null) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + return publicKey != null |
| 104 | + ? publicKey.equals(ecKeyPair.publicKey) |
| 105 | + : ecKeyPair.publicKey == null; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int hashCode() { |
| 110 | + int result = privateKey != null ? privateKey.hashCode() : 0; |
| 111 | + result = 31 * result + (publicKey != null ? publicKey.hashCode() : 0); |
| 112 | + return result; |
| 113 | + } |
| 114 | +} |
0 commit comments