Skip to content

Commit

Permalink
don't allow people to initialize the payer as a program account
Browse files Browse the repository at this point in the history
  • Loading branch information
henrye committed Nov 25, 2022
1 parent a73bd72 commit 7382038
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
3 changes: 3 additions & 0 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ pub enum ErrorCode {
/// 4100 - The declared program id does not match actual program id
#[msg("The declared program id does not match the actual program id")]
DeclaredProgramIdMismatch = 4100,
/// 4101 - You cannot/should not initialize the payer account as a program account
#[msg("You cannot/should not initialize the payer account as a program account")]
TryingToInitPayerAsProgramAccount = 4101,

// Deprecated
/// 5000 - The API being used is deprecated and should no longer be used
Expand Down
1 change: 1 addition & 0 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ pub fn generate_create_account(
let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts);
anchor_lang::system_program::create_account(cpi_context.with_signer(&[#seeds_with_nonce]), lamports, #space as u64, #owner)?;
} else {
require_keys_neq!(payer.key(), #field.key(), anchor_lang::error::ErrorCode::TryingToInitPayerAsProgramAccount);
// Fund the account for rent exemption.
let required_lamports = __anchor_rent
.minimum_balance(#space)
Expand Down
20 changes: 18 additions & 2 deletions tests/misc/tests/misc/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,25 @@ describe("misc", () => {
}),
],
});
});

const account = await program.account.dataI8.fetch(data.publicKey);
assert.strictEqual(account.data, 3);
it("Should fail when trying to init the payer as a program account", async () => {
try {
await program.rpc.testInit({
accounts: {
data: provider.wallet.publicKey,
payer: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
},
});
assert.fail("Transaction should fail");
} catch (e) {
// "Error Code: TryingToInitPayerAsProgramAccount. Error Number: 4101. Error Message: You cannot/should not initialize the payer account as a program account."
assert.strictEqual(e.error.errorCode.number, 4101);
}

// const account = await program.account.dataI8.fetch(data.publicKey);
// assert.strictEqual(account.data, 3);
});

it("Can init a random zero copy account", async () => {
Expand Down
51 changes: 28 additions & 23 deletions tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
bn.js "^5.1.2"
bs58 "^4.0.1"
buffer-layout "^1.2.2"
camelcase "^5.3.1"
camelcase "^6.3.0"
cross-fetch "^3.1.5"
crypto-hash "^1.3.0"
eventemitter3 "^4.0.7"
Expand Down Expand Up @@ -99,7 +99,7 @@
snake-case "^3.0.4"
toml "^3.0.0"

"@project-serum/borsh@^0.2.5":
"@project-serum/borsh@^0.2.2", "@project-serum/borsh@^0.2.5":
version "0.2.5"
resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.2.5.tgz#6059287aa624ecebbfc0edd35e4c28ff987d8663"
integrity sha512-UmeUkUoKdQ7rhx6Leve1SssMR/Ghv8qrEiyywyxSWg7ooV7StdpPBhciiy5eB3T0qU1BXvdRNC8TdrkxK7WC5Q==
Expand Down Expand Up @@ -175,27 +175,7 @@
buffer-layout "^1.2.0"
dotenv "10.0.0"

"@solana/web3.js@^1.21.0":
version "1.30.2"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.30.2.tgz#e85da75e0825dc64f53eb64a1ff0115b27bec135"
integrity sha512-hznCj+rkfvM5taRP3Z+l5lumB7IQnDrB4l55Wpsg4kDU9Zds8pE5YOH5Z9bbF/pUzZJKQjyBjnY/6kScBm3Ugg==
dependencies:
"@babel/runtime" "^7.12.5"
"@ethersproject/sha2" "^5.5.0"
"@solana/buffer-layout" "^3.0.0"
bn.js "^5.0.0"
borsh "^0.4.0"
bs58 "^4.0.1"
buffer "6.0.1"
cross-fetch "^3.1.4"
jayson "^3.4.4"
js-sha3 "^0.8.0"
rpc-websockets "^7.4.2"
secp256k1 "^4.0.2"
superstruct "^0.14.2"
tweetnacl "^1.0.0"

"@solana/web3.js@^1.32.0":
"@solana/web3.js@^1.17.0", "@solana/web3.js@^1.32.0":
version "1.66.2"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.66.2.tgz#80b43c5868b846124fe3ebac7d3943930c3fa60c"
integrity sha512-RyaHMR2jGmaesnYP045VLeBGfR/gAW3cvZHzMFGg7bkO+WOYOYp1nEllf0/la4U4qsYGKCsO9eEevR5fhHiVHg==
Expand All @@ -216,6 +196,26 @@
rpc-websockets "^7.5.0"
superstruct "^0.14.2"

"@solana/web3.js@^1.21.0":
version "1.30.2"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.30.2.tgz#e85da75e0825dc64f53eb64a1ff0115b27bec135"
integrity sha512-hznCj+rkfvM5taRP3Z+l5lumB7IQnDrB4l55Wpsg4kDU9Zds8pE5YOH5Z9bbF/pUzZJKQjyBjnY/6kScBm3Ugg==
dependencies:
"@babel/runtime" "^7.12.5"
"@ethersproject/sha2" "^5.5.0"
"@solana/buffer-layout" "^3.0.0"
bn.js "^5.0.0"
borsh "^0.4.0"
bs58 "^4.0.1"
buffer "6.0.1"
cross-fetch "^3.1.4"
jayson "^3.4.4"
js-sha3 "^0.8.0"
rpc-websockets "^7.4.2"
secp256k1 "^4.0.2"
superstruct "^0.14.2"
tweetnacl "^1.0.0"

"@solana/web3.js@^1.64.0":
version "1.64.0"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.64.0.tgz#b7f5a976976039a0161242e94d6e1224ab5d30f9"
Expand Down Expand Up @@ -522,6 +522,11 @@ camelcase@^6.0.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809"
integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==

camelcase@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a"
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==

chai@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.3.4.tgz#b55e655b31e1eac7099be4c08c21964fce2e6c49"
Expand Down

0 comments on commit 7382038

Please sign in to comment.