Skip to content

Commit

Permalink
Implement Intent/SignedIntent Hash formatted method
Browse files Browse the repository at this point in the history
  • Loading branch information
micbakos-rdx committed Apr 3, 2024
1 parent e167046 commit 433874a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AddressFormat
import com.radixdlt.sargon.IntentHash
import com.radixdlt.sargon.intentHashFormatted
import com.radixdlt.sargon.newIntentHashFromString

@Throws(SargonException::class)
fun IntentHash.Companion.init(string: String) =
newIntentHashFromString(string = string)
newIntentHashFromString(string = string)

fun IntentHash.formatted(format: AddressFormat = AddressFormat.DEFAULT) =
intentHashFormatted(address = this, format = format)
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AddressFormat
import com.radixdlt.sargon.SignedIntentHash
import com.radixdlt.sargon.newSignedIntentHashFromString
import com.radixdlt.sargon.signedIntentHashFormatted

@Throws(SargonException::class)
fun SignedIntentHash.Companion.init(string: String) =
newSignedIntentHashFromString(string = string)
newSignedIntentHashFromString(string = string)

fun SignedIntentHash.formatted(format: AddressFormat = AddressFormat.DEFAULT) =
signedIntentHashFormatted(address = this, format = format)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.radixdlt.sargon

import com.radixdlt.sargon.extensions.formatted
import com.radixdlt.sargon.extensions.init
import com.radixdlt.sargon.samples.Sample
import com.radixdlt.sargon.samples.sample
Expand Down Expand Up @@ -30,4 +31,26 @@ class IntentHashTest: SampleTestable<IntentHash> {
IntentHash.init(txId).bech32EncodedTxId
)
}

