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

FIPs 36-40 changes to fio.contracts #269

Closed
60 changes: 60 additions & 0 deletions contracts/fio.address/fio.address.abi
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@
}
]
},
{
"name": "fioname_info_item",
"base": "",
"fields": [
{
"name": "id",
"type": "uint64"
},
{
"name": "fionameid",
"type": "uint64"
},
{
"name": "datadesc",
"type": "string"
},
{
"name": "datavalue",
"type": "string"
}
]
},
{
"name": "domain",
"base": "",
Expand Down Expand Up @@ -132,6 +154,32 @@
}
]
},
{
"name": "updcryptkey",
"base": "",
"fields": [
{
"name": "fio_address",
"type": "string"
},
{
"name": "encrypt_public_key",
"type": "string"
},
{
"name": "max_fee",
"type": "int64"
},
{
"name": "actor",
"type": "name"
},
{
"name": "tpid",
"type": "string"
}
]
},
{
"name": "tokenpubaddr",
"base": "",
Expand Down Expand Up @@ -690,6 +738,11 @@
"type": "regaddress",
"ricardian_contract": ""
},
{
"name": "updcryptkey",
"type": "updcryptkey",
"ricardian_contract": ""
},
{
"name": "addaddress",
"type": "addaddress",
Expand Down Expand Up @@ -792,6 +845,13 @@
],
"type": "fioname"
},
{
"name": "fionameinfo",
"index_type": "i64",
"key_names": [],
"key_types": [],
"type": "fioname_info_item"
},
{
"name": "domains",
"index_type": "i64",
Expand Down
210 changes: 205 additions & 5 deletions contracts/fio.address/fio.address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace fioio {
domains_table domains;
domainsales_table domainsales;
fionames_table fionames;
//FIP-39 begin
fionameinfo_table fionameinfo;
//FIP-39 end
fiofee_table fiofees;
eosio_names_table accountmap;
bundlevoters_table bundlevoters;
Expand All @@ -42,6 +45,9 @@ namespace fioio {
domains(_self, _self.value),
domainsales(EscrowContract, EscrowContract.value),
fionames(_self, _self.value),
//FIP-39 begin
fionameinfo(_self, _self.value),
//FIP-39 end
fiofees(FeeContract, FeeContract.value),
bundlevoters(FeeContract, FeeContract.value),
accountmap(_self, _self.value),
Expand Down Expand Up @@ -144,6 +150,66 @@ namespace fioio {

}

// FIP-39 begin
inline void updfionminf(const string &datavalue, const string &datadesc, const uint64_t &fionameid, const name &actor) {
auto fionameinfobynameid = fionameinfo.get_index<"byfionameid"_n>();
auto fionameinfo_iter = fionameinfobynameid.find(fionameid);
if(fionameinfo_iter == fionameinfobynameid.end()){
uint64_t id = fionameinfo.available_primary_key();
fionameinfo.emplace(actor, [&](struct fioname_info_item &d) {
d.id = id;
d.fionameid = fionameid;
d.datadesc = datadesc;
d.datavalue = datavalue;
});
}else {
auto matchdesc_iter = fionameinfo_iter;
//now check for multiples of same desc, enforce no duplicate datadesc values permitted in table.
int countem = 0;
while (fionameinfo_iter != fionameinfobynameid.end()) {
if (fionameinfo_iter->datadesc.compare(datadesc) == 0) {
countem++;
matchdesc_iter = fionameinfo_iter;
}
fionameinfo_iter++;
}

//this code if(countem == 0) is not tested by the existing contracts because we have only got one data description used by the contracts
// in the first delivery of the new table.
if(countem == 0){
uint64_t id = fionameinfo.available_primary_key();
fionameinfo.emplace(actor, [&](struct fioname_info_item &d) {
d.id = id;
d.fionameid = fionameid;
d.datadesc = datadesc;
d.datavalue = datavalue;
});
}
else {
//we found one to get into this block so if more than one then error.
fio_400_assert(countem == 1, "datadesc", datadesc,
"handle info error -- multiple data values present for datadesc",
ErrorInvalidValue);
fionameinfobynameid.modify(matchdesc_iter, actor, [&](struct fioname_info_item &d) {
d.datavalue = datavalue;
});
}
}

}

inline void remhandleinf(const uint64_t &fionameid) {
auto fionameinfobynameid = fionameinfo.get_index<"byfionameid"_n>();
auto fionameinfo_iter = fionameinfobynameid.find(fionameid);
if(fionameinfo_iter != fionameinfobynameid.end()){
auto next_iter = fionameinfo_iter;
next_iter++;
fionameinfobynameid.erase(fionameinfo_iter);
fionameinfo_iter = next_iter;
}
}
//FIP-39 end

inline void register_errors(const FioAddress &fa, bool domain) const {
string fioname = "fio_address";
string fioerror = "Invalid FIO address";
Expand Down Expand Up @@ -229,6 +295,8 @@ namespace fioio {
fio_400_assert(key_iter != accountmap.end(), "owner", to_string(owner.value),
"Owner is not bound in the account map.", ErrorActorNotInFioAccountMap);



uint64_t id = fionames.available_primary_key();
vector <tokenpubaddr> pubaddresses;
tokenpubaddr t1;
Expand All @@ -249,6 +317,11 @@ namespace fioio {
a.bundleeligiblecountdown = getBundledAmount();
});

//FIP-39 begin
//update the encryption key to use.
updfionminf(key_iter->clientkey, FIO_REQUEST_CONTENT_ENCRYPTION_PUB_KEY_DATA_DESC,id,owner);
//FIP-39 end

uint64_t fee_amount = chain_data_update(fa.fioaddress, pubaddresses, max_fee, fa, actor, owner,
true, tpid);

Expand Down Expand Up @@ -386,8 +459,6 @@ namespace fioio {
"Fee exceeds supplied maximum.",
ErrorMaxFeeExceeded);

//NOTE -- question here, should we always record the transfer for the fees, even when its zero,
//or should we do as this code does and not do a transaction when the fees are 0.
fio_fees(actor, asset(reg_amount, FIOSYMBOL), REMOVE_PUB_ADDRESS_ENDPOINT);
process_rewards(tpid, reg_amount, get_self(), actor);

Expand Down Expand Up @@ -488,8 +559,6 @@ namespace fioio {
"Fee exceeds supplied maximum.",
ErrorMaxFeeExceeded);

//NOTE -- question here, should we always record the transfer for the fees, even when its zero,
//or should we do as this code does and not do a transaction when the fees are 0.
fio_fees(actor, asset(reg_amount, FIOSYMBOL), REMOVE_ALL_PUB_ENDPOINT);
process_rewards(tpid, reg_amount, get_self(), actor);

Expand Down Expand Up @@ -669,6 +738,128 @@ namespace fioio {

/********* CONTRACT ACTIONS ********/

//FIP-39 begin
[[eosio::action]]
void
updcryptkey(const string &fio_address, const string &encrypt_public_key, const int64_t &max_fee,
const name &actor,
const string &tpid) {


print("updcryptkey -- called. \n");

//VERIFY INPUTS
FioAddress fa;
fio_400_assert(validateTPIDFormat(tpid), "tpid", tpid,
"TPID must be empty or valid FIO address",
ErrorPubKeyValid);

fio_400_assert(max_fee >= 0, "max_fee", to_string(max_fee), "Invalid fee value",
ErrorMaxFeeInvalid);

//requirement, allow empty string to be used!
if (encrypt_public_key.length() > 0) {
fio_400_assert(isPubKeyValid(encrypt_public_key), "encrypt_public_key", encrypt_public_key,
"Encrypt key not a valid FIO Public Key",
ErrorPubKeyValid);
}

getFioAddressStruct(fio_address, fa);
const uint128_t nameHash = string_to_uint128_hash(fa.fioaddress.c_str());
const uint128_t domainHash = string_to_uint128_hash(fa.fiodomain.c_str());

fio_400_assert(!fa.domainOnly, "fio_address", fa.fioaddress, "Invalid FIO address",
ErrorInvalidFioNameFormat);

auto domainsbyname = domains.get_index<"byname"_n>();
auto domains_iter = domainsbyname.find(domainHash);

fio_400_assert(domains_iter != domainsbyname.end(), "fio_address", fa.fioaddress,
"FIO Domain not registered",
ErrorDomainNotRegistered);

//add 30 days to the domain expiration, this call will work until 30 days past expire.
const uint32_t domain_expiration = get_time_plus_seconds(domains_iter->expiration, SECONDS30DAYS);

const uint32_t present_time = now();
fio_400_assert(present_time <= domain_expiration, "fio_address", fa.fioaddress, "FIO Domain expired",
ErrorDomainExpired);

auto namesbyname = fionames.get_index<"byname"_n>();
auto fioname_iter = namesbyname.find(nameHash);
fio_400_assert(fioname_iter != namesbyname.end(), "fio_address", fa.fioaddress,
"FIO address not registered", ErrorFioNameNotRegistered);
fio_403_assert(fioname_iter->owner_account == actor.value,
ErrorSignature); // check if actor owns FIO Address




//FEE PROCESSING
uint64_t fee_amount = 0;

//begin fees, bundle eligible fee logic
const uint128_t endpoint_hash = string_to_uint128_hash(UPDATE_ENCRYPT_KEY_ENDPOINT);

auto fees_by_endpoint = fiofees.get_index<"byendpoint"_n>();
auto fee_iter = fees_by_endpoint.find(endpoint_hash);

//if the fee isnt found for the endpoint, then 400 error.
fio_400_assert(fee_iter != fees_by_endpoint.end(), "endpoint_name", UPDATE_ENCRYPT_KEY_ENDPOINT,
"FIO fee not found for endpoint", ErrorNoEndpoint);

const int64_t reg_amount = fee_iter->suf_amount;
const uint64_t fee_type = fee_iter->type;

fio_400_assert(fee_type == 1, "fee_type", to_string(fee_type),
"update_encrypt_key unexpected fee type for endpoint update_encrypt_key, expected 1",
ErrorNoEndpoint);

const uint64_t bundleeligiblecountdown = fioname_iter->bundleeligiblecountdown;

if (bundleeligiblecountdown > 0) {
namesbyname.modify(fioname_iter, _self, [&](struct fioname &a) {
a.bundleeligiblecountdown = (bundleeligiblecountdown - 1);
});
} else {
fee_amount = fee_iter->suf_amount;
fio_400_assert(max_fee >= (int64_t) fee_amount, "max_fee", to_string(max_fee),
"Fee exceeds supplied maximum.",
ErrorMaxFeeExceeded);

fio_fees(actor, asset(reg_amount, FIOSYMBOL), UPDATE_ENCRYPT_KEY_ENDPOINT);
process_rewards(tpid, reg_amount, get_self(), actor);

if (reg_amount > 0) {
INLINE_ACTION_SENDER(eosiosystem::system_contract, updatepower)
("eosio"_n, {{_self, "active"_n}},
{actor, true}
);
}
}

if (UPDENCRYPTKEYRAM > 0) {
action(
permission_level{SYSTEMACCOUNT, "active"_n},
"eosio"_n,
"incram"_n,
std::make_tuple(actor, UPDENCRYPTKEYRAM)
).send();
}

updfionminf(encrypt_public_key, FIO_REQUEST_CONTENT_ENCRYPTION_PUB_KEY_DATA_DESC,fioname_iter->id,actor);

fio_400_assert(fee_iter != fees_by_endpoint.end(), "endpoint_name", UPDATE_ENCRYPT_KEY_ENDPOINT,
"FIO fee not found for endpoint", ErrorNoEndpoint);

const string response_string = string("{\"status\": \"OK\",\"fee_collected\":") +
to_string(reg_amount) + string("}");

send_response(response_string.c_str());

}
//FIP-39 end

[[eosio::action]]
void
regaddress(const string &fio_address, const string &owner_fio_public_key, const int64_t &max_fee,
Expand Down Expand Up @@ -1898,6 +2089,11 @@ namespace fioio {
a.addresses = pubaddresses;
});

//FIP-39 begin
//update the encryption key to use.
updfionminf(new_owner_fio_public_key, FIO_REQUEST_CONTENT_ENCRYPTION_PUB_KEY_DATA_DESC,fioname_iter->id,nm);
//FIP-39 end

// Burn the NFTs belonging to the FIO address that was just transferred

auto contractsbyname = nftstable.get_index<"byaddress"_n>();
Expand Down Expand Up @@ -1981,6 +2177,10 @@ namespace fioio {

//do the burn
const uint64_t bundleeligiblecountdown = fioname_iter->bundleeligiblecountdown;
//FIP-39 begin
//remove the associated handle information.
remhandleinf(fioname_iter->id);
//FIP-39 end
namesbyname.erase(fioname_iter);
if (tpid_iter != tpid_by_name.end()) { tpid_by_name.erase(tpid_iter); }

Expand Down Expand Up @@ -2234,7 +2434,7 @@ namespace fioio {
};

EOSIO_DISPATCH(FioNameLookup, (regaddress)(addaddress)(remaddress)(remalladdr)(regdomain)(renewdomain)(renewaddress)(
setdomainpub)(burnexpired)(decrcounter)
setdomainpub)(burnexpired)(decrcounter)(updcryptkey)
(bind2eosio)(burnaddress)(xferdomain)(xferaddress)(addbundles)(xferescrow)(addnft)(remnft)(remallnfts)
(burnnfts))
}
Loading