TestNet URL:
private final static String rpcUrl = "https://rpc1.newchain.newtonproject.org/";
Get a Web3j
instance with a Web3jService
instance, which needs a URL as the parameter:
Web3j web3 = Web3j.build(new HttpService(rpcUrl));
Get chain ID with the Web3j
instance:
NetVersion netVersion = web3.netVersion().send();
String chainIDStr = netVersion.getNetVersion();
Create a standard keystore:
String fileName = WalletUtils.generateNewWalletFile(
"123qwe",
new File("C:\\Files\\wallet"));
- password (
String
): The password for the keystore. - destinationDirectory (
File
): The destination directory for the keystore.
Return a String
value which is the name of the keystore. It`s in the following format:
UTC--2019-03-09T03-20-42.743000000Z--02d9bec4c13aecd197362adf92ed23b00a95d8ab.json
Load an existing keystore:
Credentials credentials = WalletUtils.loadCredentials(
"123qwe",
"C:\\Files\\wallet\\UTC--2019-03-09T03-20-42.743000000Z--02d9bec4c13aecd197362adf92ed23b00a95d8ab.json");
- password(
String
): The password for the keystore. - source(
String
): The path of the keystore.
Return an Credentials
instance with keystore information.
Get the address of the keystore with the Credentials
instance:
String fromAddress = credentials.getAddress();
Convert the original format address into NEW format:
String fromAddressNEWFormat = AddressUtil.originalAddress2NewAddress(fromAddress, chainIDStr);
- originalAddress(
String
): The address in original format. - chainID(
String
): The chainID you get above.
Return a String
value which is the NEW format address.
Get balance of the keystore with the Web3j
instance:
EthGetBalance balance = web3.ethGetBalance(fromAddress, DefaultBlockParameterName.LATEST).send();
BigInteger b = balance.getBalance();
- s(
String
): The address. - defaultBlockParameter(
DefaultBlockParameter
): Integer block number, or the string "latest", "earliest" or "pending". You should put intoDefaultBlockParameterName.LATEST
("latest").
Returns the balance of the account of given address.
Get the transaction count(nonce) of the account:
EthGetTransactionCount ethGetTransactionCount = web3.ethGetTransactionCount(
fromAddress, DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
- s(
String
): The address. - defaultBlockParameter(
DefaultBlockParameter
): Integer block number, or the string "latest", "earliest" or "pending". You should put intoDefaultBlockParameterName.LATEST
("latest").
Return the nonce of the account of given address.
String newAddress = "NEW17zJoq3eHwv3x7cJNgdmG73Limvv7TwQurB4";
String toAddress = AddressUtil.newAddress2originalAddress(newAddress);
//getChainID() return the chain ID in hex string
String addressChainID = AddressUtil.getChainID(newAddress);
Integer inputChainID = Integer.parseInt(addressChainID,16);
Integer chainID = Integer.parseInt(chainIDStr);
if(!inputChainID.equals(chainID)){
System.out.println("Wrong address. Please check the address.");
return;
}
- newAdderss(
String
): The address in NEW format.
Return the original address.
EthGasPrice ethGasPrice = web3.ethGasPrice().send();
BigInteger gasPrice = ethGasPrice.getGasPrice();
None
Return the gasPrice.
Get gas limit with Web3j.ethEstimateGas()
function.This won`t sent a transaction on block chain.
BigDecimal value = BigDecimal.valueOf(10);
Transaction tx = Transaction.createEtherTransaction(
fromAddress, nonce, gasPrice, null, toAddress, Convert.toWei(value, Convert.Unit.ETHER).toBigInteger());
EthEstimateGas ethEstimateGas = web3.ethEstimateGas(tx).send();
BigInteger gasLimit = ethEstimateGas.getAmountUsed();
The Transaction.createEtherTransaction()
function has 6 parameters:
- from(
String
): From address. - nonce(
BigInteger
): The nonce you get from chain above. - gasPrice(
BigInteger
): The gasPrice you get from chain above. - gasLimit(
BigInteger
): The value you ask for, put null in this position. - to(
String
): To address. - value(
BigInteger
): The value you want to transfer.
Return the gasLimit.
The value passed into Transaction.createEtherTransaction()
is in format of ISSAC(WEI)
.
You can convert NEW(ETHER)
into ISSAC(WEI)
:
Convert.toWei(value, Convert.Unit.ETHER); // 10 NEW(ETHER)
Also you can convert ISSAC(WEI)
into NEW(ETHER)
:
Convert.fromWei(value, Convert.Unit.ETHER); // 10 ISSAC(WEI)
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
nonce, gasPrice, gasLimit, toAddress, Convert.toWei(value, Convert.Unit.ETHER).toBigInteger());
- nonce(
BigInteger
): The nonce. - gasPrice(
BigInteger
): The gasPrice. - gasLimit(
BigInteger
): The gasLimit. - to(
String
): To address. - value(
BigInteger
): The value.
Return the RawTransaction
instance.
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, Integer.parseInt(chainIDStr), credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(hexValue).send();
The TransactionEncoder.signMessage()
function require 3 parameters:
- rawTransaction(
RawTransaction
): The instance ofRawTransaction
. - chainId(
int
): The chainId. - credentials(
Credentials
): The instance ofCredentials
.
Return the signed message in byte[]
.
Response.Error error = ethSendTransaction.getError();
None
Return an instance of Response.Error
. If there are no errors, the instance will be null
.
Get transaction hash.
String hash = ethSendTransaction.getTransactionHash();
None
Return the String
value of transaction hash.
import com.fasterxml.jackson.databind.ObjectMapper;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Wallet;
import org.web3j.crypto.WalletFile;
import java.io.File;
import java.io.IOException;
...
private static void updateKeyStorePassword(String password, ECKeyPair ecKeyPair, String filePath) throws CipherException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
WalletFile walletFile = Wallet.createStandard(password, ecKeyPair);
File destination = new File(filePath);
objectMapper.writeValue(destination, walletFile);
}
...
String password = "password"; //Your <b>OLD</b> password
File file = new File("/your/keystore/path"); //Your <b>KEYSTORE</b> path
String filePath = file.getAbsolutePath();
Credentials credentials = WalletUtils.loadCredentials(password, file);
ECKeyPair ecKeyPair = credentials.getEcKeyPair();
updateKeyStorePassword(password, ecKeyPair, filePath);
String password, ECKeyPair ecKeyPair, File destinationDirectory, String fileName
- password(
String
): The NEW password you want for the keystore. - ecKeyPair(
ECKeyPair
): The ECKeyPair instance got from CredenTtials.getEcKeyPair(). - filePath(
String
)L: The absolute file path of your keystore.
No return value.
####Code
String addressChainID = AddressUtil.getChainID(newAddress);
Integer inputChainID = Integer.parseInt(addressChainID,16);
System.out.println("input ID : " + inputChainID);
Integer chainID = Integer.parseInt(chainIDStr);
System.out.println("chain ID : " + chainID);
if(!inputChainID.equals(chainID)){
System.out.println("Wrong input address. Please check the address you input.");
return;
}