Skip to content

Commit

Permalink
Merge branch 'amber-dev' of http://github.com/ambertime/amberchain in…
Browse files Browse the repository at this point in the history
…to amber-dev
  • Loading branch information
miozzz committed Sep 8, 2018
2 parents d095a22 + d2f73e3 commit 23155e3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/rpc/rpclist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ static const CRPCCommand vRPCCommands[] =
{ "wallet", "completepurchase", &completepurchase, false, false, true },
{ "wallet", "refundpurchase", &refundpurchase, false, false, true },
{ "wallet", "expirepurchase", &expirepurchase, false, false, true },
{ "wallet", "appendrawsendfrom", &appendrawsendfrom, false, false, true },
{ "wallet", "getservice", &getservice, false, false, true },
{ "wallet", "appendrawsendfrom", &appendrawsendfrom, false, false, true },
{ "wallet", "verifyblocksignature", &verifyblocksignature, false, false, true },

/* AMB END */
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/rpcmisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ Value getpubkeyforaddress(const Array& params, bool fHelp)
}
}

throw JSONRPCError(RPC_INVALID_PARAMETER, "No valid pubkey is registered for the given address.");
throw JSONRPCError(RPC_INVALID_PARAMETER, "No valid pubkey is registered for the given address. " + addressStr);
}


