calvinmetcalf / crypto-pouch

plugin for encrypted pouchdb/couchdb databases
MIT License
243 stars 43 forks source link

crypto-pouch and ionic 2 again #59

Closed tubauwe closed 3 years ago

tubauwe commented 7 years ago

After updating my ionic 2 project and all needed dependencies I have a problem with crypto-pouch. I'm getting the following error:

TypeError: "size" argument must be a number
    at assertSize (main.js:15267)
    at allocUnsafe (main.js:15298)
    at Function.Buffer.allocUnsafe (main.js:15312)
    at module.exports (main.js:85350)
    at main.js:129381
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (main.js:4477)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3

Without using crypto-pouch everything seems to be okay. Do you know a solution? Thanks in advance!

calvinmetcalf commented 7 years ago

are you absolutely sure ALL your dependencies are up to daye ?

tubauwe commented 7 years ago

I think that all my dependencies are up to date. I tried this with a new ionic project. Here's my list of the dependencies of my project:

"dependencies": {
    "@angular/common": "4.1.0",
    "@angular/compiler": "4.1.0",
    "@angular/compiler-cli": "4.1.0",
    "@angular/core": "4.1.0",
    "@angular/forms": "4.1.0",
    "@angular/http": "4.1.0",
    "@angular/platform-browser": "4.1.0",
    "@angular/platform-browser-dynamic": "4.1.0",
    "@ionic-native/android-fingerprint-auth": "^3.10.2",
    "@ionic-native/core": "3.7.0",
    "@ionic-native/file": "^3.10.2",
    "@ionic-native/secure-storage": "^3.10.2",
    "@ionic-native/splash-screen": "3.7.0",
    "@ionic-native/status-bar": "3.7.0",
    "@ionic-native/touch-id": "^3.10.2",
    "@ionic/storage": "2.0.1",
    "@ng-idle/core": "^2.0.0-beta.11",
    "@types/pouchdb": "6.1.0",
    "cordova-plugin-compat": "^1.1.0",
    "cordova-plugin-console": "1.0.5",
    "cordova-plugin-device": "1.1.4",
    "cordova-plugin-file": "^4.3.3",
    "cordova-plugin-splashscreen": "~4.0.1",
    "cordova-plugin-sqlite-2": "^1.0.4",
    "cordova-plugin-statusbar": "2.2.2",
    "cordova-plugin-whitelist": "1.3.1",
    "crypto-pouch": "^3.1.2",
    "ionic-angular": "3.2.1",
    "ionic-plugin-keyboard": "~2.2.1",
    "ionicons": "3.0.0",
    "pouchdb": "^6.2.0",
    "pouchdb-adapter-cordova-sqlite": "^2.0.2",
    "pouchdb-replication-stream": "^1.2.9",
    "rxjs": "5.1.1",
    "sw-toolbox": "3.6.0",
    "uuid": "^3.0.1",
    "zone.js": "0.8.10"
},

Maybe you can see, if there's one out of date. Thanks!

tubauwe commented 7 years ago

Do you have any idea? My app isn't working anymore tue to this error. This is very frustrating at the moment.

I'm getting this error even if I start a new project.

tubauwe commented 7 years ago

I've installed nearly everything new. Tue to this, I think all dependencies are up to date.

I'm waiting now for quite a long time without any comment by you. I've tried a lot of variants without success. Can you help me, please? Do you have any idea? Thanks in advance!

calvinmetcalf commented 7 years ago

can you post some code to allow me to reproduce it? that's a pretty generic error you have

tubauwe commented 7 years ago

Thanks for your reply. Here is in short, what I've been trying the last days:

I started a new ionic project by

ionic start ionic2-pouchdb-test blank -v2 cd ionic2-pouchdb-test ionic cordova platform add android

To install SQLite Plugin I executed the following command

ionic plugin add cordova-plugin-sqlite-2

Next, I installed PouchDB, ...

npm install pouchdb pouchdb-adapter-cordova-sqlite crypto-pouch --save

I generated an provider with

ionic generate provider database

In this provider I use the following code

import { Injectable } from '@angular/core';
import * as PouchDB from 'pouchdb';
import cordovaSqlitePlugin from 'pouchdb-adapter-cordova-sqlite';
import CryptoPouch from 'crypto-pouch';

PouchDB.plugin(CryptoPouch);

@Injectable()
export class DatabaseProvider {
   private db;

