1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Cryptographic key pairs for the XRP Ledger
//!
//! An implementation of XRP Ledger keypairs & wallet generation
//! which supports rfc6979 and eddsa deterministic signatures.
//!
//! # Examples
//!
//! ## Generate a random XRP Ledger address
//!
//! ```
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! #
//! use ripple_keypairs::Seed;
//!
//! let seed = Seed::random();
//! let (_, public_key) = seed.derive_keypair()?;
//! let address = public_key.derive_address();
//!
//! assert!(address.starts_with("r"));
//! #
//! # Ok(())
//! # }
//! ```
//!
//! ## Encode a seed in Base58 XRP Legder format
//!
//! ```
//! use ripple_keypairs::{Seed, Entropy, Algorithm};
//!
//! // In the real world you **must** generate random entropy
//! let entropy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
//! let seed = Seed::new(Entropy::Array(entropy), &Algorithm::Secp256k1);
//!
//! assert_eq!(seed.to_string(), "sp5fghtJtpUorTwvof1NpDXAzNwf5");
//! ```
//!
//! ## Parse a string into a seed
//!
//! ```
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! #
//! use std::str::FromStr;
//! use ripple_keypairs::{Seed, error};
//!
//! let seed = Seed::from_str("sp5fghtJtpUorTwvof1NpDXAzNwf5")?;
//!
//! assert_eq!(seed, "sp5fghtJtpUorTwvof1NpDXAzNwf5".parse()?);
//! assert_eq!(Err(error::DecodeError), "bad seed".parse::<Seed>());
//! #
//! # Ok(())
//! # }
//! ```

#![deny(
    warnings,
    clippy::all,
    missing_debug_implementations,
    missing_copy_implementations,
    missing_docs,
    missing_crate_level_docs,
    missing_doc_code_examples,
    non_ascii_idents,
    unreachable_pub
)]
#![doc(test(attr(deny(warnings))))]
#![doc(html_root_url = "https://docs.rs/ripple-keypairs/0.1.0")]

use std::{convert::TryInto, fmt, str::FromStr};

use ring::rand::{SecureRandom, SystemRandom};

mod utils;

mod hexbytes;
pub use hexbytes::HexBytes;

mod algorithm;
use algorithm as alg;

pub mod error;
type KeyPairResult = error::Result<(PrivateKey, PublicKey)>;

pub use codec::{Algorithm, Entropy as EntropyArray};
use ripple_address_codec as codec;

use Algorithm::*;
use Entropy::*;

/// Entropy which is used to generate seed
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Entropy {
    /// Random entropy
    Random,
    /// An array of bytes
    Array(EntropyArray),
}

/// A seed that can be used to generate keypairs
///
/// # Examples
///
/// ## Generate a new seed
///
/// ```
/// use ripple_keypairs::{Seed, Entropy, Algorithm};
///
/// let seed = Seed::new(Entropy::Random, &Algorithm::Secp256k1);
///
/// assert!(seed.to_string().starts_with("s"));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Seed {
    entropy: EntropyArray,
    kind: &'static Algorithm,
}

impl Seed {
    /// Generate a new seed
    ///
    /// # Examples
    ///
    /// ```
    /// use ripple_keypairs::{Seed, Entropy, Algorithm};
    ///
    /// let seed_secp256k1 = Seed::new(Entropy::Random, &Algorithm::Secp256k1);
    ///
    /// assert!(seed_secp256k1.to_string().starts_with("s"));
    /// assert_eq!(seed_secp256k1.as_kind(), &Algorithm::Secp256k1);
    ///
    /// let seed_ed25519 = Seed::new(Entropy::Random, &Algorithm::Ed25519);
    ///
    /// assert!(seed_ed25519.to_string().starts_with("s"));
    /// assert_eq!(seed_ed25519.as_kind(), &Algorithm::Ed25519);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics only if something goes wrong with the random generator
    /// when using the [`Entropy::Random`] parameter.
    pub fn new(entropy: Entropy, kind: &'static Algorithm) -> Self {
        let entropy = match entropy {
            Array(entropy) => entropy,

            Random => {
                let mut entropy: EntropyArray = [0; 16];

                SystemRandom::new()
                    .fill(&mut entropy)
                    .expect("unspecified random geterator error");

                entropy
            }
        };

        Self { entropy, kind }
    }

