urma-runtime 0.1.0

URMA publication planning, network transports and recovery
Documentation
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use crate::commitment::PreparedReveal;
use crate::config::Limits;
use crate::config::{
    LITECOIN_MAX_FEE_LITOSHIS, LITECOIN_MAX_SCAN_BLOCKS, LITECOIN_MAX_SCAN_BYTES, LITECOIN_NETWORK,
    LITECOIN_RETURN_LITOSHIS,
};
use crate::config::{RpcScope, ScanEnd, confirmations};
use crate::error::{Context, Error, bail, ensure};
use crate::journal;
use crate::transaction::{decode as decode_transaction, transaction};
use bitcoin::{
    Amount, OutPoint, ScriptBuf, Transaction, TxOut, Witness,
    consensus::serialize,
    hashes::Hash,
    secp256k1::{Keypair, Secp256k1, SecretKey},
};
use bitcoincore_rpc::{Auth, Client, RpcApi};
use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::{path::Path, str::FromStr};
use urma_core::envelope;
use urma_core::format::RecordKind;
use urma_core::format::Urma;
use urma_wallet::funding::Funding;

use crate::{
    backend::{Evidence, Family, Locator, Observation, RecordSource},
    container,
};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Quote {
    pub network: String,
    pub records: usize,
    pub fee_rate_litoshi_vb: u64,
    pub commit_fee_litoshis: u64,
    pub reveal_fee_litoshis: u64,
    pub total_fee_litoshis: u64,
    pub minimum_funding_litoshis: u64,
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Plan {
    pub network: String,
    pub version: u8,
    pub input_bytes: usize,
    pub start_height: u64,
    pub fee_rate_litoshi_vb: u64,
    pub funding: Funding,
    pub commit: String,
    pub reveals: Vec<String>,
}

fn decode(raw: &str) -> Result<Transaction, Error> {
    ensure!(
        raw.len() <= 800_000,
        "transparent transaction exceeds limit"
    );
    decode_transaction(raw, 800_000).map_err(|cause| match cause {
        Error::Context { cause, .. } => Error::Context {
            message: "invalid transparent transaction (MWEB funding is unsupported)".into(),
            cause,
        },
        cause => cause,
    })
}

pub fn quote(input_bytes: usize, rate: u64) -> Result<Quote, Error> {
    if input_bytes > Limits::INPUT_BYTES {
        return Err(Error::Capacity("client input capacity exceeded".into()));
    }

    ensure!(
        (1..=Limits::INPUT_BYTES).contains(&input_bytes),
        "input exceeds client publication capacity"
    );
    ensure!(
        (1..=100).contains(&rate),
        "fee rate must be 1..100 litoshi/vB"
    );
    let count = input_bytes.div_ceil(Urma::CHUNK_BYTES);
    let secp = Secp256k1::new();
    let signer = Keypair::from_secret_key(&secp, &SecretKey::from_slice(&[1; 32])?);
    let mut sizing_record = vec![0; Urma::PRIVATE_RECORD_BYTES];
    sizing_record[..8].copy_from_slice(&RecordKind::Private.prefix());
    sizing_record[60..64].copy_from_slice(&1u32.to_le_bytes());
    let (script, info) = envelope::build(&sizing_record, &signer)?;
    let payout = ScriptBuf::new_p2wpkh(&bitcoin::WPubkeyHash::from_byte_array([0; 20]));
    let mut reveal = transaction(
        OutPoint::null(),
        TxOut {
            value: Amount::from_sat(LITECOIN_RETURN_LITOSHIS),
            script_pubkey: payout.clone(),
        },
    );
    reveal.input[0].witness = envelope::witness(&[0; 64], &script, &info)?;
    let reveal_fee = u64::try_from(reveal.vsize())? * rate;
    let mut commit = transaction(
        OutPoint::null(),
        TxOut {
            value: Amount::from_sat(LITECOIN_RETURN_LITOSHIS),
            script_pubkey: payout.clone(),
        },
    );
    commit.output = vec![
        TxOut {
            value: Amount::ZERO,
            script_pubkey: ScriptBuf::new_p2tr_tweaked(info.output_key())
        };
        count
    ];
    commit.output.push(TxOut {
        value: Amount::ZERO,
        script_pubkey: payout,
    });
    commit.input[0].witness = Witness::from_slice(&[vec![0; 73], vec![0; 33]]);
    let commit_fee = u64::try_from(commit.vsize())? * rate;
    let total = commit_fee + reveal_fee * u64::try_from(count)?;
    ensure!(
        total <= LITECOIN_MAX_FEE_LITOSHIS,
        "client fee cap exceeded"
    );
    Ok(Quote {
        network: LITECOIN_NETWORK.into(),
        records: count,
        fee_rate_litoshi_vb: rate,
        commit_fee_litoshis: commit_fee,
        reveal_fee_litoshis: reveal_fee * u64::try_from(count)?,
        total_fee_litoshis: total,
        minimum_funding_litoshis: total + LITECOIN_RETURN_LITOSHIS * (u64::try_from(count)? + 1),
    })
}

pub fn prepare<R: RngCore + CryptoRng>(
    records: &[Vec<u8>],
    input_bytes: usize,
    funding: Funding,
    rate: u64,
    start_height: u64,
    rng: &mut R,
) -> Result<Plan, Error> {
    let costs = quote(input_bytes, rate)?;
    ensure!(
        records.len() == costs.records,
        "record count disagrees with input length"
    );
    let first = container::inspect_header(&records[0])?;
    for (i, record) in records.iter().enumerate() {
        let h = container::inspect_header(record)?;
        ensure!(
            h.id == first.id
                && h.discovery_tag == first.discovery_tag
                && usize::try_from(h.index)? == i
                && usize::try_from(h.count)? == records.len(),
            "expected one ordered complete opaque object"
        );
    }
    let (outpoint, previous) = funding.prevout()?;
    ensure!(
        previous.value.to_sat() >= costs.minimum_funding_litoshis,
        "insufficient test funding"
    );
    let payout = &previous.script_pubkey;
    let mut pending = Vec::new();
    let mut commit = transaction(
        outpoint,
        TxOut {
            value: Amount::from_sat(LITECOIN_RETURN_LITOSHIS),
            script_pubkey: payout.clone(),
        },
    );
    commit.output.clear();
    for record in records {
        let prepared = PreparedReveal::new(record, payout, rate, LITECOIN_RETURN_LITOSHIS, rng)?;
        commit.output.push(prepared.output());
        pending.push(prepared);
    }
    commit.output.push(TxOut {
        value: Amount::from_sat(
            previous.value.to_sat()
                - costs.total_fee_litoshis
                - LITECOIN_RETURN_LITOSHIS * u64::try_from(costs.records)?,
        ),
        script_pubkey: payout.clone(),
    });
    let mut reveals = Vec::new();
    for (index, prepared) in pending.into_iter().enumerate() {
        reveals.push(hex::encode(serialize(&prepared.sign(
            &commit,
            u32::try_from(index)?,
            payout,
        )?)));
    }
    let plan = Plan {
        network: LITECOIN_NETWORK.into(),
        version: Urma::VERSION,
        input_bytes,
        start_height,
        fee_rate_litoshi_vb: rate,
        funding,
        commit: hex::encode(serialize(&commit)),
        reveals,
    };
    inspect(&plan, false)?;
    Ok(plan)
}

pub fn inspect(plan: &Plan, signed: bool) -> Result<Value, Error> {
    ensure!(
        plan.network == LITECOIN_NETWORK,
        "only Litecoin testnet plans are accepted"
    );
    let costs = quote(plan.input_bytes, plan.fee_rate_litoshi_vb)?;
    ensure!(plan.reveals.len() == costs.records, "invalid reveal count");
    let (_, previous) = plan.funding.prevout()?;
    let reveal = decode(&plan.reveals[0])?;
    let witness = &reveal
        .input
        .first()
        .context("missing reveal input")?
        .witness;
    let record = envelope::extract(witness)?.record;
    let header = container::inspect_header(&record)?;
    let common = journal::Plan {
        version: plan.version,
        network: LITECOIN_NETWORK.into(),
        object_id: hex::encode(header.id),
        start_height: plan.start_height,
        input_bytes: plan.input_bytes,
        fee_sats: costs.total_fee_litoshis,
        commit_fee_sats: costs.commit_fee_litoshis,
        reveal_fee_sats: costs.reveal_fee_litoshis,
        fee_rate_sat_vb: plan.fee_rate_litoshi_vb,
        funding: plan.funding.clone(),
        commit: plan.commit.clone(),
        reveals: plan.reveals.clone(),
        mining_address: String::new(),
    };
    let checked = journal::validate_transparent_plan(&common, &previous.script_pubkey, signed)?;
    Ok(
        json!({"network":LITECOIN_NETWORK, "status":if signed {"signed"} else {"draft"},
        "broadcast":false, "object_id":checked["object_id"], "start_height":plan.start_height,
        "commit_txid":checked["commit_txid"], "reveal_txids":checked["reveal_txids"],
        "commit_vsize":checked["commit_vsize"], "reveal_vsizes":checked["reveal_vsizes"],
        "quote":costs, "funding_litoshis":previous.value.to_sat(),
        "content_authenticated":false, "chain_status_checked":false}),
    )
}

pub fn is_signed(plan: &Plan) -> Result<bool, Error> {
    Ok(!decode(&plan.commit)?
        .input
        .first()
        .context("missing commit input")?
        .witness
        .is_empty())
}

pub trait Rpc {
    fn call(&self, method: &str, args: &[Value]) -> Result<Value, Error>;
}

pub struct Core {
    client: Client,
}

impl Core {
    pub fn connect(endpoint: &str, cookie: &Path, wallet: RpcScope<'_>) -> Result<Self, Error> {
        let mut url = url::Url::parse(endpoint).context("invalid local RPC URL")?;
        match (url.password(), url.query(), url.fragment()) {
            (None, None, None) => {}
            components => {
                bail!("URL credentials, query and fragment are forbidden: {components:?}")
            }
        }
        ensure!(
            url.scheme() == "http" && url.username().is_empty() && url.path() == "/",
            "RPC requires a plain local HTTP origin and cookie authentication"
        );
        let host = url.host_str().context("missing RPC host")?;
        let host = host.trim_matches(['[', ']']);
        ensure!(
            host.parse::<std::net::IpAddr>()?.is_loopback(),
            "RPC must use a loopback IP"
        );
        if let RpcScope::Wallet(wallet) = wallet {
            ensure!(!wallet.is_empty(), "wallet name is empty");
            url.path_segments_mut()
                .map_err(|unit| Error::Invalid(format!("invalid wallet URL: {unit:?}")))?
                .push("wallet")
                .push(wallet);
        }
        let client = Client::new(url.as_str(), Auth::CookieFile(cookie.to_owned()))
            .context("cannot configure local cookie-authenticated RPC")?;
        let core = Self { client };
        check_node(&core, false)?;
        Ok(core)
    }
}

impl Rpc for Core {
    fn call(&self, method: &str, args: &[Value]) -> Result<Value, Error> {
        ensure!(
            [
                "getblockchaininfo",
                "getblockhash",
                "getblock",
                "getrawtransaction",
                "gettxout",
                "getaddressinfo",
                "getnewaddress",
                "signrawtransactionwithwallet",
                "testmempoolaccept",
                "sendrawtransaction"
            ]
            .contains(&method),
            "unsupported Litecoin RPC method"
        );
        match self.client.call::<Value>(method, args) {
            Ok(v) => Ok(v),
            Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(e)))
                if method == "getrawtransaction" && e.code == -5 =>
            {
                tracing::warn!(code = e.code, "transaction absent from node");
                Ok(Value::Null)
            }
            Err(error) => Err(error.into()),
        }
    }
}

