Skip to content

Commit

Permalink
Fix Buffer.from usage with hexadecimal strings
Browse files Browse the repository at this point in the history
`Buffer.from` does not support `0x` prefix in hexadecimal strings.
In Node CLI:

    > Buffer.from("0x04", "hex")
    <Buffer >
    > Buffer.from("0x04", "hex").length
    0

Use `"04"` without the prefix to really add byte `04` to the return value of `Point.encode`.
  • Loading branch information
niooss-ledger authored Sep 14, 2023
1 parent 77efd29 commit df83b29
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/Point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Point {
encode(enc: string): Buffer {
switch (enc) {
case "arr":
return Buffer.concat([Buffer.from("0x04", "hex"), Buffer.from(this.x.toString("hex"), "hex"), Buffer.from(this.y.toString("hex"), "hex")]);
return Buffer.concat([Buffer.from("04", "hex"), Buffer.from(this.x.toString("hex"), "hex"), Buffer.from(this.y.toString("hex"), "hex")]);
case "elliptic-compressed": {
const key = this.ecCurve.keyFromPublic({ x: this.x.toString("hex", 64), y: this.y.toString("hex", 64) }, "hex");
return Buffer.from(key.getPublic(true, "hex"));
Expand Down

0 comments on commit df83b29

Please sign in to comment.