solignition-cli 1.1.1

CLI tool for deploying Solana programs via the Solignition lending protocol
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
// Response structs intentionally expose every field returned by the deployer
// service for forward compatibility and Debug output, even when not all
// fields are read by the CLI today.
#![allow(dead_code)]

use anyhow::{anyhow, Context, Result};
use rand::RngCore;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::multipart;
use serde::Deserialize;
use sha2::{Digest, Sha256};
use solana_sdk::signature::Keypair;
use solana_sdk::signer::Signer;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

/// Wire-format version tag for the shared CLI/frontend auth spec.
const AUTH_VERSION_TAG: &str = "solignition-auth-v1";

/// SHA-256 of the empty string, in lowercase hex. Used as `BODY_HASH_HEX` for
/// GET requests and any other body-less request.
const EMPTY_BODY_HASH: &str =
    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

/// Auth headers produced by signing the canonical request message.
struct AuthHeaders {
    pubkey_b58: String,
    timestamp_ms: String,
    nonce_b58: String,
    signature_b58: String,
}

impl AuthHeaders {
    fn apply(self, mut req: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        req = req.header("X-Auth-Pubkey", self.pubkey_b58);
        req = req.header("X-Auth-Timestamp", self.timestamp_ms);
        req = req.header("X-Auth-Nonce", self.nonce_b58);
        req = req.header("X-Auth-Signature", self.signature_b58);
        req
    }
}

fn sha256_hex(bytes: &[u8]) -> String {
    let digest = Sha256::digest(bytes);
    hex::encode(digest)
}

// ─── Response Types ──────────────────────────────────────────────────────────

#[derive(Debug, Deserialize)]
pub struct UploadResponse {
    pub success: bool,
    #[serde(rename = "fileId")]
    pub file_id: String,
    #[serde(rename = "estimatedCost")]
    pub estimated_cost: f64,
    #[serde(rename = "binaryHash")]
    pub binary_hash: String,
    pub message: String,
}

#[derive(Debug, Deserialize)]
pub struct FileUploadInfo {
    #[serde(rename = "fileId")]
    pub file_id: String,
    pub borrower: String,
    #[serde(rename = "fileName")]
    pub file_name: String,
    #[serde(rename = "fileSize")]
    pub file_size: u64,
    #[serde(rename = "binaryHash")]
    pub binary_hash: String,
    #[serde(rename = "estimatedCost")]
    pub estimated_cost: f64,
    pub status: String,
    #[serde(rename = "createdAt")]
    pub created_at: u64,
}