pub fn check_node(rpc: &dyn Rpc, require_ready: bool) -> Result<Value, Error> {
    let info = rpc.call("getblockchaininfo", &[])?;
    ensure!(info["chain"] == "test", "RPC must be Litecoin testnet");
    ensure!(
        rpc.call("getblockhash", &[json!(0)])?
            == urma_chain::observation::Chain::LitecoinTestnet
                .genesis()?
                .0
                .to_string(),
        "Litecoin testnet genesis mismatch"
    );
    if require_ready {
        ensure!(
            info["initialblockdownload"] == false
                && info["blocks"].as_u64().context("missing block height")? > 0,
            "node is not synchronized"
        );
        ensure!(
            info["softforks"]["segwit"]["active"] == true
                && info["softforks"]["taproot"]["active"] == true,
            "SegWit and Taproot must be active"
        );
    }
    Ok(info)
}

pub fn receiving_address(rpc: &dyn Rpc) -> Result<String, Error> {
    check_node(rpc, false)?;
    let value = rpc.call(
        "getnewaddress",
        &[json!("urma-litecoin-testnet"), json!("bech32")],
    )?;
    let address = value.as_str().context("missing testnet address")?;
    ensure!(
        address.starts_with("tltc1q"),
        "wallet returned a non-testnet/non-P2WPKH address"
    );
    let info = rpc.call("getaddressinfo", &[json!(address)])?;
    let script = ScriptBuf::from_bytes(hex::decode(
        info["scriptPubKey"]
            .as_str()
            .context("missing address script")?,
    )?);
    ensure!(
        info["ismine"] == true && script.is_p2wpkh(),
        "address must belong to a transparent test wallet"
    );
    Ok(address.to_owned())
}

