diff --git a/teal/wormhole/core.teal b/teal/wormhole/core.teal new file mode 100644 index 000000000..98de9fb3d --- /dev/null +++ b/teal/wormhole/core.teal @@ -0,0 +1,191 @@ +#pragma version 5 +// ------------------------------------------------------------------------------------------------ +// +// Core Wormhole TEAL Program +// +// (c) 2021 Randlabs, Inc. +// +// ------------------------------------------------------------------------------------------------ +// +// This program allows us to: +// - Verify Wormhole' signed VAAs +// - Acknowledge change in validator sets +// +// (!) We dont support submitting VAAs. +// +// VAA structure is defined in: +// https://github.com/certusone/wormhole/blob/dev.v2/design/0001_generic_message_passing.md +// +// Governance VAAs: +// https://github.com/certusone/wormhole/blob/dev.v2/design/0002_governance_messaging.md +// +// Ethereum Struct Reference: +// https://github.com/certusone/wormhole/blob/dev.v2/ethereum/contracts/Structs.sol +// +// For the acceptVAA application call, we accept the following packed structure: +// +// VAA +// i Bytes Field +// 0 1 Version +// 1 4 GuardianSetIndex +// 5 1 LenSignatures (LN) +// 6 66*LN Signatures where each S = { guardianIndex (1),r(32),s(32),v(1) } + +// -------------------------------------< hashed/signed body starts here. +// 4 timestamp +// 4 Nonce +// 2 emitterChainId +// 32 emitterAddress +// 8 sequence +// 1 consistencyLevel +// N payload +// --------------------------------------< hashed/signed body ends here. +// +// SLOTS: +// 0 Entire signed VAA +// 1 Parsed number of signatures. +// 2 Signature block +// 3 Size of all signatures (66 * N) +// 4 VAA Body hash + + +// Application creation. +int 0 +txn ApplicationID +== +bnz handle_create + +// Handle app call: validate signed VAAs +txn OnCompletion +int NoOp +== +bnz handle_call + +// Handle deletion. +txn OnCompletion +int DeleteApplication +== +bnz success + +// Fail otherwise +err + +handle_create: +// ----------------------------------------------------- +// Handle creation +// ----------------------------------------------------- + +b success + +// ----------------------------------------------------- +// Handle app call +// ----------------------------------------------------- + +handle_call: + +// (!) Group size must be Maxed out to raise computational allowance + +global GroupSize +int 16 +== +assert + +// if this is one of dummy transactions(0..14), exit with success + +txn GroupIndex +int 15 +!= +bnz success + +// Retrieve signed VAA, store in slot 0 + +txn ApplicationArgs 0 +store 0 + +// Check version + +load 0 +extract 0 1 +btoi +int 1 +== +assert + +// Store num of signatures in slot 1 + +load 0 +extract 5 1 +btoi +dup +store 1 + +// Store signatures in slot 2, size of signatures in 3. + +int 66 +* +dup +store 3 // size of signatures (66*N) +load 0 +swap +int 6 +extract3 // extract msg from 66 * sig_count from byte 6 +store 2 + +// Hash body +// Body starts at index 6 + (66*#Sig) + +load 1 +int 66 +* +int 6 ++ +load 0 +swap +int 0 // Stack has ...msgbyte, begin_of_body, 0 (extract all) +extract3 +keccak256 +store 4 + +// verify signature loop + +load 2 +int 0 // sig index +dup +int 66 +* +extract3 + + + +substr3 +edcsa_verify 0 + + + + +// ---------------------------------------------------------------------------- + +fail: +int 0 +return + +success: +int 1 +return + + + + + + + + + + + + + + + + +