@Test
fun testFormatted() {
val txId = "txid_rdx1frcm6zzyfd08z0deu9x24sh64eccxeux4j2dv3dsqeuh9qsz4y6szm3ltd"
val formatted = "txid...zm3ltd"
assertEquals(
formatted,
IntentHash.init(txId).formatted()
)
assertEquals(
formatted,
IntentHash.init(txId).formatted(format = AddressFormat.DEFAULT)
)
assertEquals(
txId,
IntentHash.init(txId).formatted(format = AddressFormat.FULL)
)
assertEquals(
txId,
IntentHash.init(txId).formatted(format = AddressFormat.RAW)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.radixdlt.sargon

import com.radixdlt.sargon.extensions.formatted
import com.radixdlt.sargon.extensions.init
import com.radixdlt.sargon.samples.Sample
import com.radixdlt.sargon.samples.sample
Expand Down Expand Up @@ -30,4 +31,26 @@ class SignedIntentHashTest: SampleTestable<SignedIntentHash> {
SignedIntentHash.init(s).bech32EncodedTxId
)
}

@Test
fun testFormatted() {
val txId = "signedintent_rdx1frcm6zzyfd08z0deu9x24sh64eccxeux4j2dv3dsqeuh9qsz4y6sxsk6nl"
val formatted = "sign...xsk6nl"
Assertions.assertEquals(
formatted,
SignedIntentHash.init(txId).formatted()
)
Assertions.assertEquals(
formatted,
SignedIntentHash.init(txId).formatted(format = AddressFormat.DEFAULT)
)
Assertions.assertEquals(
txId,
SignedIntentHash.init(txId).formatted(format = AddressFormat.FULL)
)
Assertions.assertEquals(
txId,
SignedIntentHash.init(txId).formatted(format = AddressFormat.RAW)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ macro_rules! decl_tx_hash {
$struct_name: ident,
$scrypto_struct_name: ident,
$mod_test_name: ident,
$expected_sample_str: literal
$expected_sample_str: literal,
$expected_sample_str_formatted: literal
) => {

$(
Expand All @@ -38,6 +39,11 @@ macro_rules! decl_tx_hash {
$struct_name::from_str(&string)
}

#[uniffi::export]

Check warning on line 42 in src/wrapped_radix_engine_toolkit/low_level/transaction_hashes/transaction_hashes.rs

View check run for this annotation

Codecov / codecov/patch

src/wrapped_radix_engine_toolkit/low_level/transaction_hashes/transaction_hashes.rs#L42

Added line #L42 was not covered by tests
pub fn [< $struct_name:snake _formatted>](address: &$struct_name, format: AddressFormat) -> String {
address.formatted(format)
}

#[cfg(test)]
mod [< uniffi_ $struct_name:snake _tests>] {
use super::*;
Expand All @@ -49,6 +55,14 @@ macro_rules! decl_tx_hash {
fn from_str() {
assert_eq!(SUT::sample(), [< new_$struct_name:snake _from_string>]($expected_sample_str.to_owned()).unwrap());
}

#[test]
fn formatted() {
let sut = SUT::sample();
assert_eq!(sut.formatted(AddressFormat::Default), [< $struct_name:snake _formatted>](&sut, AddressFormat::Default));
assert_eq!(sut.formatted(AddressFormat::Raw), [< $struct_name:snake _formatted>](&sut, AddressFormat::Raw));
assert_eq!(sut.formatted(AddressFormat::Full), [< $struct_name:snake _formatted>](&sut, AddressFormat::Full));
}
}
}

Expand Down Expand Up @@ -81,6 +95,13 @@ macro_rules! decl_tx_hash {
validate_and_decode_hash::<$scrypto_struct_name>(s)
.map(|t| Self::from_scrypto(t.0, t.1))
}

pub fn formatted(&self, format: AddressFormat) -> String {
match format {
AddressFormat::Default => format_string(self.bech32_encoded_tx_id.to_string(), 4, 6),
AddressFormat::Full | AddressFormat::Raw => self.bech32_encoded_tx_id.to_string(),
}
}
}

impl FromStr for $struct_name {
Expand Down Expand Up @@ -123,6 +144,14 @@ macro_rules! decl_tx_hash {
fn from_str() {
assert_eq!(SUT::sample(), $expected_sample_str.parse::<SUT>().unwrap());
}

#[test]
fn formatted() {
let sut = $expected_sample_str.parse::<SUT>().unwrap();
assert_eq!($expected_sample_str_formatted, sut.formatted(AddressFormat::Default));
assert_eq!($expected_sample_str, sut.formatted(AddressFormat::Raw));
assert_eq!($expected_sample_str, sut.formatted(AddressFormat::Full));
}
}
};

Expand All @@ -131,7 +160,8 @@ macro_rules! decl_tx_hash {
#[doc = $expr: expr]
)*
$hash_type: ident,
$expected_sample_str: literal
$expected_sample_str: literal,
$expected_sample_str_formatted: literal,
) => {
paste! {
decl_tx_hash!(
Expand All @@ -141,7 +171,8 @@ macro_rules! decl_tx_hash {
[< $hash_type Hash >],
[< Scrypto $hash_type Hash >],
[< tests_ $hash_type:snake >],
$expected_sample_str
$expected_sample_str,
$expected_sample_str_formatted
);
}
};
Expand All @@ -152,11 +183,13 @@ decl_tx_hash!(
/// Representation is bech32 encoded string starting with `txid_` e.g.:
/// `"txid_rdx19rpveua6xuhvz0axu0mwpqk8fywr83atv8mkrugchvw6uuslgppqh9cnj4"`
Intent,
"txid_rdx1frcm6zzyfd08z0deu9x24sh64eccxeux4j2dv3dsqeuh9qsz4y6szm3ltd"
"txid_rdx1frcm6zzyfd08z0deu9x24sh64eccxeux4j2dv3dsqeuh9qsz4y6szm3ltd",
"txid...zm3ltd",
);

decl_tx_hash!(
/// A Signed Intent Hash is a bech32 encoded string starting with `"signedintent_"
SignedIntent,
"signedintent_rdx1frcm6zzyfd08z0deu9x24sh64eccxeux4j2dv3dsqeuh9qsz4y6sxsk6nl"
"signedintent_rdx1frcm6zzyfd08z0deu9x24sh64eccxeux4j2dv3dsqeuh9qsz4y6sxsk6nl",
"sign...xsk6nl",
);

0 comments on commit 433874a

Please sign in to comment.