Package: radixlib
RadixLibTable of ContentIntroductionInstalling the PackageMethod 1: Setting up the package through pip and PyPIMethod 2: Setting up the package from sourceGetting StartedLoading Your WalletMethod 1: Loading your wallet through the mnemonic phraseMethod 2: Loading your wallet through the seedMethod 3: Loading your wallet through thewallet.jsonQuery for Account BalanceQuery for Account TransactionsAdditional More Detailed ExamplesAcknowledgementLicence: MITIntroductionRadixLib is an API wrapper for the Radix Gateway API which allows for a quick, easy, and pythonic way to interact with the Radix blockchain directly from python. The following are some of the features offered in this python package:Connecting to the Radix blockchain through the Gateway API.Connecting to custom Radix networks (i.e. stokenet, localnet, devnets or any custom network.)Querying the Gateway API for information (example: account balances, transactions, etc...)Building, signing, and submitting transactions to the blockchain.Creating signers to hold the seed phrase and derive public and private keys.Loading signers from the mnemonic phrase, seed phrase or thewallet.jsonfrom the Radix desktop wallet.Creating signers from random mnemonic phrases.A powerful derivations module for all kinds of derivations needed in Radix.The features listed above are only a small subset of the features offered by this python package. This wrapper is fully compatible with the Gateway API which means that all of the operations that can be done through the Gateway API are supported by this package. This package offerers so much more and can in theory be used to build a python version of theRadix desktop wallet.Installing the PackageThe radixlib package is available onpypiwhich means that it can be installed through pip. Alternatively, the package may be installed and setup from the source code. The sections below outline how these two methods may be carried out.Method 1: Setting up the package through pip and PyPIThe installation of this package through pip is as simple as running the following commandpython3-mpip3installradixlibAfter running the above command, you should find that the radixlib package is now installed to the python interpreter that you're currently using.Method 2: Setting up the package from sourceTo setup this package from the source code you may begin by cloning this repository to your local machine and then running the setup script. You may do that through the following commands:gitclone-bmasterhttps://github.com/0xOmarA/RadixLibcdRadixLib
python3setup.pyinstallAfter installing the package through one of the methods outlined above, you can check if the package has been installed correctly or not by runningpip freezeand checking ifradixlibis listed there or not.Getting StartedIn this section of the document, a series of simple examples are given to help you get started using this package. More detailed and indepth examples are provided in theexamplesdirectory.With the package installed, you are now ready to begin using this package and the functionality that it has. One thing to note is that the name that this package uses in python is the same as the name it uses on Github and on mypy; meaning, that if we were to import this package into our python script, we may do so using the following code:importradixlibHowever, throughout this code and in the examples, the name of the package import is changed toradixupon import to make the package somewhat easier to use a little bit less verbose. So, you will often see this package imported in the following manner:importradixlibasradixLoading Your WalletThe very first example that we will be looking at here is how can you load your wallet using this packahe into your python code. There are three main methods which you may use to load your wallet:Through the mnemonic phraseThrough the seedThrough thewallet.jsonfile obtained from the Radix desktop walletThe following three examples go through how loading your wallet may be done through all three of the above-mentioned methods:Method 1: Loading your wallet through the mnemonic phraseimportradixlibasradixdefmain()->None:# The network that we will be connecting the wallet tonetwork:radix.network.Network=radix.network.MAINNET# The mnemonic phrase we will be using to load the walletmnemonic_phrase:str="abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"# Loading the wallet from the mnemonic phrasewallet:radix.Wallet=radix.Wallet(provider=radix.Provider(network),signer=radix.Signer.from_mnemonic(mnemonic_phrase))print("Wallet address:",wallet.address)if__name__=="__main__":main()To load a wallet through the mnemonic phrase the first thing that we do is define the network that we will be connecting to (for the purpose of this example we connect to the radix mainnet) and then we define the mnemonic phrase that we will be using for the wallet. Finally, theradix.Walletobject is instantiated through the provider and the signer.In this case, thenetworkvariable is used to inform the provider of the network and the url of the gateway API to communicate with, and the mnemonic phrase is used by the signer to derive the seed and eventually the public and private keys of the wallet.Method 2: Loading your wallet through the seedimportradixlibasradixdefmain()->None:# The network that we will be connecting the wallet tonetwork:radix.network.Network=radix.network.MAINNET# The seed phrase that the signer will be using for the wallet# Don't worry, this is the seed phrase of the abandon wallet so I'm not exposing any sensitive# info ;)seed:str="94cfb81f135f8d85d787a84173cf1e9fc51792f3723e2b93a162fa57a03370fd80971d026eed300544116dfee4d5b375c77ea86b65dfd44e2ecda58044684fe0"# Loading the wallet from the mnemonic phrasewallet:radix.Wallet=radix.Wallet(provider=radix.Provider(network),signer=radix.Signer(seed))print("Wallet address:",wallet.address)if__name__=="__main__":main()As you might notice, the code used for this second method is almost identical to that used in the first method. However, there are two main differences here:We're now using the seed to create ourSignerobject instead of using the mnemonic.We're instantiating theSignerthrough the class constructor and not through a class function.Aside from the above-mentioned differences, the code for the previous method and this method is identical.Method 3: Loading your wallet through thewallet.jsonYou might be wondering "what exactly is awallet.jsonfile?" and you would be right to ask that question. In short,wallet.jsonis a file that the Radix desktop wallet produces where your mnemonic phrase is encrypted using your wallet passphrase. You may find more information about thewallet.jsonfile and it's format inthisamazing article written by Stuart fromRadixPool.com.To load up aRadix.Walletobject from thewallet.jsonfile, you may use the following code:importradixlibasradixdefmain()->None:# The network that we will be connecting the wallet tonetwork:radix.network.Network=radix.network.MAINNET# The path to your wallet.json file and the passphrase used by the Radix desktop walletwallet_json_path:str="./wallet.json"passphrase:str="MyUltraSuperSecurePassword1234"# Loading the wallet from the mnemonic phrasewallet:radix.Wallet=radix.Wallet(provider=radix.Provider(network),signer=radix.Signer.from_wallet_json(wallet_json_path,passphrase),)print("Wallet address:",wallet.address)if__name__=="__main__":main()In this case, we are using the path to thewallet.jsonfile as well as the passphrase which the Radix wallet uses to encrypt the contents of the file as arguments to theradix.Signer.from_wallet_jsonmethod to create the signer that we will be using for the wallet object.Query for Account BalanceNow that we have discussed how aradix.Walletobject may be instantiated, we may now begin to dive deeper into what theradix.Walletobject offers and what it gives us. One of the things that theradix.Walletobject allows for is an easy way to query the gateway API for the balance of the currently loaded account.To demonstrate querying for the account balance may be done, we will take the code fromMethod 1: Loading your wallet through the mnemonic phraseand extend it to allow for the added functionality. The modified code is as follows:fromtypingimportDictimportradixlibasradixdefmain()->None:# The network that we will be connecting the wallet tonetwork:radix.network.Network=radix.network.MAINNET# The mnemonic phrase we will be using to load the walletmnemonic_phrase:str="abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"# Loading the wallet from the mnemonic phrasewallet:radix.Wallet=radix.Wallet(provider=radix.Provider(network),signer=radix.Signer.from_mnemonic(mnemonic_phrase))print("Wallet address:",wallet.address)# Getting the balance for the currently loaded accountbalances:Dict[str,Dict[str,int]]=wallet.get_account_balances()print("Wallet balances:",balances)if__name__=="__main__":main()The only section that was added in this code is a call to thewallet.get_account_balancesmethod which gets the balances of all of the tokens that the account currently holds.Query for Account TransactionsAnother typical thing which you might want to do is to query for the transaction history for your account. This is very simple to do using the radixlib package.Extending upon the code from the previous example, we may get the last 30 transactions involving our account through the code below:fromtypingimportTuple,List,Dict,Anyimportradixlibasradixdefmain()->None:# The network that we will be connecting the wallet tonetwork:radix.network.Network=radix.network.MAINNET# The mnemonic phrase we will be using to load the walletmnemonic_phrase:str="abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"# Loading the wallet from the mnemonic phrasewallet:radix.Wallet=radix.Wallet(provider=radix.Provider(network),signer=radix.Signer.from_mnemonic(mnemonic_phrase))print("Wallet address:",wallet.address)# Getting the balance for the currently loaded accountbalances:Dict[str,Dict[str,int]]=wallet.get_account_balances()print("Wallet balances:",balances)# Getting the last 30 transactions on the currently loaded account_,transactions=wallet.get_account_transactions(limit=30)print("Transactions:",transactions)if__name__=="__main__":main()Getting the last 30 transactions that our account was involved in was as simple as calling thewallet.get_account_transactionsmethod with the argument limit set to 30.Additional More Detailed ExamplesThe examples that you've seen here are some simple examples which might be helpful for you to get started using this package. You might want to checkout the examples in theexamplesdirectory of the repo for more detailed examples where more interesting concepts such assending transactionsis showcased and explained.AcknowledgementThank you to everybody who has helped make this package possible from the Radix team and from the community. Special thanks to Stuart fromRadixPool.comfor his amazing efforts and write ups on the technical side of Radix.Licence: MITCopyright (c) 2022 0xOmarAPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.