Skip to content

Commit

Permalink
experimental RPC to shorten URLs and decoded shortened twister URI to…
Browse files Browse the repository at this point in the history
… the original URL
  • Loading branch information
miguelfreitas committed Mar 19, 2016
1 parent cb020fa commit a00dfcf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/bitcoinrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ static const CRPCCommand vRPCCommands[] =
{ "peekpost", &peekpost, false, true, true },
{ "usernametouid", &usernametouid, false, true, true },
{ "uidtousername", &uidtousername, false, true, true },
{ "newshorturl", &newshorturl, false, true, false },
{ "decodeshorturl", &decodeshorturl, false, true, true },
};

CRPCTable::CRPCTable()
Expand Down Expand Up @@ -1372,6 +1374,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "peekpost" && n > 3) ConvertTo<boost::int64_t>(params[3]);
if (strMethod == "uidtousername" && n > 0) ConvertTo<boost::int64_t>(params[0]);
if (strMethod == "usernametouid" && n > 1) ConvertTo<bool>(params[1]);;
if (strMethod == "newshorturl" && n > 1) ConvertTo<boost::int64_t>(params[1]);;
if (strMethod == "decodeshorturl" && n > 1) ConvertTo<boost::int64_t>(params[1]);;

return params;
}
Expand Down
2 changes: 2 additions & 0 deletions src/bitcoinrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,7 @@ extern json_spirit::Value getpiecemaxseen(const json_spirit::Array& params, bool
extern json_spirit::Value peekpost(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value uidtousername(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value usernametouid(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value newshorturl(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value decodeshorturl(const json_spirit::Array& params, bool fHelp);

#endif
89 changes: 89 additions & 0 deletions src/twister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4283,3 +4283,92 @@ Value usernametouid(const Array& params, bool fHelp)

return Value(uid);
}

Value newshorturl(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 3)
throw runtime_error(
"newshorturl <username> <k> <url>\n"
"Shorten URL, create a post containing it add to swarm.\n"
"Returns the shortened twister URI (multiple options may be returned)");

EnsureWalletIsUnlocked();

string strUsername = params[0].get_str();
int k = params[1].get_int();
string strUrl = params[2].get_str();

Array paramsSub;
Value res;

paramsSub.clear();
paramsSub.push_back(strUsername);
paramsSub.push_back(k);
Object fields;
fields.push_back(Pair("url",strUrl));
paramsSub.push_back(fields);
res = newpostcustom(paramsSub,false);

paramsSub.clear();
paramsSub.push_back(strUsername);
res = usernametouid(paramsSub, false);

vector<unsigned char> vch;
vch.resize(8);
le32enc(&vch[0], res.get_int());
le32enc(&vch[4], k);

string uid_k_64 = EncodeBase64(&vch[0], vch.size());

Array uriOptions;
uriOptions.push_back(string("twist:")+uid_k_64);

return uriOptions;
}

Value decodeshorturl(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2 )
throw runtime_error(
"decodeshorturl <twist:xxx> [timeout_sec=90]\n"
"Decodes a shortened URL by twister. May take some time to complete, like dhtget etc.\n"
"Returns the original URL or error if not found, timeout");

string strTwistURI = params[0].get_str();
int timeout = 0;
if( params.size() > 1 )
timeout = params[1].get_int();

string protocol("twist:");
if (strTwistURI.find(protocol) != 0) {
throw JSONRPCError(RPC_PARSE_ERROR, "protocol prefix error");
}
string uid_k_64 = strTwistURI.substr(protocol.size());
if (uid_k_64.length() < 12) {
throw JSONRPCError(RPC_PARSE_ERROR, "base64 string too small");
}

string vch = DecodeBase64(uid_k_64);
int uid = le32dec(&vch[0]);
int k = le32dec(&vch[4]);

Array paramsSub;
Value res;

paramsSub.clear();
paramsSub.push_back(uid);
res = uidtousername(paramsSub, false);

string strUsername = res.get_str();

paramsSub.clear();
paramsSub.push_back(strUsername);
paramsSub.push_back(k);
paramsSub.push_back("url");
if(timeout) {
paramsSub.push_back(timeout);
}
return peekpost(paramsSub,false);
}


0 comments on commit a00dfcf

Please sign in to comment.