aptos-sdk 0.4.1

A user-friendly, idiomatic Rust SDK for the Aptos blockchain
Documentation
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Ed25519 account implementations.
//!
//! This module provides two Ed25519 account types:
//!
//! - [`Ed25519Account`]: Uses the legacy Ed25519 authenticator (scheme 0).
//!   This is the most common account type and is backwards compatible.
//!
//! - [`Ed25519SingleKeyAccount`]: Uses the modern `SingleKey` authenticator (scheme 2).
//!   This format is more flexible and recommended for new implementations.
//!
//! **Note**: The two account types produce DIFFERENT addresses for the same private key
//! because they use different authentication key derivation schemes.

#[cfg(feature = "mnemonic")]
use crate::account::Mnemonic;
use crate::account::account::{Account, AuthenticationKey};
use crate::crypto::{
    ED25519_SCHEME, Ed25519PrivateKey, Ed25519PublicKey, SINGLE_KEY_SCHEME,
    derive_authentication_key,
};
use crate::error::AptosResult;
use crate::types::AccountAddress;
use std::fmt;

/// An Ed25519 account for signing transactions.
///
/// This is the most common account type on Aptos.
///
/// # Example
///
/// ```rust
/// use aptos_sdk::account::Ed25519Account;
///
/// // Generate a new random account
/// let account = Ed25519Account::generate();
/// println!("Address: {}", account.address());
/// ```
#[derive(Clone)]
pub struct Ed25519Account {
    private_key: Ed25519PrivateKey,
    public_key: Ed25519PublicKey,
    address: AccountAddress,
}

impl Ed25519Account {
    /// Generates a new random Ed25519 account.
    pub fn generate() -> Self {
        let private_key = Ed25519PrivateKey::generate();
        Self::from_private_key(private_key)
    }

    /// Creates an account from a private key.
    pub fn from_private_key(private_key: Ed25519PrivateKey) -> Self {
        let public_key = private_key.public_key();
        let address = public_key.to_address();
        Self {
            private_key,
            public_key,
            address,
        }
    }

    /// Creates an account from private key bytes.
    ///
    /// # Errors
    ///
    /// Returns an error if the bytes are not a valid Ed25519 private key (must be exactly 32 bytes).
    pub fn from_private_key_bytes(bytes: &[u8]) -> AptosResult<Self> {
        let private_key = Ed25519PrivateKey::from_bytes(bytes)?;
        Ok(Self::from_private_key(private_key))
    }

    /// Creates an account from a private key hex string.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The hex string is invalid or cannot be decoded
    /// - The decoded bytes are not a valid Ed25519 private key
    pub fn from_private_key_hex(hex_str: &str) -> AptosResult<Self> {
        let private_key = Ed25519PrivateKey::from_hex(hex_str)?;
        Ok(Self::from_private_key(private_key))
    }

    /// Creates an account from a BIP-39 mnemonic phrase.
    ///
    /// Uses the standard Aptos derivation path: `m/44'/637'/0'/0'/index'`
    ///
    /// # Arguments
    ///
    /// * `mnemonic` - A BIP-39 mnemonic phrase (12, 15, 18, 21, or 24 words)
    /// * `index` - The account index in the derivation path
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use aptos_sdk::account::Ed25519Account;
    ///
    /// let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
    /// let account = Ed25519Account::from_mnemonic(mnemonic, 0).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the mnemonic phrase is invalid or if key derivation fails.
    #[cfg(feature = "mnemonic")]
    pub fn from_mnemonic(mnemonic: &str, index: u32) -> AptosResult<Self> {
        let mnemonic = Mnemonic::from_phrase(mnemonic)?;
        let private_key = mnemonic.derive_ed25519_key(index)?;
        Ok(Self::from_private_key(private_key))
    }

