|
@@ -1,11 +1,11 @@
|
|
|
-import crypto = require('libp2p-crypto');
|
|
|
|
|
|
|
+import { IKeyPair } from './key-pair';
|
|
|
import WebSocket from 'ws';
|
|
import WebSocket from 'ws';
|
|
|
import { ContactAddress } from './contact-address';
|
|
import { ContactAddress } from './contact-address';
|
|
|
import { ContactBook } from './contact-book';
|
|
import { ContactBook } from './contact-book';
|
|
|
import { ContactItem } from './contact-item';
|
|
import { ContactItem } from './contact-item';
|
|
|
import { ContentItem } from './content-item';
|
|
import { ContentItem } from './content-item';
|
|
|
import { ContentParams } from './content-params';
|
|
import { ContentParams } from './content-params';
|
|
|
-import RsaPrivateKey = crypto.keys;
|
|
|
|
|
|
|
+import { ICrypto } from './crypto';
|
|
|
import { Storage } from './storage';
|
|
import { Storage } from './storage';
|
|
|
import { UploadItemParameters } from './upload-item-parameters';
|
|
import { UploadItemParameters } from './upload-item-parameters';
|
|
|
import { encodeHex, mergeDeep, uuid } from './util';
|
|
import { encodeHex, mergeDeep, uuid } from './util';
|
|
@@ -32,24 +32,26 @@ export class BankClient {
|
|
|
return { host, address, topic };
|
|
return { host, address, topic };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private privateKey: RsaPrivateKey | undefined;
|
|
|
|
|
|
|
+ private privateKey: IKeyPair | undefined;
|
|
|
private wsUrlBase: string;
|
|
private wsUrlBase: string;
|
|
|
private bootstrapPromise: any;
|
|
private bootstrapPromise: any;
|
|
|
private bootstrapResult: any;
|
|
private bootstrapResult: any;
|
|
|
|
|
|
|
|
- constructor(private urlBase: string, private ipfsUrlBase: string, private storage: Storage, private webClient: IWebClient) {
|
|
|
|
|
|
|
+ constructor(private urlBase: string, private ipfsUrlBase: string, private storage: Storage, private webClient: IWebClient, private crypto: ICrypto) {
|
|
|
this.wsUrlBase = urlBase.replace(/^http/i, 'ws');
|
|
this.wsUrlBase = urlBase.replace(/^http/i, 'ws');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public getPub(): Promise<string> {
|
|
public getPub(): Promise<string> {
|
|
|
return new Promise(async (resolve, reject) => {
|
|
return new Promise(async (resolve, reject) => {
|
|
|
await this.bootstrap();
|
|
await 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);
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -61,35 +63,21 @@ export class BankClient {
|
|
|
return this.bootstrapPromise;
|
|
return this.bootstrapPromise;
|
|
|
}
|
|
}
|
|
|
return this.bootstrapPromise = new Promise((resolve, reject) => {
|
|
return this.bootstrapPromise = new Promise((resolve, reject) => {
|
|
|
- this.storage.get('notaprivatekey').then(privateKeyFromStorage => {
|
|
|
|
|
|
|
+ this.storage.get('notaprivatekey').then(async (privateKeyFromStorage) => {
|
|
|
if (privateKeyFromStorage == null) {
|
|
if (privateKeyFromStorage == null) {
|
|
|
console.log('no private key in storage. generating new');
|
|
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 = await this.crypto.generateRsaKeyPair(2048);
|
|
|
|
|
+ const exportResult = await privateKey.export();
|
|
|
|
|
+
|
|
|
|
|
+ this.storage.set('notaprivatekey', exportResult).then(err => {
|
|
|
|
|
+ // whatever
|
|
|
|
|
+ }).catch(reject);
|
|
|
|
|
+ this.privateKey = privateKey;
|
|
|
|
|
+ resolve(true);
|
|
|
} else {
|
|
} else {
|
|
|
// console.log('importing privatekey');
|
|
// 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 = await this.crypto.importRsaKeyPair(privateKeyFromStorage);
|
|
|
|
|
+ resolve(true);
|
|
|
}
|
|
}
|
|
|
}).catch(reject);
|
|
}).catch(reject);
|
|
|
});
|
|
});
|
|
@@ -429,40 +417,23 @@ export class BankClient {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private getPriv(): RsaPrivateKey {
|
|
|
|
|
|
|
+ private async makePlaintextPayload(message: string) {
|
|
|
|
|
+ const messageBytes = Buffer.from(message, 'utf-8');
|
|
|
|
|
+ await this.bootstrap();
|
|
|
if (!this.privateKey) {
|
|
if (!this.privateKey) {
|
|
|
- throw new Error('missing private key');
|
|
|
|
|
|
|
+ throw new Error('missing privateKey');
|
|
|
}
|
|
}
|
|
|
- return this.privateKey;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private makePlaintextPayload(message: string) {
|
|
|
|
|
- const messageBytes = Buffer.from(message, 'utf-8');
|
|
|
|
|
-
|
|
|
|
|
- return new Promise(async (resolve, reject) => {
|
|
|
|
|
- await this.bootstrap();
|
|
|
|
|
- this.privateKey.sign(messageBytes, async (signErr, signatureBytes) => {
|
|
|
|
|
- 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: encodeHex(messageBytes),
|
|
|
|
|
- pub: encodeHex(publicDERBytes),
|
|
|
|
|
- pubHash,
|
|
|
|
|
- sig: encodeHex(signatureBytes),
|
|
|
|
|
- };
|
|
|
|
|
- // console.log('result', result, signatureBytes);
|
|
|
|
|
- resolve(result);
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ const signatureBytes = await this.privateKey.sign(messageBytes);
|
|
|
|
|
+ const publicKey = await this.privateKey.getPublicKey();
|
|
|
|
|
+ const pubHash = await this.privateKey.getPublicHash();
|
|
|
|
|
+
|
|
|
|
|
+ const result = {
|
|
|
|
|
+ date: new Date().toISOString(),
|
|
|
|
|
+ msg: encodeHex(messageBytes),
|
|
|
|
|
+ pub: encodeHex(Buffer.from(publicKey, 'hex')),
|
|
|
|
|
+ pubHash,
|
|
|
|
|
+ sig: encodeHex(signatureBytes),
|
|
|
|
|
+ };
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|