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

[account] Add create_delegate_account function #31

Merged
merged 3 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/StarcoinFramework/BuildInfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: YieldFarmingV2
: StarcoinFramework
source_digest: 393B652869BD1D391A88E8A7874298F9AEEF181C4955513FE7D69C00899AD3A2
source_digest: E9144252A3E50D7727A1E1A0DD76494580D66DD68C46C9625CC1042087A4D5BE
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file modified build/StarcoinFramework/bytecode_modules/Account.mv
Binary file not shown.
68 changes: 68 additions & 0 deletions build/StarcoinFramework/docs/Account.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The module for the account resource that governs every account
- [Function `create_signer`](#0x1_Account_create_signer)
- [Function `create_account_with_initial_amount`](#0x1_Account_create_account_with_initial_amount)
- [Function `create_account_with_initial_amount_v2`](#0x1_Account_create_account_with_initial_amount_v2)
- [Function `create_delegate_account`](#0x1_Account_create_delegate_account)
- [Function `deposit_to_self`](#0x1_Account_deposit_to_self)
- [Function `deposit`](#0x1_Account_deposit)
- [Function `deposit_with_metadata`](#0x1_Account_deposit_with_metadata)
Expand Down Expand Up @@ -77,6 +78,7 @@ The module for the account resource that governs every account


<pre><code><b>use</b> <a href="Authenticator.md#0x1_Authenticator">0x1::Authenticator</a>;
<b>use</b> <a href="BCS.md#0x1_BCS">0x1::BCS</a>;
<b>use</b> <a href="CoreAddresses.md#0x1_CoreAddresses">0x1::CoreAddresses</a>;
<b>use</b> <a href="Errors.md#0x1_Errors">0x1::Errors</a>;
<b>use</b> <a href="Event.md#0x1_Event">0x1::Event</a>;
Expand Down Expand Up @@ -476,6 +478,16 @@ Message for accept token events



<a name="0x1_Account_ADDRESS_LENGTH"></a>

The address bytes length


<pre><code><b>const</b> <a href="Account.md#0x1_Account_ADDRESS_LENGTH">ADDRESS_LENGTH</a>: u64 = 16;
</code></pre>



<a name="0x1_Account_CONTRACT_ACCOUNT_AUTH_KEY_PLACEHOLDER"></a>


Expand Down Expand Up @@ -1097,6 +1109,62 @@ reserved address for the MoveVM.



</details>

<a name="0x1_Account_create_delegate_account"></a>

## Function `create_delegate_account`

Generate an new address and create a new account, then delegate the account and return the new account address and <code><a href="Account.md#0x1_Account_SignerCapability">SignerCapability</a></code>


<pre><code><b>public</b> <b>fun</b> <a href="Account.md#0x1_Account_create_delegate_account">create_delegate_account</a>(sender: &signer): (<b>address</b>, <a href="Account.md#0x1_Account_SignerCapability">Account::SignerCapability</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="Account.md#0x1_Account_create_delegate_account">create_delegate_account</a>(sender: &signer) : (<b>address</b>, <a href="Account.md#0x1_Account_SignerCapability">SignerCapability</a>) <b>acquires</b> <a href="Account.md#0x1_Account_Balance">Balance</a>, <a href="Account.md#0x1_Account">Account</a> {
<b>let</b> sender_address = <a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender);
<b>let</b> sequence_number = <a href="Account.md#0x1_Account_sequence_number">Self::sequence_number</a>(sender_address);
// <b>use</b> stc balance <b>as</b> part of seed, just for new <b>address</b> more random.
<b>let</b> stc_balance = <a href="Account.md#0x1_Account_balance">Self::balance</a>&lt;<a href="STC.md#0x1_STC">STC</a>&gt;(sender_address);

<b>let</b> seed_bytes = <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&sender_address);
<a href="Vector.md#0x1_Vector_append">Vector::append</a>(&<b>mut</b> seed_bytes, <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&sequence_number));
<a href="Vector.md#0x1_Vector_append">Vector::append</a>(&<b>mut</b> seed_bytes, <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&stc_balance));

<b>let</b> seed_hash = <a href="Hash.md#0x1_Hash_sha3_256">Hash::sha3_256</a>(seed_bytes);
<b>let</b> i = 0;
<b>let</b> address_bytes = <a href="Vector.md#0x1_Vector_empty">Vector::empty</a>();
<b>while</b> (i &lt; <a href="Account.md#0x1_Account_ADDRESS_LENGTH">ADDRESS_LENGTH</a>) {
<a href="Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> address_bytes, *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&seed_hash,i));
i = i + 1;
};
<b>let</b> new_address = <a href="BCS.md#0x1_BCS_to_address">BCS::to_address</a>(address_bytes);
<a href="Account.md#0x1_Account_create_account_with_address">Self::create_account_with_address</a>&lt;<a href="STC.md#0x1_STC">STC</a>&gt;(new_address);
<b>let</b> new_signer = <a href="Account.md#0x1_Account_create_signer">Self::create_signer</a>(new_address);
(new_address, <a href="Account.md#0x1_Account_remove_signer_capability">Self::remove_signer_capability</a>(&new_signer))
}
</code></pre>



</details>

<details>
<summary>Specification</summary>



<pre><code><b>pragma</b> verify = <b>false</b>;
</code></pre>



</details>

<a name="0x1_Account_deposit_to_self"></a>
Expand Down
Binary file modified build/StarcoinFramework/source_maps/Account.mvsm
Binary file not shown.
17 changes: 17 additions & 0 deletions integration-tests/account/delegate_account.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
processed 7 tasks

task 5 'run'. lines 27-44:
{
"gas_used": 522473,
"status": {
"Keep": "Executed"
}
}

task 6 'run'. lines 47-62:
{
"gas_used": 139958,
"status": {
"Keep": "Executed"
}
}
62 changes: 62 additions & 0 deletions integration-tests/account/delegate_account.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//# init -n dev

//# faucet --addr bob --amount 2000000000

//# faucet --addr alice

//# faucet --addr default

//# publish
module default::Holder {

struct Hold<T> has key, store {
x: T
}

public fun hold<T: store>(account: &signer, x: T) {
move_to(account, Hold<T>{x})
}

public fun get<T: store>(account: address): T
acquires Hold {
let Hold {x} = move_from<Hold<T>>(account);
x
}
}

//# run --signers bob

script {
use StarcoinFramework::STC::STC;
use StarcoinFramework::Account;
use StarcoinFramework::Signer;
use default::Holder;

fun main(sender: signer) {
let (new_address, signer_cap) = Account::create_delegate_account(&sender);
// transfer STC to new account
Account::pay_from<STC>(&sender, new_address, 10000);
let new_account_signer = Account::create_signer_with_cap(&signer_cap);
assert!(new_address == Signer::address_of(&new_account_signer), 1000);
Holder::hold(&sender, signer_cap);
}
}
// check: EXECUTED


//# run --signers alice
script {
use StarcoinFramework::STC::STC;
use StarcoinFramework::Account::{Self, SignerCapability};
use default::Holder;
fun main(sender: signer) {
let contract_account_signer_cap = Holder::get<SignerCapability>(@bob);
//alice withdraw from contract account and deposit to self.
let contract_account = Account::create_signer_with_cap(&contract_account_signer_cap);
let stc = Account::withdraw<STC>(&contract_account, 10000);
Account::deposit_to_self(&sender, stc);
Holder::hold(&sender, contract_account_signer_cap);
}
}

// check: EXECUTED
33 changes: 33 additions & 0 deletions sources/Account.move
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Account {
use StarcoinFramework::CoreAddresses;
use StarcoinFramework::Errors;
use StarcoinFramework::STC::{Self, STC};
use StarcoinFramework::BCS;

spec module {
pragma verify = false;
Expand Down Expand Up @@ -132,6 +133,9 @@ module Account {
const DUMMY_AUTH_KEY:vector<u8> = x"0000000000000000000000000000000000000000000000000000000000000000";
// cannot be dummy key, or empty key
const CONTRACT_ACCOUNT_AUTH_KEY_PLACEHOLDER:vector<u8> = x"0000000000000000000000000000000000000000000000000000000000000001";

/// The address bytes length
const ADDRESS_LENGTH: u64 = 16;

/// A one-way action, once SignerCapability is removed from signer, the address cannot send txns anymore.
/// This function can only called once by signer.
Expand Down Expand Up @@ -279,6 +283,35 @@ module Account {
pragma verify = false;
}

/// Generate an new address and create a new account, then delegate the account and return the new account address and `SignerCapability`
public fun create_delegate_account(sender: &signer) : (address, SignerCapability) acquires Balance, Account {
let sender_address = Signer::address_of(sender);
let sequence_number = Self::sequence_number(sender_address);
// use stc balance as part of seed, just for new address more random.
let stc_balance = Self::balance<STC>(sender_address);

let seed_bytes = BCS::to_bytes(&sender_address);
Vector::append(&mut seed_bytes, BCS::to_bytes(&sequence_number));
Vector::append(&mut seed_bytes, BCS::to_bytes(&stc_balance));

let seed_hash = Hash::sha3_256(seed_bytes);
let i = 0;
let address_bytes = Vector::empty();
while (i < ADDRESS_LENGTH) {
Vector::push_back(&mut address_bytes, *Vector::borrow(&seed_hash,i));
i = i + 1;
};
let new_address = BCS::to_address(address_bytes);
Self::create_account_with_address<STC>(new_address);
let new_signer = Self::create_signer(new_address);
(new_address, Self::remove_signer_capability(&new_signer))
}

spec create_delegate_account {
pragma verify = false;
//TODO write spec
}

/// Deposits the `to_deposit` token into the self's account balance
public fun deposit_to_self<TokenType: store>(account: &signer, to_deposit: Token<TokenType>)
acquires Account, Balance, AutoAcceptToken {
Expand Down