Skip to content

Commit 3f312d5

Browse files
authored
Merge pull request #156 from Tonomy-Foundation/testnet
Master release: SSO login flow changes
2 parents bf04db1 + 23b0b91 commit 3f312d5

File tree

2 files changed

+97
-76
lines changed

2 files changed

+97
-76
lines changed

contracts/tonomy/include/tonomy/tonomy.hpp

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <eosio/producer_schedule.hpp>
99
#include <eosio/asset.hpp>
1010
#include <eosio/singleton.hpp>
11-
1211
#include "native.hpp"
1312

1413
namespace tonomysystem
@@ -93,45 +92,34 @@ namespace tonomysystem
9392
checksum256 username_hash,
9493
public_key password_key,
9594
checksum256 password_salt);
96-
97-
/**
98-
* Manually sets the details of an app (admin only)
99-
*
100-
* @param account_name - name of the account
101-
* @param app_name - name of the app
102-
* @param description - description of the app
103-
* @param username_hash - hash of the username
104-
* @param logo_url - url to the logo of the app
105-
* @param origin - domain associated with the app
106-
*/
107-
[[eosio::action]] void adminsetapp(
108-
name account_name,
109-
string app_name,
110-
string description,
111-
checksum256 username_hash,
112-
string logo_url,
113-
string origin);
114-
95+
/**
96+
* Manually sets the details of an app (admin only)
97+
*
98+
* @param account_name - name of the account
99+
* @param json_data - JSON string containing app details (name,description, logo_url, background_color, accent_color)
100+
* @param username_hash - hash of the username
101+
* @param origin - domain associated with the app
102+
*/
103+
[[eosio::action]] void adminsetapp(
104+
name account_name,
105+
string json_data,
106+
checksum256 username_hash,
107+
string origin);
115108
/**
116-
* Create a new account for an app and registers it's details
109+
* Create a new account for an app and registers its details
117110
*
118-
* @details Creates a new account for an app and registers it's details.
119-
*
120-
* @param name - name of the app
121-
* @param description - description of the app
122-
* @param username_hash - hash of the username
123-
* @param logo_url - url to the logo of the app
124-
* @param origin - domain associated with the app
125-
* @param password_key - public key generated from the account's password
111+
* @param json_data - JSON string containing app details (name,description, logo_url, background_color, accent_color)
112+
* @param username_hash - Hash of the username
113+
* @param origin - Domain associated with the app
114+
* @param key - Public key generated from the account's password
126115
*/
127116
[[eosio::action]] void newapp(
128-
string app_name,
129-
string description,
117+
string json_data,
130118
checksum256 username_hash,
131-
string logo_url,
132119
string origin,
133120
public_key key);
134-
121+
122+
135123
/**
136124
* Adds a new key to a person's account to log into an app with
137125
*
@@ -158,6 +146,7 @@ namespace tonomysystem
158146
public_key key,
159147
bool link_auth = false);
160148

149+
161150
/**
162151
* Update active of a person
163152
* (this is so that users can add staking.tmy@eosio.code authorization to use the staking contract)
@@ -177,6 +166,11 @@ namespace tonomysystem
177166
*/
178167
[[eosio::action]] void setresparams(double ram_price, uint64_t total_ram_available, double ram_fee);
179168

