-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
file.rs
362 lines (320 loc) Β· 9.44 KB
/
file.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use super::*;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Default)]
#[serde(deny_unknown_fields)]
pub struct File {
pub inscriptions: Vec<Entry>,
pub mode: Mode,
pub parent: Option<InscriptionId>,
pub postage: Option<u64>,
#[serde(default)]
pub reinscribe: bool,
pub etching: Option<Etching>,
pub sat: Option<Sat>,
pub satpoint: Option<SatPoint>,
}
impl File {
pub(crate) fn load(path: &Path) -> Result<Self> {
let batchfile: Self = serde_yaml::from_reader(fs::File::open(path)?)?;
ensure!(
!batchfile.inscriptions.is_empty(),
"batchfile must contain at least one inscription",
);
let sat_and_satpoint = batchfile.sat.is_some() && batchfile.satpoint.is_some();
ensure!(
!sat_and_satpoint,
"batchfile cannot set both `sat` and `satpoint`",
);
let sat_or_satpoint = batchfile.sat.is_some() || batchfile.satpoint.is_some();
if sat_or_satpoint {
ensure!(
batchfile.mode == Mode::SameSat,
"neither `sat` nor `satpoint` can be set in `same-sat` mode",
);
}
if batchfile
.inscriptions
.iter()
.any(|entry| entry.destination.is_some())
&& (batchfile.mode == Mode::SharedOutput || batchfile.mode == Mode::SameSat)
{
bail!(
"individual inscription destinations cannot be set in `shared-output` or `same-sat` mode"
);
}
let any_entry_has_satpoint = batchfile
.inscriptions
.iter()
.any(|entry| entry.satpoint.is_some());
if any_entry_has_satpoint {
ensure!(
batchfile.mode == Mode::SatPoints,
"specifying `satpoint` in an inscription only works in `satpoints` mode"
);
ensure!(
batchfile.inscriptions.iter().all(|entry| entry.satpoint.is_some()),
"if `satpoint` is set for any inscription, then all inscriptions need to specify a satpoint"
);
ensure!(
batchfile
.inscriptions
.iter()
.all(|entry| entry.satpoint.unwrap().offset == 0),
"`satpoint` can only be specified for first sat of an output"
);
}
if batchfile.mode == Mode::SatPoints {
ensure!(
batchfile.postage.is_none(),
"`postage` cannot be set if in `satpoints` mode"
);
ensure!(
batchfile.sat.is_none(),
"`sat` cannot be set if in `satpoints` mode"
);
ensure!(
batchfile.satpoint.is_none(),
"`satpoint cannot be set if in `satpoints` mode"
);
let mut seen = HashSet::new();
for entry in batchfile.inscriptions.iter() {
let satpoint = entry.satpoint.unwrap_or_default();
if !seen.insert(satpoint) {
bail!("duplicate satpoint {}", satpoint);
}
}
}
Ok(batchfile)
}
pub(crate) fn inscriptions(
&self,
wallet: &Wallet,
utxos: &BTreeMap<OutPoint, TxOut>,
parent_value: Option<u64>,
compress: bool,
) -> Result<(
Vec<Inscription>,
Vec<(SatPoint, TxOut)>,
Vec<Amount>,
Vec<Address>,
)> {
let mut inscriptions = Vec::new();
let mut reveal_satpoints = Vec::new();
let mut postages = Vec::new();
let mut pointer = parent_value.unwrap_or_default();
for (i, entry) in self.inscriptions.iter().enumerate() {
if let Some(delegate) = entry.delegate {
ensure! {
wallet.inscription_exists(delegate)?,
"delegate {delegate} does not exist"
}
}
inscriptions.push(Inscription::from_file(
wallet.chain(),
compress,
entry.delegate,
entry.metadata()?,
entry.metaprotocol.clone(),
self.parent.into_iter().collect(),
&entry.file,
Some(pointer),
self
.etching
.and_then(|etch| (i == 0).then_some(etch.rune.rune)),
)?);
let postage = if self.mode == Mode::SatPoints {
let satpoint = entry
.satpoint
.ok_or_else(|| anyhow!("no satpoint specified for {}", entry.file.display()))?;
let txout = utxos
.get(&satpoint.outpoint)
.ok_or_else(|| anyhow!("{} not in wallet", satpoint))?;
reveal_satpoints.push((satpoint, txout.clone()));
txout.value
} else {
self
.postage
.map(Amount::from_sat)
.unwrap_or(TARGET_POSTAGE)
.to_sat()
};
pointer += postage;
if self.mode == Mode::SameSat && i > 0 {
continue;
} else {
postages.push(Amount::from_sat(postage));
}
}
let destinations = match self.mode {
Mode::SharedOutput | Mode::SameSat => vec![wallet.get_change_address()?],
Mode::SeparateOutputs | Mode::SatPoints => self
.inscriptions
.iter()
.map(|entry| {
entry.destination.as_ref().map_or_else(
|| wallet.get_change_address(),
|address| {
address
.clone()
.require_network(wallet.chain().network())
.map_err(|e| e.into())
},
)
})
.collect::<Result<Vec<_>, _>>()?,
};
Ok((inscriptions, reveal_satpoints, postages, destinations))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn batchfile_not_sat_and_satpoint() {
let tempdir = TempDir::new().unwrap();
let batch_file = tempdir.path().join("batch.yaml");
fs::write(
batch_file.clone(),
r#"
mode: same-sat
sat: 55555
satpoint: 4651dc5e964879b1eb9183d467d1187dcd252504698002b01853446c460db2c5:0:0
inscriptions:
- file: inscription.txt
- file: tulip.png
- file: meow.wav
"#,
)
.unwrap();
assert_eq!(
File::load(batch_file.as_path()).unwrap_err().to_string(),
"batchfile cannot set both `sat` and `satpoint`"
);
}
#[test]
fn batchfile_wrong_mode_for_satpoints() {
let tempdir = TempDir::new().unwrap();
let batch_file = tempdir.path().join("batch.yaml");
fs::write(
batch_file.clone(),
r#"
mode: separate-outputs
inscriptions:
- file: inscription.txt
satpoint: bc4c30829a9564c0d58e6287195622b53ced54a25711d1b86be7cd3a70ef61ed:0:0
- file: tulip.png
satpoint: 5fddcbdc3eb21a93e8dd1dd3f9087c3677f422b82d5ba39a6b1ec37338154af6:0:0
- file: meow.wav
satpoint: 4651dc5e964879b1eb9183d467d1187dcd252504698002b01853446c460db2c5:0:0
"#,
)
.unwrap();
assert_eq!(
batch::File::load(batch_file.as_path())
.unwrap_err()
.to_string(),
"specifying `satpoint` in an inscription only works in `satpoints` mode"
);
}
#[test]
fn batchfile_missing_satpoint() {
let tempdir = TempDir::new().unwrap();
let batch_file = tempdir.path().join("batch.yaml");
fs::write(
batch_file.clone(),
r#"
mode: satpoints
inscriptions:
- file: inscription.txt
satpoint: bc4c30829a9564c0d58e6287195622b53ced54a25711d1b86be7cd3a70ef61ed:0:0
- file: tulip.png
- file: meow.wav
satpoint: 4651dc5e964879b1eb9183d467d1187dcd252504698002b01853446c460db2c5:0:0
"#,
)
.unwrap();
assert_eq!(
batch::File::load(batch_file.as_path())
.unwrap_err()
.to_string(),
"if `satpoint` is set for any inscription, then all inscriptions need to specify a satpoint"
);
}
#[test]
fn batchfile_only_first_sat_of_outpoint() {
let tempdir = TempDir::new().unwrap();
let batch_file = tempdir.path().join("batch.yaml");
fs::write(
batch_file.clone(),
r#"
mode: satpoints
inscriptions:
- file: inscription.txt
satpoint: bc4c30829a9564c0d58e6287195622b53ced54a25711d1b86be7cd3a70ef61ed:0:0
- file: tulip.png
satpoint: 5fddcbdc3eb21a93e8dd1dd3f9087c3677f422b82d5ba39a6b1ec37338154af6:0:21
- file: meow.wav
satpoint: 4651dc5e964879b1eb9183d467d1187dcd252504698002b01853446c460db2c5:0:0
"#,
)
.unwrap();
assert_eq!(
batch::File::load(batch_file.as_path())
.unwrap_err()
.to_string(),
"`satpoint` can only be specified for first sat of an output"
);
}
#[test]
fn batchfile_no_postage_if_mode_satpoints() {
let tempdir = TempDir::new().unwrap();
let batch_file = tempdir.path().join("batch.yaml");
fs::write(
batch_file.clone(),
r#"
mode: satpoints
postage: 1111
inscriptions:
- file: inscription.txt
satpoint: bc4c30829a9564c0d58e6287195622b53ced54a25711d1b86be7cd3a70ef61ed:0:0
- file: tulip.png
satpoint: 5fddcbdc3eb21a93e8dd1dd3f9087c3677f422b82d5ba39a6b1ec37338154af6:0:0
- file: meow.wav
satpoint: 4651dc5e964879b1eb9183d467d1187dcd252504698002b01853446c460db2c5:0:0
"#,
)
.unwrap();
assert_eq!(
batch::File::load(batch_file.as_path())
.unwrap_err()
.to_string(),
"`postage` cannot be set if in `satpoints` mode"
);
}
#[test]
fn batchfile_no_duplicate_satpoints() {
let tempdir = TempDir::new().unwrap();
let batch_file = tempdir.path().join("batch.yaml");
fs::write(
batch_file.clone(),
r#"
mode: satpoints
inscriptions:
- file: inscription.txt
satpoint: bc4c30829a9564c0d58e6287195622b53ced54a25711d1b86be7cd3a70ef61ed:0:0
- file: tulip.png
satpoint: 5fddcbdc3eb21a93e8dd1dd3f9087c3677f422b82d5ba39a6b1ec37338154af6:0:0
- file: meow.wav
satpoint: 4651dc5e964879b1eb9183d467d1187dcd252504698002b01853446c460db2c5:0:0
- file: inscription_1.txt
satpoint: bc4c30829a9564c0d58e6287195622b53ced54a25711d1b86be7cd3a70ef61ed:0:0
"#,
)
.unwrap();
assert_eq!(
batch::File::load(batch_file.as_path())
.unwrap_err()
.to_string(),
"duplicate satpoint bc4c30829a9564c0d58e6287195622b53ced54a25711d1b86be7cd3a70ef61ed:0:0"
);
}
}