    /// Generate a random seed
    ///
    /// The algorithm defaults to Secp256k1.
    ///
    /// # Examples
    ///
    /// ```
    /// use ripple_keypairs::{Seed, Algorithm};
    ///
    /// let seed = Seed::random();
    ///
    /// assert_eq!(seed.as_kind(), &Algorithm::Secp256k1);
    /// assert_ne!(Seed::random(), Seed::random());
    /// ```
    pub fn random() -> Self {
        Self::new(Random, &Secp256k1)
    }

    /// Derive a public and private key from a seed
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::error::Error;
    /// # fn main() -> Result<(), Box<dyn Error>> {
    /// #
    /// use ripple_keypairs::Seed;
    ///
    /// let seed = Seed::random();
    /// let (private_key, public_key) = seed.derive_keypair()?;
    /// let msg = "Test message";
    ///
    /// assert_eq!(public_key.verify(&msg, &private_key.sign(&msg)), Ok(()));
    /// #
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// May return [`error::DeriveKeyPairError`] if the derived keypair
    /// did not generate a verifiable signature
    pub fn derive_keypair(&self) -> KeyPairResult {
        let keypair = self.method().derive_keypair(self.as_entropy())?;

        /* additional safety check */
        {
            let test_message =
                utils::sha512_digest_32("This test message should verify".as_bytes());

            let (private_key, public_key) = &keypair;

            public_key
                .verify(&test_message, &private_key.sign(&test_message))
                .map_err(|_| error::DeriveKeyPairError)?;
        }

        Ok(keypair)
    }

    /// Seed as [`EntropyArray`]
    ///
    /// # Examples
    /// ```
    /// use ripple_keypairs::{Seed, Entropy, Algorithm, EntropyArray};
    ///
    /// let seed = Seed::new(Entropy::Array([0; 16]), &Algorithm::Secp256k1);
    ///
    /// assert_eq!(seed.as_entropy(), &[0; 16]);
    /// assert_eq!(seed.as_entropy(), <Seed as AsRef<EntropyArray>>::as_ref(&seed));
    /// ```
    ///
    /// # Traits
    ///
    /// This method is used in [`AsRef`] trait.
    pub fn as_entropy(&self) -> &EntropyArray {
        &self.entropy
    }

    /// Seed as [`Algorithm`]
    ///
    /// # Examples
    /// ```
    /// use ripple_keypairs::{Seed, Entropy, Algorithm};
    ///
    /// let seed = Seed::new(Entropy::Random, &Algorithm::Ed25519);
    ///
    /// assert_eq!(seed.as_kind(), &Algorithm::Ed25519);
    /// assert_eq!(seed.as_kind(), <Seed as AsRef<Algorithm>>::as_ref(&seed));
    /// ```
    ///
    /// # Traits
    ///
    /// This method is used in [`AsRef`] trait.
    pub fn as_kind(&self) -> &Algorithm {
        self.kind
    }

    fn method(&self) -> &'static dyn alg::Seed {
        match self.kind {
            Secp256k1 => &alg::secp256k1::SeedEcDsaSecP256K1,
            Ed25519 => &alg::ed25519::SeedEd25519,
        }
    }
}

impl fmt::Display for Seed {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.method().encode(&self.entropy))
    }
}

impl FromStr for Seed {
    type Err = error::Error;

    fn from_str(s: &str) -> error::Result<Self> {
        let (entropy, kind) = codec::decode_seed(s).map_err(|_| error::DecodeError)?;

        Ok(Self::new(Array(entropy), kind))
    }
}

impl AsRef<EntropyArray> for Seed {
    fn as_ref(&self) -> &EntropyArray {
        self.as_entropy()
    }
}

impl AsRef<Algorithm> for Seed {
    fn as_ref(&self) -> &Algorithm {
        self.as_kind()
    }
}

/// Signatures can be treated as bytes or as hex encoded strings.
pub trait Signature: AsRef<[u8]> + AsRef<str> + ToString + Into<Vec<u8>> {}

