use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
fn on_start() {
#[cfg(feature = "debug")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen(getter_with_clone)]
pub struct KeypairResult {
pub secret_key: Vec<u8>,
pub public_key: Vec<u8>,
}
#[wasm_bindgen(getter_with_clone)]
pub struct SchnorrSignatureResult {
pub public_nonce: Vec<u8>,
pub signature: Vec<u8>,
}
#[wasm_bindgen(getter_with_clone)]
pub struct StealthOutputsResult {
pub statement_json: String,
pub aggregated_output_mask: Vec<u8>,
}
#[wasm_bindgen(getter_with_clone)]
pub struct DecryptedOutputResult {
pub mask: Vec<u8>,
pub value: u64,
pub memo_json: Option<String>,
}
#[wasm_bindgen(getter_with_clone)]
pub struct OotleSecretKey {
pub owner_key: Vec<u8>,
pub view_key: Vec<u8>,
}
#[wasm_bindgen(getter_with_clone)]
pub struct OotlePublicKey {
pub owner_key: Vec<u8>,
pub view_key: Vec<u8>,
}
#[wasm_bindgen(getter_with_clone)]
pub struct ParsedOotleAddress {
pub owner_key: Vec<u8>,
pub view_key: Vec<u8>,
pub network: u8,
pub memo: Option<Vec<u8>>,
}
#[wasm_bindgen(js_name = "sealTransaction")]
pub fn seal_transaction(tx_json: &str, seal_signer_secret_key: &[u8]) -> Result<String, JsError> {
ootle_wasm_core::transaction::seal_transaction_json(tx_json, seal_signer_secret_key)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "addTransactionSigner")]
pub fn add_transaction_signer(
tx_json: &str,
signer_secret_key: &[u8],
seal_signer_public_key: &[u8],
) -> Result<String, JsError> {
ootle_wasm_core::transaction::add_transaction_signer_json(tx_json, signer_secret_key, seal_signer_public_key)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "borEncodeTransaction")]
pub fn bor_encode_transaction(transaction_json: &str) -> Result<String, JsError> {
ootle_wasm_core::bor::bor_encode_transaction_json(transaction_json).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "schnorrSign")]
pub fn schnorr_sign(secret_key: &[u8], message: &[u8]) -> Result<SchnorrSignatureResult, JsError> {
let result = ootle_wasm_core::sign::schnorr_sign(secret_key, message).map_err(|e| JsError::new(&e.to_string()))?;
Ok(SchnorrSignatureResult {
public_nonce: result.public_nonce,
signature: result.signature,
})
}
#[wasm_bindgen(js_name = "publicKeyFromSecretKey")]
pub fn public_key_from_secret_key(secret_key: &[u8]) -> Result<Vec<u8>, JsError> {
ootle_wasm_core::sign::public_key_from_secret_key(secret_key).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "generateKeypair")]
pub fn generate_keypair() -> KeypairResult {
let result = ootle_wasm_core::sign::generate_keypair();
KeypairResult {
secret_key: result.secret_key,
public_key: result.public_key,
}
}
#[wasm_bindgen(js_name = "hashUnsignedTransaction")]
pub fn hash_unsigned_transaction(unsigned_tx_json: &str, seal_signer_public_key: &[u8]) -> Result<Vec<u8>, JsError> {
ootle_wasm_core::hash::hash_unsigned_transaction_json(unsigned_tx_json, seal_signer_public_key)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "generateOotleSecretKey")]
pub fn generate_ootle_secret_key() -> OotleSecretKey {
let result = ootle_wasm_core::address::generate_ootle_secret_key();
OotleSecretKey {
owner_key: result.owner_key,
view_key: result.view_key,
}
}
#[wasm_bindgen(js_name = "ootlePublicKeyFromSecretKey")]
pub fn ootle_public_key_from_secret_key(owner_key: &[u8], view_key: &[u8]) -> Result<OotlePublicKey, JsError> {
let secret = ootle_wasm_core::address::OotleSecretKeyResult {
owner_key: owner_key.to_vec(),
view_key: view_key.to_vec(),
};
let result = ootle_wasm_core::address::ootle_public_key_from_secret_key(&secret)
.map_err(|e| JsError::new(&e.to_string()))?;
Ok(OotlePublicKey {
owner_key: result.owner_key,
view_key: result.view_key,
})
}
#[wasm_bindgen(js_name = "generateOotleAddress")]
pub fn generate_ootle_address(
owner_public_key: &[u8],
view_public_key: &[u8],
network: u8,
memo: Option<Vec<u8>>,
) -> Result<String, JsError> {
ootle_wasm_core::address::generate_ootle_address(owner_public_key, view_public_key, network, memo.as_deref())
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "parseOotleAddress")]
pub fn parse_ootle_address(address: &str) -> Result<ParsedOotleAddress, JsError> {
let result = ootle_wasm_core::address::parse_ootle_address(address).map_err(|e| JsError::new(&e.to_string()))?;
Ok(ParsedOotleAddress {
owner_key: result.owner_key,
view_key: result.view_key,
network: result.network,
memo: result.memo,
})
}
#[wasm_bindgen(js_name = "generateStealthOutputsStatement")]
pub fn generate_stealth_outputs_statement(
witnesses_json: &str,
revealed_output_amount_microtari: u64,
) -> Result<StealthOutputsResult, JsError> {
let result = ootle_wasm_core::stealth::outputs::generate_stealth_outputs_statement(
witnesses_json,
revealed_output_amount_microtari,
)
.map_err(|e| JsError::new(&e.to_string()))?;
Ok(StealthOutputsResult {
statement_json: result.statement_json,
aggregated_output_mask: result.aggregated_output_mask,
})
}
#[wasm_bindgen(js_name = "createStealthOutputWitness")]
#[allow(clippy::too_many_arguments)]
pub fn create_stealth_output_witness(
network: u8,
destination_account_public_key: &[u8],
destination_view_public_key: &[u8],
amount: u64,
resource_address: &str,
resource_view_key: Option<Vec<u8>>,
memo_json: Option<String>,
pay_to_json: Option<String>,
minimum_value_promise: u64,
) -> Result<String, JsError> {
ootle_wasm_core::stealth::outputs::create_stealth_output_witness(
ootle_wasm_core::stealth::outputs::CreateStealthOutputWitnessParams {
network,
destination_account_public_key,
destination_view_public_key,
amount,
resource_address,
resource_view_key: resource_view_key.as_deref(),
memo_json: memo_json.as_deref(),
pay_to_json: pay_to_json.as_deref(),
minimum_value_promise,
},
)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "buildStealthInputsStatement")]
pub fn build_stealth_inputs_statement(
input_commitments: &[u8],
revealed_amount_microtari: u64,
) -> Result<String, JsError> {
ootle_wasm_core::stealth::inputs::build_stealth_inputs_statement(input_commitments, revealed_amount_microtari)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "aggregateInputMasks")]
pub fn aggregate_input_masks(masks_concat: &[u8]) -> Result<Vec<u8>, JsError> {
ootle_wasm_core::stealth::inputs::aggregate_input_masks(masks_concat).map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "generateExtendedBulletProof")]
pub fn generate_extended_bullet_proof(witnesses_json: &str) -> Result<Vec<u8>, JsError> {
ootle_wasm_core::stealth::outputs::generate_extended_bullet_proof(witnesses_json)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "generateStealthBalanceProofSignature")]
pub fn generate_stealth_balance_proof_signature(
aggregated_input_mask: &[u8],
aggregated_output_mask: &[u8],
inputs_statement_json: &str,
outputs_statement_json: &str,
) -> Result<SchnorrSignatureResult, JsError> {
let result = ootle_wasm_core::stealth::balance_proof::generate_stealth_balance_proof_signature(
aggregated_input_mask,
aggregated_output_mask,
inputs_statement_json,
outputs_statement_json,
)
.map_err(|e| JsError::new(&e.to_string()))?;
Ok(SchnorrSignatureResult {
public_nonce: result.public_nonce,
signature: result.signature,
})
}
#[wasm_bindgen(js_name = "validateBalanceProofSignature")]
pub fn validate_balance_proof_signature(
public_nonce: &[u8],
signature: &[u8],
inputs_statement_json: &str,
outputs_statement_json: &str,
) -> Result<bool, JsError> {
ootle_wasm_core::stealth::balance_proof::validate_balance_proof_signature(
public_nonce,
signature,
inputs_statement_json,
outputs_statement_json,
)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "validateStealthTransfer")]
pub fn validate_stealth_transfer(transfer_json: &str, view_key: Option<Vec<u8>>) -> Result<(), JsError> {
ootle_wasm_core::stealth::validate::validate_stealth_transfer(transfer_json, view_key.as_deref())
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "unblindOutput")]
pub fn unblind_output(
output_commitment: &[u8],
encrypted_data: &[u8],
encryption_key: &[u8],
skip_memo: bool,
) -> Result<DecryptedOutputResult, JsError> {
let result = ootle_wasm_core::stealth::encrypted_data::unblind_output(
output_commitment,
encrypted_data,
encryption_key,
skip_memo,
)
.map_err(|e| JsError::new(&e.to_string()))?;
Ok(DecryptedOutputResult {
mask: result.mask,
value: result.value,
memo_json: result.memo_json,
})
}
#[wasm_bindgen(js_name = "stealthDhSecret")]
pub fn stealth_dh_secret(network: u8, private_key: &[u8], public_nonce: &[u8]) -> Result<Vec<u8>, JsError> {
ootle_wasm_core::stealth::kdfs::stealth_dh_secret(network, private_key, public_nonce)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "encryptedDataDhKdfAead")]
pub fn encrypted_data_dh_kdf_aead(private_key: &[u8], public_key: &[u8]) -> Result<Vec<u8>, JsError> {
ootle_wasm_core::stealth::kdfs::encrypted_data_dh_kdf(private_key, public_key)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "generateElgamalViewableBalanceProof")]
pub fn generate_elgamal_viewable_balance_proof(
mask: &[u8],
amount: u64,
commitment: &[u8],
view_public_key: &[u8],
) -> Result<String, JsError> {
ootle_wasm_core::stealth::viewable_balance::generate_elgamal_viewable_balance_proof(
mask,
amount,
commitment,
view_public_key,
)
.map_err(|e| JsError::new(&e.to_string()))
}
#[wasm_bindgen(js_name = "decryptElgamalViewableBalance")]
pub fn decrypt_elgamal_viewable_balance(
proof_json: &str,
commitment: &[u8],
view_public_key: &[u8],
view_secret_key: &[u8],
min_value: u64,
max_value: u64,
) -> Result<Option<u64>, JsError> {
ootle_wasm_core::stealth::viewable_balance::decrypt_elgamal_viewable_balance(
proof_json,
commitment,
view_public_key,
view_secret_key,
min_value,
max_value,
)
.map_err(|e| JsError::new(&e.to_string()))
}