Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

240723 java example fix #30

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web3j-ext/web3j-ext/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ publishing {
}
}
repositories {
maven {
maven {
def releasesRepoUrl = layout.buildDirectory.dir('libs')
url = releasesRepoUrl
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class KaiaCredentials {

private KaiaCredentials(ECKeyPair ecKeyPair, String address) {
this.ecKeyPair = ecKeyPair;
this.address = !Strings.isEmpty(address) ? Numeric.toHexStringWithPrefixZeroPadded(Numeric.toBigInt(address), 40) : "";
this.address = !Strings.isEmpty(address)
? Numeric.toHexStringWithPrefixZeroPadded(Numeric.toBigInt(address), 40)
: "";
}

public ECKeyPair getEcKeyPair() {
Expand Down Expand Up @@ -69,7 +71,7 @@ public static KaiaCredentials create(ECKeyPair ecKeyPair) {
* Static method for creating KaiaCredentials instance
*
* @param privateKey private key for transaction signing
* @param address address of account
* @param address address of account
* @return KaiaCredentials
*/
public static KaiaCredentials create(String privateKey, String address) {
Expand All @@ -78,27 +80,24 @@ public static KaiaCredentials create(String privateKey, String address) {

public Credentials convertToCredentials() {
Credentials Ethcredentials = Credentials.create(this.getEcKeyPair());
return Ethcredentials;

return Ethcredentials;
}

public boolean isDeCoupled() {
String address = Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair));
return !(address.equals(this.address));
}


public static boolean isDeCoupled(String privKey, String address) {
ECKeyPair ecKeyPair = ECKeyPair.create(Numeric.toBigInt(privKey));
return !(address.equals(Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair))));
}


/**
* Static method for creating KaiaCredentials instance
*
* @param ecKeyPair ecKeyPair for transaction signing
* @param address address of account
* @param address address of account
* @return KaiaCredentials
*/
public static KaiaCredentials create(ECKeyPair ecKeyPair, String address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* paper</a>.
*/
public class KaiaRawTransaction extends RawTransaction {
private byte[] value;
private byte[] value;
private Set<KaiaSignatureData> signatureData;

public KaiaRawTransaction(ITransaction transaction, Set<KaiaSignatureData> signatureData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public interface AccountKey {
byte[] toRlp();

enum Type {
NIL((byte)0x0, AccountKeyNil.class),
LEGACY((byte)0x01, AccountKeyLegacy.class),
PUBLIC((byte)0x02, AccountKeyPublic.class),
FAIL((byte)0x03, AccountKeyFail.class),
MULTISIG((byte)0x04, AccountKeyWeightedMultiSig.class),
ROLEBASED((byte)0x05, AccountKeyRoleBased.class);
NIL((byte) 0x0, AccountKeyNil.class),
LEGACY((byte) 0x01, AccountKeyLegacy.class),
PUBLIC((byte) 0x02, AccountKeyPublic.class),
FAIL((byte) 0x03, AccountKeyFail.class),
MULTISIG((byte) 0x04, AccountKeyWeightedMultiSig.class),
ROLEBASED((byte) 0x05, AccountKeyRoleBased.class);

private byte value;
private Class keyClass;
Expand All @@ -48,9 +48,9 @@ public Class getKeyClass() {
return keyClass;
}

public static AccountKey.Type findByValue(byte value){
for(AccountKey.Type v : values()){
if( v.getValue() == value ){
public static AccountKey.Type findByValue(byte value) {
for (AccountKey.Type v : values()) {
if (v.getValue() == value) {
return v;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.function.Function;

public class AccountKeyDecoder {
private static HashMap<AccountKey.Type, Function<byte[], AccountKey>> typeMap = new HashMap<AccountKey.Type, Function<byte[], AccountKey>>(){
private static HashMap<AccountKey.Type, Function<byte[], AccountKey>> typeMap = new HashMap<AccountKey.Type, Function<byte[], AccountKey>>() {
{
put(AccountKey.Type.PUBLIC, AccountKeyPublic::decodeFromRlp);
put(AccountKey.Type.MULTISIG, AccountKeyWeightedMultiSig::decodeFromRlp);
Expand All @@ -33,7 +33,7 @@ public class AccountKeyDecoder {
};

public static AccountKey fromRlp(String raw) {
if(Numeric.toHexString(AccountKeyNil.RLP).equals(raw))
if (Numeric.toHexString(AccountKeyNil.RLP).equals(raw))
return AccountKeyNil.create();

AccountKey.Type type = AccountKey.Type.findByValue(Numeric.hexStringToByteArray(raw)[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import java.util.Arrays;

/**
* AccountKeyFail is used for smart contract accounts so that a transaction sent from
* AccountKeyFail is used for smart contract accounts so that a transaction sent
* from
* the smart contract account always fails.
* If an account has the key AccountKeyFail, the tx validation process always fails.
* If an account has the key AccountKeyFail, the tx validation process always
* fails.
*/
public class AccountKeyFail implements AccountKey {

private static byte[] RLP = new byte[]{(byte) 0x03, (byte) 0xc0};
private static byte[] RLP = new byte[] { (byte) 0x03, (byte) 0xc0 };

protected AccountKeyFail() {
}
Expand Down Expand Up @@ -56,8 +58,10 @@ public byte[] toRlp() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AccountKeyFail that = (AccountKeyFail) o;
return Arrays.equals(toRlp(), that.toRlp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@
import java.util.Arrays;

/**
* AccountKeyLegacy represents a key of legacy account types. If an account has AccountKeyLegacy,
* AccountKeyLegacy represents a key of legacy account types. If an account has
* AccountKeyLegacy,
* the tx validation process is done like below (as Ethereum did):
* <ul>
* <li> Get the public key from ecrecover(txhash, txsig) </li>
* <li> Get the address of the public key </li>
* <li> The address is the sender </li>
* <li>Get the public key from ecrecover(txhash, txsig)</li>
* <li>Get the address of the public key</li>
* <li>The address is the sender</li>
* </ul>
*/
public class AccountKeyLegacy implements AccountKey {

private static byte[] RLP = new byte[]{(byte) 0x01, (byte) 0xc0};
private static byte[] RLP = new byte[] { (byte) 0x01, (byte) 0xc0 };

protected AccountKeyLegacy() {
}


public static AccountKeyLegacy create() {
return new AccountKeyLegacy();
}
Expand All @@ -61,8 +61,10 @@ public byte[] toRlp() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AccountKeyLegacy that = (AccountKeyLegacy) o;
return Arrays.equals(toRlp(), that.toRlp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@
import java.util.Arrays;

/**
* AccountKeyNil represents an empty key. If an account tries to having an AccountKeyNil object,
* the transaction will be failed. AccountKeyNil is only used only for TxTypeAccountUpdate transactions
* with role-based keys. For example, if an account tries to update RoleAccountUpdate key only, the key
* field of the TxTypeAccountUpdate transaction would be: [AccountKeyNil, NewKey, AccountKeyNil]
* Then, only the RoleAccountUpdate key is updated. Other roles are not updated. Refer to the
* AccountKeyNil represents an empty key. If an account tries to having an
* AccountKeyNil object,
* the transaction will be failed. AccountKeyNil is only used only for
* TxTypeAccountUpdate transactions
* with role-based keys. For example, if an account tries to update
* RoleAccountUpdate key only, the key
* field of the TxTypeAccountUpdate transaction would be: [AccountKeyNil,
* NewKey, AccountKeyNil]
* Then, only the RoleAccountUpdate key is updated. Other roles are not updated.
* Refer to the
* {@link AccountKeyRoleBased} for more detail.
*/
public class AccountKeyNil implements AccountKey {

public static byte[] RLP = new byte[]{(byte) 0x80};
public static byte[] RLP = new byte[] { (byte) 0x80 };

protected AccountKeyNil() {
}
Expand Down Expand Up @@ -59,8 +64,10 @@ public byte[] toRlp() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AccountKeyNil that = (AccountKeyNil) o;
return Arrays.equals(toRlp(), that.toRlp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@

/**
* AccountKeyPublic is used for accounts having one public key.
* If an account has an AccountKeyPublic object, the tx validation process is done like below:
* If an account has an AccountKeyPublic object, the tx validation process is
* done like below:
* <ul>
* <li> Get the public key derived from ecrecover(txhash, txsig) </li>
* <li> Check that the derived public key is the same as the corresponding </li>
* <li> account's public key </li>
* <li>Get the public key derived from ecrecover(txhash, txsig)</li>
* <li>Check that the derived public key is the same as the corresponding</li>
* <li>account's public key</li>
* </ul>
*/
public class AccountKeyPublic implements AccountKey {
Expand All @@ -58,15 +59,14 @@ public static AccountKeyPublic create(BigInteger publicKey) {

public static AccountKeyPublic create(String publicKeyX, String publicKeyY) {
return new AccountKeyPublic(
Numeric.prependHexPrefix(publicKeyX), Numeric.prependHexPrefix(publicKeyY)
);
Numeric.prependHexPrefix(publicKeyX), Numeric.prependHexPrefix(publicKeyY));
}

@Override
public byte[] toRlp() {
byte[] encodedTransaction = RlpEncoder.encode(
RlpString.create(Numeric.hexStringToByteArray(toCompressedPublicKey())));
byte[] type = {getType().getValue()};
byte[] type = { getType().getValue() };
return BytesUtils.concat(type, encodedTransaction);
}

Expand Down Expand Up @@ -111,8 +111,10 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AccountKeyPublic that = (AccountKeyPublic) o;
return Arrays.equals(toRlp(), that.toRlp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@
public class AccountKeyRoleBased implements AccountKey {

/**
* <p>First Key : roleTransaction<br>
* Default key. Transactions other than TxTypeAccountUpdate should be signed by the key of this role.
* <p>
* First Key : roleTransaction<br>
* Default key. Transactions other than TxTypeAccountUpdate should be signed by
* the key of this role.
*
* <p>Second Key : roleUpdate<br>
* TxTypeAccountUpdate transaction should be signed by this key. If this key is not present in the account,
* <p>
* Second Key : roleUpdate<br>
* TxTypeAccountUpdate transaction should be signed by this key. If this key is
* not present in the account,
* TxTypeAccountUpdate transaction is validated using RoleTransaction key.
*
* <p>Third Key : roleFeePayer<br>
* If this account wants to send tx fee instead of the sender, the transaction should be signed by this key.
* If this key is not present in the account, a fee-delegated transaction is validated using RoleTransaction key.
* <p>
* Third Key : roleFeePayer<br>
* If this account wants to send tx fee instead of the sender, the transaction
* should be signed by this key.
* If this key is not present in the account, a fee-delegated transaction is
* validated using RoleTransaction key.
*/
private List<AccountKey> accountKeys;

Expand Down Expand Up @@ -91,7 +98,7 @@ public byte[] toRlp() {
}

byte[] encodedTransaction = RlpEncoder.encode(new RlpList(rlpTypeList));
byte[] type = {getType().getValue()};
byte[] type = { getType().getValue() };
return BytesUtils.concat(type, encodedTransaction);
}

Expand All @@ -102,8 +109,10 @@ public Type getType() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AccountKeyRoleBased that = (AccountKeyRoleBased) o;
return Arrays.equals(toRlp(), that.toRlp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@
import java.util.List;

/**
* AccountKeyWeightedMultiSig is an account key type containing a threshold and WeightedPublicKeys.
* WeightedPublicKeys contains a slice of {weight and key}. To be a valid tx for an account associated
* with AccountKeyWeightedMultiSig, the weighted sum of signed public keys should be larger than the threshold.
* AccountKeyWeightedMultiSig is an account key type containing a threshold and
* WeightedPublicKeys.
* WeightedPublicKeys contains a slice of {weight and key}. To be a valid tx for
* an account associated
* with AccountKeyWeightedMultiSig, the weighted sum of signed public keys
* should be larger than the threshold.
*/
public class AccountKeyWeightedMultiSig implements AccountKey {

/**
* Validation threshold. To be a valid transaction, the weight sum of signatures should be larger than
* Validation threshold. To be a valid transaction, the weight sum of signatures
* should be larger than
* or equal to the threshold.
*/
private BigInteger threshold;

/**
* A slice of weighted public keys. A weighted public key contains a weight and a public key.
* A slice of weighted public keys. A weighted public key contains a weight and
* a public key.
*/
private List<WeightedPublicKey> weightedPublicKeys = new ArrayList<>();

Expand Down Expand Up @@ -84,7 +89,7 @@ public byte[] toRlp() {
rlpTypeList.add(new RlpList(rlpWeightedPublicKeys));

byte[] encodedTransaction = RlpEncoder.encode(new RlpList(rlpTypeList));
byte[] type = {getType().getValue()};
byte[] type = { getType().getValue() };
return BytesUtils.concat(type, encodedTransaction);
}

Expand All @@ -102,7 +107,8 @@ public static AccountKeyWeightedMultiSig decodeFromRlp(byte[] rawTransaction) {
RlpList rlpWeightedPublicKey = (RlpList) item;
BigInteger weight = ((RlpString) rlpWeightedPublicKey.getValues().get(0)).asPositiveBigInteger();
String compressedPublicKey = ((RlpString) rlpWeightedPublicKey.getValues().get(1)).asString();
weightedPublicKeys.add(new WeightedPublicKey(weight, AccountKeyPublicUtils.decompressKey(compressedPublicKey)));
weightedPublicKeys
.add(new WeightedPublicKey(weight, AccountKeyPublicUtils.decompressKey(compressedPublicKey)));
}

return new AccountKeyWeightedMultiSig(threshold, weightedPublicKeys);
Expand Down Expand Up @@ -144,8 +150,10 @@ public AccountKeyPublic getKey() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AccountKeyWeightedMultiSig that = (AccountKeyWeightedMultiSig) o;
return Arrays.equals(toRlp(), that.toRlp());
}
Expand Down
Loading
Loading