fedimint_ln_common/contracts/
outgoing.rsuse bitcoin::hashes::Hash as BitcoinHash;
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::secp256k1::PublicKey;
use fedimint_core::Amount;
use serde::{Deserialize, Serialize};
use super::Preimage;
use crate::contracts::{ContractId, IdentifiableContract};
use crate::LightningInput;
const CANCELLATION_TAG: &str = "outgoing contract cancellation";
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
pub struct OutgoingContract {
pub hash: bitcoin::hashes::sha256::Hash,
pub gateway_key: PublicKey,
pub timelock: u32,
pub user_key: PublicKey,
pub cancelled: bool,
}
impl IdentifiableContract for OutgoingContract {
fn contract_id(&self) -> ContractId {
let mut engine = ContractId::engine();
Encodable::consensus_encode(&self.hash, &mut engine).expect("Hashing never fails");
Encodable::consensus_encode(&self.gateway_key, &mut engine).expect("Hashing never fails");
Encodable::consensus_encode(&self.timelock, &mut engine).expect("Hashing never fails");
Encodable::consensus_encode(&self.user_key, &mut engine).expect("Hashing never fails");
ContractId::from_engine(engine)
}
}
impl OutgoingContract {
pub fn cancellation_message(&self) -> bitcoin::hashes::sha256::Hash {
let mut engine = bitcoin::hashes::sha256::Hash::engine();
Encodable::consensus_encode(&CANCELLATION_TAG.as_bytes(), &mut engine)
.expect("Hashing never fails");
Encodable::consensus_encode(&self.contract_id(), &mut engine).expect("Hashing never fails");
bitcoin::hashes::sha256::Hash::from_engine(engine)
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, Encodable, Decodable, Serialize, Deserialize)]
pub struct OutgoingContractData {
pub recovery_key: bitcoin::key::Keypair,
pub contract_account: OutgoingContractAccount,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Serialize, Deserialize)]
pub struct OutgoingContractAccount {
pub amount: Amount,
pub contract: OutgoingContract,
}
impl OutgoingContractAccount {
pub fn claim(&self, preimage: Preimage) -> LightningInput {
LightningInput::new_v0(self.contract.contract_id(), self.amount, Some(preimage))
}
pub fn refund(&self) -> LightningInput {
LightningInput::new_v0(self.contract.contract_id(), self.amount, None)
}
}