taproot-assets-rpc 0.0.2

Taproot Assets gRPC client
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
use crate::taprpc;
use taproot_assets_types as types;

use bitcoin::hashes::{Hash, sha256::Hash as Sha256Hash};
use bitcoin::{BlockHash, OutPoint, Witness};
use std::convert::{TryFrom, TryInto};
use std::str::FromStr;

#[derive(Debug)]
pub enum ConversionError {
    InvalidEnumValue(String),
    MissingField(String), // Kept for future use, not used in current impl
    InvalidStringFormat(String),
    InvalidHashBytes(String),
    InvalidWitnessData(String), // Kept for future use
    RecursiveError(Box<ConversionError>),
    Other(String), // Generic fallback
}

pub type Result<T> = std::result::Result<T, ConversionError>;

impl std::fmt::Display for ConversionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConversionError::InvalidEnumValue(s) => write!(f, "Invalid enum value: {}", s),
            ConversionError::MissingField(s) => write!(f, "Missing field: {}", s),
            ConversionError::InvalidStringFormat(s) => write!(f, "Invalid string format: {}", s),
            ConversionError::InvalidHashBytes(s) => write!(f, "Invalid hash bytes: {}", s),
            ConversionError::InvalidWitnessData(s) => write!(f, "Invalid witness data: {}", s),
            ConversionError::RecursiveError(e) => write!(f, "Recursive conversion error: {}", e),
            ConversionError::Other(s) => write!(f, "Conversion error: {}", s),
        }
    }
}

impl std::error::Error for ConversionError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ConversionError::RecursiveError(e) => Some(e.as_ref()),
            _ => None,
        }
    }
}

impl From<bitcoin::hashes::FromSliceError> for ConversionError {
    fn from(e: bitcoin::hashes::FromSliceError) -> Self {
        ConversionError::InvalidHashBytes(format!("Failed to convert from slice: {}", e))
    }
}

impl From<bitcoin::consensus::encode::Error> for ConversionError {
    fn from(e: bitcoin::consensus::encode::Error) -> Self {
        ConversionError::Other(format!("Bitcoin consensus decoding error: {}", e))
    }
}

// Helper for hash conversion from Vec<u8> to Sha256Hash
fn vec_to_sha256hash(bytes: Vec<u8>, field_name: &str) -> Result<Sha256Hash> {
    Sha256Hash::from_slice(&bytes).map_err(|err| {
        ConversionError::InvalidHashBytes(format!(
            "Invalid {} hash bytes (expected 32 for {}): {}, (got {} bytes)",
            field_name,
            field_name,
            err,
            bytes.len()
        ))
    })
}

// Helper to parse OutPoint from string "txid:vout"
fn string_to_outpoint(s: String, field_name: &str) -> Result<OutPoint> {
    OutPoint::from_str(&s).map_err(|e| {
        ConversionError::InvalidStringFormat(format!(
            "Failed to parse {} from '{}': {}",
            field_name, s, e
        ))
    })
}

// Helper to parse BlockHash from string
fn string_to_blockhash(s: String, field_name: &str) -> Result<BlockHash> {
    BlockHash::from_str(&s).map_err(|e| {
        ConversionError::InvalidStringFormat(format!(
            "Failed to parse {} from '{}': {}",
            field_name, s, e
        ))
    })
}

// Helper for Option<F> -> Result<Option<R>, E> conversion
fn try_option<F, R>(opt_f: Option<F>) -> Result<Option<R>>
where
    F: TryInto<R, Error = ConversionError>,
{
    opt_f
        .map(TryInto::try_into)
        .transpose()
        .map_err(|err| ConversionError::RecursiveError(Box::new(err)))
}

// Helper for Vec<F> -> Result<Vec<R>, E> conversion
fn try_vec<F, R>(vec_f: Vec<F>) -> Result<Vec<R>>
where
    F: TryInto<R, Error = ConversionError>,
{
    vec_f
        .into_iter()
        .map(TryInto::try_into)
        .collect::<std::result::Result<Vec<R>, ConversionError>>()
        .map_err(|err| ConversionError::RecursiveError(Box::new(err)))
}

// --- Struct Conversions ---

impl TryFrom<taprpc::DecimalDisplay> for types::asset::DecimalDisplay {
    type Error = ConversionError;

    fn try_from(value: taprpc::DecimalDisplay) -> Result<Self> {
        Ok(types::asset::DecimalDisplay {
            decimal_display: value.decimal_display,
        })
    }
}

impl TryFrom<taprpc::GenesisInfo> for types::asset::GenesisInfo {
    type Error = ConversionError;

