ootle-wasm-core 0.33.5

Pure Rust crypto and encoding logic for Tari Ootle WASM — BOR encoding, transaction hashing, Schnorr signing, and key management
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
//   Copyright 2026 The Tari Project
//   SPDX-License-Identifier: BSD-3-Clause

//! Building the output side of a stealth transfer: per-output commitments + encrypted data, optional
//! ElGamal view-key proofs, and the aggregated bulletproof range proof.

use std::str::FromStr;

use ootle_byte_type::ToByteType;
use ootle_network::Network;
use tari_crypto::{
    keys::{PublicKey, SecretKey},
    ristretto::{RistrettoPublicKey, RistrettoSecretKey},
    tari_utilities::ByteArray,
};
use tari_ootle_wallet_crypto::{
    OutputWitness,
    StealthCryptoApi,
    StealthOutputWitness,
    bullet_proof::generate_extended_bullet_proof as crypto_generate_extended_bullet_proof,
    memo::Memo,
    pay_to::PayTo,
    stealth::create_outputs_statement,
};
use tari_template_lib_types::{Amount, ResourceAddress, crypto::RangeProofBytes, stealth::SpendCondition};

use crate::{
    error::OotleWasmError,
    keys::public_key_from_bytes,
    stealth::types::{OutputWitnessJson, StealthOutputWitnessJson},
};

/// Result of [`generate_stealth_outputs_statement`]: the wire-ready statement plus the aggregated mask the
/// sender retains for the balance proof.
#[derive(Debug, Clone)]
pub struct StealthOutputsResult {
    /// Serialized `StealthOutputsStatement` (containing outputs, revealed amount, and aggregated range
    /// proof).
    pub statement_json: String,
    /// Sum of all witness masks as a 32-byte Ristretto scalar. Feed this into
    /// [`crate::stealth::balance_proof::generate_stealth_balance_proof_signature`] as the aggregated
    /// output mask.
    pub aggregated_output_mask: Vec<u8>,
}

/// Generate the output half of a stealth transfer.
///
/// `witnesses_json` is a JSON array of [`StealthOutputWitnessJson`]; `revealed_output_amount_microtari` is
/// the plaintext revealed amount (in microtari, fits a `u64`). Output witnesses that contain a
/// `resource_view_key` automatically receive an ElGamal viewable-balance proof.
pub fn generate_stealth_outputs_statement(
    witnesses_json: &str,
    revealed_output_amount_microtari: u64,
) -> Result<StealthOutputsResult, OotleWasmError> {
    use tari_ootle_wallet_crypto::StealthOutputWitness;

    let witnesses: Vec<StealthOutputWitnessJson> = serde_json::from_str(witnesses_json)?;
    let witnesses: Vec<StealthOutputWitness> = witnesses
        .into_iter()
        .map(StealthOutputWitness::try_from)
        .collect::<Result<Vec<_>, OotleWasmError>>()?;

    let aggregated_output_mask = witnesses
        .iter()
        .map(|w| &w.witness.mask)
        .fold(RistrettoSecretKey::default(), |acc, mask| acc + mask);

    let statement = create_outputs_statement(witnesses.iter(), Amount::from_u64(revealed_output_amount_microtari))
        .map_err(|e| OotleWasmError::Stealth(e.to_string()))?;

    Ok(StealthOutputsResult {
        statement_json: serde_json::to_string(&statement)?,
        aggregated_output_mask: aggregated_output_mask.as_bytes().to_vec(),
    })
}

