Skip to content

Commit

Permalink
contract: move AttachState from standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 26, 2024
1 parent c41cac2 commit b52e1ae
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
39 changes: 32 additions & 7 deletions src/contract/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use amplify::{ByteArray, Bytes32};
use baid64::{Baid64ParseError, DisplayBaid64, FromBaid64Str};
use bp::secp256k1::rand::{random, Rng, RngCore};
use commit_verify::{CommitId, CommitmentId, Conceal, DigestExt, Sha256};
use strict_encoding::StrictEncode;
use strict_encoding::{StrictEncode, StrictSerialize};

use super::{ConfidentialState, ExposedState};
use crate::{
Expand Down Expand Up @@ -65,21 +65,44 @@ impl Display for AttachId {

impl_serde_baid64!(AttachId);

#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_COMMIT)]
#[derive(CommitEncode)]
#[commit_encode(strategy = strict, id = ConcealedAttach)]
#[display("{id}:{media_type}")]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct RevealedAttach {
pub struct AttachState {
pub id: AttachId,
/// We do not enforce a MIME standard since non-standard types can be also
/// used
pub media_type: MediaType,
}
impl StrictSerialize for AttachState {}

impl From<RevealedAttach> for AttachState {
fn from(attach: RevealedAttach) -> Self {
AttachState {
id: attach.file.id,
media_type: attach.file.media_type,
}
}
}

#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_COMMIT)]
#[derive(CommitEncode)]
#[commit_encode(strategy = strict, id = ConcealedAttach)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct RevealedAttach {
pub file: AttachState,
pub salt: u64,
}

Expand All @@ -103,8 +126,10 @@ impl RevealedAttach {
/// Convenience constructor.
pub fn with_salt(id: AttachId, media_type: impl Into<MediaType>, salt: u64) -> Self {
Self {
id,
media_type: media_type.into(),
file: AttachState {
id,
media_type: media_type.into(),
},
salt,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use assignments::{
Assign, AssignAttach, AssignData, AssignFungible, AssignRights, Assignments, AssignmentsRef,
TypedAssigns,
};
pub use attachment::{AttachId, ConcealedAttach, RevealedAttach};
pub use attachment::{AttachId, AttachState, ConcealedAttach, RevealedAttach};
pub use bundle::{BundleId, InputMap, TransitionBundle, Vin};
pub use commit::{
AssignmentCommitment, AssignmentIndex, BaseCommitment, BundleDisclosure, ContractId,
Expand Down
4 changes: 2 additions & 2 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ use crate::{

/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB_COMMIT: &str =
"stl:DjmgI8Aw-L0XQMsu-q9HS$Za-!D8BzVj-bvaq$yq-p56MdqY#jester-philips-prelude";
"stl:!fGY8ly8-b4JlfE9-tH00YSF-aLoZPpU-Fplj9Sp-aOmGNN8#poem-amen-provide";
/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB_LOGIC: &str =
"stl:J61WrKfv-HBO6mFZ-9bZzqau-j!wrLMA-9IgEJGM-hcaprmA#podium-colombo-buenos";
"stl:UMHaWn4i-HC$$goM-lrPiosO-cDL60HR-QfnpQAN-4fPoOZU#second-germany-cloud";

fn _rgb_commit_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB_COMMIT), tiny_bset! {
Expand Down
4 changes: 2 additions & 2 deletions src/validation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ impl OwnedStateSchema {
(
OwnedStateSchema::Attachment(media_type),
RevealedState::Attachment(attach),
) if !attach.media_type.conforms(media_type) => {
) if !attach.file.media_type.conforms(media_type) => {
status.add_failure(validation::Failure::MediaTypeMismatch {
opid,
state_type,
expected: *media_type,
found: attach.media_type,
found: attach.file.media_type,
});
}
(OwnedStateSchema::Fungible(schema), RevealedState::Fungible(v))
Expand Down
6 changes: 3 additions & 3 deletions src/vm/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use amplify::confinement::LargeOrdMap;
use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};

use crate::{
AssignmentType, DataState, FungibleState, GlobalStateType, RevealedAttach, WitnessOrd,
XOutpoint, XWitnessId, LIB_NAME_RGB_LOGIC,
AssignmentType, AttachState, DataState, FungibleState, GlobalStateType, WitnessOrd, XOutpoint,
XWitnessId, LIB_NAME_RGB_LOGIC,
};

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display, From)]
Expand Down Expand Up @@ -173,5 +173,5 @@ pub trait ContractState {
&self,
outpoint: XOutpoint,
ty: AssignmentType,
) -> impl DoubleEndedIterator<Item = impl Borrow<RevealedAttach>>;
) -> impl DoubleEndedIterator<Item = impl Borrow<AttachState>>;
}

0 comments on commit b52e1ae

Please sign in to comment.