Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: has inline public key method #132

Merged
merged 1 commit into from
Sep 23, 2020
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
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ declare class PeerId {
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
isValid(): boolean;

/**
* Check if the PeerId has an inline public key.
*/
hasInlinePublicKey(): boolean;
}

export = PeerId;
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ class PeerId {
this.pubKey.bytes instanceof Uint8Array &&
uint8ArrayEquals(this.privKey.public.bytes, this.pubKey.bytes))
}

/**
* Check if the PeerId has an inline public key.
* @returns {boolean}
*/
hasInlinePublicKey () {
try {
const decoded = mh.decode(this.id)
if (decoded.name === 'identity') {
return true
}
} catch (_) {
// Ignore, there is no valid public key
}

return false
}
}

const PeerIdWithIs = withIs(PeerId, {
Expand Down
12 changes: 12 additions & 0 deletions test/peer-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ describe('PeerId', () => {
expect(ids[0].equals(ids[1].id)).to.equal(false)
})

describe('hasInlinePublicKey', () => {
it('returns true if uses a key type with inline public key', async () => {
const peerId = await PeerId.create({ keyType: 'secp256k1' })
expect(peerId.hasInlinePublicKey()).to.equal(true)
})

it('returns false if uses a key type with no inline public key', async () => {
const peerId = await PeerId.create({ keyType: 'RSA' })
expect(peerId.hasInlinePublicKey()).to.equal(false)
})
})

describe('fromJSON', () => {
it('full node', async () => {
const id = await PeerId.create(testOpts)
Expand Down