/// Inputs to [`create_stealth_output_witness`].
///
/// All keys are raw 32-byte slices; `resource_address` is the bech-style `resource_<hex>` string. The
/// commitment mask and ephemeral nonce are generated internally, so this struct carries only the
/// caller-controlled parameters.
pub struct CreateStealthOutputWitnessParams<'a> {
    /// Network byte (0x00 = MainNet, 0x10 = LocalNet, 0x26 = Esmeralda, ...).
    pub network: u8,
    /// Recipient's long-term account public key (used to derive the one-time stealth spend key).
    pub destination_account_public_key: &'a [u8],
    /// Recipient's view-only public key (used for the AEAD key and the UTXO scanning tag).
    pub destination_view_public_key: &'a [u8],
    /// Output value in microtari.
    pub amount: u64,
    /// The resource this output belongs to, as a `resource_<hex>` string.
    pub resource_address: &'a str,
    /// View public key of the resource view-key holder, if the resource has a viewable balance.
    pub resource_view_key: Option<&'a [u8]>,
    /// Optional JSON-encoded [`Memo`] to embed in the encrypted payload.
    pub memo_json: Option<&'a str>,
    /// Optional JSON-encoded [`PayTo`]: `"StealthPublicKey"` (the default when `None`) or
    /// `{"AccessRule": <AccessRule>}`.
    pub pay_to_json: Option<&'a str>,
    /// Minimum value the range proof commits to (must be `<= amount`). Use `0` unless you have a reason
    /// to reveal a lower bound.
    pub minimum_value_promise: u64,
}

/// Build a single stealth output witness entirely client-side, mirroring the wallet daemon's
/// `StealthOutputsApi::create_output_witness`.
///
/// Generates a fresh random commitment mask and ephemeral nonce, AEAD-encrypts the value+mask to the
/// recipient, derives the spend condition and the UTXO scanning tag, and returns one
/// [`StealthOutputWitnessJson`] serialized to a JSON string. Collect one such witness per output (including
/// change) into a JSON array and feed it to [`generate_stealth_outputs_statement`].
///
/// The mask is random rather than HD-derived: recipients (including the sender, for change) always recover
/// `value` and `mask` by decrypting `encrypted_data`, so determinism is not required.
pub fn create_stealth_output_witness(params: CreateStealthOutputWitnessParams<'_>) -> Result<String, OotleWasmError> {
    let network = Network::try_from(params.network).map_err(|e| OotleWasmError::InvalidNetwork(e.to_string()))?;
    let account_key = public_key_from_bytes(params.destination_account_public_key)?;
    let view_key = public_key_from_bytes(params.destination_view_public_key)?;
    let resource_address = ResourceAddress::from_str(params.resource_address)
        .map_err(|e| OotleWasmError::InvalidAddress(e.to_string()))?;
    let resource_view_key = params.resource_view_key.map(public_key_from_bytes).transpose()?;
    let memo: Option<Memo> = params.memo_json.map(serde_json::from_str).transpose()?;
    let pay_to: PayTo = params
        .pay_to_json
        .map(serde_json::from_str)
        .transpose()?
        .unwrap_or_default();

    // Fail fast with a clear message rather than a cryptic range-proof error later, at statement time.
    if params.minimum_value_promise > params.amount {
        return Err(OotleWasmError::Stealth(format!(
            "minimum_value_promise ({}) must be <= amount ({})",
            params.minimum_value_promise, params.amount
        )));
    }

    let mask = RistrettoSecretKey::random(&mut rand::rng());
    let (nonce_secret, public_nonce) = RistrettoPublicKey::random_keypair(&mut rand::rng());

    let crypto = StealthCryptoApi::new();
    let encrypted_data = crypto
        .encrypt_value_and_mask(params.amount, &mask, &view_key, &nonce_secret, memo.as_ref())
        .map_err(|e| OotleWasmError::Stealth(e.to_string()))?;

    let spend_condition = match pay_to {
        PayTo::StealthPublicKey => {
            let owner_public_key = crypto.derive_stealth_owner_public_key(network, &account_key, &nonce_secret);
            SpendCondition::Signed(owner_public_key.to_byte_type())
        },
        PayTo::AccessRule(access_rule) => SpendCondition::AccessRule(access_rule),
    };

    let tag = crypto.derive_stealth_output_tag(network, &nonce_secret, &view_key, &resource_address);

    let witness = StealthOutputWitness {
        witness: OutputWitness {
            amount: params.amount,
            mask,
            sender_public_nonce: public_nonce,
            minimum_value_promise: params.minimum_value_promise,
            encrypted_data,
            resource_view_key,
        },
        spend_condition,
        tag,
    };

    Ok(serde_json::to_string(&StealthOutputWitnessJson::from(&witness))?)
}

