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
use crate::prelude::*;

use super::program::get_attestation_program;
use anchor_client::anchor_lang::Event;
use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
use base64::{engine::general_purpose, Engine as _};
use futures::{Future, StreamExt};
use sha2::{Digest, Sha256};
use solana_client::nonblocking::pubsub_client::PubsubClient;
use solana_client::nonblocking::rpc_client::RpcClient as NonblockingRpcClient;
use solana_client::rpc_config::RpcTransactionLogsConfig;
use solana_client::rpc_config::RpcTransactionLogsFilter;
use solana_sdk::client::SyncClient;
use solana_sdk::signer::keypair::{keypair_from_seed, read_keypair_file, Keypair};
use solana_sdk::signer::Signer;
use std::env;
use std::result::Result;
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::RwLock;
use std::fmt::Debug;

pub fn build_tx<A: ToAccountMetas, I: InstructionData + Discriminator>(
    anchor_client: &anchor_client::Client<std::sync::Arc<Keypair>>,
    program_id: &Pubkey,
    accounts: A,
    params: I,
    signers: Vec<&Keypair>,
) -> Result<Transaction, SbError> {
    let payer = signers[0];
    let ix = Instruction {
        program_id: *program_id,
        accounts: accounts.to_account_metas(None),
        data: params.data(),
    };
    let mut tx = Transaction::new_with_payer(&[ix], Some(&payer.pubkey()));
    let program = get_attestation_program(anchor_client)?;
    let blockhash = program.rpc().get_latest_blockhash().unwrap_or_default();
    tx.try_sign(&signers, blockhash)
        .map_err(|e| SbError::CustomError {
            message: "Failed to sign txn".into(),
            source: std::sync::Arc::new(e),
        })?;
    Ok(tx)
}

pub async fn get_async_rpc(
    client: &Arc<RwLock<AnchorClient>>,
) -> Result<Arc<NonblockingRpcClient>, SbError> {
    let client = client.clone();
    let ro_client = client.read().await;
    let program = get_attestation_program(&ro_client)?;
    let rpc = program.async_rpc();
    Ok(Arc::new(rpc))
}

pub fn ix_to_tx(
    ixs: &[Instruction],
    signers: &[&Keypair],
    blockhash: solana_program::hash::Hash,
) -> Result<Transaction, SbError> {
    let msg = Message::new(ixs, Some(&signers[0].pubkey()));
    let mut tx = Transaction::new_unsigned(msg);
    // for (i,s) in signers.iter().enumerate() {
    //     tx.try_sign(&signers.to_vec(), blockhash)
    //     .map_err(|e| SbError::CustomError { message: format!("Failed to sign txn", {}), source: std::sync::Arc::new(e) })?;
    // }
    tx.try_sign(&signers.to_vec(), blockhash)
        .map_err(|e| SbError::CustomError {
            message: "Failed to sign txn".into(),
            source: std::sync::Arc::new(e),
        })?;
    Ok(tx)
}

pub async fn get_enclave_signer_pubkey(
    enclave_signer: &Arc<RwLock<Keypair>>,
) -> Result<Arc<Pubkey>, SbError> {
    let enclave_signer = enclave_signer.clone();
    let ro_enclave_signer = enclave_signer.read().await;
    let pubkey = Arc::new(ro_enclave_signer.pubkey());
    Ok(pubkey)
}

pub fn load_env_pubkey(key: &str) -> Result<Pubkey, SbError> {
    Pubkey::from_str(&env::var(key).unwrap_or_default())
        .map_err(|_| SbError::EnvVariableMissing(key.to_string()))
}

/// Parse a string into an optional Pubkey. If the string is empty, return None.
pub fn parse_optional_pubkey(var: &str) -> Option<Pubkey> {
    if var.is_empty() {
        None
    } else {
        match Pubkey::from_str(var) {
            Ok(pubkey) => {
                if pubkey != Pubkey::default() {
                    Some(pubkey)
                } else {
                    None
                }
            }
            Err(_) => None,
        }
    }
}

