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

Commit

Permalink
feat: add exportToJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Jun 27, 2022
1 parent ba37d81 commit b87aa93
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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 && peerId.privateKey != null ? uint8ArrayToString(peerId.privateKey, 'base64pad') : undefined
}
}

export async function createFromJSON (obj: { id: string, privKey?: string, pubKey?: string }) {
return await createFromParts(
uint8ArrayFromString(obj.id, 'base58btc'),
Expand Down
21 changes: 20 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,18 @@ 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).to.deep.equal(goId)
})

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

0 comments on commit b87aa93

Please sign in to comment.