/// Generate an extended bulletproof that aggregates range proofs for the supplied output witnesses.
///
/// Exposed independently of [`generate_stealth_outputs_statement`] for testing and audit. The Python
/// transfer flow uses the bundled variant.
///
/// `witnesses_json` is a JSON array of [`OutputWitnessJson`]. Returns the raw `RangeProofBytes` (may be
/// empty for an empty input array).
pub fn generate_extended_bullet_proof(witnesses_json: &str) -> Result<Vec<u8>, OotleWasmError> {
    let witnesses: Vec<OutputWitnessJson> = serde_json::from_str(witnesses_json)?;
    let witnesses = witnesses
        .into_iter()
        .map(OutputWitnessJson::try_into_witness)
        .collect::<Result<Vec<_>, OotleWasmError>>()?;

    let proof: RangeProofBytes =
        crypto_generate_extended_bullet_proof(witnesses.iter()).map_err(|e| OotleWasmError::Stealth(e.to_string()))?;
    Ok(proof.into_vec())
}

#[cfg(test)]
mod tests {
    use ootle_byte_type::ToByteType;
    use tari_crypto::{
        keys::{PublicKey, SecretKey},
        ristretto::{RistrettoPublicKey, RistrettoSecretKey},
    };
    use tari_engine_types::stealth::validate_stealth_outputs_statement;
    use tari_template_lib_types::{EncryptedData, stealth::StealthOutputsStatement};

    use super::*;

    fn make_witness_json(amount: u64) -> String {
        let mask = RistrettoSecretKey::random(&mut rand::rng());
        let nonce = RistrettoPublicKey::from_secret_key(&mask);
        let spend_pk: tari_template_lib_types::crypto::RistrettoPublicKeyBytes = nonce.to_byte_type();
        format!(
            r#"[{{"witness":{{"amount":{},"mask":"{}","sender_public_nonce":"{}","minimum_value_promise":0,"encrypted_data":"{}"}},"spend_condition":{{"Signed":"{}"}},"tag":0}}]"#,
            amount,
            hex::encode(mask.as_bytes()),
            hex::encode(nonce.as_bytes()),
            hex::encode(vec![0u8; EncryptedData::min_size()]),
            hex::encode(spend_pk.as_bytes()),
        )
    }

    #[test]
    fn generate_outputs_produces_valid_statement() {
        let witnesses = make_witness_json(1000);
        let result = generate_stealth_outputs_statement(&witnesses, 0).unwrap();
        let stmt: StealthOutputsStatement = serde_json::from_str(&result.statement_json).unwrap();
        validate_stealth_outputs_statement(&stmt, None).unwrap();
        assert_eq!(result.aggregated_output_mask.len(), 32);
    }

    #[test]
    fn generate_outputs_with_empty_array() {
        let result = generate_stealth_outputs_statement("[]", 100).unwrap();
        let stmt: StealthOutputsStatement = serde_json::from_str(&result.statement_json).unwrap();
        assert!(stmt.outputs.is_empty());
        assert!(stmt.agg_range_proof.is_empty());
        assert_eq!(result.aggregated_output_mask, vec![0u8; 32]);
    }

    #[test]
    fn generate_extended_bullet_proof_empty() {
        let proof = generate_extended_bullet_proof("[]").unwrap();
        assert!(proof.is_empty());
    }

    #[test]
    fn generate_extended_bullet_proof_non_empty() {
        // Flat OutputWitnessJson (no spend_condition / tag wrapper).
        let mask = RistrettoSecretKey::random(&mut rand::rng());
        let nonce = RistrettoPublicKey::from_secret_key(&mask);
        let witness_json = format!(
            r#"[{{"amount":50,"mask":"{}","sender_public_nonce":"{}","minimum_value_promise":0,"encrypted_data":"{}"}}]"#,
            hex::encode(mask.as_bytes()),
            hex::encode(nonce.as_bytes()),
            hex::encode(vec![0u8; EncryptedData::min_size()]),
        );
        let proof = generate_extended_bullet_proof(&witness_json).unwrap();
        assert!(!proof.is_empty());
    }

