@@ -48,58 +48,55 @@ export class CryptoID {
48
48
return this ;
49
49
}
50
50
async initializeKeypairs ( ) {
51
- if ( isBuffer ( this . signatureKeypair ) ) {
51
+ if ( await this . cipherSuite . signature . isKeypairInitialized ( this . signatureKeypair ) === false ) {
52
52
this . signatureKeypair = await this . cipherSuite . signature . initializeKeypair ( this . signatureKeypair ) ;
53
53
}
54
- if ( isBuffer ( this . keyExchangeKeypair ) ) {
54
+ if ( await this . cipherSuite . keyExchange . isKeypairInitialized ( this . keyExchangeKeypair ) === false ) {
55
55
this . keyExchangeKeypair = await this . cipherSuite . keyExchange . initializeKeypair ( this . keyExchangeKeypair ) ;
56
56
}
57
57
}
58
- async importKeypairs ( config ) {
58
+ async importKeypairs ( core ) {
59
59
const {
60
- version,
61
60
signatureKeypair,
62
61
keyExchangeKeypair,
63
- cipherSuite
64
- } = config ;
62
+ } = core ;
65
63
if ( signatureKeypair ) {
66
64
this . signatureKeypair = signatureKeypair ;
67
65
}
68
66
if ( keyExchangeKeypair ) {
69
67
this . keyExchangeKeypair = keyExchangeKeypair ;
70
68
}
71
- if ( version ) {
72
- this . version = version ;
73
- }
74
- if ( cipherSuite ?. id ) {
75
- this . cipherSuiteId = cipherSuite . id ;
76
- }
77
69
await this . initializeKeypairs ( ) ;
70
+ return this ;
78
71
}
79
72
async generate ( options ) {
80
73
this . signatureKeypair = await this . cipherSuite . signature . signatureKeypair ( ) ;
81
74
this . keyExchangeKeypair = await this . cipherSuite . keyExchange . keyExchangeKeypair ( ) ;
82
- console . log ( 'KEY EXCHANGE' , this . keyExchangeKeypair ) ;
75
+ // console.log('KEY EXCHANGE', this.keyExchangeKeypair);
83
76
}
84
77
async importFromBinary ( data , encryptionKey ) {
85
78
const password = ( isString ( encryptionKey ) ) ? await this . cipherSuite . hash . hash256 ( Buffer . from ( encryptionKey ) ) : encryptionKey ;
86
- const decodedData = ( password ) ? await this . decryptBinary ( data , password ) : await decode ( data ) ;
87
- await this . importFromObject ( decodedData , password ) ;
79
+ const encodedData = decode ( data ) ;
80
+ const decodedCore = ( password ) ? await this . decryptBinary ( encodedData . core , password ) : encodedData . core ;
81
+ encodedData . core = decodedCore ;
82
+ await this . importFromObject ( encodedData , password ) ;
88
83
return this ;
89
84
}
90
85
async importFromObject ( decodedData , encryptionKey ) {
91
86
const password = ( isString ( encryptionKey ) ) ? await this . cipherSuite . hash . hash256 ( Buffer . from ( encryptionKey ) ) : encryptionKey ;
92
87
const data = ( password ) ? await this . decryptBinary ( decodedData . encrypted , password ) : decodedData ;
93
88
const {
94
89
version,
95
- keyExchangeKeypair,
96
- signatureKeypair,
97
- cipherSuiteId
90
+ cipherID,
91
+ core : {
92
+ keyExchangeKeypair,
93
+ signatureKeypair,
94
+ } ,
98
95
} = data ;
99
96
this . version = version ;
100
97
this . keyExchangeKeypair = keyExchangeKeypair ;
101
98
this . signatureKeypair = signatureKeypair ;
102
- this . cipherSuiteId = cipherSuiteId ;
99
+ this . cipherID = cipherID ;
103
100
await this . initializeKeypairs ( ) ;
104
101
return this ;
105
102
}
@@ -108,7 +105,7 @@ export class CryptoID {
108
105
return decode ( decrypted ) ;
109
106
}
110
107
async exportKeypairs ( ) {
111
- console . log ( 'keyExchangeKeypair' , this . keyExchangeKeypair ) ;
108
+ // console.log('keyExchangeKeypair', this.keyExchangeKeypair);
112
109
const keyExchangeKeypair = await this . cipherSuite . keyExchange . exportKeypair ( this . keyExchangeKeypair ) ;
113
110
const signatureKeypair = await this . cipherSuite . signature . exportKeypair ( this . signatureKeypair ) ;
114
111
return {
@@ -117,16 +114,14 @@ export class CryptoID {
117
114
} ;
118
115
}
119
116
async exportBinary ( encryptionKey ) {
120
- const {
121
- version,
122
- cipherSuiteId
123
- } = this ;
117
+ const { version, } = this ;
124
118
const data = {
125
119
version,
126
- cipherSuiteId,
127
120
date : Date . now ( ) ,
121
+ cipherID : this . cipherSuite . id ,
122
+ core : { }
128
123
} ;
129
- assign ( data , await this . exportKeypairs ( ) ) ;
124
+ assign ( data . core , await this . exportKeypairs ( ) ) ;
130
125
const dataEncoded = await encodeStrict ( data ) ;
131
126
if ( encryptionKey ) {
132
127
const password = ( isString ( encryptionKey ) ) ? await this . cipherSuite . hash . hash256 ( Buffer . from ( encryptionKey ) ) : encryptionKey ;
@@ -144,9 +139,8 @@ export class CryptoID {
144
139
async importFile ( filePath , encryptionPassword ) {
145
140
const data = await readStructured ( filePath ) ;
146
141
if ( data ) {
147
- return this . importFromObject ( data , encryptionPassword ) ;
142
+ const loadedFile = await this . importFromObject ( data , encryptionPassword ) ;
148
143
}
149
- console . log ( 'Error Importing Profile' , filePath ) ;
150
144
return false ;
151
145
}
152
146
async saveToKeychain ( accountName = 'profile' , encryptionPassword ) {
0 commit comments