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

[Move] Introduce Witness and authority design pattern #53

Closed
jolestar opened this issue Apr 18, 2023 · 1 comment
Closed

[Move] Introduce Witness and authority design pattern #53

jolestar opened this issue Apr 18, 2023 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Milestone

Comments

@jolestar
Copy link
Contributor

jolestar commented Apr 18, 2023

This PR introduces a design pattern. We can use this pattern to implement the type security in Move like borrow_global|move_from|move_to.

The PR introduces a new struct_tag module, but we can implement the pattern in the type_info module.

module moveos_std::type_info{

   /// Returns the module authority for the TypeInfo of type `T`
   public fun module_authority<T>(): TypeInfo {
       let TypeInfo {
            account_address,
            module_name,
            struct_name: _,
        } = type_of<T>();

        TypeInfo {
            address_,
            module_name,
            struct_name: b"Witness",
        }
   }
}

Then we can implement a public version of bcd::from_bytes

Current friend version:

moveos_std::bcd{
   public(friend) native fun from_bytes<MoveValue>(bytes: vector<u8>): MoveValue;
}

New public version:

module moveos_std::bcd{
   public(friend) native fun internal_from_bytes<MoveValue>(bytes: vector<u8>): MoveValue;
   public fun from_bytes<W,MoveValue>(witness: W, bytes: vector<u8>): MoveValue{
      assert!(type_info::module_authority<MoveValue> == type_info::type_of<W>, ENO_MODULE_AUTHORITY);
      internal_from_bytes(bytes)
   }
}

module my_app:my_module{
   struct Witness{}
   struct MyResource{}
   
   public fun new_from_bytes(bytes:&vector<u8>): MyResource{
      bcd::from_bytes<Witness, MyResource>(Witness{}, bytes) 
   }
}

We also can use this method to resolve the #19 security issue.

@jolestar jolestar added the enhancement New feature or request label Apr 18, 2023
@jolestar jolestar added this to Rooch Apr 18, 2023
@jolestar jolestar added this to the Rooch v0.1 milestone Apr 18, 2023
@jolestar jolestar added the help wanted Extra attention is needed label Apr 18, 2023
@jolestar
Copy link
Contributor Author

jolestar commented May 4, 2023

If we introduce the private_generics #64 , we do not need this feature.

@jolestar jolestar closed this as not planned Won't fix, can't repro, duplicate, stale May 4, 2023
@github-project-automation github-project-automation bot moved this to Done in Rooch May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
Status: Done
Development

No branches or pull requests

1 participant