index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. getPriv() {
  105. if (!this.privateKey) {
  106. throw new Error('missing private key');
  107. }
  108. return this.privateKey;
  109. }
  110. makePlaintextPayload(message) {
  111. const messageBytes = new TextEncoder().encode(message);
  112. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  113. yield this.bootstrap();
  114. this.privateKey.sign(messageBytes, (signErr, signatureBytes) => __awaiter(this, void 0, void 0, function* () {
  115. if (signErr) {
  116. reject(signErr);
  117. return;
  118. }
  119. const publicDERBytes = this.privateKey.public.bytes;
  120. this.privateKey.id((idErr, pubHash) => {
  121. if (idErr) {
  122. reject(idErr);
  123. return;
  124. }
  125. const result = {
  126. date: new Date().toISOString(),
  127. msg: util_1.encodeHex(messageBytes),
  128. pub: util_1.encodeHex(publicDERBytes),
  129. pubHash,
  130. sig: util_1.encodeHex(signatureBytes),
  131. };
  132. // console.log('result', result, signatureBytes);
  133. resolve(result);
  134. });
  135. }));
  136. }));
  137. }
  138. }
  139. exports.BankClient = BankClient;
  140. //# sourceMappingURL=index.js.map