Skip to content

Commit 1f848c7

Browse files
authored
Merge pull request #353 from tronprotocol/develop-evan-address
Develop evan address
2 parents ea45d2a + 04ad72b commit 1f848c7

34 files changed

+419
-215
lines changed

src/main/java/org/tron/common/crypto/ECKey.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.Arrays;
4141
import javax.annotation.Nullable;
4242
import javax.crypto.KeyAgreement;
43-
4443
import lombok.extern.slf4j.Slf4j;
4544
import org.spongycastle.asn1.ASN1InputStream;
4645
import org.spongycastle.asn1.ASN1Integer;
@@ -367,7 +366,7 @@ public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean
367366
* Compute an address from an encoded public key.
368367
*
369368
* @param pubBytes an encoded (uncompressed) public key
370-
* @return 20-byte address
369+
* @return 21-byte address
371370
*/
372371
public static byte[] computeAddress(byte[] pubBytes) {
373372
return Hash.sha3omit12(
@@ -378,7 +377,7 @@ public static byte[] computeAddress(byte[] pubBytes) {
378377
* Compute an address from a public point.
379378
*
380379
* @param pubPoint a public point
381-
* @return 20-byte address
380+
* @return 21-byte address
382381
*/
383382
public static byte[] computeAddress(ECPoint pubPoint) {
384383
return computeAddress(pubPoint.getEncoded(/* uncompressed */ false));

src/main/java/org/tron/common/crypto/Hash.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import java.security.Security;
2727
import lombok.extern.slf4j.Slf4j;
2828
import org.tron.common.crypto.jce.TronCastleProvider;
29+
import org.tron.core.Wallet;
2930

3031
@Slf4j
3132
public class Hash {
33+
3234
private static final Provider CRYPTO_PROVIDER;
3335

3436
private static final String HASH_256_ALGORITHM_NAME;
@@ -124,10 +126,12 @@ public static byte[] sha512(byte[] input) {
124126
* Calculates RIGTMOST160(SHA3(input)). This is used in address calculations. *
125127
*
126128
* @param input - data
127-
* @return - 20 right bytes of the hash keccak of the data
129+
* @return - add_pre_fix + 20 right bytes of the hash keccak of the data
128130
*/
129131
public static byte[] sha3omit12(byte[] input) {
130132
byte[] hash = sha3(input);
131-
return copyOfRange(hash, 12, hash.length);
133+
byte[] address = copyOfRange(hash, 11, hash.length);
134+
address[0] = Wallet.getAddressPreFixByte();
135+
return address;
132136
}
133137
}

src/main/java/org/tron/core/Constant.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ public class Constant {
3333
public static final String NORMAL_CONF = "config.conf";
3434
public static final String TEST_CONF = "config-test.conf";
3535
public static final String DATABASE_DIR = "storage.directory";
36+
37+
public static final byte ADD_PRE_FIX_BYTE_MAINNET = (byte) 0xa1; //a1 + address ,a1 is version
38+
public static final String ADD_PRE_FIX_STRING_MAINNET = "a1";
39+
public static final byte ADD_PRE_FIX_BYTE_TESTNET = (byte) 0xa0; //a0 + address ,a0 is version
40+
public static final String ADD_PRE_FIX_STRING_TESTNET = "a0";
41+
public static final int ADDRESS_SIZE = 42;
3642
}

src/main/java/org/tron/core/Wallet.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class Wallet {
7070
private Application app;
7171
private Node p2pnode;
7272
private Manager dbManager;
73+
private static String addressPreFixString = Constant.ADD_PRE_FIX_STRING_TESTNET; //default testnet
74+
private static byte addressPreFixByte = Constant.ADD_PRE_FIX_BYTE_TESTNET;
7375

7476
/**
7577
* Creates a new Wallet with a random ECKey.
@@ -102,6 +104,66 @@ public byte[] getAddress() {
102104
return ecKey.getAddress();
103105
}
104106

107+
public static String getAddressPreFixString() {
108+
return addressPreFixString;
109+
}
110+
111+
public static void setAddressPreFixString(String addressPreFixString) {
112+
Wallet.addressPreFixString = addressPreFixString;
113+
}
114+
115+
public static byte getAddressPreFixByte() {
116+
return addressPreFixByte;
117+
}
118+
119+
public static void setAddressPreFixByte(byte addressPreFixByte) {
120+
Wallet.addressPreFixByte = addressPreFixByte;
121+
}
122+
123+
public static boolean addressValid(ByteString bsAddress) {
124+
125+
if (bsAddress == null || bsAddress.size() == 0) {
126+
logger.warn("Warning: Address is empty !!");
127+
return false;
128+
}
129+
byte[] address = bsAddress.toByteArray();
130+
return addressValid(address);
131+
}
132+
133+
public static boolean addressValid(byte[] address) {
134+
if (address == null || address.length == 0) {
135+
logger.warn("Warning: Address is empty !!");
136+
return false;
137+
}
138+
if (address.length != Constant.ADDRESS_SIZE / 2) {
139+
logger.warn(
140+
"Warning: Address length need " + Constant.ADDRESS_SIZE + " but " + address.length
141+
+ " !!");
142+
return false;
143+
}
144+
if (address[0] != addressPreFixByte) {
145+
logger.warn("Warning: Address need prefix with " + addressPreFixByte + " but "
146+
+ address[0] + " !!");
147+
return false;
148+
}
149+
//Other rule;
150+
return true;
151+
}
152+
153+
public static boolean addressValid(String addressStr) {
154+
if (addressStr == null || "".equals(addressStr)) {
155+
logger.warn("Warning: Address is empty !!");
156+
return false;
157+
}
158+
try {
159+
byte[] address = ByteArray.fromHexString(addressStr);
160+
return addressValid(address);
161+
} catch (Exception e) {
162+
logger.error(e.getMessage());
163+
return false;
164+
}
165+
}
166+
105167
/**
106168
* Get balance by address.
107169
*/

src/main/java/org/tron/core/actuator/AssetIssueActuator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.protobuf.InvalidProtocolBufferException;
2222
import lombok.extern.slf4j.Slf4j;
2323
import org.tron.common.utils.ByteArray;
24+
import org.tron.core.Wallet;
2425
import org.tron.core.capsule.AccountCapsule;
2526
import org.tron.core.capsule.AssetIssueCapsule;
2627
import org.tron.core.capsule.TransactionResultCapsule;
@@ -85,7 +86,9 @@ public boolean validate() throws ContractValidateException {
8586
try {
8687
final AssetIssueContract assetIssueContract = this.contract.unpack(AssetIssueContract.class);
8788

88-
Preconditions.checkNotNull(assetIssueContract.getOwnerAddress(), "OwnerAddress is null");
89+
if (!Wallet.addressValid(assetIssueContract.getOwnerAddress())) {
90+
throw new ContractValidateException("Invalidate ownerAddress");
91+
}
8992
Preconditions.checkNotNull(assetIssueContract.getName(), "name is null");
9093

9194
if (this.dbManager.getAssetIssueStore().get(assetIssueContract.getName().toByteArray())

src/main/java/org/tron/core/actuator/CreateAccountActuator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.protobuf.ByteString;
66
import com.google.protobuf.InvalidProtocolBufferException;
77
import lombok.extern.slf4j.Slf4j;
8+
import org.tron.core.Wallet;
89
import org.tron.core.capsule.AccountCapsule;
910
import org.tron.core.capsule.TransactionResultCapsule;
1011
import org.tron.core.db.Manager;
@@ -52,7 +53,9 @@ public boolean validate() throws ContractValidateException {
5253
AccountCreateContract contract = this.contract.unpack(AccountCreateContract.class);
5354

5455
Preconditions.checkNotNull(contract.getAccountName(), "AccountName is null");
55-
Preconditions.checkNotNull(contract.getOwnerAddress(), "OwnerAddress is null");
56+
if (!Wallet.addressValid(contract.getOwnerAddress())) {
57+
throw new ContractValidateException("Invalidate ownerAddress");
58+
}
5659
Preconditions.checkNotNull(contract.getType(), "Type is null");
5760

5861
if (dbManager.getAccountStore().has(contract.getOwnerAddress().toByteArray())) {

src/main/java/org/tron/core/actuator/ParticipateAssetIssueActuator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import lombok.extern.slf4j.Slf4j;
2323
import org.joda.time.DateTime;
2424
import org.tron.common.utils.ByteArray;
25+
import org.tron.core.Wallet;
2526
import org.tron.core.capsule.AccountCapsule;
2627
import org.tron.core.capsule.AssetIssueCapsule;
2728
import org.tron.core.capsule.TransactionResultCapsule;
@@ -92,9 +93,12 @@ public boolean validate() throws ContractValidateException {
9293
final Contract.ParticipateAssetIssueContract participateAssetIssueContract =
9394
this.contract.unpack(Contract.ParticipateAssetIssueContract.class);
9495

95-
Preconditions
96-
.checkNotNull(participateAssetIssueContract.getOwnerAddress(), "OwnerAddress is null");
97-
Preconditions.checkNotNull(participateAssetIssueContract.getToAddress(), "ToAddress is null");
96+
if (!Wallet.addressValid(participateAssetIssueContract.getOwnerAddress())) {
97+
throw new ContractValidateException("Invalidate ownerAddress");
98+
}
99+
if (!Wallet.addressValid(participateAssetIssueContract.getToAddress())) {
100+
throw new ContractValidateException("Invalidate toAddress");
101+
}
98102
Preconditions.checkNotNull(participateAssetIssueContract.getAssetName(), "trx name is null");
99103
if (participateAssetIssueContract.getAmount() <= 0) {
100104
throw new ContractValidateException("Trx Num must be positive!");

src/main/java/org/tron/core/actuator/TransferActuator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.protobuf.ByteString;
66
import com.google.protobuf.InvalidProtocolBufferException;
77
import lombok.extern.slf4j.Slf4j;
8+
import org.tron.core.Wallet;
89
import org.tron.core.capsule.AccountCapsule;
910
import org.tron.core.capsule.TransactionResultCapsule;
1011
import org.tron.core.config.Parameter.ChainConstant;
@@ -62,8 +63,12 @@ public boolean validate() throws ContractValidateException {
6263
.getClass() + "]");
6364
}
6465
TransferContract transferContract = this.contract.unpack(TransferContract.class);
65-
Preconditions.checkNotNull(transferContract.getOwnerAddress(), "OwnerAddress is null");
66-
Preconditions.checkNotNull(transferContract.getToAddress(), "ToAddress is null");
66+
if (!Wallet.addressValid(transferContract.getOwnerAddress())) {
67+
throw new ContractValidateException("Invalidate ownerAddress");
68+
}
69+
if (!Wallet.addressValid(transferContract.getToAddress())) {
70+
throw new ContractValidateException("Invalidate toAddress");
71+
}
6772
Preconditions.checkNotNull(transferContract.getAmount(), "Amount is null");
6873

6974
if (transferContract.getOwnerAddress().equals(transferContract.getToAddress())) {

src/main/java/org/tron/core/actuator/TransferAssetActuator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.protobuf.InvalidProtocolBufferException;
2222
import java.util.Map;
2323
import org.tron.common.utils.ByteArray;
24+
import org.tron.core.Wallet;
2425
import org.tron.core.capsule.AccountCapsule;
2526
import org.tron.core.capsule.TransactionResultCapsule;
2627
import org.tron.core.db.AccountStore;
@@ -81,8 +82,12 @@ public boolean validate() throws ContractValidateException {
8182
TransferAssetContract transferAssetContract = this.contract
8283
.unpack(TransferAssetContract.class);
8384

84-
Preconditions.checkNotNull(transferAssetContract.getOwnerAddress(), "OwnerAddress is null");
85-
Preconditions.checkNotNull(transferAssetContract.getToAddress(), "ToAddress is null");
85+
if (!Wallet.addressValid(transferAssetContract.getOwnerAddress())) {
86+
throw new ContractValidateException("Invalidate ownerAddress");
87+
}
88+
if (!Wallet.addressValid(transferAssetContract.getToAddress())) {
89+
throw new ContractValidateException("Invalidate toAddress");
90+
}
8691
Preconditions.checkNotNull(transferAssetContract.getAssetName(), "AssetName is null");
8792
Preconditions.checkNotNull(transferAssetContract.getAmount(), "Amount is null");
8893
if (transferAssetContract.getOwnerAddress().equals(transferAssetContract.getToAddress())) {

src/main/java/org/tron/core/actuator/VoteWitnessActuator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.tron.core.actuator;
22

3-
import com.google.common.base.Preconditions;
43
import com.google.protobuf.Any;
54
import com.google.protobuf.ByteString;
65
import com.google.protobuf.InvalidProtocolBufferException;
76
import java.util.Iterator;
87
import lombok.extern.slf4j.Slf4j;
98
import org.tron.common.utils.ByteArray;
9+
import org.tron.core.Wallet;
1010
import org.tron.core.capsule.AccountCapsule;
1111
import org.tron.core.capsule.TransactionResultCapsule;
1212
import org.tron.core.db.AccountStore;
@@ -50,8 +50,10 @@ public boolean validate() throws ContractValidateException {
5050
}
5151

5252
VoteWitnessContract contract = this.contract.unpack(VoteWitnessContract.class);
53+
if (!Wallet.addressValid(contract.getOwnerAddress())) {
54+
throw new ContractValidateException("Invalidate address");
55+
}
5356
ByteString ownerAddress = contract.getOwnerAddress();
54-
Preconditions.checkNotNull(ownerAddress, "OwnerAddress is null");
5557

5658
AccountStore accountStore = dbManager.getAccountStore();
5759
byte[] ownerAddressBytes = ownerAddress.toByteArray();

0 commit comments

Comments
 (0)