-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtx_builder.rs
594 lines (543 loc) · 20.4 KB
/
tx_builder.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
use std::collections::{HashMap, HashSet};
use elements::{
confidential::Value,
issuance::ContractHash,
pset::{Output, PartiallySignedTransaction},
secp256k1_zkp::ZERO_TWEAK,
Address, AssetId, Script, Transaction,
};
use rand::thread_rng;
use crate::{
hashes::Hash,
model::{IssuanceDetails, Recipient},
pset_create::{validate_address, IssuanceRequest},
Contract, ElementsNetwork, Error, UnvalidatedRecipient, Wollet, EC,
};
pub fn extract_issuances(tx: &Transaction) -> Vec<IssuanceDetails> {
let mut r = vec![];
for (vin, txin) in tx.input.iter().enumerate() {
if txin.has_issuance() {
let contract_hash = ContractHash::from_byte_array(txin.asset_issuance.asset_entropy);
let entropy = AssetId::generate_asset_entropy(txin.previous_output, contract_hash)
.to_byte_array();
let (asset, token) = txin.issuance_ids();
let is_reissuance = txin.asset_issuance.asset_blinding_nonce != ZERO_TWEAK;
// FIXME: attempt to unblind if blinded
let asset_amount = match txin.asset_issuance.amount {
Value::Explicit(a) => Some(a),
_ => None,
};
let token_amount = match txin.asset_issuance.inflation_keys {
Value::Explicit(a) => Some(a),
_ => None,
};
// FIXME: comment if the issuance is blinded
r.push(IssuanceDetails {
txid: tx.txid(),
vin: vin as u32,
entropy,
asset,
token,
is_reissuance,
asset_amount,
token_amount,
});
}
}
r
}
/// A transaction builder
///
/// See [`WolletTxBuilder`] for usage from rust.
///
/// Design decisions:
///
/// * We are not holding a reference of the wallet in the struct and we instead pass a reference
/// of the wallet in the finish methods because this it more friendly for bindings implementation.
/// Moreover, we could have an alternative finish which don't use a wallet at all.
/// * We are consuming and returning self to build the tx with method chaining
#[derive(Debug)]
pub struct TxBuilder {
network: ElementsNetwork,
recipients: Vec<Recipient>,
fee_rate: f32,
issuance_request: IssuanceRequest,
drain_lbtc: bool,
drain_to: Option<Address>,
}
impl TxBuilder {
/// Creates a transaction builder for bindings code. From rust use [`WolletTxBuilder`]
pub fn new(network: ElementsNetwork) -> Self {
TxBuilder {
network,
recipients: vec![],
fee_rate: 100.0,
issuance_request: IssuanceRequest::None,
drain_lbtc: false,
drain_to: None,
}
}
fn network(&self) -> ElementsNetwork {
self.network
}
pub fn add_recipient(
self,
address: &Address,
satoshi: u64,
asset_id: AssetId,
) -> Result<Self, Error> {
let rec = UnvalidatedRecipient {
satoshi,
address: address.to_string(),
asset: asset_id.to_string(),
};
self.add_unvalidated_recipient(&rec)
}
pub fn add_unvalidated_recipient(
mut self,
recipient: &UnvalidatedRecipient,
) -> Result<Self, Error> {
let addr: Recipient = recipient.validate(self.network())?;
self.recipients.push(addr);
Ok(self)
}
pub fn add_validated_recipient(mut self, recipient: Recipient) -> Self {
self.recipients.push(recipient);
self
}
/// Replace current recipients with the given list
pub fn set_unvalidated_recipients(
mut self,
recipients: &[UnvalidatedRecipient],
) -> Result<Self, Error> {
self.recipients.clear();
for recipient in recipients {
self = self.add_unvalidated_recipient(recipient)?;
}
Ok(self)
}
pub fn add_lbtc_recipient(self, address: &Address, satoshi: u64) -> Result<Self, Error> {
let rec = UnvalidatedRecipient::lbtc(address.to_string(), satoshi);
self.add_unvalidated_recipient(&rec)
}
pub fn add_burn(self, satoshi: u64, asset_id: AssetId) -> Result<Self, Error> {
let rec = UnvalidatedRecipient::burn(asset_id.to_string(), satoshi);
self.add_unvalidated_recipient(&rec)
}
pub fn fee_rate(mut self, fee_rate: Option<f32>) -> Self {
if let Some(fee_rate) = fee_rate {
self.fee_rate = fee_rate
}
self
}
/// Issue an asset
///
/// There will be `asset_sats` units of this asset that will be received by
/// `asset_receiver` if it's set, otherwise to an address of the wallet generating the issuance.
///
/// There will be `token_sats` reissuance tokens that allow token holder to reissue the created
/// asset. Reissuance token will be received by `token_receiver` if it's some, or to an
/// address of the wallet generating the issuance if none.
///
/// If a `contract` is provided, it's metadata will be committed in the generated asset id.
///
/// Can't be used if `reissue_asset` has been called
pub fn issue_asset(
mut self,
asset_sats: u64,
asset_receiver: Option<Address>,
token_sats: u64,
token_receiver: Option<Address>,
contract: Option<Contract>,
) -> Result<Self, Error> {
if !matches!(self.issuance_request, IssuanceRequest::None) {
return Err(Error::IssuanceAlreadySet);
}
if let Some(addr) = asset_receiver.as_ref() {
validate_address(&addr.to_string(), self.network())?;
}
if let Some(addr) = token_receiver.as_ref() {
validate_address(&addr.to_string(), self.network())?;
}
if asset_sats == 0 {
return Err(Error::InvalidAmount);
}
self.issuance_request = IssuanceRequest::Issuance(
asset_sats,
asset_receiver,
token_sats,
token_receiver,
contract,
);
Ok(self)
}
/// Reissue an asset
///
/// reissue the asset defined by `asset_to_reissue`, provided the reissuance token is owned
/// by the wallet generating te reissuance.
///
/// Generated transaction will create `satoshi_to_reissue` new asset units, and they will be
/// sent to the provided `asset_receiver` address if some, or to an address from the wallet
/// generating the reissuance transaction if none.
///
/// If the issuance transaction does not involve this wallet,
/// pass the issuance transaction in `issuance_tx`.
pub fn reissue_asset(
mut self,
asset_to_reissue: AssetId,
satoshi_to_reissue: u64,
asset_receiver: Option<Address>,
issuance_tx: Option<Transaction>,
) -> Result<Self, Error> {
if !matches!(self.issuance_request, IssuanceRequest::None) {
return Err(Error::IssuanceAlreadySet);
}
if let Some(addr) = asset_receiver.as_ref() {
validate_address(&addr.to_string(), self.network())?;
}
if satoshi_to_reissue == 0 {
return Err(Error::InvalidAmount);
}
self.issuance_request = IssuanceRequest::Reissuance(
asset_to_reissue,
satoshi_to_reissue,
asset_receiver,
issuance_tx,
);
Ok(self)
}
/// Select all available L-BTC inputs
pub fn drain_lbtc_wallet(mut self) -> Self {
self.drain_lbtc = true;
self
}
/// Sets the address to drain excess L-BTC to
pub fn drain_lbtc_to(mut self, address: Address) -> Self {
self.drain_to = Some(address);
self
}
pub fn finish(self, wollet: &Wollet) -> Result<PartiallySignedTransaction, Error> {
// Init PSET
let mut pset = PartiallySignedTransaction::new_v2();
let mut inp_txout_sec = HashMap::new();
let mut last_unused_internal = wollet.change(None)?.index();
let mut last_unused_external = wollet.address(None)?.index();
let mut inp_weight = 0;
let policy_asset = self.network().policy_asset();
let (addressees_lbtc, addressees_asset): (Vec<_>, Vec<_>) = self
.recipients
.into_iter()
.partition(|a| a.asset == policy_asset);
// Assets inputs and outputs
let assets: HashSet<_> = addressees_asset.iter().map(|a| a.asset).collect();
for asset in assets {
let mut satoshi_out = 0;
let mut satoshi_in = 0;
for addressee in addressees_asset.iter().filter(|a| a.asset == asset) {
wollet.add_output(&mut pset, addressee)?;
satoshi_out += addressee.satoshi;
}
for utxo in wollet.asset_utxos(&asset)? {
wollet.add_input(&mut pset, &mut inp_txout_sec, &mut inp_weight, &utxo)?;
satoshi_in += utxo.unblinded.value;
if satoshi_in >= satoshi_out {
if satoshi_in > satoshi_out {
let satoshi_change = satoshi_in - satoshi_out;
let addressee = wollet.addressee_change(
satoshi_change,
asset,
&mut last_unused_internal,
)?;
wollet.add_output(&mut pset, &addressee)?;
}
break;
}
}
if satoshi_in < satoshi_out {
return Err(Error::InsufficientFunds);
}
}
// L-BTC inputs and outputs
// Fee and L-BTC change after (re)issuance
let mut satoshi_out = 0;
let mut satoshi_in = 0;
for addressee in addressees_lbtc {
wollet.add_output(&mut pset, &addressee)?;
satoshi_out += addressee.satoshi;
}
// FIXME: For implementation simplicity now we always add all L-BTC inputs
for utxo in wollet.asset_utxos(&wollet.policy_asset())? {
wollet.add_input(&mut pset, &mut inp_txout_sec, &mut inp_weight, &utxo)?;
satoshi_in += utxo.unblinded.value;
}
// Set (re)issuance data
match self.issuance_request {
IssuanceRequest::None => {}
IssuanceRequest::Issuance(
satoshi_asset,
address_asset,
satoshi_token,
address_token,
contract,
) => {
// At least a L-BTC input for the fee was added.
let idx = 0;
let (asset, token) =
wollet.set_issuance(&mut pset, idx, satoshi_asset, satoshi_token, contract)?;
let addressee = match address_asset {
Some(address) => Recipient::from_address(satoshi_asset, &address, asset),
None => wollet.addressee_external(
satoshi_asset,
asset,
&mut last_unused_external,
)?,
};
wollet.add_output(&mut pset, &addressee)?;
if satoshi_token > 0 {
let addressee = match address_token {
Some(address) => Recipient::from_address(satoshi_token, &address, token),
None => wollet.addressee_external(
satoshi_token,
token,
&mut last_unused_external,
)?,
};
wollet.add_output(&mut pset, &addressee)?;
}
}
IssuanceRequest::Reissuance(asset, satoshi_asset, address_asset, issuance_tx) => {
let issuance = if let Some(issuance_tx) = issuance_tx {
extract_issuances(&issuance_tx)
.iter()
.find(|i| i.asset == asset)
.ok_or_else(|| Error::MissingIssuance)?
.clone()
} else {
wollet.issuance(&asset)?
};
let token = issuance.token;
// Find or add input for the token
let (idx, token_asset_bf) =
match inp_txout_sec.iter().find(|(_, u)| u.asset == token) {
Some((idx, u)) => (*idx, u.asset_bf),
None => {
// Add an input sending the token,
let utxos_token = wollet.asset_utxos(&token)?;
let utxo_token = utxos_token
.first()
.ok_or_else(|| Error::InsufficientFunds)?;
let idx = wollet.add_input(
&mut pset,
&mut inp_txout_sec,
&mut inp_weight,
utxo_token,
)?;
// and an outpout receiving the token
let satoshi_token = utxo_token.unblinded.value;
let addressee = wollet.addressee_change(
satoshi_token,
token,
&mut last_unused_internal,
)?;
wollet.add_output(&mut pset, &addressee)?;
(idx, utxo_token.unblinded.asset_bf)
}
};
// Set reissuance data
wollet.set_reissuance(
&mut pset,
idx,
satoshi_asset,
&token_asset_bf,
&issuance.entropy,
)?;
let addressee = match address_asset {
Some(address) => Recipient::from_address(satoshi_asset, &address, asset),
None => wollet.addressee_external(
satoshi_asset,
asset,
&mut last_unused_external,
)?,
};
wollet.add_output(&mut pset, &addressee)?;
}
}
// Add a temporary fee, and always add a change output,
// then we'll tweak those values to match the given fee rate.
let temp_fee = 1000;
if satoshi_in < (satoshi_out + temp_fee) {
return Err(Error::InsufficientFunds);
}
let satoshi_change = satoshi_in - satoshi_out - temp_fee;
let addressee = if let Some(address) = self.drain_to {
Recipient::from_address(satoshi_change, &address, wollet.policy_asset())
} else {
wollet.addressee_change(
satoshi_change,
wollet.policy_asset(),
&mut last_unused_internal,
)?
};
wollet.add_output(&mut pset, &addressee)?;
let fee_output =
Output::new_explicit(Script::default(), temp_fee, wollet.policy_asset(), None);
pset.add_output(fee_output);
let weight = {
let mut rng = thread_rng();
let mut temp_pset = pset.clone();
temp_pset.blind_last(&mut rng, &EC, &inp_txout_sec)?;
inp_weight + temp_pset.extract_tx()?.weight()
};
let vsize = (weight + 4 - 1) / 4;
let fee = (vsize as f32 * self.fee_rate / 1000.0).ceil() as u64;
if satoshi_in < (satoshi_out + temp_fee) {
return Err(Error::InsufficientFunds);
}
let satoshi_change = satoshi_in - satoshi_out - fee;
// Replace change and fee outputs
let n_outputs = pset.n_outputs();
let outputs = pset.outputs_mut();
let change_output = &mut outputs[n_outputs - 2]; // index check: we always have the lbtc change and the fee output at least
change_output.amount = Some(satoshi_change);
let fee_output = &mut outputs[n_outputs - 1];
fee_output.amount = Some(fee);
// TODO inputs/outputs(except fee) randomization, not trivial because of blinder_index on inputs
// Blind the transaction
let mut rng = thread_rng();
pset.blind_last(&mut rng, &EC, &inp_txout_sec)?;
// Add details to the pset from our descriptor, like bip32derivation and keyorigin
wollet.add_details(&mut pset)?;
Ok(pset)
}
}
/// A transaction builder.
#[derive(Debug)]
pub struct WolletTxBuilder<'a> {
wollet: &'a Wollet,
inner: TxBuilder,
}
impl<'a> WolletTxBuilder<'a> {
/// Creates a transaction builder. Could be conveniently created with [`Wollet::tx_builder()`]
pub fn new(wollet: &'a Wollet) -> Self {
WolletTxBuilder {
wollet,
inner: TxBuilder::new(wollet.network()),
}
}
/// Consume this builder and create a transaction
pub fn finish(self) -> Result<PartiallySignedTransaction, Error> {
self.inner.finish(self.wollet)
}
/// Wrapper of [`TxBuilder::add_recipient()`]
pub fn add_recipient(
self,
address: &Address,
satoshi: u64,
asset_id: AssetId,
) -> Result<Self, Error> {
Ok(Self {
wollet: self.wollet,
inner: self.inner.add_recipient(address, satoshi, asset_id)?,
})
}
/// Wrapper of [`TxBuilder::add_unvalidated_recipient()`]
pub fn add_unvalidated_recipient(
self,
recipient: &UnvalidatedRecipient,
) -> Result<Self, Error> {
Ok(Self {
wollet: self.wollet,
inner: self.inner.add_unvalidated_recipient(recipient)?,
})
}
/// Wrapper of [`TxBuilder::add_validated_recipient()`]
pub fn add_validated_recipient(self, recipient: Recipient) -> Self {
Self {
wollet: self.wollet,
inner: self.inner.add_validated_recipient(recipient),
}
}
/// Wrapper of [`TxBuilder::set_unvalidated_recipients()`]
pub fn set_unvalidated_recipients(
self,
recipients: &[UnvalidatedRecipient],
) -> Result<Self, Error> {
Ok(Self {
wollet: self.wollet,
inner: self.inner.set_unvalidated_recipients(recipients)?,
})
}
/// Wrapper of [`TxBuilder::add_lbtc_recipient()`]
pub fn add_lbtc_recipient(self, address: &Address, satoshi: u64) -> Result<Self, Error> {
Ok(Self {
wollet: self.wollet,
inner: self.inner.add_lbtc_recipient(address, satoshi)?,
})
}
/// Wrapper of [`TxBuilder::add_burn()`]
pub fn add_burn(self, satoshi: u64, asset_id: AssetId) -> Result<Self, Error> {
Ok(Self {
wollet: self.wollet,
inner: self.inner.add_burn(satoshi, asset_id)?,
})
}
/// Wrapper of [`TxBuilder::fee_rate()`]
pub fn fee_rate(self, fee_rate: Option<f32>) -> Self {
Self {
wollet: self.wollet,
inner: self.inner.fee_rate(fee_rate),
}
}
/// Wrapper of [`TxBuilder::issue_asset()`]
pub fn issue_asset(
self,
asset_sats: u64,
asset_receiver: Option<Address>,
token_sats: u64,
token_receiver: Option<Address>,
contract: Option<Contract>,
) -> Result<Self, Error> {
Ok(Self {
wollet: self.wollet,
inner: self.inner.issue_asset(
asset_sats,
asset_receiver,
token_sats,
token_receiver,
contract,
)?,
})
}
/// Wrapper of [`TxBuilder::reissue_asset()`]
pub fn reissue_asset(
self,
asset_to_reissue: AssetId,
satoshi_to_reissue: u64,
asset_receiver: Option<Address>,
issuance_tx: Option<Transaction>,
) -> Result<Self, Error> {
Ok(Self {
wollet: self.wollet,
inner: self.inner.reissue_asset(
asset_to_reissue,
satoshi_to_reissue,
asset_receiver,
issuance_tx,
)?,
})
}
/// Wrapper of [`TxBuilder::drain_lbtc_wallet()`]
pub fn drain_lbtc_wallet(self) -> Self {
Self {
wollet: self.wollet,
inner: self.inner.drain_lbtc_wallet(),
}
}
/// Wrapper of [`TxBuilder::drain_lbtc_to()`]
pub fn drain_lbtc_to(self, address: Address) -> Self {
Self {
wollet: self.wollet,
inner: self.inner.drain_lbtc_to(address),
}
}
}