-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathsignupeoseos.cpp
99 lines (85 loc) · 3.17 KB
/
signupeoseos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by Hongbo Tang on 2018/7/5.
//
#include "signupeoseos.hpp"
void signupeoseos::transfer(account_name from, account_name to, asset quantity, string memo) {
if (from == _self || to != _self) {
return;
}
eosio_assert(quantity.symbol == string_to_symbol(4, "EOS"), "signupeoseos only accepts EOS for signup eos account");
eosio_assert(quantity.is_valid(), "Invalid token transfer");
eosio_assert(quantity.amount > 0, "Quantity must be positive");
memo.erase(memo.begin(), find_if(memo.begin(), memo.end(), [](int ch) {
return !isspace(ch);
}));
memo.erase(find_if(memo.rbegin(), memo.rend(), [](int ch) {
return !isspace(ch);
}).base(), memo.end());
auto separator_pos = memo.find(' ');
if (separator_pos == string::npos) {
separator_pos = memo.find('-');
}
eosio_assert(separator_pos != string::npos, "Account name and other command must be separated with space or minuses");
string account_name_str = memo.substr(0, separator_pos);
eosio_assert(account_name_str.length() == 12, "Length of account name should be 12");
account_name new_account_name = string_to_name(account_name_str.c_str());
string public_key_str = memo.substr(separator_pos + 1);
eosio_assert(public_key_str.length() == 53, "Length of publik key should be 53");
string pubkey_prefix("EOS");
auto result = mismatch(pubkey_prefix.begin(), pubkey_prefix.end(), public_key_str.begin());
eosio_assert(result.first == pubkey_prefix.end(), "Public key should be prefix with EOS");
auto base58substr = public_key_str.substr(pubkey_prefix.length());
vector<unsigned char> vch;
eosio_assert(decode_base58(base58substr, vch), "Decode pubkey failed");
eosio_assert(vch.size() == 37, "Invalid public key");
array<unsigned char,33> pubkey_data;
copy_n(vch.begin(), 33, pubkey_data.begin());
asset stake_net(1000, S(4, EOS));
asset stake_cpu(1000, S(4, EOS));
asset buy_ram = quantity - stake_net - stake_cpu;
eosio_assert(buy_ram.amount > 0, "Not enough eos to buy ram");
signup_public_key pubkey = {
.type = 0,
.data = pubkey_data,
};
key_weight pubkey_weight = {
.key = pubkey,
.weight = 1,
};
authority owner = authority{
.threshold = 1,
.keys = {pubkey_weight},
.accounts = {},
.waits = {}
};
authority active = authority{
.threshold = 1,
.keys = {pubkey_weight},
.accounts = {},
.waits = {}
};
newaccount new_account = newaccount{
.creator = _self,
.name = new_account_name,
.owner = owner,
.active = active
};
action(
permission_level{ _self, N(active) },
N(eosio),
N(newaccount),
new_account
).send();
action(
permission_level{ _self, N(active)},
N(eosio),
N(buyram),
make_tuple(_self, new_account_name, buy_ram)
).send();
action(
permission_level{ _self, N(active)},
N(eosio),
N(delegatebw),
make_tuple(_self, new_account_name, stake_net, stake_cpu, true)
).send();
}