    /// Generates a new account with a random mnemonic.
    ///
    /// Returns both the account and the mnemonic phrase (for backup).
    ///
    /// # Errors
    ///
    /// Returns an error if mnemonic generation or key derivation fails.
    #[cfg(feature = "mnemonic")]
    pub fn generate_with_mnemonic() -> AptosResult<(Self, String)> {
        let mnemonic = Mnemonic::generate(24)?;
        let phrase = mnemonic.phrase().to_string();
        let private_key = mnemonic.derive_ed25519_key(0)?;
        let account = Self::from_private_key(private_key);
        Ok((account, phrase))
    }

    /// Returns the account address.
    pub fn address(&self) -> AccountAddress {
        self.address
    }

    /// Returns the public key.
    pub fn public_key(&self) -> &Ed25519PublicKey {
        &self.public_key
    }

    /// Returns a reference to the private key.
    ///
    /// **Warning**: Handle with care to avoid leaking sensitive key material.
    pub fn private_key(&self) -> &Ed25519PrivateKey {
        &self.private_key
    }

    /// Signs a message and returns the Ed25519 signature.
    pub fn sign_message(&self, message: &[u8]) -> crate::crypto::Ed25519Signature {
        self.private_key.sign(message)
    }
}

impl Account for Ed25519Account {
    fn address(&self) -> AccountAddress {
        self.address
    }

    fn authentication_key(&self) -> AuthenticationKey {
        AuthenticationKey::new(self.public_key.to_authentication_key())
    }

    fn sign(&self, message: &[u8]) -> AptosResult<Vec<u8>> {
        Ok(self.private_key.sign(message).to_bytes().to_vec())
    }

    fn public_key_bytes(&self) -> Vec<u8> {
        self.public_key.to_bytes().to_vec()
    }

    fn signature_scheme(&self) -> u8 {
        ED25519_SCHEME
    }
}

impl fmt::Debug for Ed25519Account {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Ed25519Account")
            .field("address", &self.address)
            .field("public_key", &self.public_key)
            .finish_non_exhaustive()
    }
}

/// An Ed25519 account using the modern `SingleKey` authenticator format.
///
/// This account type uses the `SingleSender` > `SingleKey` > `AnyPublicKey::Ed25519`
/// authenticator path, which is the modern unified format recommended for new
/// implementations.
///
/// **Note**: This produces a DIFFERENT address than [`Ed25519Account`] for the
/// same private key because it uses scheme ID 2 instead of 0.
///
/// # Authentication Key Derivation
///
/// The authentication key is derived as:
/// ```text
/// auth_key = SHA3-256(BCS(AnyPublicKey::Ed25519) || 0x02)
/// ```
///
/// Where `BCS(AnyPublicKey::Ed25519) = 0x00 || ULEB128(32) || public_key_bytes`
///
/// # Example
///
/// ```rust
/// use aptos_sdk::account::Ed25519SingleKeyAccount;
///
/// // Generate a new random account
/// let account = Ed25519SingleKeyAccount::generate();
/// println!("Address: {}", account.address());
/// ```
#[derive(Clone)]
pub struct Ed25519SingleKeyAccount {
    private_key: Ed25519PrivateKey,
    public_key: Ed25519PublicKey,
    address: AccountAddress,
}

impl Ed25519SingleKeyAccount {
    /// Generates a new random Ed25519 `SingleKey` account.
    pub fn generate() -> Self {
        let private_key = Ed25519PrivateKey::generate();
        Self::from_private_key(private_key)
    }

    /// Creates an account from a private key.
    pub fn from_private_key(private_key: Ed25519PrivateKey) -> Self {
        let public_key = private_key.public_key();
        let address = Self::derive_address(&public_key);
        Self {
            private_key,
            public_key,
            address,
        }
    }

    /// Creates an account from private key bytes.
    ///
    /// # Errors
    ///
    /// Returns an error if the bytes are not a valid Ed25519 private key (must be exactly 32 bytes).
    pub fn from_private_key_bytes(bytes: &[u8]) -> AptosResult<Self> {
        let private_key = Ed25519PrivateKey::from_bytes(bytes)?;
        Ok(Self::from_private_key(private_key))
    }