    fn tari_resource() -> String {
        tari_template_lib_types::constants::STEALTH_TARI_RESOURCE_ADDRESS.to_string()
    }

    fn random_public_key() -> RistrettoPublicKey {
        RistrettoPublicKey::random_keypair(&mut rand::rng()).1
    }

    #[test]
    fn created_witness_produces_valid_outputs_statement() {
        let account_pk = random_public_key();
        let view_pk = random_public_key();
        let resource = tari_resource();

        let json = create_stealth_output_witness(CreateStealthOutputWitnessParams {
            network: Network::LocalNet.as_byte(),
            destination_account_public_key: account_pk.as_bytes(),
            destination_view_public_key: view_pk.as_bytes(),
            amount: 1000,
            resource_address: &resource,
            resource_view_key: None,
            memo_json: None,
            pay_to_json: None,
            minimum_value_promise: 0,
        })
        .unwrap();

        let result = generate_stealth_outputs_statement(&format!("[{json}]"), 0).unwrap();
        let stmt: StealthOutputsStatement = serde_json::from_str(&result.statement_json).unwrap();
        validate_stealth_outputs_statement(&stmt, None).unwrap();
        assert_eq!(stmt.outputs.len(), 1);
        // The default pay_to produces a one-time stealth spend key.
        assert!(matches!(stmt.outputs[0].spend_condition, SpendCondition::Signed(_)));
    }

    #[test]
    fn created_witness_is_spendable_and_decryptable() {
        use tari_crypto::commitment::HomomorphicCommitmentFactory;
        use tari_engine_types::crypto::get_commitment_factory;

        let network = Network::LocalNet;
        let (account_sk, account_pk) = RistrettoPublicKey::random_keypair(&mut rand::rng());
        let (view_sk, view_pk) = RistrettoPublicKey::random_keypair(&mut rand::rng());
        let amount = 12_345u64;
        let resource = tari_resource();

        let json = create_stealth_output_witness(CreateStealthOutputWitnessParams {
            network: network.as_byte(),
            destination_account_public_key: account_pk.as_bytes(),
            destination_view_public_key: view_pk.as_bytes(),
            amount,
            resource_address: &resource,
            resource_view_key: None,
            memo_json: None,
            pay_to_json: None,
            minimum_value_promise: 0,
        })
        .unwrap();

        let witness: StealthOutputWitness = serde_json::from_str::<StealthOutputWitnessJson>(&json)
            .unwrap()
            .try_into()
            .unwrap();

        // The recipient recomputes the AEAD key from (view_secret, sender_public_nonce) and decrypts the
        // value + mask back out, confirming the output is addressed to them.
        let commitment = get_commitment_factory()
            .commit_value(&witness.witness.mask, amount)
            .to_byte_type();
        let encryption_key = crate::stealth::kdfs::encrypted_data_dh_kdf(
            view_sk.as_bytes(),
            witness.witness.sender_public_nonce.as_bytes(),
        )
        .unwrap();
        let decrypted = crate::stealth::encrypted_data::unblind_output(
            commitment.as_bytes(),
            witness.witness.encrypted_data.as_bytes(),
            &encryption_key,
            false,
        )
        .unwrap();
        assert_eq!(decrypted.value, amount);
        assert_eq!(decrypted.mask, witness.witness.mask.as_bytes().to_vec());

        // The recipient's one-time spend secret matches the spend condition's public key, so the output
        // is actually spendable.
        let stealth_secret_bytes = crate::stealth::kdfs::stealth_dh_secret(
            network.as_byte(),
            account_sk.as_bytes(),
            witness.witness.sender_public_nonce.as_bytes(),
        )
        .unwrap();
        let stealth_secret = RistrettoSecretKey::from_canonical_bytes(&stealth_secret_bytes).unwrap();
        let derived_pub = RistrettoPublicKey::from_secret_key(&stealth_secret);
        let SpendCondition::Signed(expected) = witness.spend_condition else {
            panic!("expected a Signed spend condition");
        };
        assert_eq!(derived_pub.to_byte_type(), expected);
    }

