index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. Object.defineProperty(exports, "__esModule", { value: true });
  11. const crypto = require("libp2p-crypto");
  12. const util = require("util");
  13. const TextEncoder = util.TextEncoder;
  14. const util_1 = require("./util");
  15. class BankClient {
  16. constructor(urlBase, storage, webClient) {
  17. this.urlBase = urlBase;
  18. this.storage = storage;
  19. this.webClient = webClient;
  20. }
  21. getPub() {
  22. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  23. yield this.bootstrap();
  24. this.getPriv().id((idErr, pubHash) => {
  25. if (idErr) {
  26. return reject(idErr);
  27. }
  28. resolve(pubHash);
  29. });
  30. }));
  31. }
  32. bootstrap() {
  33. if (this.bootstrapResult) {
  34. return Promise.resolve(this.bootstrapResult);
  35. }
  36. if (this.bootstrapPromise) {
  37. return this.bootstrapPromise;
  38. }
  39. return this.bootstrapPromise = new Promise((resolve, reject) => {
  40. this.storage.get('notaprivatekey').then(privateKeyFromStorage => {
  41. if (privateKeyFromStorage == null) {
  42. console.log('no private key in storage. generating new');
  43. crypto.keys.generateKeyPair('RSA', 2048, (generateErr, privateKey) => {
  44. if (generateErr) {
  45. return reject(generateErr);
  46. }
  47. privateKey.export('password', (exportErr, exportResult) => {
  48. if (exportErr) {
  49. return reject(exportErr);
  50. }
  51. this.storage.set('notaprivatekey', exportResult).then(err => {
  52. // whatever
  53. }).catch(reject);
  54. this.privateKey = privateKey;
  55. resolve(true);
  56. });
  57. });
  58. }
  59. else {
  60. // console.log('importing privatekey');
  61. crypto.keys.import(privateKeyFromStorage, 'password', (err, importedPrivateKey) => {
  62. if (err) {
  63. return reject(err);
  64. }
  65. this.privateKey = importedPrivateKey;
  66. // console.log(this.getPublicKeyString());
  67. // console.log(privateKeyFromStorage);
  68. resolve(true);
  69. });
  70. }
  71. }).catch(reject);
  72. });
  73. }
  74. getNonce() {
  75. return __awaiter(this, void 0, void 0, function* () {
  76. const nonce = yield this.webClient.request({
  77. method: 'GET',
  78. url: this.urlBase + '/bank/nonce'
  79. });
  80. return Number(nonce);
  81. });
  82. }
  83. getBalance() {
  84. return __awaiter(this, void 0, void 0, function* () {
  85. const nonce = yield this.getNonce();
  86. const retrieveRequest = yield this.makePlaintextPayload(JSON.stringify({
  87. _date: new Date().toISOString(),
  88. _nonce: nonce
  89. }));
  90. const topicURL = this.urlBase + '/bank/getbalance';
  91. const postResponse = this.webClient.requestJSON({
  92. body: retrieveRequest,
  93. method: 'POST',
  94. url: topicURL
  95. });
  96. return postResponse;
  97. });
  98. }
  99. getPriv() {
  100. if (!this.privateKey) {
  101. throw new Error('missing private key');
  102. }
  103. return this.privateKey;
  104. }
  105. makePlaintextPayload(message) {
  106. const messageBytes = new TextEncoder().encode(message);
  107. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  108. yield this.bootstrap();
  109. this.privateKey.sign(messageBytes, (signErr, signatureBytes) => __awaiter(this, void 0, void 0, function* () {
  110. if (signErr) {
  111. reject(signErr);
  112. return;
  113. }
  114. const publicDERBytes = this.privateKey.public.bytes;
  115. this.privateKey.id((idErr, pubHash) => {
  116. if (idErr) {
  117. reject(idErr);
  118. return;
  119. }
  120. const result = {
  121. date: new Date().toISOString(),
  122. msg: util_1.encodeHex(messageBytes),
  123. pub: util_1.encodeHex(publicDERBytes),
  124. pubHash,
  125. sig: util_1.encodeHex(signatureBytes),
  126. };
  127. // console.log('result', result, signatureBytes);
  128. resolve(result);
  129. });
  130. }));
  131. }));
  132. }
  133. }
  134. exports.BankClient = BankClient;
  135. //# sourceMappingURL=index.js.map