stellar-baselib 0.5.7

A low level Rust library that offers a comprehensive set of functions for reading, writing, hashing, and signing primitive XDR constructs utilized in the Stellar network
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
use std::str::FromStr;

use crate::xdr;
use stellar_strkey::{
    ed25519::{self, MuxedAccount, PublicKey},
    Contract, Strkey,
};

use crate::hashing::{self, HashingBehavior};

#[derive(Debug)]
pub enum AddressType {
    Account,
    Contract,
    MuxedAccount,
}

#[derive(Debug)]
pub struct Address {
    address_type: AddressType,
    key: Vec<u8>,
}

pub trait AddressTrait {
    /// Creates a new Address instance from a string representation.
    fn new(address: &str) -> Result<Self, &'static str>
    where
        Self: Sized;

    /// Parses a string and returns an Address object.
    fn from_string(address: &str) -> Result<Self, &'static str>
    where
        Self: Sized;

    /// Creates a new account Address object from a buffer of raw bytes.
    fn account(buffer: &[u8]) -> Result<Self, &'static str>
    where
        Self: Sized;

    fn muxed_account(buffer: &[u8]) -> Result<Self, &'static str>
    where
        Self: Sized;

    /// Creates a new contract Address object from a buffer of raw bytes.
    fn contract(buffer: &[u8]) -> Result<Self, &'static str>
    where
        Self: Sized;

    /// Convert from an xdr.ScVal type.
    fn from_sc_val(sc_val: &xdr::ScVal) -> Result<Self, &'static str>
    where
        Self: Sized;

    /// Convert from an xdr.ScAddress type.
    fn from_sc_address(sc_address: &xdr::ScAddress) -> Result<Self, &'static str>
    where
        Self: Sized;

    /// Serialize an address to string.
    fn to_string(&self) -> String;

    /// Convert the Address to an xdr.ScVal type.
    fn to_sc_val(&self) -> Result<xdr::ScVal, &'static str>;

    /// Convert the Address to an xdr.ScAddress type.
    fn to_sc_address(&self) -> Result<xdr::ScAddress, &'static str>;

    /// Return the raw public key bytes for this address.
    fn to_buffer(&self) -> Vec<u8>;
}

impl AddressTrait for Address {
    fn new(address: &str) -> Result<Self, &'static str>
    where
        Self: Sized,
    {
        let value = match stellar_strkey::Strkey::from_string(address) {
            Ok(Strkey::PublicKeyEd25519(public_key)) => {
                (AddressType::Account, public_key.0.to_vec())
            }
            Ok(Strkey::Contract(contract)) => (AddressType::Contract, contract.0.to_vec()),
            Ok(Strkey::MuxedAccountEd25519(x)) => {
                let mut payload: [u8; 40] = [0; 40];
                let (ed25519, id) = payload.split_at_mut(32);
                ed25519.copy_from_slice(&x.ed25519);
                id.copy_from_slice(&x.id.to_be_bytes());
                (AddressType::MuxedAccount, payload.to_vec())
            }

            _ => return Err("Unsupported address type"),
        };

        Ok(Self {
            address_type: value.0,
            key: value.1,
        })
    }
    fn from_string(address: &str) -> Result<Self, &'static str>
    where
        Self: Sized,
    {
        Self::new(address)
    }

    fn account(buffer: &[u8]) -> Result<Self, &'static str>
    where
        Self: Sized,
    {
        let acc = Strkey::PublicKeyEd25519(PublicKey::from_payload(buffer).unwrap()).to_string();
        Self::new(&acc)
    }

    fn muxed_account(buffer: &[u8]) -> Result<Self, &'static str>
    where
        Self: Sized,
    {
        let acc =
            Strkey::MuxedAccountEd25519(MuxedAccount::from_payload(buffer).unwrap()).to_string();
        Self::new(&acc)
    }

    fn contract(buffer: &[u8]) -> Result<Self, &'static str>
    where
        Self: Sized,
    {
        Self::new(
            &Strkey::Contract(Contract(
                buffer.try_into().expect("Slice is not 32 bytes long"),
            ))
            .to_string(),
        )
    }

