-
Notifications
You must be signed in to change notification settings - Fork 8
fix: handle oracle messages as a tlv #131
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| //! Structs containing oracle information. | ||
|
|
||
| use crate::ser_impls::{ | ||
| read_as_tlv, read_i32, read_schnorr_pubkey, read_schnorrsig, read_strings_u16, write_as_tlv, | ||
| write_i32, write_schnorr_pubkey, write_schnorrsig, write_strings_u16, BigSize, | ||
| read_as_tlv, read_i32, read_schnorr_pubkey, read_strings_u16, write_as_tlv, write_i32, | ||
| write_schnorr_pubkey, write_schnorrsig, write_strings_u16, BigSize, | ||
| }; | ||
| use bitcoin::hashes::{Hash, HashEngine}; | ||
| use ddk_dlc::{Error, OracleInfo as DlcOracleInfo}; | ||
|
|
@@ -224,11 +224,18 @@ impl OracleAnnouncement { | |
| } | ||
| } | ||
|
|
||
| impl_dlc_writeable!(OracleAnnouncement, { | ||
| (announcement_signature, {cb_writeable, write_schnorrsig, read_schnorrsig}), | ||
| (oracle_public_key, {cb_writeable, write_schnorr_pubkey, read_schnorr_pubkey}), | ||
| (oracle_event, {cb_writeable, write_as_tlv, read_as_tlv}) | ||
| }); | ||
| // fn read_oracle_message<T: Readable>(msg: MagnoliaResponse) -> Result<T, Error> { | ||
| // let bytes = hex::decode(msg.hex).map_err(|e| Error::OracleError(e.to_string()))?; | ||
| // let mut cursor = Cursor::new(bytes); | ||
| // let _type_id: u64 = lightning::util::ser::BigSize::read(&mut cursor).unwrap().0; | ||
| // let _length: u64 = lightning::util::ser::BigSize::read(&mut cursor).unwrap().0; | ||
| // T::read(&mut cursor).map_err(|e| Error::OracleError(e.to_string())) | ||
| // } | ||
| // impl_dlc_writeable!(OracleAnnouncement, { | ||
| // (announcement_signature, {cb_writeable, write_schnorrsig, read_schnorrsig}), | ||
| // (oracle_public_key, {cb_writeable, write_schnorr_pubkey, read_schnorr_pubkey}), | ||
| // (oracle_event, {cb_writeable, write_as_tlv, read_as_tlv}) | ||
| // }); | ||
|
Comment on lines
+227
to
+238
Contributor
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 worth keeping commented out versus deleting entirely? |
||
|
|
||
| impl From<&OracleAnnouncement> for DlcOracleInfo { | ||
| fn from(input: &OracleAnnouncement) -> DlcOracleInfo { | ||
|
|
@@ -239,6 +246,49 @@ impl From<&OracleAnnouncement> for DlcOracleInfo { | |
| } | ||
| } | ||
|
|
||
| impl Readable for OracleAnnouncement { | ||
| fn read<R: bitcoin::io::Read>(reader: &mut R) -> Result<Self, DecodeError> { | ||
| let _type_id: u64 = BigSize::read(reader)?.0; | ||
| let _length: u64 = BigSize::read(reader)?.0; | ||
|
Contributor
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. On lines 252, 257, 502 the |
||
|
|
||
| let announcement_signature = crate::ser_impls::read_schnorrsig(reader)?; | ||
| let oracle_public_key = crate::ser_impls::read_schnorr_pubkey(reader)?; | ||
| let _type_id: u64 = BigSize::read(reader)?.0; | ||
| let _length: u64 = BigSize::read(reader)?.0; | ||
|
Comment on lines
+256
to
+257
Contributor
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. Why is TLV header read twice? |
||
| let oracle_event = OracleEvent::read(reader)?; | ||
|
|
||
| Ok(Self { | ||
| announcement_signature, | ||
| oracle_public_key, | ||
| oracle_event, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Writeable for OracleAnnouncement { | ||
| fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> { | ||
| BigSize(self.type_id() as u64).write(writer)?; | ||
| BigSize(self.serialized_length() as u64).write(writer)?; | ||
| write_schnorrsig(&self.announcement_signature, writer)?; | ||
| write_schnorr_pubkey(&self.oracle_public_key, writer)?; | ||
| write_as_tlv(&self.oracle_event, writer)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn serialized_length(&self) -> usize { | ||
| // Calculate size without the outer TLV wrapper (type + length) | ||
| 64 + // announcement_signature | ||
| 32 + // oracle_public_key | ||
| { | ||
| // oracle_event as TLV | ||
| let event_size = self.oracle_event.serialized_length(); | ||
| BigSize(self.oracle_event.type_id() as u64).serialized_length() + | ||
| BigSize(event_size as u64).serialized_length() + | ||
| event_size | ||
|
Comment on lines
+284
to
+287
Contributor
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. Calculating the size manually could lead to buffer overflows. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Eq, PartialEq, Debug)] | ||
| #[cfg_attr( | ||
| feature = "use-serde", | ||
|
|
@@ -446,12 +496,53 @@ impl Type for OracleAttestation { | |
| } | ||
| } | ||
|
|
||
| impl_dlc_writeable!(OracleAttestation, { | ||
| (event_id, string), | ||
| (oracle_public_key, {cb_writeable, write_schnorr_pubkey, read_schnorr_pubkey}), | ||
| (signatures, {vec_u16_cb, write_schnorrsig, read_schnorrsig}), | ||
| (outcomes, {cb_writeable, write_strings_u16, read_strings_u16}) | ||
| }); | ||
| impl Readable for OracleAttestation { | ||
| fn read<R: bitcoin::io::Read>(reader: &mut R) -> Result<Self, DecodeError> { | ||
| let _type_id: u64 = BigSize::read(reader)?.0; | ||
| let _length: u64 = BigSize::read(reader)?.0; | ||
|
|
||
| Ok(Self { | ||
| event_id: Readable::read(reader)?, | ||
| oracle_public_key: crate::ser_impls::read_schnorr_pubkey(reader)?, | ||
| signatures: { | ||
| let len: u16 = Readable::read(reader)?; | ||
| let mut signatures = Vec::with_capacity(len as usize); | ||
|
Contributor
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. No validation on |
||
| for _ in 0..len { | ||
| signatures.push(crate::ser_impls::read_schnorrsig(reader)?); | ||
| } | ||
| signatures | ||
| }, | ||
| outcomes: crate::ser_impls::read_strings_u16(reader)?, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Writeable for OracleAttestation { | ||
| fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> { | ||
| BigSize(self.type_id() as u64).write(writer)?; | ||
| BigSize(self.serialized_length() as u64).write(writer)?; | ||
| self.event_id.write(writer)?; | ||
| write_schnorr_pubkey(&self.oracle_public_key, writer)?; | ||
| (self.signatures.len() as u16).write(writer)?; | ||
| for sig in &self.signatures { | ||
| write_schnorrsig(sig, writer)?; | ||
| } | ||
| write_strings_u16(&self.outcomes, writer)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn serialized_length(&self) -> usize { | ||
| // Calculate size without the outer TLV wrapper (type + length) | ||
| self.event_id.serialized_length() + | ||
| 32 + // oracle_public_key | ||
| 2 + // signatures length u16 | ||
| (64 * self.signatures.len()) + // each signature is 64 bytes | ||
| { | ||
| // outcomes as u16 length + strings | ||
| 2 + self.outcomes.iter().map(|s| s.len() + 2).sum::<usize>() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
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.
Should we add validation on whether the parsed content matches the expected values?