-
Notifications
You must be signed in to change notification settings - Fork 300
feat(lazer/sui): verify signatures against trusted signers #2982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
2c4ae46
feat(lazer/sui): specify version
tejasbadadare 69682bc
feat(lazer-sui): add state module with trusted signer storage and tests
devin-ai-integration[bot] 21d8937
Merge branch 'tb/lazer/sui-storage' of github.com:pyth-network/pyth-c…
tejasbadadare 7380293
fix
devin-ai-integration[bot] 0e70796
Merge branch 'tb/lazer/sui-storage' of github.com:pyth-network/pyth-c…
tejasbadadare f34b7fc
fix(sui-lazer): finalize state module with trusted signer management …
devin-ai-integration[bot] 112cd39
Merge branch 'tb/lazer/sui-storage' of github.com:pyth-network/pyth-c…
tejasbadadare d7bd862
chore(sui-lazer): remove warnings in state module; clean build on Sui…
devin-ai-integration[bot] 48e070d
feat(lazer/sui): add trusted signer state
tejasbadadare 8ee64ba
feat(lazer/sui): add package init and AdminCapability; share State an…
devin-ai-integration[bot] a7436fa
comments
tejasbadadare 88ef465
Merge branch 'tb/lazer/sui-storage' of github.com:pyth-network/pyth-c…
tejasbadadare e64eb21
resolve warnings
tejasbadadare 187f0e9
feat(sui-lazer): add admin-gated entry to update trusted signer via A…
devin-ai-integration[bot] ddad6d6
Revert "feat(sui-lazer): add admin-gated entry to update trusted sign…
devin-ai-integration[bot] 0fc417e
naming, make update_trusted_signer public
tejasbadadare da1a2a1
feat(sui-lazer): use OTW for AdminCap in admin::init; add OTW to pyth…
devin-ai-integration[bot] 389f5d7
lint
tejasbadadare f1b6fc4
doc
tejasbadadare 96eaa10
feat(lazer/sui): verify against trusted signers
tejasbadadare f53e83f
docs, clean up
tejasbadadare b9a844d
remove unneeded otw type check
tejasbadadare 9c92f6e
Merge branch 'tb/lazer/sui-storage' of github.com:pyth-network/pyth-c…
tejasbadadare 41c66dc
lint
tejasbadadare 7f10b9d
Merge branch 'tb/lazer/sui-storage' of github.com:pyth-network/pyth-c…
tejasbadadare d8028d1
max verify_le_ecdsa_message package private
tejasbadadare 54ad564
Merge branch 'main' of github.com:pyth-network/pyth-crosschain into t…
tejasbadadare 049363c
fix imports
tejasbadadare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module pyth_lazer::admin; | ||
|
||
public struct AdminCap has key, store { | ||
id: UID, | ||
} | ||
|
||
/// The `ADMIN` resource serves as the one-time witness. | ||
/// It has the `drop` ability, allowing it to be consumed immediately after use. | ||
/// See: https://move-book.com/programmability/one-time-witness | ||
public struct ADMIN has drop {} | ||
|
||
|
||
/// Initializes the module. Called at publish time. | ||
/// Creates and transfers ownership of the singular AdminCap capability to the deployer. | ||
/// Only the AdminCap owner can update the trusted signers. | ||
fun init(_: ADMIN, ctx: &mut TxContext) { | ||
let cap = AdminCap { id: object::new(ctx) }; | ||
transfer::public_transfer(cap, tx_context::sender(ctx)); | ||
} | ||
|
||
#[test_only] | ||
public fun mint_for_test(ctx: &mut TxContext): AdminCap { | ||
AdminCap { id: object::new(ctx) } | ||
} | ||
|
||
#[test_only] | ||
public fun destroy_for_test(cap: AdminCap) { | ||
let AdminCap { id } = cap; | ||
object::delete(id) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,96 @@ | ||
module pyth_lazer::pyth_lazer; | ||
|
||
use pyth_lazer::i16::Self; | ||
use pyth_lazer::i64::Self; | ||
use pyth_lazer::update::{Self, Update}; | ||
use pyth_lazer::channel; | ||
use pyth_lazer::feed::{Self, Feed}; | ||
use pyth_lazer::channel::Self; | ||
use pyth_lazer::i16; | ||
use pyth_lazer::i64; | ||
use pyth_lazer::state::{Self, State}; | ||
use pyth_lazer::update::{Self, Update}; | ||
use sui::bcs; | ||
use sui::ecdsa_k1::secp256k1_ecrecover; | ||
use sui::clock::Clock; | ||
|
||
const SECP256K1_SIG_LEN: u32 = 65; | ||
const UPDATE_MESSAGE_MAGIC: u32 = 1296547300; | ||
const PAYLOAD_MAGIC: u32 = 2479346549; | ||
|
||
// Error codes | ||
const EInvalidUpdate: u64 = 1; | ||
const ESignerNotTrusted: u64 = 2; | ||
const ESignerExpired: u64 = 3; | ||
|
||
// TODO: | ||
// initializer | ||
// administration -> admin cap, upgrade cap, governance? | ||
// storage module -> trusted signers, update fee?, treasury? | ||
// error handling | ||
// standalone verify signature function | ||
|
||
/// Parse the Lazer update message and validate the signature. | ||
/// The `PYTH_LAZER` resource serves as the one-time witness. | ||
/// It has the `drop` ability, allowing it to be consumed immediately after use. | ||
/// See: https://move-book.com/programmability/one-time-witness | ||
public struct PYTH_LAZER has drop {} | ||
|
||
/// Initializes the module. Called at publish time. | ||
/// Creates and shares the singular State object. | ||
/// AdminCap is created and transferred in admin::init via a One-Time Witness. | ||
fun init(_: PYTH_LAZER, ctx: &mut TxContext) { | ||
let s = state::new(ctx); | ||
transfer::public_share_object(s); | ||
} | ||
|
||
/// Verify LE ECDSA message signature against trusted signers. | ||
/// | ||
/// This function recovers the public key from the signature and payload, | ||
/// then checks if the recovered public key is in the trusted signers list | ||
/// and has not expired. | ||
/// | ||
/// # Arguments | ||
/// * `s` - The pyth_lazer::state::State | ||
/// * `clock` - The sui::clock::Clock | ||
/// * `signature` - The ECDSA signature bytes (little endian) | ||
/// * `payload` - The message payload that was signed | ||
/// | ||
/// # Errors | ||
/// * `ESignerNotTrusted` - The recovered public key is not in the trusted signers list | ||
/// * `ESignerExpired` - The signer's certificate has expired | ||
public fun verify_le_ecdsa_message( | ||
s: &State, | ||
clock: &Clock, | ||
signature: &vector<u8>, | ||
payload: &vector<u8>, | ||
) { | ||
// 0 stands for keccak256 hash | ||
let pubkey = secp256k1_ecrecover(signature, payload, 0); | ||
|
||
// Check if the recovered pubkey is in the trusted signers list | ||
let trusted_signers = state::get_trusted_signers(s); | ||
let mut maybe_idx = state::find_signer_index(trusted_signers, &pubkey); | ||
|
||
if (option::is_some(&maybe_idx)) { | ||
let idx = option::extract(&mut maybe_idx); | ||
let found_signer = &trusted_signers[idx]; | ||
let expires_at = state::expires_at(found_signer); | ||
assert!(clock.timestamp_ms() < expires_at, ESignerExpired); | ||
} else { | ||
abort ESignerNotTrusted | ||
} | ||
} | ||
|
||
/// Parse the Lazer update message and validate the signature within. | ||
/// The parsing logic is based on the Lazer rust protocol definition defined here: | ||
/// https://github.com/pyth-network/pyth-crosschain/tree/main/lazer/sdk/rust/protocol | ||
public fun parse_and_verify_le_ecdsa_update(update: vector<u8>): Update { | ||
/// | ||
/// # Arguments | ||
/// * `s` - The pyth_lazer::state::State | ||
/// * `clock` - The sui::clock::Clock | ||
/// * `update` - The LeEcdsa formatted Lazer update | ||
/// | ||
/// # Errors | ||
/// * `EInvalidUpdate` - Failed to parse the update according to the protocol definition | ||
/// * `ESignerNotTrusted` - The recovered public key is not in the trusted signers list | ||
public fun parse_and_verify_le_ecdsa_update(s: &State, clock: &Clock, update: vector<u8>): Update { | ||
let mut cursor = bcs::new(update); | ||
|
||
// TODO: introduce helper functions to check data len before peeling. allows us to return more | ||
// granular error messages. | ||
|
||
let magic = cursor.peel_u32(); | ||
assert!(magic == UPDATE_MESSAGE_MAGIC, 0); | ||
|
||
|
@@ -44,18 +108,15 @@ public fun parse_and_verify_le_ecdsa_update(update: vector<u8>): Update { | |
|
||
assert!((payload_len as u64) == payload.length(), 0); | ||
|
||
// 0 stands for keccak256 hash | ||
let pubkey = secp256k1_ecrecover(&signature, &payload, 0); | ||
|
||
// Lazer signer pubkey | ||
// FIXME: validate against trusted signer set in storage | ||
assert!(pubkey == x"03a4380f01136eb2640f90c17e1e319e02bbafbeef2e6e67dc48af53f9827e155b", 0); | ||
|
||
let mut cursor = bcs::new(payload); | ||
let payload_magic = cursor.peel_u32(); | ||
assert!(payload_magic == PAYLOAD_MAGIC, 0); | ||
|
||
let timestamp = cursor.peel_u64(); | ||
|
||
// Verify the signature against trusted signers | ||
verify_le_ecdsa_message(s, clock, &signature, &payload); | ||
|
||
let channel_value = cursor.peel_u8(); | ||
let channel = if (channel_value == 0) { | ||
channel::new_invalid() | ||
|
@@ -86,7 +147,7 @@ public fun parse_and_verify_le_ecdsa_update(update: vector<u8>): Update { | |
option::none(), | ||
option::none(), | ||
option::none(), | ||
option::none() | ||
option::none(), | ||
); | ||
|
||
let properties_count = cursor.peel_u8(); | ||
|
@@ -105,14 +166,18 @@ public fun parse_and_verify_le_ecdsa_update(update: vector<u8>): Update { | |
} else if (property_id == 1) { | ||
let best_bid_price = cursor.peel_u64(); | ||
if (best_bid_price != 0) { | ||
feed.set_best_bid_price(option::some(option::some(i64::from_u64(best_bid_price)))); | ||
feed.set_best_bid_price( | ||
option::some(option::some(i64::from_u64(best_bid_price))), | ||
); | ||
} else { | ||
feed.set_best_bid_price(option::some(option::none())); | ||
} | ||
} else if (property_id == 2) { | ||
let best_ask_price = cursor.peel_u64(); | ||
if (best_ask_price != 0) { | ||
feed.set_best_ask_price(option::some(option::some(i64::from_u64(best_ask_price)))); | ||
feed.set_best_ask_price( | ||
option::some(option::some(i64::from_u64(best_ask_price))), | ||
); | ||
} else { | ||
feed.set_best_ask_price(option::some(option::none())); | ||
} | ||
|
@@ -151,14 +216,16 @@ public fun parse_and_verify_le_ecdsa_update(update: vector<u8>): Update { | |
|
||
if (exists == 1) { | ||
let funding_rate_interval = cursor.peel_u64(); | ||
feed.set_funding_rate_interval(option::some(option::some(funding_rate_interval))); | ||
feed.set_funding_rate_interval( | ||
option::some(option::some(funding_rate_interval)), | ||
); | ||
} else { | ||
feed.set_funding_rate_interval(option::some(option::none())); | ||
} | ||
} else { | ||
// When we have an unknown property, we do not know its length, and therefore | ||
// we cannot ignore it and parse the next properties. | ||
abort 0 // FIXME: return more granular error messages | ||
abort EInvalidUpdate // FIXME: return more granular error messages | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this no longer a fixme? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still a FIXME since there are several other places in the parsing where the code can revert. Will be tackling this next. |
||
}; | ||
|
||
properties_i = properties_i + 1; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to make it public :? maybe public in crate be enough (so we can test it, but users don't use it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, figured it might be handy for users, but yeah i can't think of a scenario where they'd wanna validate without parsing.