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

Error reading in an Ed25519KeyIdentity from pem file after bumping agentJS from 0.15.3 to 1.1.1 #870

Open
ByronBecker opened this issue Apr 2, 2024 · 1 comment

Comments

@ByronBecker
Copy link

Describe the bug
Previously, in a NodeJS enviroment calling Ed25519KeyIdentity.fromSecretKey() with a key generated from dfx worked.

After bumping agentJS from ^0.15.3 to ^1.1.1, I now receive the following error.

<path_to_my_project>/node_modules/@noble/curves/src/abstract/utils.ts:134
    throw new Error(`${title} expected ${expectedLength} bytes, got ${len}`);
          ^
Error: private key expected 32 bytes, got 64
    at ensureBytes (<path_to_my_project>/node_modules/@noble/curves/src/abstract/utils.ts:134:11)
    at getExtendedPublicKey (<path_to_my_project>node_modules/@noble/curves/src/abstract/edwards.ts:421:22)
    at Object.getPublicKey (<path_to_my_project>/node_modules/@noble/curves/src/abstract/edwards.ts:435:12)
    at Function.fromSecretKey (<path_to_my_project>/node_modules/@dfinity/identity/src/identity/ed25519.ts:160:31)
    at identityFromPemFile (<path_to_my_project>/scripts/utils.ts:12:29)
    ...

Where the code for forming the identity from the pem file looks like this:

// utils.ts
import { Ed25519KeyIdentity } from "@dfinity/identity";
import { readFileSync } from "fs-extra";
const pemFile = require("pem-file");

export function identityFromPemFile(pemFilePath: string): Ed25519KeyIdentity {
  const rawKey = readFileSync(pemFilePath).toString();
  const buf = pemFile.decode(rawKey);
  if (buf.length !== 85) {
    throw new Error(`expecting byte length 85 but got ${buf.length}`);
  }
  const secretKey = Buffer.concat([buf.slice(16, 48), buf.slice(53, 85)]);
  return Ed25519KeyIdentity.fromSecretKey(secretKey);
}

// Example usage
const identity = identityFromPemFile(<path_to_your_key);

I see that in the 0.20.0 Release Notes you have

this change replaces the package tweetnacl with @noble/curves for Ed25519 curve signatures and validation.

To Reproduce
Steps to reproduce the behavior:

  1. Generate an Ed25519KeyIdentity with dfx (old version of dfx, not exactly sure which one 🤷 )
  2. Set up a TypeScript NodeJS Project with agent-js 0.15.3 installed and the script from utils.ts above, with your identity file path subbed in
  3. Run the script with ts-node, it should succeed
  4. Bump agent-js to ^1.1.1, and run the script again. You should see the error message described above.

Expected behavior
Ed25519KeyIdentity.fromSecretKey() should not behave differently between 0.15.3 and agent-js >= 0.20.0

Desktop (please complete the following information):

  • OS: OSX
  • Node v18.12.1
@ByronBecker ByronBecker changed the title Issues reading in an Ed25519KeyIdentity from pem file after bumping agentJS from 0.15.3 to 1.1.1 Error reading in an Ed25519KeyIdentity from pem file after bumping agentJS from 0.15.3 to 1.1.1 Apr 2, 2024
@ByronBecker
Copy link
Author

@krpeacock

For anyone else that runs into this issue, this solution fixed the error for me.

https://forum.dfinity.org/t/using-dfinity-agent-in-node-js/6169/69

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

1 participant