    /// Creates an account from a private key hex string.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The hex string is invalid or cannot be decoded
    /// - The decoded bytes are not a valid Ed25519 private key
    pub fn from_private_key_hex(hex_str: &str) -> AptosResult<Self> {
        let private_key = Ed25519PrivateKey::from_hex(hex_str)?;
        Ok(Self::from_private_key(private_key))
    }

    /// Creates an account from a BIP-39 mnemonic phrase.
    ///
    /// Uses the standard Aptos derivation path: `m/44'/637'/0'/0'/index'`
    ///
    /// # Errors
    ///
    /// Returns an error if the mnemonic phrase is invalid or if key derivation fails.
    #[cfg(feature = "mnemonic")]
    pub fn from_mnemonic(mnemonic: &str, index: u32) -> AptosResult<Self> {
        let mnemonic = Mnemonic::from_phrase(mnemonic)?;
        let private_key = mnemonic.derive_ed25519_key(index)?;
        Ok(Self::from_private_key(private_key))
    }

    /// Returns the account address.
    pub fn address(&self) -> AccountAddress {
        self.address
    }

    /// Returns the public key.
    pub fn public_key(&self) -> &Ed25519PublicKey {
        &self.public_key
    }

    /// Returns a reference to the private key.
    pub fn private_key(&self) -> &Ed25519PrivateKey {
        &self.private_key
    }

    /// Signs a message and returns the Ed25519 signature.
    pub fn sign_message(&self, message: &[u8]) -> crate::crypto::Ed25519Signature {
        self.private_key.sign(message)
    }

    /// Derives the address for an Ed25519 public key using `SingleKey` scheme.
    fn derive_address(public_key: &Ed25519PublicKey) -> AccountAddress {
        // BCS format: variant_byte || ULEB128(length) || public_key_bytes
        let pk_bytes = public_key.to_bytes();
        let mut bcs_bytes = Vec::with_capacity(1 + 1 + pk_bytes.len());
        bcs_bytes.push(0x00); // Ed25519 variant
        bcs_bytes.push(32); // ULEB128(32) = 32 (since 32 < 128)
        bcs_bytes.extend_from_slice(&pk_bytes);
        let auth_key = derive_authentication_key(&bcs_bytes, SINGLE_KEY_SCHEME);
        AccountAddress::new(auth_key)
    }

    /// Returns the BCS-serialized public key bytes for `SingleKey` authenticator.
    ///
    /// Format: `0x00 || ULEB128(32) || public_key_bytes`
    fn bcs_public_key_bytes(&self) -> Vec<u8> {
        let pk_bytes = self.public_key.to_bytes();
        let mut bcs_bytes = Vec::with_capacity(1 + 1 + pk_bytes.len());
        bcs_bytes.push(0x00); // Ed25519 variant
        bcs_bytes.push(32); // ULEB128(32) = 32
        bcs_bytes.extend_from_slice(&pk_bytes);
        bcs_bytes
    }
}

impl Account for Ed25519SingleKeyAccount {
    fn address(&self) -> AccountAddress {
        self.address
    }

    fn authentication_key(&self) -> AuthenticationKey {
        let bcs_bytes = self.bcs_public_key_bytes();
        let key = derive_authentication_key(&bcs_bytes, SINGLE_KEY_SCHEME);
        AuthenticationKey::new(key)
    }

    fn sign(&self, message: &[u8]) -> AptosResult<Vec<u8>> {
        Ok(self.private_key.sign(message).to_bytes().to_vec())
    }

    fn public_key_bytes(&self) -> Vec<u8> {
        // Return BCS-serialized AnyPublicKey::Ed25519 format
        self.bcs_public_key_bytes()
    }

    fn signature_scheme(&self) -> u8 {
        SINGLE_KEY_SCHEME
    }
}

impl fmt::Debug for Ed25519SingleKeyAccount {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Ed25519SingleKeyAccount")
            .field("address", &self.address)
            .field("public_key", &self.public_key)
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate() {
        let account = Ed25519Account::generate();
        assert!(!account.address().is_zero());
    }

