Hyperledger Fabric: Use JavaScript libraries on transaction processor

Tarek Lejmi
Coinmonks
3 min readJan 10, 2019

--

While coding the logic of your business in Business Network Model, Developer may be obliged to write function or code that already exists in a Javascript library. Library-like ‘numeral.js’ and ‘moment.js’, as an example , and many others may help a lot the developer.

But, since import and require are not are not supported in the script . It appears that is not possible to include any library.

Js file in lib folder

Later on, I realize that all the javascript files in the lib folder are included in the application .

So I decide to include moment.js and numeral.js to lib folder in BNM and check if they will be added to the run-time . and the result is that they are identified in the console.

momentjs and numeraljs are identified in “Javascript Runtime”

Here , I decide to use them in my transaction processor ‘logic.js’ , without importing or require them , since they will be injected later in the application and i disabled eslint undefined . where two fields of the participant in my model will have a value generated by moment and numeral . “proofs.othersProofs and adress.stressAddressOptional” when using RequestUser Transaction .

// eslint-disable-next-line no-undef
Screenshot from user.js : Transaction

Here the result in the playground and the composer-rest-server after submitting a transaction.

Composer Playground
Composer-reset-server explorer

But, you have to pay attention while adding some libraries because they may be the reason for certain exceptions. You may face problems in Playground v0.19.5 and you don’t find them in Playground v0.20.6 and others are in both. So try to add them one by one and check the result every time you add a new library to the lib file, because some of them have dependencies and others may use certain names of global variable or function defined in composer.

Happy Coding …

WWOLF UNIT 1

--

--