简体   繁体   中英

Is there a schema to store this object with typeof, Integer { value: 340895965n } in mongoose with node.js

I'm very new to nodejs. For a while I've been working with paillier-js module which returns a object typeof() of this nature: Integer { value: 340895965n }. Is there a schema that can allow for appropriate storage of this Object with mongoose. I have tried passing the Object directly to mongodb which is successful but when I view it using the MongoDB compass, I get empty { }

const paillier = require('paillier-js');
const bigInt = require('big-integer');


const { publicKey, privateKey } = paillier.generateRandomKeys(16);


//Set the values for the public key for the encryption
publicKey.n.value =  55687n
publicKey._n2.value = 3101041969n;
publicKey.g.value = 1696460192n;

//Set the values for the privateKey for the decryption
privateKey.lambda.value =  27608n;
privateKey.mu.value = 11489n;
privateKey._p.value = 239n;
privateKey._q.value = 233n;
privateKey.publicKey = publicKey


let num1 = 101;
let num2 = 104;
let num3 = 1;

let bn1 = bigInt(num1).mod(publicKey.n);

while (bn1.lt(0)) bn1 = bn1.add(publicKey.n); 
let bn2 = bigInt(num2).mod(publicKey.n);
while (bn2.lt(0)) bn2 = bn2.add(publicKey.n); 
let bn3 = bigInt(num3).mod(publicKey.n);
while (bn3.lt(0)) bn3 = bn3.add(publicKey.n);


let c1 = publicKey.encrypt(bn1);
console.log("C1: "+ c1);
console.log(c1, "is of type ", typeof(c1))

//c1 = c1.value


let c2 = publicKey.encrypt(bn2);
console.log("C2: ",c2);
//c2 = c2.value

let c3 = publicKey.encrypt(bn3);
console.log("C3: "+c3);
//c3 = c3.value

console.log(typeof(c1))

let encryptedSum = publicKey.addition(c1, c2, c3);
let decryptedSum = privateKey.decrypt(encryptedSum);

console.log('Decrypted addition:', decryptedSum.toString());

Here is what I did. I used toString() on the class Object, which I stored in mongodb. Then when I retrieved it, I use BigInt() on the String and then, I create a prototype of the class object and then set it's value to the BigInt() value. An it worked...

//c1 is the class object
rc1 = c1.toString() 


// then rc1 is stored as String in mongodb

rc1 = BigInt(rc1)   //rc1 is retrieved from db and converted to BigInt

oc1 = Object.create(c2)  //An Object prototype was created  from c2 (an object of the class)
oc1.value = rc1;  //value of the Object prototype was then set to the result of BigInt()

Not familiar with MongoDB, but you can use toString() on a BigInt, converting it to a string before storing in MongoDB, and then of course when reading it from MongoDB, you'll need to reapply BigInt . Ie,

x = 14142352423459034590345934959032n;

// convert x to string...
xs = x.toString();

// convert string to BigInt
bix = BigInt(xs);

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM