(posted here as per requested in https://github.com/ipfs/go-ipfs/issues/7715)
I’m using IPFS 0.7.0 with the shiny new feature to export keys.
So i do:
ipfs key gen test ipfs key export test
Which gives me a “test.key” in binary libp2p protobuf format.
What i try to do now is get that key working in nodejs crypto.
This is where i’m hitting some snags.
In node.js i need to import the key as a PEM key to be able to import and use it. This means it needs to be in this exact format:
-----BEGIN PRIVATE KEY----- <THE KEY> -----END PRIVATE KEY-----
Begin and end are easy
The key needs to be in base64.
const fs = require("fs") const crypto = require('crypto'); const Schema = require("./crypto_pb") let data = fs.readFileSync("./test.key") let deserializedData = Schema.PrivateKey.deserializeBinary(data) let keyBuffer = deserializedData.getData(); console.log(Buffer.from(keyBuffer).toString('base64'))
Note that very last console.log line. There i’m printing the key, which results in:
That’s too long.
keyBuffer object is (i verified that) 64 bytes long. So it does seem to match what the libp2p spec tells.
The base64 result is however 88 characters which doesn’t seem to be right. Importing it as that gives me an error of:
header too long.
I’m sure i’m missing a conversion somewhere. But where and from what to what is not clear to me.
Thank a lot!
I changed the title from
How to convert an exported key to base64 in nodejs? to
[solved] How to use the IPFS self public and private key in node.js? because i essentially want to use the IPFS self key in node.js to add functionality. I want to sign a hash with the node’s private key which anyone else can then verify if they have the node’s peer id (which is also it’s public key as of ipfs 0.7.0).