-
Notifications
You must be signed in to change notification settings - Fork 2
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
refactor: enhance PDA validation #40
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9555f86
add PDA address to README
brewmaster012 46cecb2
enhance pda validation
brewmaster012 f156723
Update programs/protocol-contracts-solana/src/lib.rs
brewmaster012 64e914f
Update README.md
brewmaster012 b0d3184
add pda_ata validation as constraints
brewmaster012 4f36439
Merge branch 'main' into enhance-pda-validation
brewmaster012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,7 +169,7 @@ describe("some tests", () => { | |
await gatewayProgram.methods.depositSplToken(new anchor.BN(1_000_000), Array.from(address)).accounts({ | ||
from: tokenAccount.address, | ||
to: pda_ata.address, | ||
}).rpc({commitment: 'confirmed'}); | ||
}).rpc({commitment: 'processed'}); | ||
acct = await spl.getAccount(conn, pda_ata.address); | ||
let bal1 = acct.amount; | ||
expect(bal1-bal0).to.be.eq(1_000_000n); | ||
|
@@ -298,7 +298,7 @@ describe("some tests", () => { | |
} catch (err) { | ||
expect(err).to.be.instanceof(anchor.AnchorError); | ||
console.log("Error message: ", err.message); | ||
expect(err.message).to.include("SPLAtaAndMintAddressMismatch"); | ||
expect(err.message).to.include("ConstraintTokenMint"); | ||
const account4 = await spl.getAccount(conn, pda_ata.address); | ||
console.log("After 2nd withdraw: Account balance:", account4.amount.toString()); | ||
expect(account4.amount).to.be.eq(2_500_000n); | ||
|
@@ -307,7 +307,7 @@ describe("some tests", () => { | |
}); | ||
|
||
it("deposit and withdraw 0.5 SOL from Gateway with ECDSA signature", async () => { | ||
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), Array.from(address)).accounts({pda: pdaAccount}).rpc(); | ||
await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), Array.from(address)).accounts({}).rpc(); | ||
let bal1 = await conn.getBalance(pdaAccount); | ||
console.log("pda account balance", bal1); | ||
expect(bal1).to.be.gte(1_000_000_000); | ||
|
@@ -341,7 +341,6 @@ describe("some tests", () => { | |
await gatewayProgram.methods.withdraw( | ||
amount, Array.from(signatureBuffer), Number(recoveryParam), Array.from(message_hash), nonce) | ||
.accounts({ | ||
pda: pdaAccount, | ||
to: to, | ||
}).rpc(); | ||
let bal2 = await conn.getBalance(pdaAccount); | ||
|
@@ -353,7 +352,7 @@ describe("some tests", () => { | |
|
||
it("deposit and call", async () => { | ||
let bal1 = await conn.getBalance(pdaAccount); | ||
const txsig = await gatewayProgram.methods.depositAndCall(new anchor.BN(1_000_000_000), Array.from(address), Buffer.from("hello", "utf-8")).accounts({pda: pdaAccount}).rpc({commitment: 'confirmed'}); | ||
const txsig = await gatewayProgram.methods.depositAndCall(new anchor.BN(1_000_000_000), Array.from(address), Buffer.from("hello", "utf-8")).accounts({}).rpc({commitment: 'confirmed'}); | ||
const tx = await conn.getParsedTransaction(txsig, 'confirmed'); | ||
console.log("deposit and call parsed tx", tx); | ||
let bal2 = await conn.getBalance(pdaAccount); | ||
|
@@ -375,7 +374,6 @@ describe("some tests", () => { | |
// only the authority stored in PDA can update the TSS address; the following should fail | ||
try { | ||
await gatewayProgram.methods.updateTss(Array.from(newTss)).accounts({ | ||
pda: pdaAccount, | ||
signer: mint.publicKey, | ||
}).signers([mint]).rpc(); | ||
} catch (err) { | ||
|
@@ -389,12 +387,12 @@ describe("some tests", () => { | |
randomFillSync(newTss); | ||
// console.log("generated new TSS address", newTss); | ||
await gatewayProgram.methods.setDepositPaused(true).accounts({ | ||
pda: pdaAccount, | ||
|
||
}).rpc(); | ||
|
||
// now try deposit, should fail | ||
try { | ||
await gatewayProgram.methods.depositAndCall(new anchor.BN(1_000_000), Array.from(address), Buffer.from('hi', 'utf-8')).accounts({pda: pdaAccount}).rpc(); | ||
await gatewayProgram.methods.depositAndCall(new anchor.BN(1_000_000), Array.from(address), Buffer.from('hi', 'utf-8')).accounts({}).rpc(); | ||
} catch (err) { | ||
console.log("Error message: ", err.message); | ||
expect(err).to.be.instanceof(anchor.AnchorError); | ||
|
@@ -405,15 +403,15 @@ describe("some tests", () => { | |
it("update authority", async () => { | ||
const newAuthority = anchor.web3.Keypair.generate(); | ||
await gatewayProgram.methods.updateAuthority(newAuthority.publicKey).accounts({ | ||
pda: pdaAccount, | ||
|
||
}).rpc(); | ||
// const pdaAccountData = await gatewayProgram.account.pda.fetch(pdaAccount); | ||
// expect(pdaAccountData.authority).to.be.eq(newAuthority.publicKey); | ||
|
||
// now the old authority cannot update TSS address and will fail | ||
try { | ||
await gatewayProgram.methods.updateTss(Array.from(new Uint8Array(20))).accounts({ | ||
pda: pdaAccount, | ||
|
||
}).rpc(); | ||
} catch (err) { | ||
console.log("Error message: ", err.message); | ||
|
@@ -422,6 +420,38 @@ describe("some tests", () => { | |
} | ||
}); | ||
|
||
it("create an account owned by the gateway program", async () => { | ||
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. nits: maybe lets remove console logs and comment if we need this test 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. this test case is removed in the subsequent PR #42 |
||
const gateway_id =gatewayProgram.programId; | ||
console.log("gateway program id", gateway_id.toString()); | ||
const fake_pda = anchor.web3.Keypair.generate(); | ||
const rentExemption = await conn.getMinimumBalanceForRentExemption(100); | ||
const instr1 = anchor.web3.SystemProgram.createAccount( | ||
{ | ||
fromPubkey: wallet.publicKey, | ||
newAccountPubkey: fake_pda.publicKey, | ||
lamports: rentExemption, | ||
space: 100, | ||
programId: gatewayProgram.programId, | ||
} | ||
) | ||
const tx = new anchor.web3.Transaction(); | ||
tx.add(instr1, ); | ||
await anchor.web3.sendAndConfirmTransaction(conn, tx, [wallet, fake_pda]); | ||
|
||
const newTss = new Uint8Array(20); | ||
randomFillSync(newTss); | ||
// console.log("generated new TSS address", newTss); | ||
try { | ||
await gatewayProgram.methods.updateTss(Array.from(newTss)).accounts({ | ||
pda: fake_pda.publicKey, | ||
}).rpc(); | ||
} catch (err) { | ||
console.log("Error message: ", err.message); | ||
expect(err).to.be.instanceof(anchor.AnchorError); | ||
expect(err.message).to.include("AccountDiscriminatorMismatch."); | ||
} | ||
}); | ||
|
||
|
||
}); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
just to double check if we can add some of these spl token constraints on these related accounts:
https://www.quicknode.com/guides/solana-development/anchor/how-to-use-constraints-in-anchor#spl-token-constraints
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.
b0d3184
added a few constraints. This seems to be redundant to the checks inside the withdrawSPLToken function:
as a result, there is no way to trigger the error
SPLAtaAndMintAddressMismatch
in the test.For now let's leave both validation there as they do not harm.
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.
yes i suspected we can achieve the same validation with anchor after checking link above
i am ok with leaving both validations, but also would be ok to remove ours if it cant be triggered anymore to keep programs simpler
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.
will keep both for now.