-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.html
178 lines (154 loc) · 4.65 KB
/
crypto.html
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
<!DOCTYPE html>
<html>
<!--
Copyright (c) 2023 Social Commerce Engine Inc. All rights reserved.
This HTML document contains code developed by Social Commerce Engine Inc
and/or its developers, and is protected by copyright law.
Any unauthorized use, reproduction, modification or distribution
of this code is strictly prohibited.
Social Commerce Engine Inc and its developers are not liable for any loss
or damage caused by the use of this code, and assume no
responsibility for any errors or omissions in the content of this
document.
For inquiries regarding licensing, distribution or support of this
code, please contact support@socialstore.app
-->
<script
src="https://cdn.jsdelivr.net/gh/yngfoxx/web-crypto-hooks/subtle/ecdsa.js"
></script>
<script>
var cert;
(async () => {
/**
* @typedef MessagePayload
* @type {object}
* @property {string} id
* @property {string} data
* @property {string} hash
* @property {string} signature
*/
/**
* @typedef MessageObject
* @type {object}
* @property {'INIT' | 'HASH' | 'VERIFY'} action
* @property {MessagePayload} payload
*/
/**
* @typedef SessionData
* @type {object}
* @property {string} publicKey
*/
/**
* @typedef HashedData
* @type {object}
* @property {string} id
* @property {string} hash
* @property {string} signature
*/
/**
* @typedef VerifyHashData
* @type {object}
* @property {string} id
* @property {boolean} valid
*/
/**
* @typedef CertManagerSessionMessage
* @type {object}
* @property {'session'} type
* @property {SessionData} data
*/
/**
* @typedef CertManagerHashMessage
* @type {object}
* @property {'hash'} type
* @property {HashedData} data
*/
/**
* @typedef CertManagerVerifyMessage
* @type {object}
* @property {'verify'} type
* @property {VerifyHashData} data
*/
/**
* @typedef {(
* | CertManagerSessionMessage
* | CertManagerHashMessage
* | CertManagerVerifyMessage
* )} CertManagerMessage
*/
const currentWindow = 'ReactNativeWebView' in window ?
window.ReactNativeWebView :
window
if ('subtle' in window.crypto === false) {
currentWindow.postMessage(JSON.stringify({
message: 'not-supported',
}))
return;
}
/**
* ? ECDSA Certificate Manager
* @param {{data: string}} msg
*/
const messageHandler = async msg => {
if (!msg || !msg?.data) return;
/**
* @type {MessageObject}
*/
const messagePayload = JSON.parse(msg.data)
const {
action,
payload,
} = messagePayload;
switch (action) {
case 'INIT':
cert = await useECDSA()
// Export public key in IEEE P1363 format
const pub = cert.tmpKeys.publicKey
crypto.subtle.exportKey('raw', pub).then(pbk => {
/** @type {CertManagerSessionMessage} */
const v = {
type: 'session',
data: {
publicKey: cert.buf2hex(new Uint8Array(pbk).buffer)
}
}
currentWindow.postMessage(JSON.stringify(v))
}).catch(alert)
break;
case 'HASH':
const hash = cert.buf2hex(cert.hash(payload.data).buffer)
cert.sign(new Uint8Array(cert.hex2buf(hash))).then(sig => {
/** @type {CertManagerHashMessage} */
const v = {
type: 'hash',
data: {
hash,
id: payload.id,
signature: cert.buf2hex(new Uint8Array(sig).buffer)
}
}
currentWindow.postMessage(JSON.stringify(v))
}).catch(alert)
break;
case 'VERIFY':
cert.verify(
new Uint8Array(cert.hex2buf(payload.hash)),
new Uint8Array(cert.hex2buf(payload.signature))
).then(valid => {
/** @type {CertManagerVerifyMessage} */
const v = {
type: 'verify',
data: {
valid,
id: payload.id,
}
}
currentWindow.postMessage(JSON.stringify(v))
}).catch(alert)
break;
}
}
window.addEventListener('message', messageHandler)
})()
</script>
</html>