-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathvalid_ptb.rs
257 lines (234 loc) · 8.02 KB
/
valid_ptb.rs
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
// // Copyright (c), Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::errors::InternalError;
use crate::return_err;
use crate::KeyId;
use crypto::create_full_id;
use sui_sdk::types::transaction::{Argument, CallArg, Command, ProgrammableTransaction};
use sui_types::base_types::ObjectID;
use sui_types::transaction::ProgrammableMoveCall;
use tracing::debug;
///
/// PTB that is valid for evaluating a policy. See restrictions in try_from below.
///
pub struct ValidPtb(ProgrammableTransaction);
impl TryFrom<ProgrammableTransaction> for ValidPtb {
type Error = InternalError;
fn try_from(ptb: ProgrammableTransaction) -> Result<Self, Self::Error> {
debug!("Creating vptb from: {:?}", ptb);
// Restriction: The PTB must have at least one input and one command.
if ptb.inputs.is_empty() || ptb.commands.is_empty() {
return_err!(
InternalError::InvalidPTB("Empty PTB input or command".to_string()),
"Invalid PTB {:?}",
ptb
);
}
// Checked above that there is at least one command
let Command::MoveCall(first_cmd) = &ptb.commands[0] else {
return_err!(
InternalError::InvalidPTB("Invalid first command".to_string()),
"Invalid PTB first command {:?}",
ptb
);
};
let pkg_id = first_cmd.package;
for cmd in &ptb.commands {
// Restriction: All commands must be a MoveCall.
let Command::MoveCall(cmd) = &cmd else {
return_err!(
InternalError::InvalidPTB("Non MoveCall command".to_string()),
"Non MoveCall command {:?}",
cmd
);
};
// Restriction: The first argument to the move call must be a non-empty id.
let _ = get_key_id(&ptb, cmd)?;
// Restriction: The called function must start with the prefix seal_approve.
// Restriction: All commands must use the same package id.
if !cmd.function.starts_with("seal_approve") || cmd.package != pkg_id {
return_err!(
InternalError::InvalidPTB("Invalid function or package id".to_string()),
"Invalid function or package id {:?}",
cmd
);
}
}
// TODO: sanity checks - non mutable objs.
Ok(ValidPtb(ptb))
}
}
fn get_key_id(
ptb: &ProgrammableTransaction,
cmd: &ProgrammableMoveCall,
) -> Result<KeyId, InternalError> {
if cmd.arguments.is_empty() {
return_err!(
InternalError::InvalidPTB("Empty args".to_string()),
"Invalid PTB command {:?}",
cmd
);
}
let Argument::Input(arg_idx) = cmd.arguments[0] else {
return_err!(
InternalError::InvalidPTB("Invalid index for first argument".to_string()),
"Invalid PTB command {:?}",
cmd
);
};
let CallArg::Pure(id) = &ptb.inputs[arg_idx as usize] else {
return_err!(
InternalError::InvalidPTB("Invalid first parameter for seal_approve".to_string()),
"Invalid PTB command {:?}",
cmd
);
};
bcs::from_bytes(id).map_err(|_| {
InternalError::InvalidPTB("Invalid BCS for first parameter for seal_approve".to_string())
})
}
impl ValidPtb {
// The ids without the pkgId prefix
pub fn inner_ids(&self) -> Vec<KeyId> {
self.0
.commands
.iter()
.map(|cmd| {
let Command::MoveCall(cmd) = cmd else {
unreachable!()
};
get_key_id(&self.0, cmd).expect("checked above")
})
.collect()
}
pub fn pkg_id(&self) -> ObjectID {
let Command::MoveCall(cmd) = &self.0.commands[0] else {
unreachable!()
};
cmd.package
}
pub fn full_ids(&self, first_pkg_id: &ObjectID) -> Vec<KeyId> {
self.inner_ids()
.iter()
.map(|inner_id| create_full_id(&first_pkg_id.into_bytes(), inner_id))
.collect()
}
pub fn ptb(&self) -> &ProgrammableTransaction {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use sui_sdk::types::base_types::SuiAddress;
use sui_types::base_types::ObjectID;
use sui_types::programmable_transaction_builder::ProgrammableTransactionBuilder;
use sui_types::Identifier;
#[test]
fn test_valid() {
let mut builder = ProgrammableTransactionBuilder::new();
let id = vec![1u8, 2, 3, 4];
let id_caller = builder.pure(id.clone()).unwrap();
let pkgid = ObjectID::random();
builder.programmable_move_call(
pkgid,
Identifier::new("bla").unwrap(),
Identifier::new("seal_approve_x").unwrap(),
vec![],
vec![id_caller],
);
builder.programmable_move_call(
pkgid,
Identifier::new("bla2").unwrap(),
Identifier::new("seal_approve_y").unwrap(),
vec![],
vec![id_caller],
);
let ptb = builder.finish();
let valid_ptb = ValidPtb::try_from(ptb).unwrap();
assert_eq!(valid_ptb.inner_ids(), vec![id.clone(), id]);
assert_eq!(valid_ptb.pkg_id(), pkgid);
}
#[test]
fn test_invalid_empty_ptb() {
let builder = ProgrammableTransactionBuilder::new();
let ptb = builder.finish();
assert_eq!(
ValidPtb::try_from(ptb).err(),
Some(InternalError::InvalidPTB(
"Empty PTB input or command".to_string()
))
);
}
#[test]
fn test_invalid_no_inputs() {
let mut builder = ProgrammableTransactionBuilder::new();
let pkgid = ObjectID::random();
builder.programmable_move_call(
pkgid,
Identifier::new("bla").unwrap(),
Identifier::new("seal_approve").unwrap(),
vec![],
vec![],
);
let ptb = builder.finish();
assert_eq!(
ValidPtb::try_from(ptb).err(),
Some(InternalError::InvalidPTB(
"Empty PTB input or command".to_string()
))
);
}
#[test]
fn test_invalid_non_move_call() {
let mut builder = ProgrammableTransactionBuilder::new();
let sender = SuiAddress::random_for_testing_only();
let id = vec![1u8, 2, 3, 4];
let id_caller = builder.pure(id.clone()).unwrap();
let pkgid = ObjectID::random();
builder.programmable_move_call(
pkgid,
Identifier::new("bla").unwrap(),
Identifier::new("seal_approve_x").unwrap(),
vec![],
vec![id_caller],
);
// Add a transfer command instead of move call
builder.transfer_sui(sender, Some(1));
let ptb = builder.finish();
assert_eq!(
ValidPtb::try_from(ptb).err(),
Some(InternalError::InvalidPTB(
"Non MoveCall command".to_string()
))
);
}
#[test]
fn test_invalid_different_package_ids() {
let mut builder = ProgrammableTransactionBuilder::new();
let id = builder.pure(vec![1u8, 2, 3]).unwrap();
let pkgid1 = ObjectID::random();
let pkgid2 = ObjectID::random();
builder.programmable_move_call(
pkgid1,
Identifier::new("bla").unwrap(),
Identifier::new("seal_approve").unwrap(),
vec![],
vec![id],
);
builder.programmable_move_call(
pkgid2, // Different package ID
Identifier::new("bla").unwrap(),
Identifier::new("seal_approve").unwrap(),
vec![],
vec![id],
);
let ptb = builder.finish();
assert_eq!(
ValidPtb::try_from(ptb).err(),
Some(InternalError::InvalidPTB(
"Invalid function or package id".to_string()
))
);
}
}