import { decrypt as ecDecrypt , encrypt as ecEncrypt , generatePrivate } from "@toruslabs/eccrypto" ; import BN from "bn.js" ; import { ec as EC } from "elliptic" ; import { keccak256 , toChecksumAddress } from "web3-utils" ; import { EncryptedMessage } from "./baseTypes/commonTypes" ; export const ecCurve = new EC ( "secp256k1" ) ; export async function encrypt ( publicKey , msg ) { const encryptedDetails = await ecEncrypt ( publicKey , msg ) ; return { ciphertext : encryptedDetails . ciphertext . toString ( "hex" ) , ephemPublicKey : encryptedDetails . ephemPublicKey . toString ( "hex" ) , iv : encryptedDetails . iv . toString ( "hex" ) , mac : encryptedDetails . mac . toString ( "hex" ) , } ; } export async function decrypt ( privKey , msg ) { const bufferEncDetails = { ciphertext : Buffer . from ( msg . ciphertext , "hex" ) , ephemPublicKey : Buffer . from ( msg . ephemPublicKey , "hex" ) , iv : Buffer . from ( msg . iv , "hex" ) , mac : Buffer . from ( msg . mac , "hex" ) , } ; return ecDecrypt ( privKey , bufferEncDetails ) ; } export function isEmptyObject ( obj ) { return Object . keys ( obj ) . length === 0 && obj . constructor === Object ; } export const isErrorObj = ( err ) : boolean => err && err . stack && err . message !== "" ; export function prettyPrintError ( error ) { if ( isErrorObj ( error ) ) { return error . message ; } return error . toString ( ) ; } export function generateAddressFromPublicKey ( publicKey ) { const ethAddressLower = `0x${ keccak256 ( publicKey . toString ( "hex" ) ) . slice ( 64 - 38 ) }` ; return toChecksumAddress ( ethAddressLower ) ; } export function normalize ( input ) { if ( ! input ) { return undefined ; } let hexString ; if ( typeof input === "number" ) { hexString = input . toString ( 16 ) ; if ( hexString . length % 2 ) { hexString = `0${ hexString }` ; } } if ( typeof input === "string" ) { hexString = input . toLowerCase ( ) ; } return `0x${ hexString }` ; } export function generatePrivateExcludingIndexes ( shareIndexes ) { const key = new BN ( generatePrivate ( ) ) ; if ( shareIndexes . find ( ( el ) => el . eq ( key ) ) ) { return generatePrivateExcludingIndexes ( shareIndexes ) ; } return key ; } export const KEY_NOT_FOUND = "KEY_NOT_FOUND" ; export const SHARE_DELETED = "SHARE_DELETED" ; export function derivePubKeyXFromPolyID ( polyID ) { return polyID . split ( "|" ) [ 0 ] . slice ( 2 ) ; } export function stripHexPrefix ( str ) { if ( str . slice ( 0 , 2 ) === "0x" ) return str . slice ( 2 ) ; return str ; } export function generateID ( ) { return `${ Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) }` ; }