Skip to content

Commit

Permalink
Add COV_SCRIPT_BYTES constant
Browse files Browse the repository at this point in the history
AFAIK, we cannot construct a `const` Script, so we must use bytes.

It is a bit annoying to have to construct the script just to pass it
into `descriptor_satisfiers` as an argument, but otherwise we end up
returning a value referencing the local variable for the covenant
script inside `descriptor_satisfiers`.

Alternatives are welcome.
  • Loading branch information
luckysori committed Jul 29, 2021
1 parent e26838d commit ed0c504
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ pub struct CollateralContract {
/// This is the only part of the script which is included in the
/// transaction sighash which is signed with `COVENANT_SK`.
///
/// In a regular `CovenantDescriptor` this is just
/// `OP_CHECKSIGVERIFY` and `OP_CHECKSIGFROMSTACK`. In our case,
/// it's `OP_CHECKSIGVERIFY`, `OP_CHECKSIGFROMSTACK` and
/// `OP_ENDIF`.
cov_script: Script,
borrower_pk: PublicKey,
lender_pk: PublicKey,
repayment_principal_output: TxOut,
Expand All @@ -99,6 +94,15 @@ pub struct CollateralContract {
}

impl CollateralContract {
/// The bytes of the Script to be included in the sighash which is signed to
/// satisfy the covenant descriptor.
///
/// In a regular `CovenantDescriptor` this is just
/// `OP_CHECKSIGVERIFY` and `OP_CHECKSIGFROMSTACK`. In our case,
/// it's `OP_CHECKSIGVERIFY`, `OP_CHECKSIGFROMSTACK` and
/// `OP_ENDIF`.
const COV_SCRIPT_BYTES: [u8; 3] = [0xad, 0xc1, 0x68];

/// Fill in the collateral contract template with the provided arguments.
fn new(
borrower_pk: PublicKey,
Expand Down Expand Up @@ -183,16 +187,9 @@ impl CollateralContract {
Script::from(script)
};

let cov_script = Builder::new()
.push_opcode(OP_CHECKSIGVERIFY)
.push_opcode(OP_CHECKSIGFROMSTACK)
.push_opcode(OP_ENDIF)
.into_script();

Ok(Self {
descriptor,
raw_script,
cov_script,
borrower_pk,
lender_pk,
repayment_principal_output,
Expand All @@ -216,13 +213,15 @@ impl CollateralContract {
SF: Future<Output = Result<Signature>>,
{
let transaction_cloned = transaction.clone();
let cov_script = Script::from(Self::COV_SCRIPT_BYTES.to_vec());
let satisfiers = self
.descriptor_satisfiers(
identity_signer,
&transaction_cloned,
input_value,
input_index,
self.borrower_pk,
&cov_script,
)
.await?;

Expand All @@ -246,13 +245,15 @@ impl CollateralContract {
SF: Future<Output = Result<Signature>>,
{
let transaction_cloned = transaction.clone();
let cov_script = Script::from(Self::COV_SCRIPT_BYTES.to_vec());
let satisfiers = self
.descriptor_satisfiers(
identity_signer,
&transaction_cloned,
input_value,
input_index,
self.lender_pk,
&cov_script,
)
.await?;
let after_sat = After(self.timelock);
Expand Down Expand Up @@ -324,14 +325,14 @@ impl CollateralContract {
input_value: confidential::Value,
input_index: u32,
identity_pk: PublicKey,
cov_script: &'a Script,
) -> Result<impl Satisfier<PublicKey> + 'a>
where
S: FnOnce(secp256k1::Message) -> SF,
SF: Future<Output = Result<Signature>>,
{
let descriptor_cov = &self.descriptor.as_cov().expect("covenant descriptor");

let cov_script = &self.cov_script;
let cov_sat = CovSatisfier::new_segwitv0(
&transaction,
input_index,
Expand Down Expand Up @@ -1474,9 +1475,7 @@ pub mod transaction_as_string {

#[cfg(test)]
mod constant_tests {
use super::{COVENANT_PK, COVENANT_SK};
use elements::bitcoin::{PrivateKey, PublicKey};
use secp256k1_zkp::SECP256K1;
use super::*;

#[test]
fn covenant_pk_is_the_public_key_of_covenant_sk() {
Expand All @@ -1487,4 +1486,18 @@ mod constant_tests {

assert_eq!(format!("{}", pk), COVENANT_PK)
}

#[test]
fn cov_script_bytes_represents_correct_script() {
use elements::opcodes::all::*;

let expected = Builder::new()
.push_opcode(OP_CHECKSIGVERIFY)
.push_opcode(OP_CHECKSIGFROMSTACK)
.push_opcode(OP_ENDIF)
.into_script();
let actual = Script::from(CollateralContract::COV_SCRIPT_BYTES.to_vec());

assert_eq!(actual, expected);
}
}

0 comments on commit ed0c504

Please sign in to comment.