Skip to content

Commit

Permalink
Merge pull request #7728 from Zondax/bugfix/casper
Browse files Browse the repository at this point in the history
bugfix(casper): adjust address from public key
  • Loading branch information
hedi-edelbloute authored Sep 6, 2024
2 parents 9c55e81 + 9635bb8 commit 46006ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-chairs-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

adjust the adrres when it is obtained from the public key in Casper Blockchain
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Account } from "@ledgerhq/types-live";
import { CLPublicKey, CLPublicKeyTag } from "casper-js-sdk";
import { blake2bFinal, blake2bInit, blake2bUpdate } from "blakejs";
import { CLPublicKey, CLPublicKeyTag } from "casper-js-sdk";
import { CASPER_CHECKSUM_HEX_LEN } from "../../consts";

export const getAddress = (
Expand Down Expand Up @@ -40,7 +40,7 @@ export function casperAccountHashFromPublicKey(
}

export function casperAddressFromPubKey(pubkey: Buffer, keySig: CLPublicKeyTag): string {
return `${keySig.toString()}${pubkey}`;
return `${keySig.toString().padStart(2, "0")}${Buffer.from(pubkey).toString("hex")}`;
}

export function getCLPublicKey(address: string): CLPublicKey {
Expand Down
46 changes: 45 additions & 1 deletion libs/ledger-live-common/src/families/casper/utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isAddressValid } from "./bridge/bridgeHelpers/addresses";
import { CLPublicKeyTag } from "casper-js-sdk";
import { casperAddressFromPubKey, isAddressValid } from "./bridge/bridgeHelpers/addresses";

describe("Casper addresses", () => {
const pubKeys = {
Expand Down Expand Up @@ -28,4 +29,47 @@ describe("Casper addresses", () => {
expect(isAddressValid(pubKeys.invalidCharacter)).toBe(false);
expect(isAddressValid(pubKeys.invalidChecksum)).toBe(false);
});

test("Get the address from the publick key", () => {
/**
* Extracts the public key and key signature from a given Casper address.
*
* @param {string} address - The Casper address from which to extract the public key and key signature.
* @returns {{ pubkey: Buffer, keySig: CLPublicKeyTag }} An object containing the public key as a Buffer and the key signature as a CLPublicKeyTag.
*/
function casperPubKeyFromAddress(address: string): {
pubkey: Buffer;
keySig: CLPublicKeyTag;
} {
const keySig = parseInt(address.slice(0, 2), 10) as CLPublicKeyTag;
const pubkeyHex = address.slice(2);
const pubkey = Buffer.from(pubkeyHex, "hex");
return { pubkey, keySig };
}

expect(
casperAddressFromPubKey(
casperPubKeyFromAddress(pubKeys.validSecp256k1).pubkey,
CLPublicKeyTag.SECP256K1,
),
).toBe(pubKeys.validSecp256k1);
expect(
casperAddressFromPubKey(
casperPubKeyFromAddress(pubKeys.validSecp256k1Checksum).pubkey,
CLPublicKeyTag.SECP256K1,
),
).toBe(pubKeys.validSecp256k1Checksum.toLowerCase());
expect(
casperAddressFromPubKey(
casperPubKeyFromAddress(pubKeys.validEd25519).pubkey,
CLPublicKeyTag.ED25519,
),
).toBe(pubKeys.validEd25519);
expect(
casperAddressFromPubKey(
casperPubKeyFromAddress(pubKeys.validEd25519Checksum).pubkey,
CLPublicKeyTag.ED25519,
),
).toBe(pubKeys.validEd25519Checksum.toLowerCase());
});
});

0 comments on commit 46006ef

Please sign in to comment.