forked from BlockstreamResearch/simplicity-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.rs
More file actions
456 lines (400 loc) · 16.6 KB
/
basic.rs
File metadata and controls
456 lines (400 loc) · 16.6 KB
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
use crate::modules::store::Store;
use crate::modules::utils::derive_keypair;
use anyhow::anyhow;
use crate::commands::NETWORK;
use crate::explorer::{broadcast_tx, fetch_utxo};
use clap::Subcommand;
use simplicityhl::elements::hashes::{Hash, sha256};
use simplicityhl::elements::pset::serialize::Serialize;
use simplicityhl::elements::secp256k1_zkp::SECP256K1;
use simplicityhl::elements::{AssetId, ContractHash};
use simplicityhl::simplicity::elements::{Address, OutPoint};
use simplicityhl::simplicity::hex::DisplayHex;
use simplicityhl::tracker::TrackerLogLevel;
use simplicityhl_core::{
create_p2pk_signature, derive_public_blinder_key, finalize_p2pk_transaction, get_p2pk_address,
hash_script,
};
#[derive(Subcommand, Debug)]
pub enum Basic {
/// Print a deterministic Liquid testnet address derived from index
Address {
/// Address index (0-based)
index: u32,
},
/// Build tx transferring LBTC (explicit) to recipient
TransferNative {
/// Transaction id (hex) and output index (vout) of the UTXO you will spend
#[arg(long = "utxo")]
utxo_outpoint: OutPoint,
/// Recipient address (Liquid testnet bech32m)
#[arg(long = "to-address")]
to_address: Address,
/// Amount to send to the recipient in satoshis (LBTC)
#[arg(long = "send-sats")]
amount_to_send: u64,
/// Miner fee in satoshis (LBTC)
#[arg(long = "fee-sats")]
fee_amount: u64,
/// Account that will pay for transaction fees and that owns a tokens to send
#[arg(long = "account-index", default_value_t = 0)]
account_index: u32,
/// When set, broadcast the built transaction via Esplora and print txid
#[arg(long = "broadcast")]
broadcast: bool,
},
/// Build tx splitting one LBTC UTXO into any number of UTXOs.
SplitNative {
/// Transaction id (hex) and output index (vout) of the UTXO you will split
#[arg(long = "fee-utxo")]
fee_utxo: OutPoint,
/// Number of UTXOs to split the LBTC UTXO into
#[arg(long = "split-parts")]
split_parts: u64,
/// Miner fee in satoshis (LBTC)
#[arg(long = "fee-amount")]
fee_amount: u64,
/// Account that will pay for transaction fees and that owns a tokens to split
#[arg(long = "account-index", default_value_t = 0)]
account_index: u32,
/// When set, broadcast the built transaction via Esplora and print txid
#[arg(long = "broadcast")]
broadcast: bool,
},
/// Build tx transferring an asset UTXO to recipient (LBTC UTXO pays fees)
TransferAsset {
/// Transaction id (hex) and output index (vout) of the ASSET UTXO you will spend
#[arg(long = "asset-utxo")]
asset_utxo: OutPoint,
/// Transaction id (hex) and output index (vout) of the LBTC UTXO used to pay fees
#[arg(long = "fee-utxo")]
fee_utxo: OutPoint,
/// Recipient address (Liquid testnet bech32m)
#[arg(long = "to-address")]
to_address: Address,
/// Amount to send of the asset in its smallest units (it does not account for decimals)
#[arg(long = "send-sats")]
send_amount: u64,
/// Miner fee in satoshis (LBTC)
#[arg(long = "fee-sats")]
fee_amount: u64,
/// Account that will pay for transaction fees and that owns a tokens to send
#[arg(long = "account-index", default_value_t = 0)]
account_index: u32,
/// When set, broadcast the built transaction via Esplora and print txid
#[arg(long = "broadcast")]
broadcast: bool,
},
/// Build tx issuing an asset
IssueAsset {
/// Transaction id (hex) and output index (vout) of the LBTC UTXO used to pay fees and issue the asset
#[arg(long = "fee-utxo")]
fee_utxo_outpoint: OutPoint,
/// Asset name (this will be stored in the CLI's database only, so it will be not shown on the Esplora UI)
#[arg(long = "asset-name")]
asset_name: String,
/// Amount to issue of the asset in its satoshi units
#[arg(long = "issue-sats")]
issue_amount: u64,
/// Miner fee in satoshis (LBTC)
#[arg(long = "fee-sats")]
fee_amount: u64,
/// Account that will pay for transaction fees and that owns a tokens to send
#[arg(long = "account-index", default_value_t = 0)]
account_index: u32,
/// When set, broadcast the built transaction via Esplora and print txid
#[arg(long = "broadcast")]
broadcast: bool,
},
/// Reissue an asset
ReissueAsset {
/// Transaction id (hex) and output index (vout) of the REISSUANCE ASSET UTXO you will spend
#[arg(long = "reissue-asset-utxo")]
reissue_asset_outpoint: OutPoint,
/// Transaction id (hex) and output index (vout) of the LBTC UTXO used to pay fees and reissue the asset
#[arg(long = "fee-utxo")]
fee_utxo_outpoint: OutPoint,
/// Asset name (this will be stored in the CLI's database only, so it will be not shown on the Esplora UI)
#[arg(long = "asset-name")]
asset_name: String,
/// Amount to reissue of the asset in its satoshi units
#[arg(long = "reissue-sats")]
reissue_amount: u64,
/// Miner fee in satoshis (LBTC)
#[arg(long = "fee-sats")]
fee_amount: u64,
/// Account that will pay for transaction fees and that owns a tokens to send
#[arg(long = "account-index", default_value_t = 0)]
account_index: u32,
/// When set, broadcast the built transaction via Esplora and print txid
#[arg(long = "broadcast")]
broadcast: bool,
},
}
impl Basic {
/// Handle basic CLI subcommand execution.
///
/// # Errors
/// Returns error if the subcommand operation fails.
///
/// # Panics
/// Panics if asset entropy conversion fails.
#[expect(clippy::too_many_lines)]
pub async fn handle(&self) -> anyhow::Result<()> {
match self {
Self::Address { index } => {
let keypair = derive_keypair(*index);
let public_key = keypair.x_only_public_key().0;
let address = get_p2pk_address(&public_key, NETWORK)?;
let mut script_hash: [u8; 32] = hash_script(&address.script_pubkey());
script_hash.reverse();
println!("X Only Public Key: {public_key}");
println!("P2PK Address: {address}");
println!("Script hash: {}", hex::encode(script_hash));
Ok(())
}
Self::TransferNative {
utxo_outpoint,
to_address,
amount_to_send,
fee_amount,
account_index,
broadcast,
} => {
let keypair = derive_keypair(*account_index);
let tx_out = fetch_utxo(*utxo_outpoint).await?;
let pst = contracts::sdk::transfer_native(
(*utxo_outpoint, tx_out.clone()),
to_address,
*amount_to_send,
*fee_amount,
)?;
let tx = pst.extract_tx()?;
let utxos = &[tx_out];
let x_only_public_key = keypair.x_only_public_key().0;
let signature = create_p2pk_signature(&tx, utxos, &keypair, 0, NETWORK)?;
let tx = finalize_p2pk_transaction(
tx,
utxos,
&x_only_public_key,
&signature,
0,
NETWORK,
TrackerLogLevel::None,
)?;
if *broadcast {
println!("Broadcasted txid: {}", broadcast_tx(&tx).await?);
} else {
println!("{}", tx.serialize().to_lower_hex_string());
}
Ok(())
}
Self::SplitNative {
split_parts,
fee_utxo,
fee_amount,
account_index,
broadcast,
} => {
let keypair = derive_keypair(*account_index);
let tx_out = fetch_utxo(*fee_utxo).await?;
let pst = contracts::sdk::split_native_any(
(*fee_utxo, tx_out.clone()),
*split_parts,
*fee_amount,
)?;
let tx = pst.extract_tx()?;
let utxos = &[tx_out];
let x_only_public_key = keypair.x_only_public_key().0;
let signature = create_p2pk_signature(&tx, utxos, &keypair, 0, NETWORK)?;
let tx = finalize_p2pk_transaction(
tx,
utxos,
&x_only_public_key,
&signature,
0,
NETWORK,
TrackerLogLevel::None,
)?;
if *broadcast {
println!("Broadcasted txid: {}", broadcast_tx(&tx).await?);
} else {
println!("{}", tx.serialize().to_lower_hex_string());
}
Ok(())
}
Self::TransferAsset {
asset_utxo: asset_utxo_outpoint,
fee_utxo: fee_utxo_outpoint,
to_address,
send_amount,
fee_amount,
account_index,
broadcast,
} => {
let keypair = derive_keypair(*account_index);
let asset_tx_out = fetch_utxo(*asset_utxo_outpoint).await?;
let fee_tx_out = fetch_utxo(*fee_utxo_outpoint).await?;
let pst = contracts::sdk::transfer_asset(
(*asset_utxo_outpoint, asset_tx_out.clone()),
(*fee_utxo_outpoint, fee_tx_out.clone()),
to_address,
*send_amount,
*fee_amount,
)?;
let tx = pst.extract_tx()?;
let utxos = vec![asset_tx_out, fee_tx_out];
let x_only_public_key = keypair.x_only_public_key().0;
let signature_0 = create_p2pk_signature(&tx, &utxos, &keypair, 0, NETWORK)?;
let tx = finalize_p2pk_transaction(
tx,
&utxos,
&x_only_public_key,
&signature_0,
0,
NETWORK,
TrackerLogLevel::None,
)?;
let signature_1 = create_p2pk_signature(&tx, &utxos, &keypair, 1, NETWORK)?;
let tx = finalize_p2pk_transaction(
tx,
&utxos,
&x_only_public_key,
&signature_1,
1,
NETWORK,
TrackerLogLevel::None,
)?;
if *broadcast {
println!("Broadcasted txid: {}", broadcast_tx(&tx).await?);
} else {
println!("{}", tx.serialize().to_lower_hex_string());
}
Ok(())
}
Self::IssueAsset {
fee_utxo_outpoint,
asset_name,
issue_amount,
fee_amount,
account_index,
broadcast,
} => {
let store = Store::load()?;
if store.store.get(asset_name)?.is_some() {
return Err(anyhow!("Asset name already exists"));
}
let keypair = derive_keypair(*account_index);
let blinding_keypair = derive_public_blinder_key();
let fee_tx_out = fetch_utxo(*fee_utxo_outpoint).await?;
let pst = contracts::sdk::issue_asset(
&blinding_keypair.public_key(),
(*fee_utxo_outpoint, fee_tx_out.clone()),
*issue_amount,
*fee_amount,
)?;
let (asset_id, reissuance_asset_id) = pst.inputs()[0].issuance_ids();
let asset_entropy = pst.inputs()[0]
.issuance_asset_entropy
.expect("expected entropy");
let asset_entropy = AssetId::generate_asset_entropy(
*fee_utxo_outpoint,
ContractHash::from_byte_array(asset_entropy),
);
let tx = pst.extract_tx()?;
let utxos = &[fee_tx_out];
let x_only_public_key = keypair.x_only_public_key().0;
let signature = create_p2pk_signature(&tx, utxos, &keypair, 0, NETWORK)?;
let tx = finalize_p2pk_transaction(
tx,
utxos,
&x_only_public_key,
&signature,
0,
NETWORK,
TrackerLogLevel::None,
)?;
println!(
"Asset id: {asset_id}, \
Reissuance asset: {reissuance_asset_id}, \
Asset entropy: {}",
hex::encode(asset_entropy)
);
if *broadcast {
println!("Broadcasted txid: {}", broadcast_tx(&tx).await?);
} else {
println!("{}", tx.serialize().to_lower_hex_string());
}
store
.store
.insert(asset_name, hex::encode(asset_entropy).as_bytes())?;
Ok(())
}
Self::ReissueAsset {
reissue_asset_outpoint,
fee_utxo_outpoint,
asset_name,
reissue_amount,
fee_amount,
account_index,
broadcast,
} => {
let store = Store::load()?;
let Some(asset_entropy) = store.store.get(asset_name)? else {
return Err(anyhow!("Asset name not found"));
};
let asset_entropy = String::from_utf8(asset_entropy.to_vec())?;
let asset_entropy = hex::decode(asset_entropy)?;
let asset_entropy_bytes: [u8; 32] =
asset_entropy.try_into().expect("expected 32 bytes");
let asset_entropy = sha256::Midstate::from_byte_array(asset_entropy_bytes);
let keypair = derive_keypair(*account_index);
let blinding_keypair = derive_public_blinder_key();
let reissue_tx_out = fetch_utxo(*reissue_asset_outpoint).await?;
let fee_tx_out = fetch_utxo(*fee_utxo_outpoint).await?;
let reissue_utxo_secrets =
reissue_tx_out.unblind(SECP256K1, blinding_keypair.secret_key())?;
let pst = contracts::sdk::reissue_asset(
&blinding_keypair.public_key(),
(*reissue_asset_outpoint, reissue_tx_out.clone()),
reissue_utxo_secrets,
(*fee_utxo_outpoint, fee_tx_out.clone()),
*reissue_amount,
*fee_amount,
asset_entropy,
)?;
let (asset_id, reissuance_asset_id) = pst.inputs()[0].issuance_ids();
let tx = pst.extract_tx()?;
let utxos = vec![reissue_tx_out, fee_tx_out];
let x_only_public_key = keypair.x_only_public_key().0;
let signature_0 = create_p2pk_signature(&tx, &utxos, &keypair, 0, NETWORK)?;
let tx = finalize_p2pk_transaction(
tx,
&utxos,
&x_only_public_key,
&signature_0,
0,
NETWORK,
TrackerLogLevel::None,
)?;
let signature_1 = create_p2pk_signature(&tx, &utxos, &keypair, 1, NETWORK)?;
let tx = finalize_p2pk_transaction(
tx,
&utxos,
&x_only_public_key,
&signature_1,
1,
NETWORK,
TrackerLogLevel::None,
)?;
println!("Asset id: {asset_id}, Reissuance id: {reissuance_asset_id}");
if *broadcast {
println!("Broadcasted txid: {}", broadcast_tx(&tx).await?);
} else {
println!("{}", tx.serialize().to_lower_hex_string());
}
Ok(())
}
}
}
}