Skip to content

Instantly share code, notes, and snippets.

@w3kim
Last active December 17, 2019 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3kim/81eeb81dc0d933cb3736a8db41e8e097 to your computer and use it in GitHub Desktop.
Save w3kim/81eeb81dc0d933cb3736a8db41e8e097 to your computer and use it in GitHub Desktop.
private KlayTransactionReceipt.TransactionReceipt transfer(KlayCredentials from, KlayCredentials to, BigInteger value) throws IOException {
log.info("transfer {} KLAY [{} -> {}]", value.toString(), from.getAddress(), to.getAddress());
BigInteger nonce = caver.klay().getTransactionCount(from.getAddress(), BLK_PARAM).send().getValue();
TxTypeValueTransfer tx = TxTypeValueTransfer.createTransaction(
nonce,
GAS_PRICE,
GAS_LIMIT,
to.getAddress(),
value,
from.getAddress()
);
TransactionManager manager = new TransactionManager.Builder(caver, from)
.setTransactionReceiptProcessor(new PollingTransactionReceiptProcessor(caver, 1000, 10))
.setChaindId(CHAIN_ID)
.build();
return manager.executeTransaction(tx);
}
@Test
public void AccountUpdate() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
BigInteger privKey = Keys.createEcKeyPair().getPrivateKey();
KlayCredentials credential = KlayCredentials.create(privKey.toString(16)); // newly created credential
// At this point the account represented by acct does not exist as there hasn't been a single transaction regarding this account.
log.info("address = {}, private_key = {}", credential.getAddress(), privKey.toString(16));
KlayTransactionReceipt.TransactionReceipt receipt;
// Let's send small amount of KLAY to give birth to our new account
receipt = transfer(senderCredential, credential, Convert.toPeb("0.5", Convert.Unit.KLAY).toBigInteger());
Assert.assertNotNull(receipt);
// Let's see if the account has been created by retrieving it
KlayAccount account;
account = caver.klay().getAccount(credential.getAddress(), DefaultBlockParameterName.LATEST).send();
Assert.assertNotNull(account);
log.info("Account Type = {}", account.getResult().getAccType());
log.info("Balance = {}", caver.klay().getBalance(credential.getAddress(), BLK_PARAM).send().getValue());
// Now you can change your private key
ECKeyPair newKeyPair = Keys.createEcKeyPair();
AccountUpdateTransaction accountUpdateTx = AccountUpdateTransaction.create(
credential.getAddress(),
AccountKeyPublic.create(newKeyPair.getPublicKey()),
GAS_LIMIT
);
// New transaction manager
TransactionManager manager = new TransactionManager.Builder(caver, credential)
.setTransactionReceiptProcessor(new PollingTransactionReceiptProcessor(caver, 1000, 10))
.setChaindId(CHAIN_ID)
.build();
receipt = manager.executeTransaction(accountUpdateTx);
Assert.assertNotNull(receipt);
log.info("Receipt -> status = {}, tx_hash = {}", receipt.getStatus(), receipt.getTransactionHash());
log.info("Balance = {}", caver.klay().getBalance(credential.getAddress(), BLK_PARAM).send().getValue());
account = caver.klay().getAccount(credential.getAddress(), DefaultBlockParameterName.LATEST).send();
Assert.assertNotNull(account);
log.info("Account Type = {}", account.getResult().getAccType());
log.info("Balance = {}", caver.klay().getBalance(credential.getAddress(), BLK_PARAM).send().getValue());
// Confirming the updated account is working
KlayCredentials updatedCredential = KlayCredentials.create(
newKeyPair.getPrivateKey().toString(16),
credential.getAddress()
);
receipt = transfer(updatedCredential, senderCredential, Convert.toPeb("0.2", Convert.Unit.KLAY).toBigInteger());
Assert.assertNotNull(receipt);
log.info("Balance = {}", caver.klay().getBalance(credential.getAddress(), BLK_PARAM).send().getValue());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment