diem_types/transaction/
script.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::{
9    serde_helper::vec_bytes,
10    transaction::transaction_argument::TransactionArgument,
11};
12use move_core_types::{
13    identifier::{IdentStr, Identifier},
14    language_storage::{ModuleId, TypeTag},
15};
16use serde::{Deserialize, Serialize};
17use std::fmt;
18
19/// Call a Move script.
20#[derive(Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
21pub struct Script {
22    #[serde(with = "serde_bytes")]
23    code: Vec<u8>,
24    ty_args: Vec<TypeTag>,
25    args: Vec<TransactionArgument>,
26}
27
28impl Script {
29    pub fn new(
30        code: Vec<u8>, ty_args: Vec<TypeTag>, args: Vec<TransactionArgument>,
31    ) -> Self {
32        Script {
33            code,
34            ty_args,
35            args,
36        }
37    }
38
39    pub fn code(&self) -> &[u8] { &self.code }
40
41    pub fn ty_args(&self) -> &[TypeTag] { &self.ty_args }
42
43    pub fn args(&self) -> &[TransactionArgument] { &self.args }
44
45    pub fn into_inner(self) -> (Vec<u8>, Vec<TransactionArgument>) {
46        (self.code, self.args)
47    }
48}
49
50impl fmt::Debug for Script {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        f.debug_struct("Script")
53            .field("code", &hex::encode(&self.code))
54            .field("ty_args", &self.ty_args)
55            .field("args", &self.args)
56            .finish()
57    }
58}
59
60/// How to call a particular Move script (aka. an "ABI").
61#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
62pub enum ScriptABI {
63    TransactionScript(TransactionScriptABI),
64    ScriptFunction(ScriptFunctionABI),
65}
66
67#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
68pub struct ScriptFunctionABI {
69    /// The public name of the script.
70    name: String,
71    /// The module name where the script lives.
72    module_name: ModuleId,
73    /// Some text comment.
74    doc: String,
75    /// The names of the type arguments.
76    ty_args: Vec<TypeArgumentABI>,
77    /// The description of regular arguments.
78    args: Vec<ArgumentABI>,
79}
80
81#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
82pub struct TransactionScriptABI {
83    /// The public name of the script.
84    name: String,
85    /// Some text comment.
86    doc: String,
87    /// The `code` value to set in the `Script` object.
88    #[serde(with = "serde_bytes")]
89    code: Vec<u8>,
90    /// The names of the type arguments.
91    ty_args: Vec<TypeArgumentABI>,
92    /// The description of regular arguments.
93    args: Vec<ArgumentABI>,
94}
95
96/// The description of a (regular) argument in a script.
97#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
98pub struct ArgumentABI {
99    /// The name of the argument.
100    name: String,
101    /// The expected type.
102    /// In Move scripts, this does contain generics type parameters.
103    type_tag: TypeTag,
104}
105
106/// The description of a type argument in a script.
107#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
108pub struct TypeArgumentABI {
109    /// The name of the argument.
110    name: String,
111}
112
113impl TransactionScriptABI {
114    pub fn new(
115        name: String, doc: String, code: Vec<u8>,
116        ty_args: Vec<TypeArgumentABI>, args: Vec<ArgumentABI>,
117    ) -> Self {
118        Self {
119            name,
120            doc,
121            code,
122            ty_args,
123            args,
124        }
125    }
126
127    pub fn name(&self) -> &str { &self.name }
128
129    pub fn doc(&self) -> &str { &self.doc }
130
131    pub fn code(&self) -> &[u8] { &self.code }
132
133    pub fn ty_args(&self) -> &[TypeArgumentABI] { &self.ty_args }
134
135    pub fn args(&self) -> &[ArgumentABI] { &self.args }
136}
137
138impl ScriptFunctionABI {
139    pub fn new(
140        name: String, module_name: ModuleId, doc: String,
141        ty_args: Vec<TypeArgumentABI>, args: Vec<ArgumentABI>,
142    ) -> Self {
143        Self {
144            name,
145            module_name,
146            doc,
147            ty_args,
148            args,
149        }
150    }
151
152    pub fn name(&self) -> &str { &self.name }
153
154    pub fn module_name(&self) -> &ModuleId { &self.module_name }
155
156    pub fn doc(&self) -> &str { &self.doc }
157
158    pub fn ty_args(&self) -> &[TypeArgumentABI] { &self.ty_args }
159
160    pub fn args(&self) -> &[ArgumentABI] { &self.args }
161}
162
163impl ScriptABI {
164    pub fn is_script_fun_abi(&self) -> bool {
165        matches!(self, Self::ScriptFunction(_))
166    }
167
168    pub fn is_transaction_script_abi(&self) -> bool {
169        matches!(self, Self::TransactionScript(_))
170    }
171
172    pub fn name(&self) -> &str {
173        match self {
174            Self::TransactionScript(abi) => abi.name(),
175            Self::ScriptFunction(abi) => abi.name(),
176        }
177    }
178
179    pub fn doc(&self) -> &str {
180        match self {
181            Self::TransactionScript(abi) => abi.doc(),
182            Self::ScriptFunction(abi) => abi.doc(),
183        }
184    }
185
186    pub fn ty_args(&self) -> &[TypeArgumentABI] {
187        match self {
188            Self::TransactionScript(abi) => abi.ty_args(),
189            Self::ScriptFunction(abi) => abi.ty_args(),
190        }
191    }
192
193    pub fn args(&self) -> &[ArgumentABI] {
194        match self {
195            Self::TransactionScript(abi) => abi.args(),
196            Self::ScriptFunction(abi) => abi.args(),
197        }
198    }
199}
200
201impl ArgumentABI {
202    pub fn new(name: String, type_tag: TypeTag) -> Self {
203        Self { name, type_tag }
204    }
205
206    pub fn name(&self) -> &str { &self.name }
207
208    pub fn type_tag(&self) -> &TypeTag { &self.type_tag }
209}
210
211impl TypeArgumentABI {
212    pub fn new(name: String) -> Self { Self { name } }
213
214    pub fn name(&self) -> &str { &self.name }
215}
216
217/// Call a Move script function.
218#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
219pub struct ScriptFunction {
220    module: ModuleId,
221    function: Identifier,
222    ty_args: Vec<TypeTag>,
223    #[serde(with = "vec_bytes")]
224    args: Vec<Vec<u8>>,
225}
226
227impl ScriptFunction {
228    pub fn new(
229        module: ModuleId, function: Identifier, ty_args: Vec<TypeTag>,
230        args: Vec<Vec<u8>>,
231    ) -> Self {
232        ScriptFunction {
233            module,
234            function,
235            ty_args,
236            args,
237        }
238    }
239
240    pub fn module(&self) -> &ModuleId { &self.module }
241
242    pub fn function(&self) -> &IdentStr { &self.function }
243
244    pub fn ty_args(&self) -> &[TypeTag] { &self.ty_args }
245
246    pub fn args(&self) -> &[Vec<u8>] { &self.args }
247}