|
|
@@ -11,7 +11,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
};
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
-const crypto = require("libp2p-crypto");
|
|
|
const ws_1 = __importDefault(require("ws"));
|
|
|
const contact_address_1 = require("./contact-address");
|
|
|
const contact_book_1 = require("./contact-book");
|
|
|
@@ -19,11 +18,12 @@ const contact_item_1 = require("./contact-item");
|
|
|
const content_item_1 = require("./content-item");
|
|
|
const util_1 = require("./util");
|
|
|
class BankClient {
|
|
|
- constructor(urlBase, ipfsUrlBase, storage, webClient) {
|
|
|
+ constructor(urlBase, ipfsUrlBase, storage, webClient, crypto) {
|
|
|
this.urlBase = urlBase;
|
|
|
this.ipfsUrlBase = ipfsUrlBase;
|
|
|
this.storage = storage;
|
|
|
this.webClient = webClient;
|
|
|
+ this.crypto = crypto;
|
|
|
this.wsUrlBase = urlBase.replace(/^http/i, 'ws');
|
|
|
}
|
|
|
static parseBankLink(bankLink) {
|
|
|
@@ -48,12 +48,15 @@ class BankClient {
|
|
|
getPub() {
|
|
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
|
yield this.bootstrap();
|
|
|
- this.getPriv().id((idErr, pubHash) => {
|
|
|
- if (idErr) {
|
|
|
- return reject(idErr);
|
|
|
+ try {
|
|
|
+ if (!this.privateKey) {
|
|
|
+ throw new Error('missing privateKey');
|
|
|
}
|
|
|
- resolve(pubHash);
|
|
|
- });
|
|
|
+ resolve(this.privateKey.getPublicHash());
|
|
|
+ }
|
|
|
+ catch (e) {
|
|
|
+ reject(e);
|
|
|
+ }
|
|
|
}));
|
|
|
}
|
|
|
bootstrap() {
|
|
|
@@ -64,38 +67,23 @@ class BankClient {
|
|
|
return this.bootstrapPromise;
|
|
|
}
|
|
|
return this.bootstrapPromise = new Promise((resolve, reject) => {
|
|
|
- this.storage.get('notaprivatekey').then(privateKeyFromStorage => {
|
|
|
+ this.storage.get('notaprivatekey').then((privateKeyFromStorage) => __awaiter(this, void 0, void 0, function* () {
|
|
|
if (privateKeyFromStorage == null) {
|
|
|
console.log('no private key in storage. generating new');
|
|
|
- crypto.keys.generateKeyPair('RSA', 2048, (generateErr, privateKey) => {
|
|
|
- if (generateErr) {
|
|
|
- return reject(generateErr);
|
|
|
- }
|
|
|
- privateKey.export('password', (exportErr, exportResult) => {
|
|
|
- if (exportErr) {
|
|
|
- return reject(exportErr);
|
|
|
- }
|
|
|
- this.storage.set('notaprivatekey', exportResult).then(err => {
|
|
|
- // whatever
|
|
|
- }).catch(reject);
|
|
|
- this.privateKey = privateKey;
|
|
|
- resolve(true);
|
|
|
- });
|
|
|
- });
|
|
|
+ const privateKey = yield this.crypto.generateRsaKeyPair(2048);
|
|
|
+ const exportResult = yield privateKey.export();
|
|
|
+ this.storage.set('notaprivatekey', exportResult).then(err => {
|
|
|
+ // whatever
|
|
|
+ }).catch(reject);
|
|
|
+ this.privateKey = privateKey;
|
|
|
+ resolve(true);
|
|
|
}
|
|
|
else {
|
|
|
// console.log('importing privatekey');
|
|
|
- crypto.keys.import(privateKeyFromStorage, 'password', (err, importedPrivateKey) => {
|
|
|
- if (err) {
|
|
|
- return reject(err);
|
|
|
- }
|
|
|
- this.privateKey = importedPrivateKey;
|
|
|
- // console.log(this.getPublicKeyString());
|
|
|
- // console.log(privateKeyFromStorage);
|
|
|
- resolve(true);
|
|
|
- });
|
|
|
+ this.privateKey = yield this.crypto.importRsaKeyPair(privateKeyFromStorage);
|
|
|
+ resolve(true);
|
|
|
}
|
|
|
- }).catch(reject);
|
|
|
+ })).catch(reject);
|
|
|
});
|
|
|
}
|
|
|
getNonce() {
|
|
|
@@ -429,39 +417,25 @@ class BankClient {
|
|
|
resolve();
|
|
|
}));
|
|
|
}
|
|
|
- getPriv() {
|
|
|
- if (!this.privateKey) {
|
|
|
- throw new Error('missing private key');
|
|
|
- }
|
|
|
- return this.privateKey;
|
|
|
- }
|
|
|
makePlaintextPayload(message) {
|
|
|
- const messageBytes = Buffer.from(message, 'utf-8');
|
|
|
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
+ const messageBytes = Buffer.from(message, 'utf-8');
|
|
|
yield this.bootstrap();
|
|
|
- this.privateKey.sign(messageBytes, (signErr, signatureBytes) => __awaiter(this, void 0, void 0, function* () {
|
|
|
- if (signErr) {
|
|
|
- reject(signErr);
|
|
|
- return;
|
|
|
- }
|
|
|
- const publicDERBytes = this.privateKey.public.bytes;
|
|
|
- this.privateKey.id((idErr, pubHash) => {
|
|
|
- if (idErr) {
|
|
|
- reject(idErr);
|
|
|
- return;
|
|
|
- }
|
|
|
- const result = {
|
|
|
- date: new Date().toISOString(),
|
|
|
- msg: util_1.encodeHex(messageBytes),
|
|
|
- pub: util_1.encodeHex(publicDERBytes),
|
|
|
- pubHash,
|
|
|
- sig: util_1.encodeHex(signatureBytes),
|
|
|
- };
|
|
|
- // console.log('result', result, signatureBytes);
|
|
|
- resolve(result);
|
|
|
- });
|
|
|
- }));
|
|
|
- }));
|
|
|
+ if (!this.privateKey) {
|
|
|
+ throw new Error('missing privateKey');
|
|
|
+ }
|
|
|
+ const signatureBytes = yield this.privateKey.sign(messageBytes);
|
|
|
+ const publicKey = yield this.privateKey.getPublicKey();
|
|
|
+ const pubHash = yield this.privateKey.getPublicHash();
|
|
|
+ const result = {
|
|
|
+ date: new Date().toISOString(),
|
|
|
+ msg: util_1.encodeHex(messageBytes),
|
|
|
+ pub: util_1.encodeHex(Buffer.from(publicKey, 'hex')),
|
|
|
+ pubHash,
|
|
|
+ sig: util_1.encodeHex(signatureBytes),
|
|
|
+ };
|
|
|
+ return result;
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
exports.BankClient = BankClient;
|