1414 * limitations under the License.
1515 */
1616
17+ import { NetworkType } from '../../model/blockchain/NetworkType' ;
1718import { WalletAlgorithm } from '../../model/wallet/WalletAlgorithm' ;
1819import { Convert as convert } from '../format/Convert' ;
1920import { KeyPair } from './KeyPair' ;
20- import * as utility from './Utilities' ;
2121import { SignSchema } from './SignSchema' ;
22+ import * as utility from './Utilities' ;
23+ // tslint:disable-next-line: no-var-requires
2224const CryptoJS = require ( 'crypto-js' ) ;
2325export class Crypto {
2426 /**
@@ -219,16 +221,16 @@ export class Crypto {
219221 * @param {string } msg - A text message
220222 * @param {Uint8Array } iv - An initialization vector
221223 * @param {Uint8Array } salt - A salt
222- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
224+ * @param {NetworkType } networkType - Catapult network identifier
223225 * @return {string } - The encoded message
224226 */
225- public static _encode = ( senderPriv , recipientPub , msg , iv , salt , signSchema : SignSchema = SignSchema . SHA3 ) => {
227+ public static _encode = ( senderPriv , recipientPub , msg , iv , salt , networkType : NetworkType ) => {
226228 // Errors
227229 if ( ! senderPriv || ! recipientPub || ! msg || ! iv || ! salt ) { throw new Error ( 'Missing argument !' ) ; }
228230 // Processing
229- const keyPair = KeyPair . createKeyPairFromPrivateKeyString ( senderPriv , signSchema ) ;
231+ const keyPair = KeyPair . createKeyPairFromPrivateKeyString ( senderPriv , networkType ) ;
230232 const pk = convert . hexToUint8 ( recipientPub ) ;
231- const encKey = utility . ua2words ( KeyPair . deriveSharedKey ( keyPair , pk , salt , signSchema ) , 32 ) ;
233+ const encKey = utility . ua2words ( KeyPair . deriveSharedKey ( keyPair , pk , salt , networkType ) , 32 ) ;
232234 const encIv = {
233235 iv : utility . ua2words ( iv , 16 ) ,
234236 } ;
@@ -244,16 +246,16 @@ export class Crypto {
244246 * @param {string } senderPriv - A sender private key
245247 * @param {string } recipientPub - A recipient public key
246248 * @param {string } msg - A text message
247- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
249+ * @param {NetworkType } networkType - Catapult network identifier
248250 * @return {string } - The encoded message
249251 */
250- public static encode = ( senderPriv , recipientPub , msg , signSchema : SignSchema = SignSchema . SHA3 ) => {
252+ public static encode = ( senderPriv , recipientPub , msg , networkType : NetworkType ) => {
251253 // Errors
252254 if ( ! senderPriv || ! recipientPub || ! msg ) { throw new Error ( 'Missing argument !' ) ; }
253255 // Processing
254256 const iv = Crypto . randomBytes ( 16 ) ;
255257 const salt = Crypto . randomBytes ( 32 ) ;
256- const encoded = Crypto . _encode ( senderPriv , recipientPub , msg , iv , salt , signSchema ) ;
258+ const encoded = Crypto . _encode ( senderPriv , recipientPub , msg , iv , salt , networkType ) ;
257259 // Result
258260 return encoded ;
259261 }
@@ -264,16 +266,16 @@ export class Crypto {
264266 * @param {string } recipientPrivate - A recipient private key
265267 * @param {string } senderPublic - A sender public key
266268 * @param {Uint8Array } _payload - An encrypted message payload in bytes
267- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
269+ * @param {NetworkType } networkType - Catapult network identifier
268270 * @return {string } - The decoded payload as hex
269271 */
270- public static _decode = ( recipientPrivate , senderPublic , payload , iv , salt , signSchema : SignSchema = SignSchema . SHA3 ) => {
272+ public static _decode = ( recipientPrivate , senderPublic , payload , iv , salt , networkType : NetworkType ) => {
271273 // Error
272274 if ( ! recipientPrivate || ! senderPublic || ! payload ) { throw new Error ( 'Missing argument !' ) ; }
273275 // Processing
274- const keyPair = KeyPair . createKeyPairFromPrivateKeyString ( recipientPrivate , signSchema ) ;
276+ const keyPair = KeyPair . createKeyPairFromPrivateKeyString ( recipientPrivate , networkType ) ;
275277 const pk = convert . hexToUint8 ( senderPublic ) ;
276- const encKey = utility . ua2words ( KeyPair . deriveSharedKey ( keyPair , pk , salt , signSchema ) , 32 ) ;
278+ const encKey = utility . ua2words ( KeyPair . deriveSharedKey ( keyPair , pk , salt , networkType ) , 32 ) ;
277279 const encIv = {
278280 iv : utility . ua2words ( iv , 16 ) ,
279281 } ;
@@ -291,18 +293,18 @@ export class Crypto {
291293 * @param {string } recipientPrivate - A recipient private key
292294 * @param {string } senderPublic - A sender public key
293295 * @param {string } _payload - An encrypted message payload
294- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
296+ * @param {NetworkType } networkType - Catapult network identifier
295297 * @return {string } - The decoded payload as hex
296298 */
297- public static decode = ( recipientPrivate , senderPublic , _payload , signSchema : SignSchema = SignSchema . SHA3 ) => {
299+ public static decode = ( recipientPrivate , senderPublic , _payload , networkType : NetworkType ) => {
298300 // Error
299301 if ( ! recipientPrivate || ! senderPublic || ! _payload ) { throw new Error ( 'Missing argument !' ) ; }
300302 // Processing
301303 const binPayload = convert . hexToUint8 ( _payload ) ;
302304 const payload = new Uint8Array ( binPayload . buffer , 48 ) ;
303305 const salt = new Uint8Array ( binPayload . buffer , 0 , 32 ) ;
304306 const iv = new Uint8Array ( binPayload . buffer , 32 , 16 ) ;
305- const decoded = Crypto . _decode ( recipientPrivate , senderPublic , payload , iv , salt , signSchema ) ;
307+ const decoded = Crypto . _decode ( recipientPrivate , senderPublic , payload , iv , salt , networkType ) ;
306308 return decoded ;
307309 }
308310
@@ -316,4 +318,18 @@ export class Crypto {
316318 const crypto = require ( 'crypto' ) ;
317319 return crypto . randomBytes ( length ) ;
318320 }
321+
322+ /**
323+ * Resolve signature schema from given network type
324+ *
325+ * @param {NetworkType } networkType - Network type
326+ *
327+ * @return {SignSchema }
328+ */
329+ public static resolveNetworkType ( networkType : NetworkType ) : SignSchema {
330+ if ( networkType === NetworkType . MAIN_NET || networkType === NetworkType . TEST_NET ) {
331+ return SignSchema . KECCAK ;
332+ }
333+ return SignSchema . SHA3 ;
334+ }
319335}
0 commit comments