/
crypto.ts
186 lines (159 loc) · 4.58 KB
/
crypto.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as bs58check from 'bs58check'
import * as sodium from 'libsodium-wrappers'
/* eslint-disable prefer-arrow/prefer-arrow-functions */
/**
* Convert a value to hex
*
* @param value
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function toHex(value: any): string {
return Buffer.from(value).toString('hex')
}
/**
* Get the hex hash of a value
*
* @param key
*/
export async function getHexHash(key: string | Buffer | Uint8Array): Promise<string> {
await sodium.ready
return toHex(sodium.crypto_generichash(32, key))
}
/**
* Get a keypair from a seed
*
* @param seed
*/
export async function getKeypairFromSeed(seed: string): Promise<sodium.KeyPair> {
await sodium.ready
return sodium.crypto_sign_seed_keypair(sodium.crypto_generichash(32, sodium.from_string(seed)))
}
/**
* Encrypt a message with a shared key
*
* @param message
* @param sharedKey
*/
export async function encryptCryptoboxPayload(
message: string,
sharedKey: Uint8Array
): Promise<string> {
await sodium.ready
const nonce = Buffer.from(sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES))
const combinedPayload = Buffer.concat([
nonce,
Buffer.from(sodium.crypto_secretbox_easy(Buffer.from(message, 'utf8'), nonce, sharedKey))
])
return toHex(combinedPayload)
}
/**
* Decrypt a message with a shared key
*
* @param payload
* @param sharedKey
*/
export async function decryptCryptoboxPayload(
payload: Uint8Array,
sharedKey: Uint8Array
): Promise<string> {
await sodium.ready
const nonce = payload.slice(0, sodium.crypto_secretbox_NONCEBYTES)
const ciphertext = payload.slice(sodium.crypto_secretbox_NONCEBYTES)
return Buffer.from(sodium.crypto_secretbox_open_easy(ciphertext, nonce, sharedKey)).toString(
'utf8'
)
}
/**
* Encrypt a message with a public key
*
* @param payload
* @param publicKey
*/
export async function sealCryptobox(
payload: string | Buffer,
publicKey: Uint8Array
): Promise<string> {
await sodium.ready
const kxSelfPublicKey = sodium.crypto_sign_ed25519_pk_to_curve25519(Buffer.from(publicKey)) // Secret bytes to scalar bytes
const encryptedMessage = sodium.crypto_box_seal(payload, kxSelfPublicKey)
return toHex(encryptedMessage)
}
/**
* Decrypt a message with public + private key
*
* @param encryptedPayload
* @param publicKey
* @param privateKey
*/
export async function openCryptobox(
encryptedPayload: string | Buffer,
publicKey: Uint8Array,
privateKey: Uint8Array
): Promise<string> {
await sodium.ready
const kxSelfPrivateKey = sodium.crypto_sign_ed25519_sk_to_curve25519(Buffer.from(privateKey)) // Secret bytes to scalar bytes
const kxSelfPublicKey = sodium.crypto_sign_ed25519_pk_to_curve25519(Buffer.from(publicKey)) // Secret bytes to scalar bytes
const decryptedMessage = sodium.crypto_box_seal_open(
encryptedPayload,
kxSelfPublicKey,
kxSelfPrivateKey
)
return Buffer.from(decryptedMessage).toString()
}
/**
* Get an address from the public key
*
* @param publicKey
*/
export async function getAddressFromPublicKey(publicKey: string): Promise<string> {
await sodium.ready
const prefixes = {
// tz1...
edpk: {
length: 54,
prefix: Buffer.from(new Uint8Array([6, 161, 159]))
},
// tz2...
sppk: {
length: 55,
prefix: Buffer.from(new Uint8Array([6, 161, 161]))
},
// tz3...
p2pk: {
length: 55,
prefix: Buffer.from(new Uint8Array([6, 161, 164]))
}
}
let prefix: Buffer | undefined
let plainPublicKey: string | undefined
if (publicKey.length === 64) {
prefix = prefixes.edpk.prefix
plainPublicKey = publicKey
} else {
const entries = Object.entries(prefixes)
for (let index = 0; index < entries.length; index++) {
const [key, value] = entries[index]
if (publicKey.startsWith(key) && publicKey.length === value.length) {
prefix = value.prefix
const decoded = bs58check.decode(publicKey)
plainPublicKey = decoded.slice(key.length, decoded.length).toString('hex')
break
}
}
}
if (!prefix || !plainPublicKey) {
throw new Error(`invalid publicKey: ${publicKey}`)
}
const payload: Uint8Array = sodium.crypto_generichash(20, Buffer.from(plainPublicKey, 'hex'))
return bs58check.encode(Buffer.concat([prefix, Buffer.from(payload)]))
}
/**
* Get the recipient string used in the matrix message
*
* @param recipientHash
* @param relayServer
*/
export function recipientString(recipientHash: string, relayServer: string): string {
return `@${recipientHash}:${relayServer}`
}
/* eslint-enable prefer-arrow/prefer-arrow-functions */