Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
runtian-zhou committed Nov 26, 2024
1 parent e4b36b5 commit 594644d
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 21 deletions.
157 changes: 138 additions & 19 deletions aptos-move/framework/aptos-framework/doc/account.md

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions aptos-move/framework/aptos-framework/doc/fungible_asset.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ metadata object can be any object that equipped with <code><a href="fungible_ass
- [Function `ensure_store_upgraded_to_concurrent_internal`](#0x1_fungible_asset_ensure_store_upgraded_to_concurrent_internal)
- [Function `grant_permission`](#0x1_fungible_asset_grant_permission)
- [Function `grant_apt_permission`](#0x1_fungible_asset_grant_apt_permission)
- [Function `refill_permission_with_fa`](#0x1_fungible_asset_refill_permission_with_fa)
- [Function `revoke_permission`](#0x1_fungible_asset_revoke_permission)
- [Specification](#@Specification_1)
- [High-level Requirements](#high-level-req)
Expand Down Expand Up @@ -3930,6 +3931,39 @@ Master signer grant permissioned signer ability to withdraw a given amount of fu



</details>

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

## Function `refill_permission_with_fa`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_refill_permission_with_fa">refill_permission_with_fa</a>(permissioned: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, fa: &<a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">fungible_asset::FungibleAsset</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_refill_permission_with_fa">refill_permission_with_fa</a>(
permissioned: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
fa: &<a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">FungibleAsset</a>
) {
<a href="permissioned_signer.md#0x1_permissioned_signer_increase_limit">permissioned_signer::increase_limit</a>(
permissioned,
<a href="fungible_asset.md#0x1_fungible_asset_amount">amount</a>(fa) <b>as</b> u256,
<a href="fungible_asset.md#0x1_fungible_asset_WithdrawPermission">WithdrawPermission</a> {
metadata_address: <a href="object.md#0x1_object_object_address">object::object_address</a>(&<a href="fungible_asset.md#0x1_fungible_asset_metadata_from_asset">metadata_from_asset</a>(fa)),
}
)
}
</code></pre>



</details>

<a id="0x1_fungible_asset_revoke_permission"></a>
Expand Down
32 changes: 32 additions & 0 deletions aptos-move/framework/aptos-framework/doc/primary_fungible_store.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fungible asset to it. This emits an deposit event.
- [Function `is_frozen`](#0x1_primary_fungible_store_is_frozen)
- [Function `withdraw`](#0x1_primary_fungible_store_withdraw)
- [Function `deposit`](#0x1_primary_fungible_store_deposit)
- [Function `deposit_with_signer`](#0x1_primary_fungible_store_deposit_with_signer)
- [Function `force_deposit`](#0x1_primary_fungible_store_force_deposit)
- [Function `transfer`](#0x1_primary_fungible_store_transfer)
- [Function `transfer_assert_minimum_deposit`](#0x1_primary_fungible_store_transfer_assert_minimum_deposit)
Expand Down Expand Up @@ -507,6 +508,37 @@ Deposit fungible asset <code>fa</code> to the given account's primary store.



</details>

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

## Function `deposit_with_signer`

Deposit fungible asset <code>fa</code> to the given account's primary store using signer.

If <code>owner</code> is a permissioned signer, the signer will be granted with permission to withdraw
the same amount of fund in the future.


<pre><code><b>public</b> <b>fun</b> <a href="primary_fungible_store.md#0x1_primary_fungible_store_deposit_with_signer">deposit_with_signer</a>(owner: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, fa: <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">fungible_asset::FungibleAsset</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="primary_fungible_store.md#0x1_primary_fungible_store_deposit_with_signer">deposit_with_signer</a>(owner: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, fa: FungibleAsset) <b>acquires</b> <a href="primary_fungible_store.md#0x1_primary_fungible_store_DeriveRefPod">DeriveRefPod</a> {
<a href="fungible_asset.md#0x1_fungible_asset_refill_permission_with_fa">fungible_asset::refill_permission_with_fa</a>(owner, &fa);
<b>let</b> metadata = <a href="fungible_asset.md#0x1_fungible_asset_asset_metadata">fungible_asset::asset_metadata</a>(&fa);
<b>let</b> store = <a href="primary_fungible_store.md#0x1_primary_fungible_store_ensure_primary_store_exists">ensure_primary_store_exists</a>(<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(owner), metadata);
<a href="dispatchable_fungible_asset.md#0x1_dispatchable_fungible_asset_deposit">dispatchable_fungible_asset::deposit</a>(store, fa);
}
</code></pre>



</details>

<a id="0x1_primary_fungible_store_force_deposit"></a>
Expand Down
30 changes: 30 additions & 0 deletions aptos-move/framework/aptos-framework/sources/account.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module aptos_framework::account {
use aptos_framework::create_signer::create_signer;
use aptos_framework::event::{Self, EventHandle};
use aptos_framework::guid;
use aptos_framework::permissioned_signer;
use aptos_framework::system_addresses;
use aptos_std::ed25519;
use aptos_std::from_bcs;
Expand Down Expand Up @@ -179,6 +180,8 @@ module aptos_framework::account {
const ENEW_AUTH_KEY_ALREADY_MAPPED: u64 = 21;
/// The current authentication key and the new authentication key are the same
const ENEW_AUTH_KEY_SAME_AS_CURRENT: u64 = 22;
/// Current permissioned signer cannot perform the privilaged operations.
const ENO_ACCOUNT_PERMISSION: u64 = 23;

/// Explicitly separate the GUID space between Object and Account to prevent accidental overlap.
const MAX_GUID_CREATION_NUM: u64 = 0x4000000000000;
Expand All @@ -187,6 +190,23 @@ module aptos_framework::account {
/// Create signer for testing, independently of an Aptos-style `Account`.
public fun create_signer_for_test(addr: address): signer { create_signer(addr) }

struct AccountPermission has copy, drop, store {}

/// Permissions
inline fun check_signer_permission(s: &signer) {
assert!(
permissioned_signer::check_permission_exists(s, AccountPermission {}),
error::permission_denied(ENO_ACCOUNT_PERMISSION),
);
}

/// Grant permission to perform key rotations on behalf of the master signer.
///
/// This is **extermely dangerous** and should be granted only when it's absolutely needed.
public fun grant_permission(master: &signer, permissioned_signer: &signer) {
permissioned_signer::authorize_unlimited(master, permissioned_signer, AccountPermission {})
}

/// Only called during genesis to initialize system resources for this module.
public(friend) fun initialize(aptos_framework: &signer) {
system_addresses::assert_aptos_framework(aptos_framework);
Expand Down Expand Up @@ -302,6 +322,7 @@ module aptos_framework::account {
vector::length(&new_auth_key) == 32,
error::invalid_argument(EMALFORMED_AUTHENTICATION_KEY)
);
check_signer_permission(account);
let account_resource = borrow_global_mut<Account>(addr);
account_resource.authentication_key = new_auth_key;
}
Expand Down Expand Up @@ -357,6 +378,7 @@ module aptos_framework::account {
) acquires Account, OriginatingAddress {
let addr = signer::address_of(account);
assert!(exists_at(addr), error::not_found(EACCOUNT_DOES_NOT_EXIST));
check_signer_permission(account);
let account_resource = borrow_global_mut<Account>(addr);

// Verify the given `from_public_key_bytes` matches this account's current authentication key.
Expand Down Expand Up @@ -412,6 +434,7 @@ module aptos_framework::account {
new_public_key_bytes: vector<u8>,
cap_update_table: vector<u8>
) acquires Account, OriginatingAddress {
check_signer_permission(delegate_signer);
assert!(exists_at(rotation_cap_offerer_address), error::not_found(EOFFERER_ADDRESS_DOES_NOT_EXIST));

// Check that there exists a rotation capability offer at the offerer's account resource for the delegate.
Expand Down Expand Up @@ -471,6 +494,7 @@ module aptos_framework::account {
account_public_key_bytes: vector<u8>,
recipient_address: address,
) acquires Account {
check_signer_permission(account);
let addr = signer::address_of(account);
assert!(exists_at(recipient_address), error::not_found(EACCOUNT_DOES_NOT_EXIST));

Expand Down Expand Up @@ -569,6 +593,7 @@ module aptos_framework::account {
/// Revoke the rotation capability offer given to `to_be_revoked_recipient_address` from `account`
public entry fun revoke_rotation_capability(account: &signer, to_be_revoked_address: address) acquires Account {
assert!(exists_at(to_be_revoked_address), error::not_found(EACCOUNT_DOES_NOT_EXIST));
check_signer_permission(account);
let addr = signer::address_of(account);
let account_resource = borrow_global<Account>(addr);
assert!(
Expand All @@ -580,6 +605,7 @@ module aptos_framework::account {

/// Revoke any rotation capability offer in the specified account.
public entry fun revoke_any_rotation_capability(account: &signer) acquires Account {
check_signer_permission(account);
let account_resource = borrow_global_mut<Account>(signer::address_of(account));
option::extract(&mut account_resource.rotation_capability_offer.for);
}
Expand All @@ -600,6 +626,7 @@ module aptos_framework::account {
account_public_key_bytes: vector<u8>,
recipient_address: address
) acquires Account {
check_signer_permission(account);
let source_address = signer::address_of(account);
assert!(exists_at(recipient_address), error::not_found(EACCOUNT_DOES_NOT_EXIST));

Expand Down Expand Up @@ -639,6 +666,7 @@ module aptos_framework::account {
/// has a signer capability offer from `account` but will be revoked in this function).
public entry fun revoke_signer_capability(account: &signer, to_be_revoked_address: address) acquires Account {
assert!(exists_at(to_be_revoked_address), error::not_found(EACCOUNT_DOES_NOT_EXIST));
check_signer_permission(account);
let addr = signer::address_of(account);
let account_resource = borrow_global<Account>(addr);
assert!(
Expand All @@ -650,13 +678,15 @@ module aptos_framework::account {

/// Revoke any signer capability offer in the specified account.
public entry fun revoke_any_signer_capability(account: &signer) acquires Account {
check_signer_permission(account);
let account_resource = borrow_global_mut<Account>(signer::address_of(account));
option::extract(&mut account_resource.signer_capability_offer.for);
}

/// Return an authorized signer of the offerer, if there's an existing signer capability offer for `account`
/// at the offerer's address.
public fun create_authorized_signer(account: &signer, offerer_address: address): signer acquires Account {
check_signer_permission(account);
assert!(exists_at(offerer_address), error::not_found(EOFFERER_ADDRESS_DOES_NOT_EXIST));

// Check if there's an existing signer capability offer from the offerer.
Expand Down
16 changes: 15 additions & 1 deletion aptos-move/framework/aptos-framework/sources/account.spec.move
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ spec aptos_framework::account {
let post account_resource = global<Account>(addr);
aborts_if !exists<Account>(addr);
aborts_if vector::length(new_auth_key) != 32;
aborts_if permissioned_signer::spec_is_permissioned_signer(account);
modifies global<Account>(addr);
ensures account_resource.authentication_key == new_auth_key;
}
Expand All @@ -209,6 +210,7 @@ spec aptos_framework::account {
let post account_resource = global<Account>(addr);
aborts_if !exists<Account>(addr);
aborts_if vector::length(new_auth_key) != 32;
aborts_if permissioned_signer::spec_is_permissioned_signer(account);
modifies global<Account>(addr);
ensures account_resource.authentication_key == new_auth_key;
}
Expand Down Expand Up @@ -258,6 +260,7 @@ spec aptos_framework::account {
let addr = signer::address_of(account);
let account_resource = global<Account>(addr);
aborts_if !exists<Account>(addr);
aborts_if permissioned_signer::spec_is_permissioned_signer(account);

/// [high-level-req-6.1]
include from_scheme == ED25519_SCHEME ==> ed25519::NewUnvalidatedPublicKeyFromBytesAbortsIf { bytes: from_public_key_bytes };
Expand Down Expand Up @@ -331,6 +334,7 @@ spec aptos_framework::account {
new_public_key_bytes: vector<u8>,
cap_update_table: vector<u8>
) {
aborts_if permissioned_signer::spec_is_permissioned_signer(delegate_signer);
aborts_if !exists<Account>(rotation_cap_offerer_address);
let delegate_address = signer::address_of(delegate_signer);
let offerer_account_resource = global<Account>(rotation_cap_offerer_address);
Expand Down Expand Up @@ -390,7 +394,7 @@ spec aptos_framework::account {
source_address,
recipient_address,
};

aborts_if permissioned_signer::spec_is_permissioned_signer(account);
aborts_if !exists<chain_id::ChainId>(@aptos_framework);
aborts_if !exists<Account>(recipient_address);
aborts_if !exists<Account>(source_address);
Expand Down Expand Up @@ -445,6 +449,7 @@ spec aptos_framework::account {
recipient_address,
};

aborts_if permissioned_signer::spec_is_permissioned_signer(account);
aborts_if !exists<Account>(recipient_address);
aborts_if !exists<Account>(source_address);

Expand Down Expand Up @@ -504,6 +509,7 @@ spec aptos_framework::account {
/// The Account existed under the signer.
/// The value of signer_capability_offer.for of Account resource under the signer is to_be_revoked_address.
spec revoke_signer_capability(account: &signer, to_be_revoked_address: address) {
aborts_if permissioned_signer::spec_is_permissioned_signer(account);
aborts_if !exists<Account>(to_be_revoked_address);
let addr = signer::address_of(account);
let account_resource = global<Account>(addr);
Expand All @@ -515,13 +521,15 @@ spec aptos_framework::account {

spec revoke_any_signer_capability(account: &signer) {
modifies global<Account>(signer::address_of(account));
aborts_if permissioned_signer::spec_is_permissioned_signer(account);
/// [high-level-req-7.4]
aborts_if !exists<Account>(signer::address_of(account));
let account_resource = global<Account>(signer::address_of(account));
aborts_if !option::is_some(account_resource.signer_capability_offer.for);
}

spec revoke_rotation_capability(account: &signer, to_be_revoked_address: address) {
aborts_if permissioned_signer::spec_is_permissioned_signer(account);
aborts_if !exists<Account>(to_be_revoked_address);
let addr = signer::address_of(account);
let account_resource = global<Account>(addr);
Expand All @@ -534,6 +542,7 @@ spec aptos_framework::account {
}

spec revoke_any_rotation_capability(account: &signer) {
aborts_if permissioned_signer::spec_is_permissioned_signer(account);
let addr = signer::address_of(account);
modifies global<Account>(addr);
aborts_if !exists<Account>(addr);
Expand All @@ -547,6 +556,7 @@ spec aptos_framework::account {
/// The Account existed under the signer.
/// The value of signer_capability_offer.for of Account resource under the signer is offerer_address.
spec create_authorized_signer(account: &signer, offerer_address: address): signer {
aborts_if permissioned_signer::spec_is_permissioned_signer(account);
/// [high-level-req-8]
include AccountContainsAddr{
account,
Expand Down Expand Up @@ -581,9 +591,13 @@ spec aptos_framework::account {
spec fun spec_create_resource_address(source: address, seed: vector<u8>): address;

spec create_resource_account(source: &signer, seed: vector<u8>): (signer, SignerCapability) {
use aptos_framework::create_signer;
let source_addr = signer::address_of(source);
let resource_addr = spec_create_resource_address(source_addr, seed);

let resource = create_signer::spec_create_signer(resource_addr);
aborts_if permissioned_signer::spec_is_permissioned_signer(resource);

aborts_if len(ZERO_AUTH_KEY) != 32;
include exists_at(resource_addr) ==> CreateResourceAccountAbortsIf;
include !exists_at(resource_addr) ==> CreateAccountAbortsIf {addr: resource_addr};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ module aptos_framework::aptos_account {
coin::deposit(signer::address_of(alice), coin::mint(10000, &mint_cap));

let perm_handle = permissioned_signer::create_permissioned_handle(alice);
let alice_perm_signer = permissioned_signer::signer_from_permissioned(&perm_handle);
let alice_perm_signer = permissioned_signer::signer_from_permissioned_handle(&perm_handle);
fungible_asset::grant_apt_permission(alice, &alice_perm_signer, 500);

transfer(&alice_perm_signer, bob, 500);
Expand Down

0 comments on commit 594644d

Please sign in to comment.