-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathsendAndConfirmPostVaa.ts
185 lines (173 loc) · 4.71 KB
/
sendAndConfirmPostVaa.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import {
Commitment,
ConfirmOptions,
Connection,
Keypair,
PublicKeyInitData,
Transaction,
} from '@solana/web3.js';
import {
signSendAndConfirmTransaction,
SignTransaction,
sendAndConfirmTransactionsWithRetry,
modifySignTransaction,
TransactionSignatureAndResponse,
PreparedTransactions,
} from './utils';
import { addComputeBudget } from './computeBudget';
import {
createPostVaaInstruction,
createVerifySignaturesInstructions,
} from './wormhole';
import { isBytes, ParsedVaa, parseVaa, SignedVaa } from '../../../vaa/wormhole';
/**
* @category Solana
*/
export async function postVaaWithRetry(
connection: Connection,
signTransaction: SignTransaction,
wormholeProgramId: PublicKeyInitData,
payer: PublicKeyInitData,
vaa: Buffer,
maxRetries?: number,
commitment?: Commitment,
): Promise<TransactionSignatureAndResponse[]> {
const { unsignedTransactions, signers } =
await createPostSignedVaaTransactions(
connection,
wormholeProgramId,
payer,
vaa,
commitment,
);
for (const unsignedTransaction of unsignedTransactions) {
await addComputeBudget(connection, unsignedTransaction, []);
}
const postVaaTransaction = unsignedTransactions.pop()!;
const responses = await sendAndConfirmTransactionsWithRetry(
connection,
modifySignTransaction(signTransaction, ...signers),
payer.toString(),
unsignedTransactions,
maxRetries,
);
//While the signature_set is used to create the final instruction, it doesn't need to sign it.
responses.push(
...(await sendAndConfirmTransactionsWithRetry(
connection,
signTransaction,
payer.toString(),
[postVaaTransaction],
maxRetries,
)),
);
return responses;
}
/**
* @category Solana
*/
export async function postVaa(
connection: Connection,
signTransaction: SignTransaction,
wormholeProgramId: PublicKeyInitData,
payer: PublicKeyInitData,
vaa: Buffer,
options?: ConfirmOptions,
asyncVerifySignatures: boolean = true,
): Promise<TransactionSignatureAndResponse[]> {
const { unsignedTransactions, signers } =
await createPostSignedVaaTransactions(
connection,
wormholeProgramId,
payer,
vaa,
options?.commitment,
);
const postVaaTransaction = unsignedTransactions.pop()!;
const verifySignatures = async (transaction: Transaction) =>
signSendAndConfirmTransaction(
connection,
payer,
modifySignTransaction(signTransaction, ...signers),
transaction,
options,
);
const output: TransactionSignatureAndResponse[] = [];
if (asyncVerifySignatures) {
const verified = await Promise.all(
unsignedTransactions.map(async (transaction) =>
verifySignatures(transaction),
),
);
output.push(...verified);
} else {
for (const transaction of unsignedTransactions) {
output.push(await verifySignatures(transaction));
}
}
output.push(
await signSendAndConfirmTransaction(
connection,
payer,
signTransaction,
postVaaTransaction,
options,
),
);
return output;
}
/**
* @category Solana
*
* Send transactions for `verify_signatures` and `post_vaa` instructions.
*
* Using a signed VAA, execute transactions generated by {@link verifySignatures} and
* {@link postVaa}. At most 4 transactions are sent (up to 3 from signature verification
* and 1 to post VAA data to an account).
*
* @param {Connection} connection - Solana web3 connection
* @param {PublicKeyInitData} wormholeProgramId - wormhole program address
* @param {web3.Keypair} payer - transaction signer address
* @param {Buffer} signedVaa - bytes of signed VAA
* @param {Commitment} [options] - Solana commitment
*
*/
export async function createPostSignedVaaTransactions(
connection: Connection,
wormholeProgramId: PublicKeyInitData,
payer: PublicKeyInitData,
vaa: SignedVaa | ParsedVaa,
commitment?: Commitment,
): Promise<PreparedTransactions> {
const parsed = isBytes(vaa) ? parseVaa(vaa) : vaa;
const signatureSet = Keypair.generate();
const verifySignaturesInstructions = await createVerifySignaturesInstructions(
connection,
wormholeProgramId,
payer,
parsed,
signatureSet.publicKey,
commitment,
);
const unsignedTransactions: Transaction[] = [];
for (let i = 0; i < verifySignaturesInstructions.length; i += 2) {
unsignedTransactions.push(
new Transaction().add(...verifySignaturesInstructions.slice(i, i + 2)),
);
}
unsignedTransactions.push(
new Transaction().add(
createPostVaaInstruction(
connection,
wormholeProgramId,
payer,
parsed,
signatureSet.publicKey,
),
),
);
return {
unsignedTransactions,
signers: [signatureSet],
};
}