-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Unit test] role based key fee payer #59
base: dev
Are you sure you want to change the base?
Changes from 3 commits
b1c64ab
22fb77f
9052180
220b539
fcfd08c
5af8b52
5240378
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { Web3, TxType, AccountKeyType, getPublicKeyFromPrivate, toPeb } from "@kaiachain/web3js-ext"; | ||
import { assert } from "chai"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io"); | ||
const web3 = new Web3(provider); | ||
|
||
type Account = { | ||
address: string; | ||
privateKey: string; | ||
signTransaction: (tx: any) => Promise<any>; | ||
} | ||
|
||
// Feedback1. Generate Temporary Key. | ||
function generateTemporaryAccount(): Account { | ||
return web3.eth.accounts.create(); | ||
} | ||
|
||
describe("Role-based Key Tests", function () { | ||
this.timeout(10000); | ||
|
||
let roleTransactionAccount: Account; | ||
let roleAccountUpdate: Account; | ||
let roleFeePayerAccount: Account; | ||
|
||
// Before all tests, set up Role-based Key | ||
before(async function () { | ||
console.log("\n--- Generating Temporary Accounts ---"); | ||
roleTransactionAccount = generateTemporaryAccount(); | ||
roleAccountUpdate = generateTemporaryAccount(); | ||
roleFeePayerAccount = generateTemporaryAccount(); | ||
|
||
const pub1 = getPublicKeyFromPrivate(roleTransactionAccount.privateKey); | ||
const pub2 = getPublicKeyFromPrivate(roleAccountUpdate.privateKey); | ||
const pub3 = getPublicKeyFromPrivate(roleFeePayerAccount.privateKey); | ||
kjeom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
console.log("Generated Public keys:", { pub1, pub2, pub3 }); | ||
kjeom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const updateTx = { | ||
type: TxType.AccountUpdate, | ||
from: roleAccountUpdate.address, | ||
gasLimit: 100000, | ||
key: { | ||
type: AccountKeyType.RoleBased, | ||
keys: [ | ||
{ type: AccountKeyType.Public, key: pub1 }, | ||
{ type: AccountKeyType.Public, key: pub2 }, | ||
{ type: AccountKeyType.Public, key: pub3 } | ||
] | ||
} | ||
}; | ||
|
||
const signedUpdateTx = await roleAccountUpdate.signTransaction(updateTx); | ||
kjeom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log("Account Updated:", signedUpdateTx); | ||
assert.isNotNull(signedUpdateTx.transactionHash, "Account update transaction should succeed"); | ||
}); | ||
|
||
// Test Case 1: Sending a normal transaction with RoleTransaction key | ||
it("1. Sending a normal transaction with RoleTransaction key", async function () { | ||
const valueTx = { | ||
type: TxType.ValueTransfer, | ||
from: generateTemporaryAccount().address, | ||
to: generateTemporaryAccount().address, | ||
value: toPeb("0.01"), | ||
gasLimit: 100000 | ||
}; | ||
|
||
const signedTx = await roleTransactionAccount.signTransaction(valueTx); | ||
console.log("RoleTransaction signedTx:", signedTx.transactionHash); | ||
assert.isNotNull(signedTx.transactionHash, "RoleTransaction transaction should succeed"); | ||
}); | ||
|
||
// Test Case 2: Attempting to sign a regular transaction with RoleFeePayer key (should fail) | ||
it("2. Attempting to sign a regular transaction with RoleFeePayer key (failure test)", async function () { | ||
const valueTx = { | ||
type: TxType.ValueTransfer, | ||
from: generateTemporaryAccount().address, | ||
to: generateTemporaryAccount().address, | ||
value: toPeb("0.01"), | ||
gasLimit: 100000 | ||
}; | ||
|
||
try { | ||
const signedTx = await roleFeePayerAccount.signTransaction(valueTx); | ||
kjeom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log("Unexpected Success - Signed Transaction:", signedTx); | ||
assert.fail("RoleFeePayer key should not sign a ValueTransfer transaction."); | ||
} catch (error: any) { | ||
console.log("Expected Error (RoleFeePayer):", error.message); | ||
assert.isTrue(true, "Error occurred as expected due to role mismatch"); | ||
} | ||
}); | ||
|
||
// Test Case 3: Fee Delegated transaction signed by RoleFeePayer key (should succeed) | ||
it("3. Fee Delegated transaction signed by RoleTransaction and RoleFeePayer keys", async function () { | ||
const feeDelegatedTx = { | ||
type: TxType.FeeDelegatedValueTransfer, | ||
from: generateTemporaryAccount().address, | ||
to: generateTemporaryAccount().address, | ||
value: toPeb("0.01"), | ||
gasLimit: 100000 | ||
}; | ||
|
||
// Feedback2. Remove the dependencies with network connection. just check the signed result. | ||
// Feedback3. signed by a user (from address,roleTransactionAccount) first | ||
const signedTxBySender = await roleTransactionAccount.signTransaction(feeDelegatedTx); | ||
assert.isNotNull(signedTxBySender.rawTransaction, "Transaction should be signed by RoleTransaction"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Feedback 2] don't send the tx to the testnet, just check the signed result. |
||
|
||
console.log("Sender signed transaction:", signedTxBySender.rawTransaction); | ||
|
||
// Feedback3. FeePayer(FeePayerAccount) sign the tx after the user's sign. | ||
try { | ||
const signedTxByFeePayer = await roleFeePayerAccount.signTransaction({ | ||
senderRawTransaction: signedTxBySender.rawTransaction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Feedback 3] It should be signed by a user (from address) first. FeePayer normally sign the tx after the user's sign. Actually, I have two questions about this Feedback3.
cc. @kjeom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @minminkikiki "A user" means another account that is used by user, not feePayer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @minminkikiki If you got the new error regarding the sign of feepayer, please add the description of the error in the PR description. |
||
}); | ||
assert.isNotNull(signedTxByFeePayer.rawTransaction, "FeePayer should sign the transaction"); | ||
console.log("Signed Fee Delegated Transaction:", signedTxByFeePayer.rawTransaction); | ||
} catch (error: any) { | ||
console.log("Expected Error (FeePayer signing):", error.message); | ||
assert.fail("FeePayer failed to sign the already signed transaction."); | ||
} | ||
}); | ||
|
||
// Test Case 4: Attempting to sign Fee Delegated transaction with RoleTransaction key (should fail) | ||
it("4. Attempting to sign Fee Delegated transaction with RoleTransaction key (should fail)", async function () { | ||
const feeDelegatedTx = { | ||
type: TxType.FeeDelegatedValueTransfer, | ||
from: generateTemporaryAccount().address, | ||
to: generateTemporaryAccount().address, | ||
value: toPeb("0.01"), | ||
gasLimit: 100000 | ||
}; | ||
|
||
try { | ||
const signedTx = await roleTransactionAccount.signTransaction(feeDelegatedTx); | ||
console.log("Unexpected Success - Transaction Hash:", signedTx.transactionHash); | ||
assert.fail("RoleTransaction key should not sign Fee Delegated transactions"); | ||
} catch (error: any) { | ||
console.log("Expected Error (RoleTransaction as FeePayer):", error.message); | ||
assert.isTrue(true, "Error occurred as expected"); | ||
} | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Feedback 1] fixed this test to generate temporary key and update the account key to role based.
cc. @kjeom