Skip to content
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

web3.js: Implement addSignature in VersionedTransaction #27945

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions web3.js/src/transaction/versioned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {SIGNATURE_LENGTH_IN_BYTES} from './constants';
import * as shortvec from '../utils/shortvec-encoding';
import * as Layout from '../layout';
import {sign} from '../utils/ed25519';
import {PublicKey} from '../publickey';

export type TransactionVersion = 'legacy' | 0;

Expand Down Expand Up @@ -106,4 +107,20 @@ export class VersionedTransaction {
this.signatures[signerIndex] = sign(messageData, signer.secretKey);
}
}

addSignature(publicKey: PublicKey, signature: Uint8Array) {
assert(signature.byteLength === 64, 'Signature must be 64 bytes long');
const signerPubkeys = this.message.staticAccountKeys.slice(
vsakos marked this conversation as resolved.
Show resolved Hide resolved
0,
this.message.header.numRequiredSignatures,
);
const signerIndex = signerPubkeys.findIndex(pubkey =>
pubkey.equals(publicKey),
);
assert(
signerIndex >= 0,
`Can not add signature; \`${publicKey.toBase58()}\` is not required to sign this transaction`,
);
this.signatures[signerIndex] = signature;
}
}
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);
vsakos marked this conversation as resolved.
Show resolved Hide resolved
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`,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure want these to be separate tests, so that when they fail, the reader doesn't have to dig to find out what functionality was broken.

describe('addSignature', () => {
  it('appends an externally generated signature at the correct index', () => { /* ... */ })
  it('fatals when the signature is the wrong length', () => { /* ... */ })
  it('fatals when adding a signature for a public key that has not been marked as a signer', () => { /* ... */ })
});

});
});