pub fn sign(rpc: &dyn Rpc, draft: &Plan) -> Result<Plan, Error> {
    inspect(draft, false)?;
    check_node(rpc, false)?;
    let (outpoint, previous) = draft.funding.prevout()?;
    let result = rpc.call("signrawtransactionwithwallet", &[json!(draft.commit), json!([{
        "txid":outpoint.txid, "vout":outpoint.vout, "scriptPubKey":hex::encode(previous.script_pubkey.as_bytes()),
        "amount":coin_value(previous.value.to_sat())?
    }]), json!("ALL")])?;
    ensure!(
        result["complete"] == true,
        "wallet did not fully sign commit"
    );
    let mut signed = draft.clone();
    signed.commit = result["hex"]
        .as_str()
        .context("missing signed commit")?
        .to_owned();
    ensure!(
        decode(&signed.commit)?.compute_txid() == decode(&draft.commit)?.compute_txid(),
        "signer changed commit body"
    );
    inspect(&signed, true)?;
    Ok(signed)
}

fn coin_value(litoshis: u64) -> Result<Value, Error> {
    serde_json::from_str(&format!(
        "{}.{:08}",
        litoshis / 100_000_000,
        litoshis % 100_000_000
    ))
    .map_err(Into::into)
}

fn litoshis(value: &Value) -> Result<u64, Error> {
    let text = value.to_string();
    Amount::from_str_in(&text, bitcoin::Denomination::Bitcoin)
        .map(|amount| amount.to_sat())
        .context("invalid whole-litoshi RPC amount")
}