    #[test]
    fn created_witness_with_access_rule() {
        let account_pk = random_public_key();
        let view_pk = random_public_key();
        let resource = tari_resource();

        let json = create_stealth_output_witness(CreateStealthOutputWitnessParams {
            network: Network::LocalNet.as_byte(),
            destination_account_public_key: account_pk.as_bytes(),
            destination_view_public_key: view_pk.as_bytes(),
            amount: 500,
            resource_address: &resource,
            resource_view_key: None,
            memo_json: None,
            pay_to_json: Some(r#"{"AccessRule":"AllowAll"}"#),
            minimum_value_promise: 0,
        })
        .unwrap();

        let witness: StealthOutputWitnessJson = serde_json::from_str(&json).unwrap();
        assert!(matches!(witness.spend_condition, SpendCondition::AccessRule(_)));

        let result = generate_stealth_outputs_statement(&format!("[{json}]"), 0).unwrap();
        let stmt: StealthOutputsStatement = serde_json::from_str(&result.statement_json).unwrap();
        validate_stealth_outputs_statement(&stmt, None).unwrap();
    }

    #[test]
    fn created_witness_with_memo_round_trips() {
        use tari_crypto::commitment::HomomorphicCommitmentFactory;
        use tari_engine_types::crypto::get_commitment_factory;

        let (_, account_pk) = RistrettoPublicKey::random_keypair(&mut rand::rng());
        let (view_sk, view_pk) = RistrettoPublicKey::random_keypair(&mut rand::rng());
        let amount = 777u64;
        let resource = tari_resource();

        let json = create_stealth_output_witness(CreateStealthOutputWitnessParams {
            network: Network::LocalNet.as_byte(),
            destination_account_public_key: account_pk.as_bytes(),
            destination_view_public_key: view_pk.as_bytes(),
            amount,
            resource_address: &resource,
            resource_view_key: None,
            memo_json: Some(r#"{"Message":"gm"}"#),
            pay_to_json: None,
            minimum_value_promise: 0,
        })
        .unwrap();

        let witness: StealthOutputWitness = serde_json::from_str::<StealthOutputWitnessJson>(&json)
            .unwrap()
            .try_into()
            .unwrap();
        let commitment = get_commitment_factory()
            .commit_value(&witness.witness.mask, amount)
            .to_byte_type();
        let encryption_key = crate::stealth::kdfs::encrypted_data_dh_kdf(
            view_sk.as_bytes(),
            witness.witness.sender_public_nonce.as_bytes(),
        )
        .unwrap();
        let decrypted = crate::stealth::encrypted_data::unblind_output(
            commitment.as_bytes(),
            witness.witness.encrypted_data.as_bytes(),
            &encryption_key,
            false,
        )
        .unwrap();
        assert_eq!(decrypted.memo_json.as_deref(), Some(r#"{"Message":"gm"}"#));
    }

    #[test]
    fn create_witness_rejects_invalid_resource_address() {
        let account_pk = random_public_key();
        let view_pk = random_public_key();

        let err = create_stealth_output_witness(CreateStealthOutputWitnessParams {
            network: Network::LocalNet.as_byte(),
            destination_account_public_key: account_pk.as_bytes(),
            destination_view_public_key: view_pk.as_bytes(),
            amount: 1,
            resource_address: "not-a-resource",
            resource_view_key: None,
            memo_json: None,
            pay_to_json: None,
            minimum_value_promise: 0,
        })
        .unwrap_err();
        assert!(matches!(err, OotleWasmError::InvalidAddress(_)));
    }

    #[test]
    fn create_witness_rejects_minimum_value_promise_above_amount() {
        let account_pk = random_public_key();
        let view_pk = random_public_key();
        let resource = tari_resource();

        let err = create_stealth_output_witness(CreateStealthOutputWitnessParams {
            network: Network::LocalNet.as_byte(),
            destination_account_public_key: account_pk.as_bytes(),
            destination_view_public_key: view_pk.as_bytes(),
            amount: 100,
            resource_address: &resource,
            resource_view_key: None,
            memo_json: None,
            pay_to_json: None,
            minimum_value_promise: 101,
        })
        .unwrap_err();
        assert!(matches!(err, OotleWasmError::Stealth(_)));
    }
}