Last active
December 9, 2019 02:14
-
-
Save w3kim/df5a92872f224e4845d57c4347c359b6 to your computer and use it in GitHub Desktop.
createAccountForUpdate example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Caver = require('caver-js'); | |
const caver = new Caver('https://api.baobab.klaytn.net:8651'); | |
const address = '0x{address_of_account_you_wish_to_update}'; | |
const oldPrivateKey = '0x{effective_private_key_bound_to_the_address_above}'; | |
const newPrivateKey = caver.klay.accounts.create().privateKey; // new private key to update | |
// use createAccountForUpdate | |
const accountForUpdate = caver.klay.accounts.createAccountForUpdate(address, newPrivateKey); | |
console.log('>>> about to configure ACCOUNT(address=', address, ') with the following key:', newPrivateKey) | |
const updateTx = { | |
type: 'ACCOUNT_UPDATE', | |
from: address, | |
key: accountForUpdate, | |
gas: 300000, | |
} | |
async function print(updateTx) { | |
// sign with private key, not with address | |
const signed = await caver.klay.accounts.signTransaction(updateTx, oldPrivateKey); | |
// CAUTION: once this transaction gets passed, your old account uses the new private key | |
const receipt = await caver.klay.sendSignedTransaction(signed) | |
console.log(receipt) | |
console.log('>>> new private key is in effect:', newPrivateKey); | |
console.log('>>> old private key is no longer effective:', oldPrivateKey); | |
const updatedKey = await caver.klay.getAccountKey(address) | |
console.log(updatedKey) | |
} | |
print(updateTx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment