Skip to content

Commit f3238bb

Browse files
committed
Add RawMessage for creating signature
1 parent 836c559 commit f3238bb

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ hex = { version = "0.4.3", features = ["serde"] }
1111
reqwest = { version = "0.12.19", features = ["json"] }
1212
secp256k1 = { version = "0.31.0", features = ["recovery"] }
1313
serde = "1.0.219"
14+
serde_wormhole = "0.1.0"
1415
sha3 = "0.10.8"
1516
solana-account-decoder = "2.2.7"
1617
solana-client = "2.2.7"

src/api_client.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,23 @@ pub struct ApiClient {
2323
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
2424

2525
#[derive(Serialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
26-
pub struct Observation<P> {
26+
pub struct Observation<P: Serialize> {
2727
pub version: u8,
2828
#[serde(with = "hex::serde")]
2929
pub signature: [u8; 65],
30+
#[serde(serialize_with = "serialize_body")]
3031
pub body: Body<P>,
3132
}
3233

34+
fn serialize_body<S, P>(body: &Body<P>, serializer: S) -> Result<S::Ok, S::Error>
35+
where
36+
S: serde::Serializer,
37+
P: Serialize,
38+
{
39+
let serialized = serde_wormhole::to_vec(body).map_err(serde::ser::Error::custom)?;
40+
serializer.serialize_bytes(&serialized)
41+
}
42+
3343
impl<P: Serialize> Observation<P> {
3444
pub fn try_new(body: Body<P>, secret_key: SecretKey) -> Result<Self, anyhow::Error> {
3545
let digest = body.digest()?;

src/main.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use {
44
clap::Parser,
55
posted_message::PostedMessageUnreliableData,
66
secp256k1::SecretKey,
7+
serde_wormhole::RawMessage,
78
solana_account_decoder::UiAccountEncoding,
89
solana_client::{
910
nonblocking::pubsub_client::PubsubClient,
@@ -93,31 +94,30 @@ async fn run_listener(input: RunListenerInput) -> Result<(), PubsubClientError>
9394
continue;
9495
}
9596

96-
let body = Body {
97-
timestamp: unreliable_data.submission_time,
98-
nonce: unreliable_data.nonce,
99-
emitter_chain: unreliable_data.emitter_chain.into(),
100-
emitter_address: Address(unreliable_data.emitter_address),
101-
sequence: unreliable_data.sequence,
102-
consistency_level: unreliable_data.consistency_level,
103-
payload: unreliable_data.payload.clone(),
104-
};
105-
106-
match Observation::try_new(body, input.secret_key) {
107-
Ok(observation) => {
108-
tokio::spawn({
109-
let api_client = input.api_client.clone();
110-
async move {
97+
tokio::spawn({
98+
let api_client = input.api_client.clone();
99+
async move {
100+
let body = Body {
101+
timestamp: unreliable_data.submission_time,
102+
nonce: unreliable_data.nonce,
103+
emitter_chain: unreliable_data.emitter_chain.into(),
104+
emitter_address: Address(unreliable_data.emitter_address),
105+
sequence: unreliable_data.sequence,
106+
consistency_level: unreliable_data.consistency_level,
107+
payload: RawMessage::new(unreliable_data.payload.as_slice()),
108+
};
109+
match Observation::try_new(body.clone(), input.secret_key) {
110+
Ok(observation) => {
111111
if let Err(e) = api_client.post_observation(observation).await {
112112
tracing::error!(error = ?e, "Failed to post observation");
113113
} else {
114114
tracing::info!("Observation posted successfully");
115-
}
115+
};
116116
}
117-
});
117+
Err(e) => tracing::error!(error = ?e, "Failed to create observation"),
118+
}
118119
}
119-
Err(e) => tracing::error!(error = ?e, "Failed to create observation"),
120-
};
120+
});
121121
}
122122

123123
tokio::spawn(async move { unsubscribe().await });

0 commit comments

Comments
 (0)