-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from bianjieai/sv/evm
evm
- Loading branch information
Showing
15 changed files
with
814 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package irita.sdk.crypto.eth; | ||
|
||
import org.bouncycastle.crypto.params.ECDomainParameters; | ||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; | ||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; | ||
|
||
import java.math.BigInteger; | ||
import java.security.KeyPairGenerator; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
public class KeyPair { | ||
|
||
private final SECPPrivateKey privateKey; | ||
private final SECPPublicKey publicKey; | ||
|
||
public KeyPair(final SECPPrivateKey privateKey, final SECPPublicKey publicKey) { | ||
checkNotNull(privateKey); | ||
checkNotNull(publicKey); | ||
this.privateKey = privateKey; | ||
this.publicKey = publicKey; | ||
} | ||
|
||
public static KeyPair generate(final KeyPairGenerator keyPairGenerator, final String algorithm) { | ||
final java.security.KeyPair rawKeyPair = keyPairGenerator.generateKeyPair(); | ||
final BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate(); | ||
final BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic(); | ||
|
||
final BigInteger privateKeyValue = privateKey.getD(); | ||
|
||
// Ethereum does not use encoded public keys like bitcoin - see | ||
// https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details | ||
// Additionally, as the first bit is a constant prefix (0x04) we ignore this value | ||
final byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); | ||
final BigInteger publicKeyValue = | ||
new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); | ||
|
||
return new KeyPair( | ||
SECPPrivateKey.create(privateKeyValue, algorithm), | ||
SECPPublicKey.create(publicKeyValue, algorithm)); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(privateKey, publicKey); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object other) { | ||
if (!(other instanceof KeyPair)) { | ||
return false; | ||
} | ||
|
||
final KeyPair that = (KeyPair) other; | ||
return this.privateKey.equals(that.privateKey) && this.publicKey.equals(that.publicKey); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ("KeyPair{privateKey: " | ||
+ this.privateKey.toString() | ||
+ ", publicKey: " | ||
+ this.publicKey.toString() | ||
+ "]"); | ||
} | ||
|
||
public SECPPrivateKey getPrivateKey() { | ||
return privateKey; | ||
} | ||
|
||
public SECPPublicKey getPublicKey() { | ||
return publicKey; | ||
} | ||
} |
Oops, something went wrong.