169+
/**
170+
* Delete all the old apps
171+
*/
172+
[[eosio::action]] void eraseoldapps();
173+
180174
/**
181175
* Buy RAM action allows an app to purchase RAM.
182176
* It checks the account type of the app, ensures the RAM is being purchased with the correct token,
@@ -254,7 +248,7 @@ namespace tonomysystem
254248
// also index by username hash
255249
checksum256 index_by_username_hash() const { return username_hash; }
256250
checksum256 index_by_origin_hash() const { return eosio::sha256(origin.c_str(), std::strlen(origin.c_str())); }
257-
};
251+
};
258252

259253
// Create a multi-index-table with two indexes
260254
typedef eosio::multi_index<"apps"_n, app,
@@ -267,6 +261,30 @@ namespace tonomysystem
267261
// Create an instance of the table that can is initalized in the constructor
268262
apps_table _apps;
269263

264+
struct [[eosio::table]] appv2
265+
{
266+
name account_name;
267+
string json_data; // JSON string containing app details (name, description, logo URL, background_color, accent_color)
268+
uint16_t version; // Version number to track schema changes
269+
checksum256 username_hash;
270+
string origin;
271+
272+
uint64_t primary_key() const { return account_name.value; }
273+
checksum256 index_by_username_hash() const { return username_hash; }
274+
checksum256 index_by_origin_hash() const { return eosio::sha256(origin.c_str(), std::strlen(origin.c_str())); }
275+
};
276+
277+
// Create a multi-index-table with two indexes
278+
typedef eosio::multi_index<"appsv2"_n, appv2,
279+
eosio::indexed_by<"usernamehash"_n,
280+
eosio::const_mem_fun<appv2, checksum256, &appv2::index_by_username_hash>>,
281+
eosio::indexed_by<"originhash"_n,
282+
eosio::const_mem_fun<appv2, checksum256, &appv2::index_by_origin_hash>>>
283+
appsv2_table;
284+
285+
// Create an instance of the table that can is initalized in the constructor
286+
appsv2_table _appsv2;
287+
270288
struct [[eosio::table]] resource_config
271289
{
272290
double ram_fee; // RAM fee fraction (0.01 = 1% fee)
@@ -328,6 +346,7 @@ namespace tonomysystem
328346
using loginwithapp_action = action_wrapper<"loginwithapp"_n, &tonomy::loginwithapp>;
329347
using adminsetapp_action = action_wrapper<"adminsetapp"_n, &tonomy::adminsetapp>;
330348
using setresparams_action = action_wrapper<"setresparams"_n, &tonomy::setresparams>;
349+
using eraseoldapps_action = action_wrapper<"eraseoldapps"_n, &tonomy::eraseoldapps>;
331350
using buyram_action = action_wrapper<"buyram"_n, &tonomy::buyram>;
332351
using sellram_action = action_wrapper<"sellram"_n, &tonomy::sellram>;
333352

contracts/tonomy/src/tonomy.cpp

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace tonomysystem
1111
tonomy::tonomy(name receiver, name code, eosio::datastream<const char *> ds) : native(receiver, code, ds),
1212
// instantiate multi-index instance as data member (find it defined below)
1313
_people(receiver, receiver.value),
14-
_apps(receiver, receiver.value)
14+
_apps(receiver, receiver.value),
15+
_appsv2(receiver, receiver.value)
1516
{
1617
}
1718

@@ -153,22 +154,18 @@ namespace tonomysystem
153154
row.version = 1; });
154155
}
155156

156-
void tonomy::newapp(
157-
string app_name,
158-
string description,
159-
checksum256 username_hash,
160-
string logo_url,
161-
string origin,
162-
public_key key)
157+
void tonomy::newapp(string json_data,
158+
checksum256 username_hash,
159+
string origin,
160+
public_key key)
163161
{
164162
// TODO: in the future only an organization type can create an app
165163
// check the transaction is signed by the `id.tmy` account
166-
eosio::require_auth(get_self());
167-
168-
checksum256 description_hash = eosio::sha256(description.c_str(), description.length());
164+
eosio::require_auth(get_self());
169165

170166
// generate new random account name
171-
const eosio::name random_name = random_account_name(username_hash, description_hash, enum_account_type::App);
167+
auto json_hash = eosio::sha256(json_data.c_str(), std::strlen(json_data.c_str()));
168+
const eosio::name random_name = random_account_name(username_hash, json_hash, enum_account_type::App);
172169

173170
// use the password_key public key for the owner authority
174171
authority owner_authority = create_authority_with_account(app_controller_account);
@@ -180,7 +177,7 @@ namespace tonomysystem
180177
newaccountaction.send(get_self(), random_name, owner_authority, active_authority);
181178

182179
// Check the username is not already taken
183-
auto apps_by_username_hash_itr = _apps.get_index<"usernamehash"_n>();
180+
auto apps_by_username_hash_itr = _appsv2.get_index<"usernamehash"_n>();
184181
const auto username_itr = apps_by_username_hash_itr.find(username_hash);
185182
if (username_itr != apps_by_username_hash_itr.end())
186183
{
@@ -189,7 +186,7 @@ namespace tonomysystem
189186

190187
// Check the origin is not already taken
191188
auto origin_hash = eosio::sha256(origin.c_str(), std::strlen(origin.c_str()));
192-
auto apps_by_origin_hash_itr = _apps.get_index<"originhash"_n>();
189+
auto apps_by_origin_hash_itr = _appsv2.get_index<"originhash"_n>();
193190
const auto origin_itr = apps_by_origin_hash_itr.find(origin_hash);
194191
if (origin_itr != apps_by_origin_hash_itr.end())
195192
{
@@ -203,14 +200,15 @@ namespace tonomysystem
203200
_resource_config.set(config, get_self());
204201

205202
// Store the password_salt and hashed username in table
206-
_apps.emplace(get_self(), [&](auto &app_itr)
207-
{
208-
app_itr.account_name = random_name;
209-
app_itr.app_name = app_name;
210-
app_itr.description = description;
211-
app_itr.logo_url = logo_url;
212-
app_itr.origin = origin;
213-
app_itr.username_hash = username_hash; });
203+
// Store the app details in JSON format
204+
_appsv2.emplace(get_self(), [&](auto &app_itr)
205+
{
206+
app_itr.account_name = random_name;
207+
app_itr.json_data = json_data;
208+
app_itr.version = 2;
209+
app_itr.username_hash = username_hash;
210+
app_itr.origin = origin;
211+
});
214212

215213
// Store the account type in the account_type table
216214
account_type_table account_type(get_self(), get_self().value);
@@ -221,11 +219,19 @@ namespace tonomysystem
221219
row.version = 1; });
222220
}
223221

222+
void tonomy::eraseoldapps() {
223+
eosio::require_auth(get_self());
224+
225+
// Delete all items in the v1 table
226+
while (_apps.begin() != _apps.end()) {
227+
_apps.erase(_apps.begin());
228+
}
229+
}
224230

225231
void tonomy::check_app_username(const checksum256 &username_hash)
226232
{
227233
// Check the username is not already taken
228-
auto apps_by_username_hash_itr = _apps.get_index<"usernamehash"_n>();
234+
auto apps_by_username_hash_itr = _appsv2.get_index<"usernamehash"_n>();
229235
const auto username_itr = apps_by_username_hash_itr.find(username_hash);
230236
if (username_itr != apps_by_username_hash_itr.end())
231237
{
@@ -235,7 +241,7 @@ namespace tonomysystem
235241
void tonomy::check_app_origin(const string &origin) {
236242
// Check the origin is not already taken
237243
auto origin_hash = eosio::sha256(origin.c_str(), std::strlen(origin.c_str()));
238-
auto apps_by_origin_hash_itr = _apps.get_index<"originhash"_n>();
244+
auto apps_by_origin_hash_itr = _appsv2.get_index<"originhash"_n>();
239245
const auto origin_itr = apps_by_origin_hash_itr.find(origin_hash);
240246
if (origin_itr != apps_by_origin_hash_itr.end())
241247
{
@@ -244,12 +250,10 @@ namespace tonomysystem
244250
}
245251

246252
void tonomy::adminsetapp(
247-
name account_name,
248-
string app_name,
249-
string description,
250-
checksum256 username_hash,
251-
string logo_url,
252-
string origin)
253+
name account_name,
254+
string json_data,
255+
checksum256 username_hash,
256+
string origin)
253257
{
254258
eosio::require_auth(get_self()); // signed by tonomy@active permission
255259

@@ -267,34 +271,32 @@ namespace tonomysystem
267271
}
268272

269273
// Check the account name is not already used
270-
auto apps_itr = _apps.find(account_name.value);
274+
auto apps_itr = _appsv2.find(account_name.value);
271275

272-
if (apps_itr != _apps.end())
276+
if (apps_itr != _appsv2.end())
273277
{
274278
if (apps_itr->origin != origin) {
275279
check_app_origin(origin);
276280
}
277281
if (apps_itr->username_hash != username_hash) {
278282
check_app_username(username_hash);
279283
}
280-
_apps.modify(apps_itr, get_self(), [&](auto &app_itr) {
284+
_appsv2.modify(apps_itr, get_self(), [&](auto &app_itr) {
281285
app_itr.account_name = account_name;
282-
app_itr.app_name = app_name;
283-
app_itr.description = description;
284-
app_itr.logo_url = logo_url;
285286
app_itr.origin = origin;
286287
app_itr.username_hash = username_hash;
288+
app_itr.json_data = json_data;
289+
app_itr.version = 2;
287290
});
288291
} else {
289292
check_app_username(username_hash);
290293
check_app_origin(origin);
291-
_apps.emplace(get_self(), [&](auto &app_itr) {
294+
_appsv2.emplace(get_self(), [&](auto &app_itr) {
292295
app_itr.account_name = account_name;
293-
app_itr.app_name = app_name;
294-
app_itr.description = description;
295-
app_itr.logo_url = logo_url;
296296
app_itr.origin = origin;
297297
app_itr.username_hash = username_hash;
298+
app_itr.json_data = json_data;
299+
app_itr.version = 2;
298300
});
299301
}
300302
}
@@ -374,8 +376,8 @@ namespace tonomysystem
374376
// eosio::require_auth(account); // this is not needed as tonomy::tonomy::updateauth_action checks the permission
375377

376378
// check the app exists and is registered with status
377-
auto app_itr = _apps.find(app.value);
378-
check(app_itr != _apps.end(), "App does not exist");
379+
auto app_itr = _appsv2.find(app.value);
380+
check(app_itr != _appsv2.end(), "App does not exist");
379381

380382
// TODO: uncomment when apps have status
381383
// check(app_itr->status == tonomy::enum_account_status::Active_Status, "App is not active");

0 commit comments

Comments
 (0)