pub struct LitecoinRecords<'a> {
    pub rpc: &'a dyn Rpc,
    pub start_height: u64,
}

impl RecordSource for LitecoinRecords<'_> {
    fn read_records(
        &self,
        emit: &mut dyn FnMut(&[u8]) -> Result<(), Error>,
    ) -> Result<Observation, Error> {
        self.read_range(
            ScanEnd::Tip,
            &mut |height| {
                tracing::trace!(height, "scan progress");
                Ok(())
            },
            emit,
        )
    }
}

impl LitecoinRecords<'_> {
    pub fn read_range(
        &self,
        end: ScanEnd,
        progress: &mut dyn FnMut(u64) -> Result<(), Error>,
        emit: &mut dyn FnMut(&[u8]) -> Result<(), Error>,
    ) -> Result<Observation, Error> {
        let info = check_node(self.rpc, true)?;
        let current_tip = info["blocks"].as_u64().context("missing tip height")?;
        let tip = match end {
            ScanEnd::Tip => current_tip,
            ScanEnd::Height(height) => height,
        };
        ensure!(tip <= current_tip, "end height beyond tip");
        ensure!(
            self.start_height <= tip && tip - self.start_height < LITECOIN_MAX_SCAN_BLOCKS,
            "scan interval exceeds limits"
        );
        let tip_hash = self.rpc.call("getblockhash", &[json!(tip)])?;
        let tip_hash = parse_hash(&tip_hash)?;
        let mut previous = if self.start_height == 0 {
            bitcoin::BlockHash::all_zeros()
        } else {
            parse_hash(
                &self
                    .rpc
                    .call("getblockhash", &[json!(self.start_height - 1)])?,
            )?
        };
        let mut scanned_bytes = 0usize;
        for height in self.start_height..=tip {
            progress(height)?;
            let hash = parse_hash(&self.rpc.call("getblockhash", &[json!(height)])?)?;
            let block = self
                .rpc
                .call("getblock", &[json!(hash.to_string()), json!(2)])?;
            let bytes = serde_json::to_vec(&block)?.len();
            ensure!(bytes <= 32 * 1024 * 1024, "block JSON exceeds limit");
            scanned_bytes = scanned_bytes
                .checked_add(bytes)
                .context("scan byte overflow")?;
            ensure!(
                scanned_bytes <= LITECOIN_MAX_SCAN_BYTES,
                "scan byte limit exceeded"
            );
            ensure!(
                parse_hash(&block["hash"])? == hash
                    && block["height"] == height
                    && block["confirmations"]
                        .as_i64()
                        .context("missing block confirmations")?
                        > 0,
                "invalid selected block response"
            );
            if height > 0 {
                ensure!(
                    parse_hash(&block["previousblockhash"])? == previous,
                    "chain changed during scan"
                );
            }
            emit_block_records(&block, emit)?;
            previous = hash;
        }
        ensure!(
            previous == tip_hash
                && parse_hash(&self.rpc.call("getblockhash", &[json!(tip)])?)? == tip_hash,
            "chain changed during scan"
        );
        Ok(Observation {
            backend: Family::Litecoin,
            locator: Locator::BlockRange {
                network: LITECOIN_NETWORK.into(),
                start_height: self.start_height,
                tip_height: tip,
                tip_hash: tip_hash.to_string(),
            },
            evidence: Evidence::LitecoinCoreTrusted,
            scanned_bytes,
        })
    }
}