/// Generates a keypair from a base seed, secret key, optional additional bytes, and an optional program ID.
///
/// # Arguments
///
/// * `base` - The base seed as a string.
/// * `secret_key` - The secret key as a vector of bytes.
/// * `more_bytes` - Optional additional bytes to include in the seed.
/// * `program_id` - Optional program ID to include in the seed.
///
/// # Returns
///
/// Returns a `Result` containing an `Arc<Keypair>` if the keypair is successfully derived, or an `SbError` if there is an error.
///
/// # Errors
///
/// Returns an `SbError` with the message "InvalidSecretKey" if the length of the secret key is not 32 bytes.
/// Returns an `SbError` with the message "Failed to derive keypair" if there is an error deriving the keypair.
///
/// # Example
///
/// ```rust
/// use solana_sdk::pubkey::Pubkey;
/// use solana_sdk::signature::{Keypair, keypair_from_seed};
/// use solana_sdk::hash::Hash;
/// use sha2::{Digest, Sha256};
/// use std::sync::Arc;
/// use switchboard_solana::SbError;
///
/// let base = "base_seed";
/// let secret_key = vec![0; 32];
/// let more_bytes = Some(vec![1, 2, 3]);
/// let program_id = Some(Pubkey::new_unique());
///
/// let result = switchboard_solana::client::utils::keypair_from_base_seed(base, secret_key, more_bytes, program_id);
/// match result {
///     Ok(keypair) => {
///         // Key pair successfully derived
///         println!("Derived keypair: {:?}", keypair);
///     }
///     Err(error) => {
///         // Error deriving key pair
///         println!("Failed to derive keypair: {:?}", error);
///     }
/// }
/// ```
pub fn keypair_from_base_seed(
    base: &str,
    secret_key: Vec<u8>,
    more_bytes: Option<Vec<u8>>,
    program_id: Option<Pubkey>,
) -> Result<Arc<Keypair>, SbError> {
    if secret_key.len() != 32 {
        return Err(SbError::Message("InvalidSecretKey"));
    }

    let mut seed = base.as_bytes().to_vec();
    seed.extend_from_slice(&secret_key);

    if let Some(bytes) = more_bytes.as_ref() {
        seed.extend_from_slice(bytes);
    }

    // Optionally, allow the progam ID to be included in the bytes so we
    // can create new environments on different program IDs without collisions.
    if let Some(program_id) = program_id.as_ref() {
        seed.extend_from_slice(&program_id.try_to_vec().unwrap_or_default());
    } else {
        seed.extend_from_slice(
            &*SWITCHBOARD_ATTESTATION_PROGRAM_ID
                .try_to_vec()
                .unwrap_or_default(),
        );
    }

    match keypair_from_seed(&Sha256::digest(&seed)) {
        Ok(keypair) => Ok(Arc::new(keypair)),
        Err(e) => {
            if let Some(err) = e.source() {
                println!("Failed to derive keypair -- {}", err);
            }

            Err(SbError::Message("Failed to derive keypair"))
        }
    }
}

/// Creates a signing keypair generated from randomness sourced from the enclave
/// runtime.
pub fn generate_signer() -> Arc<Keypair> {
    let mut randomness = [0; 32];
    switchboard_common::Gramine::read_rand(&mut randomness).unwrap();
    Arc::new(keypair_from_seed(&randomness).unwrap())
}

pub fn signer_to_pubkey(signer: Arc<Keypair>) -> std::result::Result<Pubkey, SbError> {
    Ok(signer.pubkey())
}

pub fn load_keypair_fs(fs_path: &str) -> Result<Arc<Keypair>, SbError> {
    match read_keypair_file(fs_path) {
        Ok(keypair) => Ok(Arc::new(keypair)),
        Err(e) => {
            if let Some(err) = e.source() {
                println!("Failed to read keypair file -- {}", err);
            }

            Err(SbError::Message("Failed to read keypair file"))
        }
    }
}

