dist/key/ed25519/PublicKey.js   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 1.5

Size

Lines of Code 70
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 70
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 6
mnd 2
bc 2
fnc 4
bpm 0.5
cpm 1.5
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A PublicKey.constructor 0 12 2
A PublicKey.verifySignature 0 6 2
A PublicKey.getPublicKey 0 3 1
A PublicKey.getBytes 0 3 1
1
/**
2
 * @license
3
 * Copyright (c) 2020 UMI
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in all
13
 * copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 * SOFTWARE.
22
 */
23
24
'use strict'
25
26 1
const array = require('../../util/array.js')
27 1
const index = require('../../util/ed25519/index.js')
28
29
/**
30
 * Класс для работы с публичными ключами.
31
 * @class
32
 */
33
class PublicKey {
34
  /**
35
   * @param {ArrayLike<number>} bytes Публичный ключ в формате libsodium, 32 байта.
36
   * @throws {Error}
37
   * @example
38
   * let bytes = new Uint8Array(32)
39
   * let pubKey = new PublicKey(bytes)
40
   */
41
  constructor (bytes) {
42
    /**
43
     * Публичный ключ в бинарном виде. В формате libsodium.
44
     * @type {number[]}
45
     * @private
46
     */
47 13
    this._bytes = []
48 13
    if (bytes.length !== 32) {
49 1
      throw new Error('invalid length')
50
    }
51 12
    array.arraySet(this._bytes, bytes)
52
  }
53
54
  /**
55
   * Публичный ключ в формате libsodium, 32 байта.
56
   * @returns {number[]}
57
   * @example
58
   * let bytes = new PublicKey(new Uint8Array(32)).getBytes()
59
   */
60
  getBytes () {
61 8
    return this._bytes.slice(0)
62
  }
63
64
  /**
65
   * Публичный ключ.
66
   * @returns {PublicKey}
67
   * @private
68
   */
69
  getPublicKey () {
70 1
    return this
71
  }
72
73
  /**
74
   * Проверяет цифровую подпись.
75
   * @param {ArrayLike<number>} signature Подпись, 64 байта.
76
   * @param {ArrayLike<number>} message Сообщение.
77
   * @returns {boolean}
78
   * @throws {Error}
79
   * @example
80
   * let pubKey = new PublicKey(new Uint8Array(32))
81
   * let signature = new Uint8Array(64)
82
   * let message = new TextEncoder().encode('Hello World')
83
   * let isValid = pubKey.verifySignature(signature, message)
84
   */
85
  verifySignature (signature, message) {
86 4
    if (signature.length !== 64) {
87 1
      throw new Error('invalid length')
88
    }
89 3
    return index.verify(signature, message, this._bytes)
90
  }
91
}
92
93
exports.PublicKey = PublicKey
94