    fn try_from(value: taprpc::GenesisInfo) -> Result<Self> {
        Ok(types::asset::GenesisInfo {
            genesis_point: string_to_outpoint(value.genesis_point, "GenesisInfo.genesis_point")?,
            name: value.name,
            meta_hash: vec_to_sha256hash(value.meta_hash, "GenesisInfo.meta_hash")?,
            asset_id: vec_to_sha256hash(value.asset_id, "GenesisInfo.asset_id")?,
            asset_type: types::asset::AssetType::try_from(value.asset_type).map_err(|e| {
                ConversionError::InvalidEnumValue(format!(
                    "GenesisInfo.asset_type (value: {}): {}",
                    value.asset_type, e
                ))
            })?,
            output_index: value.output_index,
        })
    }
}

impl TryFrom<taprpc::AssetGroup> for types::asset::AssetGroup {
    type Error = ConversionError;

    fn try_from(value: taprpc::AssetGroup) -> Result<Self> {
        let tapscript_root = if value.tapscript_root.is_empty() {
            None
        } else {
            Some(vec_to_sha256hash(
                value.tapscript_root,
                "AssetGroup.tapscript_root",
            )?)
        };

        Ok(types::asset::AssetGroup {
            raw_group_key: value.raw_group_key,
            tweaked_group_key: value.tweaked_group_key,
            asset_witness: value.asset_witness,
            tapscript_root: tapscript_root,
        })
    }
}

impl TryFrom<taprpc::AnchorInfo> for types::asset::AnchorInfo {
    type Error = ConversionError;

    fn try_from(value: taprpc::AnchorInfo) -> Result<Self> {
        let tx = bitcoin::consensus::encode::deserialize(&value.anchor_tx)?;

        Ok(types::asset::AnchorInfo {
            anchor_tx: tx,
            anchor_block_hash: string_to_blockhash(
                value.anchor_block_hash,
                "AnchorInfo.anchor_block_hash",
            )?,
            anchor_outpoint: string_to_outpoint(
                value.anchor_outpoint,
                "AnchorInfo.anchor_outpoint",
            )?,
            internal_key: value.internal_key,
            merkle_root: vec_to_sha256hash(value.merkle_root, "AnchorInfo.merkle_root")?,
            tapscript_sibling: value.tapscript_sibling,
            block_height: value.block_height,
            block_timestamp: value.block_timestamp,
        })
    }
}

impl TryFrom<taprpc::PrevInputAsset> for types::asset::PrevInputAsset {
    type Error = ConversionError;

    fn try_from(value: taprpc::PrevInputAsset) -> Result<Self> {
        Ok(types::asset::PrevInputAsset {
            anchor_point: string_to_outpoint(value.anchor_point, "PrevInputAsset.anchor_point")?,
            asset_id: vec_to_sha256hash(value.asset_id, "PrevInputAsset.asset_id")?,
            script_key: value.script_key,
            amount: value.amount,
        })
    }
}

impl TryFrom<taprpc::Asset> for types::asset::Asset {
    type Error = ConversionError;

    fn try_from(value: taprpc::Asset) -> Result<Self> {
        Ok(types::asset::Asset {
            version: types::asset::AssetVersion::try_from(value.version).map_err(|e| {
                ConversionError::InvalidEnumValue(format!(
                    "Asset.version (value: {}): {}",
                    value.version, e
                ))
            })?,
            asset_genesis: try_option(value.asset_genesis)?,
            amount: value.amount,
            lock_time: value.lock_time,
            relative_lock_time: value.relative_lock_time,
            script_version: value.script_version,
            script_key: value.script_key,
            script_key_is_local: value.script_key_is_local,
            asset_group: try_option(value.asset_group)?,
            chain_anchor: try_option(value.chain_anchor)?,
            prev_witnesses: try_vec(value.prev_witnesses)?,
            split_commitment_root: None,
            is_spent: value.is_spent,
            lease_owner: value.lease_owner,
            lease_expiry: value.lease_expiry,
            is_burn: value.is_burn,
            script_key_declared_known: value.script_key_declared_known,
            script_key_has_script_path: value.script_key_has_script_path,
            decimal_display: try_option(value.decimal_display)?,
            script_key_type: types::asset::ScriptKeyType::try_from(value.script_key_type).map_err(
                |e| {
                    ConversionError::InvalidEnumValue(format!(
                        "Asset.script_key_type (value: {}): {}",
                        value.script_key_type, e
                    ))
                },
            )?,
        })
    }
}

impl TryFrom<taprpc::SplitCommitment> for types::asset::SplitCommitment {
    type Error = ConversionError;

