[Caver] How to Update Klaytn Account Keys with Caver #2 — AccountKeyWeightedMultiSig

// caver-js 
const senderKeyring = caver.wallet.keyring.create(senderAddress, senderPrivateKey)
// caver-java
SingleKeyring senderKeyring = caver.wallet.keyring.create(senderAddress, senderPrivateKey);
// caver-js
caver.wallet.add(senderKeyring)
// caver-java
caver.wallet.add(senderKeyring);
// caver-js
const newKeys = caver.wallet.keyring.generateMultipleKeys(3)
// caver-java
String[] newKeys = caver.wallet.keyring.generateMultipleKeys(3);
// caver-js
const newKeyring = caver.wallet.keyring.create(senderKeyring.address, newKeys)
// caver-java
MultipleKeyring newKeyring = caver.wallet.keyring.create(senderKeyring.getAddress(), newKeys);
// caver-js
const account = newKeyring.toAccount({ threshold: 3, weights: [2, 1, 1] })
BigInteger[] weights = {BigInteger.valueOf(2), BigInteger.ONE, BigInteger.ONE};
WeightedMultiSigOptions options = new WeightedMultiSigOptions(BigInteger.valueOf(3), Arrays.asList(weights));
Account account = newKeyring.toAccount(options);
// caver-js
const accountUpdate = caver.transaction.accountUpdate.create({
from: senderKeyring.address,
account: account,
gas: 100000,
})
// caver-java
AccountUpdate accountUpdate = caver.transaction.accountUpdate.create(
TxPropertyBuilder.accountUpdate()
.setFrom(senderKeyring.getAddress())
.setAccount(account)
.setGas(BigInteger.valueOf(100000))
);
// caver-js
await caver.wallet.sign(senderKeyring.address, accountUpdate)
// caver-java
caver.wallet.sign(senderKeyring.getAddress(), accountUpdate);
// caver-js
caver.rpc.klay.sendRawTransaction(accountUpdate)
.on('transactionHash', hash => {
console.log(hash)
})
.on('receipt', receipt => {
console.log(receipt)
})
// caver-js
const receipt = await caver.rpc.klay.sendRawTransaction(accountUpdate)
// caver-java
Bytes32 sendResult = caver.rpc.klay.sendRawTransaction(accountUpdate).send();
String txHash = sendResult.getResult();
// caver-java
public String objectToString(Object value) throws JsonProcessingException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
return ow.writeValueAsString(value);
}
TransactionReceiptProcessor receiptProcessor = new PollingTransactionReceiptProcessor(caver, 1000, 15);
TransactionReceipt.TransactionReceiptData receiptData = receiptProcessor.waitForTransactionReceipt(txHash);System.out.println(objectToString(receiptData));
{
blockHash: ‘0xdf8b53b76fc1bc2dde099199026c48b7cae56724b752b1476ae2ab867598a1d5’,
blockNumber: ‘0x33c98c3’,
contractAddress: null,
from: ‘0xf3ecd45eeb2bb22d33d2d83dc2350d18691b5f80’,
gas: ‘0x186a0’,
gasPrice: ‘0x5d21dba00’,
gasUsed: ‘0x13c68’,
key: ‘0x04f86f03f86ce302a103d2df49e8d3b4da20b39a81f422c82c50e29a32b0a001b90d6de72a9cc76535f5e301a103b00854f13c6cb6cabab766c2c4b74bc568568a5600599be6ace18a789fc3b5dce301a1021968807b49413b84eea56d24bfe212166ccafdba2c1ae4d7c1e04a179f28dc5f’,
logs: [],
logsBloom: ‘0x00000…’,
nonce: ‘0x0’,
senderTxHash: ‘0x736f2a35cd4618f00ed4e6a76637b526b2b72e8fdc1a7dab35c3341a2c2027af’,
signatures: [
{ V: ‘0x7f5’, R: ‘0x89e33…’, S: ‘0x1c9a0…’ }
],
status: ‘0x1’,
transactionHash: ‘0x736f2a35cd4618f00ed4e6a76637b526b2b72e8fdc1a7dab35c3341a2c2027af’,
transactionIndex: ‘0x0’,
type: ‘TxTypeAccountUpdate’,
typeInt: 32
}
// caver-js
const accountKey = await caver.rpc.klay.getAccountKey(senderKeyring.address)
console.log(accountKey)
// caver-java
AccountKey accountKey = caver.rpc.klay.getAccountKey(senderKeyring.getAddress()).send();
System.out.println(objectToString(accountKey));
{
keyType: 4,
key: {
threshold: 3,
keys: [
{
weight: 2,
key: { x: ‘0xd2df4…’, y: ‘0x813fe…’ }
},
{
weight: 1,
key: { x: ‘0xb0085…’, y: ‘0x478f5…’ }
},
{
weight: 1,
key: { x: ‘0x19688…’, y: ‘0x9d8b3…’ }
}
]
}
}
Error: Returned error: invalid transaction v, r, s values of the sender
// caver-js
caver.wallet.updateKeyring(newKeyring)
// caver-java
caver.wallet.updateKeyring(newKeyring);
// caver-js
const vt = caver.transaction.valueTransfer.create({
from: senderKeyring.address,
to: recipientAddress,
value: 1,
gas: 100000,
})

await caver.wallet.sign(senderKeyring.address, vt)

const vtReceipt = await caver.rpc.klay.sendRawTransaction(vt)
// caver-javaValueTransfer vt = caver.transaction.valueTransfer.create(
TxPropertyBuilder.valueTransfer()
.setFrom(sednerKeyring.getAddress())
.setTo(recipientAddress)
.setValue(BigInteger.valueOf(1))
.setGas(BigInteger.valueOf(100000))
);
caver.wallet.sign(senderKeyring.getAddress(), vt);Bytes32 vtResult = caver.rpc.klay.sendRawTransaction(vt).send();TransactionReceipt.TransactionReceiptData vtReceiptData = receiptProcessor.waitForTransactionReceipt(vtResult.getResult());

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store