Skip to content

Commit

Permalink
Use msgpack fork that supports bigints. (#229)
Browse files Browse the repository at this point in the history
A bigint is used only when the decoded number cannot fit in to a Number.

Resolves: #130
  • Loading branch information
EvanJRichard authored and jasonpaulos committed Oct 14, 2020
1 parent 676a508 commit 10da502
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
17 changes: 10 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"url": "git://github.com/algorand/js-algorand-sdk.git"
},
"dependencies": {
"@msgpack/msgpack": "^1.9.0",
"@msgpack/msgpack": "^1.12.2",
"algo-msgpack-with-bigint": "^2.1.0",
"hi-base32": "^0.5.0",
"js-sha256": "^0.9.0",
"js-sha512": "^0.8.0",
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 5. Binary blob should be used for binary data and string for strings
* */

const msgpack = require("@msgpack/msgpack");
const msgpack = require("algo-msgpack-with-bigint");

// Errors
const ERROR_CONTAINS_EMPTY_STRING = "The object contains empty or 0 values. First empty or 0 value encountered during encoding: ";
Expand Down
12 changes: 12 additions & 0 deletions tests/2.Encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ describe('encoding', function () {
assert.notStrictEqual(algosdk.encodeObj(o), golden);
});

it('should safely encode/decode bigints', function () {
let beforeZero = BigInt("0")
let afterZero = encoding.decode(encoding.encode(beforeZero));
assert.ok(beforeZero == afterZero); //after is a Number because 0 fits into a Number - so we do this loose comparison
let beforeLarge = BigInt("18446744073709551612"); // larger than a Number, but fits into a uint64
let afterLarge = encoding.decode(encoding.encode(beforeLarge));
assert.strictEqual(beforeLarge, afterLarge);
let beforeTooLarge = BigInt("18446744073709551616") // larger than even fits into a uint64. we do not want to work with these too-large numbers
let afterTooLarge = encoding.decode(encoding.encode(beforeTooLarge));
assert.notStrictEqual(beforeTooLarge, afterTooLarge)
});

it('should match our go code', function () {
let golden = new Uint8Array([134, 163, 97, 109, 116, 205, 3, 79, 163, 102, 101, 101, 10, 162, 102, 118, 51, 162, 108, 118, 61, 163, 114, 99, 118, 196, 32, 145, 154, 160, 178, 192, 112, 147, 3, 73, 200, 52, 23, 24, 49, 180, 79, 91, 78, 35, 190, 125, 207, 231, 37, 41, 131, 96, 252, 244, 221, 54, 208, 163, 115, 110, 100, 196, 32, 145, 154, 160, 178, 192, 112, 147, 3, 73, 200, 52, 23, 24, 49, 180, 79, 91, 78, 35, 190, 125, 207, 231, 37, 41, 131, 96, 252, 244, 221, 54, 208]);
let ad = "SGNKBMWAOCJQGSOIGQLRQMNUJ5NU4I56PXH6OJJJQNQPZ5G5G3IOVLI5VM";
Expand Down

0 comments on commit 10da502

Please sign in to comment.