/// Fetches a zero-copy account from the Solana blockchain.
///
/// # Arguments
///
/// * `client` - The Solana RPC client used to interact with the blockchain.
/// * `pubkey` - The public key of the account to fetch.
///
/// # Returns
///
/// Returns a result containing the fetched account data as the specified type `T`, or an `SbError` if an error occurs.
///
/// # Errors
///
/// This function can return the following errors:
///
/// * `SbError::AccountNotFound` - If the account with the specified public key is not found.
/// * `SbError::Message("no discriminator found")` - If no discriminator is found in the account data.
/// * `SbError::Message("Discriminator error, check the account type")` - If the discriminator in the account data does not match the expected discriminator for type `T`.
/// * `SbError::Message("AnchorParseError")` - If an error occurs while parsing the account data into type `T`.
pub fn fetch_zerocopy_account<T: bytemuck::Pod + Discriminator + Owner>(
    client: &solana_client::rpc_client::RpcClient,
    pubkey: Pubkey,
) -> Result<T, SbError> {
    let data = client
        .get_account_data(&pubkey)
        .map_err(|_| SbError::AccountNotFound)?;

    if data.len() < T::discriminator().len() {
        return Err(SbError::Message("no discriminator found"));
    }

    let mut disc_bytes = [0u8; 8];
    disc_bytes.copy_from_slice(&data[..8]);
    if disc_bytes != T::discriminator() {
        return Err(SbError::Message(
            "Discriminator error, check the account type",
        ));
    }

    Ok(*bytemuck::try_from_bytes::<T>(&data[8..])
        .map_err(|_| SbError::Message("AnchorParseError"))?)
}

/// Fetches the account data synchronously from the Solana blockchain using the provided client.
///
/// # Arguments
///
/// * `client` - The client used to interact with the Solana blockchain.
/// * `pubkey` - The public key of the account to fetch.
///
/// # Generic Parameters
///
/// * `C` - The type of the client, which must implement the `SyncClient` trait.
/// * `T` - The type of the account data, which must implement the `bytemuck::Pod`, `Discriminator`, and `Owner` traits.
///
/// # Returns
///
/// Returns a `Result` containing the fetched account data of type `T` if successful, or an `SbError` if an error occurs.
pub fn fetch_zerocopy_account_sync<C: SyncClient, T: bytemuck::Pod + Discriminator + Owner>(
    client: &C,
    pubkey: Pubkey,
) -> Result<T, SbError> {
    let data = client
        .get_account_data(&pubkey)
        .map_err(|_| SbError::AccountNotFound)?
        .ok_or(SbError::AccountNotFound)?;

    if data.len() < T::discriminator().len() {
        return Err(SbError::Message("no discriminator found"));
    }

    let mut disc_bytes = [0u8; 8];
    disc_bytes.copy_from_slice(&data[..8]);
    if disc_bytes != T::discriminator() {
        return Err(SbError::Message(
            "Discriminator error, check the account type",
        ));
    }

    Ok(*bytemuck::try_from_bytes::<T>(&data[8..])
        .map_err(|_| SbError::Message("AnchorParseError"))?)
}

/// Fetches an account asynchronously using the provided client and public key.
///
/// # Arguments
///
/// * `client` - The non-blocking RPC client used to fetch the account.
/// * `pubkey` - The public key of the account to fetch.
///
/// # Generic Parameters
///
/// * `T` - The type of the account data. Must implement `bytemuck::Pod`, `Discriminator`, and `Owner`.
///
/// # Returns
///
/// Returns a `Result` containing the fetched account data of type `T` if successful, or an `SbError` if an error occurs.
///
/// # Errors
///
/// This function can return the following errors:
///
/// * `SbError::AccountNotFound` - If the account is not found.
/// * `SbError::Message("no discriminator found")` - If no discriminator is found in the account data.
/// * `SbError::Message("Discriminator error, check the account type")` - If the discriminator does not match the expected value.
/// * `SbError::Message("AnchorParseError")` - If there is an error parsing the account data into type `T`.
///
/// # Example
///
/// ```rust
/// use switchboard_solana::client::NonblockingRpcClient;
/// use switchboard_solana::error::SbError;
/// use switchboard_solana::types::{Discriminator, Owner};
/// use bytemuck::Pod;
/// use solana_sdk::pubkey::Pubkey;
///
/// async fn example(client: &NonblockingRpcClient, pubkey: Pubkey) -> Result<(), SbError> {
///     let account_data: MyAccountType = fetch_zerocopy_account_async(client, pubkey).await?;
///     // Do something with the fetched account data...
///     Ok(())
/// }
/// ```
pub async fn fetch_zerocopy_account_async<T: bytemuck::Pod + Discriminator + Owner>(
    client: &NonblockingRpcClient,
    pubkey: Pubkey,
) -> Result<T, SbError> {
    let data = client
        .get_account_data(&pubkey)
        .await
        .map_err(|_| SbError::AccountNotFound)?;

    if data.len() < T::discriminator().len() {
        return Err(SbError::Message("no discriminator found"));
    }

    let mut disc_bytes = [0u8; 8];
    disc_bytes.copy_from_slice(&data[..8]);
    if disc_bytes != T::discriminator() {
        return Err(SbError::Message(
            "Discriminator error, check the account type",
        ));
    }

    Ok(*bytemuck::try_from_bytes::<T>(&data[8..])
        .map_err(|_| SbError::Message("AnchorParseError"))?)
}

