From a2e12ea51ed8326b9b424bca1fc907c28ac37b35 Mon Sep 17 00:00:00 2001 From: CalDescent <> Date: Sun, 15 May 2022 12:15:16 +0100 Subject: [PATCH] Attempt to redeem P2SH with secret. This is highly experimental. Based on Qortal standard HTLCs. Will need adapting to Pirate specific ones at the very least. --- zcash_primitives/src/transaction/builder.rs | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/zcash_primitives/src/transaction/builder.rs b/zcash_primitives/src/transaction/builder.rs index e30d88f..dc60bd1 100644 --- a/zcash_primitives/src/transaction/builder.rs +++ b/zcash_primitives/src/transaction/builder.rs @@ -141,6 +141,7 @@ struct TransparentInputInfo { sk: secp256k1::SecretKey, pubkey: [u8; secp256k1::constants::PUBLIC_KEY_SIZE], coin: TxOut, + secret: Vec, } #[cfg(feature = "transparent-inputs")] @@ -171,6 +172,7 @@ impl TransparentInputs { sk: secp256k1::SecretKey, utxo: OutPoint, coin: TxOut, + secret: Vec, ) -> Result<(), Error> { if coin.value.is_negative() { return Err(Error::InvalidAmount); @@ -190,7 +192,7 @@ impl TransparentInputs { } mtx.vin.push(TxIn::new(utxo)); - self.inputs.push(TransparentInputInfo { sk, pubkey, coin }); + self.inputs.push(TransparentInputInfo { sk, pubkey, coin, secret }); Ok(()) } @@ -232,8 +234,14 @@ impl TransparentInputs { let mut sig_bytes: Vec = sig.serialize_der()[..].to_vec(); sig_bytes.extend(&[SIGHASH_ALL as u8]); - // P2PKH scriptSig - mtx.vin[i].script_sig = Script::default() << &sig_bytes[..] << &info.pubkey[..]; + if (&info.secret.len() > 0) { + // Include secret and redeem script + mtx.vin[i].script_sig = Script::default() << &info.secret[..] << &sig_bytes[..] << &info.pubkey[..] << &info.coin.script_pubkey[..]; + } + else { + // P2PKH scriptSig + mtx.vin[i].script_sig = Script::default() << &sig_bytes[..] << &info.pubkey[..]; + } } } @@ -398,7 +406,19 @@ impl Builder { utxo: OutPoint, coin: TxOut, ) -> Result<(), Error> { - self.transparent_inputs.push(&mut self.mtx, sk, utxo, coin) + self.transparent_inputs.push(&mut self.mtx, sk, utxo, coin, Vec::new()) + } + + /// Adds a transparent coin to be spent in this transaction, with HTLC secret. + #[cfg(feature = "transparent-inputs")] + pub fn add_transparent_input_with_secret( + &mut self, + sk: secp256k1::SecretKey, + utxo: OutPoint, + coin: TxOut, + secret: Vec, + ) -> Result<(), Error> { + self.transparent_inputs.push(&mut self.mtx, sk, utxo, coin, secret) } /// Adds a transparent address to send funds to.