Skip to content

Commit

Permalink
VersionedTransaction.addSignature test
Browse files Browse the repository at this point in the history
  • Loading branch information
vsakos committed Sep 20, 2022
1 parent 9669533 commit c413b3b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions web3.js/test/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {PublicKey} from '../src/publickey';
import {
Transaction,
TransactionInstruction,
TransactionMessage,
VersionedTransaction,
} from '../src/transaction';
import {StakeProgram, SystemProgram} from '../src/programs';
Expand Down Expand Up @@ -962,4 +963,48 @@ describe('Transaction', () => {
t1.partialSign(signer);
t1.serialize();
});

it('externally signed versioned transaction', () => {
const signer = Keypair.generate();
const invalidSigner = Keypair.generate();

const recentBlockhash = new PublicKey(3).toBuffer();

const message = new TransactionMessage({
payerKey: signer.publicKey,
instructions: [
new TransactionInstruction({
data: Buffer.from('Hello!'),
keys: [
{
pubkey: signer.publicKey,
isSigner: true,
isWritable: true,
},
],
programId: new PublicKey(
'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr',
),
}),
],
recentBlockhash: bs58.encode(recentBlockhash),
});

const transaction = new VersionedTransaction(message.compileToV0Message());

const signature = sign(transaction.message.serialize(), signer.secretKey);

transaction.addSignature(signer.publicKey, signature);

expect(transaction.signatures).to.have.length(1);
expect(transaction.signatures[0]).to.eq(signature);
expect(() => {
transaction.addSignature(signer.publicKey, new Uint8Array(32));
}).to.throw('Signature must be 64 bytes long');
expect(() => {
transaction.addSignature(invalidSigner.publicKey, new Uint8Array(64));
}).to.throw(
`Can not add signature; \`${invalidSigner.publicKey.toBase58()}\` is not required to sign this transaction`,
);
});
});

0 comments on commit c413b3b

Please sign in to comment.