1

Unfortunately is Node.js 'require' not supported - I was hoping to import Stanford Javascript Crypto Library - by the transaction processor. So my question: I would like to compute a sha256 hash of a file's content in a transaction. Is there a "painless" way of computing a sha256 hash using the transaction processor?

Thanks for your help!

gouigoui
  • 48
  • 8

2 Answers2

1

Don't try to use Node. Just include a JavaScript function right in the transaction definition. Use a separate file if you want. I did a quick Google and found a few such as

https://github.com/emn178/js-sha256/blob/master/src/sha256.js

I do my file hashing in the client but use a JavaScript function within my transaction function to generate a GUID so the process is the same.

David Berger
  • 766
  • 5
  • 10
  • Thanks a lot! Since I can't rely on the data source I thought that computing the hash within the transaction function would ensure the file's integrity, or am I going in the wrong direction? – gouigoui Feb 18 '18 at 20:08
0

Quick example using the library found by David Berger:

https://github.com/emn178/js-sha256/blob/master/src/sha256.js

(downloading file from the web, then computing the hash).

Probably it would be wiser to use an external REST service to compute the hash (maybe with https://hyperledger.github.io/composer/integrating/call-out, but headers are missing?)

Edit: It works only using playground (client side), see Function in logic.js works in playground but not in REST server, right still learning... Maybe I should try with https://www.npmjs.com/package/request... or my own external REST service.

/**
* This part will only work on playground. Should try with
* @param {String} documentUrl 
*/
function getContent(documentUrl)
{
    return fetch(documentUrl, {
        method: 'GET'
    }).then((response) => {
        return response.text()
        .then((text) => {
          return text;
        });
     });
}

/**
 * @param {String} documentUrl
 */
function generateHash(documentUrl)
{
    return getContent(documentUrl)
    .then((documentContent) => {
        let hash = sha256.create();
        hash.update(documentContent);
        return hash.hex();
    });
}

So, case closed, but it was - I must admit - an easy one. Now I'm facing more complicated problems calling external rest APIs using Http, rest endpoints instead of using wrappers... The good point: the code will be stripped of unnecessary stuff. Still, the learning curve of hyperleder composer is far less steep than hyperledger fabric alone. Great tool!

gouigoui
  • 48
  • 8