Error reading in an Ed25519KeyIdentity from pem file after bumping agentJS from 0.15.3 to 1.1.1
ByronBecker opened this issue · comments
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:
- Generate an Ed25519KeyIdentity with dfx (old version of dfx, not exactly sure which one 🤷 )
- 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 - Run the script with ts-node, it should succeed
- 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
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