    fn from_sc_val(sc_val: &xdr::ScVal) -> Result<Self, &'static str>
    where
        Self: Sized,
    {
        let address_sc_val = match sc_val {
            xdr::ScVal::Address(sc_address) => sc_address,
            _ => panic!("Invalid Type"),
        };
        Self::from_sc_address(address_sc_val)
    }

    fn from_sc_address(sc_address: &xdr::ScAddress) -> Result<Self, &'static str>
    where
        Self: Sized,
    {
        match sc_address {
            xdr::ScAddress::Account(account_id) => {
                let xdr::PublicKey::PublicKeyTypeEd25519(m) = &account_id.0;

                Self::account(&m.0)
            }
            xdr::ScAddress::Contract(xdr::ContractId(hash)) => Self::contract(&hash.0),
            xdr::ScAddress::MuxedAccount(xdr::MuxedEd25519Account {
                id,
                ed25519: xdr::Uint256(edkey),
            }) => {
                let mut payload: [u8; 40] = [0; 40];
                let (key, keyid) = payload.split_at_mut(32);
                key.copy_from_slice(edkey);
                keyid.copy_from_slice(&id.to_be_bytes());
                Self::muxed_account(&payload)
            }
            _ => Err("Address type not supported"),
        }
    }

    fn to_string(&self) -> String {
        match &self.address_type {
            AddressType::Account => Strkey::PublicKeyEd25519(PublicKey(
                *self
                    .key
                    .last_chunk::<32>()
                    .expect("Public key is less than 32 bytes"),
            ))
            .to_string(),
            AddressType::Contract => {
                let id = self
                    .key
                    .last_chunk::<32>()
                    .expect("Contract key is less than 32 bytes");
                Strkey::Contract(Contract(*id)).to_string()
            }
            AddressType::MuxedAccount => {
                //

                let (ed25519, id) = self.key.split_at(32);
                let id = u64::from_be_bytes(
                    *id.last_chunk::<8>()
                        .expect("Muxed account id is less than 8 bytes"),
                );
                let ed25519 = *ed25519
                    .last_chunk::<32>()
                    .expect("Muxed account key is less than 32 bytes");

                Strkey::MuxedAccountEd25519(MuxedAccount { id, ed25519 }).to_string()
            }
        }
    }

