-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathsigner.rs
More file actions
383 lines (323 loc) · 14 KB
/
signer.rs
File metadata and controls
383 lines (323 loc) · 14 KB
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
use base64::{engine::general_purpose::STANDARD, Engine};
use solana_sdk::{pubkey::Pubkey, signature::Signature, transaction::VersionedTransaction};
use std::str::FromStr;
use crate::signer::privy::types::{
PrivyError, PrivySigner, SignTransactionParams, SignTransactionRequest,
SignTransactionResponse, WalletResponse,
};
impl PrivySigner {
/// Create a new PrivySigner
pub fn new(app_id: String, app_secret: String, wallet_id: String) -> Self {
Self {
app_id,
app_secret,
wallet_id,
api_base_url: "https://api.privy.io/v1".to_string(),
client: reqwest::Client::new(),
public_key: Pubkey::default(),
}
}
/// Initialize the signer by fetching the public key
pub async fn init(&mut self) -> Result<(), PrivyError> {
let pubkey = self.get_public_key().await?;
self.public_key = pubkey;
Ok(())
}
/// Get the cached public key
pub fn solana_pubkey(&self) -> Pubkey {
self.public_key
}
/// Get the Basic Auth header value
fn get_privy_auth_header(&self) -> String {
let credentials = format!("{}:{}", self.app_id, self.app_secret);
format!("Basic {}", STANDARD.encode(credentials))
}
/// Get the public key for this wallet
pub async fn get_public_key(&self) -> Result<Pubkey, PrivyError> {
let url = format!("{}/wallets/{}", self.api_base_url, self.wallet_id);
let response = self
.client
.get(&url)
.header("Authorization", self.get_privy_auth_header())
.header("privy-app-id", &self.app_id)
.send()
.await?;
if !response.status().is_success() {
let status = response.status().as_u16();
let error_text = response
.text()
.await
.unwrap_or_else(|_| "Failed to read error response".to_string());
log::error!(
"Privy API get_public_key error - status: {status}, response: {error_text}"
);
return Err(PrivyError::ApiError(status));
}
let wallet_info: WalletResponse = response.json().await?;
// For Solana wallets, the address is the public key
Pubkey::from_str(&wallet_info.address).map_err(|_| PrivyError::InvalidPublicKey)
}
/// Sign a transaction
///
/// The transaction parameter should be a fully serialized Solana transaction
/// (including empty signature placeholders), not just the message bytes.
pub async fn sign_solana(
&self,
transaction: &VersionedTransaction,
) -> Result<Signature, PrivyError> {
let url = format!("{}/wallets/{}/rpc", self.api_base_url, self.wallet_id);
let serialized =
bincode::serialize(transaction).map_err(|_| PrivyError::SerializationError)?;
let request = SignTransactionRequest {
method: "signTransaction",
params: SignTransactionParams {
transaction: STANDARD.encode(serialized),
encoding: "base64",
},
};
let response = self
.client
.post(&url)
.header("Authorization", self.get_privy_auth_header())
.header("privy-app-id", &self.app_id)
.header("Content-Type", "application/json")
.json(&request)
.send()
.await?;
if !response.status().is_success() {
let status = response.status().as_u16();
let error_text = response
.text()
.await
.unwrap_or_else(|_| "Failed to read error response".to_string());
log::error!("Privy API sign_solana error - status: {status}, response: {error_text}");
return Err(PrivyError::ApiError(status));
}
let response_text = response.text().await?;
let sign_response: SignTransactionResponse = serde_json::from_str(&response_text)?;
// Decode the signed transaction from base64
let signed_tx_bytes = STANDARD.decode(&sign_response.data.signed_transaction)?;
// Deserialize the transaction to extract the signature
let signed_tx: VersionedTransaction =
bincode::deserialize(&signed_tx_bytes).map_err(|_| PrivyError::InvalidResponse)?;
// Get the first signature (which should be the one we just created)
if let Some(signature) = signed_tx.signatures.first() {
Ok(*signature)
} else {
Err(PrivyError::InvalidSignature)
}
}
pub async fn sign(&self, transaction: &VersionedTransaction) -> Result<Vec<u8>, PrivyError> {
let signature = self.sign_solana(transaction).await?;
Ok(signature.as_ref().to_vec())
}
}
#[cfg(test)]
mod tests {
use crate::tests::transaction_mock::create_mock_transaction;
use super::*;
use mockito::Server;
use solana_sdk::pubkey::Pubkey;
#[test]
fn test_new_privy_signer() {
let app_id = "test_app_id".to_string();
let app_secret = "test_app_secret".to_string();
let wallet_id = "test_wallet_id".to_string();
let signer = PrivySigner::new(app_id.clone(), app_secret.clone(), wallet_id.clone());
assert_eq!(signer.app_id, app_id);
assert_eq!(signer.app_secret, app_secret);
assert_eq!(signer.wallet_id, wallet_id);
assert_eq!(signer.api_base_url, "https://api.privy.io/v1");
assert_eq!(signer.public_key, Pubkey::default());
}
#[test]
fn test_solana_pubkey() {
let mut signer = PrivySigner::new(
"app_id".to_string(),
"app_secret".to_string(),
"wallet_id".to_string(),
);
let test_pubkey = Pubkey::new_unique();
signer.public_key = test_pubkey;
assert_eq!(signer.solana_pubkey(), test_pubkey);
}
#[test]
fn test_get_privy_auth_header() {
let signer = PrivySigner::new(
"test_app".to_string(),
"test_secret".to_string(),
"wallet123".to_string(),
);
let auth_header = signer.get_privy_auth_header();
let expected_credentials = "test_app:test_secret";
let expected_encoded = STANDARD.encode(expected_credentials);
let expected_header = format!("Basic {expected_encoded}");
assert_eq!(auth_header, expected_header);
}
#[tokio::test]
async fn test_get_public_key_success() {
// Setup mock server
let mut server = Server::new_async().await;
// Mocked response from Privy API based on https://docs.privy.io/api-reference/wallets/get
let mock_response = r#"{
"id": "clz4ndjp705bh14za2p80kt3f",
"object": "wallet",
"created_at": 1721937199,
"address": "11111111111111111111111111111111",
"chain_type": "solana",
"chain_id": "solana:101",
"wallet_client": "privy",
"wallet_client_type": "privy",
"connector_type": "embedded",
"recovery_method": "privy",
"imported": false,
"delegated": false,
"user_id": "did:privy:cm0xlrcmj01ja13m6ncg4ewce"
}"#;
// Create mock endpoint for GET /wallets/{wallet_id}
let _mock = server
.mock("GET", "/wallets/test_wallet")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(mock_response)
.create_async()
.await;
let mut signer = PrivySigner::new(
"test_app".to_string(),
"test_secret".to_string(),
"test_wallet".to_string(),
);
signer.api_base_url = server.url();
// Test successful public key retrieval
let result = signer.get_public_key().await;
assert!(result.is_ok());
assert_eq!(result.unwrap().to_string(), "11111111111111111111111111111111");
}
#[tokio::test]
async fn test_get_public_key_api_error() {
let mut server = Server::new_async().await;
// Mocked error response from Privy API based on https://docs.privy.io/api-reference/wallets/get
let mock_error_response = r#"{
"error": {
"error": "wallet_not_found",
"error_description": "Wallet not found."
}
}"#;
// Create mock endpoint for GET /wallets/{wallet_id} returning 404 error
let _mock = server
.mock("GET", "/wallets/invalid_wallet")
.with_status(404)
.with_header("content-type", "application/json")
.with_body(mock_error_response)
.create_async()
.await;
let mut signer = PrivySigner::new(
"test_app".to_string(),
"test_secret".to_string(),
"invalid_wallet".to_string(),
);
signer.api_base_url = server.url();
// Test API error handling
let result = signer.get_public_key().await;
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), PrivyError::ApiError(404)));
}
#[tokio::test]
async fn test_sign_solana_success() {
// Setup mock server
let mut server = Server::new_async().await;
// Mocked response from Privy RPC API based on https://docs.privy.io/api-reference/wallets/solana/sign-transaction
// Modified to match the SignTransactionResponse struct
let mock_response = r#"{
"method": "signTransaction",
"data": {
"signed_transaction": "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDArczbMia1tLmq7zz4DinMNN0pJ1JtLdqIJPUw3YrGCzuAXUE8535pRk2d+dzOdFlBIpWfgXa9F2zWLidMUr5zdDlBG2q1y4YJlUDl7ov7FLfWvDlhVAidT5nXu6bJgZG1qNgJQBd55PwKBNYMFYBJ2rIbgNhfHu6E/OmZFpV9EUCuE8AAAABd2J0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFB8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAA=",
"encoding": "base64"
}
}"#;
// Create mock endpoint for POST /wallets/{wallet_id}/rpc
let _mock = server
.mock("POST", "/wallets/test_wallet/rpc")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(mock_response)
.create_async()
.await;
let test_transaction = create_mock_transaction();
let mut signer = PrivySigner::new(
"test_app".to_string(),
"test_secret".to_string(),
"test_wallet".to_string(),
);
signer.api_base_url = server.url();
// Test successful signing
let result = signer.sign_solana(&test_transaction).await;
assert!(result.is_ok());
let signature = result.unwrap();
assert_eq!(signature.to_string().len(), 64); // Hex encoded signature length
}
#[tokio::test]
async fn test_sign_solana_api_error() {
let mut server = Server::new_async().await;
// Mocked error response from Privy RPC API based on https://docs.privy.io/api-reference/wallets/solana/sign-transaction
let mock_error_response = r#"{
"error": {
"error": "invalid_request",
"error_description": "The transaction is invalid or malformed."
}
}"#;
// Create mock endpoint for POST /wallets/{wallet_id}/rpc returning 400 error
let _mock = server
.mock("POST", "/wallets/test_wallet/rpc")
.with_status(400)
.with_header("content-type", "application/json")
.with_body(mock_error_response)
.create_async()
.await;
let test_transaction = create_mock_transaction();
let mut signer = PrivySigner::new(
"test_app".to_string(),
"test_secret".to_string(),
"test_wallet".to_string(),
);
signer.api_base_url = server.url();
// Test API error handling
let result = signer.sign_solana(&test_transaction).await;
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), PrivyError::ApiError(400)));
}
#[tokio::test]
async fn test_sign_success() {
// Setup mock server
let mut server = Server::new_async().await;
// Mocked response from Privy RPC API (same as sign_solana) based on https://docs.privy.io/api-reference/wallets/solana/sign-transaction
// Modified to match the SignTransactionResponse struct
let mock_response = r#"{
"method": "signTransaction",
"data": {
"signed_transaction": "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDArczbMia1tLmq7zz4DinMNN0pJ1JtLdqIJPUw3YrGCzuAXUE8535pRk2d+dzOdFlBIpWfgXa9F2zWLidMUr5zdDlBG2q1y4YJlUDl7ov7FLfWvDlhVAidT5nXu6bJgZG1qNgJQBd55PwKBNYMFYBJ2rIbgNhfHu6E/OmZFpV9EUCuE8AAAABd2J0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFB8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAA=",
"encoding": "base64"
}
}"#;
// Create mock endpoint for POST /wallets/{wallet_id}/rpc
let _mock = server
.mock("POST", "/wallets/test_wallet/rpc")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(mock_response)
.create_async()
.await;
let test_transaction = create_mock_transaction();
let mut signer = PrivySigner::new(
"test_app".to_string(),
"test_secret".to_string(),
"test_wallet".to_string(),
);
signer.api_base_url = server.url();
// Test successful signing returns Vec<u8>
let result = signer.sign(&test_transaction).await;
assert!(result.is_ok());
let signature_bytes = result.unwrap();
assert_eq!(signature_bytes.len(), 64); // Solana signature is 64 bytes
}
}