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
|
1 |
|
const PublicKey = require('./PublicKey.js') |
29
|
1 |
|
const sha256 = require('../../util/sha256.js') |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Класс для работы с приватными ключами. |
33
|
|
|
* @class |
34
|
|
|
*/ |
35
|
|
|
class SecretKey { |
36
|
|
|
/** |
37
|
|
|
* @param {ArrayLike<number>} bytes Приватный ключ в бинарном виде. |
38
|
|
|
* В формате libsodium, 64 байта. |
39
|
|
|
* @throws {Error} |
40
|
|
|
* @example |
41
|
|
|
* let bytes = SecretKey.fromSeed(new Uint8Array(32)).getBytes() |
42
|
|
|
* let secKey = new SecretKey(bytes) |
43
|
|
|
*/ |
44
|
|
|
constructor (bytes) { |
45
|
|
|
/** |
46
|
|
|
* Приватный ключ в бинарном виде. В формате libsodium. |
47
|
|
|
* @type {number[]} |
48
|
|
|
* @private |
49
|
|
|
*/ |
50
|
8 |
|
this._bytes = [] |
51
|
8 |
|
if (bytes.length !== 64) { |
52
|
1 |
|
throw new Error('invalid length') |
53
|
|
|
} |
54
|
7 |
|
array.arraySet(this._bytes, bytes) |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Статический метод, создающий приватный ключ из seed.\ |
59
|
|
|
* Libsodium принимает seed длиной 32 байта, поэтому если длина |
60
|
|
|
* отличается, то берется sha256 хэш. |
61
|
|
|
* @param {ArrayLike<number>} seed Массив байтов любой длины. |
62
|
|
|
* @returns {SecretKey} |
63
|
|
|
* @example |
64
|
|
|
* let seed = new Uint8Array(32) |
65
|
|
|
* let secKey = SecretKey.fromSeed(seed) |
66
|
|
|
*/ |
67
|
|
|
static fromSeed (seed) { |
68
|
5 |
|
let entropy = seed |
69
|
5 |
|
if (seed.length !== 32) { |
70
|
3 |
|
entropy = sha256.sha256(entropy) |
71
|
|
|
} |
72
|
5 |
|
return new SecretKey(index.secretKeyFromSeed(entropy)) |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Приватный ключ в бинарном виде. В формате libsodium, 64 байта. |
77
|
|
|
* @returns {number[]} |
78
|
|
|
* @example |
79
|
|
|
* let secKey = SecretKey.fromSeed(new Uint8Array(32)) |
80
|
|
|
* let bytes = secKey.getBytes() |
81
|
|
|
*/ |
82
|
|
|
getBytes () { |
83
|
1 |
|
return this._bytes.slice(0) |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Публичный ключ, соответствующий приватному ключу. |
88
|
|
|
* @returns {PublicKey} |
89
|
|
|
* @example |
90
|
|
|
* let secKey = SecretKey.fromSeed(new Uint8Array(32)) |
91
|
|
|
* let pubKey = secKey.getPublicKey() |
92
|
|
|
*/ |
93
|
|
|
getPublicKey () { |
94
|
5 |
|
return new PublicKey.PublicKey(this._bytes.slice(32, 64)) |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Создает цифровую подпись сообщения. |
99
|
|
|
* @param {ArrayLike<number>} message Сообщение, которое необходимо подписать. |
100
|
|
|
* @returns {number[]} Подпись длиной 64 байта. |
101
|
|
|
* @example |
102
|
|
|
* let secKey = SecretKey.fromSeed(new Uint8Array(32)) |
103
|
|
|
* let message = new TextEncoder().encode('Hello World') |
104
|
|
|
* let signature = secKey.sign(message) |
105
|
|
|
*/ |
106
|
|
|
sign (message) { |
107
|
2 |
|
return index.sign(message, this._bytes) |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
exports.SecretKey = SecretKey |
112
|
|
|
|