Skip to content

Need support bigint #240

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

Closed
baryon opened this issue Jan 30, 2024 · 4 comments
Closed

Need support bigint #240

baryon opened this issue Jan 30, 2024 · 4 comments

Comments

@baryon
Copy link

baryon commented Jan 30, 2024

When I set a number is 2100n * 10000n * 10n ** 8n, got an error

Error: Unrecognized object: [object BigInt]
    at Encoder.encodeObject (~/@msgpack/msgpack/src/Encoder.ts:307:13)

@baryon
Copy link
Author

baryon commented Jan 31, 2024

https://gist.github.com/baryon/6a0f97fc55a39b826d6e3d40166afa59

I noticed that bigint can be interpreted as int64. I have another proposal, what does everyone think? That is to use the hex of bigint, and use a byte array to represent bigint. Of course, we also need to consider negative numbers. I wrote code and test code to perform the conversion between bigint and buffer. Is there anyone who can review and integrate it into this codebase? I'm not quite clear on which format should be used in msgpack to represent it.

@baryon
Copy link
Author

baryon commented Feb 8, 2024

https://no2chem.github.io/bigint-buffer/

a better lib than my code, it support Big/Little ending

@wangxm345566462
Copy link

Add bigint

private encodeObject(object: unknown, depth: number) {
    // try to encode objects with custom codec first of non-primitives
    const ext = this.extensionCodec.tryToEncode(object, this.context);
    if (ext != null) {
      this.encodeExtension(ext);
    } else if (Array.isArray(object)) {
      this.encodeArray(object, depth);
    } else if (ArrayBuffer.isView(object)) {
      this.encodeBinary(object);
    } else if (typeof object === "object") {
      this.encodeMap(object as Record<string, unknown>, depth);
    } else if(typeof object === "bigint"){
        this.encodeBigInt64(object);
    } else {
      // symbol, function and other special object come here unless extensionCodec handles them.
      throw new Error(`Unrecognized object: ${Object.prototype.toString.apply(object)}`);
    }
  }

@baryon
Copy link
Author

baryon commented Jul 1, 2024

I am using useBigInt64 flag.

https://github.com/NoteProtocol/NoteWallet/blob/cf7778e09606cf6742798129ed5276008648fe15/src/wallet.ts#L242C4-L245C8

const encodedData = msgpack.encode(data, {
      sortKeys: true,
      useBigInt64: true,
    });

@baryon baryon closed this as completed Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants