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
use anyhow::{anyhow, Error, Result};
use diem_crypto::HashValue;
use diem_types::transaction::{ScriptABI, TransactionScriptABI};
use include_dir::{include_dir, Dir};
use std::{convert::TryFrom, fmt, path::PathBuf};
const TXN_SCRIPTS_ABI_DIR: Dir = include_dir!("legacy/script_abis");
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum LegacyStdlibScript {
AddCurrencyToAccount,
AddRecoveryRotationCapability,
AddValidatorAndReconfigure,
Burn,
BurnTxnFees,
CancelBurn,
CreateChildVaspAccount,
CreateDesignatedDealer,
CreateParentVaspAccount,
CreateRecoveryAddress,
CreateValidatorAccount,
CreateValidatorOperatorAccount,
FreezeAccount,
PeerToPeerWithMetadata,
Preburn,
PublishSharedEd2551PublicKey,
RegisterValidatorConfig,
RemoveValidatorAndReconfigure,
RotateAuthenticationKey,
RotateAuthenticationKeyWithNonce,
RotateAuthenticationKeyWithNonceAdmin,
RotateAuthenticationKeyWithRecoveryAddress,
RotateDualAttestationInfo,
RotateSharedEd2551PublicKey,
SetValidatorConfigAndReconfigure,
SetValidatorOperator,
SetValidatorOperatorWithNonceAdmin,
TieredMint,
UnfreezeAccount,
UpdateExchangeRate,
UpdateDiemVersion,
UpdateMintingAbility,
UpdateDualAttestationLimit,
}
impl LegacyStdlibScript {
pub fn all() -> Vec<Self> {
use LegacyStdlibScript::*;
vec![
AddCurrencyToAccount,
AddRecoveryRotationCapability,
AddValidatorAndReconfigure,
Burn,
BurnTxnFees,
CancelBurn,
CreateChildVaspAccount,
CreateDesignatedDealer,
CreateParentVaspAccount,
CreateRecoveryAddress,
CreateValidatorAccount,
CreateValidatorOperatorAccount,
FreezeAccount,
PeerToPeerWithMetadata,
Preburn,
PublishSharedEd2551PublicKey,
RegisterValidatorConfig,
RemoveValidatorAndReconfigure,
RotateAuthenticationKey,
RotateAuthenticationKeyWithNonce,
RotateAuthenticationKeyWithNonceAdmin,
RotateAuthenticationKeyWithRecoveryAddress,
RotateDualAttestationInfo,
RotateSharedEd2551PublicKey,
SetValidatorConfigAndReconfigure,
SetValidatorOperator,
SetValidatorOperatorWithNonceAdmin,
TieredMint,
UnfreezeAccount,
UpdateExchangeRate,
UpdateDiemVersion,
UpdateMintingAbility,
UpdateDualAttestationLimit,
]
}
pub fn allowlist() -> Vec<HashValue> {
LegacyStdlibScript::all()
.iter()
.map(|script| script.compiled_bytes().hash())
.collect()
}
pub fn name(self) -> String {
self.to_string()
}
pub fn is(code_bytes: &[u8]) -> bool {
Self::try_from(code_bytes).is_ok()
}
pub fn compiled_bytes(self) -> CompiledBytes {
CompiledBytes(self.abi().code().to_vec())
}
pub fn abi(self) -> TransactionScriptABI {
let mut path = PathBuf::from(self.name());
path.set_extension("abi");
let content = TXN_SCRIPTS_ABI_DIR
.get_file(path.clone())
.unwrap_or_else(|| panic!("File {:?} does not exist", path))
.contents();
match bcs::from_bytes::<ScriptABI>(content)
.unwrap_or_else(|err| panic!("Failed to deserialize ABI file {:?}: {}", path, err))
{
ScriptABI::TransactionScript(abi) => abi,
ScriptABI::ScriptFunction(_) => {
panic!("Found a script function in the legacy ABIs -- this shouldn't happen")
}
}
}
pub fn hash(self) -> HashValue {
self.compiled_bytes().hash()
}
}
#[derive(Clone)]
pub struct CompiledBytes(Vec<u8>);
impl CompiledBytes {
pub fn hash(&self) -> HashValue {
Self::hash_bytes(&self.0)
}
fn hash_bytes(bytes: &[u8]) -> HashValue {
HashValue::sha3_256_of(bytes)
}
pub fn into_vec(self) -> Vec<u8> {
self.0
}
}
impl TryFrom<&[u8]> for LegacyStdlibScript {
type Error = Error;
fn try_from(code_bytes: &[u8]) -> Result<Self> {
let hash = CompiledBytes::hash_bytes(code_bytes);
Self::all()
.iter()
.find(|script| script.hash() == hash)
.cloned()
.ok_or_else(|| anyhow!("Could not create standard library script from bytes"))
}
}
impl fmt::Display for LegacyStdlibScript {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use LegacyStdlibScript::*;
write!(
f,
"{}",
match self {
AddValidatorAndReconfigure => "add_validator_and_reconfigure",
AddCurrencyToAccount => "add_currency_to_account",
AddRecoveryRotationCapability => "add_recovery_rotation_capability",
Burn => "burn",
BurnTxnFees => "burn_txn_fees",
CancelBurn => "cancel_burn",
CreateChildVaspAccount => "create_child_vasp_account",
CreateDesignatedDealer => "create_designated_dealer",
CreateParentVaspAccount => "create_parent_vasp_account",
CreateRecoveryAddress => "create_recovery_address",
CreateValidatorAccount => "create_validator_account",
CreateValidatorOperatorAccount => "create_validator_operator_account",
FreezeAccount => "freeze_account",
PeerToPeerWithMetadata => "peer_to_peer_with_metadata",
Preburn => "preburn",
PublishSharedEd2551PublicKey => "publish_shared_ed25519_public_key",
RegisterValidatorConfig => "register_validator_config",
RemoveValidatorAndReconfigure => "remove_validator_and_reconfigure",
RotateAuthenticationKey => "rotate_authentication_key",
RotateAuthenticationKeyWithNonce => "rotate_authentication_key_with_nonce",
RotateAuthenticationKeyWithNonceAdmin =>
"rotate_authentication_key_with_nonce_admin",
RotateAuthenticationKeyWithRecoveryAddress =>
"rotate_authentication_key_with_recovery_address",
RotateDualAttestationInfo => "rotate_dual_attestation_info",
RotateSharedEd2551PublicKey => "rotate_shared_ed25519_public_key",
SetValidatorConfigAndReconfigure => "set_validator_config_and_reconfigure",
SetValidatorOperator => "set_validator_operator",
SetValidatorOperatorWithNonceAdmin => "set_validator_operator_with_nonce_admin",
TieredMint => "tiered_mint",
UpdateDualAttestationLimit => "update_dual_attestation_limit",
UnfreezeAccount => "unfreeze_account",
UpdateDiemVersion => "update_diem_version",
UpdateExchangeRate => "update_exchange_rate",
UpdateMintingAbility => "update_minting_ability",
}
)
}
}
#[cfg(test)]
mod test {
use super::*;
const COMPILED_TXN_SCRIPTS_DIR: Dir = include_dir!("legacy/scripts");
#[test]
fn test_file_correspondence() {
let files = COMPILED_TXN_SCRIPTS_DIR.files();
let scripts = LegacyStdlibScript::all();
for file in files {
assert!(
LegacyStdlibScript::is(file.contents()),
"File {} missing from StdlibScript enum",
file.path().display()
)
}
assert_eq!(
files.len(),
scripts.len(),
"Mismatch between stdlib script files and StdlibScript enum. {}",
if files.len() > scripts.len() {
"Did you forget to extend the StdlibScript enum?"
} else {
"Did you forget to rebuild the standard library?"
}
);
}
#[test]
fn test_names() {
for script in LegacyStdlibScript::all() {
assert_eq!(
script.name(),
script.abi().name(),
"The main function in language/diem-framework/transaction_scripts/{}.move is named `{}` instead of `{}`.",
script.name(),
script.abi().name(),
script.name(),
);
}
}
#[test]
fn test_docs() {
for script in LegacyStdlibScript::all() {
assert!(
!script.abi().doc().is_empty(),
"The main function in language/diem-framework/transaction_scripts/{}.move does not have a `///` inline doc comment.",
script.name(),
);
}
}
}