Skip to content

Commit 1b40bf2

Browse files
committed
Own WithExtra types
1 parent 99cafa5 commit 1b40bf2

File tree

6 files changed

+81
-28
lines changed

6 files changed

+81
-28
lines changed

plutus-ledger-api/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Changelog](https://keepachangelog.com/en/1.1.0).
2020
- Addresses are formatted using bech32, if supplemented with network id
2121
- CurrencySymbols are formatted as hex if native tokens or as the `lovelace`
2222
string
23+
- `WithExtraInfo` types are now owned
2324

2425
## 3.1.0
2526

plutus-ledger-api/src/v1/address.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use serde_with::{DeserializeFromStr, SerializeDisplay};
1414
use crate as plutus_ledger_api;
1515
use crate::csl::csl_to_pla::{FromCSL, TryFromCSL, TryFromCSLError, TryToPLA};
1616
use crate::csl::pla_to_csl::{TryFromPLA, TryFromPLAError, TryToCSL};
17+
use crate::error::ConversionError;
1718
use crate::plutus_data::{
1819
parse_constr, parse_fixed_len_constr_fields, IsPlutusData, PlutusData, PlutusDataError,
1920
};
@@ -39,7 +40,7 @@ pub struct Address {
3940
}
4041

4142
impl Address {
42-
pub fn with_extra_info<'a>(&'a self, network_tag: u8) -> AddressWithExtraInfo<'a> {
43+
pub fn with_extra_info(self, network_tag: u8) -> AddressWithExtraInfo {
4344
AddressWithExtraInfo {
4445
address: self,
4546
network_tag,
@@ -89,13 +90,13 @@ impl TryFromCSL<csl::Address> for Address {
8990
#[cfg_attr(feature = "serde", derive(SerializeDisplay, DeserializeFromStr))]
9091
/// Address with network information. The `WithExtraInfo` variant has Display instance, serializing into
9192
/// a bech32 address format.
92-
pub struct AddressWithExtraInfo<'a> {
93-
pub address: &'a Address,
93+
pub struct AddressWithExtraInfo {
94+
pub address: Address,
9495
pub network_tag: u8,
9596
}
9697

97-
impl TryFromPLA<AddressWithExtraInfo<'_>> for csl::Address {
98-
fn try_from_pla(val: &AddressWithExtraInfo<'_>) -> Result<Self, TryFromPLAError> {
98+
impl TryFromPLA<AddressWithExtraInfo> for csl::Address {
99+
fn try_from_pla(val: &AddressWithExtraInfo) -> Result<Self, TryFromPLAError> {
99100
let payment = val.address.credential.try_to_csl()?;
100101

101102
Ok(match val.address.staking_credential {
@@ -113,20 +114,71 @@ impl TryFromPLA<AddressWithExtraInfo<'_>> for csl::Address {
113114
}
114115
}
115116

117+
impl TryFromCSL<csl::Address> for AddressWithExtraInfo {
118+
fn try_from_csl(value: &csl::Address) -> Result<Self, TryFromCSLError>
119+
where
120+
Self: Sized,
121+
{
122+
Ok(AddressWithExtraInfo {
123+
address: value.try_to_pla()?,
124+
network_tag: value.network_id().map_err(|err| {
125+
TryFromCSLError::ImpossibleConversion(format!(
126+
"Couldn't extract network tag from address: {err}"
127+
))
128+
})?,
129+
})
130+
}
131+
}
132+
116133
/// Serializing into a bech32 address format.
117-
impl std::fmt::Display for AddressWithExtraInfo<'_> {
134+
impl std::fmt::Display for AddressWithExtraInfo {
118135
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119136
let bech32_addr: Option<String> = self
120137
.try_to_csl()
121138
.ok()
122139
.and_then(|csl_addr: csl::Address| csl_addr.to_bech32(None).ok());
123140
match bech32_addr {
124141
Some(addr) => write!(f, "{}", addr),
125-
None => write!(f, "INVALID ADDRESS {:?}", self),
142+
None => Err(std::fmt::Error),
126143
}
127144
}
128145
}
129146

147+
impl FromStr for AddressWithExtraInfo {
148+
type Err = ConversionError;
149+
150+
fn from_str(s: &str) -> Result<Self, Self::Err> {
151+
let csl_addr = csl::Address::from_bech32(s).map_err(|err| {
152+
ConversionError::ParseError(anyhow!(
153+
"Error while parsing bech32 address '{}': {}",
154+
s,
155+
err
156+
))
157+
})?;
158+
159+
let address = Address::try_from_csl(&csl_addr).map_err(|err| {
160+
ConversionError::ParseError(anyhow!(
161+
"Error while parsing bech32 address '{}': {}",
162+
s,
163+
err
164+
))
165+
})?;
166+
167+
let network_tag = csl_addr.network_id().map_err(|err| {
168+
ConversionError::ParseError(anyhow!(
169+
"Error while parsing extracting network tag from address '{}': {}",
170+
s,
171+
err
172+
))
173+
})?;
174+
175+
Ok(AddressWithExtraInfo {
176+
address,
177+
network_tag,
178+
})
179+
}
180+
}
181+
130182
////////////////
131183
// Credential //
132184
////////////////

plutus-ledger-api/src/v1/redeemer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ use crate::v1::crypto::LedgerBytes;
2525
pub struct Redeemer(pub PlutusData);
2626

2727
#[derive(Clone, Debug)]
28-
pub struct RedeemerWithExtraInfo<'a> {
29-
pub redeemer: &'a Redeemer,
30-
pub tag: &'a csl::RedeemerTag,
28+
pub struct RedeemerWithExtraInfo {
29+
pub redeemer: Redeemer,
30+
pub tag: csl::RedeemerTag,
3131
pub index: u64,
3232
}
3333

34-
impl TryFromPLA<RedeemerWithExtraInfo<'_>> for csl::Redeemer {
35-
fn try_from_pla<'a>(val: &RedeemerWithExtraInfo<'_>) -> Result<csl::Redeemer, TryFromPLAError> {
36-
let Redeemer(plutus_data) = val.redeemer;
34+
impl TryFromPLA<RedeemerWithExtraInfo> for csl::Redeemer {
35+
fn try_from_pla<'a>(val: &RedeemerWithExtraInfo) -> Result<csl::Redeemer, TryFromPLAError> {
36+
let Redeemer(plutus_data) = &val.redeemer;
3737
Ok(csl::Redeemer::new(
38-
val.tag,
38+
&val.tag,
3939
&val.index.try_to_csl()?,
4040
&plutus_data.try_to_csl()?,
4141
&csl::ExUnits::new(&csl::BigNum::from(0u64), &csl::BigNum::from(0u64)),

plutus-ledger-api/src/v2/transaction.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ impl TryFromCSL<csl::TransactionOutputs> for Vec<TransactionOutput> {
8585
}
8686

8787
#[derive(Clone, Debug)]
88-
pub struct TransactionOutputWithExtraInfo<'a> {
89-
pub transaction_output: &'a TransactionOutput,
90-
pub scripts: &'a BTreeMap<ScriptHash, csl::PlutusScript>,
88+
pub struct TransactionOutputWithExtraInfo {
89+
pub transaction_output: TransactionOutput,
90+
pub scripts: BTreeMap<ScriptHash, csl::PlutusScript>,
9191
pub network_id: u8,
92-
pub data_cost: &'a csl::DataCost,
92+
pub data_cost: csl::DataCost,
9393
}
9494

95-
impl TryFromPLA<TransactionOutputWithExtraInfo<'_>> for csl::TransactionOutput {
96-
fn try_from_pla(val: &TransactionOutputWithExtraInfo<'_>) -> Result<Self, TryFromPLAError> {
95+
impl TryFromPLA<TransactionOutputWithExtraInfo> for csl::TransactionOutput {
96+
fn try_from_pla(val: &TransactionOutputWithExtraInfo) -> Result<Self, TryFromPLAError> {
9797
let mut output_builder = csl::TransactionOutputBuilder::new().with_address(
9898
&AddressWithExtraInfo {
99-
address: &val.transaction_output.address,
99+
address: val.transaction_output.address.clone(),
100100
network_tag: val.network_id,
101101
}
102102
.try_to_csl()?,
@@ -127,7 +127,7 @@ impl TryFromPLA<TransactionOutputWithExtraInfo<'_>> for csl::TransactionOutput {
127127

128128
let value_without_min_utxo = val.transaction_output.value.try_to_csl()?;
129129

130-
let mut calc = csl::MinOutputAdaCalculator::new_empty(val.data_cost)
130+
let mut calc = csl::MinOutputAdaCalculator::new_empty(&val.data_cost)
131131
.map_err(TryFromPLAError::CSLJsError)?;
132132
calc.set_amount(&value_without_min_utxo);
133133
match &val.transaction_output.datum {
@@ -204,13 +204,13 @@ pub struct TransactionInfo {
204204
}
205205

206206
#[derive(Clone, Debug)]
207-
pub struct WithdrawalsWithExtraInfo<'a> {
208-
pub withdrawals: &'a AssocMap<StakingCredential, BigInt>,
207+
pub struct WithdrawalsWithExtraInfo {
208+
pub withdrawals: AssocMap<StakingCredential, BigInt>,
209209
pub network_tag: u8,
210210
}
211211

212-
impl TryFromPLA<WithdrawalsWithExtraInfo<'_>> for csl::Withdrawals {
213-
fn try_from_pla(val: &WithdrawalsWithExtraInfo<'_>) -> Result<Self, TryFromPLAError> {
212+
impl TryFromPLA<WithdrawalsWithExtraInfo> for csl::Withdrawals {
213+
fn try_from_pla(val: &WithdrawalsWithExtraInfo) -> Result<Self, TryFromPLAError> {
214214
val.withdrawals
215215
.0
216216
.iter()

plutus-ledger-api/tests/csl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mod csl_pla_roundtrip_tests {
124124
#[test]
125125
fn test_address(val in arb_address()) {
126126
prop_assert_eq!(
127-
Address::try_from_csl(&val.with_extra_info(1).try_to_csl()?)?,
127+
Address::try_from_csl(&val.clone().with_extra_info(1).try_to_csl()?)?,
128128
val
129129
)
130130
}

plutus-ledger-api/tests/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ mod display_serialisation_tests {
140140

141141
#[test]
142142
fn address(val in arb_address()) {
143-
let roundtripped = Address::from_str(&val.with_extra_info(0).to_string()).unwrap();
143+
let roundtripped = Address::from_str(&val.clone().with_extra_info(0).to_string()).unwrap();
144144

145145
assert_eq!(val, roundtripped);
146146
}

0 commit comments

Comments
 (0)