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

Commit e2713a3

Browse files
authored
fix: update interfaces (#40)
Updates all deps
1 parent c869945 commit e2713a3

File tree

5 files changed

+30
-39
lines changed

5 files changed

+30
-39
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@
159159
"release": "aegir release"
160160
},
161161
"dependencies": {
162+
"@libp2p/interfaces": "^1.3.21",
162163
"err-code": "^3.0.1",
163164
"multiformats": "^9.4.5",
164165
"protons-runtime": "^1.0.2",
165166
"uint8arrays": "^3.0.0"
166167
},
167168
"devDependencies": {
168-
"@libp2p/crypto": "^0.22.9",
169-
"@libp2p/interfaces": "^1.3.20",
170-
"aegir": "^37.0.10",
169+
"@libp2p/crypto": "^0.22.10",
170+
"aegir": "^37.0.13",
171171
"protons": "^3.0.2"
172172
}
173173
}

src/record.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable @typescript-eslint/no-namespace */
33

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

67
export interface Record {
78
key: Uint8Array
@@ -10,7 +11,7 @@ export interface Record {
1011
}
1112

1213
export namespace Record {
13-
export const codec = () => {
14+
export const codec = (): Codec<Record> => {
1415
return message<Record>({
1516
1: { name: 'key', codec: bytes },
1617
2: { name: 'value', codec: bytes },

src/selectors.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import errcode from 'err-code'
1+
import errCode from 'err-code'
22
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
33
import type { Selectors } from '@libp2p/interfaces/dht'
44

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

12-
throw errcode(new Error(errMsg), 'ERR_NO_RECORDS_RECEIVED')
12+
throw errCode(new Error(errMsg), 'ERR_NO_RECORDS_RECEIVED')
1313
}
1414

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

21-
throw errcode(new Error(errMsg), 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY')
21+
throw errCode(new Error(errMsg), 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY')
2222
}
2323

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

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

29-
throw errcode(new Error(errMsg), 'ERR_UNRECOGNIZED_KEY_PREFIX')
29+
throw errCode(new Error(errMsg), 'ERR_UNRECOGNIZED_KEY_PREFIX')
3030
}
3131

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

48-
export const selectors = {
48+
export const selectors: Selectors = {
4949
pk: publickKey
5050
}

src/validators.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import errcode from 'err-code'
1+
import errCode from 'err-code'
22
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
33
import type { Libp2pRecord } from './index.js'
44
import type { Validators } from '@libp2p/interfaces/dht'
@@ -25,10 +25,10 @@ export function verifyRecord (validators: Validators, record: Libp2pRecord) {
2525
if (validator == null) {
2626
const errMsg = 'Invalid record keytype'
2727

28-
throw errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
28+
throw errCode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
2929
}
3030

31-
return validator.func(key, record.value)
31+
return validator(key, record.value)
3232
}
3333

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

4848
if (key.byteLength < 5) {
49-
throw errcode(new Error('invalid public key record'), 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
49+
throw errCode(new Error('invalid public key record'), 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
5050
}
5151

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

5454
if (prefix !== '/pk/') {
55-
throw errcode(new Error('key was not prefixed with /pk/'), 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
55+
throw errCode(new Error('key was not prefixed with /pk/'), 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
5656
}
5757

5858
const keyhash = key.slice(4)
5959

6060
const publicKeyHash = await sha256.digest(publicKey)
6161

6262
if (!uint8ArrayEquals(keyhash, publicKeyHash.bytes)) {
63-
throw errcode(new Error('public key does not match passed in key'), 'ERR_INVALID_RECORD_HASH_MISMATCH')
63+
throw errCode(new Error('public key does not match passed in key'), 'ERR_INVALID_RECORD_HASH_MISMATCH')
6464
}
6565
}
6666

67-
const publicKey = {
68-
func: validatePublicKeyRecord,
69-
sign: false
70-
}
71-
72-
export const validators = {
73-
pk: publicKey
67+
export const validators: Validators = {
68+
pk: validatePublicKeyRecord
7469
}

test/validator.spec.ts

+11-16
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ describe('validator', () => {
6767
const rec = new Libp2pRecord(k, uint8ArrayFromString('world'), new Date())
6868

6969
const validators: Validators = {
70-
hello: {
71-
async func (key, value) {
72-
expect(key).to.eql(k)
73-
expect(value).to.eql(uint8ArrayFromString('world'))
74-
}
70+
async hello (key, value) {
71+
expect(key).to.eql(k)
72+
expect(value).to.eql(uint8ArrayFromString('world'))
7573
}
7674
}
7775
return validator.verifyRecord(validators, rec)
@@ -82,11 +80,9 @@ describe('validator', () => {
8280
const rec = new Libp2pRecord(k, uint8ArrayFromString('world'), new Date())
8381

8482
const validators: Validators = {
85-
hello: {
86-
async func (key, value) {
87-
expect(key).to.eql(k)
88-
expect(value).to.eql(uint8ArrayFromString('world'))
89-
}
83+
async hello (key, value) {
84+
expect(key).to.eql(k)
85+
expect(value).to.eql(uint8ArrayFromString('world'))
9086
}
9187
}
9288
return expect(
@@ -103,24 +99,23 @@ describe('validator', () => {
10399
})
104100

105101
describe('public key', () => {
106-
it('exports func and sign', () => {
102+
it('exports func', () => {
107103
const pk = validator.validators.pk
108104

109-
expect(pk).to.have.property('func')
110-
expect(pk).to.have.property('sign', false)
105+
expect(pk).to.be.a('function')
111106
})
112107

113108
it('does not error on valid record', async () => {
114109
return await Promise.all(cases.valid.publicKey.map(async (k) => {
115-
return await validator.validators.pk.func(k, key.public.bytes)
110+
return await validator.validators.pk(k, key.public.bytes)
116111
}))
117112
})
118113

119114
it('throws on invalid records', async () => {
120115
return await Promise.all(cases.invalid.publicKey.map(async ({ data, code }) => {
121116
try {
122117
//
123-
await validator.validators.pk.func(data, key.public.bytes)
118+
await validator.validators.pk(data, key.public.bytes)
124119
} catch (err: any) {
125120
expect(err.code).to.eql(code)
126121
return
@@ -137,7 +132,7 @@ describe('validator', () => {
137132

138133
const hash = await pubKey.hash()
139134
const k = Uint8Array.of(...uint8ArrayFromString('/pk/'), ...hash)
140-
return await validator.validators.pk.func(k, pubKey.bytes)
135+
return await validator.validators.pk(k, pubKey.bytes)
141136
})
142137
})
143138
})

0 commit comments

Comments
 (0)