Struct Secp256k1

Source
pub struct Secp256k1;
Expand description

Methods for using the ECDSA cryptographic system with the SECP256K1 elliptic curve.

Trait Implementations§

Source§

impl CryptoImplementation for Secp256k1

Source§

fn derive_keypair( &self, decoded_seed: &[u8], is_validator: bool, ) -> XRPLCoreResult<(String, String)>

Derives a key pair for use with the XRP Ledger from a seed value.

§Examples
§Basic usage
use xrpl::core::keypairs::Secp256k1;
use xrpl::core::keypairs::exceptions::XRPLKeypairsException;
use xrpl::core::keypairs::CryptoImplementation;
use xrpl::core::exceptions::XRPLCoreException;

let decoded_seed: &[u8] = &[
    207, 45, 227, 120, 251, 221, 126, 46,
    232, 125, 72, 109, 251, 90, 123, 255
];
let validator: bool = false;
let tuple: (String, String) = (
    "0203F2D90BC50012EC7CB20B07A1B818D6863636FB1E945D17449092CFB5495E1E".into(),
    "0048D93A3B5948E5F9B323BF654BFAD6E8FF75B5FCAB03C5A55AD30CB2515B461F".into(),
);

let derivation: Option<(String, String)> = match Secp256k1.derive_keypair(
    decoded_seed,
    validator,
) {
    Ok((public, private)) => Some((public, private)),
    Err(e) => match e {
        XRPLCoreException::XRPLKeypairsError(XRPLKeypairsException::InvalidSignature) => None,
        XRPLCoreException::XRPLKeypairsError(XRPLKeypairsException::InvalidSecret) => None,
        XRPLCoreException::XRPLKeypairsError(XRPLKeypairsException::SECP256K1Error(_)) => None,
        _ => None,
    },
};

assert_eq!(Some(tuple), derivation);
Source§

fn sign( &self, message_bytes: &[u8], private_key: &str, ) -> XRPLCoreResult<Vec<u8>>

Signs a message using a given private key.

  • message - Text about foo.
  • private_key - Text about bar.
§Examples
§Basic usage
use xrpl::core::keypairs::Secp256k1;
use xrpl::core::keypairs::exceptions::XRPLKeypairsException;
use xrpl::core::keypairs::CryptoImplementation;
use xrpl::core::exceptions::XRPLCoreException;

let message: &[u8] = "test message".as_bytes();
let private_key: &str = "00D78B9735C3F26501C7337B8A5727FD5\
                         3A6EFDBC6AA55984F098488561F985E23";
let signature: Vec<u8> = vec![
    48, 68, 2, 32, 88, 58, 145, 201, 94, 84, 230, 166, 81, 196,
    123, 236, 34, 116, 78, 11, 16, 30, 44, 64, 96, 231, 176, 143,
    99, 65, 101, 125, 173, 155, 195, 238, 2, 32, 125, 20, 137,
    199, 57, 93, 176, 24, 141, 58, 86, 169, 119, 236, 186, 84,
    179, 111, 169, 55, 27, 64, 49, 150, 85, 177, 180, 66, 158,
    51, 239, 45,
];

let signing: Option<Vec<u8>> = match Secp256k1.sign(
    message,
    private_key,
) {
    Ok(signature) => Some(signature),
    Err(e) => match e {
        XRPLCoreException::XRPLKeypairsError(XRPLKeypairsException::SECP256K1Error(_)) => None,
        _ => None,
    },
};

assert_eq!(Some(signature), signing);
Source§

fn is_valid_message( &self, message_bytes: &[u8], signature: &str, public_key: &str, ) -> bool

Verifies the signature on a given message.

§Examples
§Basic usage
use xrpl::core::keypairs::Secp256k1;
use xrpl::core::keypairs::exceptions::XRPLKeypairsException;
use xrpl::core::keypairs::CryptoImplementation;

let message: &[u8] = "test message".as_bytes();
let signature: &str = "30440220583A91C95E54E6A651C47BEC\
                       22744E0B101E2C4060E7B08F6341657D\
                       AD9BC3EE02207D1489C7395DB0188D3A\
                       56A977ECBA54B36FA9371B40319655B1\
                       B4429E33EF2D";
let public_key: &str = "030D58EB48B4420B1F7B9DF55087E0E\
                        29FEF0E8468F9A6825B01CA2C361042D435";

assert!(Secp256k1.is_valid_message(
    message,
    signature,
    public_key,
));

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,