Expand Down
1 change: 1 addition & 0 deletions src/rpc/rpcserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ extern json_spirit::Value completepurchase(const json_spirit::Array& params, boo
extern json_spirit::Value refundpurchase(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value expirepurchase(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value appendrawsendfrom(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getservice(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getauthmultisigaddress(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getescrowmultisigaddress(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value writemultisigdetails(const json_spirit::Array& params, bool fHelp);
Expand Down
123 changes: 107 additions & 16 deletions src/rpc/rpcstreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2733,21 +2733,99 @@ Value delistservice(const Array& params, bool fHelp)
return writeannotatedservice(ext_params, fHelp);
}

Value hexStrToJson(Value v_str) {
std::vector<unsigned char> vucJson(ParseHex(v_str.get_str().c_str()));
std::stringstream ss;
for(unsigned int i=0;i<vucJson.size();++i) {
ss << vucJson[i];
}
Value val;
read_string(ss.str(), val);
return val;
}

// param1 - service txid
Value getservice(const Array& params, bool fHelp)
{
Array service_params;
service_params.push_back(STREAM_SERVICES);
service_params.push_back(params[0]);
Object service_result = getstreamitem(service_params, fHelp).get_obj();

BOOST_FOREACH(const Pair& d, service_result)
{
if (d.name_ == "data") {
if (d.value_.type() == obj_type) {
int vout = 0;
BOOST_FOREACH(const Pair& subpair, d.value_.get_obj())
{
if (subpair.name_ == "vout") {
vout = subpair.value_.get_int();
}
}
Array txout_params;
txout_params.push_back(params[0]); // the txid should be the same as the stream item itself
txout_params.push_back(vout);
Value v = gettxoutdata(txout_params, fHelp);
return hexStrToJson(v);
}
else {
// only other option is string
return hexStrToJson(d.value_);
}
}
}

return getstreamitem(service_params, false).get_obj();
}

// get Value in service data by name
Value getservicedata(Value service_id, std::string name)
{
Array service_params;
service_params.push_back(service_id);
Object service_result = getservice(service_params, false).get_obj();
BOOST_FOREACH(const Pair& d, service_result)
{
if (d.name_ == "data") {
BOOST_FOREACH(const Pair& subpair, d.value_.get_obj())
{
if (subpair.name_ == name) {
return subpair.value_;
}
}
}
}
return "";
}

bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}

// param1 - from-address (buyer's address)
// param2 - service txid
// param3 - name of service
// param4 - total amount
// param5 - escrow address (optional)
// param5 - badge notes, encrypted for badge creator (optional)
// param6 - badge notes, encrypted for seller (optional)
Value purchasenonconsumableservice(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 4)
throw runtime_error("Help message not found\n");

bool is_escrow = false;

if (params.size() > 4)
// support either string or number for param4
double amount = 0.0;
if (params[3].type() == str_type)
{
is_escrow = true;
amount = atof(params[3].get_str().c_str());
}
if (params[3].type() == real_type)
{
amount = params[3].get_real();
}

Array service_params;
Expand All @@ -2757,13 +2835,7 @@ Value purchasenonconsumableservice(const Array& params, bool fHelp)

std::string publisher = service_result[0].value_.get_array().back().get_str();

Array getinfo_params;
Object info = getinfo(getinfo_params,false).get_obj();

std::string burn_address = info[9].value_.get_str();

std::string funds_receiver = publisher;
std::string assets_receiver = burn_address;

Object final_purchase_data;
Object purchase_data;
Expand All @@ -2772,14 +2844,25 @@ Value purchasenonconsumableservice(const Array& params, bool fHelp)

purchase_data.push_back(Pair("selleraddress", publisher));
purchase_data.push_back(Pair("buyeraddress", params[0]));
purchase_data.push_back(Pair("amount", params[3]));
purchase_data.push_back(Pair("amount", amount));
purchase_data.push_back(Pair("quantity", "0"));
if (params.size() > 4) {
purchase_data.push_back(Pair("badgenotescreator", params[4]));
}
if (params.size() > 5) {
purchase_data.push_back(Pair("badgenotesseller", params[5]));
}

// automatically derive escrow status and escrow address
Value exp_period = getservicedata(params[1], "expirationperiod");
bool is_escrow = is_number(exp_period.get_str()) && strcmp(exp_period.get_str().c_str(), "0") != 0;
if (is_escrow)
{
std::string escrow_address = params[4].get_str();
Array escrow_params;
escrow_params.push_back("3"); // TODO: Constant
escrow_params.push_back(params[0]);
std::string escrow_address = getescrowmultisigaddress(escrow_params, false).get_str();
funds_receiver = escrow_address;
assets_receiver = escrow_address;
purchase_data.push_back(Pair("multisigaddress", escrow_address));
purchase_data.push_back(Pair("status", "in escrow"));
}
Expand All @@ -2793,7 +2876,7 @@ Value purchasenonconsumableservice(const Array& params, bool fHelp)
Array data_array;
Array ext_params;

addresses.push_back(Pair(funds_receiver, params[3]));
addresses.push_back(Pair(funds_receiver, amount));

purchase_data.push_back(Pair("toaddress", funds_receiver));
final_purchase_data.push_back(Pair("data", purchase_data));
Expand Down Expand Up @@ -2824,14 +2907,16 @@ Value purchasenonconsumableservice(const Array& params, bool fHelp)
// param4 - total amount
// param5 - quantity
// param6 - escrow address (optional)
// param7 - badge notes, encrypted for badge creator (optional)
// param8 - badge notes, encrypted for seller (optional)
Value purchaseconsumableservice(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 5)
throw runtime_error("Help message not found\n");

bool is_escrow = false;

if (params.size() > 5)
if (params.size() > 5 && params[5].get_str().length() > 0)
{
is_escrow = true;
}
Expand Down Expand Up @@ -2860,6 +2945,12 @@ Value purchaseconsumableservice(const Array& params, bool fHelp)
purchase_data.push_back(Pair("buyeraddress", params[0]));
purchase_data.push_back(Pair("amount", params[3]));
purchase_data.push_back(Pair("quantity", params[4]));
if (params.size() > 6) {
purchase_data.push_back(Pair("badgenotescreator", params[6]));
}
if (params.size() > 7) {
purchase_data.push_back(Pair("badgenotesseller", params[7]));
}

if (is_escrow)
{
Expand Down

0 comments on commit 23155e3

Please sign in to comment.