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 PublicKey = require('../key/ed25519/PublicKey.js') |
28
|
1 |
|
const converter = require('../util/converter.js') |
29
|
1 |
|
const bech32 = require('../util/bech32.js') |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Класс для работы с адресами. |
33
|
|
|
* @class |
34
|
|
|
*/ |
35
|
|
|
class Address { |
36
|
|
|
/** |
37
|
|
|
* @example |
38
|
|
|
* let address = new Address() |
39
|
|
|
*/ |
40
|
|
|
constructor () { |
41
|
|
|
/** |
42
|
|
|
* Адрес в бинарном виде, длина 34 байта. |
43
|
|
|
* @type {number[]} |
44
|
|
|
* @private |
45
|
|
|
*/ |
46
|
41 |
|
this._bytes = array.arrayNew(34) |
47
|
41 |
|
this.setPrefix('umi') |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Статический метод, создает объект из адреса в формате Bech32. |
52
|
|
|
* @param {string} bech32 Адрес в формате Bech32, длина 62 или 65 символов. |
53
|
|
|
* @returns {Address} |
54
|
|
|
* @throws {Error} |
55
|
|
|
* @example |
56
|
|
|
* let address = Address.fromBech32('umi18d4z00xwk6jz6c4r4rgz5mcdwdjny9thrh3y8f36cpy2rz6emg5s6rxnf6') |
57
|
|
|
*/ |
58
|
|
|
static fromBech32 (bech32$1) { |
59
|
15 |
|
const adr = new Address() |
60
|
15 |
|
array.arraySet(adr._bytes, bech32.bech32Decode(bech32$1)) |
61
|
5 |
|
return adr |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Статический метод, создает объект из бинарного представления. |
66
|
|
|
* @param {ArrayLike<number>} bytes Адрес в бинарном виде, длина 34 байта. |
67
|
|
|
* @returns {Address} |
68
|
|
|
* @throws {Error} |
69
|
|
|
* @example |
70
|
|
|
* let address = Address.fromBytes(new Uint8Array(34)) |
71
|
|
|
*/ |
72
|
|
|
static fromBytes (bytes) { |
73
|
5 |
|
if (bytes.length !== 34) { |
74
|
1 |
|
throw new Error('length must be 34 bytes') |
75
|
|
|
} |
76
|
4 |
|
const adr = new Address() |
77
|
4 |
|
array.arraySet(adr._bytes, bytes) |
78
|
4 |
|
return adr |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Статический метод, создает объект из публичного или приватного ключа. |
83
|
|
|
* @param {(PublicKey|SecretKey)} key Публичный или приватный ключ. |
84
|
|
|
* @returns {Address} |
85
|
|
|
* @example |
86
|
|
|
* let secKey = SecretKey.fromSeed([]) |
87
|
|
|
* let address = Address.fromKey(secKey) |
88
|
|
|
*/ |
89
|
|
|
static fromKey (key) { |
90
|
4 |
|
return new Address().setPublicKey(key.getPublicKey()) |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Адрес в формате Bech32, длина 62 или 65 символов. |
95
|
|
|
* @returns {string} |
96
|
|
|
* @example |
97
|
|
|
* let bech32 = new Address().getBech32() |
98
|
|
|
*/ |
99
|
|
|
getBech32 () { |
100
|
7 |
|
return bech32.bech32Encode(this._bytes) |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Адрес в бинарном виде, длина 34 байта. |
105
|
|
|
* @returns {number[]} |
106
|
|
|
* @example |
107
|
|
|
* let bytes = new Address().getBytes() |
108
|
|
|
*/ |
109
|
|
|
getBytes () { |
110
|
5 |
|
return this._bytes.slice(0) |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Префикс адреса, три символа латиницы в нижнем регистре. |
115
|
|
|
* @returns {string} |
116
|
|
|
* @throws {Error} |
117
|
|
|
* @example |
118
|
|
|
* let prefix = new Address().getPrefix() |
119
|
|
|
*/ |
120
|
|
|
getPrefix () { |
121
|
6 |
|
return converter.versionToPrefix(converter.bytesToUint16(this._bytes.slice(0, 2))) |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Устанавливает префикс адреса и возвращает this. |
126
|
|
|
* @param {string} prefix Префикс адреса, три символа латиницы в нижнем регистре. |
127
|
|
|
* @returns {Address} |
128
|
|
|
* @throws {Error} |
129
|
|
|
* @example |
130
|
|
|
* let address = new Address().setPrefix('umi') |
131
|
|
|
*/ |
132
|
|
|
setPrefix (prefix) { |
133
|
57 |
|
array.arraySet(this._bytes, converter.uint16ToBytes(converter.prefixToVersion(prefix))) |
134
|
48 |
|
return this |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Публичный ключ. |
139
|
|
|
* @returns {PublicKey} |
140
|
|
|
* @example |
141
|
|
|
* let pubKey = new Address().getPublicKey() |
142
|
|
|
*/ |
143
|
|
|
getPublicKey () { |
144
|
2 |
|
return new PublicKey.PublicKey(this._bytes.slice(2)) |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Устанавливает публичный ключ и возвращает this. |
149
|
|
|
* @param {PublicKey} publicKey Публичный ключ. |
150
|
|
|
* @returns {Address} |
151
|
|
|
* @throws {Error} |
152
|
|
|
* @example |
153
|
|
|
* let pubKey = SecretKey.fromSeed([]).getPublicKey() |
154
|
|
|
* let address = new Address().setPublicKey(pubKey) |
155
|
|
|
*/ |
156
|
|
|
setPublicKey (publicKey) { |
157
|
5 |
|
array.arraySet(this._bytes, publicKey.getBytes(), 2) |
158
|
4 |
|
return this |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
exports.Address = Address |
163
|
|
|
|