    fn try_from(value: taprpc::SplitCommitment) -> Result<Self> {
        let rpc_asset = value.root_asset.ok_or_else(|| {
            ConversionError::MissingField("SplitCommitment.root_asset".to_string())
        })?;
        let domain_asset: types::asset::Asset = rpc_asset
            .try_into()
            .map_err(|e: ConversionError| ConversionError::RecursiveError(Box::new(e)))?;
        let proof = types::mssmt::MssmtProof { nodes: Vec::new() };
        Ok(types::asset::SplitCommitment {
            proof,
            root_asset: Box::new(domain_asset),
        })
    }
}

impl TryFrom<taprpc::PrevWitness> for types::asset::PrevWitness {
    type Error = ConversionError;

    fn try_from(value: taprpc::PrevWitness) -> Result<Self> {
        let prev_id = value
            .prev_id
            .map(|prev| {
                let out_point =
                    string_to_outpoint(prev.anchor_point, "PrevInputAsset.anchor_point")?;
                let asset_id = vec_to_sha256hash(prev.asset_id, "PrevInputAsset.asset_id")?;
                if prev.script_key.len() != 33 {
                    return Err(ConversionError::InvalidHashBytes(format!(
                        "Invalid PrevInputAsset.script_key length: {}",
                        prev.script_key.len()
                    )));
                }
                let mut key_bytes = [0u8; 33];
                key_bytes.copy_from_slice(&prev.script_key);
                Ok(types::asset::PrevId {
                    out_point,
                    asset_id,
                    script_key: types::asset::SerializedKey { bytes: key_bytes },
                })
            })
            .transpose()?;

        Ok(types::asset::PrevWitness {
            prev_id,
            tx_witness: Witness::from(value.tx_witness),
            split_commitment: try_option(value.split_commitment)?,
        })
    }
}

impl TryFrom<taprpc::ListAssetResponse> for crate::taprpc::types::ListAssetsResponse {
    type Error = ConversionError;

    fn try_from(value: taprpc::ListAssetResponse) -> Result<Self> {
        let assets = value
            .assets
            .into_iter()
            .map(types::asset::Asset::try_from)
            .collect::<Result<Vec<types::asset::Asset>>>()
            .map_err(|e| ConversionError::RecursiveError(Box::new(e)))?;

        Ok(crate::taprpc::types::ListAssetsResponse {
            assets,
            unconfirmed_transfers: value.unconfirmed_transfers,
            unconfirmed_mints: value.unconfirmed_mints,
        })
    }
}

impl TryFrom<taprpc::ProofFile> for crate::taprpc::types::ExportProofResponse {
    type Error = ConversionError;

    fn try_from(value: taprpc::ProofFile) -> Result<Self> {
        let genesis_point = if value.genesis_point.is_empty() {
            None
        } else {
            Some(string_to_outpoint(
                value.genesis_point,
                "ProofFile.genesis_point",
            )?)
        };

        Ok(crate::taprpc::types::ExportProofResponse {
            raw_proof_file: value.raw_proof_file,
            genesis_point: genesis_point,
        })
    }
}

impl TryFrom<taprpc::VerifyProofResponse> for crate::taprpc::types::VerifyProofResponse {
    type Error = ConversionError;

    fn try_from(value: taprpc::VerifyProofResponse) -> Result<Self> {
        Ok(crate::taprpc::types::VerifyProofResponse {
            valid: value.valid,
            decoded_proof: try_option(value.decoded_proof)?,
        })
    }
}

// --- Proof Conversion ---

impl TryFrom<taprpc::AssetMeta> for types::asset::AssetMeta {
    type Error = ConversionError;

    fn try_from(value: taprpc::AssetMeta) -> Result<Self> {
        Ok(types::asset::AssetMeta {
            data: value.data,
            meta_type: types::asset::AssetMetaType::try_from(value.r#type).map_err(|e| {
                ConversionError::InvalidEnumValue(format!(
                    "AssetMeta.type (value: {}): {}",
                    value.r#type, e
                ))
            })?,
        })
    }
}

// --- Fixed GenesisReveal conversion ---
impl TryFrom<taprpc::GenesisReveal> for types::asset::GenesisReveal {
    type Error = ConversionError;

    fn try_from(value: taprpc::GenesisReveal) -> Result<Self> {
        // The taprpc::GenesisReveal only contains genesis_base_reveal, but
        // types::asset::GenesisReveal requires additional fields. Since these
        // aren't available in the RPC structure, we use reasonable defaults.
        Ok(types::asset::GenesisReveal {
            genesis_base: try_option(value.genesis_base_reveal)?,
            asset_type: types::asset::AssetType::Normal, // Default to Normal type
            amount: 0,         // Default amount since not available in RPC
            meta_reveal: None, // No meta reveal available in RPC
        })
    }
}

