@libp2p/record 0.10.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +40 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +52 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/record.d.ts +71 -0
- package/dist/src/record.js +202 -0
- package/dist/src/record.proto +20 -0
- package/dist/src/selectors.d.ts +16 -0
- package/dist/src/selectors.d.ts.map +1 -0
- package/dist/src/selectors.js +38 -0
- package/dist/src/selectors.js.map +1 -0
- package/dist/src/utils.d.ts +11 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +41 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/src/validators.d.ts +15 -0
- package/dist/src/validators.d.ts.map +1 -0
- package/dist/src/validators.js +58 -0
- package/dist/src/validators.js.map +1 -0
- package/package.json +176 -0
- package/src/index.ts +68 -0
- package/src/record.d.ts +71 -0
- package/src/record.js +202 -0
- package/src/record.proto +20 -0
- package/src/selectors.ts +50 -0
- package/src/utils.ts +46 -0
- package/src/validators.ts +74 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import errcode from 'err-code'
|
|
2
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
3
|
+
import type { Libp2pRecord } from './index.js'
|
|
4
|
+
import type { Validators } from '@libp2p/interfaces/dht'
|
|
5
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
|
6
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks a record and ensures it is still valid.
|
|
10
|
+
* It runs the needed validators.
|
|
11
|
+
* If verification fails the returned Promise will reject with the error.
|
|
12
|
+
*/
|
|
13
|
+
export function verifyRecord (validators: Validators, record: Libp2pRecord) {
|
|
14
|
+
const key = record.key
|
|
15
|
+
const keyString = uint8ArrayToString(key)
|
|
16
|
+
const parts = keyString.split('/')
|
|
17
|
+
|
|
18
|
+
if (parts.length < 3) {
|
|
19
|
+
// No validator available
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const validator = validators[parts[1].toString()]
|
|
24
|
+
|
|
25
|
+
if (validator == null) {
|
|
26
|
+
const errMsg = 'Invalid record keytype'
|
|
27
|
+
|
|
28
|
+
throw errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return validator.func(key, record.value)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Validator for public key records.
|
|
36
|
+
* Verifies that the passed in record value is the PublicKey
|
|
37
|
+
* that matches the passed in key.
|
|
38
|
+
* If validation fails the returned Promise will reject with the error.
|
|
39
|
+
*
|
|
40
|
+
* @param {Uint8Array} key - A valid key is of the form `'/pk/<keymultihash>'`
|
|
41
|
+
* @param {Uint8Array} publicKey - The public key to validate against (protobuf encoded).
|
|
42
|
+
*/
|
|
43
|
+
const validatePublicKeyRecord = async (key: Uint8Array, publicKey: Uint8Array) => {
|
|
44
|
+
if (!(key instanceof Uint8Array)) {
|
|
45
|
+
throw errcode(new Error('"key" must be a Uint8Array'), 'ERR_INVALID_RECORD_KEY_NOT_BUFFER')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (key.byteLength < 5) {
|
|
49
|
+
throw errcode(new Error('invalid public key record'), 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const prefix = uint8ArrayToString(key.subarray(0, 4))
|
|
53
|
+
|
|
54
|
+
if (prefix !== '/pk/') {
|
|
55
|
+
throw errcode(new Error('key was not prefixed with /pk/'), 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const keyhash = key.slice(4)
|
|
59
|
+
|
|
60
|
+
const publicKeyHash = await sha256.digest(publicKey)
|
|
61
|
+
|
|
62
|
+
if (!uint8ArrayEquals(keyhash, publicKeyHash.bytes)) {
|
|
63
|
+
throw errcode(new Error('public key does not match passed in key'), 'ERR_INVALID_RECORD_HASH_MISMATCH')
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const publicKey = {
|
|
68
|
+
func: validatePublicKeyRecord,
|
|
69
|
+
sign: false
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const validators = {
|
|
73
|
+
publicKey
|
|
74
|
+
}
|