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

fix: peer-id privKey and pubKey can be undefined #126

Merged
merged 5 commits into from
Jan 2, 2022
Merged
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
31 changes: 11 additions & 20 deletions packages/compliance-tests/src/peer-id/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,6 @@ module.exports = (common) => {
const peerId2 = factory.createFromB58String(peerId.toB58String())

expect(peerId1).to.deep.equal(peerId2)

peerId1.toString()

expect(peerId1).to.deep.equal(peerId2)
})

describe('throws on inconsistent data', () => {
Expand All @@ -382,29 +378,24 @@ module.exports = (common) => {

it('missmatch private - public key', async () => {
const digest = await k1.public.hash()
expect(() => {
factory.createFromJSON({
id: digest,
pubKey: k1,
privKey: k2.public
}) // eslint-disable-line no-new
}).to.throw(/inconsistent arguments/)
await expect(factory.createFromJSON({
id: digest,
pubKey: k1,
privKey: k2.public
})).eventually.be.rejectedWith(/inconsistent arguments/)
})

it('missmatch id - private - public key', async () => {
const digest = await k1.public.hash()
expect(() => {
factory.createFromJSON({
id: digest,
pubKey: k1,
privKey: k3.public
}) // eslint-disable-line no-new
}).to.throw(/inconsistent arguments/)
await expect(factory.createFromJSON({
id: digest,
pubKey: k1,
privKey: k3.public
})).eventually.be.rejectedWith(/inconsistent arguments/)
})

it('invalid id', () => {
// @ts-expect-error incorrect constructor arg type
expect(() => factory.createFromJSON('hello world')).to.throw(/invalid id/)
await expect(factory.createFromJSON('hello world')).eventually.be.rejectedWith(/invalid id/)
})
})
})
Expand Down
17 changes: 11 additions & 6 deletions packages/interfaces/src/peer-id/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import type { CID } from 'multiformats/cid'
import type { PublicKey, PrivateKey, KeyType } from '../keys/types'

interface PeerIdJSON {
export interface PeerIdJSON {
readonly id: string;
readonly pubKey?: string;
readonly privKey?: string;
}

interface CreateOptions {
export interface CreateOptions {
bits?: number;
keyType?: KeyType;
}

export interface PeerId {
readonly id: Uint8Array;
privKey: PrivateKey;
pubKey: PublicKey;
readonly id: Uint8Array
privKey: PrivateKey | undefined;
pubKey: PublicKey | undefined;

/**
* Return the protobuf version of the public key, matching go ipfs formatting
*/
marshalPubKey ():Uint8Array;
marshalPubKey: () => Uint8Array | undefined;

/**
* Return the protobuf version of the private key, matching go ipfs formatting
Expand Down Expand Up @@ -85,6 +85,11 @@ export interface PeerId {
}

export interface PeerIdFactory {
/**
* Create a new PeerId.
**/
new (id: Uint8Array, privKey?: PrivateKey, pubKey?: PublicKey): PeerId;

/**
* Create a new PeerId.
**/
Expand Down