pub fn fetch_borsh_account<T: Discriminator + Owner + AccountDeserialize>(
    client: &solana_client::rpc_client::RpcClient,
    pubkey: Pubkey,
) -> Result<T, SbError> {
    let account_data = client
        .get_account_data(&pubkey)
        .map_err(|_| SbError::AccountNotFound)?;

    T::try_deserialize(&mut account_data.as_slice())
        .map_err(|_| SbError::Message("AnchorParseError"))
}

pub async fn fetch_borsh_account_async<T: Discriminator + Owner + AccountDeserialize>(
    client: &NonblockingRpcClient,
    pubkey: Pubkey,
) -> Result<T, SbError> {
    let account_data = client
        .get_account_data(&pubkey)
        .await
        .map_err(|_| SbError::AccountNotFound)?;

    T::try_deserialize(&mut account_data.as_slice())
        .map_err(|_| SbError::Message("AnchorParseError"))
}

pub fn fetch_borsh_account_sync<C: SyncClient, T: Discriminator + Owner + AccountDeserialize>(
    client: &C,
    pubkey: Pubkey,
) -> Result<T, SbError> {
    let data = client
        .get_account_data(&pubkey)
        .map_err(|_| SbError::AccountNotFound)?
        .ok_or(SbError::AccountNotFound)?;

    T::try_deserialize(&mut data.as_slice()).map_err(|_| SbError::Message("AnchorParseError"))
}

pub async fn subscribe<E, F, T>(
    program_id: Pubkey,
    url: &str,
    client: Arc<RwLock<AnchorClient>>,
    quote_key: Arc<Pubkey>,
    enclave_key: Arc<RwLock<Keypair>>,
    payer: Arc<Keypair>,
    async_fn: F,
) where
    F: Fn(Arc<RwLock<AnchorClient>>, Arc<Pubkey>, Arc<RwLock<Keypair>>, Arc<Keypair>, E) -> T
        + Send
        + Sync
        + 'static,
    T: Future<Output = ()> + Send + 'static,
    E: Event,
{
    // TODO: This may pull events from other programs if targeted but the
    // request still goes through verification so not a fatal issue.
    let pubsub_client = PubsubClient::new(url).await.unwrap();
    loop {
        let (mut r, _handler) = pubsub_client
            .logs_subscribe(
                RpcTransactionLogsFilter::Mentions(vec![program_id.to_string()]),
                RpcTransactionLogsConfig {
                    commitment: Some(CommitmentConfig::processed()),
                },
            )
            .await
            .unwrap();
        while let Some(event) = r.next().await {
            let log: String = event.value.logs.join(" ");
            for w in log.split(' ') {
                let decoded = general_purpose::STANDARD.decode(w);
                if decoded.is_err() {
                    continue;
                }
                let decoded = decoded.unwrap();
                if decoded.len() < 8 {
                    continue;
                }
                if decoded[..8] != E::DISCRIMINATOR {
                    continue;
                }

                if let Ok(event) = E::try_from_slice(&decoded[8..]) {
                    async_fn(
                        client.clone(),
                        quote_key.clone(),
                        enclave_key.clone(),
                        payer.clone(),
                        event,
                    )
                    .await;
                }
            }
        }
    }
}

