X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fchain%2Fkeysinterface.rs;h=c5ad6f28b263d85dd8559c0df82b5e6e9e91f1e7;hb=bfd9646092e3c63f07d96df35dd72488af55a176;hp=114ceec211cd254fb22d14f64ac379960237f59b;hpb=c7e198e6fca8fc31e004b51368978b109c1caff6;p=rust-lightning diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index 114ceec2..c5ad6f28 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -37,7 +37,7 @@ use ln::chan_utils; use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction}; use ln::msgs::UnsignedChannelAnnouncement; -use std::collections::HashSet; +use prelude::*; use core::sync::atomic::{AtomicUsize, Ordering}; use std::io::Error; use ln::msgs::{DecodeError, MAX_VALUE_MSAT}; @@ -72,6 +72,16 @@ impl DelayedPaymentOutputDescriptor { pub const MAX_WITNESS_LENGTH: usize = 1 + 73 + 1 + chan_utils::REVOKEABLE_REDEEMSCRIPT_MAX_LENGTH + 1; } +impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, { + (0, outpoint), + (2, per_commitment_point), + (4, to_self_delay), + (6, output), + (8, revocation_pubkey), + (10, channel_keys_id), + (12, channel_value_satoshis), +}, {}, {}); + /// Information about a spendable output to our "payment key". See /// SpendableOutputDescriptor::StaticPaymentOutput for more details on how to spend this. #[derive(Clone, Debug, PartialEq)] @@ -93,6 +103,12 @@ impl StaticPaymentOutputDescriptor { // redeemscript push length. pub const MAX_WITNESS_LENGTH: usize = 1 + 73 + 34; } +impl_writeable_tlv_based!(StaticPaymentOutputDescriptor, { + (0, outpoint), + (2, output), + (4, channel_keys_id), + (6, channel_value_satoshis), +}, {}, {}); /// When on-chain outputs are created by rust-lightning (which our counterparty is not able to /// claim at any point in the future) an event is generated which you must track and be able to @@ -151,62 +167,15 @@ pub enum SpendableOutputDescriptor { StaticPaymentOutput(StaticPaymentOutputDescriptor), } -impl Writeable for SpendableOutputDescriptor { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - match self { - &SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => { - 0u8.write(writer)?; - outpoint.write(writer)?; - output.write(writer)?; - }, - &SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) => { - 1u8.write(writer)?; - descriptor.outpoint.write(writer)?; - descriptor.per_commitment_point.write(writer)?; - descriptor.to_self_delay.write(writer)?; - descriptor.output.write(writer)?; - descriptor.revocation_pubkey.write(writer)?; - descriptor.channel_keys_id.write(writer)?; - descriptor.channel_value_satoshis.write(writer)?; - }, - &SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => { - 2u8.write(writer)?; - descriptor.outpoint.write(writer)?; - descriptor.output.write(writer)?; - descriptor.channel_keys_id.write(writer)?; - descriptor.channel_value_satoshis.write(writer)?; - }, - } - Ok(()) - } -} - -impl Readable for SpendableOutputDescriptor { - fn read(reader: &mut R) -> Result { - match Readable::read(reader)? { - 0u8 => Ok(SpendableOutputDescriptor::StaticOutput { - outpoint: Readable::read(reader)?, - output: Readable::read(reader)?, - }), - 1u8 => Ok(SpendableOutputDescriptor::DelayedPaymentOutput(DelayedPaymentOutputDescriptor { - outpoint: Readable::read(reader)?, - per_commitment_point: Readable::read(reader)?, - to_self_delay: Readable::read(reader)?, - output: Readable::read(reader)?, - revocation_pubkey: Readable::read(reader)?, - channel_keys_id: Readable::read(reader)?, - channel_value_satoshis: Readable::read(reader)?, - })), - 2u8 => Ok(SpendableOutputDescriptor::StaticPaymentOutput(StaticPaymentOutputDescriptor { - outpoint: Readable::read(reader)?, - output: Readable::read(reader)?, - channel_keys_id: Readable::read(reader)?, - channel_value_satoshis: Readable::read(reader)?, - })), - _ => Err(DecodeError::InvalidValue), - } - } -} +impl_writeable_tlv_based_enum!(SpendableOutputDescriptor, + (0, StaticOutput) => { + (0, outpoint), + (2, output), + }, {}, {}, +; + (1, DelayedPaymentOutput), + (2, StaticPaymentOutput), +); /// A trait to sign lightning channel transactions as described in BOLT 3. /// @@ -704,10 +673,15 @@ impl BaseSign for InMemorySigner { } } +const SERIALIZATION_VERSION: u8 = 1; +const MIN_SERIALIZATION_VERSION: u8 = 1; + impl Sign for InMemorySigner {} impl Writeable for InMemorySigner { fn write(&self, writer: &mut W) -> Result<(), Error> { + write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION); + self.funding_key.write(writer)?; self.revocation_base_key.write(writer)?; self.payment_key.write(writer)?; @@ -718,12 +692,16 @@ impl Writeable for InMemorySigner { self.channel_value_satoshis.write(writer)?; self.channel_keys_id.write(writer)?; + write_tlv_fields!(writer, {}, {}); + Ok(()) } } impl Readable for InMemorySigner { fn read(reader: &mut R) -> Result { + let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION); + let funding_key = Readable::read(reader)?; let revocation_base_key = Readable::read(reader)?; let payment_key = Readable::read(reader)?; @@ -739,6 +717,8 @@ impl Readable for InMemorySigner { &htlc_base_key); let keys_id = Readable::read(reader)?; + read_tlv_fields!(reader, {}, {}); + Ok(InMemorySigner { funding_key, revocation_base_key,