Struct NodePubkey

Source
pub struct NodePubkey(pub PublicKey);
Available on crate feature secp256k1 only.
Expand description

Newtype over secp256k1::PublicKey representing a deserialized key identifying an LN node.

This can be considered similar to NodeId with these differences:

  • NodeId is more performant for non-cryptographic operations.
  • NodeId can not perform any cryptographic operations itself.
  • NodePubkey, despite its field being public, maintains more invariants. In this library, a valid NodeId is not guaranteed to be a valid NodePubkey

§Example

let marvin_str = "029ef8ee0ba895e2807ac1df1987a7888116c468e70f42e7b089e06811b0e45482";
let marvin = marvin_str.parse::<ln_types::NodePubkey>().unwrap();
assert_eq!(marvin.to_string(), marvin_str);

Tuple Fields§

§0: PublicKey

The underlying public key used for cryptographic operations.

Implementations§

Source§

impl NodePubkey

Source

pub fn verify<M: Into<Message>, C: Verification>( &self, secp: &Secp256k1<C>, message: M, signature: &[u8], ) -> Result<(), Error>

Available on crate feature node_pubkey_verify only.

Verify a message signed by this key.

This is a convenience method that simply uses slices. While this could be seen as regression from strongly-typed secp256k1 library, it should be a good tradeoff here. The reason is we know that LN signatures are not DER-encoded and there shouldn’t be a reason to need to keep message hash around.

If you need anything advanced, you can still use the raw secp256k1::PublicKey.

§Example
use ln_types::secp256k1::hashes::{sha256d, Hash};

let marvin_str = "029ef8ee0ba895e2807ac1df1987a7888116c468e70f42e7b089e06811b0e45482";
let marvin = marvin_str.parse::<ln_types::NodePubkey>().unwrap();
let message = "Lightning Signed Message:I am the author of `ln-types` Rust crate.";
let signature = &[0x29, 0x79, 0x4d, 0x9a, 0x6a, 0x48, 0x68, 0x0f, 0x9b, 0x8d, 0x60, 0x97, 0xa6, 0xd8, 0xef, 0x1d, 0x5c, 0xf9, 0xdc, 0x27, 0xcd, 0x76, 0x9a, 0x86, 0x58, 0xd6, 0x94, 0x00, 0x1c, 0x12, 0xb8, 0xdd, 0x49, 0xaf, 0x2b, 0xca, 0x0a, 0x24, 0xd8, 0xf4, 0x5a, 0x3b, 0x3c, 0xc7, 0x87, 0xf0, 0x48, 0x60, 0x63, 0x23, 0xf4, 0x24, 0xba, 0xa8, 0x0f, 0x5e, 0xe6, 0x05, 0x79, 0x81, 0xe2, 0x29, 0x6f, 0x0d];
let secp = ln_types::secp256k1::Secp256k1::verification_only();
let message = sha256d::Hash::hash(message.as_bytes());
let message = secp256k1::Message::from_digest(message.to_byte_array());

marvin.verify(&secp, message, signature).unwrap();
Source

pub fn verify_lightning_message<C: Verification>( &self, secp: &Secp256k1<C>, message: &[u8], signature: &[u8], ) -> Result<(), Error>

Available on crate feature node_pubkey_recovery only.

Verifies a message signed by signmessage Eclair/LND RPC.

The signatures of messages returned by node RPCs are not so simple. They prefix messages with Lightning Signed Message:, use double sha256 and recovery. It is the reason why this function requires the recovery feature of secp256k1.

This function takes care of all that complexity for you so that you can verify the messages conveniently.

§Example
let marvin_str = "029ef8ee0ba895e2807ac1df1987a7888116c468e70f42e7b089e06811b0e45482";
let marvin = marvin_str.parse::<ln_types::NodePubkey>().unwrap();
let message = "I am the author of `ln-types` Rust crate.";
let signature = &[0x1f, 0x29, 0x79, 0x4d, 0x9a, 0x6a, 0x48, 0x68, 0x0f, 0x9b, 0x8d, 0x60, 0x97, 0xa6, 0xd8, 0xef, 0x1d, 0x5c, 0xf9, 0xdc, 0x27, 0xcd, 0x76, 0x9a, 0x86, 0x58, 0xd6, 0x94, 0x00, 0x1c, 0x12, 0xb8, 0xdd, 0x49, 0xaf, 0x2b, 0xca, 0x0a, 0x24, 0xd8, 0xf4, 0x5a, 0x3b, 0x3c, 0xc7, 0x87, 0xf0, 0x48, 0x60, 0x63, 0x23, 0xf4, 0x24, 0xba, 0xa8, 0x0f, 0x5e, 0xe6, 0x05, 0x79, 0x81, 0xe2, 0x29, 0x6f, 0x0d];
let secp = ln_types::secp256k1::Secp256k1::verification_only();

marvin.verify_lightning_message(&secp, message.as_bytes(), signature).unwrap();
Source

pub fn to_node_id(&self) -> NodeId

Convenience conversion method.

This is more readable and less prone to inference problems than Into::into.

Source

pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>, sk: &SecretKey) -> Self

