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

convert web3j-ext klay->kaia #5

Merged
merged 7 commits into from
Jul 3, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
42 changes: 21 additions & 21 deletions web3j-ext/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Web3j extension for Klaytn
# Web3j extension for Kaia

## Requirements
### Setting Java
- Use java version: 11 <= v <= 18
- Visit https://adoptopenjdk.net/ site
- Download OpenJDK

## Install Web3j Klaytn extension
## Install Web3j Kaia extension

To add the [Gradle Library](https://docs.gradle.org/current/userguide/getting_started.html) to your project:
```shell

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation "foundation.klaytn:web3j-ext:v0.9.3"
implementation "foundation.klaytn:web3rpc-java:v0.9.0"
implementation "org.web3j:core:4.9.8"
implementation "foundation.klaytn:web3j-ext:v0.9.3"
implementation "foundation.klaytn:web3rpc-java:v0.9.0"
implementation "org.web3j:core:4.9.8"
}
````
## Usage
Expand All @@ -28,35 +28,35 @@ See [example](./web3j-ext/src/main/java/org/web3j/example).
For basic web3j usage, you can learn through [Web3j tutorial](https://docs.web3j.io/4.10.0/quickstart/) .

### Send Fee Delegated Transaction on Baobab Test network
If you want to know more about the concept of fee delegated transaction supported by Klaytn network, please refer to [Klaytn docs](https://docs.klaytn.foundation/content/klaytn/design/transactions).
If you want to know more about the concept of fee delegated transaction supported by Kaia network, please refer to [Kaia docs](https://docs.klaytn.foundation/content/klaytn/design/transactions).

Check FeeDelegatedValueTransferExample.java file in web3j-ext [examples](https://github.com/klaytn/web3klaytn/tree/dev/web3j-ext/web3j-ext/src/main/java/org/web3j/example).
Check FeeDelegatedValueTransferExample.java file in web3j-ext [examples](https://github.com/kaiachain/kaia-sdk/tree/dev/web3j-ext/web3j-ext/src/main/java/org/web3j/example).
```file
package org.web3j.example;
import java.io.IOException;
import java.math.BigInteger;
import org.web3j.crypto.KlayCredentials;
import org.web3j.crypto.KlayRawTransaction;
import org.web3j.crypto.KlayTransactionEncoder;
import org.web3j.crypto.KaiaCredentials;
import org.web3j.crypto.KaiaRawTransaction;
import org.web3j.crypto.KaiaTransactionEncoder;
import org.web3j.crypto.transaction.type.TxType;
import org.web3j.crypto.transaction.type.TxTypeValueTransfer;
import org.web3j.crypto.transaction.type.TxType.Type;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthChainId;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.klaytn.Web3j;
import org.web3j.protocol.kaia.Web3j;
import org.web3j.utils.Numeric;
import org.web3j.protocol.klaytn.core.method.response.TransactionReceipt;
import org.web3j.protocol.kaia.core.method.response.TransactionReceipt;
public class FeeDelegatedValueTransferExample implements keySample {
public static void run() throws Exception {
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials credentials = KlayCredentials.create(keySample.LEGACY_KEY_privkey);
KlayCredentials credentials_feepayer = KlayCredentials.create(keySample.LEGACY_KEY_FEEPAYER_privkey);
KaiaCredentials credentials = KaiaCredentials.create(keySample.LEGACY_KEY_privkey);
KaiaCredentials credentials_feepayer = KaiaCredentials.create(keySample.LEGACY_KEY_FEEPAYER_privkey);
BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
Expand All @@ -70,7 +70,7 @@ public class FeeDelegatedValueTransferExample implements keySample {
TxType.Type type = Type.FEE_DELEGATED_VALUE_TRANSFER;
KlayRawTransaction raw = KlayRawTransaction.createTransaction(
KaiaRawTransaction raw = KaiaRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
Expand All @@ -80,10 +80,10 @@ public class FeeDelegatedValueTransferExample implements keySample {
from);
// Sign as sender
byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials);
byte[] signedMessage = KaiaTransactionEncoder.signMessage(raw, chainId, credentials);
// Sign same message as Fee payer
signedMessage = KlayTransactionEncoder.signMessageAsFeePayer(raw, chainId, credentials_feepayer);
signedMessage = KaiaTransactionEncoder.signMessageAsFeePayer(raw, chainId, credentials_feepayer);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
Expand All @@ -98,13 +98,13 @@ public class FeeDelegatedValueTransferExample implements keySample {
org.web3j.protocol.core.methods.response.TransactionReceipt ethReceipt = transactionReceiptProcessor
.waitForTransactionReceipt(txHash);
System.out.println("Receipt from eth_getTransactionReceipt : \n" + ethReceipt);
TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();
System.out.println("Receipt from klay_getTransactionReceipt : \n" + receipt);
TransactionReceipt receipt = web3j.kaiaGetTransactionReceipt(txHash).send().getResult();
System.out.println("Receipt from kaia_getTransactionReceipt : \n" + receipt);
web3j.shutdown();
TxTypeFeeDelegatedValueTransfer rawTransaction = TxTypeFeeDelegatedValueTransfer
.decodeFromRawTransaction(hexValue);
System.out.println("TxType : " + rawTransaction.getKlayType());
System.out.println("TxType : " + rawTransaction.getKaiaType());
}
}
````
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.web3j.utils.Numeric;
import org.web3j.utils.Strings;

public class KlayCredentials {
public class KaiaCredentials {
private final ECKeyPair ecKeyPair;
private final String address;

private KlayCredentials(ECKeyPair ecKeyPair, String address) {
private KaiaCredentials(ECKeyPair ecKeyPair, String address) {
this.ecKeyPair = ecKeyPair;
this.address = !Strings.isEmpty(address) ? Numeric.toHexStringWithPrefixZeroPadded(Numeric.toBigInt(address), 40) : "";
}
Expand All @@ -41,38 +41,38 @@ public String getAddress() {
}

/**
* Static method for creating KlayCredentials instance
* Static method for creating KaiaCredentials instance
* Use address extracted from private key
*
* @param privateKey private key for transaction signing
* @return KlayCredentials
* @return KaiaCredentials
*/
public static KlayCredentials create(String privateKey) {
public static KaiaCredentials create(String privateKey) {
ECKeyPair ecKeyPair = ECKeyPair.create(Numeric.toBigInt(privateKey));
String address = Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair));
return create(ecKeyPair, address);
}

/**
* Static method for creating KlayCredentials instance
* Static method for creating KaiaCredentials instance
* Use address extracted from private key
*
* @param ecKeyPair ecKeyPair for transaction signing
* @return KlayCredentials
* @return KaiaCredentials
*/
public static KlayCredentials create(ECKeyPair ecKeyPair) {
public static KaiaCredentials create(ECKeyPair ecKeyPair) {
String address = Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair));
return create(ecKeyPair, address);
}

/**
* Static method for creating KlayCredentials instance
* Static method for creating KaiaCredentials instance
*
* @param privateKey private key for transaction signing
* @param address address of account
* @return KlayCredentials
* @return KaiaCredentials
*/
public static KlayCredentials create(String privateKey, String address) {
public static KaiaCredentials create(String privateKey, String address) {
return create(ECKeyPair.create(Numeric.toBigInt(privateKey)), Numeric.prependHexPrefix(address));
}

Expand All @@ -95,20 +95,20 @@ public static boolean isDeCoupled(String privKey, String address) {


/**
* Static method for creating KlayCredentials instance
* Static method for creating KaiaCredentials instance
*
* @param ecKeyPair ecKeyPair for transaction signing
* @param address address of account
* @return KlayCredentials
* @return KaiaCredentials
*/
public static KlayCredentials create(ECKeyPair ecKeyPair, String address) {
return new KlayCredentials(ecKeyPair, address);
public static KaiaCredentials create(ECKeyPair ecKeyPair, String address) {
return new KaiaCredentials(ecKeyPair, address);
}

public static KlayCredentials createWithKlaytnWalletKey(String klaytnWalletKey) {
klaytnWalletKey = Numeric.cleanHexPrefix(klaytnWalletKey);
String privateKey = klaytnWalletKey.substring(0, 64);
String address = klaytnWalletKey.substring(68);
public static KaiaCredentials createWithKaiaWalletKey(String kaiaWalletKey) {
kaiaWalletKey = Numeric.cleanHexPrefix(kaiaWalletKey);
String privateKey = kaiaWalletKey.substring(0, 64);
String address = kaiaWalletKey.substring(68);
return create(privateKey, address);
}

Expand Down
Loading
Loading