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
//! `SignerContext` - task-local keypair isolation for secure on-chain operations.
//!
//! Implements the security boundary described in the `rig-onchain-kit`
//! documentation: every async on-chain call must be wrapped in
//! [`with_signer`], which scopes the active [`solana_sdk::signature::Keypair`]
//! to exactly the current Tokio task via `tokio::task_local!`.
//!
//! ## Why task-local storage?
//!
//! In an async runtime multiple trades can be in-flight simultaneously.
//! Using a global or thread-local signer would risk one task's keypair leaking
//! into another task's signing operation. `task_local!` gives each spawned
//! task its own isolated slot with no locking overhead.
//!
//! ## Production upgrade path
//!
//! Replace [`LocalSolanaSigner`] and the hand-rolled `task_local!` storage
//! with `rig_onchain_kit::signer::SignerContext` once that crate is published
//! to crates.io.
use Arc;
use Result;
use ;
use info;
use crateConfig;
// Task-local slot that holds the active signer for the current Tokio task.
// Declared with a regular line comment rather than a doc comment (///)
// because `tokio::task_local!` is a macro invocation - rustdoc cannot attach
// outer doc attributes to macro call sites, which would trigger the
// `unused_doc_comments` lint.
task_local!
/// A Solana keypair wrapper that loads its key from the process environment.
///
/// In production, decode the base-58 private key stored in
/// `SOLANA_PRIVATE_KEY` using `Keypair::from_base58_string`. In this demo a
/// fresh random keypair is generated regardless of whether the variable is set.
/// Execute an async closure with `signer` installed as the task-local signer.
///
/// Installs `signer` into the `CURRENT_SIGNER` task-local slot for the
/// duration of the future returned by `f`. The signer is automatically
/// removed when the scope exits, so it cannot outlive the operation it was
/// created for.
///
/// # Type parameters
///
/// * `F` - A [`FnOnce`] closure that produces a future.
/// * `Fut` - The [`Future`] returned by `F`.
/// * `T` - The `Ok` type of the future's output.
///
/// # Errors
///
/// Propagates any [`anyhow::Error`] returned by the closure `f`.
///
/// # Examples
///
/// ```rust,no_run
/// use hft_core::onchain::signer::{with_signer, LocalSolanaSigner};
///
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// let signer = LocalSolanaSigner::from_env();
/// let result = with_signer(signer, || async {
/// Ok::<&str, anyhow::Error>("swap executed")
/// }).await?;
/// # Ok(()) }
/// ```
pub async
/// Spawn three concurrent tasks and demonstrate that each has an independent
/// task-local signer.
///
/// Each task calls [`LocalSolanaSigner::from_env`] and [`with_signer`]
/// independently. Because `CURRENT_SIGNER` is task-local, no public key leaks
/// between tasks even though they overlap in time.
///
/// # Arguments
///
/// * `_cfg` - Runtime configuration (reserved for future credential loading; currently unused).
///
/// # Errors
///
/// Returns `Err` if any spawned task panics or its
/// [`tokio::task::JoinHandle`] returns an error.
pub async