Useful module for creating a pair of public and private keys using which you can sign transactions.
pub struct Keypair {
public_key: Vec<u8>,
secret_key: Option<Vec<u8>>,
secret_seed: Option<Vec<u8>>,
}
trait KeypairTrait {
// Creates new keypair obj
fn new(public_key: Option<[u8; 32]>, secret_key: Option<[u8; 32]>) -> Result<Self, Box<dyn Error>>;
// Creates a keypair obj from secret seed
fn new_from_secret_key(secret_seed: Vec<u8>) -> Result<Self, Box<dyn Error>>;
// Creates a keypair obj from public key
fn new_from_public_key(public_key: Vec<u8>) -> Result<Self, Box<dyn Error>>;
// Creates a new keypair obj from secret
fn from_secret(secret: &str) -> Result<Self, Box<dyn Error>>;
// Creates a new keypair obj from public key
fn from_public_key(public_key: &str) -> Result<Self, Box<dyn Error>>;
// Creates a new keypair obj from raw seed
fn from_raw_ed25519_seed(seed: &[u8]) -> Result<Self, Box<dyn Error>>;
// Getter Methods
fn raw_secret_key(&self) -> Option<Vec<u8>>;
fn raw_public_key(&self) -> &Vec<u8>;
fn secret_key(&self) -> Result<String, Box<dyn Error>>;
fn public_key(&self) -> String;
// returns `true` if the keypair obj contains secret key and can sign.
fn can_sign(&self) -> bool;
// Able to sign the data using the keypair obj
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, Box<dyn Error>>;
// verifies if signature for the data is valid
fn verify(&self, data: &[u8], signature: &[u8]) -> bool;
// Creates a Random Keypair
fn random() -> Result<Self, Box<dyn Error>>;
// Returns keypair obj which is the network master key
fn master(network_passphrase: Option<&str>) -> Result<Self, Box<dyn Error>>;
// XDR Representations
fn xdr_account_id(&self) -> AccountId;
fn xdr_public_key(&self) -> stellar_xdr::PublicKey;
fn xdr_muxed_account_id(&self, id: &str) -> stellar_xdr::MuxedAccount;
// Returns raw public key
fn raw_pubkey(&self) -> [u8; 32];
// part of the decorated signature
fn signature_hint(&self) -> Option<Vec<u8>>;
// Returns the decorated signature (hint+sig) for arbitrary data.
fn sign_decorated(&self, data: &[u8]) -> DecoratedSignature;
// Returns the raw decorated signature (hint+sig) for a signed payload signer.
fn sign_payload_decorated(&self, data: &[u8]) -> DecoratedSignature;
}
https://github.com/rahul-soshte/rs-stellar-base/blob/main/src/keypair.rs