Computes public key from a secret key and stores it as NodePubkey.

Trait Implementations§

Source§

impl AsMut<PublicKey> for NodePubkey

Source§

fn as_mut(&mut self) -> &mut PublicKey

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<PublicKey> for NodePubkey

Source§

fn as_ref(&self) -> &PublicKey

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<PublicKey> for NodePubkey

Source§

fn borrow(&self) -> &PublicKey

Immutably borrows from an owned value. Read more
Source§

impl BorrowMut<PublicKey> for NodePubkey

Source§

fn borrow_mut(&mut self) -> &mut PublicKey

Mutably borrows from an owned value. Read more
Source§

impl Clone for NodePubkey

Source§

fn clone(&self) -> NodePubkey

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NodePubkey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for NodePubkey

Shows NodePubkey as hex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a NodePubkey> for NodeId

Source§

fn from(value: &'a NodePubkey) -> Self

Converts to this type from the input type.
Source§

impl From<NodePubkey> for [u8; 33]

Source§

fn from(value: NodePubkey) -> Self

Converts to this type from the input type.
Source§

impl From<NodePubkey> for NodeId

Source§

fn from(value: NodePubkey) -> Self

Converts to this type from the input type.
Source§

impl<'a> FromSql<'a> for NodePubkey

Available on crate feature postgres-types only.

Supports BYTEA, TEXT, and VARCHAR.

Decoded as bytes if BYTEA is used, as hex string otherwise.

Source§

fn from_sql( ty: &Type, raw: &'a [u8], ) -> Result<Self, Box<dyn Error + Send + Sync + 'static>>

Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be created from the specified Postgres Type.
Source§

fn from_sql_null(ty: &Type) -> Result<Self, Box<dyn Error + Send + Sync>>

Creates a new value of this type from a NULL SQL value. Read more
Source§

fn from_sql_nullable( ty: &Type, raw: Option<&'a [u8]>, ) -> Result<Self, Box<dyn Error + Send + Sync>>

A convenience function that delegates to from_sql and from_sql_null depending on the value of raw.
Source§

impl FromStr for NodePubkey

Expects hex representation

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for NodePubkey

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl LowerHex for NodePubkey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for NodePubkey

Source§

fn cmp(&self, other: &NodePubkey) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl ParseArgFromStr for NodePubkey

Available on crate feature parse_arg only.
Source§

fn describe_type<W: Write>(writer: W) -> Result

Writes human-readable description of the type to the writer. Read more
Source§

impl PartialEq for NodePubkey

Source§

fn eq(&self, other: &NodePubkey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for NodePubkey

Source§

fn partial_cmp(&self, other: &NodePubkey) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl ToSql for NodePubkey

Available on crate feature postgres-types only.

Supports BYTEA, TEXT, and VARCHAR.

Stored as bytes if BYTEA is used, as hex string otherwise.

Source§

fn to_sql( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Send + Sync + 'static>>

Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be converted to the specified Postgres Type.
Source§

fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

An adaptor method used internally by Rust-Postgres. Read more
Source§

fn encode_format(&self, _ty: &Type) -> Format

Specify the encode format
Source§

impl<'a> TryFrom<&'a [u8]> for NodePubkey

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(slice: &'a [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a str> for NodePubkey

Expects hex representation

Source§

type Error = ParseError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Box<[u8]>> for NodePubkey

Available on crate feature alloc only.
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(slice: Box<[u8]>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Box<str>> for NodePubkey

Available on crate feature alloc only.

Expects hex representation

Source§

type Error = ParseError

The type returned in the event of a conversion error.
Source§

fn try_from(s: Box<str>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<NodeId> for NodePubkey

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: NodeId) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for NodePubkey

Available on crate feature alloc only.

Expects hex representation

Source§

type Error = ParseError

The type returned in the event of a conversion error.
Source§

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Vec<u8>> for NodePubkey

Available on crate feature alloc only.
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl UpperHex for NodePubkey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Value for NodePubkey

Available on crate feature slog only.

Currently uses Display but may use emit_bytes if/when it’s implemented.

Source§

fn serialize( &self, _rec: &Record<'_>, key: Key, serializer: &mut dyn Serializer, ) -> Result

Serialize self into Serializer Read more
Source§

impl Eq for NodePubkey

Source§

impl StructuralPartialEq for NodePubkey

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> BorrowToSql for T
where T: ToSql,

Source§

fn borrow_to_sql(&self) -> &dyn ToSql

Returns a reference to self as a ToSql trait object.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ParseArg for T
where T: ParseArgFromStr, <T as FromStr>::Err: Display,

Source§

type Error = ParseArgError<<T as FromStr>::Err>

Type returned in the Err variant of Result when parsing fails.
Source§

fn parse_arg(arg: &OsStr) -> Result<T, <T as ParseArg>::Error>

Parses the argument.
Source§

fn describe_type<W>(writer: W) -> Result<(), Error>
where W: Write,

Writes human-readable description of the type to the writer. Read more
Source§

fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error>

Parses the argument consuming it. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> FromSqlOwned for T
where T: for<'a> FromSql<'a>,