|
@@ -216,9 +216,15 @@ class BankClient {
|
|
|
return result;
|
|
return result;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
- appendPrivate(peerAddr, topic, hash) {
|
|
|
|
|
|
|
+ appendPrivate(peerAddr, topic, hash, replaceHash) {
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
- const payload = yield this.makePlaintextPayload(hash);
|
|
|
|
|
|
|
+ const nonce = yield this.getNonce();
|
|
|
|
|
+ const payload = yield this.makePlaintextPayload(JSON.stringify({
|
|
|
|
|
+ _date: new Date().toISOString(),
|
|
|
|
|
+ _nonce: nonce,
|
|
|
|
|
+ hash,
|
|
|
|
|
+ replaceHash
|
|
|
|
|
+ }));
|
|
|
const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(peerAddr) + '/' + encodeURIComponent(topic);
|
|
const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(peerAddr) + '/' + encodeURIComponent(topic);
|
|
|
const result = yield this.webClient.request({
|
|
const result = yield this.webClient.request({
|
|
|
body: JSON.stringify(payload),
|
|
body: JSON.stringify(payload),
|
|
@@ -230,43 +236,62 @@ class BankClient {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
- getOrCreateContact(peerAddr, contact, type) {
|
|
|
|
|
|
|
+ getOrCreateContact(peerId, contactAddr) {
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
- const contactList = yield this.retrievePrivate(peerAddr, '📇');
|
|
|
|
|
|
|
+ const contactList = yield this.retrievePrivate(peerId, '📇');
|
|
|
const itemList = yield this.getItemsForCommaList(contactList);
|
|
const itemList = yield this.getItemsForCommaList(contactList);
|
|
|
- const contactHash = yield this.getContactHash(contact, type);
|
|
|
|
|
// console.log('contact hash for', contact, type, 'is', contactHash);
|
|
// console.log('contact hash for', contact, type, 'is', contactHash);
|
|
|
- const existing = itemList.filter(item => item.contactHash === contactHash)[0];
|
|
|
|
|
|
|
+ const existing = itemList.filter(item => item.addrs && item.addrs.includes(contactAddr))[0];
|
|
|
if (existing != null) {
|
|
if (existing != null) {
|
|
|
return existing;
|
|
return existing;
|
|
|
}
|
|
}
|
|
|
const newItem = {
|
|
const newItem = {
|
|
|
- contact,
|
|
|
|
|
- contactHash,
|
|
|
|
|
- type,
|
|
|
|
|
|
|
+ addrs: [
|
|
|
|
|
+ contactAddr
|
|
|
|
|
+ ],
|
|
|
|
|
+ id: util_1.uuid()
|
|
|
};
|
|
};
|
|
|
const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
|
- yield this.appendPrivate(peerAddr, '📇', newItemHash);
|
|
|
|
|
|
|
+ yield this.appendPrivate(peerId, '📇', newItemHash);
|
|
|
return newItem;
|
|
return newItem;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
- getContactHash(contact, type) {
|
|
|
|
|
|
|
+ getContactById(peerId, contactId) {
|
|
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
|
+ const contactList = yield this.retrievePrivate(peerId, '📇');
|
|
|
|
|
+ const itemList = yield this.getItemsForCommaList(contactList);
|
|
|
|
|
+ const existing = itemList.filter(item => item.id === contactId)[0];
|
|
|
|
|
+ if (!existing) {
|
|
|
|
|
+ throw new Error('Cannot find contact with id ' + contactId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return existing;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ updateContact(peerId, contactId, newProperties) {
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
- const value = type + ':' + contact;
|
|
|
|
|
- return yield this.uploadSlimText(value);
|
|
|
|
|
|
|
+ const existing = yield this.getContactById(peerId, contactId);
|
|
|
|
|
+ const newProps = util_1.mergeDeep({}, newProperties);
|
|
|
|
|
+ delete newProps.id;
|
|
|
|
|
+ const newItem = util_1.mergeDeep(existing, newProps);
|
|
|
|
|
+ delete newItem.hash;
|
|
|
|
|
+ const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
|
|
|
+ yield this.appendPrivate(peerId, '📇', newItemHash, existing.hash);
|
|
|
|
|
+ return yield this.getContactById(peerId, contactId);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
getItemsForCommaList(commaList) {
|
|
getItemsForCommaList(commaList) {
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
- // console.log('commaList', commaList);
|
|
|
|
|
const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
|
|
const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
|
|
|
- // console.log('itemHashes', itemHashes);
|
|
|
|
|
- return yield Promise.all(itemHashes.map(itemId => {
|
|
|
|
|
|
|
+ const items = yield Promise.all(itemHashes.map(itemId => {
|
|
|
return this.webClient.requestJSON({
|
|
return this.webClient.requestJSON({
|
|
|
method: 'get',
|
|
method: 'get',
|
|
|
url: this.ipfsUrlBase + '/ipfs/' + itemId,
|
|
url: this.ipfsUrlBase + '/ipfs/' + itemId,
|
|
|
});
|
|
});
|
|
|
}));
|
|
}));
|
|
|
|
|
+ for (const item of items) {
|
|
|
|
|
+ item.hash = itemHashes.shift();
|
|
|
|
|
+ }
|
|
|
|
|
+ return items;
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
getPriv() {
|
|
getPriv() {
|