fn parse_hash(value: &Value) -> Result<bitcoin::BlockHash, Error> {
    bitcoin::BlockHash::from_str(value.as_str().context("missing block hash")?)
        .context("invalid block hash")
}

fn transaction_status(rpc: &dyn Rpc, raw: &str) -> Result<Value, Error> {
    let expected = decode(raw)?;
    let txid = expected.compute_txid();
    let value = rpc.call("getrawtransaction", &[json!(txid.to_string()), json!(true)])?;
    if value.is_null() {
        return Ok(json!({"txid":txid, "state":"unknown", "confirmations":0}));
    }
    let observed = decode(
        value["hex"]
            .as_str()
            .context("missing observed transaction")?,
    )?;
    ensure!(
        observed == expected,
        "node returned different transaction/witness bytes"
    );
    let confirmations = confirmations(&value)?;
    ensure!(confirmations >= 0, "transaction conflicted or reorganized");
    if confirmations > 0 {
        let hash = parse_hash(&value["blockhash"])?;
        let block = rpc.call("getblock", &[json!(hash.to_string()), json!(1)])?;
        let height = block["height"]
            .as_u64()
            .context("missing confirmation height")?;
        ensure!(
            parse_hash(&rpc.call("getblockhash", &[json!(height)])?)? == hash
                && block["tx"]
                    .as_array()
                    .context("missing txids")?
                    .contains(&json!(txid.to_string())),
            "confirmation not in selected chain"
        );
    }
    Ok(
        json!({"txid":txid, "state":if confirmations > 0 {"confirmed"} else {"pending"}, "confirmations":confirmations}),
    )
}

pub fn status(rpc: &dyn Rpc, plan: &Plan) -> Result<Value, Error> {
    inspect(plan, true)?;
    check_node(rpc, true)?;
    let commit = transaction_status(rpc, &plan.commit)?;
    let reveals = plan
        .reveals
        .iter()
        .map(|raw| transaction_status(rpc, raw))
        .collect::<Result<Vec<_>, Error>>()?;
    Ok(
        json!({"network":LITECOIN_NETWORK, "commit":commit, "reveals":reveals,
        "evidence":"litecoin_core_trusted", "quote":quote(plan.input_bytes,plan.fee_rate_litoshi_vb)?}),
    )
}

