-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathcrypto.ts
More file actions
119 lines (110 loc) · 2.92 KB
/
crypto.ts
File metadata and controls
119 lines (110 loc) · 2.92 KB
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
import { MultiFormatTypes } from '@requestnetwork/types';
import { ethers } from 'ethers';
import {
decryptWithAes256cbc,
decryptWithAes256gcm,
encryptWithAes256cbc,
encryptWithAes256gcm,
random32Bytes,
} from './crypto/crypto-wrapper';
import {
ecDecrypt,
ecEncrypt,
ecRecover,
ecSign,
getAddressFromPrivateKey,
getAddressFromPublicKey,
} from './crypto/ec-utils';
import { deepSort } from './utils';
/**
* manages crypto functions
*/
export {
decryptWithAes256cbc,
decryptWithAes256gcm,
encryptWithAes256cbc,
encryptWithAes256gcm,
random32Bytes,
ecDecrypt,
ecEncrypt,
getAddressFromPrivateKey,
getAddressFromPublicKey,
ecRecover,
ecSign,
generate32BufferKey,
generate8randomBytes,
keccak256Hash,
last20bytesOfNormalizedKeccak256Hash,
normalize,
normalizeKeccak256Hash,
};
/**
* Hashes with the keccak256 algorithm with a normalization before and formats it
*
* @notice It will sort the object by keys before hashing
*
* @param data The data to hash
* @returns The hashed data multi-formatted
*/
function normalizeKeccak256Hash(data: unknown): MultiFormatTypes.HashTypes.IHash {
return {
type: MultiFormatTypes.HashTypes.TYPE.KECCAK256,
value: keccak256Hash(normalize(data)),
};
}
/**
* Normalizes data: sorts the object by keys and convert it in string
*
* @param data The data to normalize
* @returns The normalized data
*/
function normalize(data: unknown): string {
if (data === undefined) {
return 'undefined';
}
// deeply sort data keys
const sortedData = deepSort(data);
// convert to string and lowerCase it, to be case insensitive (e.g: avoid ethereum address casing checksum)
return JSON.stringify(sortedData).toLowerCase();
}
/**
* Hashes with the keccak256 algorithm
*
* @param data The string to hash
* @returns The hashed data multi-formatted
*/
function keccak256Hash(data: string): string {
return ethers.utils.keccak256(ethers.utils.toUtf8Bytes(data));
}
/**
* Hashes with the keccak256 algorithm with a normalization before and formats it
*
* @notice It will sort the object by keys before hashing
*
* @param data The data to hash
* @returns The hashed data multi-formatted
*/
function last20bytesOfNormalizedKeccak256Hash(data: unknown): string {
const hash = keccak256Hash(normalize(data));
// eslint-disable-next-line no-magic-numbers
return `0x${hash.slice(-40)}`;
}
/**
* Generates a 32 bytes key in a base64 string
*
* @returns a random buffer of 32 bytes in a base64 string
*/
async function generate32BufferKey(): Promise<string> {
return (await random32Bytes()).toString('base64');
}
/**
* Generate 8 random bytes and return as a hexadecimal string.
* Used for salt in ETH input data.
* Example: 'ea3bc7caf64110ca'
*
* @returns a string of 8 random bytes
*/
async function generate8randomBytes(): Promise<string> {
const random32BytesHex = await random32Bytes();
return random32BytesHex.slice(0, 8).toString('hex');
}