I installed the openpgp lib in my project and run a service that uses the Android emulator. It then throws the following error:
An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException:
Error calling module function
Error calling module function
Error calling module function
Error calling module function
Error: com.tns.NativeScriptException: Failed to find module: "stream", relative to: app/tns_modules/
In other solutions to this problem it was suggested to remove the call to @angular/platform-server from the package.json file, but my package.json does not have this.
Here are the relevant parts of my code:
import { Injectable } from '@angular/core';
//import * as openpgp from 'openpgp';
import * as openpgp from 'openpgp';
@Injectable()
export class RsaKeyService {
constructor() { }
generateKey () {
const name: string = this.hashGen(7);
const email: string = this.hashGen(7) + '@test.com';
const keyOptions = {
userIds: [{ name: name, email: email }],
passphrase: this.hashGen(10),
curve: 'ed25519',
compression: openpgp.enums.compression.zip
}
const user = {};
return new Promise((resolve) => {
openpgp.generateKey(keyOptions)
.then((key) => {
user['privateKey'] = key.privateKeyArmored;
user['publicKey'] = key.publicKeyArmored;
localStorage.setItem('RSAKey', JSON.stringify(user));
resolve(true);
});
})
}
async encrypt(stringToEncrypt: string, receiverPublicKey) {
const options: any = {
data: stringToEncrypt,
publicKeys: await openpgp.key.readArmored(receiverPublicKey).then((data) => {return data.keys})
};
return await openpgp.encrypt(options)
.then((cipherText) => {
return cipherText.data;
}).catch((error) => {
console.log('Error', error);
})
}
async decrypt(encryptedMessage: string, dataAccess) {
const privateKey = await openpgp.key.readArmored(dataAccess['privateKey']).then((data) => {return data.keys[0]});
privateKey.decrypt(dataAccess['passphrase']);
return openpgp.decrypt({
privateKeys: [privateKey],
message: await openpgp.message.readArmored(encryptedMessage)
}).then((decryptedData) => {
return decryptedData.data;
})
}
hashGen(length) {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
}
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
// Uncomment and add to NgModule imports if you need to use two-way binding
import { NativeScriptFormsModule } from "nativescript-angular/forms";
// Uncomment and add to NgModule imports if you need to use the HttpClient wrapper
import { NativeScriptHttpClientModule } from "nativescript-angular/http-client";
import { LoginComponent } from "./modules/components/login/login.component";
import { UserServiceService } from "./core/services/UserService.service";
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
import { HttpErrorInterceptor } from "./core/interceptors/http.interseptor";
import { HomeComponent } from "./modules/components/home/home.component";
import { AuthGuardService } from "./core/guards/guards.services";
import { token } from "./core/authentication/token";
import { HttpService } from "./core/services/http.service";
import { HttpModule } from "@angular/http";
import { HttpHeadersInterceptor } from "./core/interceptors/http.header.interceptor";
import { NativeScriptHttpModule } from "nativescript-angular/http";
import { RsaKeyService } from "./core/services/rsa.key.service";
@NgModule({
bootstrap: [
AppComponent
],
imports: [
NativeScriptModule,
AppRoutingModule,
NativeScriptFormsModule,
HttpClientModule,
HttpModule,
NativeScriptHttpModule
],
declarations: [
AppComponent,
LoginComponent,
HomeComponent,
],
providers: [
HttpService,
UserServiceService,
AuthGuardService,
RsaKeyService,
token,
{
provide: HTTP_INTERCEPTORS,
useClass: HttpErrorInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: HttpHeadersInterceptor,
multi: true
}
],
schemas: [
NO_ERRORS_SCHEMA
]
})
/*
Pass your application module to the bootstrapModule function located in main.ts to start your app
*/
export class AppModule { }
{
"nativescript": {
"id": "org.nativescript.CriptoZoneApp",
"tns-ios": {
"version": "5.2.0"
},
"tns-android": {
"version": "5.2.1"
}
},
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"repository": "<fill-your-repository-here>",
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/http": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"nativescript-angular": "~7.2.1",
"nativescript-localstorage": "^2.0.0",
"nativescript-theme-core": "~1.0.4",
"openpgp": "^4.4.10",
"reflect-metadata": "~0.1.12",
"rxjs": "~6.3.0",
"rxjs-compat": "^6.4.0",
"tns-core-modules": "~5.2.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "~7.2.0",
"@nativescript/schematics": "~0.5.0",
"@ngtools/webpack": "~7.2.0",
"nativescript-dev-typescript": "~0.8.0",
"nativescript-dev-webpack": "~0.20.0"
},
"gitHead": "f548ec926e75201ab1b7c4a3a7ceefe7a4db15af",
"readme": "NativeScript Application"
}
The library seems to use the stream
module that will be available only within node environment. You could only use the NPM modules those purely written with JavaScript apis, otherwise it may not work in {N} environment.