   initDB() {
     PouchDB.plugin(cordovaSqlitePlugin);
     this.db = new PouchDB('test.db', { adapter: 'cordova-sqlite' });
     this.db.crypto('pa$$w0rd');
  }
}

After that I changed the code in app.component.ts to

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { HomePage } from '../pages/home/home';
import { DatabaseProvider } from '../providers/database/database';

@Component({
   templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, database: DatabaseProvider) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();

      database.initDB();
    });
  }
}

If I try the code above in browser - with websql not cordova-sqlite as pouchdb adapter - it's working, if I use Chrome. But if I use

ionic cordova run android --livereload

I'm getting the following error

main.js:1362 ERROR Error: Uncaught (in promise): TypeError: "size" argument must be a number
TypeError: "size" argument must be a number
    at assertSize (main.js:15392)
    at allocUnsafe (main.js:15423)
    at Function.Buffer.allocUnsafe (main.js:15437)
    at module.exports (main.js:80142)
    at main.js:122421
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (main.js:4427)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
    at assertSize (main.js:15392)
    at allocUnsafe (main.js:15423)
    at Function.Buffer.allocUnsafe (main.js:15437)
    at module.exports (main.js:80142)
    at main.js:122421
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (main.js:4427)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
    at c (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (main.js:4418)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)
    at invoke (polyfills.js:3)
    at n (polyfills.js:2)

Maybe this code can help you to find the error. I tried a lot the last weeks, but without success. I don't have any idea at the moment, how to solve this. Thanks in advance for your answer.

calvinmetcalf commented 7 years ago

I'm going to be honest I don't use ionic so I'm not sure I'll be able to set it up to test anytime soon, if you could tell me what the code at module.exports (main.js:80142) is that would be helpful

tubauwe commented 7 years ago

The wanted code is:

module.exports = function (password, salt, iterations, keylen, digest) {
  if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding)
  if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding)

  checkParameters(iterations, keylen)

  digest = digest || 'sha1'

  var hmac = new Hmac(digest, password, salt.length)

  var DK = Buffer.allocUnsafe(keylen)
  var block1 = Buffer.allocUnsafe(salt.length + 4)
  salt.copy(block1, 0, 0, salt.length)

  var U, j, destPos, len

  var hLen = hmac.size
  var T = Buffer.allocUnsafe(hLen)    <== This is line 80142
  var l = Math.ceil(keylen / hLen)
  var r = keylen - (l - 1) * hLen

  for (var i = 1; i <= l; i++) {
    block1.writeUInt32BE(i, salt.length)
    U = hmac.run(block1, hmac.ipad1)

    U.copy(T, 0, 0, hLen)

    for (j = 1; j < iterations; j++) {
      U = hmac.run(U, hmac.ipad2)
      for (var k = 0; k < hLen; k++) T[k] ^= U[k]
    }

    destPos = (i - 1) * hLen
    len = (i === l ? r : hLen)
    T.copy(DK, destPos, 0, len)
  }

  return DK
}

It would be great, if you can help me. Thanks in advance!

calvinmetcalf commented 7 years ago

sorry I think you misunderstood, what is the code I have to run to see the same error as you without having to install anything except from npm ?

tubauwe commented 7 years ago

To get the same error you have to install ionic and cordova:

npm install -g ionic cordova

Then following my description above (in my third comment to this issue) you can get this error if you are testing it on an android device.

Testing in browser (Safari or Firefox) by

ionic serve

I'm getting the same error, but not in Chrome.

tubauwe commented 7 years ago

Today I made another little update of ionic (3.4.0 -> 3.4.2) and run my project on my android device without option --livereload and it works! Great!

After trying a lot of changes, I don't know if I tried it without --livereload before this update. I think, I did...

Thank you very much for your help! But it seems not to be an issue of crypto pouch.

tubauwe commented 7 years ago

The problem is back: Without the --livereload option it's working on my android device, but if I'm trying it on an iOS device there's a similar error message.

Maybe you can see - better than me - what is meant by "size" and why this Exception being thrown? Is there a possibility to handle with? Can you help me, please? Thanks in advance!

garbados commented 3 years ago

Hi @tubauwe ! We just published version 4.0 which changes plenty of things. Can you try again and let me know how it goes? I can help you debug from there.

Since it's been four years, I'm closing the issue for now. I can re-open it at your request :)