question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BigNumber encoding hex result doesn't match test cases

See original GitHub issue

I haven’t been able to recreate this encoding test case. Is there something I’m doing wrong?

const cbor = require('cbor')
const { BigNumber } = require('bignumber.js')

// This doesn't match the test case here: https://github.com/hildjj/node-cbor/blob/466d01b25959ab1270a409e31453f53b5bba082d/test/cases.js#L695
const num = new BigNumber('9223372036854775807')
console.log(cbor.encode(num).toString('hex')) // c2487fffffffffffffff

// This matches the test case here: https://github.com/hildjj/node-cbor/blob/466d01b25959ab1270a409e31453f53b5bba082d/test/cases.js#L80
const num2 = new BigNumber('18446744073709551616')
console.log(cbor.encode(num2).toString('hex')) // c249010000000000000000

Here’s a repl.it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hildjjcommented, Apr 4, 2020

Fixed in version 5.0.2. Use the collapseBigIntegers option.

console.log(cbor.encodeOne(0x7fffffffffffffffn, {
  collapseBigIntegers: true
}).toString('hex'))

generates: 1b7fffffffffffffff

1reaction
hildjjcommented, Mar 26, 2020

I guess you’re asking for BigNumber integers and ECMAscript bigints to be encoded as normal integers, if they would fit. I’m open to adding an option for that, but since it’s a breaking change as well as breaking round-tripping, I don’t want to make it the default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

BigNumber issue with the AbiCoder. #418 - ethers-io/ethers.js
To solve this, keep in mind to use strings (decimal or 0x prefixed hex) and do all math using big numbers. Here is...
Read more >
Convert hex string to int - java - Stack Overflow
The test case I need to satisfy is essentially to Convert "AA0F245C" to int and then back to that string so that I...
Read more >
Numerical constants written in hex should allow overflow ...
Let me sum up some observations about this problem and the current state of affairs. 1. The problem with hex int is not...
Read more >
Documentation - ethers
The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem. It was originally...
Read more >
math/big - Go Packages
(By always passing in a result value via the receiver, memory use can be much ... that correspond to normal (i.e., not denormal)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found