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

Commit e06658d

Browse files
fix: peer-id privKey and pubKey can be undefined (#126)
Also export CreateOptions and PeerIdJSON types Co-authored-by: Alex Potsides <[email protected]>
1 parent 0401310 commit e06658d

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

packages/compliance-tests/src/peer-id/index.js

+11-20
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,6 @@ module.exports = (common) => {
357357
const peerId2 = factory.createFromB58String(peerId.toB58String())
358358

359359
expect(peerId1).to.deep.equal(peerId2)
360-
361-
peerId1.toString()
362-
363-
expect(peerId1).to.deep.equal(peerId2)
364360
})
365361

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

383379
it('missmatch private - public key', async () => {
384380
const digest = await k1.public.hash()
385-
expect(() => {
386-
factory.createFromJSON({
387-
id: digest,
388-
pubKey: k1,
389-
privKey: k2.public
390-
}) // eslint-disable-line no-new
391-
}).to.throw(/inconsistent arguments/)
381+
await expect(factory.createFromJSON({
382+
id: digest,
383+
pubKey: k1,
384+
privKey: k2.public
385+
})).eventually.be.rejectedWith(/inconsistent arguments/)
392386
})
393387

394388
it('missmatch id - private - public key', async () => {
395389
const digest = await k1.public.hash()
396-
expect(() => {
397-
factory.createFromJSON({
398-
id: digest,
399-
pubKey: k1,
400-
privKey: k3.public
401-
}) // eslint-disable-line no-new
402-
}).to.throw(/inconsistent arguments/)
390+
await expect(factory.createFromJSON({
391+
id: digest,
392+
pubKey: k1,
393+
privKey: k3.public
394+
})).eventually.be.rejectedWith(/inconsistent arguments/)
403395
})
404396

405397
it('invalid id', () => {
406-
// @ts-expect-error incorrect constructor arg type
407-
expect(() => factory.createFromJSON('hello world')).to.throw(/invalid id/)
398+
await expect(factory.createFromJSON('hello world')).eventually.be.rejectedWith(/invalid id/)
408399
})
409400
})
410401
})

packages/interfaces/src/peer-id/types.d.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import type { CID } from 'multiformats/cid'
22
import type { PublicKey, PrivateKey, KeyType } from '../keys/types'
33

4-
interface PeerIdJSON {
4+
export interface PeerIdJSON {
55
readonly id: string;
66
readonly pubKey?: string;
77
readonly privKey?: string;
88
}
99

10-
interface CreateOptions {
10+
export interface CreateOptions {
1111
bits?: number;
1212
keyType?: KeyType;
1313
}
1414

1515
export interface PeerId {
16-
readonly id: Uint8Array;
17-
privKey: PrivateKey;
18-
pubKey: PublicKey;
16+
readonly id: Uint8Array
17+
privKey: PrivateKey | undefined;
18+
pubKey: PublicKey | undefined;
1919

2020
/**
2121
* Return the protobuf version of the public key, matching go ipfs formatting
2222
*/
23-
marshalPubKey ():Uint8Array;
23+
marshalPubKey: () => Uint8Array | undefined;
2424

2525
/**
2626
* Return the protobuf version of the private key, matching go ipfs formatting
@@ -85,6 +85,11 @@ export interface PeerId {
8585
}
8686

8787
export interface PeerIdFactory {
88+
/**
89+
* Create a new PeerId.
90+
**/
91+
new (id: Uint8Array, privKey?: PrivateKey, pubKey?: PublicKey): PeerId;
92+
8893
/**
8994
* Create a new PeerId.
9095
**/

0 commit comments

Comments
 (0)