wavesplatform / waves-signature-generator

4 stars 15 forks source link

npm issue with secure-random.js #20

Closed carlos-verdes closed 5 years ago

carlos-verdes commented 5 years ago

How to reproduce.

Install on npm project as a dependency:

npm install --save @waves/signature-generator@5.0.1

Import using typescript 3.2.4:

import { utils as wavesUtils } from '@waves/signature-generator';

Execute npm start and get next error:

ERROR in ./node_modules/@waves/signature-generator/dist/libs/secure-random.js
Module not found: Error: Can't resolve 'crypto' in '$project_folder/node_modules/@waves/signature-generator/dist/libs'

Checking that dependency I see that secure-random.js call require('crypto') but there is no crypto.js on the same folder (secure-random.js is defined on libs folder and crypto on utils folder. image

Could you help with fix this issue please?

carlos-verdes commented 5 years ago

I was able to workaround this replicating same steps manually:

import { default as axlsign } from '@waves/signature-generator/libs/axlsign';
import { default as convert } from '@waves/signature-generator/dist/utils/convert';
import { concatUint8Arrays } from '@waves/signature-generator/dist/utils/concat';
import { default as base58 } from '@waves/signature-generator/dist/libs/base58';

...

  validateAuth(authPayload) {

    console.log("(login) validating auth", authPayload);

    let signature = authPayload.signature;
    let publicKey = authPayload.publicKey;
    let prefix = authPayload.prefix;
    let host = authPayload.host;
    let payload = this.loginPayload;

    let data = [prefix, host, payload]
        .map(d => convert.stringToByteArrayWithSize(d))
        .map(stringWithSize => Uint8Array.from(stringWithSize));

    let dataBytes = concatUint8Arrays(...data);
    let publicKeyBytes = base58.decode(publicKey);
    let signatureBytes = base58.decode(signature);

    //console.log("WavesGenerator", WavesGenerator);
    let validSignature = axlsign.verify(publicKeyBytes, dataBytes, signatureBytes);
    console.log("(login) validSignature?", validSignature);
  }
tsigel commented 5 years ago

This code only for nodejs. require('crypto') crypto documentation

function secureRandom(count, options) {

    options = options || { type: 'Array' };

    if (typeof window !== 'undefined' || typeof self !== 'undefined') {
        return browserRandom(count, options);
    } else if (typeof exports === 'object' && typeof module !== 'undefined') {
        return nodeRandom(count, options);
    } else {
        throw new Error('Your environment is not defined');
    }

}

U run your app in browser or nodejs?

carlos-verdes commented 5 years ago

I'm running an Angular project (browser only)... but the problem is at compile time. When I do npm start the Typescript compiler is raising an error so I can't run anything (not browser nor node).

I don't get your example, the only thing I'm trying to do is to validate a signature before sending to the server, so the main point I need to cover is the next code:

 let validSignature = axlsign.verify(publicKeyBytes, dataBytes, signatureBytes);

Original idea was to use the library:

import { utils as wavesUtils } from '@waves/signature-generator';

wavesUtils.crypto.isValidSignature(bytes, sign, publicKey)

I followed the next documentation: https://docs.wavesplatform.com/en/waves-api-and-sdk/client-api/auth-api.html

But when I import that library... I get the mentioned error. Let me know if you need more clarifications.

tsigel commented 5 years ago

What the version of typescript u use? And give u tsconfig file please. Can u get more logs, and u build script.

ERROR in ./node_modules/@waves/signature-generator/dist/libs/secure-random.js
Module not found: Error: Can't resolve 'crypto' in '$project_folder/node_modules/@waves/signature-generator/dist/libs'

This look like no typescript error.

carlos-verdes commented 5 years ago

Version of Typescript (I think I put on original post): 3.2.4

As I said I use Angular, to reproduce the error I use the next code inside a service:

import { utils as wavesUtils } from '@waves/signature-generator';
...
console.log("test waves", wavesUtils.crypto.isValidSignature(dataBytes, authPayload.signature, user.publicKey));

Then when I execute a ng serve:

$ ng serve
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **

Date: 2019-04-01T18:42:51.745Z
Hash: 97b437ba42fb91d6ff37
Time: 6550ms
chunk {main} main.js, main.js.map (main) 82.9 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 237 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.08 kB [entry] [rendered]
chunk {scripts} scripts.js, scripts.js.map (scripts) 729 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 1.1 MB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 7.08 MB [initial] [rendered]

ERROR in ./node_modules/@waves/signature-generator/dist/libs/secure-random.js
Module not found: Error: Can't resolve 'crypto' in '/Users/cverdes/git/smart-trade-front/node_modules/@waves/signature-generator/dist/libs'
ℹ 「wdm」: Failed to compile.

I understand is a compilation problem because secure-random is calling this:

var crypto = require('crypto');

... and there is no crypto lib on the same folder as signature-generator. image

tsconfig (renamed the extension to .txt, original is .json): tsconfig.txt

Let me know if you need more information!!

Jahsus commented 5 years ago

Hello, Carlos Verdes! This problem is caused by your collector settings. When building a project, the builder analyzes the code, finds 'require' calls in it, and tries to connect the appropriate dependencies, while 'crypto' is the native nodejs module. The collector does not find this module and gives an error. In the code, this place will never be called up in the browser, so you should configure the collector to skip 'crypto' and do not display any errors.

Let me know if you have more questions about this issue

carlos-verdes commented 5 years ago

It fails compiling with Node JS using Angular, could you give me a hint of how to solve the issue? Maybe I should add something on my Typescript config?

Thanks for the answer in any case.

tsigel commented 5 years ago

This is not typescript error. This error with your angular build.

carlos-verdes commented 5 years ago

@tsigel this error happen only when I add your dependency as I mentioned before and I show you which part of the code is failing.

In any case I moved from Angular to another framework and now I'm using rollup.js to get my dependencies and found another error... a Circular dependency error.

Is there any place I can chat with you @tsigel ?? I'm surprised on your answer.

image

tsigel commented 5 years ago

U can use compiled file from path "@waves/signature-generator/dist/signature-generator.min.js" from package or give me link to ur repository with problem and i try to help u.