Package: xchainpy-crypto
XCHAIN-CRYPTOThe XCHAIN CRYPTO package is a crypto package used by allXCHAINclients.XCHAIN-CRYPTO encrypts a master phrase to a keystore. This keystore can then be exported to other XCHAIN wallets or stored securely.Users can export their phrase and import them into other wallets since it is a BIP39 compatible phrase.DesignTypically keystore files encrypt aseedto a file, however this is not appropriate or UX friendly, since the phrase cannot be recovered after the fact.Crypto design:[entropy] -> [phrase] -> [seed] -> [privateKey] -> [publicKey] -> [address]Instead, XCHAIN-CRYPTO stores the phrase in a keystore file, then decrypts and passes this phrase to other clients:[keystore] -> XCHAIN-CRYPTO -> [phrase] -> ChainClientThe ChainClients can then convert this into their respective key-pairs and addresses.
Users can also export their phrases after the fact, ensuring they have saved it securely. This could enhance UX onboarding since users aren't forced to write their phrases down immediately for empty or test wallets.# Crypto Constants for xchainCIPHER=AES.MODE_CTRNBITS=128KDF="pbkdf2"PRF="hmac-sha256"DKLEN=32C=262144HASHFUNCTION=SHA256META="xchain-keystore"Installationpipinstallxchainpy_cryptoBefore install the package on M1 Mac, execute this command:brewinstallautoconfautomakelibffilibtoolpkg-configUsageBasic usagefromxchainpy_crypto.cryptoimportvalidate_phrase,encrypt_to_keystore,decrypt_from_keystore,generate_mnemonicphrase=generate_mnemonic(size=12,language='english')print(phrase)is_correct=validate_phrase(phrase)print(is_correct)password='thorchain'keystore=awaitencrypt_to_keystore(phrase,password)phrase_decrypted=awaitdecrypt_from_keystore(keystore,password)print(phrase_decrypted)Keystore ModelclassKeystore:def__init__(self,crypto:CryptoStruct,id:str,version:int,meta:str):self._crypto=cryptoself._id=idself._version=versionself._meta=meta@classmethoddeffrom_dict(cls,keystore):new_keystore=cls.__new__(cls)forkeyinkeystore:setattr(new_keystore,key,keystore[key])returnnew_keystore@propertydefcrypto(self):returnself._crypto@crypto.setterdefcrypto(self,crypto):ifisinstance(crypto,dict):self._crypto=CryptoStruct.from_dict(crypto)else:self._crypto=crypto@propertydefid(self):returnself._id@id.setterdefid(self,id):self._id=id@propertydefversion(self):returnself._version@version.setterdefversion(self,version):self._version=version@propertydefmeta(self):returnself._meta@meta.setterdefmeta(self,meta):self._meta=metadefto_json(self):returnjson.dumps(self,default=lambdao:{key.lstrip('_'):valueforkey,valueino.__dict__.items()})CipherParamsclassCipherParams:def__init__(self,iv:str):self._iv=iv@classmethoddeffrom_dict(cls,cipherparams):new_cipherparams=cls.__new__(cls)forkeyincipherparams:setattr(new_cipherparams,key,cipherparams[key])returnnew_cipherparams@propertydefiv(self):returnself._iv@iv.setterdefiv(self,iv):self._iv=ivCryptoStructclassCryptoStruct:def__init__(self,cipher:int,ciphertext:str,cipherparams:CipherParams,kdf:str,kdfparams:KdfParams,mac:str,):self._cipher=cipherself._ciphertext=ciphertextself._cipherparams=cipherparamsself._kdf=kdfself._kdfparams=kdfparamsself._mac=mac@classmethoddeffrom_dict(cls,crypto):new_crypto=cls.__new__(cls)forkeyincrypto:setattr(new_crypto,key,crypto[key])returnnew_crypto@propertydefcipher(self):returnself._cipher@cipher.setterdefcipher(self,cipher):self._cipher=cipher@propertydefciphertext(self):returnself._ciphertext@ciphertext.setterdefciphertext(self,ciphertext):self._ciphertext=ciphertext@propertydefcipherparams(self):returnself._cipherparams@cipherparams.setterdefcipherparams(self,cipherparams):ifisinstance(cipherparams,dict):self._cipherparams=CipherParams.from_dict(cipherparams)else:self._cipherparams=cipherparams@propertydefkdf(self):returnself._kdf@kdf.setterdefkdf(self,kdf):self._kdf=kdf@propertydefkdfparams(self):returnself._kdfparams@kdfparams.setterdefkdfparams(self,kdfparams):ifisinstance(kdfparams,dict):self._kdfparams=KdfParams.from_dict(kdfparams)else:self._kdfparams=kdfparams@propertydefmac(self):returnself._mac@mac.setterdefmac(self,mac):self._mac=macKdfParamsclassKdfParams:def__init__(self,prf:str,dklen:int,salt:str,c:int):self._prf=prfself._dklen=dklenself._salt=saltself._c=c@classmethoddeffrom_dict(cls,kdfparams):new_kdfparams=cls.__new__(cls)forkeyinkdfparams:setattr(new_kdfparams,key,kdfparams[key])returnnew_kdfparams@propertydefprf(self):returnself._prf@prf.setterdefprf(self,prf):self._prf=prf@propertydefdklen(self):returnself._dklen@dklen.setterdefdklen(self,dklen):self._dklen=dklen@propertydefsalt(self):returnself._salt@salt.setterdefsalt(self,salt):self._salt=salt@propertydefc(self):returnself._c@c.setterdefc(self,c):self._c=cTestsThese packages needed to run tests:pytestpip install pytestpytest-asynciopip install pytest-asyncioHow to run test ?$python-mpytestxchainpy/xchainpy_crypto/tests