indutny / bn.js

BigNum in pure javascript
MIT License
1.19k stars 150 forks source link

Converting json.stringified BN back into BN #283

Closed 0xCactus closed 2 years ago

0xCactus commented 2 years ago

Applied JSON.stringify on BN which gave me "{"_bn":{"negative":0,"words":[22127744,7770787,46556922,61120705,33815569,32632618,19307227,5865125,41554856,1514719,0],"length":10,"red":null}}". Any idea how I may convert this back into an instance of BN?

fanatid commented 2 years ago

You need serialize BN as String in JSON.stringify and create a new BN from String in JSON.parse.

0xCactus commented 2 years ago

You serialize BN as String in JSON.stringify and create a new BN from String in JSON.parse.

@fanatid JSON.parse converts it to an object of {"_bn":{"negative":0,"words":[22127744,7770787,46556922,61120705,33815569,32632618,19307227,5865125,41554856,1514719,0],"length":10,"red":null}} without the BN class method. I tried passing the object into new BN(object) but the BN created is off from the original BN