-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
093dad1
commit 281aae5
Showing
13 changed files
with
42 additions
and
53 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
62 changes: 27 additions & 35 deletions
62
src/main/java/org/arkecosystem/crypto/identities/Address.java
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 |
---|---|---|
@@ -1,58 +1,50 @@ | ||
package org.arkecosystem.crypto.identities; | ||
|
||
import com.google.common.primitives.Bytes; | ||
import org.arkecosystem.crypto.configuration.Network; | ||
import org.arkecosystem.crypto.encoding.Base58; | ||
import org.arkecosystem.crypto.encoding.Hex; | ||
import org.bitcoinj.core.ECKey; | ||
import org.bouncycastle.crypto.digests.RIPEMD160Digest; | ||
import org.web3j.crypto.Hash; | ||
import org.web3j.crypto.Keys; | ||
|
||
public class Address { | ||
public static String fromPassphrase(String passphrase, Integer networkVersion) { | ||
return fromPrivateKey(PrivateKey.fromPassphrase(passphrase), networkVersion); | ||
} | ||
|
||
public static String fromPassphrase(String passphrase) { | ||
return Address.fromPassphrase(passphrase, null); | ||
ECKey privateKey = PrivateKey.fromPassphrase(passphrase); | ||
return fromPrivateKey(privateKey); | ||
} | ||
|
||
public static String fromPublicKey(String publicKey, Integer networkVersion) { | ||
public static String fromPublicKey(String publicKey) { | ||
byte[] publicKeyBytes = Hex.decode(publicKey); | ||
|
||
RIPEMD160Digest digest = new RIPEMD160Digest(); | ||
digest.update(publicKeyBytes, 0, publicKeyBytes.length); | ||
byte[] out = new byte[20]; | ||
digest.doFinal(out, 0); | ||
// Ensure the public key is uncompressed | ||
ECKey ecKey = ECKey.fromPublicOnly(publicKeyBytes); | ||
byte[] uncompressedPublicKeyBytes = ecKey.getPubKeyPoint().getEncoded(false); | ||
|
||
if (networkVersion == null) { | ||
networkVersion = Network.get().version(); | ||
} | ||
// Remove the prefix (0x04) | ||
byte[] rawPublicKey = new byte[uncompressedPublicKeyBytes.length - 1]; | ||
System.arraycopy(uncompressedPublicKeyBytes, 1, rawPublicKey, 0, rawPublicKey.length); | ||
|
||
byte[] bytes = Bytes.concat(new byte[] {networkVersion.byteValue()}, out); | ||
return Base58.encodeChecked(bytes); | ||
} | ||
// Hash the public key using Keccak-256 | ||
byte[] keccakHash = Hash.sha3(rawPublicKey); | ||
|
||
public static String fromPublicKey(String publicKey) { | ||
return Address.fromPublicKey(publicKey, null); | ||
} | ||
// Take the last 20 bytes of the Keccak-256 hash | ||
byte[] addressBytes = new byte[20]; | ||
|
||
public static String fromPrivateKey(ECKey privateKey, Integer networkVersion) { | ||
return fromPublicKey(privateKey.getPublicKeyAsHex(), networkVersion); | ||
} | ||
System.arraycopy(keccakHash, keccakHash.length - 20, addressBytes, 0, 20); | ||
|
||
public static String fromPrivateKey(ECKey privateKey) { | ||
return Address.fromPrivateKey(privateKey, null); | ||
} | ||
// Convert to checksum address | ||
String address = "0x" + Hex.encode(addressBytes); | ||
|
||
public static Boolean validate(String address, Integer networkVersion) { | ||
if (networkVersion == null) { | ||
networkVersion = Network.get().version(); | ||
} | ||
return Keys.toChecksumAddress(address); | ||
} | ||
|
||
return Base58.decodeChecked(address)[0] == networkVersion; | ||
public static String fromPrivateKey(ECKey privateKey) { | ||
byte[] publicKeyBytes = privateKey.getPubKey(); | ||
return fromPublicKey(Hex.encode(publicKeyBytes)); | ||
} | ||
|
||
public static Boolean validate(String address) { | ||
return Address.validate(address, null); | ||
if (address == null || !address.matches("^0x[a-fA-F0-9]{40}$")) { | ||
return false; | ||
} | ||
return address.equals(Keys.toChecksumAddress(address)); | ||
} | ||
} |
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
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
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