Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / message_signing.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! Lightning message signing and verification lives here. These tools can be used to sign messages using the node's
10 //! secret so receivers are sure that they come from you. You can also use this to verify that a given message comes
11 //! from a specific node.
12 //! Furthermore, these tools can be used to sign / verify messages using ephemeral keys not tied to node's identities.
13 //!
14 //! Note this is not part of the specs, but follows lnd's signing and verifying protocol, which can is defined as follows:
15 //!
16 //! signature = zbase32(SigRec(sha256d((\"Lightning Signed Message:\" + msg)))
17 //! zbase32 from <https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt>
18 //! SigRec has first byte 31 + recovery id, followed by 64 byte sig.
19 //!
20 //! This implementation is compatible with both lnd's and c-lightning's
21 //!
22 //! <https://lightning.readthedocs.io/lightning-signmessage.7.html>
23 //! <https://api.lightning.community/#signmessage>
24
25 use alloc::str::FromStr;
26 use core::ffi::c_void;
27 use core::convert::Infallible;
28 use bitcoin::hashes::Hash;
29 use crate::c_types::*;
30 #[cfg(feature="no-std")]
31 use alloc::{vec::Vec, boxed::Box};
32
33 /// Creates a digital signature of a message given a SecretKey, like the node's secret.
34 /// A receiver knowing the PublicKey (e.g. the node's id) and the message can be sure that the signature was generated by the caller.
35 /// Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted.
36 #[no_mangle]
37 pub extern "C" fn sign(mut msg: crate::c_types::u8slice, sk: *const [u8; 32]) -> crate::c_types::derived::CResult_StringErrorZ {
38         let mut ret = lightning::util::message_signing::sign(msg.to_slice(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *sk}[..]).unwrap());
39         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { o.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
40         local_ret
41 }
42
43 /// Recovers the PublicKey of the signer of the message given the message and the signature.
44 #[no_mangle]
45 pub extern "C" fn recover_pk(mut msg: crate::c_types::u8slice, mut sig: crate::c_types::Str) -> crate::c_types::derived::CResult_PublicKeyErrorZ {
46         let mut ret = lightning::util::message_signing::recover_pk(msg.to_slice(), sig.into_str());
47         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
48         local_ret
49 }
50
51 /// Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature,
52 /// and the PublicKey.
53 #[no_mangle]
54 pub extern "C" fn verify(mut msg: crate::c_types::u8slice, mut sig: crate::c_types::Str, mut pk: crate::c_types::PublicKey) -> bool {
55         let mut ret = lightning::util::message_signing::verify(msg.to_slice(), sig.into_str(), &pk.into_rust());
56         ret
57 }
58