impl TryFrom<taprpc::GroupKeyReveal> for types::asset::GroupKeyReveal {
    type Error = ConversionError;

    fn try_from(value: taprpc::GroupKeyReveal) -> Result<Self> {
        if value.raw_group_key.len() != 33 {
            return Err(ConversionError::InvalidHashBytes(format!(
                "Invalid GroupKeyReveal.raw_group_key length: {}",
                value.raw_group_key.len()
            )));
        }
        let mut key_bytes = [0u8; 33];
        key_bytes.copy_from_slice(&value.raw_group_key);
        let tapscript_root = if value.tapscript_root.is_empty() {
            None
        } else {
            Some(vec_to_sha256hash(
                value.tapscript_root,
                "GroupKeyReveal.tapscript_root",
            )?)
        };

        Ok(types::asset::GroupKeyReveal {
            raw_group_key: types::asset::SerializedKey { bytes: key_bytes },
            tapscript_root,
            version: None,
            custom_subtree_root: None,
        })
    }
}

impl TryFrom<taprpc::DecodedProof> for crate::taprpc::types::DecodedProof {
    type Error = ConversionError;

    fn try_from(value: taprpc::DecodedProof) -> Result<Self> {
        let challenge_witness = if value.challenge_witness.is_empty() {
            None
        } else {
            Some(Witness::from(value.challenge_witness))
        };

        let inclusion_proof = types::proof::TaprootProof::from_bytes(&value.inclusion_proof)
            .map_err(|e| {
                ConversionError::Other(format!("Failed to decode inclusion proof: {}", e))
            })?;

        let exclusion_proofs = value
            .exclusion_proofs
            .into_iter()
            .map(|proof_bytes| {
                types::proof::TaprootProof::from_bytes(&proof_bytes).map_err(|e| {
                    ConversionError::Other(format!("Failed to decode exclusion proof: {}", e))
                })
            })
            .collect::<Result<Vec<types::proof::TaprootProof>>>()?;

        let split_root_proof = if value.split_root_proof.is_empty() {
            None
        } else {
            Some(
                types::proof::TaprootProof::from_bytes(&value.split_root_proof).map_err(|e| {
                    ConversionError::Other(format!("Failed to decode split root proof: {}", e))
                })?,
            )
        };

        let tx_merkle_proof = types::proof::TxMerkleProof::from_bytes(&value.tx_merkle_proof)
            .map_err(|e| {
                ConversionError::Other(format!("Failed to decode tx merkle proof: {}", e))
            })?;

        Ok(crate::taprpc::types::DecodedProof {
            proof_at_depth: value.proof_at_depth,
            number_of_proofs: value.number_of_proofs,
            asset: value
                .asset
                .ok_or_else(|| ConversionError::MissingField("DecodedProof.asset".to_string()))?
                .try_into()?,
            meta_reveal: try_option(value.meta_reveal)?,
            tx_merkle_proof,
            inclusion_proof,
            exclusion_proofs,
            split_root_proof,
            num_additional_inputs: value.num_additional_inputs,
            challenge_witness,
            is_burn: value.is_burn,
            genesis_reveal: try_option(value.genesis_reveal)?,
            group_key_reveal: try_option(value.group_key_reveal)?,
        })
    }
}

// --- Commenting out TxMerkleProof conversion due to field mismatch ---
/*
impl TryFrom<taprpc::TxMerkleProof> for types::proof::TxMerkleProof {
    type Error = ConversionError;

    fn try_from(value: taprpc::TxMerkleProof) -> Result<Self> {
        // The `taprpc::TxMerkleProof` does not contain the `bits` field that is
        // part of `types::proof::TxMerkleProof`. The `bits` are used to
        // determine the side of the sibling hash in the merkle tree.
        // Without this information, we cannot fully construct the proof.
        // The `block_header` and `tx_index` are also not used here.
        // This suggests a potential mismatch between the rpc and type definitions.
        let nodes = value
            .merkle_nodes
            .into_iter()
            .map(|node_bytes| {
                bitcoin::TxMerkleNode::from_slice(&node_bytes).map_err(|e| {
                    ConversionError::InvalidHashBytes(format!(
                        "Invalid TxMerkleNode hash bytes: {}",
                        e
                    ))
                })
            })
            .collect::<Result<Vec<bitcoin::TxMerkleNode>>>()?;

        Ok(types::proof::TxMerkleProof {
            nodes,
            bits: vec![], // Bits are not available in taprpc::TxMerkleProof
        })
    }
}
*/