HelixNetwork / pendulum

Pendulum is a distributed messaging protocol that enables globally available tamper proof timestamps :hourglass_flowing_sand:
https://dev.hlx.ai
Other
10 stars 6 forks source link

Calling API from new node #204

Closed alpertandogan closed 4 years ago

alpertandogan commented 4 years ago

This is our node http://helix.proteka.com.tr:8085

We try to call api from our server like below and we receive and error

curl -X POST "http://helix.proteka.com.tr:8085/getNodeInfo?command=%2FgetNodeInfo" -H "accept: application/json" curl -X POST "http://helix.proteka.com.tr:8085/1.0.0/getNodeInfo?command=%2FgetNodeInfo" -H "accept: application/json" {"error":"Invalid API Version","duration":1} So which method to use call api from our node.

spangin commented 4 years ago

Please add "X-HELIX-API-Version: 1" to the request header.

alpertandogan commented 4 years ago

According this is api docs for Pendulum ...

https://app.swaggerhub.com/apis-docs/Helix-Network/Pendulum/1.0.0#/

[root@etest ~]# curl -X POST "http://helix.proteka.com.tr:8085/getNodeInfo?command=getNodeInfo" -H "accept:application/json" -H "X-HELIX-API-Version: 1"

recive -- {"exception":"Invalid request payload: \u0027\u0027","duration":0}[root@etest ~]#

spangin commented 4 years ago

Please try this:

curl http://helix.proteka.com.tr:8085 -X POST -H "Content-Type: application/json" -H "X-HELIX-API-Version: 1" -d "{'command':'getNodeInfo'}"

alpertandogan commented 4 years ago

Work well Thanks {"appName":"Pendulum","appVersion":"0.6.9","jreAvailableProcessors":6,"jreFreeMemory":64525656,"jreVersion":"1.8.0_222","jreMaxMemory":883949568,"jreTotalMemory":89653248,"currentRoundIndex":107717,"latestSolidRoundHash":"0000000000000000000000000000000000000000000000000000000000000000","latestSolidRoundIndex":0,"roundStartIndex":0,"lastSnapshottedRoundIndex":0,"neighbors":0,"packetsQueueSize":0,"time":1570639762522,"tips":0,"transactionsToRequest":0,"features":["snapshotPruning","dnsRefresher","RemotePOW"],"duration":4}[

alpertandogan commented 4 years ago
Javascript test page 
var xhr = new XMLHttpRequest();
var url = "http://helix.proteka.com.tr:8085";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("X-HELIX-API-Version", "1");
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
       // var json = JSON.parse(xhr.responseText);
        console.log(xhr.responseText);
    }
};
//var data = JSON.stringify({"command": "getNodeInfo"}); ///get node info works well 
//var data = JSON.stringify({"command":"storeTransactions","txs":['aa','bb']});  //storeTransactions  GET ERROR according to doc txs as array of string
var data = JSON.stringify({"command":"storeTransactions","txs":["aa","bb"]});  //storeTransactions  according to doc txs as array of string
xhr.send(data);

ERROR RETURN

error: "Invalid bytes input. Invalid txs input",

spangin commented 4 years ago

If you send raw data, the data has to be a valid transaction. You can try to use sdk: https://github.com/HelixNetwork/pendulum-sdk

alpertandogan commented 4 years ago

I try to simulate this api but i thing its changed to 2 one.

1.. https://app.swaggerhub.com/apis-docs/Helix-Network/Pendulum/1.0.0

2.. https://github.com/HelixNetwork/pendulum-sdk/blob/master/api_reference.md

My step just post transaction /storeTransactions (txs) to node and get back /findTransactions , /getTransactionStrings and from node

oracle58 commented 4 years ago

As @spangin mentioned, in order to use storeTransactions you will need "valid" bytes, which have already been attached, i.e. you would first need to request attachToTangle (which attaches valid nonce and timestamp). This request would return txbytes, that you can store. Note, that attachToTangle only works on nodes with "remotePoW"-feature enabled (which is not recommended in production setting).

In order to use the SDK for this functionality:

  1. create a node project: $ npm init
  2. install the core package: $ npm i @helixnetwork/core
  3. install the converter package: $ npm i @helixnetwork/converter
oracle58 commented 4 years ago

SDK Example

For getNodeInfo, storeTransactions and findTransactions

// Require the Helix packages
const Helix = require("@helixnetwork/core");
const Converter = require("@helixnetwork/converter");

// Create a new instance of the Helix object
// Use the provider field to specify which node to connect to
const helix = Helix.composeAPI({
  provider: "http://localhost:8085"
});

