88} from "sodium-plus" ;
99import { Keypair , wipe } from "./util" ;
1010import { Buffer } from "buffer" ;
11- import { Store } from "secure-webstore " ;
11+ import { Store } from "../secureIDBStorage " ;
1212
1313
1414export type IdentityKeyPair = { identitySecret : Ed25519SecretKey , identityPublic : Ed25519PublicKey | any } ;
@@ -26,7 +26,7 @@ export interface IdentityKeyManagerInterface {
2626 Promise < IdentityKeyPair > ;
2727 getMyIdentityString ( ) :
2828 Promise < string > ;
29- getPreKeypair ( ) :
29+ getPreKeypair ( keyStore : Store ) :
3030 Promise < PreKeyPair > ;
3131 persistOneTimeKeys ( bundle : Keypair [ ] ) :
3232 Promise < void > ;
@@ -37,7 +37,11 @@ export interface IdentityKeyManagerInterface {
3737 persistPrekeyPair ( prekeyPair : PreKeyPair ) :
3838 Promise < void >
3939 saveIdentityKeypair ( identitykeypair : IdentityKeyPair , keyStore : Store ) :
40- Promise < void >
40+ Promise < void >
41+ savePreKeyPair ( prekeyPair : PreKeyPair , keyStore : Store ) :
42+ Promise < void >
43+
44+
4145}
4246
4347export interface SessionKeyManagerInterface {
@@ -159,10 +163,12 @@ export class DefaultSessionKeyManager implements SessionKeyManagerInterface {
159163 if ( recipient ) {
160164 const keys = await this . symmetricRatchet ( this . sessions [ id ] . receiving ) ;
161165 this . sessions [ id ] . receiving = keys [ 0 ] ;
166+ //console.log(this.sessions[id]);
162167 return keys [ 1 ] ;
163168 } else {
164169 const keys = await this . symmetricRatchet ( this . sessions [ id ] . sending ) ;
165170 this . sessions [ id ] . sending = keys [ 0 ] ;
171+ //console.log(this.sessions[id]);
166172 return keys [ 1 ] ;
167173 }
168174 }
@@ -323,8 +329,8 @@ export class DefaultIdentityKeyManager implements IdentityKeyManagerInterface {
323329 *
324330 * This only returns the X25519 keys. It doesn't include the Ed25519 signature.
325331 */
326- async getPreKeypair ( ) : Promise < PreKeyPair > {
327- const sodium = await this . getSodium ( ) ;
332+ async getPreKeypair ( keyStore : Store ) : Promise < PreKeyPair > {
333+ this . preKey = await this . loadPreKeypair ( keyStore ) ;
328334 if ( this . preKey == undefined ) {
329335 this . preKey = await this . generatePreKeypair ( ) ;
330336 }
@@ -349,6 +355,19 @@ export class DefaultIdentityKeyManager implements IdentityKeyManagerInterface {
349355 return { identitySecret : sk , identityPublic : pk } ;
350356 }
351357
358+
359+ async loadPreKeypair ( keyStore : Store ) : Promise < PreKeyPair > {
360+ const sodium = await this . getSodium ( ) ;
361+ await keyStore . init ( ) ;
362+ const sk = new X25519SecretKey (
363+ await sodium . sodium_hex2bin ( await keyStore . get ( "preKeySecret" ) )
364+ ) ;
365+ const pk = new X25519PublicKey (
366+ await sodium . sodium_hex2bin ( await keyStore . get ( "preKeyPublic" ) )
367+ ) ;
368+ return { preKeySecret : sk , preKeyPublic : pk } ;
369+ }
370+
352371 /**
353372 * Store one-time keys in memory.
354373 *
@@ -378,6 +397,17 @@ export class DefaultIdentityKeyManager implements IdentityKeyManagerInterface {
378397 }
379398
380399
400+ async savePreKeyPair ( preKeyPair : PreKeyPair , keyStore : Store ) : Promise < void > {
401+ const sodium = await this . getSodium ( ) ;
402+ if ( ! keyStore ) {
403+ console . log ( "No key store provided, not saving prekey pair." ) ;
404+ }
405+ await keyStore . init ( ) ;
406+ await keyStore . set ( "preKeyPublic" , await sodium . sodium_bin2hex ( preKeyPair . preKeyPublic . getBuffer ( ) ) ) ;
407+ await keyStore . set ( "preKeySecret" , await sodium . sodium_bin2hex ( preKeyPair . preKeySecret . getBuffer ( ) ) ) ;
408+ }
409+
410+
381411 /**
382412 * Saves the prekey pair to memory
383413 * @param {X25519PublicKey } preKeyPublic
0 commit comments