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
/*
*
* Hedera Rust SDK
*
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use hedera_proto::services;
use time::{
Duration,
OffsetDateTime,
};
use crate::protobuf::ToProtobuf;
use crate::{
AccountId,
FileId,
FromProtobuf,
KeyList,
LedgerId,
};
/// Response from [`FileInfoQuery`][crate::FileInfoQuery].
#[derive(Debug, Clone)]
pub struct FileInfo {
/// The file ID of the file for which information is requested.
pub file_id: FileId,
/// Number of bytes in contents.
pub size: u64,
/// Current time which this account is set to expire.
pub expiration_time: Option<OffsetDateTime>,
/// The auto renew period for this file.
///
/// # Network Support
/// Please note that this not supported on any hedera network at this time.
pub auto_renew_period: Option<Duration>,
/// The account to be used at this file's expiration time to extend the
/// life of the file.
///
/// # Network Support
/// Please note that this not supported on any hedera network at this time.
pub auto_renew_account_id: Option<AccountId>,
/// True if deleted but not yet expired.
pub is_deleted: bool,
/// One of these keys must sign in order to modify or delete the file.
pub keys: KeyList,
/// Memo associated with the file.
pub file_memo: String,
/// Ledger ID for the network the response was returned from.
pub ledger_id: LedgerId,
}
impl FileInfo {
/// Create a new `FileInfo` from protobuf-encoded `bytes`.
///
/// # Errors
/// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the bytes fails to produce a valid protobuf.
/// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the protobuf fails.
pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
FromProtobuf::<services::file_get_info_response::FileInfo>::from_bytes(bytes)
}
/// Convert `self` to a protobuf-encoded [`Vec<u8>`].
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
ToProtobuf::to_bytes(self)
}
}
impl FromProtobuf<services::response::Response> for FileInfo {
#[allow(deprecated)]
fn from_protobuf(pb: services::response::Response) -> crate::Result<Self>
where
Self: Sized,
{
let response = pb_getv!(pb, FileGetInfo, services::response::Response);
let info = pb_getf!(response, file_info)?;
Self::from_protobuf(info)
}
}
impl FromProtobuf<services::file_get_info_response::FileInfo> for FileInfo {
#[allow(deprecated)]
fn from_protobuf(pb: services::file_get_info_response::FileInfo) -> crate::Result<Self>
where
Self: Sized,
{
let file_id = pb_getf!(pb, file_id)?;
let ledger_id = LedgerId::from_bytes(pb.ledger_id);
Ok(Self {
file_id: FileId::from_protobuf(file_id)?,
size: pb.size as u64,
expiration_time: pb.expiration_time.map(Into::into),
auto_renew_account_id: None,
auto_renew_period: None,
is_deleted: pb.deleted,
file_memo: pb.memo,
ledger_id,
keys: KeyList::from_protobuf(pb.keys.unwrap_or_default())?,
})
}
}
impl ToProtobuf for FileInfo {
type Protobuf = services::file_get_info_response::FileInfo;
fn to_protobuf(&self) -> Self::Protobuf {
services::file_get_info_response::FileInfo {
file_id: Some(self.file_id.to_protobuf()),
size: self.size as i64,
expiration_time: self.expiration_time.to_protobuf(),
deleted: self.is_deleted,
memo: self.file_memo.clone(),
ledger_id: self.ledger_id.to_bytes(),
keys: Some(self.keys.to_protobuf()),
}
}
}
#[cfg(test)]
mod tests {
use expect_test::expect;
use hedera_proto::services;
use prost::Message;
use crate::protobuf::{
FromProtobuf,
ToProtobuf,
};
use crate::transaction::test_helpers::unused_private_key;
use crate::{
FileInfo,
Key,
LedgerId,
};
fn make_info() -> services::file_get_info_response::FileInfo {
services::file_get_info_response::FileInfo {
file_id: Some(services::FileId { shard_num: 0, realm_num: 0, file_num: 1 }),
size: 2,
expiration_time: Some(services::Timestamp { seconds: 0, nanos: 3_000 }),
deleted: true,
keys: Some(services::KeyList {
keys: Vec::from([Key::from(unused_private_key().public_key()).to_protobuf()]),
}),
ledger_id: LedgerId::mainnet().to_bytes(),
..Default::default()
}
}
#[test]
fn from_protobuf() {
expect![[r#"
FileInfo {
file_id: "0.0.1",
size: 2,
expiration_time: Some(
1970-01-01 0:00:00.000003 +00:00:00,
),
auto_renew_period: None,
auto_renew_account_id: None,
is_deleted: true,
keys: KeyList {
keys: [
Single(
"302a300506032b6570032100e0c8ec2758a5879ffac226a13c0c516b799e72e35141a0dd828f94d37988a4b7",
),
],
threshold: None,
},
file_memo: "",
ledger_id: "mainnet",
}
"#]].assert_debug_eq(&FileInfo::from_protobuf(make_info()).unwrap());
}
#[test]
fn to_protobuf() {
expect![[r#"
FileInfo {
file_id: Some(
FileId {
shard_num: 0,
realm_num: 0,
file_num: 1,
},
),
size: 2,
expiration_time: Some(
Timestamp {
seconds: 0,
nanos: 3000,
},
),
deleted: true,
keys: Some(
KeyList {
keys: [
Key {
key: Some(
Ed25519(
[
224,
200,
236,
39,
88,
165,
135,
159,
250,
194,
38,
161,
60,
12,
81,
107,
121,
158,
114,
227,
81,
65,
160,
221,
130,
143,
148,
211,
121,
136,
164,
183,
],
),
),
},
],
},
),
memo: "",
ledger_id: [
0,
],
}
"#]]
.assert_debug_eq(&FileInfo::from_protobuf(make_info()).unwrap().to_protobuf());
}
#[test]
fn from_bytes() {
expect![[r#"
FileInfo {
file_id: "0.0.1",
size: 2,
expiration_time: Some(
1970-01-01 0:00:00.000003 +00:00:00,
),
auto_renew_period: None,
auto_renew_account_id: None,
is_deleted: true,
keys: KeyList {
keys: [
Single(
"302a300506032b6570032100e0c8ec2758a5879ffac226a13c0c516b799e72e35141a0dd828f94d37988a4b7",
),
],
threshold: None,
},
file_memo: "",
ledger_id: "mainnet",
}
"#]].assert_debug_eq(&FileInfo::from_bytes(&make_info().encode_to_vec()).unwrap());
}
}