From d866f742e211d4938eae0313bee5c139d3f58d49 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Thu, 25 Feb 2021 23:47:22 -0700 Subject: [PATCH] fix: createAccountWithSeed account handling (#15482) --- web3.js/src/system-program.js | 12 ++++++---- web3.js/test/system-program.test.js | 36 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/web3.js/src/system-program.js b/web3.js/src/system-program.js index b0515320bb..1ba4daf2f5 100644 --- a/web3.js/src/system-program.js +++ b/web3.js/src/system-program.js @@ -745,12 +745,16 @@ export class SystemProgram { space: params.space, programId: params.programId.toBuffer(), }); + let keys = [ + {pubkey: params.fromPubkey, isSigner: true, isWritable: true}, + {pubkey: params.newAccountPubkey, isSigner: false, isWritable: true}, + ]; + if (params.basePubkey != params.fromPubkey) { + keys.push({pubkey: params.basePubkey, isSigner: true, isWritable: false}); + } return new TransactionInstruction({ - keys: [ - {pubkey: params.fromPubkey, isSigner: true, isWritable: true}, - {pubkey: params.newAccountPubkey, isSigner: false, isWritable: true}, - ], + keys, programId: this.programId, data, }); diff --git a/web3.js/test/system-program.test.js b/web3.js/test/system-program.test.js index abe803db58..ae3a16eca9 100644 --- a/web3.js/test/system-program.test.js +++ b/web3.js/test/system-program.test.js @@ -475,6 +475,42 @@ describe('SystemProgram', () => { ); expect(createAccountWithSeedBalance).to.eq(minimumAmount); + // Test CreateAccountWithSeed where fromPubkey != basePubkey + const uniqueFromAccount = new Account(); + const newBaseAccount = new Account(); + const createAccountWithSeedAddress2 = await PublicKey.createWithSeed( + newBaseAccount.publicKey, + seed, + programId, + ); + await helpers.airdrop({ + connection, + address: uniqueFromAccount.publicKey, + amount: 2 * LAMPORTS_PER_SOL, + }); + const createAccountWithSeedParams2 = { + fromPubkey: uniqueFromAccount.publicKey, + newAccountPubkey: createAccountWithSeedAddress2, + basePubkey: newBaseAccount.publicKey, + seed, + lamports: minimumAmount, + space, + programId, + }; + const createAccountWithSeedTransaction2 = new Transaction().add( + SystemProgram.createAccountWithSeed(createAccountWithSeedParams2), + ); + await sendAndConfirmTransaction( + connection, + createAccountWithSeedTransaction2, + [uniqueFromAccount, newBaseAccount], + {preflightCommitment: 'confirmed'}, + ); + const createAccountWithSeedBalance2 = await connection.getBalance( + createAccountWithSeedAddress2, + ); + expect(createAccountWithSeedBalance2).to.eq(minimumAmount); + // Transfer to a derived address to prep for TransferWithSeed const programId2 = new Account().publicKey; const transferWithSeedAddress = await PublicKey.createWithSeed(