|
4 | 4 | use move_binary_format::{CompiledModule, file_format::SignatureToken};
|
5 | 5 | use move_bytecode_utils::resolve_struct;
|
6 | 6 | use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
|
7 |
| -use serde::{Deserialize, Serialize}; |
| 7 | +use serde::{ |
| 8 | + Serialize, Serializer, |
| 9 | + ser::{SerializeStruct, SerializeStructVariant, SerializeTupleVariant}, |
| 10 | +}; |
8 | 11 |
|
9 | 12 | use crate::{
|
10 | 13 | IOTA_FRAMEWORK_ADDRESS,
|
11 | 14 | digests::MoveAuthenticatorDigest,
|
12 | 15 | transaction::{CallArg, Command, ProgrammableTransaction},
|
| 16 | + type_input::TypeName, |
13 | 17 | };
|
14 | 18 |
|
15 | 19 | pub const AUTH_CONTEXT_MODULE_NAME: &IdentStr = ident_str!("auth_context");
|
@@ -40,7 +44,7 @@ pub const AUTH_CONTEXT_STRUCT_NAME: &IdentStr = ident_str!("AuthContext");
|
40 | 44 | /// ```
|
41 | 45 | // Conceptually similar to `TxContext`, but designed specifically for use in the authentication
|
42 | 46 | // flow.
|
43 |
| -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] |
| 47 | +#[derive(Clone, Debug, PartialEq, Eq)] |
44 | 48 | pub struct AuthContext {
|
45 | 49 | /// The digest of the MoveAuthenticator
|
46 | 50 | auth_digest: MoveAuthenticatorDigest,
|
@@ -103,6 +107,66 @@ impl AuthContext {
|
103 | 107 | }
|
104 | 108 | }
|
105 | 109 |
|
| 110 | +// TODO: add a deserializer that can handle the Command::MoveCall and |
| 111 | +// Command::MakeMoveVec variants properly. For now, we only need serialization |
| 112 | +// for inclusion in the tx authenticator input, so we implement Serialize only. |
| 113 | +// Alternatively, a custom Command struct could be created for de/serialization |
| 114 | +// purposes or to add new functionalities. |
| 115 | +impl Serialize for AuthContext { |
| 116 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 117 | + where |
| 118 | + S: Serializer, |
| 119 | + { |
| 120 | + let mut state = serializer.serialize_struct("AuthContext", 3)?; |
| 121 | + state.serialize_field("auth_digest", &self.auth_digest)?; |
| 122 | + state.serialize_field("tx_inputs", &self.tx_inputs)?; |
| 123 | + |
| 124 | + // Serialize tx_commands as a Vec of enums, matching the original logic |
| 125 | + struct CommandSer<'a>(&'a Command); |
| 126 | + |
| 127 | + impl<'a> Serialize for CommandSer<'a> { |
| 128 | + fn serialize<SC>(&self, serializer: SC) -> Result<SC::Ok, SC::Error> |
| 129 | + where |
| 130 | + SC: Serializer, |
| 131 | + { |
| 132 | + match self.0 { |
| 133 | + Command::MoveCall(m) => { |
| 134 | + let mut s = |
| 135 | + serializer.serialize_struct_variant("Command", 0, "MoveCall", 5)?; |
| 136 | + s.serialize_field("package", &m.package)?; |
| 137 | + s.serialize_field("module", &m.module)?; |
| 138 | + s.serialize_field("function", &m.function)?; |
| 139 | + s.serialize_field( |
| 140 | + "type_arguments", |
| 141 | + &m.type_arguments |
| 142 | + .iter() |
| 143 | + .map(|ty| TypeName::from(ty)) |
| 144 | + .collect::<Vec<_>>(), |
| 145 | + )?; |
| 146 | + s.serialize_field("arguments", &m.arguments)?; |
| 147 | + s.end() |
| 148 | + } |
| 149 | + Command::MakeMoveVec(ty_opt, vals) => { |
| 150 | + let mut s = |
| 151 | + serializer.serialize_tuple_variant("Command", 5, "MakeMoveVec", 2)?; |
| 152 | + s.serialize_field(&ty_opt.as_ref().map(|ty| TypeName::from(ty)))?; |
| 153 | + s.serialize_field(vals)?; |
| 154 | + s.end() |
| 155 | + } |
| 156 | + _ => self.0.serialize(serializer), |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + state.serialize_field( |
| 162 | + "tx_commands", |
| 163 | + &self.tx_commands.iter().map(CommandSer).collect::<Vec<_>>(), |
| 164 | + )?; |
| 165 | + |
| 166 | + state.end() |
| 167 | + } |
| 168 | +} |
| 169 | + |
106 | 170 | #[derive(PartialEq, Eq, Clone, Copy)]
|
107 | 171 | pub enum AuthContextKind {
|
108 | 172 | // Not AuthContext
|
|
0 commit comments