-
Notifications
You must be signed in to change notification settings - Fork 0
/
rie.js
260 lines (246 loc) · 10.6 KB
/
rie.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
256
257
258
259
260
// Rich Ipfs Editor
import {CKE5_Page} from './cke5.js';
// just in case you find a good way to pass this in
const sourceAccountSecret = null;
// Parse the url for Stellar account number and data entry name where document's ipfs address is saved
let queryParameters = {}; let entries = [];
const segments = window.location.href.split('?');
if(segments.length === 2)
entries = segments.pop().split('&').map(pair => pair.split('='));
// check you've parsed key=value pairs
if(entries.reduce((acc, entry) => acc && (entry.length === 2), true))
// then make Object from them
queryParameters = Object.fromEntries(entries);
if(!Object.keys(queryParameters).length && segments.length === 2)
throw new Error(`could not parse urlencoded parameters from entries `, entries)
// create a SigningAccount, with keys if user agrees to sign
// a transaction signature is used as the key seed
const sourceAccount = await CKE5_Page.SigningAccount.checkForWallet();
console.log(`created sourceAccount: `, sourceAccount);
await sourceAccount.ready;
//if(sourceAccount.canSign)
addOption('accountId', sourceAccount.id);
if(queryParameters?.accountId && queryParameters.accountId !== sourceAccount.id)
addOption('accountId', queryParameters.accountId, true);
// addOption is a utility function for constructing option lists for the ui
function addOption(elId, value, selected=false, label=true){
const option = document.createElement('option');
option.label = label && value.length > 2*7+3 ? `${value.slice(0,7)}...${value.slice(-7)}` : value;
option.value = value;
option.selected = selected;
document.getElementById(elId).appendChild(option);
}
function BlockParameters(queryParameters){
this.source = {
init: function(queryParameters, blockParameters){
this.el.addEventListener('change', function(e){
document.getElementById('addresses').innerHTML = '';
if(e.target.value === 'localStorage'){
for(const key of Object.keys(localStorage))
addOption('addresses', key, false, false);
CKE5_Page.source.url = false;
}
else if(e.target.value === 'ipfs')
CKE5_Page.source.url = cid => `https://motia.com/ipfs/${cid.toString()}/`;
else
console.error(`oops, didn't expect to be here`);
console.log(`have set source url to ${CKE5_Page.source.url}`);
blockParameters.sink.el.value = e.target.value;
blockParameters.copyIt.el.disabled = blockParameters.inKeys.value === blockParameters.outKeys.value;
})
}
};
this.inKeys = {
init: function(queryParameters, blockParameters){
this.el.addEventListener('change', e => {
document.getElementById('addFrom').hidden = e.target.value !== 'add';
blockParameters.copyIt.el.disabled = blockParameters.inKeys.value === blockParameters.outKeys.value &&
blockParameters.source.value === blockParameters.sink.value;
//document.getElementById('addressInput').dispatchEvent(new Event('change'));
});
document.getElementById('addFrom').addEventListener('change', e => addOption('inKeys', e.target.value));
}
};
this.addressInput = {
init: function(queryParameters, blockParameters){
this.el.addEventListener('change', () => blockParameters.dataEntryLabel.el.value = '');
blockParameters.source.el.dispatchEvent(new Event('change'));
}
};
this.sink = {
init: function(queryParameters, blockParameters){
this.el.addEventListener('change', function(e){
if(e.target.value === 'ipfs')
CKE5_Page.sink.url = cid => typeof cid === 'string' ? `https://motia.com/api/v1/ipfs/pin/add?arg=${cid}` :
`https://motia.com/api/v1/ipfs/block/put?cid-codec=${CKE5_Page.codecForCID(cid).name}`;
else
CKE5_Page.sink.url = false;
console.log(`have set sink url to: `, CKE5_Page.sink.url);
const copyReady = e.target.value === blockParameters.source.value;
blockParameters.copyIt.el.disabled = copyReady;
if(!copyReady)
blockParameters.dataEntryLabel.el.value = '';
})
}
};
this.outKeys = {
init: function(queryParameters, blockParameters){
this.el.addEventListener('change', e => {
document.getElementById('addTo').hidden = e.target.value !== 'add';
blockParameters.copyIt.el.disabled = blockParameters.inKeys.value === blockParameters.outKeys.value &&
blockParameters.source.value === blockParameters.sink.value;
});
document.getElementById('addTo').addEventListener('change', e => addOption('outKeys', e.target.value));
}
};
this.traverse = {
init: function(){
Object.defineProperty(this, 'value', {
get: function(){
return !!parseInt(this.el.value)
},
set: function(val){
if(val)
this.el.value = '1';
else
this.el.value = '0';
}
});
}
};
this.accountId = {
init: async function(){
this.el.addEventListener('change', async function(e){
const account = await CKE5_Page.SigningAccount.load(e.target.value);
document.getElementById('dataEntries').innerHTML = '';
for(const key of Object.keys(account.data))
addOption('dataEntries', key, false, false);
});
}
};
this.dataEntryLabel = {
init: function(queryParameters, blockParameters){
if(queryParameters?.dataEntryLabel)
this.el.value = queryParameters.dataEntryLabel;
this.el.placeholder = `name of hash`;
this.el.addEventListener('change', async function(e){
if(Array.from(document.getElementById('dataEntries').children).map(option => option.value).includes(e.target.value)){
const hash = await CKE5_Page.SigningAccount.dataEntry(blockParameters.accountId.value, e.target.value);
console.log(`read hash ${hash} from account ${blockParameters.accountId.value} label ${e.target.value}`);
blockParameters.addressInput.el.value = hash;
}
});
}
};
this.nameIt = {
init: function(queryParameters, blockParameters){
this.el.addEventListener('change', async function(e){
const bool = !!parseInt(e.target.value);
Array.from(document.getElementsByClassName('hashName')).map(function(el){
el.hidden = !bool;
})
if(bool)
blockParameters.accountId.el.dispatchEvent(new Event('change'));
console.log(`blockParameters is `, blockParameters);
});
Object.defineProperty(this, 'value', {
get: function(){
return !!parseInt(this.el.value)
}
});
}
};
this.readIt = {
init: function(queryParameters, blockParameters){
this.el.addEventListener('click', function(e){
CKE5_Page.openPage(sourceAccount, blockParameters.addressInput.value);
})
}
};
this.copyIt = {
init: function(queryParameters, blockParameters){
this.el.disabled = blockParameters.source.value === blockParameters.sink.value;
this.el.addEventListener('click', async function(e){
console.log(`Now is the time to copy ${CKE5_Page.blockParameters.traverse.value?'all pages of':''} ${e.target.value} to ${CKE5_Page.blockParameters.sink.value}!`);
//const inKeys = await sourceAccount.keys.readFrom(CKE5_Page.blockParameters.inKeys.value);
//const outKeys = await sourceAccount.keys.writeTo(CKE5_Page.blockParameters.outKeys.value);
const copyOpts = {
signingAccount: sourceAccount,
address: e.target.value,
inKeys: await sourceAccount.keys.readFrom(CKE5_Page.blockParameters.inKeys.value),
outKeys: await sourceAccount.keys.writeTo(CKE5_Page.blockParameters.outKeys.value),
traverse: CKE5_Page.blockParameters.traverse.value,
dataRootLabel:
blockParameters.nameIt.value ?
blockParameters.dataEntryLabel.value :
''
}
return CKE5_Page.copy(copyOpts);
//const copyRoot = await CKE5_Page.fromCID(sourceAccount, e.target.value, keys);
//console.log(`copyIt opened root `, copyRoot);
})
}
}
Object.defineProperty(this, 'persistAll', {
get: () => {
console.log(`persist all of this? `, this);
const {source, inKeys, sink, outKeys, traverse} = this;
const truther = {
'0': sink.value !== source.value,
'1': inKeys.value !== outKeys.value,
'2': traverse.value
}
//console.log(`persistAll is ${sink.value !== source.value || inKeys.value !== outKeys.value && traverse.value} with `, truther);
console.log(`persistAll is ${traverse.value && sink.value !== source.value} with `, truther);
return sink.value !== source.value // || inKeys.value !== outKeys.value &&
}
});
for(const key of Object.keys(this)){
this[key].el = document.getElementById(key);
if(Object.hasOwn(queryParameters, key))
Array.from(this[key].el.children).map(child => child.selected = queryParameters[key] === child.value);
Object.defineProperty(this[key], 'value', {
get: function(){
return this.el.value
},
set: function(value){
return this.el.value = value
},
configurable: true,
enumerable: false,
});
}
for(const key of Object.keys(this))
this[key].init(queryParameters, this);
}
CKE5_Page.blockParameters = new BlockParameters(queryParameters);
CKE5_Page.openPage(sourceAccount);
// The next two functions are used debugging
window.checkForTestPages = async function(pages){
await window.getAllPages();
const found = []
for(const page of window.lsNames)
if(pages.includes(page.name))
found.push(page);
console.table(found.reduce((acc, {address, ...x}) => { acc[address] = x; return acc}, {}))
return found
}
window.getAllPages = async function(log=false){
window.lsNames = []; // localStorage entry names
for(const [key, value] of Object.entries(localStorage)){
/*let keys = null;
if(key.startsWith('bafk'))
keys = await sourceAccount.keys.readFrom('self');*/
const keys = key.startsWith('bafk') ? await sourceAccount.keys.readFrom('self') : null;
const page = await CKE5_Page.fromCID(sourceAccount, key, keys);
window.lsNames.push({
address: key, name: page.name,
created: page.value?.created_at,
modified: page.value?.modified_at,
updated: page.value?.updated_at
});
}
if(log)
console.table(window.lsNames.reduce((acc, {address, ...x}) => { acc[address] = x; return acc}, {}))
}
window.getAllPages()