pub async fn subscribe_ro<E, F, T>(program_id: Pubkey, url: &str, f: F)
where
    F: Fn(E) -> T + Send + Sync + 'static,
    T: Future<Output = ()> + Send + 'static,
    E: Event,
{
    // TODO: This may pull events from other programs if targeted but the
    // request still goes through verification so not a fatal issue.
    loop {
        let pubsub_client = PubsubClient::new(url).await.unwrap();
        let res = pubsub_client
            .logs_subscribe(
                RpcTransactionLogsFilter::Mentions(vec![program_id.to_string()]),
                RpcTransactionLogsConfig {
                    commitment: Some(CommitmentConfig::processed()),
                },
            )
            .await;
        if res.is_err() {
            println!("ERROR Subscription failure");
            continue;
        }
        let (mut r, _handler) = res.unwrap();
        // let mut ctxs: Vec<String> = vec![];
        while let Some(event) = r.next().await {
            for line in event.value.logs {
                // if let Some(pid) = extract_program_enter(&line) {
                // ctxs.push(Pubkey::from_str(&pid).unwrap());
                // }
                // if let Some(_pid) = extract_program_exit(&line) {
                // ctxs.pop();
                // }
                // if ctxs.last() != Some(&program_id) {
                // continue;
                // }
                for w in line.split(' ') {
                    let decoded = general_purpose::STANDARD.decode(w);
                    if decoded.is_err() {
                        continue;
                    }
                    let decoded = decoded.unwrap();
                    if decoded.len() < 8 {
                        continue;
                    }
                    if decoded[..8] != E::DISCRIMINATOR {
                        continue;
                    }
                    println!("DISCRIMINATOR_MATCH");
                    let event = E::try_from_slice(&decoded[8..]);
                    if event.is_ok() {
                        println!("EVENT_PARSE_SUCCESS");
                        f(event.unwrap()).await;
                    } else {
                        println!("EVENT_PARSE_FAILURE");
                    }
                }
            }
        }
    }
}

type GenericError = Box<dyn std::error::Error + Send + Sync>;

pub async fn subscribe_v2<E, F, EF, T>(subcription_url: &str, f: F, on_error: EF) -> Result<(), GenericError>
where
    F: Fn(E) -> T + Copy + Send + Sync + 'static,
    EF: Fn(GenericError) + Copy + Send + Sync + 'static,
    T: Future<Output = Result<(), GenericError>> + Send,
    E: Event + Send + Sync + Clone + Debug + 'static,
{
    let program_id = SWITCHBOARD_PROGRAM_ID;
    let mut discriminator: String = general_purpose::STANDARD
        .encode(E::DISCRIMINATOR)
        .replace("=", "");
    discriminator.pop();
    discriminator.pop();
    let prefix = "Program data: ".to_owned() + discriminator.as_str();
    // TODO: This may pull events from other programs if targeted but the
    // request still goes through verification so not a fatal issue.
    loop {
        println!("Subscribing on {}", subcription_url);
        let pubsub_client = PubsubClient::new(subcription_url).await.unwrap();
        let res = pubsub_client
            .logs_subscribe(
                RpcTransactionLogsFilter::Mentions(vec![program_id.to_string()]),
                RpcTransactionLogsConfig {
                    commitment: Some(CommitmentConfig::processed()),
                },
            )
            .await;
        if res.is_err() {
            println!("Subscription Failure");
            continue;
        }
        let (mut r, _handler) = res.unwrap();
        while let Some(event) = r.next().await {
            let filtered = event
                .value
                .logs
                .into_iter()
                .filter(|s| s.starts_with(&prefix))
                .filter_map(|s| general_purpose::STANDARD.decode(&s[14..]).ok())
                .map(|b| b.to_vec());
            for decoded in filtered {
                if let Ok(event) = E::try_from_slice(&decoded[8..]) {
                    tokio::spawn(async move {
                        if let Err(err) = f(event.clone()).await {
                            on_error(err);
                            // emit_metric!(UNCAUGHT_ERROR_COUNTER).inc();
                            // error!("UncaughtError", {
                                // error: err.to_string(),
                                // event: format!("{:?}", event),
                            // });
                        }
                    });
                }
            }
        }
    }
}