-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.ts
More file actions
122 lines (105 loc) · 3.15 KB
/
Copy pathindex.ts
File metadata and controls
122 lines (105 loc) · 3.15 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
120
121
122
import {
exportKey as exportKeyPolyfill,
generateKey as generateKeyPolyfill,
importKey as importKeyPolyfill,
sign as signPolyfill,
verify as verifyPolyfill,
} from './polyfill';
import { isEd25519Algorithm } from './utils/is-ed25519-algorithm';
/**
* Adds Ed25519 support to Web Crypto API's native methods.
*
* Based on the following libraries:
* https://github.com/solana-labs/solana-web3.js/tree/master/packages/webcrypto-ed25519-polyfill
* https://github.com/yoursunny/webcrypto-ed25519.
*
*/
export function install() {
const { subtle } = globalThis.crypto;
Object.defineProperty(globalThis, 'isSecureContext', {
value: true,
writable: true,
configurable: true,
});
/**
* Override `SubtleCrypto#generateKey`
*/
Object.defineProperty(subtle, 'generateKey', {
/* eslint-disable-next-line no-restricted-globals */
value: async (...args: Parameters<SubtleCrypto['generateKey']>) => {
const algorithm = args[0];
if (!isEd25519Algorithm(algorithm)) {
return await globalThis.crypto.subtle.generateKey(...args);
}
return await generateKeyPolyfill(...args);
},
writable: true,
configurable: true,
});
/**
* Override `SubtleCrypto#exportKey`
*/
Object.defineProperty(subtle, 'exportKey', {
/* eslint-disable-next-line no-restricted-globals */
value: async (...args: Parameters<SubtleCrypto['exportKey']>) => {
const key = args[1];
if (!isEd25519Algorithm(key.algorithm)) {
return await globalThis.crypto.subtle.exportKey(...args);
}
return await exportKeyPolyfill(...args);
},
writable: true,
configurable: true,
});
/**
* Override `SubtleCrypto#sign`
*/
Object.defineProperty(subtle, 'sign', {
/* eslint-disable-next-line no-restricted-globals */
value: async (...args: Parameters<SubtleCrypto['sign']>) => {
const [algorithm, key] = args;
if (
!isEd25519Algorithm(algorithm) ||
!isEd25519Algorithm(key.algorithm)
) {
return await globalThis.crypto.subtle.sign(...args);
}
return await signPolyfill(...args);
},
writable: true,
configurable: true,
});
/**
* Override `SubtleCrypto#verify`
*/
Object.defineProperty(subtle, 'verify', {
/* eslint-disable-next-line no-restricted-globals */
value: async (...args: Parameters<SubtleCrypto['verify']>) => {
const [algorithm, key] = args;
if (
!isEd25519Algorithm(algorithm) ||
!isEd25519Algorithm(key.algorithm)
) {
return await globalThis.crypto.subtle.verify(...args);
}
return await verifyPolyfill(...args);
},
writable: true,
configurable: true,
});
/**
* Override `SubtleCrypto#importKey`
*/
Object.defineProperty(subtle, 'importKey', {
/* eslint-disable-next-line no-restricted-globals */
value: async (...args: Parameters<SubtleCrypto['importKey']>) => {
const algorithm = args[2];
if (!isEd25519Algorithm(algorithm)) {
return await globalThis.crypto.subtle.importKey(...args);
}
return await importKeyPolyfill(...args);
},
writable: true,
configurable: true,
});
}