Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example of a dynamic oracle #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

glitch003
Copy link
Contributor

Let the user pass in a URL, attach it to the signed data. Anyone can use this as a generic oracle.

Comment on lines +5 to +9
const temperature = resp.properties.periods[0].temperature;

// add any other data you want to include in the signature here
// like the user's identity, or a nonce
const messageToSign = { temperature, url };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have to define .properties.periods[0].temperature then it's not truly dynamic is it?

Maybe we want to include this helper function in the Lit Action:

/**
 * Extracts a nested value from an object using a dot-notation path string that can include array indices.
 * 
 * @param {string} responseValue - The path string using dot notation (e.g. 'users[0].name' or 'meta.totalUsers')
 * @param {any} data - The object to extract the value from
 * @returns {any} The value at the specified path
 * 
 * @example
 * const data = {
 *   users: [
 *     { id: 1, name: 'Alice' },
 *     { id: 2, name: 'Bob' }
 *   ],
 *   meta: { totalUsers: 2 }
 * };
 * 
 * getValueFromResponse('users[0].name', data); // Returns 'Alice'
 * getValueFromResponse('users[1].id', data);   // Returns 2
 * getValueFromResponse('meta.totalUsers', data); // Returns 2
 */
export function getValueFromResponse(responseValue: string, data: any): any {
  return responseValue.split('.').reduce((acc: any, key: string) => {
    const arrayMatch = key.match(/(\w+)\[(\d+)\]/);

    if (arrayMatch) {
      const [, arrayKey, index] = arrayMatch;
      return acc?.[arrayKey]?.[parseInt(index)];
    } else {
      return acc?.[key];
    }
  }, data);
}

Then in jsParam define the path you want to access

jsParams:{
  responsePath: 'properties.periods[0].temperature'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants