X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fchain%2Fkeysinterface.rs;h=2b90188637c1535bb8ce442a1f5478c914926fcf;hb=a4e405624032c5192ac2777ecbbb0877d6fc43f4;hp=158f71dba28bed6a1b67c2a0a9e8d640c7458a8a;hpb=8ee626c9a4b8b3477acb772ae5de0e49704183e6;p=rust-lightning diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index 158f71db..2b901886 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -37,6 +37,7 @@ use ln::msgs::DecodeError; /// spend on-chain. The information needed to do this is provided in this enum, including the /// outpoint describing which txid and output index is available, the full output which exists at /// that txid/index, and any keys or other information required to sign. +#[derive(Clone, PartialEq)] pub enum SpendableOutputDescriptor { /// An output to a script which was provided via KeysInterface, thus you should already know /// how to spend it. No keys are provided as rust-lightning was never given any keys - only the @@ -53,7 +54,7 @@ pub enum SpendableOutputDescriptor { /// The private key which should be used to sign the transaction is provided, as well as the /// full witness redeemScript which is hashed in the output script_pubkey. /// The witness in the spending input should be: - /// + /// (MINIMALIF standard rule) /// /// Note that the nSequence field in the input must be set to_self_delay (which corresponds to /// the transaction not being broadcastable until at least to_self_delay blocks after the input @@ -88,6 +89,57 @@ pub enum SpendableOutputDescriptor { } } +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::DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => { + 1u8.write(writer)?; + outpoint.write(writer)?; + key.write(writer)?; + witness_script.write(writer)?; + to_self_delay.write(writer)?; + output.write(writer)?; + }, + &SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => { + 2u8.write(writer)?; + outpoint.write(writer)?; + key.write(writer)?; + output.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::DynamicOutputP2WSH { + outpoint: Readable::read(reader)?, + key: Readable::read(reader)?, + witness_script: Readable::read(reader)?, + to_self_delay: Readable::read(reader)?, + output: Readable::read(reader)?, + }), + 2u8 => Ok(SpendableOutputDescriptor::DynamicOutputP2WPKH { + outpoint: Readable::read(reader)?, + key: Readable::read(reader)?, + output: Readable::read(reader)?, + }), + _ => Err(DecodeError::InvalidValue), + } + } +} + /// A trait to describe an object which can get user secrets and key material. pub trait KeysInterface: Send + Sync { /// A type which implements ChannelKeys which will be returned by get_channel_keys. @@ -135,7 +187,8 @@ pub trait KeysInterface: Send + Sync { /// (TODO: We shouldn't require that, and should have an API to get them at deser time, due mostly /// to the possibility of reentrancy issues by calling the user's code during our deserialization /// routine). -/// TODO: remove Clone once we start returning ChannelUpdate objects instead of copying ChannelMonitor +/// TODO: We should remove Clone by instead requesting a new ChannelKeys copy when we create +/// ChannelMonitors instead of expecting to clone the one out of the Channel into the monitors. pub trait ChannelKeys : Send+Clone { /// Gets the private key for the anchor tx fn funding_key<'a>(&'a self) -> &'a SecretKey; @@ -329,8 +382,8 @@ impl Writeable for InMemoryChannelKeys { } } -impl Readable for InMemoryChannelKeys { - fn read(reader: &mut R) -> Result { +impl Readable for InMemoryChannelKeys { + fn read(reader: &mut R) -> Result { let funding_key = Readable::read(reader)?; let revocation_base_key = Readable::read(reader)?; let payment_base_key = Readable::read(reader)?;