Expose libresapi for GXS contacts import/export

To import contact
/identity/import_key
{"radix":"AgIRBAAABd..."}

To export contact
/identity/export_key
{"gxs_id":"ffffffffffffffffffffffffffffffff"}

In both cases if everithing went fine the answer is something like
{"data":{"radix":"AgIRBAAABd...", "gxs_id":"fff..."}, "returncode":"ok"}

Some retrocompatible adaptations were necessary to libretroshare

RsGenExchange::deserializeGroupData
p3IdService::deserialiseIdentityFromMemory

Now accept an extra optional pointer parameter to return the id of the
  key so we can return it back from libresapi too and can be used to
  request more information about the key to the API.
This commit is contained in:
Gioacchino Mazzurco 2017-04-19 23:48:25 +02:00
parent 17edf3c8de
commit 5ee517b64f
7 changed files with 184 additions and 74 deletions

View file

@ -1270,7 +1270,9 @@ bool RsGenExchange::getMsgRelatedMeta(const uint32_t &token, GxsMsgRelatedMetaMa
return ok;
}
bool RsGenExchange::getSerializedGroupData(const uint32_t &token, RsGxsGroupId& id,unsigned char *& data,uint32_t& size)
bool RsGenExchange::getSerializedGroupData(uint32_t token, RsGxsGroupId& id,
unsigned char *& data,
uint32_t& size)
{
RS_STACK_MUTEX(mGenMtx) ;
@ -1303,24 +1305,30 @@ bool RsGenExchange::getSerializedGroupData(const uint32_t &token, RsGxsGroupId&
return nxs_grp->serialise(data,size) ;
}
bool RsGenExchange::deserializeGroupData(unsigned char *data,uint32_t size)
bool RsGenExchange::deserializeGroupData(unsigned char *data, uint32_t size,
RsGxsGroupId* gId /*= nullptr*/)
{
RS_STACK_MUTEX(mGenMtx) ;
RsItem *item = RsNxsSerialiser(mServType).deserialise(data, &size);
RsNxsGrp *nxs_grp = dynamic_cast<RsNxsGrp*>(item) ;
RsNxsGrp *nxs_grp = dynamic_cast<RsNxsGrp*>(item);
if(item == NULL)
{
std::cerr << "(EE) RsGenExchange::deserializeGroupData(): cannot deserialise this data. Something's wrong." << std::endl;
delete item ;
return false ;
}
if(item == NULL)
{
std::cerr << "(EE) RsGenExchange::deserializeGroupData(): cannot "
<< "deserialise this data. Something's wrong." << std::endl;
delete item;
return false;
}
mReceivedGrps.push_back( GxsPendingItem<RsNxsGrp*, RsGxsGroupId>(nxs_grp, nxs_grp->grpId,time(NULL)) );
mReceivedGrps.push_back(
GxsPendingItem<RsNxsGrp*, RsGxsGroupId>(
nxs_grp, nxs_grp->grpId,time(NULL)) );
return true ;
if(gId) *gId = nxs_grp->grpId;
return true;
}
bool RsGenExchange::getGroupData(const uint32_t &token, std::vector<RsGxsGrpItem *>& grpItem)

View file

@ -299,8 +299,10 @@ protected:
* \return
*/
bool getSerializedGroupData(const uint32_t &token, RsGxsGroupId &id, unsigned char *& data, uint32_t& size);
bool deserializeGroupData(unsigned char *data, uint32_t size);
bool getSerializedGroupData(uint32_t token, RsGxsGroupId &id,
unsigned char *& data, uint32_t& size);
bool deserializeGroupData(unsigned char *data, uint32_t size,
RsGxsGroupId* gId = nullptr);
template<class GrpType>
bool getGroupDataT(const uint32_t &token, std::vector<GrpType*>& grpItem)

View file

@ -305,8 +305,10 @@ public:
virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) = 0 ;
virtual bool isARegularContact(const RsGxsId& id) = 0 ;
virtual bool serialiseIdentityToMemory(const RsGxsId& id,std::string& radix_string)=0;
virtual bool deserialiseIdentityFromMemory(const std::string& radix_string)=0;
virtual bool serialiseIdentityToMemory( const RsGxsId& id,
std::string& radix_string ) = 0;
virtual bool deserialiseIdentityFromMemory( const std::string& radix_string,
RsGxsId* id = nullptr ) = 0;
/*!
* \brief overallReputationLevel

View file

@ -698,9 +698,10 @@ bool p3IdService::getOwnIds(std::list<RsGxsId> &ownIds)
return true ;
}
bool p3IdService::serialiseIdentityToMemory(const RsGxsId& id,std::string& radix_string)
bool p3IdService::serialiseIdentityToMemory( const RsGxsId& id,
std::string& radix_string )
{
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
RS_STACK_MUTEX(mIdMtx);
// look into cache. If available, return the data. If not, request it.
@ -758,23 +759,27 @@ void p3IdService::handle_get_serialized_grp(uint32_t token)
mSerialisedIdentities[RsGxsId(id)] = s ;
}
bool p3IdService::deserialiseIdentityFromMemory(const std::string& radix_string)
bool p3IdService::deserialiseIdentityFromMemory(const std::string& radix_string,
RsGxsId* id /* = nullptr */)
{
std::vector<uint8_t> mem = Radix64::decode(radix_string) ;
std::vector<uint8_t> mem = Radix64::decode(radix_string);
if(mem.empty())
if(mem.empty())
{
std::cerr << "Cannot decode radix string \"" << radix_string << "\"" << std::endl;
return false ;
std::cerr << __PRETTY_FUNCTION__ << "Cannot decode radix string \""
<< radix_string << "\"" << std::endl;
return false;
}
if(!RsGenExchange::deserializeGroupData(mem.data(),mem.size()))
{
std::cerr << "Cannot load identity from radix string \"" << radix_string << "\"" << std::endl;
return false ;
}
if( !RsGenExchange::deserializeGroupData(
mem.data(), mem.size(), reinterpret_cast<RsGxsGroupId*>(id)) )
{
std::cerr << __PRETTY_FUNCTION__ << "Cannot load identity from radix "
<< "string \"" << radix_string << "\"" << std::endl;
return false;
}
return true ;
return true;
}
bool p3IdService::createIdentity(uint32_t& token, RsIdentityParameters &params)

View file

@ -350,8 +350,10 @@ public:
const RsIdentityUsage &use_info );
virtual bool requestPrivateKey(const RsGxsId &id);
virtual bool serialiseIdentityToMemory(const RsGxsId& id,std::string& radix_string);
virtual bool deserialiseIdentityFromMemory(const std::string& radix_string);
virtual bool serialiseIdentityToMemory(const RsGxsId& id,
std::string& radix_string);
virtual bool deserialiseIdentityFromMemory(const std::string& radix_string,
RsGxsId* id = nullptr);
/**************** RsGixsReputation Implementation ****************/