Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Added handling for send account update tx with key field #647

Merged
merged 2 commits into from
Apr 18, 2022
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/caver-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const POLLINGTIMEOUT = AVERAGE_BLOCK_TIME * TIMEOUTBLOCK // ~average block time

const TransactionDecoder = require('../../caver-transaction/src/transactionDecoder/transactionDecoder')
const { TX_TYPE_STRING } = require('../../caver-transaction/src/transactionHelper/transactionHelper')
const { resolveRawKeyToAccountKey } = require('../../caver-klay/caver-klay-accounts/src/transactionType/account')

function Method(options) {
// call, name should be existed to create a method.
Expand Down Expand Up @@ -427,6 +428,9 @@ const buildSendRequestFunc = (defer, sendSignedTx, sendTxCallback) => (payload,
}
} else if (key === 'codeFormat') {
tx[key] = utils.hexToNumber(payload.params[0][key])
} else if (key === 'key' && _.isObject(payload.params[0][key])) {
// If key field is `AccountForUpdate`, resolve this to raw encoded account key string.
tx.key = resolveRawKeyToAccountKey(payload.params[0])
} else if (key === 'account') {
tx.key = payload.params[0][key].getRLPEncodingAccountKey()
} else if (key === 'chainId') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,5 @@ module.exports = {
rlpEncodeForFeeDelegatedAccountUpdate,
rlpEncodeForFeeDelegatedAccountUpdateWithRatio,
parseAccountKey,
resolveRawKeyToAccountKey,
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,36 @@ const TX_TYPE_TAG = {
'0x7802': TX_TYPE_STRING.TxTypeEthereumDynamicFee,
}

const TX_TYPE_TAG_LEGACY_TX_TYPES = {
ACCOUNT_UPDATE: TX_TYPE_TAG.TxTypeAccountUpdate,
FEE_DELEGATED_ACCOUNT_UPDATE: TX_TYPE_TAG.TxTypeFeeDelegatedAccountUpdate,
FEE_DELEGATED_ACCOUNT_UPDATE_WITH_RATIO: TX_TYPE_TAG.TxTypeFeeDelegatedAccountUpdateWithRatio,

VALUE_TRANFSER: TX_TYPE_TAG.TxTypeValueTransfer,
FEE_DELEGATED_VALUE_TRANSFER: TX_TYPE_TAG.TxTypeFeeDelegatedValueTransfer,
FEE_DELEGATED_VALUE_TRANSFER_WITH_RATIO: TX_TYPE_TAG.TxTypeFeeDelegatedValueTransferWithRatio,

VALUE_TRANSFER_MEMO: TX_TYPE_TAG.TxTypeValueTransferMemo,
FEE_DELEGATED_VALUE_TRANSFER_MEMO: TX_TYPE_TAG.TxTypeFeeDelegatedValueTransferMemo,
FEE_DELEGATED_VALUE_TRANSFER_MEMO_WITH_RATIO: TX_TYPE_TAG.TxTypeFeeDelegatedValueTransferMemoWithRatio,

SMART_CONTRACT_DEPLOY: TX_TYPE_TAG.TxTypeSmartContractDeploy,
FEE_DELEGATED_SMART_CONTRACT_DEPLOY: TX_TYPE_TAG.TxTypeFeeDelegatedSmartContractDeploy,
FEE_DELEGATED_SMART_CONTRACT_DEPLOY_WITH_RATIO: TX_TYPE_TAG.TxTypeFeeDelegatedSmartContractDeployWithRatio,

SMART_CONTRACT_EXECUTION: TX_TYPE_TAG.TxTypeSmartContractExecution,
FEE_DELEGATED_SMART_CONTRACT_EXECUTION: TX_TYPE_TAG.TxTypeFeeDelegatedSmartContractExecution,
FEE_DELEGATED_SMART_CONTRACT_EXECUTION_WITH_RATIO: TX_TYPE_TAG.TxTypeFeeDelegatedSmartContractExecutionWithRatio,

CANCEL: TX_TYPE_TAG.TxTypeCancel,
FEE_DELEGATED_CANCEL: TX_TYPE_TAG.TxTypeFeeDelegatedCancel,
FEE_DELEGATED_CANCEL_WITH_RATIO: TX_TYPE_TAG.TxTypeFeeDelegatedCancelWithRatio,

CHAIN_DATA_ANCHORING: TX_TYPE_TAG.TxTypeChainDataAnchoring,
FEE_DELEGATED_CHAIN_DATA_ANCHORING: TX_TYPE_TAG.TxTypeFeeDelegatedChainDataAnchoring,
FEE_DELEGATED_CHAIN_DATA_ANCHORING_WITH_RATIO: TX_TYPE_TAG.TxTypeFeeDelegatedChainDataAnchoringWithRatio,
}

const CODE_FORMAT = {
EVM: '0x0',
}
Expand All @@ -186,7 +216,12 @@ const CODE_FORMAT = {
* @return {number}
*/
const getTypeInt = type => {
return utils.hexToNumber(TX_TYPE_TAG[type])
let typeInt = TX_TYPE_TAG[type]
// If type int cannot be found from TX_TYPE_TAG, means old type string.
if (typeInt === undefined) {
typeInt = TX_TYPE_TAG_LEGACY_TX_TYPES[type]
}
return utils.hexToNumber(typeInt)
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ describe('Personal RPC test', () => {
expect(receipt.type).to.equals('TxTypeLegacyTransaction')
}).timeout(50000)

// sendTransaction with account update tx
it('CAVERJS-UNIT-ETC-404: sendTransaction should format a transaction to send tx with account in Node.', async () => {
try {
// If account is already existed in node, return error.
const address = await caver.klay.personal.importRawKey(senderPrvKey, password)
expect(address.toLowerCase()).to.equals(senderAddress.toLowerCase())
} catch (e) {}

const receipt = await caver.klay.personal.sendTransaction(
{
type: 'ACCOUNT_UPDATE',
from: senderAddress,
key: caver.klay.accounts.createAccountForUpdateWithLegacyKey(senderAddress),
gas: 900000,
},
password
)
console.log(receipt)
expect(receipt).not.to.be.null
expect(receipt.type).to.equals('TxTypeAccountUpdate')
}).timeout(50000)

// signTransaction
it('CAVERJS-UNIT-ETC-088: signTransaction should send a signed transaction using an account in the node.', async () => {
const testAccount = caver.klay.accounts.create()
Expand Down