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

make STObjectType generic #7

Open
surangap opened this issue Oct 5, 2022 · 0 comments
Open

make STObjectType generic #7

surangap opened this issue Oct 5, 2022 · 0 comments

Comments

@surangap
Copy link
Contributor

surangap commented Oct 5, 2022

    not 100% sure if these will work but rough ideas (I prefer 2 because of the nicer syntax)

Idea 1

use tuple as the field inner type, see: https://crates.io/crates/impl-trait-for-tuples

#[derive(Field, Debug, Clone)]
pub struct SignerEntry(pub (Account, SignerWeight))

use impl_for_tuples::impl_for_tuples;

// support field objects with upto 5 items
#[impl_for_tuples(5)]
impl BinarySerialize for TupleIdentifier {
    fn binary_serialize_to(buf, for_signing) {
        for_tuples!( #( TupleIdentifier.binary_serialize_to(buf, for_signing) )* );
    }
}

#[impl_for_tuples(5)]
impl CodecField for TupleIdentifier {
    fn field_code(&self) -> u16 {
        for_tuples!( #( TupleIdentifier.field_code() )* );
    }
    fn type_code(&self) -> u16 {
        for_tuples!( #( TupleIdentifier.type_code() )* );
    }
    fn is_variable_length(&self) -> bool {
        for_tuples!( #( TupleIdentifier.is_variable_length() )* );
    }
    fn is_serialized(&self) -> bool {
        for_tuples!( #( TupleIdentifier.is_serialized() )* );
    }
    fn is_signing_field(&self) -> bool {
        for_tuples!( #( TupleIdentifier.is_signing_field() )* );
    }
    fn inner(&self) -> &dyn BinarySerialize {
        for_tuples!( #( TupleIdentifier.inner() )* );
    }
}

Idea 2

Transaction is basically STObject only missing the Field trait

// this will behave as an 'STObject'
#[derive(Field, STObject, Debug, Clone)]
pub struct SignerEntry {
    account: Account,
    weight: SignerWeight
}
  • implement CodecToFields for Field (macro might needs some changes to parse the above syntax)
  • Could then rename 'Transaction' trait/macro to 'STObject'

change the serialize for Field to do this instead:

let mut data = self.inner().binary_serialize(for_signing);

for f in &self.to_canonical_fields() {
    f.binary_serialize_to(buf, for_signing)
}

Originally posted by @jordy25519 in #5 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant