|
|
@@ -18,8 +18,9 @@ const TextEncoder = util.TextEncoder;
|
|
|
const util_1 = require("./util");
|
|
|
const webclient_node_1 = __importDefault(require("./webclient-node"));
|
|
|
class BankClient {
|
|
|
- constructor(urlBase, storage = new storage_1.Storage('bankClient'), webClient = new webclient_node_1.default()) {
|
|
|
+ constructor(urlBase, ipfsUrlBase, storage = new storage_1.Storage('bankClient'), webClient = new webclient_node_1.default()) {
|
|
|
this.urlBase = urlBase;
|
|
|
+ this.ipfsUrlBase = ipfsUrlBase;
|
|
|
this.storage = storage;
|
|
|
this.webClient = webClient;
|
|
|
}
|
|
|
@@ -126,13 +127,40 @@ class BankClient {
|
|
|
formData[attr] = params[attr];
|
|
|
}
|
|
|
}
|
|
|
- console.log('formData', formData);
|
|
|
+ // console.log('formData', formData);
|
|
|
const uploadResponse = yield this.webClient.requestJSON({
|
|
|
formData,
|
|
|
method: 'POST',
|
|
|
url
|
|
|
});
|
|
|
- console.log('uploadResponse', uploadResponse);
|
|
|
+ // console.log('uploadResponse', uploadResponse);
|
|
|
+ return uploadResponse.hash;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ uploadSlimJSON(item) {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ const url = this.urlBase + '/bank/upload/slim';
|
|
|
+ const uploadResponse = yield this.webClient.requestJSON({
|
|
|
+ body: item,
|
|
|
+ method: 'POST',
|
|
|
+ url
|
|
|
+ });
|
|
|
+ // console.log('uploadResponse', uploadResponse);
|
|
|
+ return uploadResponse.hash;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ uploadSlimText(item) {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ const url = this.urlBase + '/bank/upload/slim';
|
|
|
+ const uploadResponse = JSON.parse(yield this.webClient.request({
|
|
|
+ body: item,
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'text/plain'
|
|
|
+ },
|
|
|
+ method: 'POST',
|
|
|
+ url
|
|
|
+ }));
|
|
|
+ // console.log('uploadResponse', uploadResponse);
|
|
|
return uploadResponse.hash;
|
|
|
});
|
|
|
}
|
|
|
@@ -148,6 +176,78 @@ class BankClient {
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
+ retrievePrivate(peerAddr, topic) {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ const nonce = yield this.getNonce();
|
|
|
+ const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
|
|
|
+ _date: new Date().toISOString(),
|
|
|
+ _nonce: nonce
|
|
|
+ }));
|
|
|
+ const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(peerAddr) + '/' + encodeURIComponent(topic);
|
|
|
+ const result = yield this.webClient.request({
|
|
|
+ body: JSON.stringify(retrieveRequest),
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'application/json'
|
|
|
+ },
|
|
|
+ method: 'POST',
|
|
|
+ url: topicURL
|
|
|
+ });
|
|
|
+ return result;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ appendPrivate(peerAddr, topic, hash) {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ const payload = yield this.makePlaintextPayload(hash);
|
|
|
+ const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(peerAddr) + '/' + encodeURIComponent(topic);
|
|
|
+ const result = yield this.webClient.request({
|
|
|
+ body: JSON.stringify(payload),
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'application/json'
|
|
|
+ },
|
|
|
+ method: 'PUT',
|
|
|
+ url: topicURL
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ getOrCreateContact(peerAddr, contact, type) {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ const contactList = yield this.retrievePrivate(peerAddr, '📇');
|
|
|
+ const itemList = yield this.getItemsForCommaList(contactList);
|
|
|
+ const contactHash = yield this.getContactHash(contact, type);
|
|
|
+ // console.log('contact hash for', contact, type, 'is', contactHash);
|
|
|
+ const existing = itemList.filter(item => item.contactHash === contactHash)[0];
|
|
|
+ if (existing != null) {
|
|
|
+ return existing;
|
|
|
+ }
|
|
|
+ const newItem = {
|
|
|
+ contact,
|
|
|
+ contactHash,
|
|
|
+ type,
|
|
|
+ };
|
|
|
+ const newItemHash = yield this.uploadSlimJSON(newItem);
|
|
|
+ yield this.appendPrivate(peerAddr, '📇', newItemHash);
|
|
|
+ return newItem;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ getContactHash(contact, type) {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ const value = type + ':' + contact;
|
|
|
+ return yield this.uploadSlimText(value);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ getItemsForCommaList(commaList) {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ // console.log('commaList', commaList);
|
|
|
+ const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
|
|
|
+ // console.log('itemHashes', itemHashes);
|
|
|
+ return yield Promise.all(itemHashes.map(itemId => {
|
|
|
+ return this.webClient.requestJSON({
|
|
|
+ method: 'get',
|
|
|
+ url: this.ipfsUrlBase + '/ipfs/' + itemId,
|
|
|
+ });
|
|
|
+ }));
|
|
|
+ });
|
|
|
+ }
|
|
|
getPriv() {
|
|
|
if (!this.privateKey) {
|
|
|
throw new Error('missing private key');
|