user 6 anos atrás
pai
commit
1204535e41
7 arquivos alterados com 82 adições e 30 exclusões
  1. 5 0
      lib/index.d.ts
  2. 24 4
      lib/index.js
  3. 1 1
      lib/index.js.map
  4. 12 10
      lib/sample.js
  5. 1 1
      lib/sample.js.map
  6. 26 4
      src/index.ts
  7. 13 10
      src/sample.ts

+ 5 - 0
lib/index.d.ts

@@ -32,6 +32,11 @@ export declare class BankClient {
     updateContact(peerId: string, contactId: string, newProperties: any): Promise<any>;
     getContentItemByHash(hash: string): Promise<ContentItem>;
     getItemsForCommaList(commaList: string): Promise<any[]>;
+    parseItemHash(itemHash: string): {
+        type: string;
+        timestamp: string | null;
+        hash: string;
+    };
     private connectWebsocket;
     private getPriv;
     private makePlaintextPayload;

+ 24 - 4
lib/index.js

@@ -278,9 +278,7 @@ class BankClient {
     }
     getContentItemByHash(hash) {
         return __awaiter(this, void 0, void 0, function* () {
-            if (hash.startsWith('/ipfs/')) {
-                hash = hash.split('/').pop();
-            }
+            hash = this.parseItemHash(hash).hash;
             const contentParams = (yield this.webClient.requestJSON({
                 method: 'get',
                 url: this.ipfsUrlBase + '/ipfs/' + hash + '/content.json'
@@ -292,9 +290,10 @@ class BankClient {
         return __awaiter(this, void 0, void 0, function* () {
             const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
             const items = yield Promise.all(itemHashes.map(itemId => {
+                const itemHash = this.parseItemHash(itemId).hash;
                 return this.webClient.requestJSON({
                     method: 'get',
-                    url: this.ipfsUrlBase + '/ipfs/' + itemId,
+                    url: this.ipfsUrlBase + '/ipfs/' + itemHash,
                 });
             }));
             for (const item of items) {
@@ -303,6 +302,27 @@ class BankClient {
             return items;
         });
     }
+    parseItemHash(itemHash) {
+        let type = null;
+        let timestamp = null;
+        let hash = null;
+        if (itemHash.startsWith('/ipfs/')) {
+            itemHash = itemHash.substring(6);
+        }
+        const matched = itemHash.match(/^([0-9]*)_(..)_(.*)$/);
+        if (matched) {
+            timestamp = matched[1];
+            type = matched[2];
+            hash = matched[3];
+        }
+        if (!type) {
+            type = 'CO';
+        }
+        if (!hash) {
+            hash = itemHash;
+        }
+        return { type, timestamp, hash };
+    }
     connectWebsocket(peerAddr, topic, connectCallback, messageCallback) {
         return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
             const nonce = yield this.getNonce();

Diferenças do arquivo suprimidas por serem muito extensas
+ 1 - 1
lib/index.js.map


+ 12 - 10
lib/sample.js

@@ -14,7 +14,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
 const index_1 = require("./index");
 const storage_node_1 = require("./storage-node");
 const webclient_node_1 = __importDefault(require("./webclient-node"));
-const host = 'https://distributing.fun';
+const host = 'http://127.0.0.1:8082';
 const bankClient = new index_1.BankClient(host, 'https://distributing.fun', new storage_node_1.StorageNode('bankClient'), new webclient_node_1.default());
 (() => __awaiter(this, void 0, void 0, function* () {
     yield bankClient.bootstrap();
@@ -45,15 +45,17 @@ const bankClient = new index_1.BankClient(host, 'https://distributing.fun', new
     //     }
     // });
     // console.log('updated', updatedContact);
-    console.log('subscribing');
-    yield bankClient.subscribePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤', () => {
-        console.log('successfully connected to websocket');
-    }, (msg) => {
-        console.log('got a notify of an outbox item');
-    });
-    console.log('subscribed');
-    const starting = yield bankClient.retrievePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤');
-    console.log('inbox contents', starting);
+    // console.log('subscribing');
+    // await bankClient.subscribePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤', () => {
+    //     console.log('successfully connected to websocket');
+    // }, (msg: any) => {
+    //     console.log('got a notify of an outbox item');
+    // });
+    // console.log('subscribed');
+    // const starting = await bankClient.retrievePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤');
+    // console.log('inbox contents', starting);
+    const items = yield bankClient.getItemsForCommaList('1563498096_SL_bafkreiaocpbrzrc45xdkmnwobospujpewrb4t4hk2thqqs5kldtehy62qu');
+    console.log('items', items);
     // setTimeout(async () => {
     // const uploadResult = await bankClient.upload({
     //     type: 'file',

Diferenças do arquivo suprimidas por serem muito extensas
+ 1 - 1
lib/sample.js.map


+ 26 - 4
src/index.ts

@@ -268,9 +268,7 @@ export class BankClient {
     }
 
     public async getContentItemByHash(hash: string): Promise<ContentItem> {
-      if (hash.startsWith('/ipfs/')) {
-        hash = hash.split('/').pop() as string;
-      }
+      hash = this.parseItemHash(hash).hash;
       const contentParams = (await this.webClient.requestJSON({
         method: 'get',
         url: this.ipfsUrlBase + '/ipfs/' + hash + '/content.json'
@@ -281,9 +279,10 @@ export class BankClient {
     public async getItemsForCommaList(commaList: string): Promise<any[]> {
       const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
       const items: any[] = await Promise.all(itemHashes.map(itemId => {
+        const itemHash = this.parseItemHash(itemId).hash;
         return this.webClient.requestJSON({
           method: 'get',
-          url: this.ipfsUrlBase + '/ipfs/' + itemId,
+          url: this.ipfsUrlBase + '/ipfs/' + itemHash,
         });
       }));
       for (const item of items) {
@@ -292,6 +291,29 @@ export class BankClient {
       return items;
     }
 
+    public parseItemHash(itemHash: string) {
+      let type: string | null = null;
+      let timestamp: string | null = null;
+      let hash: string | null = null;
+
+      if (itemHash.startsWith('/ipfs/')) {
+        itemHash = itemHash.substring(6);
+      }
+      const matched = itemHash.match(/^([0-9]*)_(..)_(.*)$/);
+      if (matched) {
+        timestamp = matched[1];
+        type = matched[2];
+        hash = matched[3];
+      }
+      if (!type) {
+        type = 'CO';
+      }
+      if (!hash) {
+        hash = itemHash;
+      }
+      return { type, timestamp, hash };
+    }
+
   private connectWebsocket(peerAddr: string, topic: string, connectCallback: () => void, messageCallback: (data: any) => void) {
     return new Promise(async (resolve, reject) => {
       const nonce = await this.getNonce();

+ 13 - 10
src/sample.ts

@@ -1,7 +1,7 @@
 import { BankClient } from './index';
 import { StorageNode } from './storage-node';
 import WebClientNode from './webclient-node';
-const host = 'https://distributing.fun';
+const host = 'http://127.0.0.1:8082';
 
 const bankClient = new BankClient(host, 'https://distributing.fun', new StorageNode('bankClient'), new WebClientNode());
 
@@ -34,16 +34,19 @@ const bankClient = new BankClient(host, 'https://distributing.fun', new StorageN
     //     }
     // });
     // console.log('updated', updatedContact);
-    console.log('subscribing');
-    await bankClient.subscribePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤', () => {
-        console.log('successfully connected to websocket');
-    }, (msg: any) => {
-        console.log('got a notify of an outbox item');
-    });
-    console.log('subscribed');
+    // console.log('subscribing');
+    // await bankClient.subscribePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤', () => {
+    //     console.log('successfully connected to websocket');
+    // }, (msg: any) => {
+    //     console.log('got a notify of an outbox item');
+    // });
+    // console.log('subscribed');
+
+    // const starting = await bankClient.retrievePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤');
+    // console.log('inbox contents', starting);
 
-    const starting = await bankClient.retrievePrivate('QmXRkCwxqecBMXs4DQucS9w85pW16i26tdKcZHExt9z8s5', '📤');
-    console.log('inbox contents', starting);
+    const items = await bankClient.getItemsForCommaList('1563498096_SL_bafkreiaocpbrzrc45xdkmnwobospujpewrb4t4hk2thqqs5kldtehy62qu');
+    console.log('items', items);
 
     // setTimeout(async () => {
     // const uploadResult = await bankClient.upload({