Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use msgpack fork that supports bigints. #229

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -51,6 +51,18 @@ describe('encoding', function () {
assert.notStrictEqual(encoding.encode(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