-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
145 lines (130 loc) · 3.6 KB
/
main.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
//Intial function
'Use strict';
(async function() {
try {
$('#send-message').click(function() {
sendMessage();
});
const app = {
name: 'Feedy',
id: 'joe',
version: '1',
vendor: 'feedy.joe'
};
let appHandle = await window.safeApp.initialise(app);
auth = await window.safeApp.connect(appHandle);
Materialize.toast(' App Token: ' + auth, 3000, 'rounded');
} catch (err) {
console.error(err);
} finally {
authorised = false;
getMessages();
}
})();
async function getMessages() {
try {
let feedyHash = await window.safeCrypto.sha3Hash(auth, 'feedy');
let feedyHandle = await window.safeMutableData.newPublic(auth, feedyHash, 54321);
let entriesHandle = await window.safeMutableData.getEntries(feedyHandle);
loadingMessage.innerHTML = '';
messages.innerHTML = '';
let time = new Date().getTime();
window.safeMutableDataEntries.forEach(
entriesHandle,
(keysobject, value) => {
let key = uintToString(keysobject);
if (
value.buf.toString().length < 300 &&
value.buf.toString() !== '' &&
parseInt(key) < time &&
parseInt(key).toString().length === 13
// key.length === 13
) {
console.log(value.buf.toString());
i = key;
let date = new Date(parseInt(key));
let timestamp =
('0' + date.getDate()).slice(-2) +
'/' +
('0' + (date.getMonth() + 1)).slice(-2) +
'/' +
date.getFullYear() +
' ' +
('0' + date.getHours()).slice(-2) +
':' +
('0' + date.getMinutes()).slice(-2);
$('#messages').append(
'<div class="card-panel accent-colour item"><p class="primary-text-colour">' +
value.buf.toString() +
' <br>' +
timestamp +
'</p></div>'
);
}
window.scrollTo(0, document.body.scrollHeight);
},
err => {
console.error(err);
}
);
window.safeMutableDataEntries.free(entriesHandle);
} catch (err) {
console.error(err);
}
}
async function authorise() {
try {
if (authorised !== true) {
window.safeApp.free(auth);
const app = {
name: 'Feedy',
id: 'joe',
version: '1',
vendor: 'feedy.joe'
};
const permissions = {
_public: ['Read']
};
const owncontainer = {
own_container: true
};
let appHandle = await window.safeApp.initialise(app);
let authURI = await window.safeApp.authorise(appHandle, permissions, owncontainer);
let authorisedAppHandle = await window.safeApp.connectAuthorised(appHandle, authURI);
auth = authorisedAppHandle;
authorised = true;
Materialize.toast('Authorised App Token: ' + auth, 3000, 'rounded');
getMessages();
return auth;
}
} catch (err) {
console.error(err);
}
}
async function sendMessage() {
try {
if (authorised !== true) {
const auth = await authorise();
}
let time = new Date().getTime().toString();
let feedyHash = await window.safeCrypto.sha3Hash(auth, 'feedy');
let feedyHandle = await window.safeMutableData.newPublic(auth, feedyHash, 54321);
let mutationHandle = await window.safeMutableData.newMutation(auth);
window.safeMutableDataMutation.insert(mutationHandle, time, messagearea.value);
window.safeMutableData.applyEntriesMutation(feedyHandle, mutationHandle);
window.safeMutableDataMutation.free(mutationHandle);
window.safeMutableData.free(feedyHandle);
messagearea.value = '';
} catch (err) {
console.error(err);
} finally {
Materialize.toast('Message has been sent to the network', 3000, 'rounded');
setTimeout(function() {
getMessages();
}, 2000);
}
}
function uintToString(key) {
let uintArray = new Uint8Array(Object.values(key));
return new TextDecoder('utf-8').decode(uintArray);
}