-
Notifications
You must be signed in to change notification settings - Fork 28
/
start.js
255 lines (226 loc) · 7.82 KB
/
start.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import './start.html'
import CryptoJS from 'crypto-js';
/* global LocalStore */
/* global selectedNetwork */
/* global XMSS_OBJECT */
/* global DEFAULT_NETWORKS */
/* global SHOR_PER_QUANTA */
/* global wrapMeteorCall */
/* global nodeReturnedValidResponse */
/* global otsIndexUsed */
let additional_text_max_length_value = 45
function createMessageTxn() {
const finalNotarisation = document.getElementById('message').value
const txnFee = document.getElementById('fee').value
const otsKey = document.getElementById('otsKey').value
// Fail if OTS Key reuse is detected
if(otsIndexUsed(LocalStore.get('otsBitfield'), otsKey)) {
$('#generating').hide()
$('#otsKeyReuseDetected').modal('show')
return
}
// Convert strings to bytes
const pubKey = hexToBytes(XMSS_OBJECT.getPK())
const messageBytes = hexToBytes(finalNotarisation)
// Construct request
const request = {
message: messageBytes,
fee: txnFee * SHOR_PER_QUANTA,
xmssPk: pubKey,
network: selectedNetwork(),
}
wrapMeteorCall('createMessageTxn', request, (err, res) => {
if (err) {
LocalStore.set('documentNotarisationError', err)
$('#documentNotarisationFailed').show()
$('#notariseForm').hide()
} else {
const confirmation = {
hash: res.txnHash,
message: bytesToString(res.response.extended_transaction_unsigned.tx.message.message_hash),
message_hex: bytesToHex(res.response.extended_transaction_unsigned.tx.message.message_hash),
fee: res.response.extended_transaction_unsigned.tx.fee / SHOR_PER_QUANTA,
otsKey: otsKey,
file_name: LocalStore.get('notaryDocumentName'),
hash_function: LocalStore.get('notaryHashFunction'),
hash: LocalStore.get('notaryFileHash'),
additional_text: LocalStore.get('notaryAdditionalText'),
}
if (nodeReturnedValidResponse(request, confirmation, 'createMessageTxn')) {
LocalStore.set('notariseCreationConfirmation', confirmation)
LocalStore.set('notariseCreationConfirmationResponse', res.response)
// Send to confirm page.
const params = { }
const path = FlowRouter.path('/tools/notarise/confirm', params)
FlowRouter.go(path)
} else {
$('#invalidNodeResponse').modal('show')
}
}
})
}
function notariseDocument() {
$('#documentNotarisationFailed').hide()
let notaryDocuments = $('#notaryDocument').prop('files')
const notaryDocument = notaryDocuments[0]
const hashFunction = document.getElementById('hashFunction').value
const additional_text = document.getElementById('additional_text').value
const reader = new FileReader()
reader.onloadend = function() {
try {
let notarisation = 'AFAFA'
let fileHash
// Convert FileReader ArrayBuffer to WordArray first
var resultWordArray = CryptoJS.lib.WordArray.create(reader.result);
if(hashFunction == "SHA256") {
fileHash = CryptoJS.SHA256(resultWordArray).toString(CryptoJS.enc.Hex);
notarisation = notarisation + '2' + fileHash
}
// Convert free form text to hex
var additionalTextBytes = stringToBytes(additional_text)
var additionalTextHex = bytesToHex(additionalTextBytes)
// Construct final hex string for notarisation
var finalNotarisation = notarisation + additionalTextHex
// Set message field to document notarisation string
document.getElementById('message').value = finalNotarisation
// Set the filename in local store for later display in UI
LocalStore.set('notaryDocumentName', notaryDocument.name)
LocalStore.set('notaryAdditionalText', additional_text)
LocalStore.set('notaryHashFunction', hashFunction)
LocalStore.set('notaryFileHash', fileHash)
// Create a message txn with this data
createMessageTxn()
} catch (err) {
console.log(err)
// Invalid file format
LocalStore.set('documentNotarisationError', 'Unable to open Document - Are you sure you selected a document to notarise?')
$('#documentNotarisationFailed').show()
$('#generating').hide()
}
}
// Verify user selected a document to notarise
if (notaryDocument === undefined) {
LocalStore.set('documentNotarisationError', 'Unable to open Document - Are you sure you selected a document to notarise?')
$('#documentNotarisationFailed').show()
$('#generating').hide()
} else {
console.log('reading file ', notaryDocument)
reader.readAsArrayBuffer(notaryDocument)
}
}
// Function to initialise form validation
function initialiseFormValidation() {
let validationRules = {}
validationRules['additional_text'] = {
id: 'additional_text',
rules: [
{
type: 'maxLength[' + additional_text_max_length_value + ']',
prompt: 'The max length of the additional message is ' + additional_text_max_length_value + ' bytes.',
},
],
}
// Now set fee and otskey validation rules
validationRules['fee'] = {
id: 'fee',
rules: [
{
type: 'empty',
prompt: 'You must enter a fee',
},
{
type: 'number',
prompt: 'Fee must be a number',
},
],
}
validationRules['otsKey'] = {
id: 'otsKey',
rules: [
{
type: 'empty',
prompt: 'You must enter an OTS Key Index',
},
{
type: 'number',
prompt: 'OTS Key Index must be a number',
},
],
}
// Initliase the form validation
$('.ui.form').form({
fields: validationRules,
})
}
Template.appNotariseStart.onRendered(() => {
// Initialise dropdowns
$('.ui.dropdown').dropdown()
// Initialise Form Validation
initialiseFormValidation()
// Get wallet balance
getBalance(getXMSSDetails().address, function() {
// Show warning is otsKeysRemaining is low
if(LocalStore.get('otsKeysRemaining') < 50) {
// Shown low OTS Key warning modal
$('#lowOtsKeyWarning').modal('transition', 'disable').modal('show')
}
})
})
Template.appNotariseStart.events({
'submit #notariseForm': (event) => {
event.preventDefault()
event.stopPropagation()
$('#generating').show()
setTimeout(() => { notariseDocument() }, 200)
},
'change #hashFunction': (event) => {
const selectedFunction = document.getElementById('hashFunction').value
if(selectedFunction == "SHA256") {
additional_text_max_length_value = 45
document.getElementById('additional_text_max_length').innerHTML = '(Max Length: 45)'
}
initialiseFormValidation()
}
})
Template.appNotariseStart.helpers({
transferFrom() {
const transferFrom = {}
transferFrom.balance = LocalStore.get('transferFromBalance')
transferFrom.address = hexOrB32(LocalStore.get('transferFromAddress'))
return transferFrom
},
transactionConfirmation() {
const confirmation = LocalStore.get('transactionConfirmation')
return confirmation
},
transactionConfirmationAmount() {
const confirmationAmount = LocalStore.get('transactionConfirmationAmount')
return confirmationAmount
},
transactionConfirmationFee() {
const transactionConfirmationFee = LocalStore.get('transactionConfirmationFee')
return transactionConfirmationFee
},
transactionGenerationError() {
const error = LocalStore.get('transactionGenerationError')
return error
},
otsKeyEstimate() {
const otsKeyEstimate = LocalStore.get('otsKeyEstimate')
return otsKeyEstimate
},
otsKeysRemaining() {
const otsKeysRemaining = LocalStore.get('otsKeysRemaining')
return otsKeysRemaining
},
documentNotarisationError() {
const documentNotarisationError = LocalStore.get('documentNotarisationError')
return documentNotarisationError
},
nodeExplorerUrl() {
if ((LocalStore.get('nodeExplorerUrl') === '') || (LocalStore.get('nodeExplorerUrl') === null)) {
return DEFAULT_NETWORKS[0].explorerUrl
}
return LocalStore.get('nodeExplorerUrl')
},
})