1

I have the following Oraclize query which works (generates a random number between 1 and 5, with the API key encrypted):

oraclize_query(1, "nested", "[URL] ['json(https://api.random.org/json-rpc/1/invoke).result.random.data.0', '\\n{\"jsonrpc\":\"2.0\",\"method\":\"generateIntegers\",\"params\":{\"apiKey\":\"${[decrypt] BA3g1TNRkJ0lqv18s+v3chf2FoZEzUXuyhOUWCcoBDKrmv51clDqUvw36lLEDJizgDrCyX7GREX7YVOfctHAT/lhLiTZNQFNjw11mq/y9mJ2gXKWw+EP/13AUnvvtDcjLek7KaBsYxk2qYuKn78Nt+EAasij}\",\"n\":1,\"min\":1,\"max\":5,\"replacement\":true${[identity] \"}\"},\"id\":1${[identity] \"}\"}']");  

If I have a uint number variable, I want to be able to use this in the above query (as the 'max' value). Per Simplest way to use a variable in an Oraclize query? this is done with the strConcat and uint2str functions in the Oraclize contract. I got this working when the oraclize_query isn't nested and the API key isn't encrypted, but can't seem to get it working with the above nested query with the API key encrypted.

This is what I've tried:

string string1 = '\\n{\"jsonrpc\":\"2.0\",\"method\":\"generateIntegers\",\"params\":{\"apiKey\":\"${[decrypt] BA3g1TNRkJ0lqv18s+v3chf2FoZEzUXuyhOUWCcoBDKrmv51clDqUvw36lLEDJizgDrCyX7GREX7YVOfctHAT/lhLiTZNQFNjw11mq/y9mJ2gXKWw+EP/13AUnvvtDcjLek7KaBsYxk2qYuKn78Nt+EAasij}\",\"n\":1,\"min\":1,\"max\":';

uint number = 5;

string string3 = ',\"replacement\":true${[identity] \"}\"},\"id\":1${[identity] \"}\"}';

oraclize_query(1, "nested", "[URL] ['json(https://api.random.org/json-rpc/1/invoke).result.random.data.0', strConcat(string1, uint2str(number), string3)]");  

When testing this I'm using a new encrypted API key each time.

1 Answer 1

1

strConcat shouldn't be included inside the query string. It is a Solidity function. The correct way is the following one:

    string memory string1 = "[URL] ['json(https://api.random.org/json-rpc/1/invoke).result.random', '\\n{\"jsonrpc\":\"2.0\",\"method\":\"generateIntegers\",\"params\":{\"apiKey\":${[decrypt] BP/GT8fDh+lRNPRE4RWT/86Hcypys4kfapOzLwEHs56g4HrWhISOEKm+oKQy96i5rQnv2+mGbHNNTywqWslefgoRaYVFqXUB6cjEpKCK5XfZrDStmpftxmuO/Ekhqjj3cltH5BxYUO/PUIBzkpRECDTXP0ByjaM=},\"n\":1,\"min\":0,\"max\":";

    string memory string2 = uint2str(100);

    string memory string3 = ",\"replacement\":true,\"base\":10${[identity] \"}\"},\"id\":1${[identity] \"}\"}']";

    string memory query = strConcat(string1, string2, string3);

    bytes32 rngId = oraclize_query("nested", query);

Due to the way the engine decrypts and parses the strings, you need to encrypt your API key in the following way:

    "\"YOUR_API_KEY\""
1
  • this works but it should be "result.random.data.0" with a random.org query not "result.random"
    – ZhouW
    Oct 20, 2017 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.