Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plutus-ledger-api V2 #55

Merged
merged 41 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2b7d884
Implement extra golden tests for external use
szg251 Aug 28, 2024
fb8cc8e
Update golden tests
szg251 Sep 24, 2024
e9adfe8
Custom serde serialization for Value
szg251 Oct 3, 2024
fcbf520
Add Display implementations
szg251 Aug 8, 2024
bf30fbc
Reorganise modules
szg251 Oct 4, 2024
c60e631
Apply clippy suggestions
szg251 Oct 4, 2024
7864eb2
Update csl
szg251 Oct 4, 2024
9274576
Changelog and version bump
szg251 Oct 4, 2024
87e07ef
implement procedural derive macro for `IsPlutusData`
chfanghr Oct 14, 2024
21efc52
separate `PlutusData` and `IsPlutusData` from plutus-ledger-api
chfanghr Oct 17, 2024
1a7c261
lower version requirement of proc-macro2
chfanghr Oct 17, 2024
167395f
`plutus_data_derive_strategy` -> `is_plutus_data_derive_strategy`
chfanghr Oct 17, 2024
4d5a08b
fix `parse_fixed_len_plutus_data_list` not in scope
chfanghr Oct 17, 2024
001c558
attribute value can only be literal
chfanghr Oct 17, 2024
c7812dd
derive the shit out of it
chfanghr Oct 17, 2024
b4351b7
explicitly mark derive strategies
chfanghr Oct 18, 2024
3e64281
move plutus-data back to where it was
chfanghr Oct 21, 2024
84f92b6
remove plutus-data
chfanghr Oct 21, 2024
e84bbb0
fogot to commit .envrc
chfanghr Oct 21, 2024
32b00e6
update changelog
chfanghr Oct 21, 2024
2fd89e1
add v3 types
chfanghr Oct 18, 2024
9895366
implement generators for v3 types
chfanghr Oct 18, 2024
688be1c
add property tests for v3 types
chfanghr Oct 18, 2024
6864a9a
explicitly mark derive strategies
chfanghr Oct 18, 2024
e727987
remove true from dependencies
chfanghr Oct 21, 2024
2d51d1c
doc strings
chfanghr Oct 21, 2024
e96c707
more doc strings
chfanghr Oct 21, 2024
cf119ba
even more doc strings
chfanghr Oct 21, 2024
413e2e7
update changelog
chfanghr Oct 21, 2024
62a1ce5
Merge pull request #56 from mlabs-haskell/connor/is_plutus_data_derive
szg251 Nov 4, 2024
11e130d
Merge pull request #57 from mlabs-haskell/connor/v3-types
szg251 Nov 4, 2024
0f7507c
Reexport v2 types in v3 module
szg251 Nov 5, 2024
71cf096
Implement golden tests for Display implementations
szg251 Nov 5, 2024
ae25646
Update goldens
szg251 Nov 5, 2024
4899696
Update deps
szg251 Nov 5, 2024
d09a731
Update CHANGELOG
szg251 Nov 5, 2024
c35f7f3
Merge pull request #53 from mlabs-haskell/szg251/extra-goldens
szg251 Nov 5, 2024
1acaa8b
Add FromStr parsers
szg251 Nov 5, 2024
36c4133
Final touches before v2
szg251 Nov 7, 2024
c21d9ec
Migrate over csl roundtrip tests
szg251 Nov 7, 2024
1210948
Address PR comments
szg251 Nov 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions plutus-ledger-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ Changelog](https://keepachangelog.com/en/1.1.0).

### Added

- Added cardano-serialization-lib conversion traits (`ToCSL` and `FromCSL`)
- Added cardano-serialization-lib conversion traits (`ToCSL` and `FromCSL`) ([#55](https://github.com/mlabs-haskell/plutus-ledger-api-rust/pull/55))
- Added v3 plutus ledger types ([#57](https://github.com/mlabs-haskell/plutus-ledger-api-rust/pull/57))
- Added the ability to derive `IsPlutusData` instances ([#56](https://github.com/mlabs-haskell/plutus-ledger-api-rust/pull/56))
- Added a few utility functions for Values
- Added a few utility functions for Values ([#55](https://github.com/mlabs-haskell/plutus-ledger-api-rust/pull/55))
- Added Display and Debug implementations for
CurrencySymbol, TokenName, Value, AddressWithExtraInfo and some other types
CurrencySymbol, TokenName, Value, AddressWithExtraInfo and some other types ([#55](https://github.com/mlabs-haskell/plutus-ledger-api-rust/pull/55))
- Added FromStr implementations for CurrencySymbol, TokenName, Value, Address
and some other types
and some other types ([#55](https://github.com/mlabs-haskell/plutus-ledger-api-rust/pull/55))

### Changed

Expand Down
9 changes: 8 additions & 1 deletion plutus-ledger-api/src/aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ pub fn union_b_tree_maps_with<const N: usize, K: Clone + Ord, V: Clone, F: Fn(&V
})
}

pub fn guard_bytes(ctx: &str, bytes: Vec<u8>, expected: usize) -> Result<Vec<u8>, ConversionError> {
/// Verify that a given bytestring has the expected length
pub(crate) fn guard_bytes(
ctx: &str,
bytes: Vec<u8>,
expected: usize,
) -> Result<Vec<u8>, ConversionError> {
if bytes.len() == expected {
Ok(bytes)
} else {
Expand All @@ -75,6 +80,8 @@ pub fn guard_bytes(ctx: &str, bytes: Vec<u8>, expected: usize) -> Result<Vec<u8>
}
}

/// Nom parser for BigInt
/// Expects an arbitrary length integer, optionally signed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decimal numbers

pub(crate) fn big_int(i: &str) -> IResult<&str, BigInt, VerboseError<&str>> {
map_res(
recognize(tuple((opt(alt((char('-'), char('+')))), many1(digit1)))),
Expand Down
28 changes: 3 additions & 25 deletions plutus-ledger-api/src/v1/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use nom::{combinator::map_res, error::VerboseError, IResult};
use serde::{Deserialize, Serialize};

use crate as plutus_ledger_api;
use crate::error::ConversionError;
use crate::{
csl::{
csl_to_pla::FromCSL,
Expand Down Expand Up @@ -95,6 +94,9 @@ impl std::fmt::Display for LedgerBytes {
}
}

/// Nom parser for LedgerBytes
/// Expects a hexadecimal string of arbitrary length (0 length is allowed)
/// E.g.: 00112233445566778899aabbcc
pub(crate) fn ledger_bytes(input: &str) -> IResult<&str, LedgerBytes, VerboseError<&str>> {
map_res(nom::character::complete::hex_digit0, |hex_bytes: &str| {
HEXLOWER
Expand All @@ -103,30 +105,6 @@ pub(crate) fn ledger_bytes(input: &str) -> IResult<&str, LedgerBytes, VerboseErr
})(input)
}

pub(crate) fn hash28(input: &str) -> IResult<&str, LedgerBytes, VerboseError<&str>> {
map_res(ledger_bytes, |bytes: LedgerBytes| {
if bytes.0.len() == 28 {
Ok(bytes)
} else {
Err(ConversionError::invalid_bytestring_length(
"hash28", 28, "equal to", &bytes.0,
))
}
})(input)
}

pub(crate) fn hash32(input: &str) -> IResult<&str, LedgerBytes, VerboseError<&str>> {
map_res(ledger_bytes, |bytes: LedgerBytes| {
if bytes.0.len() == 32 {
Ok(bytes)
} else {
Err(ConversionError::invalid_bytestring_length(
"hash32", 32, "equal to", &bytes.0,
))
}
})(input)
}

#[cfg(feature = "lbf")]
impl Json for LedgerBytes {
fn to_json(&self) -> serde_json::Value {
Expand Down
19 changes: 0 additions & 19 deletions plutus-ledger-api/src/v1/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use cardano_serialization_lib as csl;

#[cfg(feature = "lbf")]
use lbr_prelude::json::Json;
use nom::combinator::map;
use nom::error::{context, VerboseError};
use nom::IResult;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand All @@ -18,8 +15,6 @@ use crate::error::ConversionError;
use crate::plutus_data::IsPlutusData;
use crate::v1::crypto::LedgerBytes;

use super::crypto::hash28;

///////////////////
// ValidatorHash //
///////////////////
Expand All @@ -43,10 +38,6 @@ impl ValidatorHash {
}
}

pub(crate) fn validator_hash(input: &str) -> IResult<&str, ValidatorHash, VerboseError<&str>> {
context("validator_hash", map(script_hash, ValidatorHash))(input)
}

///////////////////////
// MintingPolicyHash //
///////////////////////
Expand Down Expand Up @@ -76,12 +67,6 @@ impl TryFromPLA<MintingPolicyHash> for csl::PolicyID {
}
}

pub(crate) fn minting_policy_hash(
input: &str,
) -> IResult<&str, MintingPolicyHash, VerboseError<&str>> {
context("minting_policy_hash", map(script_hash, MintingPolicyHash))(input)
}

////////////////
// ScriptHash //
////////////////
Expand Down Expand Up @@ -115,7 +100,3 @@ impl TryFromPLA<ScriptHash> for csl::ScriptHash {
.map_err(TryFromPLAError::CSLDeserializeError)
}
}

pub(crate) fn script_hash(input: &str) -> IResult<&str, ScriptHash, VerboseError<&str>> {
context("script_hash", map(hash28, ScriptHash))(input)
}
50 changes: 35 additions & 15 deletions plutus-ledger-api/src/v1/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use cardano_serialization_lib as csl;
use lbr_prelude::json::Json;
use nom::{
character::complete::char,
combinator::{all_consuming, map},
combinator::{all_consuming, map, map_res},
error::{context, VerboseError},
sequence::preceded,
sequence::{preceded, tuple},
Finish, IResult,
};
use num_bigint::BigInt;
Expand All @@ -18,13 +18,16 @@ use serde::{Deserialize, Serialize};

use super::{
address::{Address, StakingCredential},
crypto::{hash32, LedgerBytes, PaymentPubKeyHash},
crypto::{ledger_bytes, LedgerBytes, PaymentPubKeyHash},
datum::{Datum, DatumHash},
interval::PlutusInterval,
value::{CurrencySymbol, Value},
};

use crate::{self as plutus_ledger_api, aux::big_int};
use crate::{
self as plutus_ledger_api,
aux::{big_int, guard_bytes},
};
use crate::{
csl::pla_to_csl::{TryFromPLAError, TryToCSL},
plutus_data::IsPlutusData,
Expand Down Expand Up @@ -93,20 +96,19 @@ impl TryFromPLA<Vec<TransactionInput>> for csl::TransactionInputs {
}
}

/// Nom parser for TransactionInput
/// Expects a transaction hash of 32 bytes in hexadecimal followed by a # and an integer index
/// E.g.: 1122334455667788990011223344556677889900112233445566778899001122#1
pub(crate) fn transaction_input(
input: &str,
) -> IResult<&str, TransactionInput, VerboseError<&str>> {
let (input, tx_id) = transaction_hash(input)?;

let (input, idx) = preceded(char('#'), big_int)(input)?;

Ok((
input,
TransactionInput {
transaction_id: tx_id,
index: idx,
map(
tuple((transaction_hash, preceded(char('#'), big_int))),
|(transaction_id, index)| TransactionInput {
transaction_id,
index,
},
))
)(input)
}

impl FromStr for TransactionInput {
Expand Down Expand Up @@ -146,6 +148,16 @@ impl fmt::Display for TransactionHash {
}
}

impl TransactionHash {
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, ConversionError> {
Ok(TransactionHash(LedgerBytes(guard_bytes(
"ScriptHash",
bytes,
32,
)?)))
}
}

impl FromCSL<csl::TransactionHash> for TransactionHash {
fn from_csl(value: &csl::TransactionHash) -> Self {
TransactionHash(LedgerBytes(value.to_bytes()))
Expand All @@ -159,8 +171,16 @@ impl TryFromPLA<TransactionHash> for csl::TransactionHash {
}
}

/// Nom parser for TransactionHash
/// Expects a hexadecimal string representation of 32 bytes
/// E.g.: 1122334455667788990011223344556677889900112233445566778899001122
pub(crate) fn transaction_hash(input: &str) -> IResult<&str, TransactionHash, VerboseError<&str>> {
context("transaction_hash", map(hash32, TransactionHash))(input)
context(
"transaction_hash",
map_res(ledger_bytes, |LedgerBytes(bytes)| {
TransactionHash::from_bytes(bytes)
}),
)(input)
}

impl FromStr for TransactionHash {
Expand Down
Loading