getNodeInfo

helix
  .getNodeInfo()
  .then(info => console.log(info))
  .catch(err => {
        // ...
   });

storeTransactions

// Sender
const seed =
  "0000000000000000000000000000000000000000000000000000000000000000"; // only needs to be changed if transfers contain value
// Receiver
const rcvAddr =
  "cf36dc1226a5e275641a2486a2bd2bf44347dac544d24079113597ee25b2cb9f"; // specify an own address here

// Store the TxBytes that are returned from prepareTransfers function
var storedTxBytes;

var transfers = [{
  address: rcvAddr,
  value: 0,
  message: Converter.asciiToTxHex("aa"),
  tag: "aa11"
},
{
  address: rcvAddr,
  value: 0,
  message: Converter.asciiToTxHex("bb"),
  tag: "bb22"
}];

// Create bundle and return the TxBytes of the prepared TXs
helix
  .prepareTransfers(seed, transfers, {security:2})
  .then(function(TxBytes) {
    storedTxBytes = TxBytes;
    // Finalize and broadcast the bundle to the node
    return helix.sendTransactionStrings(
      storedTxBytes,
      5 /*depth*/,
      2 /*minimum weight magnitude*/
    );
  })
  .then(results => console.log(results))
  .catch(err => {
    console.log(err);
  });

findTransactions

helix
  .findTransactionObjects({ addresses: [rcvAddr] })
  .then(transactions => {
    console.log("Found messages:");
    for (let i = 0; i<transactions.length; i++) {
      console.log(i + ": " + Converter.txsToAscii(transactions[i].signatureMessageFragment));
    }
  })
  .catch(err => {
           // ...
  })
alpertandogan commented 4 years ago

Hi

I want to thanks for all first.

I want to explain my scenario first and try to learn which method or way to use your pendulum in my scenario.

I have a system

On client log Esp8266 with BMe280 use my own Arduino source (private SSL) communicate with server side Apache 2.4,PHP7,Oracle 19db use https. On server site use nodejs etc.

My scenario using pendulum; I want to store my data in my DB but for each row in my system i made (SHA256, md5) summary key add this key to my record.

I want to send this md5 key to Pendulum and take a pendulum record id and put this pendulum record id to my system. Pendulum make blockchain things i dont care.

But when i want to check my record (with pendulum record id) by calling pendulum node pendulum returns my md5 and show this pendulum record is approved by nodes etc.(blockchain things)

So all this is my seneraio wish to use. Is it correct Best regards.

oracle58 commented 4 years ago

Hey @alpertandogan,

Ok, if I understand the scenario correctly, you would like to:

  1. For each new entry in your DB, submit the key of that entry to pendulum
  2. Receive a record_id from pendulum and also store this in your DB
  3. Find the data (in your case md5 hash) in pendulum using the record_id

Please feel free to reach out for a private conversation, where we could discuss possible setup (ie how to use our sdk for this type of scenario).

Btw, If you find some time, I would gladly demystify the whole "blockchain-thing" :) - but in another thread, as https://github.com/HelixNetwork/pendulum/issues should be dedicated to actual issues/problems. thanks for understanding.

alpertandogan commented 4 years ago

Hi @oliverfn

Yes you correctly understand my scenario.

I would like to call API methods directly because i will call directy from Oracle using native http request.

So its very complicated include your SDK to ORACLE .

By the way if i understand the JSON format needs for API call i can prepare JSON and parse JSON cames from Pendulum API server.

I try to simulate using pure javascript new XMLHttpRequest(); POST method . It works with getNodeInfo well.

But i am not sure for other methods . Not sure which methods to use and orders . I dont want to be expert in blockchain .I only needs to use API of Pendulum. I m sure Pendulum doing well of its needs.

As your summary 1.2. submit the key of that entry use ??? method post (SAMPLE include my MYMD5) JSON get JSON parse (FIELD RETURN JSON) -> record_id

  1. find the data (in your case md5 hash) in use ??? method post (SAMPLE include record_id) get JSON parse (FIELD RETURN JSON) -->

MYMD5: My posted string at step 1.2 APPROVED NUMBER OF NODES BY PENDULUM: this fields means my MYDB5 data approved by many nodes) RECORDED DATE : (its recorded at that date )

Also Please feel free to reach out for a private conversation thank you but i can not find also private conversation method :)

Thanks a lot and sorry for a lot of question.

oracle58 commented 4 years ago

Discussion moved. Closed for now.