Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

feat: add exportToJSON #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/libp2p-peer-id-factory/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateKeyPair, marshalPrivateKey, unmarshalPrivateKey, marshalPublicKey, unmarshalPublicKey } from '@libp2p/crypto/keys'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { peerIdFromKeys, peerIdFromBytes } from '@libp2p/peer-id'
import { PeerIdProto } from './proto.js'
import type { PublicKey, PrivateKey } from '@libp2p/interface-keys'
Expand Down Expand Up @@ -68,6 +69,14 @@ export async function createFromProtobuf (buf: Uint8Array) {
)
}

export function exportToJSON (peerId: RSAPeerId | Ed25519PeerId | Secp256k1PeerId, excludePrivateKey?: boolean) {
return {
id: uint8ArrayToString(peerId.toBytes(), 'base58btc'),
pubKey: peerId.publicKey != null ? uint8ArrayToString(peerId.publicKey, 'base64pad') : undefined,
privKey: excludePrivateKey === true || peerId.privateKey == null ? undefined : uint8ArrayToString(peerId.privateKey, 'base64pad')
}
}

export async function createFromJSON (obj: { id: string, privKey?: string, pubKey?: string }) {
return await createFromParts(
uint8ArrayFromString(obj.id, 'base58btc'),
Expand Down
22 changes: 21 additions & 1 deletion packages/libp2p-peer-id-factory/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ describe('PeerId', () => {
expect(ids[0].equals(ids[1].multihash.bytes)).to.equal(false)
})

describe('fromJSON', () => {
describe('fromJSON / toJSON', () => {
it('full node', async () => {
const id = await PeerIdFactory.createEd25519PeerId()
// create manually
const other = await PeerIdFactory.createFromJSON({
id: id.toString(),
privKey: id.privateKey != null ? uint8ArrayToString(id.privateKey, 'base64pad') : undefined,
Expand All @@ -254,6 +255,12 @@ describe('PeerId', () => {
expect(id.toString()).to.equal(other.toString())
expect(id.privateKey).to.equalBytes(other.privateKey)
expect(id.publicKey).to.equalBytes(other.publicKey)

// create with export
const otherJsonId = await PeerIdFactory.createFromJSON(PeerIdFactory.exportToJSON(id))
expect(other.toString()).to.equal(otherJsonId.toString())
expect(other.privateKey).to.equalBytes(otherJsonId.privateKey)
expect(other.publicKey).to.equalBytes(otherJsonId.publicKey)
})

it('only id', async () => {
Expand All @@ -273,6 +280,19 @@ describe('PeerId', () => {
it('go interop', async () => {
const id = await PeerIdFactory.createFromJSON(goId)
expect(id.toString()).to.eql(goId.id)

const jsonId = PeerIdFactory.exportToJSON(id)
expect(jsonId.id).to.deep.equal(goId.id)
expect(jsonId.privKey).to.deep.equal(goId.privKey)
})

it('export without private key', async () => {
const id = await PeerIdFactory.createEd25519PeerId()
const other = await PeerIdFactory.createFromJSON(PeerIdFactory.exportToJSON(id, true))

expect(other.toString()).to.equal(id.toString())
expect(other.privateKey).to.be.undefined()
expect(other.publicKey).to.equalBytes(id.publicKey)
})
})

Expand Down