diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index d5d8019d..317fab3a 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -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 3) ConvertTo(params[3]); if (strMethod == "uidtousername" && n > 0) ConvertTo(params[0]); if (strMethod == "usernametouid" && n > 1) ConvertTo(params[1]);; + if (strMethod == "newshorturl" && n > 1) ConvertTo(params[1]);; + if (strMethod == "decodeshorturl" && n > 1) ConvertTo(params[1]);; return params; } diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 39d72950..c1d1f75a 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -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 diff --git a/src/twister.cpp b/src/twister.cpp index 2682c6df..3d6360a9 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -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 \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 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 [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); +} + +