index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __importDefault = (this && this.__importDefault) || function (mod) {
  11. return (mod && mod.__esModule) ? mod : { "default": mod };
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. const crypto = require("libp2p-crypto");
  15. const util = require("util");
  16. const storage_1 = require("./storage");
  17. const TextEncoder = util.TextEncoder;
  18. const ws_1 = __importDefault(require("ws"));
  19. const util_1 = require("./util");
  20. const webclient_node_1 = __importDefault(require("./webclient-node"));
  21. const content_item_1 = require("./content-item");
  22. class BankClient {
  23. constructor(urlBase, ipfsUrlBase, storage = new storage_1.Storage('bankClient'), webClient = new webclient_node_1.default()) {
  24. this.urlBase = urlBase;
  25. this.ipfsUrlBase = ipfsUrlBase;
  26. this.storage = storage;
  27. this.webClient = webClient;
  28. this.wsUrlBase = urlBase.replace(/^http/i, 'ws');
  29. }
  30. static parseBankLink(bankLink) {
  31. if (!bankLink.startsWith('bank:')) {
  32. throw new Error('address must start with bank:');
  33. }
  34. const deprefixed = bankLink.substring(5);
  35. let host;
  36. let address;
  37. let topic;
  38. if (deprefixed[0] === '/' && deprefixed[1] === '/') {
  39. [host, address, topic] = deprefixed.substring(2).split('/');
  40. }
  41. else {
  42. [address, topic] = deprefixed.split('/');
  43. }
  44. if (!address || !topic) {
  45. throw new Error('cannot parse address and topic');
  46. }
  47. return { host, address, topic };
  48. }
  49. getPub() {
  50. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  51. yield this.bootstrap();
  52. this.getPriv().id((idErr, pubHash) => {
  53. if (idErr) {
  54. return reject(idErr);
  55. }
  56. resolve(pubHash);
  57. });
  58. }));
  59. }
  60. bootstrap() {
  61. if (this.bootstrapResult) {
  62. return Promise.resolve(this.bootstrapResult);
  63. }
  64. if (this.bootstrapPromise) {
  65. return this.bootstrapPromise;
  66. }
  67. return this.bootstrapPromise = new Promise((resolve, reject) => {
  68. this.storage.get('notaprivatekey').then(privateKeyFromStorage => {
  69. if (privateKeyFromStorage == null) {
  70. console.log('no private key in storage. generating new');
  71. crypto.keys.generateKeyPair('RSA', 2048, (generateErr, privateKey) => {
  72. if (generateErr) {
  73. return reject(generateErr);
  74. }
  75. privateKey.export('password', (exportErr, exportResult) => {
  76. if (exportErr) {
  77. return reject(exportErr);
  78. }
  79. this.storage.set('notaprivatekey', exportResult).then(err => {
  80. // whatever
  81. }).catch(reject);
  82. this.privateKey = privateKey;
  83. resolve(true);
  84. });
  85. });
  86. }
  87. else {
  88. // console.log('importing privatekey');
  89. crypto.keys.import(privateKeyFromStorage, 'password', (err, importedPrivateKey) => {
  90. if (err) {
  91. return reject(err);
  92. }
  93. this.privateKey = importedPrivateKey;
  94. // console.log(this.getPublicKeyString());
  95. // console.log(privateKeyFromStorage);
  96. resolve(true);
  97. });
  98. }
  99. }).catch(reject);
  100. });
  101. }
  102. getNonce() {
  103. return __awaiter(this, void 0, void 0, function* () {
  104. const nonce = yield this.webClient.request({
  105. method: 'GET',
  106. url: this.urlBase + '/bank/nonce'
  107. });
  108. return Number(nonce);
  109. });
  110. }
  111. getBalance() {
  112. return __awaiter(this, void 0, void 0, function* () {
  113. const nonce = yield this.getNonce();
  114. const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
  115. _date: new Date().toISOString(),
  116. _nonce: nonce
  117. }));
  118. const topicURL = this.urlBase + '/bank/getbalance';
  119. const postResponse = yield this.webClient.requestJSON({
  120. body: retrieveRequest,
  121. method: 'POST',
  122. url: topicURL
  123. });
  124. return postResponse.balance;
  125. });
  126. }
  127. upload(params) {
  128. return __awaiter(this, void 0, void 0, function* () {
  129. const url = this.urlBase + '/bank/upload';
  130. const formData = {};
  131. if (params.fileData) {
  132. formData.file = {
  133. value: params.fileData,
  134. options: {
  135. filename: params.fileName
  136. }
  137. };
  138. }
  139. if (params.thumbFileData) {
  140. formData.thumb = {
  141. value: params.thumbFileData,
  142. options: {
  143. filename: params.thumbFileName
  144. }
  145. };
  146. }
  147. if (params.links) {
  148. formData.links = JSON.stringify(params.links);
  149. }
  150. for (const attr of ['title', 'text', 'type']) {
  151. if (params[attr] != null) {
  152. formData[attr] = params[attr];
  153. }
  154. }
  155. // console.log('formData', formData);
  156. const uploadResponse = yield this.webClient.requestJSON({
  157. formData,
  158. method: 'POST',
  159. url
  160. });
  161. // console.log('uploadResponse', uploadResponse);
  162. return uploadResponse.hash;
  163. });
  164. }
  165. uploadSlimJSON(item) {
  166. return __awaiter(this, void 0, void 0, function* () {
  167. const url = this.urlBase + '/bank/upload/slim';
  168. const uploadResponse = yield this.webClient.requestJSON({
  169. body: item,
  170. method: 'POST',
  171. url
  172. });
  173. // console.log('uploadResponse', uploadResponse);
  174. return uploadResponse.hash;
  175. });
  176. }
  177. uploadSlimText(item) {
  178. return __awaiter(this, void 0, void 0, function* () {
  179. const url = this.urlBase + '/bank/upload/slim';
  180. const uploadResponse = JSON.parse(yield this.webClient.request({
  181. body: item,
  182. headers: {
  183. 'content-type': 'text/plain'
  184. },
  185. method: 'POST',
  186. url
  187. }));
  188. // console.log('uploadResponse', uploadResponse);
  189. return uploadResponse.hash;
  190. });
  191. }
  192. appendBank(bankAddress, bankTopic, itemHash) {
  193. return __awaiter(this, void 0, void 0, function* () {
  194. const payload = yield this.makePlaintextPayload(itemHash);
  195. const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(bankAddress) + '/' + encodeURIComponent(bankTopic);
  196. yield this.webClient.requestJSON({
  197. body: payload,
  198. method: 'PUT',
  199. url: topicURL
  200. });
  201. });
  202. }
  203. retrievePrivate(peerAddr, topic) {
  204. return __awaiter(this, void 0, void 0, function* () {
  205. const nonce = yield this.getNonce();
  206. const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
  207. _date: new Date().toISOString(),
  208. _nonce: nonce
  209. }));
  210. const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(peerAddr) + '/' + encodeURIComponent(topic);
  211. const result = yield this.webClient.request({
  212. body: JSON.stringify(retrieveRequest),
  213. headers: {
  214. 'content-type': 'application/json'
  215. },
  216. method: 'POST',
  217. url: topicURL
  218. });
  219. return result;
  220. });
  221. }
  222. subscribePrivate(peerAddr, topic, callback) {
  223. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  224. const nonce = yield this.getNonce();
  225. const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
  226. _date: new Date().toISOString(),
  227. _nonce: nonce,
  228. addr: peerAddr,
  229. topic: topic
  230. }));
  231. const ws = new ws_1.default(this.wsUrlBase + '/bank/ws', {
  232. headers: {
  233. 'x-msg': retrieveRequest.msg,
  234. 'x-pub': retrieveRequest.pub,
  235. 'x-pubhash': retrieveRequest.pubHash,
  236. 'x-sig': retrieveRequest.sig
  237. }
  238. });
  239. ws.on('open', function open() {
  240. console.log('opened ws');
  241. resolve();
  242. });
  243. ws.on('message', data => {
  244. // console.log('ws message', data);
  245. callback();
  246. });
  247. ws.on('error', err => {
  248. reject(err);
  249. });
  250. }));
  251. }
  252. appendPrivate(peerAddr, topic, hash, replaceHash) {
  253. return __awaiter(this, void 0, void 0, function* () {
  254. const nonce = yield this.getNonce();
  255. const payload = yield this.makePlaintextPayload(JSON.stringify({
  256. _date: new Date().toISOString(),
  257. _nonce: nonce,
  258. hash,
  259. replaceHash
  260. }));
  261. const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(peerAddr) + '/' + encodeURIComponent(topic);
  262. const result = yield this.webClient.request({
  263. body: JSON.stringify(payload),
  264. headers: {
  265. 'content-type': 'application/json'
  266. },
  267. method: 'PUT',
  268. url: topicURL
  269. });
  270. });
  271. }
  272. getOrCreateContact(peerId, contactAddr) {
  273. return __awaiter(this, void 0, void 0, function* () {
  274. const contactList = yield this.retrievePrivate(peerId, '📇');
  275. const itemList = yield this.getItemsForCommaList(contactList);
  276. // console.log('contact hash for', contact, type, 'is', contactHash);
  277. const existing = itemList.filter(item => item.addrs && item.addrs.includes(contactAddr))[0];
  278. if (existing != null) {
  279. return existing;
  280. }
  281. const newItem = {
  282. addrs: [
  283. contactAddr
  284. ],
  285. id: util_1.uuid()
  286. };
  287. const newItemHash = yield this.uploadSlimJSON(newItem);
  288. yield this.appendPrivate(peerId, '📇', newItemHash);
  289. return newItem;
  290. });
  291. }
  292. getContactById(peerId, contactId) {
  293. return __awaiter(this, void 0, void 0, function* () {
  294. const contactList = yield this.retrievePrivate(peerId, '📇');
  295. const itemList = yield this.getItemsForCommaList(contactList);
  296. const existing = itemList.filter(item => item.id === contactId)[0];
  297. if (!existing) {
  298. throw new Error('Cannot find contact with id ' + contactId);
  299. }
  300. return existing;
  301. });
  302. }
  303. updateContact(peerId, contactId, newProperties) {
  304. return __awaiter(this, void 0, void 0, function* () {
  305. const existing = yield this.getContactById(peerId, contactId);
  306. const newProps = util_1.mergeDeep({}, newProperties);
  307. delete newProps.id;
  308. const newItem = util_1.mergeDeep(existing, newProps);
  309. delete newItem.hash;
  310. const newItemHash = yield this.uploadSlimJSON(newItem);
  311. yield this.appendPrivate(peerId, '📇', newItemHash, existing.hash);
  312. return yield this.getContactById(peerId, contactId);
  313. });
  314. }
  315. getContentItemByHash(hash) {
  316. return __awaiter(this, void 0, void 0, function* () {
  317. if (!hash.startsWith('/ipfs/')) {
  318. hash = '/ipfs/' + hash;
  319. }
  320. const contentParams = (yield this.webClient.requestJSON({
  321. method: 'get',
  322. url: this.ipfsUrlBase + hash + '/content.json'
  323. }));
  324. return new content_item_1.ContentItem(contentParams);
  325. });
  326. }
  327. getItemsForCommaList(commaList) {
  328. return __awaiter(this, void 0, void 0, function* () {
  329. const itemHashes = commaList.split(',').filter(x => x.trim() !== '');
  330. const items = yield Promise.all(itemHashes.map(itemId => {
  331. return this.webClient.requestJSON({
  332. method: 'get',
  333. url: this.ipfsUrlBase + '/ipfs/' + itemId,
  334. });
  335. }));
  336. for (const item of items) {
  337. item.hash = itemHashes.shift();
  338. }
  339. return items;
  340. });
  341. }
  342. getPriv() {
  343. if (!this.privateKey) {
  344. throw new Error('missing private key');
  345. }
  346. return this.privateKey;
  347. }
  348. makePlaintextPayload(message) {
  349. const messageBytes = new TextEncoder().encode(message);
  350. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  351. yield this.bootstrap();
  352. this.privateKey.sign(messageBytes, (signErr, signatureBytes) => __awaiter(this, void 0, void 0, function* () {
  353. if (signErr) {
  354. reject(signErr);
  355. return;
  356. }
  357. const publicDERBytes = this.privateKey.public.bytes;
  358. this.privateKey.id((idErr, pubHash) => {
  359. if (idErr) {
  360. reject(idErr);
  361. return;
  362. }
  363. const result = {
  364. date: new Date().toISOString(),
  365. msg: util_1.encodeHex(messageBytes),
  366. pub: util_1.encodeHex(publicDERBytes),
  367. pubHash,
  368. sig: util_1.encodeHex(signatureBytes),
  369. };
  370. // console.log('result', result, signatureBytes);
  371. resolve(result);
  372. });
  373. }));
  374. }));
  375. }
  376. }
  377. exports.BankClient = BankClient;
  378. //# sourceMappingURL=index.js.map