    fn to_sc_val(&self) -> Result<xdr::ScVal, &'static str> {
        Ok(xdr::ScVal::Address(self.to_sc_address().unwrap()))
    }

    fn to_sc_address(&self) -> Result<xdr::ScAddress, &'static str> {
        match &self.address_type {
            AddressType::Account => {
                let k = *self.key.last_chunk::<32>().expect("");
                Ok(xdr::ScAddress::Account(xdr::AccountId(
                    xdr::PublicKey::PublicKeyTypeEd25519(xdr::Uint256(k)),
                )))
            }

            AddressType::Contract => {
                let original = self.key.last_chunk::<32>().unwrap();
                Ok(xdr::ScAddress::Contract(xdr::ContractId(xdr::Hash(
                    *original,
                ))))
            }
            AddressType::MuxedAccount => {
                let (ed25519, id) = self.key.split_at(32);
                let id = u64::from_be_bytes(
                    *id.last_chunk::<8>()
                        .expect("Muxed account id is less than 8 bytes"),
                );
                let ed25519 = *ed25519
                    .last_chunk::<32>()
                    .expect("Muxed account key is less than 32 bytes");

                Ok(xdr::ScAddress::MuxedAccount(xdr::MuxedEd25519Account {
                    id,
                    ed25519: xdr::Uint256(ed25519),
                }))
            }
        }
    }

    fn to_buffer(&self) -> Vec<u8> {
        self.key.clone()
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;

    const ACCOUNT: &str = "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB";
    const CONTRACT: &str = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE";
    const MUXED_ADDRESS: &str =
        "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK";

    #[test]
    fn test_invalid_address_creation() {
        let result = Address::new("GBBB");
        assert!(result.is_err(), "Should fail for invalid address");
    }

    #[test]
    fn test_account_address_creation() {
        let account = Address::new(ACCOUNT).expect("Should create account address");
        assert_eq!(account.to_string(), ACCOUNT);
    }

    #[test]
    fn test_contract_address_creation() {
        let contract = Address::new(CONTRACT).expect("Should create contract address");
        assert_eq!(contract.to_string(), CONTRACT);
    }

    #[test]
    fn test_muxed_account_creation() {
        const MUXED_ADDRESS: &str =
            "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK";

        let result = Address::new(MUXED_ADDRESS).expect("Should create a muxed account address");
        assert_eq!(result.to_string(), MUXED_ADDRESS);
    }

    #[test]
    fn test_from_string() {
        let account_address = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
        let address = Address::from_string(account_address).unwrap();
        assert_eq!(address.to_string(), account_address);
    }

    #[test]
    fn test_account_from_buffer() {
        let zero_buffer = vec![0; 32];
        let address = Address::account(&zero_buffer).unwrap();
        assert_eq!(
            address.to_string(),
            "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"
        );
    }

    #[test]
    fn test_contract_from_buffer() {
        let zero_buffer = vec![0; 32];
        let address = Address::contract(&zero_buffer).unwrap();
        assert_eq!(
            address.to_string(),
            "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4"
        );
    }

    #[test]
    fn creates_address_object_for_accounts() {
        let sc_address = xdr::ScAddress::from_str(ACCOUNT).unwrap();

        // Convert ScAddress to Address
        let account =
            Address::from_sc_address(&sc_address).expect("Failed to create Address from ScAddress");

        // Verify the string representation matches the original account
        assert_eq!(account.to_string(), ACCOUNT);
    }

    #[test]
    fn creates_address_object_for_contracts() {
        // Decode the contract address
        let contract = Contract::from_string(CONTRACT).expect("Failed to decode contract address");

        // Create ScAddress for contract
        let sc_address = xdr::ScAddress::Contract(xdr::ContractId(xdr::Hash(contract.0)));

        // Convert ScAddress to Address
        let contract_address =
            Address::from_sc_address(&sc_address).expect("Failed to create Address from ScAddress");

        // Verify the string representation matches the original contract address
        assert_eq!(contract_address.to_string(), CONTRACT);
    }
    #[test]
    fn creates_address_object_for_muxedaccounts() {
        let sc_address = xdr::ScAddress::from_str(MUXED_ADDRESS).unwrap();

        // Convert ScAddress to Address
        let account =
            Address::from_sc_address(&sc_address).expect("Failed to create Address from ScAddress");

        // Verify the string representation matches the original account
        assert_eq!(account.to_string(), MUXED_ADDRESS);
    }

    #[test]
    fn creates_address_object_for_accounts_sc_address() {
        // Decode the account public key

        let val = xdr::ScAddress::Account(xdr::AccountId::from_str(ACCOUNT).unwrap());
        // Create ScVal with an account address
        let sc_val = xdr::ScVal::Address(val);

        // Convert ScVal to Address
        let account = Address::from_sc_val(&sc_val).expect("Failed to create Address from ScVal");

        // Check that the toString() matches the original account
        assert_eq!(account.to_string(), ACCOUNT);
    }

    #[test]
    fn converts_accounts_to_sc_address() {
        // First, create an Address from the account string
        let address = Address::new(ACCOUNT).expect("Failed to create Address");

        // Convert to ScAddress
        let sc_address = address
            .to_sc_address()
            .expect("Failed to convert to ScAddress");

        // Check that the ScAddress is of the correct type
        match sc_address {
            xdr::ScAddress::Account(_) => {
                // Test passes if it's an Account type
            }
            _ => {
                panic!("Expected ScAddress to be an Account type")
            }
        }

        // To make this more similar to the JS test, we can also check the explicit type
        match sc_address {
            xdr::ScAddress::Account(_) => {
                assert_eq!(sc_address.discriminant(), xdr::ScAddressType::Account);
            }
            _ => panic!("Expected Account type ScAddress"),
        }
    }

    #[test]
    fn test_contract_to_sc_address() {
        // Create an Address from the contract address
        let address = Address::new(CONTRACT).expect("Failed to create Address");

        // Convert to ScAddress
        let sc_address = address
            .to_sc_address()
            .expect("Failed to convert to ScAddress");

        // Check that it's a contract type ScAddress
        match sc_address {
            xdr::ScAddress::Contract(_) => {
                // Test passes if it's a Contract type
            }
            _ => panic!("Expected ScAddress::Contract"),
        }
    }

    #[test]
    fn test_to_sc_val_for_account() {
        // Create an Address instance
        let address = Address::new(ACCOUNT).expect("Failed to create Address");

        // Convert the Address to ScVal
        let sc_val = address.to_sc_val().expect("Failed to convert to ScVal");

        // Ensure the ScVal is an Address type
        match sc_val {
            xdr::ScVal::Address(ref sc_address) => {
                // Convert the Address to ScAddress and compare
                let expected_sc_address = address
                    .to_sc_address()
                    .expect("Failed to convert to ScAddress");
                assert_eq!(sc_address, &expected_sc_address, "ScAddress mismatch");
            }
            _ => panic!("ScVal is not an Address"),
        }
    }
    #[test]
    fn test_to_sc_val_for_contract() {
        // Create an Address instance
        let address = Address::new(CONTRACT).expect("Failed to create Address");

        // Convert the Address to ScVal
        let sc_val = address.to_sc_val().expect("Failed to convert to ScVal");

        // Ensure the ScVal is an Address type
        match sc_val {
            xdr::ScVal::Address(ref sc_address) => {
                // Convert the Address to ScAddress and compare
                let expected_sc_address = address
                    .to_sc_address()
                    .expect("Failed to convert to ScAddress");
                assert_eq!(sc_address, &expected_sc_address, "ScAddress mismatch");
            }
            _ => panic!("ScVal is not an Address"),
        }
    }
    #[test]
    fn test_to_sc_val_for_muxedaccount() {
        // Create an Address instance
        let address = Address::new(MUXED_ADDRESS).expect("Failed to create Address");

        // Convert the Address to ScVal
        let sc_val = address.to_sc_val().expect("Failed to convert to ScVal");

        // Ensure the ScVal is an Address type
        match sc_val {
            xdr::ScVal::Address(ref sc_address) => {
                // Convert the Address to ScAddress and compare
                let expected_sc_address = address
                    .to_sc_address()
                    .expect("Failed to convert to ScAddress");
                assert_eq!(sc_address, &expected_sc_address, "ScAddress mismatch");
            }
            _ => panic!("ScVal is not an Address"),
        }
    }

    #[test]
    fn test_to_buffer_for_account() {
        // Create an Address instance for an account
        let address = Address::new(ACCOUNT).expect("Failed to create Address");

        // Convert the Address to raw public key bytes
        let buffer = address.to_buffer();

        // Decode the expected bytes using stellar_strkey
        let expected = match Strkey::from_string(ACCOUNT).expect("Invalid ACCOUNT address") {
            Strkey::PublicKeyEd25519(PublicKey(k)) => k.to_vec(),
            _ => panic!("Expected an Ed25519 public key"),
        };

        // Compare the buffers
        assert_eq!(buffer, expected, "Buffer for account does not match");
    }

    #[test]
    fn test_to_buffer_for_contract() {
        // Create an Address instance for a contract
        let address = Address::new(CONTRACT).expect("Failed to create Address");

        // Convert the Address to raw contract key bytes
        let buffer = address.to_buffer();

        // Decode the expected bytes using stellar_strkey
        let expected = match Strkey::from_string(CONTRACT).expect("Invalid CONTRACT address") {
            Strkey::Contract(contract) => contract.0.to_vec(),
            _ => panic!("Expected a contract key"),
        };

        // Compare the buffers
        assert_eq!(buffer, expected, "Buffer for contract does not match");
    }
    #[test]
    fn test_to_buffer_for_muxedaccount() {
        // Create an Address instance for an account
        let address = Address::new(MUXED_ADDRESS).expect("Failed to create Address");

        // Convert the Address to raw public key bytes
        let buffer = address.to_buffer();

        // Decode the expected bytes using stellar_strkey
        let expected =
            match Strkey::from_string(MUXED_ADDRESS).expect("Invalid MUXED ACCOUNT address") {
                Strkey::MuxedAccountEd25519(MuxedAccount { ed25519, id }) => {
                    let mut payload: [u8; 40] = [0; 40];
                    let (key, keyid) = payload.split_at_mut(32);
                    key.copy_from_slice(&ed25519);
                    keyid.copy_from_slice(&id.to_be_bytes());
                    payload
                }
                _ => panic!("Expected an Ed25519 public key"),
            };

        // Compare the buffers
        assert_eq!(buffer, expected, "Buffer for account does not match");
    }
}