1 | 'use strict'
|
2 |
|
3 | const errcode = require('err-code')
|
4 | const uint8ArrayToString = require('uint8arrays/to-string')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const verifyRecord = (validators, record) => {
|
19 | const key = record.key
|
20 | const keyString = uint8ArrayToString(key)
|
21 | const parts = keyString.split('/')
|
22 |
|
23 | if (parts.length < 3) {
|
24 |
|
25 | return
|
26 | }
|
27 |
|
28 | const validator = validators[parts[1].toString()]
|
29 |
|
30 | if (!validator) {
|
31 | const errMsg = 'Invalid record keytype'
|
32 |
|
33 | throw errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
|
34 | }
|
35 |
|
36 | return validator.func(key, record.value)
|
37 | }
|
38 |
|
39 | module.exports = {
|
40 | verifyRecord: verifyRecord,
|
41 | validators: require('./validators')
|
42 | }
|