+impl Sign for EnforcingSigner {}
+
+impl Writeable for EnforcingSigner {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
+ // EnforcingSigner has two fields - `inner` ([`InMemorySigner`]) and `state`
+ // ([`EnforcementState`]). `inner` is serialized here and deserialized by
+ // [`KeysInterface::read_chan_signer`]. `state` is managed by [`KeysInterface`]
+ // and will be serialized as needed by the implementation of that trait.
+ self.inner.write(writer)?;
+ Ok(())
+ }
+}
+
+impl EnforcingSigner {
+ fn verify_counterparty_commitment_tx<'a, T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1<T>) -> TrustedCommitmentTransaction<'a> {
+ commitment_tx.verify(&self.inner.get_channel_parameters().as_counterparty_broadcastable(),
+ self.inner.counterparty_pubkeys(), self.inner.pubkeys(), secp_ctx)
+ .expect("derived different per-tx keys or built transaction")
+ }
+
+ fn verify_holder_commitment_tx<'a, T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &'a CommitmentTransaction, secp_ctx: &Secp256k1<T>) -> TrustedCommitmentTransaction<'a> {
+ commitment_tx.verify(&self.inner.get_channel_parameters().as_holder_broadcastable(),
+ self.inner.pubkeys(), self.inner.counterparty_pubkeys(), secp_ctx)
+ .expect("derived different per-tx keys or built transaction")
+ }
+}
+
+/// The state used by [`EnforcingSigner`] in order to enforce policy checks
+///
+/// This structure is maintained by KeysInterface since we may have multiple copies of
+/// the signer and they must coordinate their state.
+#[derive(Clone)]
+pub struct EnforcementState {
+ /// The last counterparty commitment number we signed, backwards counting
+ pub last_counterparty_commitment: u64,
+ /// The last counterparty commitment they revoked, backwards counting
+ pub last_counterparty_revoked_commitment: u64,
+ /// The last holder commitment number we revoked, backwards counting
+ pub last_holder_revoked_commitment: u64,
+ /// The last validated holder commitment number, backwards counting
+ pub last_holder_commitment: u64,
+}
+
+impl EnforcementState {
+ /// Enforcement state for a new channel
+ pub fn new() -> Self {
+ EnforcementState {
+ last_counterparty_commitment: INITIAL_REVOKED_COMMITMENT_NUMBER,
+ last_counterparty_revoked_commitment: INITIAL_REVOKED_COMMITMENT_NUMBER,
+ last_holder_revoked_commitment: INITIAL_REVOKED_COMMITMENT_NUMBER,
+ last_holder_commitment: INITIAL_REVOKED_COMMITMENT_NUMBER,
+ }
+ }
+}