Skip to content

Commit

Permalink
Update web.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoquick committed Sep 12, 2023
1 parent e58a976 commit 18a5786
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
27 changes: 7 additions & 20 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ pub mod bitcoin {
use super::*;

#[wasm_bindgen]
pub fn hash_password(password: String) -> String {
pub fn hash_password(password: String) {
set_panic_hook();

crate::bitcoin::hash_password(&SecretString(password))
.0
.to_owned()
}

#[wasm_bindgen]
Expand All @@ -134,14 +132,11 @@ pub mod bitcoin {
}

#[wasm_bindgen]
pub fn decrypt_wallet(hash: String, encrypted_descriptors: String) -> Promise {
pub fn decrypt_wallet(encrypted_descriptors: String) -> Promise {
set_panic_hook();

future_to_promise(async move {
match crate::bitcoin::decrypt_wallet(
&SecretString(hash),
&SecretString(encrypted_descriptors),
) {
match crate::bitcoin::decrypt_wallet(&SecretString(encrypted_descriptors)) {
Ok(result) => Ok(JsValue::from_string(
serde_json::to_string(&result).unwrap(),
)),
Expand All @@ -151,16 +146,11 @@ pub mod bitcoin {
}

#[wasm_bindgen]
pub fn upgrade_wallet(
hash: String,
encrypted_descriptors: String,
seed_password: String,
) -> Promise {
pub fn upgrade_wallet(encrypted_descriptors: String, seed_password: String) -> Promise {
set_panic_hook();

future_to_promise(async move {
match crate::bitcoin::upgrade_wallet(
&SecretString(hash),
&SecretString(encrypted_descriptors),
&SecretString(seed_password),
)
Expand All @@ -175,13 +165,11 @@ pub mod bitcoin {
}

#[wasm_bindgen]
pub fn new_wallet(hash: String, seed_password: String) -> Promise {
pub fn new_wallet(seed_password: String) -> Promise {
set_panic_hook();

future_to_promise(async move {
match crate::bitcoin::new_wallet(&SecretString(hash), &SecretString(seed_password))
.await
{
match crate::bitcoin::new_wallet(&SecretString(seed_password)).await {
Ok(result) => Ok(JsValue::from_string(
serde_json::to_string(&result).unwrap(),
)),
Expand All @@ -191,13 +179,12 @@ pub mod bitcoin {
}

#[wasm_bindgen]
pub fn encrypt_wallet(mnemonic: String, hash: String, seed_password: String) -> Promise {
pub fn encrypt_wallet(mnemonic: String, seed_password: String) -> Promise {
set_panic_hook();

future_to_promise(async move {
match crate::bitcoin::encrypt_wallet(
&SecretString(mnemonic),
&SecretString(hash),
&SecretString(seed_password),
)
.await
Expand Down
5 changes: 2 additions & 3 deletions tests/rgb/web/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@ const SEED_PASSWORD: &str = "";
async fn allow_issue_and_list_contracts() {
set_panic_hook();
let mnemonic = env!("TEST_WALLET_SEED", "TEST_WALLET_SEED variable not set");
let hash = hash_password(ENCRYPTION_PASSWORD.to_owned());
hash_password(ENCRYPTION_PASSWORD.to_owned());

info!("Import Seed");
let mnemonic_data_str = resolve(encrypt_wallet(
mnemonic.to_owned(),
hash.clone(),
SEED_PASSWORD.to_owned(),
))
.await;
let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

info!("Get Vault");
let issuer_keys: JsValue = resolve(decrypt_wallet(hash, mnemonic_data.0.clone())).await;
let issuer_keys: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;

info!("Get Keys");
let issuer_keys: DecryptedWalletData = json_parse(&issuer_keys);
Expand Down
21 changes: 8 additions & 13 deletions tests/rgb/web/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ const SEED_PASSWORD: &str = "";
async fn import_fungible_from_genesis() {
set_panic_hook();
let mnemonic = env!("TEST_WALLET_SEED", "TEST_WALLET_SEED variable not set");
let hash = hash_password(ENCRYPTION_PASSWORD.to_owned());
hash_password(ENCRYPTION_PASSWORD.to_owned());

info!("Import wallet");
let mnemonic_data_str = resolve(encrypt_wallet(
mnemonic.to_owned(),
hash.clone(),
SEED_PASSWORD.to_owned(),
))
.await;
let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

info!("Get vault properties");
let vault_str: JsValue = resolve(decrypt_wallet(hash, mnemonic_data.0.clone())).await;
let vault_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;
let wallet_data: DecryptedWalletData = json_parse(&vault_str);

info!("Import Genesis (Fungible)");
Expand All @@ -69,19 +68,18 @@ async fn import_fungible_from_genesis() {
async fn import_uda_from_genesis() {
set_panic_hook();
let mnemonic = env!("TEST_WALLET_SEED", "TEST_WALLET_SEED variable not set");
let hash = hash_password(ENCRYPTION_PASSWORD.to_owned());
hash_password(ENCRYPTION_PASSWORD.to_owned());

info!("Import wallet");
let mnemonic_data_str = resolve(encrypt_wallet(
mnemonic.to_owned(),
hash.clone(),
SEED_PASSWORD.to_owned(),
))
.await;
let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

info!("Get vault properties");
let vault_str: JsValue = resolve(decrypt_wallet(hash, mnemonic_data.0.clone())).await;
let vault_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;
let wallet_data: DecryptedWalletData = json_parse(&vault_str);

info!("Import Genesis (UDA)");
Expand All @@ -102,19 +100,18 @@ async fn import_uda_from_genesis() {
async fn import_two_contracts() {
set_panic_hook();
let mnemonic = env!("TEST_WALLET_SEED", "TEST_WALLET_SEED variable not set");
let hash = hash_password(ENCRYPTION_PASSWORD.to_owned());
hash_password(ENCRYPTION_PASSWORD.to_owned());

info!("Import wallet");
let mnemonic_data_str = resolve(encrypt_wallet(
mnemonic.to_owned(),
hash.clone(),
SEED_PASSWORD.to_owned(),
))
.await;
let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

info!("Get vault properties");
let vault_str: JsValue = resolve(decrypt_wallet(hash, mnemonic_data.0.clone())).await;
let vault_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;
let wallet_data: DecryptedWalletData = json_parse(&vault_str);
let sk = &wallet_data.private.nostr_prv;

Expand Down Expand Up @@ -144,21 +141,19 @@ async fn asset_transfer() {
set_panic_hook();

let mnemonic = env!("TEST_WALLET_SEED", "TEST_WALLET_SEED variable not set");
let hash = hash_password(ENCRYPTION_PASSWORD.to_owned());
hash_password(ENCRYPTION_PASSWORD.to_owned());

// Import wallet
let mnemonic_data_str = resolve(encrypt_wallet(
mnemonic.to_owned(),
hash.clone(),
SEED_PASSWORD.to_owned(),
))
.await;

let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

// Get vault properties
let wallet_data_str: JsValue =
resolve(decrypt_wallet(hash, mnemonic_data.0.clone().clone())).await;
let wallet_data_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone().clone())).await;
let wallet_data: DecryptedWalletData = json_parse(&wallet_data_str);

info!("Get Wallets");
Expand Down
24 changes: 9 additions & 15 deletions tests/web_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ async fn create_wallet() {
set_panic_hook();

info!("Mnemonic string is 24 words long");
let hash = hash_password(ENCRYPTION_PASSWORD.to_owned());
let mnemonic: JsValue = resolve(new_wallet(hash.clone(), SEED_PASSWORD.to_owned())).await;
hash_password(ENCRYPTION_PASSWORD.to_owned());
let mnemonic: JsValue = resolve(new_wallet(SEED_PASSWORD.to_owned())).await;

assert!(!mnemonic.is_undefined());
assert!(mnemonic.is_string());

let mnemonic_data: SecretString = json_parse(&mnemonic);

let encrypted_wallet_str: JsValue =
resolve(decrypt_wallet(hash, mnemonic_data.0.clone())).await;
let encrypted_wallet_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;
let encrypted_wallet_data: DecryptedWalletData = json_parse(&encrypted_wallet_str);

assert_eq!(encrypted_wallet_data.mnemonic.split(' ').count(), 24);
Expand All @@ -54,19 +53,17 @@ async fn import_and_open_wallet() {
set_panic_hook();

info!("Import wallet");
let hash = hash_password(ENCRYPTION_PASSWORD.to_owned());
hash_password(ENCRYPTION_PASSWORD.to_owned());
let mnemonic_data_str = resolve(encrypt_wallet(
MNEMONIC.to_owned(),
hash.clone(),
SEED_PASSWORD.to_owned(),
))
.await;

let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

info!("Get encrypted wallet properties");
let encrypted_wallet_str: JsValue =
resolve(decrypt_wallet(hash, mnemonic_data.0.clone())).await;
let encrypted_wallet_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;
let encrypted_wallet_data: DecryptedWalletData = json_parse(&encrypted_wallet_str);

assert_eq!(
Expand Down Expand Up @@ -105,33 +102,30 @@ async fn import_test_wallet() {
let mnemonic = env!("TEST_WALLET_SEED", "TEST_WALLET_SEED variable not set");

info!("Import wallet");
let hash0 = hash_password(ENCRYPTION_PASSWORD.to_owned());
hash_password(ENCRYPTION_PASSWORD.to_owned());
let mnemonic_data_str = resolve(encrypt_wallet(
mnemonic.to_owned(),
hash0.clone(),
SEED_PASSWORD.to_owned(),
))
.await;
let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

info!("Get vault properties");
let vault_str: JsValue = resolve(decrypt_wallet(hash0.clone(), mnemonic_data.0.clone())).await;
let vault_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;
let _encrypted_wallet_data: DecryptedWalletData = json_parse(&vault_str);

info!("Import wallet once more");
let hash1 = hash_password(ENCRYPTION_PASSWORD.to_owned());
assert_eq!(&hash0, &hash1, "hashes match");
hash_password(ENCRYPTION_PASSWORD.to_owned());

let mnemonic_data_str = resolve(encrypt_wallet(
mnemonic.to_owned(),
hash1.clone(),
SEED_PASSWORD.to_owned(),
))
.await;
let mnemonic_data: SecretString = json_parse(&mnemonic_data_str);

info!("Get vault properties");
let vault_str: JsValue = resolve(decrypt_wallet(hash1, mnemonic_data.0.clone())).await;
let vault_str: JsValue = resolve(decrypt_wallet(mnemonic_data.0.clone())).await;
let encrypted_wallet_data: DecryptedWalletData = json_parse(&vault_str);

info!("Get wallet data");
Expand Down

0 comments on commit 18a5786

Please sign in to comment.