Skip to content

[Move] Introduce Witness and authority design pattern #53

@jolestar

Description

@jolestar

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions