View on GitHub
File Changes

                      
import {BigNumber} from 'bignumber.js'
import {mnemonicToEntropy, generateMnemonic} from 'bip39'
-
import {HdWallet, Wallet} from 'react-native-cardano'
-
import {encryptWithPassword, decryptWithPassword} from 'emip3js'
+
import {HdWallet, Wallet, PasswordProtect} from 'react-native-cardano'
+
import {
+
  encryptWithPassword as encryptWithPasswordJs,
+
  decryptWithPassword as decryptWithPasswordJs,
+
} from 'emip3js'
import {randomBytes} from 'react-native-randombytes'
import bs58 from 'bs58'
import cryptoRandomString from 'crypto-random-string'
export const encryptData = async (
  plaintextHex: string,
  secretKey: string,
+
  useJs?: boolean = false,
): Promise<string> => {
  assert.assert(!!plaintextHex, 'encrypt:: !!plaintextHex')
  assert.assert(!!secretKey, 'encrypt:: !!secretKey')
  const secretKeyHex = Buffer.from(secretKey, 'utf8').toString('hex')
  const saltHex = cryptoRandomString(2 * 32)
  const nonceHex = cryptoRandomString(2 * 12)
-
  const ciphertext = await encryptWithPassword(
+
  if (useJs) {
+
    return await encryptWithPasswordJs(
+
      secretKeyHex,
+
      saltHex,
+
      nonceHex,
+
      plaintextHex,
+
    )
+
  }
+
  return await PasswordProtect.encryptWithPassword(
    secretKeyHex,
    saltHex,
    nonceHex,
    plaintextHex,
  )
-
  return ciphertext
}

                      
export const decryptData = async (
  ciphertext: string,
  secretKey: string,
+
  useJs?: boolean = false,
): Promise<string> => {
  assert.assert(!!ciphertext, 'decrypt:: !!cyphertext')
  assert.assert(!!secretKey, 'decrypt:: !!secretKey')
  const secretKeyHex = Buffer.from(secretKey, 'utf8').toString('hex')
  try {
-
    return await decryptWithPassword(secretKeyHex, ciphertext)
+
    if (useJs) {
+
      return await decryptWithPasswordJs(secretKeyHex, ciphertext)
+
    }
+
    return PasswordProtect.decryptWithPassword(secretKeyHex, ciphertext)
  } catch (e) {
    if (e.message === KNOWN_ERROR_MSG.DECRYPT_FAILED) {
      throw new WrongPassword()