Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nonergodic committed Jan 5, 2025
1 parent 21c7327 commit 59b37a4
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/libraries/CoreBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
pragma solidity ^0.8.19;

import {IWormhole} from "wormhole-sdk/interfaces/IWormhole.sol";
import {BytesParsing} from "wormhole-sdk/libraries/BytesParsing.sol";
import {GuardianSignature, VaaBody, VaaLib} from "wormhole-sdk/libraries/VaaLib.sol";
import {minSigsForQuorum, eagerAnd, eagerOr} from "wormhole-sdk/Utils.sol";

library CoreBridgeLib {
using BytesParsing for bytes;
using VaaLib for bytes;

error VerificationFailed();
Expand All @@ -23,28 +25,47 @@ library CoreBridgeLib {
uint8 consistencyLevel,
bytes calldata payload
) {
IWormhole iwormhole = IWormhole(wormhole);
( uint32 guardianSetIndex,
GuardianSignature[] memory guardianSignatures,
uint envelopeOffset
) = encodedVaa.decodeVaaHeaderCdUnchecked();
//The following implementation looks better on the surface, but due to the extra external
// function calls and having to drag the large guardian set around, it is actually more
// expensive than using the suboptimal verifyVM call directly.
//
// IWormhole iwormhole = IWormhole(wormhole);
// ( uint32 guardianSetIndex,
// GuardianSignature[] memory guardianSignatures,
// uint envelopeOffset
// ) = encodedVaa.decodeVaaHeaderCdUnchecked();

IWormhole.Signature[] memory legacySigs = VaaLib.asIWormholeSignatures(guardianSignatures);
IWormhole.GuardianSet memory guardianSet = iwormhole.getGuardianSet(guardianSetIndex);
uint expirationTime = guardianSet.expirationTime;
if (eagerAnd(expirationTime != 0, expirationTime < block.timestamp))
//if the specified guardian set is expired, we try using the current guardian set as an adhoc
// repair attempt (there's almost certainly never more than 2 valid guardian sets at a time)
guardianSet = iwormhole.getGuardianSet(iwormhole.getCurrentGuardianSetIndex());
// IWormhole.Signature[] memory legacySigs = VaaLib.asIWormholeSignatures(guardianSignatures);
// IWormhole.GuardianSet memory guardianSet = iwormhole.getGuardianSet(guardianSetIndex);
// uint expirationTime = guardianSet.expirationTime;
// if (eagerAnd(expirationTime != 0, expirationTime < block.timestamp))
// //if the specified guardian set is expired, we try using the current guardian set as an adhoc
// // repair attempt (there's almost certainly never more than 2 valid guardian sets at a time)
// guardianSet = iwormhole.getGuardianSet(iwormhole.getCurrentGuardianSetIndex());

// bytes32 vaaHash = encodedVaa.calcVaaDoubleHashCd(envelopeOffset);
// //see https://github.com/wormhole-foundation/wormhole/blob/1dbe8459b96e182932d0dd5ae4b6bbce6f48cb09/ethereum/contracts/Messages.sol#L111
// (bool valid, ) = iwormhole.verifySignatures(vaaHash, legacySigs, guardianSet);

// if (eagerOr(legacySigs.length < minSigsForQuorum(guardianSet.keys.length), !valid))
// revert VerificationFailed();

// return encodedVaa.decodeVaaBodyCd(envelopeOffset);

bytes32 vaaHash = encodedVaa.calcVaaDoubleHashCd(envelopeOffset);
//see https://github.com/wormhole-foundation/wormhole/blob/1dbe8459b96e182932d0dd5ae4b6bbce6f48cb09/ethereum/contracts/Messages.sol#L111
(bool valid, ) = iwormhole.verifySignatures(vaaHash, legacySigs, guardianSet);
IWormhole.VM memory vm = encodedVaa.decodeVmStructCd();
(bool valid, ) = IWormhole(wormhole).verifyVM(vm);

if (eagerOr(legacySigs.length < minSigsForQuorum(guardianSet.keys.length), !valid))
if (!valid)
revert VerificationFailed();

return encodedVaa.decodeVaaBodyCd(envelopeOffset);
timestamp = vm.timestamp;
nonce = vm.nonce;
emitterChainId = vm.emitterChainId;
emitterAddress = vm.emitterAddress;
sequence = vm.sequence;
consistencyLevel = vm.consistencyLevel;
uint payloadOffset = encodedVaa.skipVaaHeaderCd() + VaaLib.ENVELOPE_SIZE;
(payload, ) = encodedVaa.sliceCdUnchecked(payloadOffset, encodedVaa.length - payloadOffset);
}

function verifyHashIsGuardianSigned(
Expand Down

0 comments on commit 59b37a4

Please sign in to comment.