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
use clap::arg;
use std::{
fmt::Display,
fs,
io::{self, Cursor},
path::Path,
};
use soroban_env_host::xdr::{self, ReadXdr, ScEnvMetaEntry, ScSpecEntry};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("reading file {filepath}: {error}")]
CannotReadContractFile {
filepath: std::path::PathBuf,
error: io::Error,
},
#[error("cannot parse wasm file {file}: {error}")]
CannotParseWasm {
file: std::path::PathBuf,
error: wasmparser::BinaryReaderError,
},
#[error("xdr processing error: {0}")]
Xdr(#[from] xdr::Error),
}
#[derive(Debug, clap::Args, Clone)]
#[group(skip)]
pub struct Args {
#[arg(long)]
pub wasm: std::path::PathBuf,
}
impl Args {
pub fn read(&self) -> Result<Vec<u8>, Error> {
fs::read(&self.wasm).map_err(|e| Error::CannotReadContractFile {
filepath: self.wasm.clone(),
error: e,
})
}
pub fn len(&self) -> Result<u64, Error> {
len(&self.wasm)
}
pub fn is_empty(&self) -> Result<bool, Error> {
self.len().map(|len| len == 0)
}
pub fn parse(&self) -> Result<ContractSpec, Error> {
let contents = self.read()?;
let mut env_meta: Option<&[u8]> = None;
let mut spec: Option<&[u8]> = None;
for payload in wasmparser::Parser::new(0).parse_all(&contents) {
let payload = payload.map_err(|e| Error::CannotParseWasm {
file: self.wasm.clone(),
error: e,
})?;
if let wasmparser::Payload::CustomSection(section) = payload {
let out = match section.name() {
"contractenvmetav0" => &mut env_meta,
"contractspecv0" => &mut spec,
_ => continue,
};
*out = Some(section.data());
};
}
let mut env_meta_base64 = None;
let env_meta = if let Some(env_meta) = env_meta {
env_meta_base64 = Some(base64::encode(env_meta));
let mut cursor = Cursor::new(env_meta);
ScEnvMetaEntry::read_xdr_iter(&mut cursor).collect::<Result<Vec<_>, xdr::Error>>()?
} else {
vec![]
};
let mut spec_base64 = None;
let spec = if let Some(spec) = spec {
spec_base64 = Some(base64::encode(spec));
let mut cursor = Cursor::new(spec);
ScSpecEntry::read_xdr_iter(&mut cursor).collect::<Result<Vec<_>, xdr::Error>>()?
} else {
vec![]
};
Ok(ContractSpec {
env_meta_base64,
env_meta,
spec_base64,
spec,
})
}
}
pub struct ContractSpec {
pub env_meta_base64: Option<String>,
pub env_meta: Vec<ScEnvMetaEntry>,
pub spec_base64: Option<String>,
pub spec: Vec<ScSpecEntry>,
}
impl Display for ContractSpec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(env_meta) = &self.env_meta_base64 {
writeln!(f, "Env Meta: {env_meta}")?;
for env_meta_entry in &self.env_meta {
match env_meta_entry {
ScEnvMetaEntry::ScEnvMetaKindInterfaceVersion(v) => {
writeln!(f, " • Interface Version: {v}")?;
}
}
}
} else {
writeln!(f, "Env Meta: None")?;
}
if let Some(spec_base64) = &self.spec_base64 {
writeln!(f, "Contract Spec: {spec_base64}")?;
for spec_entry in &self.spec {
match spec_entry {
ScSpecEntry::FunctionV0(func) => writeln!(
f,
" • Function: {} ({:?}) -> ({:?})",
func.name.to_string_lossy(),
func.inputs.as_slice(),
func.outputs.as_slice(),
)?,
ScSpecEntry::UdtUnionV0(udt) => {
writeln!(f, " • Union: {udt:?}")?;
}
ScSpecEntry::UdtStructV0(udt) => {
writeln!(f, " • Struct: {udt:?}")?;
}
ScSpecEntry::UdtEnumV0(udt) => {
writeln!(f, " • Enum: {udt:?}")?;
}
ScSpecEntry::UdtErrorEnumV0(udt) => {
writeln!(f, " • Error: {udt:?}")?;
}
}
}
} else {
writeln!(f, "Contract Spec: None")?;
}
Ok(())
}
}
pub fn len(p: &Path) -> Result<u64, Error> {
Ok(std::fs::metadata(p)
.map_err(|e| Error::CannotReadContractFile {
filepath: p.to_path_buf(),
error: e,
})?
.len())
}