1use freenet_stdlib::prelude::*;
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "crypto")]
5use ed25519_dalek::VerifyingKey;
6
7#[cfg(feature = "crypto")]
10pub fn fingerprint(verifying_key: &VerifyingKey) -> String {
11 let hash = blake3::hash(verifying_key.as_bytes());
12 bs58::encode(&hash.as_bytes()[..8]).into_string()
13}
14
15pub fn to_cbor<T: Serialize>(value: &T) -> Result<Vec<u8>, String> {
17 let mut buf = Vec::new();
18 ciborium::into_writer(value, &mut buf).map_err(|e| format!("CBOR serialize: {e}"))?;
19 Ok(buf)
20}
21
22pub fn from_cbor<T: for<'de> Deserialize<'de>>(bytes: &[u8]) -> Result<T, String> {
24 ciborium::from_reader(bytes).map_err(|e| format!("CBOR deserialize: {e}"))
25}
26
27#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
29#[non_exhaustive]
30pub enum SignatureRequestor {
31 WebApp(ContractInstanceId),
33 Delegate(DelegateKey),
35}
36
37#[derive(Serialize, Deserialize, Debug, Clone)]
40pub struct ScopedPayload {
41 pub requestor: SignatureRequestor,
42 pub payload: Vec<u8>,
43}
44
45#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
47pub struct GhostKeyInfo {
48 pub fingerprint: String,
49 pub label: Option<String>,
50 #[serde(rename = "delegate_info")]
55 pub notary_info: String,
56}
57
58#[derive(Serialize, Deserialize, Debug, Clone)]
60pub struct ExportedGhostKey {
61 pub fingerprint: String,
62 pub certificate_pem: String,
63 pub signing_key_pem: String,
64 pub label: Option<String>,
65 #[serde(rename = "delegate_info")]
66 pub notary_info: String,
67}
68
69#[derive(Serialize, Deserialize, Debug, Clone)]
71#[non_exhaustive]
72pub enum GhostkeyRequest {
73 ImportGhostKey {
76 certificate_pem: String,
77 signing_key_pem: String,
78 #[serde(default)]
79 master_verifying_key_pem: Option<String>,
80 },
81 ListGhostKeys,
83 GetGhostKey { fingerprint: String },
85 GetCertificate { fingerprint: String },
87 DeleteGhostKey { fingerprint: String },
89 SetLabel { fingerprint: String, label: String },
91 SignMessage {
94 fingerprint: String,
95 message: Vec<u8>,
96 },
97 SignWithDefault { message: Vec<u8> },
101 SetDefaultKey { fingerprint: String },
103 GetDefaultKey,
105 VerifySignedMessage { signed_message: Vec<u8> },
107 ExportGhostKey { fingerprint: String },
110 ExportAllGhostKeys,
112 GrantPermission {
114 fingerprint: String,
115 requestor: SignatureRequestor,
116 },
117 RevokePermission {
119 fingerprint: String,
120 requestor: SignatureRequestor,
121 },
122 ListPermissions { fingerprint: String },
124 TestPermissionPrompt { fingerprint: String },
126}
127
128#[derive(Serialize, Deserialize, Debug, Clone)]
130#[non_exhaustive]
131pub enum GhostkeyResponse {
132 ImportResult {
133 fingerprint: String,
134 #[serde(rename = "delegate_info")]
135 notary_info: String,
136 },
137 GhostKeyList {
138 keys: Vec<GhostKeyInfo>,
139 },
140 GhostKeyDetail {
141 fingerprint: String,
142 certificate_pem: String,
143 label: Option<String>,
144 #[serde(rename = "delegate_info")]
145 notary_info: String,
146 },
147 Certificate {
148 fingerprint: String,
149 certificate_pem: String,
150 },
151 SignResult {
152 scoped_payload: Vec<u8>,
154 signature: Vec<u8>,
156 certificate_pem: String,
158 },
159 DefaultKeyResult {
160 fingerprint: Option<String>,
161 },
162 DefaultKeySet {
163 fingerprint: String,
164 },
165 VerifyResult {
166 valid: bool,
167 signer_fingerprint: Option<String>,
168 #[serde(rename = "delegate_info")]
169 notary_info: Option<String>,
170 requestor: Option<SignatureRequestor>,
171 message: Option<Vec<u8>>,
172 },
173 Deleted {
174 fingerprint: String,
175 },
176 LabelSet {
177 fingerprint: String,
178 label: String,
179 },
180 PermissionGranted {
181 fingerprint: String,
182 requestor: SignatureRequestor,
183 },
184 PermissionRevoked {
185 fingerprint: String,
186 requestor: SignatureRequestor,
187 },
188 PermissionList {
189 fingerprint: String,
190 requestors: Vec<SignatureRequestor>,
191 },
192 ExportResult {
193 fingerprint: String,
194 certificate_pem: String,
195 signing_key_pem: String,
196 label: Option<String>,
197 },
198 ExportAllResult {
199 keys: Vec<ExportedGhostKey>,
200 },
201 PermissionDenied {
202 fingerprint: String,
203 requestor: SignatureRequestor,
204 },
205 NoIdentityAvailable,
208 KeyNotFound {
210 fingerprint: String,
211 },
212 Error {
214 message: String,
215 },
216}