Skip to content

Commit

Permalink
create utxo common function address_to_scripthash
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Dec 5, 2023
1 parent 681f039 commit 2c3d03c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
11 changes: 6 additions & 5 deletions mm2src/coins/utxo/utxo_balance_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ use mm2_event_stream::{behaviour::{EventBehaviour, EventInitStatus},
use std::collections::{BTreeMap, HashSet};

use super::utxo_standard::UtxoStandardCoin;
use crate::{utxo::{output_script, rpc_clients::electrum_script_hash, utxo_common::address_balance,
utxo_tx_history_v2::UtxoTxHistoryOps, ScripthashNotification, UtxoCoinFields},
use crate::{utxo::{output_script,
rpc_clients::electrum_script_hash,
utxo_common::{address_balance, address_to_scripthash},
utxo_tx_history_v2::UtxoTxHistoryOps,
ScripthashNotification, UtxoCoinFields},
MarketCoinOps, MmCoin};

macro_rules! try_or_continue {
Expand Down Expand Up @@ -42,9 +45,7 @@ impl EventBehaviour for UtxoStandardCoin {
let mut scripthash_to_address_map: BTreeMap<String, Address> = BTreeMap::new();

for address in addresses {
let script = output_script(&address, keys::Type::P2PKH);
let script_hash = electrum_script_hash(&script);
let scripthash = hex::encode(script_hash);
let scripthash = address_to_scripthash(&address);

scripthash_to_address_map.insert(scripthash.clone(), address);

Expand Down
28 changes: 25 additions & 3 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5048,6 +5048,12 @@ where
refund_htlc_payment(coin, args, SwapPaymentType::TakerPaymentV2).await
}

pub fn address_to_scripthash(address: &Address) -> String {
let script = output_script(address, keys::Type::P2PKH);
let script_hash = electrum_script_hash(&script);
hex::encode(script_hash)
}

/// Prepares addresses for scripthash subscription if coin balance event
/// is enabled.
pub async fn prepare_addresses_for_balance_stream_if_enabled<T>(
Expand All @@ -5062,9 +5068,7 @@ where
match &ctx.event_stream_configuration {
Some(event_config) if event_config.get_event("COIN_BALANCE").is_some() => {
for address in addresses {
let script = output_script(&address, keys::Type::P2PKH);
let script_hash = electrum_script_hash(&script);
let scripthash = hex::encode(script_hash);
let scripthash = address_to_scripthash(&address);

if let Err(e) = coin
.as_ref()
Expand Down Expand Up @@ -5183,3 +5187,21 @@ fn test_generate_taker_fee_tx_outputs_with_burn() {

assert_eq!(outputs[1].value, burn_uamount);
}

#[test]
fn test_address_to_scripthash() {
let address = Address::from("RMGJ9tRST45RnwEKHPGgBLuY3moSYP7Mhk");
let actual = address_to_scripthash(&address);
let expected = "e850499408c6ebcf6b3340282747e540fb23748429fca5f2b36cdeef54ddf5b1".to_owned();
assert_eq!(expected, actual);

let address = Address::from("R9o9xTocqr6CeEDGDH6mEYpwLoMz6jNjMW");
let actual = address_to_scripthash(&address);
let expected = "a70a7a7041ef172ce4b5f8208aabed44c81e2af75493540f50af7bd9afa9955d".to_owned();
assert_eq!(expected, actual);

let address = Address::from("qcyBHeSct7Wr4mAw18iuQ1zW5mMFYmtmBE");
let actual = address_to_scripthash(&address);
let expected = "c5b5922c86830289231539d1681d8ce621aac8326c96d6ac55400b4d1485f769".to_owned();
assert_eq!(expected, actual);
}

0 comments on commit 2c3d03c

Please sign in to comment.