Skip to main content

crypto_signer/
lib.rs

1//! # crypto-signer
2//!
3//! High-performance, low-latency, lightweight multi-chain signing primitives.
4//!
5//! ## Ethereum / EVM (EIP-712)
6//!
7//! ```rust
8//! # #[cfg(all(feature = "evm", feature = "k256-signer"))] {
9//! use crypto_signer::{Address, Domain, PermitBuilder};
10//! use crypto_signer::backends::local_k256::LocalK256Signer;
11//! use k256::ecdsa::SigningKey;
12//!
13//! let key = SigningKey::from_bytes(&[0x01; 32].into()).unwrap();
14//! let signer = LocalK256Signer::from_signing_key(key);
15//!
16//! let domain = Domain::new("USDC", "1", 137, Address::new([0x11; 20]));
17//! let signed = PermitBuilder::new(domain)
18//!     .spender(Address::new([0x22; 20]))
19//!     .value(1_000_000)
20//!     .nonce(0)
21//!     .deadline(1_700_000_000)
22//!     .build_and_sign(&signer)
23//!     .expect("signing is infallible with a valid key");
24//!
25//! let (v, r, s) = signed.vrs();
26//! assert!(v == 27 || v == 28);
27//! # }
28//! ```
29//!
30//! ## Bitcoin (PSBT via hardware wallet)
31//!
32//! ```rust,ignore
33//! # #[cfg(all(feature = "bitcoin", feature = "hw"))] {
34//! use crypto_signer::chains::bitcoin::{sign_psbt, PsbtBytes};
35//! use crypto_signer::hw::DerivationPath;
36//!
37//! // m/84'/0'/0'/0/0 — native SegWit
38//! let path = DerivationPath(vec![0x8000_0054, 0x8000_0000, 0x8000_0000, 0, 0]);
39//! let signed_psbt = sign_psbt(&ledger, &path, PsbtBytes(raw_psbt))?;
40//! # }
41//! ```
42//!
43//! ## Solana (Ed25519 via hardware wallet)
44//!
45//! ```rust,ignore
46//! # #[cfg(all(feature = "solana", feature = "hw"))] {
47//! use crypto_signer::chains::solana::{sign_transaction, SolanaTxBytes};
48//! use crypto_signer::hw::DerivationPath;
49//!
50//! // m/44'/501'/0'/0'
51//! let path = DerivationPath(vec![0x8000_002c, 0x8000_01f5, 0x8000_0000, 0x8000_0000]);
52//! let sig = sign_transaction(&ledger, &path, SolanaTxBytes(tx_bytes))?;
53//! assert_eq!(sig.0.len(), 64);
54//! # }
55//! ```
56//!
57//! ## Cosmos (SignDoc SHA-256)
58//!
59//! ```rust
60//! # #[cfg(all(feature = "cosmos", feature = "k256-signer"))] {
61//! use crypto_signer::chains::cosmos::SignDoc;
62//! use crypto_signer::Signer;
63//! use crypto_signer::backends::local_k256::LocalK256Signer;
64//! use k256::ecdsa::SigningKey;
65//!
66//! let key = SigningKey::from_bytes(&[0x02; 32].into()).unwrap();
67//! let signer = LocalK256Signer::from_signing_key(key);
68//!
69//! let doc = SignDoc {
70//!     body_bytes: vec![0x0a, 0x01, 0x01],
71//!     auth_info_bytes: vec![0x12, 0x01, 0x02],
72//!     chain_id: "osmosis-1".to_string(),
73//!     account_number: 42,
74//!     sequence: 1,
75//! };
76//!
77//! let hash = doc.signing_hash();
78//! let signature = signer.sign_hash(hash).expect("valid key");
79//! # }
80//! ```
81#![cfg_attr(not(feature = "std"), no_std)]
82
83extern crate alloc;
84
85pub mod signer;
86pub mod types;
87
88#[cfg(feature = "evm")]
89pub mod evm;
90
91pub mod backends;
92pub mod chains;
93
94#[cfg(feature = "kms")]
95pub mod kms;
96
97#[cfg(feature = "hw")]
98pub mod hw;
99
100pub use signer::{BuildError, Signer, SignerType};
101pub use types::{Address, Signature};
102
103#[cfg(feature = "evm")]
104pub use evm::{
105    domain::Domain,
106    eip712::{Eip712Type, Signed, TypedMessage, Unsigned},
107    messages::{Order, Permit, PermitBuilder, PermitSignError},
108    network::NetworkConfig,
109    RecoveryError,
110};
111
112#[cfg(all(feature = "evm", feature = "k256-signer"))]
113pub use evm::recover_signer;
114
115#[cfg(feature = "bitcoin")]
116pub use chains::bitcoin::PsbtBytes;
117
118#[cfg(feature = "solana")]
119pub use chains::solana::{SolanaSignature, SolanaTxBytes};