index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 util_1 = require("./util");
  19. const webclient_node_1 = __importDefault(require("./webclient-node"));
  20. class BankClient {
  21. constructor(urlBase, storage = new storage_1.Storage('bankClient'), webClient = new webclient_node_1.default()) {
  22. this.urlBase = urlBase;
  23. this.storage = storage;
  24. this.webClient = webClient;
  25. }
  26. getPub() {
  27. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  28. yield this.bootstrap();
  29. this.getPriv().id((idErr, pubHash) => {
  30. if (idErr) {
  31. return reject(idErr);
  32. }
  33. resolve(pubHash);
  34. });
  35. }));
  36. }
  37. bootstrap() {
  38. if (this.bootstrapResult) {
  39. return Promise.resolve(this.bootstrapResult);
  40. }
  41. if (this.bootstrapPromise) {
  42. return this.bootstrapPromise;
  43. }
  44. return this.bootstrapPromise = new Promise((resolve, reject) => {
  45. this.storage.get('notaprivatekey').then(privateKeyFromStorage => {
  46. if (privateKeyFromStorage == null) {
  47. console.log('no private key in storage. generating new');
  48. crypto.keys.generateKeyPair('RSA', 2048, (generateErr, privateKey) => {
  49. if (generateErr) {
  50. return reject(generateErr);
  51. }
  52. privateKey.export('password', (exportErr, exportResult) => {
  53. if (exportErr) {
  54. return reject(exportErr);
  55. }
  56. this.storage.set('notaprivatekey', exportResult).then(err => {
  57. // whatever
  58. }).catch(reject);
  59. this.privateKey = privateKey;
  60. resolve(true);
  61. });
  62. });
  63. }
  64. else {
  65. // console.log('importing privatekey');
  66. crypto.keys.import(privateKeyFromStorage, 'password', (err, importedPrivateKey) => {
  67. if (err) {
  68. return reject(err);
  69. }
  70. this.privateKey = importedPrivateKey;
  71. // console.log(this.getPublicKeyString());
  72. // console.log(privateKeyFromStorage);
  73. resolve(true);
  74. });
  75. }
  76. }).catch(reject);
  77. });
  78. }
  79. getNonce() {
  80. return __awaiter(this, void 0, void 0, function* () {
  81. const nonce = yield this.webClient.request({
  82. method: 'GET',
  83. url: this.urlBase + '/bank/nonce'
  84. });
  85. return Number(nonce);
  86. });
  87. }
  88. getBalance() {
  89. return __awaiter(this, void 0, void 0, function* () {
  90. const nonce = yield this.getNonce();
  91. const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
  92. _date: new Date().toISOString(),
  93. _nonce: nonce
  94. }));
  95. const topicURL = this.urlBase + '/bank/getbalance';
  96. const postResponse = yield this.webClient.requestJSON({
  97. body: retrieveRequest,
  98. method: 'POST',
  99. url: topicURL
  100. });
  101. return postResponse.balance;
  102. });
  103. }
  104. upload(params) {
  105. return __awaiter(this, void 0, void 0, function* () {
  106. const url = this.urlBase + '/bank/upload';
  107. const formData = {};
  108. if (params.fileData) {
  109. formData.file = {
  110. value: params.fileData,
  111. options: {
  112. filename: params.fileName
  113. }
  114. };
  115. }
  116. if (params.thumbFileData) {
  117. formData.thumb = {
  118. value: params.thumbFileData,
  119. options: {
  120. filename: params.thumbFileName
  121. }
  122. };
  123. }
  124. for (const attr of ['title', 'text', 'type']) {
  125. if (params[attr] != null) {
  126. formData[attr] = params[attr];
  127. }
  128. }
  129. console.log('formData', formData);
  130. const uploadResponse = yield this.webClient.requestJSON({
  131. formData,
  132. method: 'POST',
  133. url
  134. });
  135. console.log('uploadResponse', uploadResponse);
  136. return uploadResponse.hash;
  137. });
  138. }
  139. getPriv() {
  140. if (!this.privateKey) {
  141. throw new Error('missing private key');
  142. }
  143. return this.privateKey;
  144. }
  145. makePlaintextPayload(message) {
  146. const messageBytes = new TextEncoder().encode(message);
  147. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  148. yield this.bootstrap();
  149. this.privateKey.sign(messageBytes, (signErr, signatureBytes) => __awaiter(this, void 0, void 0, function* () {
  150. if (signErr) {
  151. reject(signErr);
  152. return;
  153. }
  154. const publicDERBytes = this.privateKey.public.bytes;
  155. this.privateKey.id((idErr, pubHash) => {
  156. if (idErr) {
  157. reject(idErr);
  158. return;
  159. }
  160. const result = {
  161. date: new Date().toISOString(),
  162. msg: util_1.encodeHex(messageBytes),
  163. pub: util_1.encodeHex(publicDERBytes),
  164. pubHash,
  165. sig: util_1.encodeHex(signatureBytes),
  166. };
  167. // console.log('result', result, signatureBytes);
  168. resolve(result);
  169. });
  170. }));
  171. }));
  172. }
  173. }
  174. exports.BankClient = BankClient;
  175. //# sourceMappingURL=index.js.map