feat: allow external signatures on `Transactions`
Adds a helper for adding externally created signature/pubkey pairs to `Transactions`
This commit is contained in:
parent
7d3781e19f
commit
8142aa6c1d
|
@ -406,6 +406,7 @@ declare module '@solana/web3.js' {
|
|||
signData: Buffer;
|
||||
sign(...signers: Array<Account>): void;
|
||||
signPartial(...partialSigners: Array<PublicKey | Account>): void;
|
||||
addSignature(pubkey: PublicKey, signature: Buffer): void;
|
||||
addSigner(signer: Account): void;
|
||||
verifySignatures(): boolean;
|
||||
serialize(): Buffer;
|
||||
|
|
|
@ -415,6 +415,7 @@ declare module '@solana/web3.js' {
|
|||
sign(...signers: Array<Account>): void;
|
||||
signPartial(...partialSigners: Array<PublicKey | Account>): void;
|
||||
addSigner(signer: Account): void;
|
||||
addSignature(pubkey: PublicKey, signature: Buffer): void;
|
||||
verifySignatures(): boolean;
|
||||
serialize(): Buffer;
|
||||
}
|
||||
|
|
|
@ -398,16 +398,24 @@ export class Transaction {
|
|||
* `signPartial`
|
||||
*/
|
||||
addSigner(signer: Account) {
|
||||
const index = this.signatures.findIndex(sigpair =>
|
||||
signer.publicKey.equals(sigpair.publicKey),
|
||||
);
|
||||
if (index < 0) {
|
||||
throw new Error(`Unknown signer: ${signer.publicKey.toString()}`);
|
||||
}
|
||||
|
||||
const signData = this.signData;
|
||||
const signature = nacl.sign.detached(signData, signer.secretKey);
|
||||
this.addSignature(signer.publicKey, signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an externally created signature to a transaction
|
||||
*/
|
||||
addSignature(pubkey: PublicKey, signature: Buffer) {
|
||||
invariant(signature.length === 64);
|
||||
|
||||
const index = this.signatures.findIndex(sigpair =>
|
||||
pubkey.equals(sigpair.publicKey),
|
||||
);
|
||||
if (index < 0) {
|
||||
throw new Error(`Unknown signer: ${pubkey.toString()}`);
|
||||
}
|
||||
|
||||
this.signatures[index].signature = Buffer.from(signature);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue