# Create pem file from Ed25519

**URL:** <https://forum.dfinity.org/t/create-pem-file-from-ed25519/17708>\
**Category:** Developers\
**Created:** [December 29, 2022, 7:30pm UTC](https://forum.dfinity.org/t/create-pem-file-from-ed25519/17708 "2022-12-29T19:30:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexshelkov](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alexshelkov/32/8848_2.png) [@alexshelkov](https://forum.dfinity.org/u/alexshelkov)\
**Post date:** [December 29, 2022, 7:30pm UTC](https://forum.dfinity.org/t/create-pem-file-from-ed25519/17708/1 "2022-12-29T19:30:21Z")

</div>

So I have an Ed25519KeyIdentity, I actually used a PEM file to import it, from `dfx identity export default`. And create it in Node JS.

```javascript
const identity = X // << some magic to get it from PEM file: .config/dfx/identity/default/identity.pem >>

i.getPrincipal().toText() // is matching with dfx identity get-principal

```

But how I can get the PEM file from `identity`? I need some function which can save PEM file given the Ed25519KeyIdentity. But all the methods I tried failed, and the file don’t look the same as original PEM.

---

<div class="post-metadata">

**Author:** ![inviscidpixels](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/inviscidpixels/32/9021_2.png) [@inviscidpixels](https://forum.dfinity.org/u/inviscidpixels)\
**Post date:** [January 2, 2023, 5:51pm UTC](https://forum.dfinity.org/t/create-pem-file-from-ed25519/17708/2 "2023-01-02T17:51:50Z")

</div>

You might check here: [Using @dfinity/agent in node.js - #55 by ZenVoich](https://forum.dfinity.org/t/using-dfinity-agent-in-node-js/6169/55)

---

<div class="post-metadata">

**Author:** ![alexshelkov](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alexshelkov/32/8848_2.png) [@alexshelkov](https://forum.dfinity.org/u/alexshelkov)\
**Post date:** [January 3, 2023, 12:14pm UTC](https://forum.dfinity.org/t/create-pem-file-from-ed25519/17708/3 "2023-01-03T12:14:48Z")

</div>

So I was able to figure it out:

```javascript
const DFX_PEM_BEGIN = '-----BEGIN PRIVATE KEY-----';
const DFX_PEM_END = '-----END PRIVATE KEY-----';

export const createPem = (
  identity: Ed25519KeyIdentity
): string => {
  const [publicKey, privateKey] = identity.toJSON();

  // From Dfinity ic:
  // https://github.com/dfinity/ic/blob/master/rs/crypto/utils/basic_sig/src/conversions.rs#L117

  // prettier-ignore
  const der = Buffer.concat([
    Buffer.from([
      0x30, 83, // A sequence of 83 bytes follows.
      2, 1, // An integer denoting version
      1, // 0 if secret key only, 1 if public key is also present
      48, 5, // An element of 5 bytes follows
      6, 3, 43, 101, 112, // The OID
      4, 34, // An octet string of 34 bytes follows.
      4, 32, // An octet string
    ]),
    Buffer.from(privateKey.slice(0, 64), 'hex'),
    Buffer.from([
      161, 35, // An explicitly tagged with 35 bytes.
      3, 33, // A bitstring of 33 bytes follows.
      0, // The bitstring (32 bytes) is divisible by 8
    ]),
    Buffer.from(privateKey.slice(64), 'hex')
  ]);

  return `${DFX_PEM_BEGIN}\n${der.toString('base64')}\n${DFX_PEM_END}`;
}

```
