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

Commit

Permalink
🎨 Update the tests to fix null assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Dec 25, 2021
1 parent a753fe9 commit 21d2aad
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/libp2p-interfaces-compliance-tests/src/peer-id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { TestSetup } from '../index.js'
import type { PeerIdFactory } from 'libp2p-interfaces/peer-id'
import type { PublicKey, PrivateKey } from 'libp2p-interfaces/keys'

const DAG_PB_CODE = 0x70
const LIBP2P_KEY_CODE = 0x72
Expand Down Expand Up @@ -58,14 +59,14 @@ export default (common: TestSetup<PeerIdFactory>) => {

it('can be created for a Secp256k1 key', async () => {
const id = await factory.create({ keyType: 'secp256k1', bits: 256 })
const expB58 = base58btc.encode((await identity.digest(id.pubKey!.bytes)).bytes).slice(1)
const expB58 = base58btc.encode((await identity.digest((id.pubKey as PublicKey).bytes)).bytes).slice(1)
expect(id.toB58String()).to.equal(expB58)
})

it('can get the public key from a Secp256k1 key', async () => {
const original = await factory.create({ keyType: 'secp256k1', bits: 256 })
const newId = factory.createFromB58String(original.toB58String())
expect(original.pubKey!.bytes).to.eql(newId.pubKey!.bytes)
expect((original.pubKey as PublicKey).bytes).to.eql((newId.pubKey as PublicKey).bytes)
})

it('isPeerId', async () => {
Expand Down Expand Up @@ -194,15 +195,15 @@ export default (common: TestSetup<PeerIdFactory>) => {
const key = '12D3KooWRm8J3iL796zPFi2EtGGtUJn58AG67gcqzMFHZnnsTzqD'
const id = await factory.parse(key)
expect(id.toB58String()).to.equal(key)
const expB58 = base58btc.encode((await identity.digest(id.pubKey!.bytes)).bytes).slice(1)
const expB58 = base58btc.encode((await identity.digest((id.pubKey as PublicKey).bytes)).bytes).slice(1)
expect(id.toB58String()).to.equal(expB58)
})

it('recreate from embedded secp256k1 key', async () => {
const key = '16Uiu2HAm5qw8UyXP2RLxQUx5KvtSN8DsTKz8quRGqGNC3SYiaB8E'
const id = await factory.parse(key)
expect(id.toB58String()).to.equal(key)
const expB58 = base58btc.encode((await identity.digest(id.pubKey!.bytes)).bytes).slice(1)
const expB58 = base58btc.encode((await identity.digest((id.pubKey as PublicKey).bytes)).bytes).slice(1)
expect(id.toB58String()).to.equal(expB58)
})

Expand All @@ -215,20 +216,20 @@ export default (common: TestSetup<PeerIdFactory>) => {
it('can be created from a Secp256k1 public key', async () => {
const privKey = await crypto.keys.generateKeyPair('secp256k1', 256)
const id = await factory.createFromPubKey(privKey.public.bytes)
const expB58 = base58btc.encode((await identity.digest(id.pubKey!.bytes)).bytes).slice(1)
const expB58 = base58btc.encode((await identity.digest((id.pubKey as PublicKey).bytes)).bytes).slice(1)
expect(id.toB58String()).to.equal(expB58)
})

it('can be created from a Secp256k1 private key', async () => {
const privKey = await crypto.keys.generateKeyPair('secp256k1', 256)
const id = await factory.createFromPrivKey(privKey.bytes)
const expB58 = base58btc.encode((await identity.digest(id.pubKey!.bytes)).bytes).slice(1)
const expB58 = base58btc.encode((await identity.digest((id.pubKey as PublicKey).bytes)).bytes).slice(1)
expect(id.toB58String()).to.equal(expB58)
})

it('Compare generated ID with one created from PubKey', async () => {
const id1 = await factory.create(testOpts)
const id2 = await factory.createFromPubKey(id1.marshalPubKey()!)
const id2 = await factory.createFromPubKey(id1.marshalPubKey() as Uint8Array)
expect(id1.id).to.be.eql(id2.id)
})

Expand All @@ -242,7 +243,7 @@ export default (common: TestSetup<PeerIdFactory>) => {
this.timeout(1000 * 60)
const shortId = await factory.create(testOpts)
const longId = await factory.create({ bits: 1024 })
expect(shortId.privKey!.bytes.length).is.below(longId.privKey!.bytes.length)
expect((shortId.privKey as PrivateKey).bytes.length).is.below((longId.privKey as PrivateKey).bytes.length)
})

it('Pretty printing', async () => {
Expand Down Expand Up @@ -292,8 +293,8 @@ export default (common: TestSetup<PeerIdFactory>) => {
const id = await factory.create(testOpts)
const other = await factory.createFromJSON(id.toJSON())
expect(id.toB58String()).to.equal(other.toB58String())
expect(id.privKey!.bytes).to.eql(other.privKey!.bytes)
expect(id.pubKey!.bytes).to.eql(other.pubKey!.bytes)
expect((id.privKey as PrivateKey).bytes).to.eql((other.privKey as PrivateKey).bytes)
expect((id.pubKey as PublicKey).bytes).to.eql((other.pubKey as PublicKey).bytes)
})

it('only id', async () => {
Expand All @@ -308,7 +309,7 @@ export default (common: TestSetup<PeerIdFactory>) => {

it('go interop', async () => {
const id = await factory.createFromJSON(goId)
const digest = await id.privKey!.public.hash()
const digest = await (id.privKey as PrivateKey).public.hash()
expect(base58btc.encode(digest).slice(1)).to.eql(goId.id)
})
})
Expand Down

0 comments on commit 21d2aad

Please sign in to comment.