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

Commit

Permalink
fix: update interfaces (#40)
Browse files Browse the repository at this point in the history
Updates all deps
  • Loading branch information
achingbrain authored Apr 13, 2022
1 parent c869945 commit e2713a3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 39 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/interfaces": "^1.3.21",
"err-code": "^3.0.1",
"multiformats": "^9.4.5",
"protons-runtime": "^1.0.2",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@libp2p/crypto": "^0.22.9",
"@libp2p/interfaces": "^1.3.20",
"aegir": "^37.0.10",
"@libp2p/crypto": "^0.22.10",
"aegir": "^37.0.13",
"protons": "^3.0.2"
}
}
3 changes: 2 additions & 1 deletion src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/no-namespace */

import { encodeMessage, decodeMessage, message, bytes, string } from 'protons-runtime'
import type { Codec } from 'protons-runtime'

export interface Record {
key: Uint8Array
Expand All @@ -10,7 +11,7 @@ export interface Record {
}

export namespace Record {
export const codec = () => {
export const codec = (): Codec<Record> => {
return message<Record>({
1: { name: 'key', codec: bytes },
2: { name: 'value', codec: bytes },
Expand Down
10 changes: 5 additions & 5 deletions src/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errcode from 'err-code'
import errCode from 'err-code'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { Selectors } from '@libp2p/interfaces/dht'

Expand All @@ -9,7 +9,7 @@ export function bestRecord (selectors: Selectors, k: Uint8Array, records: Uint8A
if (records.length === 0) {
const errMsg = 'No records given'

throw errcode(new Error(errMsg), 'ERR_NO_RECORDS_RECEIVED')
throw errCode(new Error(errMsg), 'ERR_NO_RECORDS_RECEIVED')
}

const kStr = uint8ArrayToString(k)
Expand All @@ -18,15 +18,15 @@ export function bestRecord (selectors: Selectors, k: Uint8Array, records: Uint8A
if (parts.length < 3) {
const errMsg = 'Record key does not have a selector function'

throw errcode(new Error(errMsg), 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY')
throw errCode(new Error(errMsg), 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY')
}

const selector = selectors[parts[1].toString()]

if (selector == null) {
const errMsg = `Unrecognized key prefix: ${parts[1]}`

throw errcode(new Error(errMsg), 'ERR_UNRECOGNIZED_KEY_PREFIX')
throw errCode(new Error(errMsg), 'ERR_UNRECOGNIZED_KEY_PREFIX')
}

if (records.length === 1) {
Expand All @@ -45,6 +45,6 @@ function publickKey (k: Uint8Array, records: Uint8Array[]) {
return 0
}

export const selectors = {
export const selectors: Selectors = {
pk: publickKey
}
23 changes: 9 additions & 14 deletions src/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errcode from 'err-code'
import errCode from 'err-code'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { Libp2pRecord } from './index.js'
import type { Validators } from '@libp2p/interfaces/dht'
Expand All @@ -25,10 +25,10 @@ export function verifyRecord (validators: Validators, record: Libp2pRecord) {
if (validator == null) {
const errMsg = 'Invalid record keytype'

throw errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
throw errCode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
}

return validator.func(key, record.value)
return validator(key, record.value)
}

/**
Expand All @@ -42,33 +42,28 @@ export function verifyRecord (validators: Validators, record: Libp2pRecord) {
*/
const validatePublicKeyRecord = async (key: Uint8Array, publicKey: Uint8Array) => {
if (!(key instanceof Uint8Array)) {
throw errcode(new Error('"key" must be a Uint8Array'), 'ERR_INVALID_RECORD_KEY_NOT_BUFFER')
throw errCode(new Error('"key" must be a Uint8Array'), 'ERR_INVALID_RECORD_KEY_NOT_BUFFER')
}

if (key.byteLength < 5) {
throw errcode(new Error('invalid public key record'), 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
throw errCode(new Error('invalid public key record'), 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
}

const prefix = uint8ArrayToString(key.subarray(0, 4))

if (prefix !== '/pk/') {
throw errcode(new Error('key was not prefixed with /pk/'), 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
throw errCode(new Error('key was not prefixed with /pk/'), 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
}

const keyhash = key.slice(4)

const publicKeyHash = await sha256.digest(publicKey)

if (!uint8ArrayEquals(keyhash, publicKeyHash.bytes)) {
throw errcode(new Error('public key does not match passed in key'), 'ERR_INVALID_RECORD_HASH_MISMATCH')
throw errCode(new Error('public key does not match passed in key'), 'ERR_INVALID_RECORD_HASH_MISMATCH')
}
}

const publicKey = {
func: validatePublicKeyRecord,
sign: false
}

export const validators = {
pk: publicKey
export const validators: Validators = {
pk: validatePublicKeyRecord
}
27 changes: 11 additions & 16 deletions test/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ describe('validator', () => {
const rec = new Libp2pRecord(k, uint8ArrayFromString('world'), new Date())

const validators: Validators = {
hello: {
async func (key, value) {
expect(key).to.eql(k)
expect(value).to.eql(uint8ArrayFromString('world'))
}
async hello (key, value) {
expect(key).to.eql(k)
expect(value).to.eql(uint8ArrayFromString('world'))
}
}
return validator.verifyRecord(validators, rec)
Expand All @@ -82,11 +80,9 @@ describe('validator', () => {
const rec = new Libp2pRecord(k, uint8ArrayFromString('world'), new Date())

const validators: Validators = {
hello: {
async func (key, value) {
expect(key).to.eql(k)
expect(value).to.eql(uint8ArrayFromString('world'))
}
async hello (key, value) {
expect(key).to.eql(k)
expect(value).to.eql(uint8ArrayFromString('world'))
}
}
return expect(
Expand All @@ -103,24 +99,23 @@ describe('validator', () => {
})

describe('public key', () => {
it('exports func and sign', () => {
it('exports func', () => {
const pk = validator.validators.pk

expect(pk).to.have.property('func')
expect(pk).to.have.property('sign', false)
expect(pk).to.be.a('function')
})

it('does not error on valid record', async () => {
return await Promise.all(cases.valid.publicKey.map(async (k) => {
return await validator.validators.pk.func(k, key.public.bytes)
return await validator.validators.pk(k, key.public.bytes)
}))
})

it('throws on invalid records', async () => {
return await Promise.all(cases.invalid.publicKey.map(async ({ data, code }) => {
try {
//
await validator.validators.pk.func(data, key.public.bytes)
await validator.validators.pk(data, key.public.bytes)
} catch (err: any) {
expect(err.code).to.eql(code)
return
Expand All @@ -137,7 +132,7 @@ describe('validator', () => {

const hash = await pubKey.hash()
const k = Uint8Array.of(...uint8ArrayFromString('/pk/'), ...hash)
return await validator.validators.pk.func(k, pubKey.bytes)
return await validator.validators.pk(k, pubKey.bytes)
})
})
})

0 comments on commit e2713a3

Please sign in to comment.