Skip to content

Commit

Permalink
Create object with permissioned signer
Browse files Browse the repository at this point in the history
  • Loading branch information
runtian-zhou committed Jan 13, 2025
1 parent e75f27b commit cb04560
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
33 changes: 33 additions & 0 deletions aptos-move/framework/aptos-framework/doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ make it so that a reference to a global object can be returned from a function.
- [Function `owns`](#0x1_object_owns)
- [Function `root_owner`](#0x1_object_root_owner)
- [Function `grant_permission`](#0x1_object_grant_permission)
- [Function `grant_permission_with_transfer_ref`](#0x1_object_grant_permission_with_transfer_ref)
- [Specification](#@Specification_1)
- [High-level Requirements](#high-level-req)
- [Module-level Specification](#module-level-spec)
Expand Down Expand Up @@ -2407,6 +2408,7 @@ to determine the identity of the starting point of ownership.

## Function `grant_permission`

Master signer offers a transfer permission of an object to a permissioned signer.


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_grant_permission">grant_permission</a>&lt;T&gt;(master: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, <a href="permissioned_signer.md#0x1_permissioned_signer">permissioned_signer</a>: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, <a href="object.md#0x1_object">object</a>: <a href="object.md#0x1_object_Object">object::Object</a>&lt;T&gt;)
Expand All @@ -2433,6 +2435,37 @@ to determine the identity of the starting point of ownership.



</details>

<a id="0x1_object_grant_permission_with_transfer_ref"></a>

## Function `grant_permission_with_transfer_ref`

Grant a transfer permission to the permissioned signer using TransferRef.


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_grant_permission_with_transfer_ref">grant_permission_with_transfer_ref</a>(<a href="permissioned_signer.md#0x1_permissioned_signer">permissioned_signer</a>: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, ref: &<a href="object.md#0x1_object_TransferRef">object::TransferRef</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_grant_permission_with_transfer_ref">grant_permission_with_transfer_ref</a>(
<a href="permissioned_signer.md#0x1_permissioned_signer">permissioned_signer</a>: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
ref: &<a href="object.md#0x1_object_TransferRef">TransferRef</a>,
) {
<a href="permissioned_signer.md#0x1_permissioned_signer_grant_unlimited_with_permissioned_signer">permissioned_signer::grant_unlimited_with_permissioned_signer</a>(
<a href="permissioned_signer.md#0x1_permissioned_signer">permissioned_signer</a>,
<a href="object.md#0x1_object_TransferPermission">TransferPermission</a> { <a href="object.md#0x1_object">object</a>: ref.self }
)
}
</code></pre>



</details>

<a id="@Specification_1"></a>
Expand Down
58 changes: 58 additions & 0 deletions aptos-move/framework/aptos-framework/doc/permissioned_signer.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ for blind signing.
- [Function `insert_or`](#0x1_permissioned_signer_insert_or)
- [Function `authorize_increase`](#0x1_permissioned_signer_authorize_increase)
- [Function `authorize_unlimited`](#0x1_permissioned_signer_authorize_unlimited)
- [Function `grant_unlimited_with_permissioned_signer`](#0x1_permissioned_signer_grant_unlimited_with_permissioned_signer)
- [Function `increase_limit`](#0x1_permissioned_signer_increase_limit)
- [Function `check_permission_exists`](#0x1_permissioned_signer_check_permission_exists)
- [Function `check_permission_capacity_above`](#0x1_permissioned_signer_check_permission_capacity_above)
Expand Down Expand Up @@ -75,6 +76,7 @@ for blind signing.
<pre><code><b>use</b> <a href="../../aptos-stdlib/doc/copyable_any.md#0x1_copyable_any">0x1::copyable_any</a>;
<b>use</b> <a href="create_signer.md#0x1_create_signer">0x1::create_signer</a>;
<b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error">0x1::error</a>;
<b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features">0x1::features</a>;
<b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option">0x1::option</a>;
<b>use</b> <a href="../../aptos-stdlib/doc/ordered_map.md#0x1_ordered_map">0x1::ordered_map</a>;
<b>use</b> <a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">0x1::signer</a>;
Expand Down Expand Up @@ -389,6 +391,16 @@ Access permission information from a master signer.



<a id="0x1_permissioned_signer_EPERMISSION_SIGNER_DISABLED"></a>

Permissioned signer feature is not activated.


<pre><code><b>const</b> <a href="permissioned_signer.md#0x1_permissioned_signer_EPERMISSION_SIGNER_DISABLED">EPERMISSION_SIGNER_DISABLED</a>: u64 = 9;
</code></pre>



<a id="0x1_permissioned_signer_E_NOT_ACTIVE"></a>

destroying permission handle that has already been revoked or not owned by the
Expand Down Expand Up @@ -663,6 +675,10 @@ and would abort if check fails.


<pre><code><b>public</b> <b>fun</b> <a href="permissioned_signer.md#0x1_permissioned_signer_signer_from_permissioned_handle">signer_from_permissioned_handle</a>(p: &<a href="permissioned_signer.md#0x1_permissioned_signer_PermissionedHandle">PermissionedHandle</a>): <a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a> {
<b>assert</b>!(
<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_is_permissioned_signer_enabled">features::is_permissioned_signer_enabled</a>(),
<a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_permission_denied">error::permission_denied</a>(<a href="permissioned_signer.md#0x1_permissioned_signer_EPERMISSION_SIGNER_DISABLED">EPERMISSION_SIGNER_DISABLED</a>)
);
<a href="permissioned_signer.md#0x1_permissioned_signer_signer_from_permissioned_handle_impl">signer_from_permissioned_handle_impl</a>(
p.master_account_addr, p.permissions_storage_addr
)
Expand Down Expand Up @@ -692,6 +708,10 @@ Generate the permissioned signer based on the storable permission handle.
<pre><code><b>public</b>(<b>package</b>) <b>fun</b> <a href="permissioned_signer.md#0x1_permissioned_signer_signer_from_storable_permissioned_handle">signer_from_storable_permissioned_handle</a>(
p: &<a href="permissioned_signer.md#0x1_permissioned_signer_StorablePermissionedHandle">StorablePermissionedHandle</a>
): <a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a> {
<b>assert</b>!(
<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_is_permissioned_signer_enabled">features::is_permissioned_signer_enabled</a>(),
<a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_permission_denied">error::permission_denied</a>(<a href="permissioned_signer.md#0x1_permissioned_signer_EPERMISSION_SIGNER_DISABLED">EPERMISSION_SIGNER_DISABLED</a>)
);
<b>assert</b>!(
<a href="timestamp.md#0x1_timestamp_now_seconds">timestamp::now_seconds</a>() &lt; p.expiration_time,
<a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_permission_denied">error::permission_denied</a>(<a href="permissioned_signer.md#0x1_permissioned_signer_E_PERMISSION_EXPIRED">E_PERMISSION_EXPIRED</a>)
Expand Down Expand Up @@ -1179,6 +1199,44 @@ Unlimited permission can be consumed however many times.



</details>

<a id="0x1_permissioned_signer_grant_unlimited_with_permissioned_signer"></a>

## Function `grant_unlimited_with_permissioned_signer`

Grant an unlimited permission to a permissioned signer **without** master signer's approvoal.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="permissioned_signer.md#0x1_permissioned_signer_grant_unlimited_with_permissioned_signer">grant_unlimited_with_permissioned_signer</a>&lt;PermKey: <b>copy</b>, drop, store&gt;(permissioned: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, perm: PermKey)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>package</b>) <b>fun</b> <a href="permissioned_signer.md#0x1_permissioned_signer_grant_unlimited_with_permissioned_signer">grant_unlimited_with_permissioned_signer</a>&lt;PermKey: <b>copy</b> + drop + store&gt;(
permissioned: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
perm: PermKey
) <b>acquires</b> <a href="permissioned_signer.md#0x1_permissioned_signer_PermissionStorage">PermissionStorage</a> {
<b>if</b>(!<a href="permissioned_signer.md#0x1_permissioned_signer_is_permissioned_signer">is_permissioned_signer</a>(permissioned)) {
<b>return</b>;
};
<a href="permissioned_signer.md#0x1_permissioned_signer_insert_or">insert_or</a>(
permissioned,
perm,
|stored_permission| {
*stored_permission = StoredPermission::Unlimited;
},
StoredPermission::Unlimited,
)
}
</code></pre>



</details>

<a id="0x1_permissioned_signer_increase_limit"></a>
Expand Down
33 changes: 33 additions & 0 deletions aptos-move/framework/aptos-framework/sources/object.move
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ module aptos_framework::object {
obj_owner
}

/// Master signer offers a transfer permission of an object to a permissioned signer.
public fun grant_permission<T>(
master: &signer,
permissioned_signer: &signer,
Expand All @@ -725,6 +726,17 @@ module aptos_framework::object {
)
}

/// Grant a transfer permission to the permissioned signer using TransferRef.
public fun grant_permission_with_transfer_ref(
permissioned_signer: &signer,
ref: &TransferRef,
) {
permissioned_signer::grant_unlimited_with_permissioned_signer(
permissioned_signer,
TransferPermission { object: ref.self }
)
}

#[test_only]
use std::option::{Self, Option};

Expand Down Expand Up @@ -1163,4 +1175,25 @@ module aptos_framework::object {

permissioned_signer::destroy_permissioned_handle(creator_permission_handle);
}

#[test(creator = @0x123)]
fun test_create_and_transfer(
creator: &signer,
) acquires ObjectCore {
let aptos_framework = account::create_signer_for_test(@0x1);
timestamp::set_time_has_started_for_testing(&aptos_framework);

let (_, hero) = create_hero(creator);
let (weapon_ref, weapon) = create_weapon(creator);
let t_ref = generate_transfer_ref(&weapon_ref);

// Create a permissioned signer
let creator_permission_handle = permissioned_signer::create_permissioned_handle(creator);
let creator_permission_signer = permissioned_signer::signer_from_permissioned_handle(&creator_permission_handle);

grant_permission_with_transfer_ref(&creator_permission_signer, &t_ref);
transfer_to_object(&creator_permission_signer, weapon, hero);

permissioned_signer::destroy_permissioned_handle(creator_permission_handle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,24 @@ module aptos_framework::permissioned_signer {
)
}

/// Grant an unlimited permission to a permissioned signer **without** master signer's approvoal.
public(package) fun grant_unlimited_with_permissioned_signer<PermKey: copy + drop + store>(
permissioned: &signer,
perm: PermKey
) acquires PermissionStorage {
if(!is_permissioned_signer(permissioned)) {
return;
};
insert_or(
permissioned,
perm,
|stored_permission| {
*stored_permission = StoredPermission::Unlimited;
},
StoredPermission::Unlimited,
)
}

/// Increase the `capacity` of a permissioned signer **without** master signer's approvoal.
///
/// The caller of the module will need to make sure the witness type `PermKey` can only be
Expand Down

0 comments on commit cb04560

Please sign in to comment.