#[derive(Debug, Deserialize)]
pub struct NotifyLoanResponse {
    pub success: bool,
    pub message: String,
    pub signature: Option<String>,
    #[serde(rename = "fileId")]
    pub file_id: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct NotifyRepaidResponse {
    pub success: bool,
    pub message: String,
    pub tx: Option<String>,
    #[serde(rename = "loanId")]
    pub loan_id: Option<String>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct DeploymentInfo {
    #[serde(rename = "loanId")]
    pub loan_id: String,
    pub borrower: String,
    #[serde(rename = "programId")]
    pub program_id: Option<String>,
    #[serde(rename = "deploymentCost")]
    pub deployment_cost: Option<f64>,
    #[serde(rename = "deployTxSignature")]
    pub deploy_tx_signature: Option<String>,
    #[serde(rename = "setDeployedTxSignature")]
    pub set_deployed_tx_signature: Option<String>,
    #[serde(rename = "recoveryTxSignature")]
    pub recovery_tx_signature: Option<String>,
    pub status: String,
    pub error: Option<String>,
    #[serde(rename = "createdAt")]
    pub created_at: u64,
    #[serde(rename = "updatedAt")]
    pub updated_at: u64,
    #[serde(rename = "binaryHash")]
    pub binary_hash: Option<String>,
    pub principal: Option<String>,
    #[serde(rename = "programAccountOpen")]
    pub program_account_open: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct HealthResponse {
    pub status: String,
    #[serde(rename = "activeLoans")]
    pub active_loans: u64,
    #[serde(rename = "totalDeployments")]
    pub total_deployments: u64,
    pub timestamp: String,
}

#[derive(Debug, Deserialize)]
pub struct ErrorResponse {
    pub error: String,
}

// ─── Client ──────────────────────────────────────────────────────────────────

pub struct DeployerClient {
    client: reqwest::Client,
    base_url: String,
    /// Signer used to produce `X-Auth-*` headers. `None` only for the anonymous
    /// `/health` call site (constructed via `new_anonymous`).
    signer: Option<Arc<Keypair>>,
}

impl DeployerClient {
    fn build_client() -> reqwest::Client {
        let mut default_headers = HeaderMap::new();
        default_headers.insert(
            "ngrok-skip-browser-warning",
            HeaderValue::from_static("true"),
        );

        reqwest::Client::builder()
            .user_agent(concat!("solignition-cli/", env!("CARGO_PKG_VERSION")))
            .default_headers(default_headers)
            .timeout(Duration::from_secs(60))
            .connect_timeout(Duration::from_secs(10))
            .build()
            .expect("failed to build HTTP client")
    }

    pub fn new(base_url: &str, signer: Arc<Keypair>) -> Self {
        Self {
            client: Self::build_client(),
            base_url: base_url.trim_end_matches('/').to_string(),
            signer: Some(signer),
        }
    }

    /// Anonymous client for endpoints that don't require auth (e.g. `/health`).
    pub fn new_anonymous(base_url: &str) -> Self {
        Self {
            client: Self::build_client(),
            base_url: base_url.trim_end_matches('/').to_string(),
            signer: None,
        }
    }

    fn signer(&self) -> Result<&Keypair> {
        self.signer
            .as_deref()
            .ok_or_else(|| anyhow!("DeployerClient was constructed anonymously; cannot sign request"))
    }

    /// Build the `X-Auth-*` headers for a request per the shared auth spec.
    ///
    /// `body_hash_hex` must be the lowercase-hex SHA-256 of the exact body bytes
    /// the client will write to the wire (file bytes for multipart, raw JSON bytes
    /// for JSON, `EMPTY_BODY_HASH` for body-less requests).
    fn sign_request(&self, method: &str, path: &str, body_hash_hex: &str) -> Result<AuthHeaders> {
        let signer = self.signer()?;

        let timestamp_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis().to_string())
            .unwrap_or_else(|_| "0".to_string());

        let mut nonce_bytes = [0u8; 16];
        rand::thread_rng().fill_bytes(&mut nonce_bytes);
        let nonce_b58 = bs58::encode(nonce_bytes).into_string();

        let canonical = format!(
            "{tag}\n{method}\n{path}\n{ts}\n{nonce}\n{body_hash}",
            tag = AUTH_VERSION_TAG,
            method = method,
            path = path,
            ts = timestamp_ms,
            nonce = nonce_b58,
            body_hash = body_hash_hex,
        );

        let signature = signer.sign_message(canonical.as_bytes());
        let signature_b58 = bs58::encode(signature.as_ref()).into_string();

        Ok(AuthHeaders {
            pubkey_b58: signer.pubkey().to_string(),
            timestamp_ms,
            nonce_b58,
            signature_b58,
        })
    }

    /// Upload a .so binary file.
    ///
    /// Signs the request with the borrower's keypair and verifies the deployer's
    /// returned `binary_hash` matches the local SHA-256 — guards against silent
    /// transit corruption or a substituted binary.
    pub async fn upload_file(&self, file_path: &Path, borrower: &str) -> Result<UploadResponse> {
        let file_name = file_path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();

        let file_bytes = tokio::fs::read(file_path).await
            .with_context(|| format!("Failed to read file: {}", file_path.display()))?;

        let local_hash = sha256_hex(&file_bytes);

        // For multipart `/upload`, the body hash is sha256(file_bytes) only —
        // the `borrower` form field is enforced separately via authz.
        let auth = self.sign_request("POST", "/upload", &local_hash)?;

        let file_part = multipart::Part::bytes(file_bytes)
            .file_name(file_name)
            .mime_str("application/octet-stream")?;

        let form = multipart::Form::new()
            .text("borrower", borrower.to_string())
            .part("file", file_part);

        let req = self
            .client
            .post(format!("{}/upload", self.base_url))
            .multipart(form);

        let resp = auth
            .apply(req)
            .send()
            .await
            .context("Failed to connect to deployer API")?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            anyhow::bail!("Upload failed ({}): {}", status, body);
        }

        let parsed: UploadResponse = resp
            .json()
            .await
            .context("Failed to parse upload response")?;

        if !parsed.binary_hash.eq_ignore_ascii_case(&local_hash) {
            anyhow::bail!(
                "Upload integrity check failed: deployer reported binary_hash `{}` but local file hash is `{}`",
                parsed.binary_hash,
                local_hash,
            );
        }

        Ok(parsed)
    }

    /// Get upload info by file ID
    pub async fn get_upload(&self, file_id: &str) -> Result<FileUploadInfo> {
        let path = format!("/uploads/{}", file_id);
        let auth = self.sign_request("GET", &path, EMPTY_BODY_HASH)?;

        let req = self.client.get(format!("{}{}", self.base_url, path));
        let resp = auth
            .apply(req)
            .send()
            .await
            .context("Failed to connect to deployer API")?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            anyhow::bail!("Failed to get upload ({}): {}", status, body);
        }

        resp.json::<FileUploadInfo>()
            .await
            .context("Failed to parse upload info")
    }

    /// Get all uploads for a borrower
    pub async fn get_uploads_by_borrower(&self, borrower: &str) -> Result<Vec<FileUploadInfo>> {
        let path = format!("/uploads/borrower/{}", borrower);
        let auth = self.sign_request("GET", &path, EMPTY_BODY_HASH)?;

        let req = self.client.get(format!("{}{}", self.base_url, path));
        let resp = auth
            .apply(req)
            .send()
            .await
            .context("Failed to connect to deployer API")?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            anyhow::bail!("Failed to get uploads ({}): {}", status, body);
        }

        resp.json::<Vec<FileUploadInfo>>()
            .await
            .context("Failed to parse uploads")
    }

    /// Notify deployer about a new loan request
    pub async fn notify_loan(
        &self,
        signature: &str,
        borrower: &str,
        loan_id: &str,
        file_id: &str,
    ) -> Result<NotifyLoanResponse> {
        let body = serde_json::json!({
            "signature": signature,
            "borrower": borrower,
            "loanId": loan_id,
            "fileId": file_id,
        });

        // Serialize once and sign over the exact bytes we'll write to the wire.
        // Using `.json(&body)` would let reqwest re-serialize and produce
        // potentially different bytes than what we hashed.
        let body_bytes = serde_json::to_vec(&body)
            .context("Failed to serialize notify-loan body")?;
        let body_hash = sha256_hex(&body_bytes);
        let auth = self.sign_request("POST", "/notify-loan", &body_hash)?;

        let req = self
            .client
            .post(format!("{}/notify-loan", self.base_url))
            .header(reqwest::header::CONTENT_TYPE, "application/json")
            .body(body_bytes);

        let resp = auth
            .apply(req)
            .send()
            .await
            .context("Failed to connect to deployer API")?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            anyhow::bail!("Failed to notify loan ({}): {}", status, body);
        }

        resp.json::<NotifyLoanResponse>()
            .await
            .context("Failed to parse notify response")
    }

    /// Notify deployer that a loan has been repaid
    pub async fn notify_repaid(
        &self,
        signature: &str,
        borrower: &str,
        loan_id: u64,
    ) -> Result<NotifyRepaidResponse> {
        let body = serde_json::json!({
            "signature": signature,
            "borrower": borrower,
            "loanId": loan_id.to_string(),
        });

        let body_bytes = serde_json::to_vec(&body)
            .context("Failed to serialize notify-repaid body")?;
        let body_hash = sha256_hex(&body_bytes);
        let auth = self.sign_request("POST", "/notify-repaid", &body_hash)?;

        let req = self
            .client
            .post(format!("{}/notify-repaid", self.base_url))
            .header(reqwest::header::CONTENT_TYPE, "application/json")
            .body(body_bytes);

        let resp = auth
            .apply(req)
            .send()
            .await
            .context("Failed to connect to deployer API")?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            anyhow::bail!("Failed to notify repaid ({}): {}", status, body);
        }

        resp.json::<NotifyRepaidResponse>()
            .await
            .context("Failed to parse repaid response")
    }

    /// Get deployment status
    pub async fn get_deployment(&self, loan_id: &str) -> Result<DeploymentInfo> {
        let path = format!("/deployments/{}", loan_id);
        let auth = self.sign_request("GET", &path, EMPTY_BODY_HASH)?;

        let req = self.client.get(format!("{}{}", self.base_url, path));
        let resp = auth
            .apply(req)
            .send()
            .await
            .context("Failed to connect to deployer API")?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            anyhow::bail!("Failed to get deployment ({}): {}", status, body);
        }

        resp.json::<DeploymentInfo>()
            .await
            .context("Failed to parse deployment info")
    }

    /// Get all deployments for a borrower
    pub async fn get_deployments_by_borrower(
        &self,
        borrower: &str,
    ) -> Result<Vec<DeploymentInfo>> {
        let path = format!("/deployments/borrower/{}", borrower);
        let auth = self.sign_request("GET", &path, EMPTY_BODY_HASH)?;

        let req = self.client.get(format!("{}{}", self.base_url, path));
        let resp = auth
            .apply(req)
            .send()
            .await
            .context("Failed to connect to deployer API")?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            anyhow::bail!("Failed to get deployments ({}): {}", status, body);
        }

        resp.json::<Vec<DeploymentInfo>>()
            .await
            .context("Failed to parse deployments")
    }

    /// Health check
    pub async fn health(&self) -> Result<HealthResponse> {
        let resp = self
            .client
            .get(format!("{}/health", self.base_url))
            .send()
            .await
            .context("Failed to connect to deployer API — is the service running?")?;

        resp.json::<HealthResponse>()
            .await
            .context("Failed to parse health response")
    }
}