Pin compiler_builtins to 0.1.109 when building std
[ldk-c-bindings] / lightning-c-bindings / src / c_types / mod.rs
index 19e567268ea1fdf4250017942ef61c93df7024ce..c8e140b3eae35316a711c8f4a60735f30ee3a40e 100644 (file)
@@ -7,6 +7,8 @@ use bitcoin::Transaction as BitcoinTransaction;
 use bitcoin::Witness as BitcoinWitness;
 use bitcoin::address;
 use bitcoin::address::WitnessProgram as BitcoinWitnessProgram;
+use bitcoin::key::TweakedPublicKey as BitcoinTweakedPublicKey;
+use bitcoin::key::XOnlyPublicKey;
 use bitcoin::hashes::Hash;
 use bitcoin::secp256k1::PublicKey as SecpPublicKey;
 use bitcoin::secp256k1::SecretKey as SecpSecretKey;
@@ -169,6 +171,25 @@ impl PublicKey {
        pub(crate) fn null() -> Self { Self { compressed_form: [0; 33] } }
 }
 
+#[derive(Clone)]
+#[repr(C)]
+/// Represents a tweaked X-only public key as required for BIP 340 (Taproot).
+pub struct TweakedPublicKey {
+       /// The bytes of the public key X coordinate
+       pub x_coordinate: [u8; 32],
+}
+impl TweakedPublicKey {
+       pub(crate) fn from_rust(pk: &BitcoinTweakedPublicKey) -> Self {
+               Self {
+                       x_coordinate: pk.serialize(),
+               }
+       }
+       pub(crate) fn into_rust(&self) -> BitcoinTweakedPublicKey {
+               let xonly_key = XOnlyPublicKey::from_slice(&self.x_coordinate).unwrap();
+               BitcoinTweakedPublicKey::dangerous_assume_tweaked(xonly_key)
+       }
+}
+
 #[repr(C)]
 #[derive(Clone)]
 /// Represents a valid secp256k1 secret key serialized as a 32 byte array.
@@ -271,6 +292,9 @@ impl BigEndianScalar {
 pub extern "C" fn BigEndianScalar_new(big_endian_bytes: ThirtyTwoBytes) -> BigEndianScalar {
        BigEndianScalar { big_endian_bytes: big_endian_bytes.data }
 }
+#[no_mangle]
+/// Creates a new BigEndianScalar which has the same data as `orig`
+pub extern "C" fn BigEndianScalar_clone(orig: &BigEndianScalar) -> BigEndianScalar { orig.clone() }
 
 #[repr(C)]
 #[derive(Copy, Clone)]
@@ -628,17 +652,39 @@ impl TxIn {
                }
        }
 }
-
-#[no_mangle]
-/// Frees the witness and script_sig in a TxIn
-pub extern "C" fn TxIn_free(_res: TxIn) { }
-
 #[no_mangle]
 /// Convenience function for constructing a new TxIn
 pub extern "C" fn TxIn_new(witness: Witness, script_sig: derived::CVec_u8Z, sequence: u32, previous_txid: ThirtyTwoBytes, previous_vout: u32) -> TxIn {
        TxIn { witness, script_sig, sequence, previous_txid, previous_vout }
 }
-
+#[no_mangle]
+/// Gets the `witness` in the given `TxIn`.
+pub extern "C" fn TxIn_get_witness(txin: &TxIn) -> Witness {
+       txin.witness.clone()
+}
+#[no_mangle]
+/// Gets the `script_sig` in the given `TxIn`.
+pub extern "C" fn TxIn_get_script_sig(txin: &TxIn) -> u8slice {
+       u8slice::from_vec(&txin.script_sig)
+}
+#[no_mangle]
+/// Gets the `sequence` in the given `TxIn`.
+pub extern "C" fn TxIn_get_sequence(txin: &TxIn) -> u32 {
+       txin.sequence
+}
+#[no_mangle]
+/// Gets the previous outpoint txid in the given `TxIn`.
+pub extern "C" fn TxIn_get_previous_txid(txin: &TxIn) -> ThirtyTwoBytes {
+       txin.previous_txid
+}
+#[no_mangle]
+/// Gets the previout outpoint index in the given `TxIn`.
+pub extern "C" fn TxIn_get_previous_vout(txin: &TxIn) -> u32 {
+       txin.previous_vout
+}
+#[no_mangle]
+/// Frees the witness and script_sig in a TxIn
+pub extern "C" fn TxIn_free(_res: TxIn) { }
 
 #[repr(C)]
 #[derive(Clone)]
@@ -672,6 +718,16 @@ pub extern "C" fn TxOut_new(script_pubkey: derived::CVec_u8Z, value: u64) -> TxO
        TxOut { script_pubkey, value }
 }
 #[no_mangle]
+/// Gets the `script_pubkey` in the given `TxOut`.
+pub extern "C" fn TxOut_get_script_pubkey(txout: &TxOut) -> u8slice {
+       u8slice::from_vec(&txout.script_pubkey)
+}
+#[no_mangle]
+/// Gets the value in the given `TxOut`.
+pub extern "C" fn TxOut_get_value(txout: &TxOut) -> u64 {
+       txout.value
+}
+#[no_mangle]
 /// Frees the data pointed to by script_pubkey.
 pub extern "C" fn TxOut_free(_res: TxOut) { }
 #[no_mangle]
@@ -721,6 +777,7 @@ pub struct ThirtyTwoBytes {
        pub data: [u8; 32],
 }
 
+#[derive(Clone)]
 #[repr(C)]
 /// A 3-byte byte array.
 pub struct ThreeBytes { /** The three bytes */ pub data: [u8; 3], }