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

fix: add public/private key type disambiguators #2698

Merged
merged 1 commit into from
Sep 12, 2024
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
15 changes: 15 additions & 0 deletions packages/crypto/test/keys/ed25519.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env mocha */
import { isPrivateKey, isPublicKey } from '@libp2p/interface'
import { expect } from 'aegir/chai'
import { Uint8ArrayList } from 'uint8arraylist'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
Expand Down Expand Up @@ -156,6 +157,20 @@ describe('ed25519', function () {
expect(key.publicKey.equals(imported)).to.be.true()
})

it('is PrivateKey', async () => {
const key = await generateKeyPair('Ed25519')

expect(isPrivateKey(key)).to.be.true()
expect(isPublicKey(key)).to.be.false()
})

it('is PublicKey', async () => {
const key = await generateKeyPair('Ed25519')

expect(isPrivateKey(key.publicKey)).to.be.false()
expect(isPublicKey(key.publicKey)).to.be.true()
})

describe('go interop', () => {
// @ts-check
it('verifies with data from go', async () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/crypto/test/keys/rsa.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
import { isPrivateKey, isPublicKey } from '@libp2p/interface'
import { expect } from 'aegir/chai'
import { Uint8ArrayList } from 'uint8arraylist'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
Expand Down Expand Up @@ -115,6 +116,20 @@ describe('RSA', function () {
expect(key.publicKey.equals(imported)).to.be.true()
})

it('is PrivateKey', async () => {
const key = await generateKeyPair('RSA', 512)

expect(isPrivateKey(key)).to.be.true()
expect(isPublicKey(key)).to.be.false()
})

it('is PublicKey', async () => {
const key = await generateKeyPair('RSA', 512)

expect(isPrivateKey(key.publicKey)).to.be.false()
expect(isPublicKey(key.publicKey)).to.be.true()
})

describe('key equals', () => {
it('equals itself', () => {
expect(key.equals(key)).to.be.true()
Expand Down
15 changes: 15 additions & 0 deletions packages/crypto/test/keys/secp256k1.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/await-thenable */ // secp is sync in node, async in browsers
/* eslint-env mocha */
import { isPrivateKey, isPublicKey } from '@libp2p/interface'
import { expect } from 'aegir/chai'
import { Uint8ArrayList } from 'uint8arraylist'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
Expand Down Expand Up @@ -83,6 +84,20 @@ describe('secp256k1 keys', () => {
expect(key.publicKey.equals(imported)).to.be.true()
})

it('is PrivateKey', async () => {
const key = await generateKeyPair('secp256k1')

expect(isPrivateKey(key)).to.be.true()
expect(isPublicKey(key)).to.be.false()
})

it('is PublicKey', async () => {
const key = await generateKeyPair('secp256k1')

expect(isPrivateKey(key.publicKey)).to.be.false()
expect(isPublicKey(key.publicKey)).to.be.true()
})

describe('key equals', () => {
it('equals itself', () => {
expect(key.equals(key)).to.be.true()
Expand Down
33 changes: 33 additions & 0 deletions packages/interface/src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ export interface Ed25519PublicKey extends PublicKeyBase<'Ed25519', 0x0> {}
export interface Secp256k1PublicKey extends PublicKeyBase<'secp256k1', 0x0> {}
export type PublicKey = RSAPublicKey | Ed25519PublicKey | Secp256k1PublicKey

/**
* Returns true if the passed argument has type overlap with the `PublicKey`
* interface. Can be used to disambiguate object types.
*/
export function isPublicKey (key?: any): key is PublicKey {
if (key == null) {
return false
}

return (key.type === 'RSA' || key.type === 'Ed25519' || key.type === 'secp256k1') &&
key.raw instanceof Uint8Array &&
typeof key.equals === 'function' &&
typeof key.toMultihash === 'function' &&
typeof key.toCID === 'function' &&
typeof key.verify === 'function'
}

/**
* Generic private key interface
*/
Expand Down Expand Up @@ -92,3 +109,19 @@ export interface RSAPrivateKey extends PrivateKeyBase<'RSA', RSAPublicKey> {}
export interface Ed25519PrivateKey extends PrivateKeyBase<'Ed25519', Ed25519PublicKey> {}
export interface Secp256k1PrivateKey extends PrivateKeyBase<'secp256k1', Secp256k1PublicKey> {}
export type PrivateKey = RSAPrivateKey | Ed25519PrivateKey | Secp256k1PrivateKey

/**
* Returns true if the passed argument has type overlap with the `PrivateKey`
* interface. Can be used to disambiguate object types.
*/
export function isPrivateKey (key?: any): key is PrivateKey {
if (key == null) {
return false
}

return (key.type === 'RSA' || key.type === 'Ed25519' || key.type === 'secp256k1') &&
isPublicKey(key.publicKey) &&
key.raw instanceof Uint8Array &&
typeof key.equals === 'function' &&
typeof key.sign === 'function'
}
Loading