    #[test]
    #[cfg(feature = "mnemonic")]
    fn test_from_mnemonic() {
        // Standard test mnemonic
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let account = Ed25519Account::from_mnemonic(mnemonic, 0).unwrap();

        // Same mnemonic should produce same account
        let account2 = Ed25519Account::from_mnemonic(mnemonic, 0).unwrap();
        assert_eq!(account.address(), account2.address());

        // Different index should produce different account
        let account3 = Ed25519Account::from_mnemonic(mnemonic, 1).unwrap();
        assert_ne!(account.address(), account3.address());
    }

    #[test]
    fn test_sign_and_verify() {
        let account = Ed25519Account::generate();
        let message = b"hello world";

        let signature = account.sign_message(message);
        assert!(account.public_key().verify(message, &signature).is_ok());
    }

    #[test]
    #[cfg(feature = "mnemonic")]
    fn test_generate_with_mnemonic() {
        let (account, mnemonic) = Ed25519Account::generate_with_mnemonic().unwrap();

        // Should be able to restore from the mnemonic
        let restored = Ed25519Account::from_mnemonic(&mnemonic, 0).unwrap();
        assert_eq!(account.address(), restored.address());
    }

    #[test]
    fn test_from_private_key() {
        let original = Ed25519Account::generate();
        let private_key = original.private_key().clone();
        let restored = Ed25519Account::from_private_key(private_key);
        assert_eq!(original.address(), restored.address());
    }

    #[test]
    fn test_from_private_key_bytes() {
        let original = Ed25519Account::generate();
        let bytes = original.private_key().to_bytes();
        let restored = Ed25519Account::from_private_key_bytes(&bytes).unwrap();
        assert_eq!(original.address(), restored.address());
    }

    #[test]
    fn test_from_private_key_hex() {
        let original = Ed25519Account::generate();
        let hex = original.private_key().to_hex();
        let restored = Ed25519Account::from_private_key_hex(&hex).unwrap();
        assert_eq!(original.address(), restored.address());
    }

    #[test]
    fn test_authentication_key() {
        let account = Ed25519Account::generate();
        let auth_key = account.authentication_key();
        assert_eq!(auth_key.as_bytes().len(), 32);
    }

    #[test]
    fn test_public_key_bytes() {
        let account = Ed25519Account::generate();
        let bytes = account.public_key_bytes();
        assert_eq!(bytes.len(), 32);
    }

    #[test]
    fn test_signature_scheme() {
        let account = Ed25519Account::generate();
        assert_eq!(account.signature_scheme(), ED25519_SCHEME);
    }

    #[test]
    fn test_sign_trait() {
        let account = Ed25519Account::generate();
        let message = b"test message";
        let sig_bytes = account.sign(message).unwrap();
        assert_eq!(sig_bytes.len(), 64);
    }

    #[test]
    fn test_debug_output() {
        let account = Ed25519Account::generate();
        let debug = format!("{account:?}");
        assert!(debug.contains("Ed25519Account"));
        assert!(debug.contains("address"));
    }

