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
/*
*
* 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 prost::Message;
use time::OffsetDateTime;
use crate::protobuf::ToProtobuf;
use crate::{
AccountId,
FromProtobuf,
LedgerId,
NftId,
};
/// Response from [`TokenNftInfoQuery`][crate::TokenNftInfoQuery].
#[derive(Debug, Clone)]
pub struct TokenNftInfo {
/// The ID of the NFT.
pub nft_id: NftId,
/// The current owner of the NFT.
pub account_id: AccountId,
/// Effective consensus timestamp at which the NFT was minted.
pub creation_time: OffsetDateTime,
/// The unique metadata of the NFT.
pub metadata: Vec<u8>,
/// If an allowance is granted for the NFT, its corresponding spender account.
pub spender_id: Option<AccountId>,
/// The ledger ID the response was returned from.
pub ledger_id: LedgerId,
}
impl TokenNftInfo {
/// Create a new `TokenInfo` 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::TokenNftInfo>::from_bytes(bytes)
}
/// Convert `self` to a protobuf-encoded [`Vec<u8>`].
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
services::TokenNftInfo {
nft_id: Some(self.nft_id.to_protobuf()),
account_id: Some(self.account_id.to_protobuf()),
creation_time: Some(self.creation_time.to_protobuf()),
metadata: self.metadata.clone(),
ledger_id: self.ledger_id.to_bytes(),
spender_id: self.spender_id.to_protobuf(),
}
.encode_to_vec()
}
}
impl FromProtobuf<services::response::Response> for TokenNftInfo {
fn from_protobuf(pb: services::response::Response) -> crate::Result<Self>
where
Self: Sized,
{
let pb = pb_getv!(pb, TokenGetNftInfo, services::response::Response);
let nft = pb_getf!(pb, nft)?;
Self::from_protobuf(nft)
}
}
impl FromProtobuf<services::TokenNftInfo> for TokenNftInfo {
fn from_protobuf(pb: services::TokenNftInfo) -> crate::Result<Self>
where
Self: Sized,
{
let nft_id = pb_getf!(pb, nft_id)?;
let account_id = pb_getf!(pb, account_id)?;
let creation_time = pb.creation_time.unwrap();
let metadata = pb.metadata;
let spender_account_id = Option::from_protobuf(pb.spender_id)?;
Ok(Self {
nft_id: NftId::from_protobuf(nft_id)?,
account_id: AccountId::from_protobuf(account_id)?,
creation_time: OffsetDateTime::from(creation_time),
metadata,
spender_id: spender_account_id,
ledger_id: LedgerId::from_bytes(pb.ledger_id),
})
}
}
#[cfg(test)]
mod tests {
use expect_test::expect;
use hex_literal::hex;
use crate::transaction::test_helpers::VALID_START;
use crate::{
AccountId,
LedgerId,
TokenId,
TokenNftInfo,
};
fn make_info(spender_account_id: Option<AccountId>) -> TokenNftInfo {
TokenNftInfo {
nft_id: TokenId::new(1, 2, 3).nft(4),
account_id: "5.6.7".parse().unwrap(),
creation_time: VALID_START,
metadata: hex!("deadbeef").into(),
spender_id: spender_account_id,
ledger_id: LedgerId::mainnet(),
}
}
#[test]
fn serialize() {
let info = make_info(Some("8.9.10".parse().unwrap()));
expect![[r#"
Ok(
TokenNftInfo {
nft_id: "1.2.3/4",
account_id: "5.6.7",
creation_time: 2019-04-01 22:42:22.0 +00:00:00,
metadata: [
222,
173,
190,
239,
],
spender_id: Some(
"8.9.10",
),
ledger_id: "mainnet",
},
)
"#]]
.assert_debug_eq(&TokenNftInfo::from_bytes(&info.to_bytes()));
}
#[test]
fn serialize_no_spender() {
let info = make_info(None);
expect![[r#"
Ok(
TokenNftInfo {
nft_id: "1.2.3/4",
account_id: "5.6.7",
creation_time: 2019-04-01 22:42:22.0 +00:00:00,
metadata: [
222,
173,
190,
239,
],
spender_id: None,
ledger_id: "mainnet",
},
)
"#]]
.assert_debug_eq(&TokenNftInfo::from_bytes(&info.to_bytes()));
}
}