You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: llms.txt
+50-34
Original file line number
Diff line number
Diff line change
@@ -21920,7 +21920,7 @@ export const TXS = [
21920
21920
21921
21921
- **`RPC`** - endpoint for interacting with an Ethereum RPC node
21922
21922
- **`ETH_CORE`** - [Wormhole's Core Contract address on Ethereum](/docs/build/reference/contract-addresses/#core-contracts){target=\_blank} responsible for verifying VAAs
21923
-
- **`WORMHOLESCAN_API`** - base URL for querying the Wormholescan API to fetch VAA data and guardian sets
21923
+
- **`WORMHOLESCAN_API`** - base URL for querying the Wormholescan API to fetch VAA data and Guardian sets
21924
21924
- **`LOG_MESSAGE_PUBLISHED_TOPIC`** - the event signature hash for `LogMessagePublished`, a Wormhole contract event that signals when a VAA has been emitted. This is used to identify relevant logs in transaction receipts
21925
21925
- **`TXS`** - list of example transaction hashes that will be used for testing
21926
21926
@@ -21969,7 +21969,8 @@ export const TXS = [
21969
21969
21970
21970
## Create VAA Handling Functions
21971
21971
21972
-
In this section, we'll create a series of helper functions in the `src/helpers/vaaHelper.ts` file that will retrieve and verify VAAs and fetch and replace outdated Guardian signatures to generate a correctly signed VAA.
21972
+
In this section, we'll create a series of helper functions in the `src/helpers/vaaHelper.ts` file that will retrieve and verify VAAs and fetch and replace outdated Guardian signatures to generate a correctly signed VAA.
21973
+
21973
21974
To get started, import the necessary dependencies:
21974
21975
21975
21976
```typescript title="src/helpers/vaaHelper.ts"
@@ -22003,7 +22004,16 @@ chain/emitter/sequence
22003
22004
- `sequence` - a unique identifier for the event
22004
22005
22005
22006
We must assemble the ID correctly since this is the format the Wormholescan API expects when querying VAAs.
22006
-
Add the `fetchVaaId()` function to extract the VAA ID from a transaction hash. Query the Ethereum node for a transaction receipt, check for a Wormhole message, and construct the VAA ID in the format `chain/emitter/sequence`.
22007
+
22008
+
To retrieve a VAA, we need to extract its VAA ID from a transaction hash. Follow the below steps to process the transaction logs and construct the VAA ID:
22009
+
22010
+
1. **Get the transaction receipt** - iterate over the array of transaction hashes and fetch the receipt to access its logs
22011
+
22012
+
2. **Find the Wormhole event** - iterate over the transaction logs and check for events emitted by the Wormhole Core contract. Look specifically for `LogMessagePublished` events, which indicate a VAA was created
22013
+
22014
+
3. **Extract the emitter and sequence number** - if a matching event is found, extract the emitter address from `log.topics[1]` and remove the `0x` prefix. Then, the sequence number from `log.data` is extracted, converting it from hex to an integer
22015
+
22016
+
4. **Construct the VAA ID** - format the extracted data in `chain/emitter/sequence` format
- **`log.address === ETH_CORE`** - ensures the event was emitted by the Wormhole contract
22045
-
- **`log.topics?.[0] === LOG_MESSAGE_PUBLISHED_TOPIC`** - filters for `LogMessagePublished` events, which indicate a VAA was created
22046
-
- **`emitter`** - extracts the emitter address from `log.topics[1]`, removing the `0x` prefix
22047
-
- **`seq`** - extracts the sequence number by reading the first 66 characters of `log.data`, converting it from hex to an integer
22048
-
22049
22054
???- code "Try it out: VAA ID retrieval"
22050
22055
If you want to try out the function before moving forward, create a test file inside the `test` directory:
22051
22056
@@ -22103,7 +22108,7 @@ testFetchVaaId();
22103
22108
22104
22109
Now that you have the VAA ID, we can use it to fetch the full VAA payload from the Wormholescan API. This payload contains the VAA bytes, which will later be used for signature validation.
22105
22110
22106
-
Open `src/helpers/vaaHelper.ts` and create the `fetchVaa()` function to iterate through VAA IDs, query the API, and extract the `vaaBytes` payload. Log any errors and continue processing the remaining IDs to ensure all available VAAs are retrieved.
22111
+
Open `src/helpers/vaaHelper.ts` and create the `fetchVaa()` function to iterate through VAA IDsand extract the `vaaBytes` payload.
22107
22112
22108
22113
```typescript title="src/helpers/vaaHelper.ts"
22109
22114
vaaIds: string[]
@@ -22123,7 +22128,7 @@ vaaIds: string[]
22123
22128
}
22124
22129
```
22125
22130
22126
-
???- note "Try it out: VAA retrieval"
22131
+
???- code "Try it out: VAA retrieval"
22127
22132
If you want to try the function before moving forward, create a script inside the `test` directory
22128
22133
22129
22134
1. **Create the script file**
@@ -22186,9 +22191,17 @@ testFetchVaa();
22186
22191
22187
22192
### Validate VAA Signatures
22188
22193
22189
-
Now, we need to verify its validity. A VAA is only considered valid if it contains signatures from currently active guardians and is correctly verified by the Wormhole Core contract.
22194
+
Now, we need to verify its validity. A VAA is only considered valid if it contains signatures from currently active Guardians and is correctly verified by the Wormhole Core contract.
22195
+
22196
+
Open `src/helpers/vaaHelper.ts` and add the `checkVaaValidity()` function. This function verifies whether a VAA is valid by submitting it to an Ethereum RPC node and checking for outdated signatures.
22190
22197
22191
-
Open `src/helpers/vaaHelper.ts` and add the `checkVaaValidity()` function. Send the VAA to an Ethereum RPC node, call the `parseAndVerifyVM` function on the Wormhole Core contract, and determine whether the VAA is valid or contains outdated signatures that need replacing.
22198
+
Follow these steps to implement the function:
22199
+
22200
+
1. **Prepare the VAA for verification** - construct the VAA payload in a format that can be sent to the Wormhole Core contract
22201
+
22202
+
2. **Send an `eth_call` request** - submit the VAA to an Ethereum RPC node, calling the `parseAndVerifyVM` function on the Wormhole Core contract
22203
+
22204
+
3. **Decode the response** - check whether the VAA is valid. If it contains outdated signatures, further action will be required to replace them
22192
22205
22193
22206
```typescript title="src/helpers/vaaHelper.ts"
22194
22207
try {
@@ -22231,7 +22244,7 @@ try {
22231
22244
}
22232
22245
```
22233
22246
22234
-
???- note "Try it out: VAA Validity"
22247
+
???- code "Try it out: VAA Validity"
22235
22248
If you want to try the function before moving forward, create a script inside the `test` directory
22236
22249
22237
22250
1. **Create the script file**
@@ -22308,9 +22321,9 @@ testCheckVaaValidity();
22308
22321
22309
22322
### Fetch Observations (VAA Signatures)
22310
22323
22311
-
Before replacing outdated signatures, we need to fetch the original VAA signatures from Wormholescan. This allows us to compare them with the latest guardian set and determine which ones need updating.
22324
+
Before replacing outdated signatures, we need to fetch the original VAA signatures from Wormholescan. This allows us to compare them with the latest Guardian set and determine which ones need updating.
22312
22325
22313
-
Inside `src/helpers/vaaHelper.ts`, create the `fetchObservations()` function to query the Wormholescan API for observations related to a given VAA. Format the response by converting guardian addresses to lowercase for consistency, and return an empty array if an error occurs.
22326
+
Inside `src/helpers/vaaHelper.ts`, create the `fetchObservations()` function to query the Wormholescan API for observations related to a given VAA. Format the response by converting Guardian addresses to lowercase for consistency, and return an empty array if an error occurs.
22314
22327
22315
22328
```typescript title="src/helpers/vaaHelper.ts"
22316
22329
try {
@@ -22331,7 +22344,7 @@ try {
22331
22344
}
22332
22345
```
22333
22346
22334
-
???- note "Try it out: Fetch Observations"
22347
+
???- code "Try it out: Fetch Observations"
22335
22348
If you want to try the function before moving forward, create a script inside the `test` directory
22336
22349
22337
22350
1. **Create the script file**
@@ -22404,9 +22417,9 @@ testFetchObservations();
22404
22417
22405
22418
### Fetch the Latest Guardian Set
22406
22419
22407
-
Now that we have the original VAA signatures, we must fetch the latest guardian set from Wormholescan. This will allow us to compare the stored signatures with the current guardians and determine which signatures need replacing.
22420
+
Now that we have the original VAA signatures, we must fetch the latest Guardian set from Wormholescan. This will allow us to compare the stored signatures with the current Guardians and determine which signatures need replacing.
22408
22421
22409
-
Create the `fetchGuardianSet()` function inside `src/helpers/vaaHelper.ts` to fetch the latest guardian set.
22422
+
Create the `fetchGuardianSet()` function inside `src/helpers/vaaHelper.ts` to fetch the latest Guardian set.
22410
22423
22411
22424
```typescript title="src/helpers/vaaHelper.ts"
22412
22425
export async function fetchGuardianSet() {
@@ -22427,7 +22440,7 @@ export async function fetchGuardianSet() {
22427
22440
}
22428
22441
```
22429
22442
22430
-
???- note "Try it out: Fetch Guardian Set"
22443
+
???- code "Try it out: Fetch Guardian Set"
22431
22444
If you want to try the function before moving forward, create a script inside the `test` directory
If an error occurs while fetching the guardian set, a `500` status error will be logged.
22490
+
If an error occurs while fetching the Guardian set, a `500` status error will be logged.
22478
22491
22479
22492
### Replace Outdated Signatures
22480
22493
22481
-
With the full VAA, guardian signatures, and the latest guardian set, we can now update outdated signatures while maintaining the required signature count.
22494
+
With the full VAA, Guardian signatures, and the latest Guardian set, we can now update outdated signatures while maintaining the required signature count.
22482
22495
22483
-
1. **Create the `replaceSignatures()` function** - open `src/helpers/vaaHelper.ts` and add the function header
22496
+
1. **Create the `replaceSignatures()` function** - open `src/helpers/vaaHelper.ts` and add the function header. To catch and handle errors properly, all logic will be wrapped inside a `try` block.
- **`observations`** - observed signatures from the network
22496
-
- **`currentGuardians`** - latest guardian set
22497
-
- **`guardianSetIndex`** - current guardian set index
22509
+
- **`currentGuardians`** - latest Guardian set
22510
+
- **`guardianSetIndex`** - current Guardian set index
22498
22511
22499
-
2. **Validate input data** - ensure all required parameters are present before proceeding. If any required input is missing, the function throws an error to prevent execution with incomplete data. The guardian set should never be empty; if it is, this likely indicates an error in fetching the guardian set in a previous step
22512
+
2. **Validate input data** - ensure all required parameters are present before proceeding. If any required input is missing, the function throws an error to prevent execution with incomplete data. The Guardian set should never be empty; if it is, this likely indicates an error in fetching the Guardian set in a previous step
22500
22513
22501
22514
```typescript
22502
-
if (!vaa) throw new Error('VAA is undefined or empty.');
22503
22515
if (currentGuardians.length === 0)
22504
22516
throw new Error('Guardian set is empty.');
22505
22517
if (observations.length === 0) throw new Error('No observations provided.');
22506
22518
```
22507
22519
22508
-
3. **Filter valid signatures** - remove signatures from inactive guardians, keeping only valid ones. If there aren't enough valid signatures to replace the outdated ones, execution is halted to prevent an incomplete or invalid VAA
22520
+
3. **Filter valid signatures** - remove signatures from inactive Guardians, keeping only valid ones. If there aren't enough valid signatures to replace the outdated ones, execution is halted to prevent an incomplete or invalid VAA
22509
22521
22510
22522
```typescript
22511
22523
currentGuardians.includes(sig.guardianAddr)
@@ -22560,7 +22572,7 @@ With the full VAA, guardian signatures, and the latest guardian set, we can now
22560
22572
}
22561
22573
```
22562
22574
22563
-
6. **Identify outdated signatures** - compare the current VAA signatures with the newly formatted ones to detect which signatures belong to outdated guardians. Remove these outdated signatures to ensure only valid ones remain
22575
+
6. **Identify outdated signatures** - compare the current VAA signatures with the newly formatted ones to detect which signatures belong to outdated Guardians. Remove these outdated signatures to ensure only valid ones remain
22564
22576
22565
22577
```typescript
22566
22578
.filter(
@@ -22617,7 +22629,7 @@ With the full VAA, guardian signatures, and the latest guardian set, we can now
22617
22629
}
22618
22630
```
22619
22631
22620
-
9. **Send the updated VAA for verification and handle errors** - submit the updated VAA to an Ethereum RPC node for validation, ensuring it can be proposed for guardian approval. If an error occurs during submission or signature replacement, log the issue and prevent further execution
22632
+
9. **Send the updated VAA for verification and handle errors** - submit the updated VAA to an Ethereum RPC node for validation, ensuring it can be proposed for Guardian approval. If an error occurs during submission or signature replacement, log the issue and prevent further execution
22621
22633
22622
22634
```typescript
22623
22635
if (!(patchedVaa instanceof Uint8Array))
@@ -22650,12 +22662,11 @@ With the full VAA, guardian signatures, and the latest guardian set, we can now
22650
22662
} catch (error) {
22651
22663
console.error('Unexpected error in replaceSignatures:', error);
22652
22664
}
22653
-
}
22654
22665
```
22655
22666
22656
22667
## Create Script to Replace Outdated VAA Signatures
22657
22668
22658
-
Now that we have all the necessary helper functions, we will create a script to automate replacing outdated VAA signatures. This script will retrieve a transaction’s VAA sequentially, check its validity, fetch the latest guardian set, and update its signatures. By the end, it will output a correctly signed VAA that can be proposed for guardian approval.
22669
+
Now that we have all the necessary helper functions, we will create a script to automate replacing outdated VAA signatures. This script will retrieve a transaction’s VAA sequentially, check its validity, fetch the latest Guardian set, and update its signatures. By the end, it will output a correctly signed VAA that can be proposed for Guardian approval.
22659
22670
22660
22671
1. **Open the file** - inside `src/scripts/replaceSignatures.ts`, import the required helper functions needed to process the VAAs
22661
22672
@@ -22723,6 +22734,10 @@ import { TXS } from '../config/constants';
@@ -22741,14 +22756,15 @@ The script logs each step, skipping valid VAAs, replacing outdated signatures fo
22741
22756
22742
22757
## Resources
22743
22758
22744
-
You can explore the complete project and find all necessary scripts and configurations in Wormhole's [demo GitHub repository](https://github.com/wormhole-foundation/demo-vaa-signature-replacement){target=_blank}.
22759
+
You can explore the complete project and find all necessary scripts and configurations in Wormhole's [demo GitHub repository](https://github.com/wormhole-foundation/demo-vaa-signature-replacement){target=\_blank}.
22760
+
22745
22761
The demo repository includes a bonus script to check the VAA redemption status on Ethereum and Solana, allowing you to verify whether a transaction has already been redeemed on the destination chain.
22746
22762
22747
22763
## Conclusion
22748
22764
22749
22765
You've successfully built a script to fetch, validate, and replace outdated signatures in VAAs using Wormholescan and the Wormhole SDK.
22750
22766
22751
-
It's important to note that this tutorial does not update VAAs in the Wormhole network. Before redeeming the VAA, you must propose it for guardian approval to finalize the process.
22767
+
It's important to note that this tutorial does not update VAAs in the Wormhole network. Before redeeming the VAA, you must propose it for Guardian approval to finalize the process.
0 commit comments