impl Signature for HexBytes {}

/// A private key that can be used to sign messages
///
/// # Examples
///
/// ```
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// #
/// use ripple_keypairs::Seed;
///
/// let seed = "sp5fghtJtpUorTwvof1NpDXAzNwf5".parse::<Seed>()?;
///
/// let (private_key, _) = seed.derive_keypair()?;
///
/// let signature = private_key.sign(&"test message");
///
/// assert_eq!(signature.to_string(), "30440220583A91C95E54E6A651C47BEC22744E0B101E2C4060E7B08F6341657DAD9BC3EE02207D1489C7395DB0188D3A56A977ECBA54B36FA9371B40319655B1B4429E33EF2D");
/// assert_eq!(signature.into(), 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]);
///
/// assert_eq!(private_key.to_string(), "00D78B9735C3F26501C7337B8A5727FD53A6EFDBC6AA55984F098488561F985E23");
/// #
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PrivateKey {
    bytes: Vec<u8>,
    kind: &'static Algorithm,
}

impl PrivateKey {
    /// Sign message
    ///
    /// Returns the [`Signature`] which that can be treated
    /// as bytes or as a hex encoded string.
    pub fn sign(&self, message: &impl AsRef<[u8]>) -> impl Signature {
        self.method()
            .sign(message.as_ref(), &self.method().as_bytes(&self.bytes))
    }

    fn method(&self) -> &'static dyn alg::Sign {
        match self.kind {
            Secp256k1 => &alg::secp256k1::PrivateKeyEcDsaSecP256K1,
            Ed25519 => &alg::ed25519::PrivateKeyEd25519,
        }
    }
}

impl fmt::Display for PrivateKey {
    /// Display as a hex encoded string
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            self.method()
                .encode_to_hex(&self.method().as_bytes(&self.bytes))
        )
    }
}

/// A public key that can be used to derive an XRP Ledger classic address and verify signatures
///
/// # Examples
///
/// ```
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// #
/// use ripple_keypairs::{Seed, error};
///
/// let seed = "sp5fghtJtpUorTwvof1NpDXAzNwf5".parse::<Seed>()?;
///
/// let (private_key, public_key) = seed.derive_keypair()?;
///
/// assert_eq!(public_key.derive_address(), "rU6K7V3Po4snVhBBaU29sesqs2qTQJWDw1");
///
/// assert_eq!(public_key.to_string(), "030D58EB48B4420B1F7B9DF55087E0E29FEF0E8468F9A6825B01CA2C361042D435");
///
/// let msg = "Test message";
///
/// assert_eq!(public_key.verify(&msg, &private_key.sign(&msg)), Ok(()));
/// assert_eq!(public_key.verify(&msg, &"bad signature"), Err(error::InvalidSignature));
/// #
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PublicKey {
    bytes: Vec<u8>,
    kind: &'static Algorithm,
}

impl PublicKey {
    /// Verify a signature
    ///
    /// # Errors
    ///
    /// Returns [`error::InvalidSignature`] if the signature is invalid.
    pub fn verify(
        &self,
        message: &impl AsRef<[u8]>,
        signature: &impl AsRef<[u8]>,
    ) -> error::Result<()> {
        self.method().verify(
            message.as_ref(),
            signature.as_ref(),
            &self.method().as_bytes(&self.bytes),
        )
    }

    /// Derive an XRP Ledger classic address
    pub fn derive_address(&self) -> String {
        let hex = HexBytes::from_hex_unchecked(&self.to_string());
        let hash: [u8; 20] = utils::hash160(&hex.as_bytes())[..20].try_into().unwrap();

        codec::encode_account_id(&hash)
    }

    fn method(&self) -> &'static dyn alg::Verify {
        match self.kind {
            Secp256k1 => &alg::secp256k1::PublicKeyEcDsaSecP256K1,
            Ed25519 => &alg::ed25519::PublicKeyEd25519,
        }
    }
}

impl fmt::Display for PublicKey {
    /// Display as a hex encoded string
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            self.method()
                .encode_to_hex(&self.method().as_bytes(&self.bytes))
        )
    }
}