Using @dfinity/agent in node.js

@anthonymq: How do you check for your “CLI” principal? IIRC you have to pass --no-wallet so that the call goes to the canister directly and not via your “wallet” canister.

dfx identity --network ic get-principal

yep that’s what I did :frowning:

Alright so I was about to post details and everything but cannot therefore I just gonna say that I face the exact same issue.

When I compare a caller principal in a query called from a NodeJS script, in which I used my private key, with the self fromActor principal, I get an error respectively both principals are not the same.

1 Like
const initIdentity = () => {
  const buffer = readFileSync('/Users/daviddalbusco/.config/dfx/identity/default/identity.pem');
  const key = buffer.toString('utf-8');

  const privateKey = crypto.createHash('sha256').update(key).digest('base64');

  return Secp256k1KeyIdentity.fromSecretKey(Buffer.from(privateKey, 'base64'));

dfx identity --network ic get-principal leads to w2bwq-cyvg6...

in nodejs

const identity = initIdentity();


I get another value → cd5mb-c6i5a....

Are you sure your pem file is corresponding to a Secp256k1 key? and not Ed25519?

I would say probably or yes because if I use the Ed25519KeyIdentity provider of agent-js instead of the Secp256k1KeyIdentity with the exact same code I get Error: bad secret key size

1 Like

Yep same error for me.