pub fn broadcast(rpc: &dyn Rpc, plan: &Plan, budget: u64) -> Result<Value, Error> {
    inspect(plan, true)?;
    ensure!(
        quote(plan.input_bytes, plan.fee_rate_litoshi_vb)?.total_fee_litoshis <= budget
            && budget <= LITECOIN_MAX_FEE_LITOSHIS,
        "explicit fee budget missing or exceeded"
    );
    check_node(rpc, true)?;
    let state = transaction_status(rpc, &plan.commit)?;
    if state["state"] == "unknown" {
        let (outpoint, previous) = plan.funding.prevout()?;
        let coin = rpc.call(
            "gettxout",
            &[json!(outpoint.txid), json!(outpoint.vout), json!(true)],
        )?;
        ensure!(
            coin["confirmations"]
                .as_u64()
                .context("missing funding confirmations")?
                > 0
                && (coin["coinbase"] == false
                    || (coin["coinbase"] == true
                        && coin["confirmations"]
                            .as_u64()
                            .context("missing coinbase confirmations")?
                            >= 100))
                && coin["scriptPubKey"]["hex"] == hex::encode(previous.script_pubkey.as_bytes())
                && litoshis(&coin["value"])? == previous.value.to_sat(),
            "funding must be confirmed, unspent and match the plan"
        );
        submit(rpc, &plan.commit)?;
        return Ok(
            json!({"network":LITECOIN_NETWORK, "status":"commit_submitted", "commit_txid":state["txid"], "broadcast":true}),
        );
    }
    if state["state"] == "pending" {
        return Ok(
            json!({"network":LITECOIN_NETWORK,"status":"awaiting_commit_confirmation","broadcast":false}),
        );
    }
    let mut submitted = Vec::new();
    for raw in &plan.reveals {
        let observed = transaction_status(rpc, raw)?;
        if observed["state"] == "unknown" {
            submitted.push(submit(rpc, raw)?);
        }
    }
    Ok(
        json!({"network":LITECOIN_NETWORK,"status":"reveals_checked","submitted_txids":submitted,
        "note":"Submission is not confirmation or successful recovery; check status and recover independently."}),
    )
}

fn submit(rpc: &dyn Rpc, raw: &str) -> Result<String, Error> {
    let expected = decode(raw)?.compute_txid().to_string();
    let acceptance = rpc.call("testmempoolaccept", &[json!([raw])])?;
    ensure!(
        acceptance[0]["allowed"] == true && acceptance[0]["txid"] == expected,
        "node policy rejected transaction; nothing submitted"
    );
    let returned = rpc.call("sendrawtransaction", &[json!(raw)])?;
    ensure!(
        returned == expected,
        "ambiguous submission result; inspect status before retrying"
    );
    Ok(expected)
}
fn emit_block_records(
    block: &Value,
    emit: &mut dyn FnMut(&[u8]) -> Result<(), Error>,
) -> Result<(), Error> {
    let transactions = block["tx"]
        .as_array()
        .context("missing block transactions")?;
    for tx in transactions {
        for input in tx["vin"].as_array().context("missing transparent inputs")? {
            let Some(stack) = input.get("txinwitness") else {
                continue;
            };
            let stack = stack.as_array().context("invalid witness array")?;
            if stack.len() != 3 {
                continue;
            }
            let mut parts = Vec::with_capacity(3);
            for (i, part) in stack.iter().enumerate() {
                let encoded = part.as_str().context("invalid witness hex")?;
                let limit = [128, (Urma::PRIVATE_RECORD_BYTES + 1024) * 2, 66][i];
                if encoded.len() > limit {
                    break;
                }
                parts.push(hex::decode(encoded)?);
            }
            if parts.len() == 3 {
                let witness = Witness::from_slice(&parts);
                if envelope::is_candidate(&witness) {
                    match extract_core_envelope(tx, &witness) {
                        Ok(parsed) => emit(&parsed.record)?,
                        Err(error) => {
                            tracing::warn!(%error, "invalid URMA envelope rejected")
                        }
                    }
                }
            }
        }
    }
    Ok(())
}

fn extract_core_envelope(tx: &Value, witness: &Witness) -> Result<envelope::ParsedEnvelope, Error> {
    let inputs = tx["vin"].as_array().context("missing transparent inputs")?;
    let outputs = tx["vout"]
        .as_array()
        .context("missing transparent outputs")?;
    let version = i32::try_from(
        tx["version"]
            .as_i64()
            .context("invalid reveal transaction shape")?,
    )?;
    envelope::validate_reveal_shape(version, inputs.len(), outputs.len())?;
    let script_sig = ScriptBuf::from_bytes(hex::decode(
        inputs[0]["scriptSig"]["hex"]
            .as_str()
            .context("missing scriptSig")?,
    )?);
    let script = ScriptBuf::from_bytes(hex::decode(
        outputs[0]["scriptPubKey"]["hex"]
            .as_str()
            .context("missing return script")?,
    )?);
    envelope::validate_reveal_scripts(&script_sig, &script)?;
    Ok(envelope::extract(witness)?)
}