    #[test]
    fn test_invalid_private_key_bytes() {
        let result = Ed25519Account::from_private_key_bytes(&[0u8; 16]);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_private_key_hex() {
        let result = Ed25519Account::from_private_key_hex("invalid");
        assert!(result.is_err());
    }

    #[test]
    #[cfg(feature = "mnemonic")]
    fn test_invalid_mnemonic() {
        let result = Ed25519Account::from_mnemonic("invalid mnemonic phrase", 0);
        assert!(result.is_err());
    }

    // Ed25519SingleKeyAccount tests

    #[test]
    fn test_single_key_generate() {
        let account = Ed25519SingleKeyAccount::generate();
        assert!(!account.address().is_zero());
    }

    #[test]
    fn test_single_key_different_address() {
        // Same private key should produce different addresses for Ed25519Account vs Ed25519SingleKeyAccount
        let legacy_account = Ed25519Account::generate();
        let private_key = legacy_account.private_key().clone();

        let single_key_account = Ed25519SingleKeyAccount::from_private_key(private_key);

        // Addresses should be DIFFERENT because they use different scheme IDs
        assert_ne!(legacy_account.address(), single_key_account.address());
    }

    #[test]
    fn test_single_key_sign_and_verify() {
        let account = Ed25519SingleKeyAccount::generate();
        let message = b"hello world";

        let signature = account.sign_message(message);
        assert!(account.public_key().verify(message, &signature).is_ok());
    }

    #[test]
    fn test_single_key_from_private_key() {
        let original = Ed25519SingleKeyAccount::generate();
        let private_key = original.private_key().clone();
        let restored = Ed25519SingleKeyAccount::from_private_key(private_key);
        assert_eq!(original.address(), restored.address());
    }

    #[test]
    fn test_single_key_from_private_key_bytes() {
        let original = Ed25519SingleKeyAccount::generate();
        let bytes = original.private_key().to_bytes();
        let restored = Ed25519SingleKeyAccount::from_private_key_bytes(&bytes).unwrap();
        assert_eq!(original.address(), restored.address());
    }

    #[test]
    fn test_single_key_from_private_key_hex() {
        let original = Ed25519SingleKeyAccount::generate();
        let hex = original.private_key().to_hex();
        let restored = Ed25519SingleKeyAccount::from_private_key_hex(&hex).unwrap();
        assert_eq!(original.address(), restored.address());
    }

    #[test]
    fn test_single_key_authentication_key() {
        let account = Ed25519SingleKeyAccount::generate();
        let auth_key = account.authentication_key();
        assert_eq!(auth_key.as_bytes().len(), 32);
    }

    #[test]
    fn test_single_key_public_key_bytes() {
        let account = Ed25519SingleKeyAccount::generate();
        let bytes = account.public_key_bytes();
        // BCS format: variant (1) + length (1) + pubkey (32) = 34 bytes
        assert_eq!(bytes.len(), 34);
        assert_eq!(bytes[0], 0x00); // Ed25519 variant
        assert_eq!(bytes[1], 32); // ULEB128(32)
    }

    #[test]
    fn test_single_key_signature_scheme() {
        let account = Ed25519SingleKeyAccount::generate();
        assert_eq!(account.signature_scheme(), SINGLE_KEY_SCHEME);
    }

    #[test]
    fn test_single_key_sign_trait() {
        let account = Ed25519SingleKeyAccount::generate();
        let message = b"test message";
        let sig_bytes = account.sign(message).unwrap();
        assert_eq!(sig_bytes.len(), 64);
    }

    #[test]
    fn test_single_key_debug_output() {
        let account = Ed25519SingleKeyAccount::generate();
        let debug = format!("{account:?}");
        assert!(debug.contains("Ed25519SingleKeyAccount"));
        assert!(debug.contains("address"));
    }

    #[test]
    #[cfg(feature = "mnemonic")]
    fn test_single_key_from_mnemonic() {
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
        let account = Ed25519SingleKeyAccount::from_mnemonic(mnemonic, 0).unwrap();

        // Same mnemonic should produce same account
        let account2 = Ed25519SingleKeyAccount::from_mnemonic(mnemonic, 0).unwrap();
        assert_eq!(account.address(), account2.address());

        // Different index should produce different account
        let account3 = Ed25519SingleKeyAccount::from_mnemonic(mnemonic, 1).unwrap();
        assert_ne!(account.address(), account3.address());
    }

    #[test]
    #[cfg(feature = "mnemonic")]
    fn test_single_key_vs_legacy_mnemonic() {
        let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";

        let legacy = Ed25519Account::from_mnemonic(mnemonic, 0).unwrap();
        let single_key = Ed25519SingleKeyAccount::from_mnemonic(mnemonic, 0).unwrap();

        // Same mnemonic, same private key, but DIFFERENT addresses
        assert_eq!(
            legacy.private_key().to_bytes(),
            single_key.private_key().to_bytes()
        );
        assert_ne!(legacy.address(), single_key.address());
    }
}