Browse Source

experimental RPC to shorten URLs and decoded shortened twister URI to the original URL

miguelfreitas
Miguel Freitas 9 years ago
parent
commit
a00dfcf247
  1. 4
      src/bitcoinrpc.cpp
  2. 2
      src/bitcoinrpc.h
  3. 89
      src/twister.cpp

4
src/bitcoinrpc.cpp

@ -288,6 +288,8 @@ static const CRPCCommand vRPCCommands[] = @@ -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()
@ -1372,6 +1374,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri @@ -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;
}

2
src/bitcoinrpc.h

@ -240,5 +240,7 @@ extern json_spirit::Value getpiecemaxseen(const json_spirit::Array& params, bool @@ -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
src/twister.cpp

@ -4283,3 +4283,92 @@ Value usernametouid(const Array& params, bool fHelp) @@ -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);
}

Loading…
Cancel
Save