Kin Android development tutorial — part 5 — Creating a backend wallet for earns and spends using the Python SDK Luc Hendriks Follow Jul 8, 2019 · 5 min read

In the next few parts of the tutorial series we are going to set up the backend as outlined in the previous tutorial. This means setting up our own backend logic (the place where only you have control over everything), connecting it to the Kin blockchain and your app and setting up an earn. After this is done, we go back to our app and create an earn button. This part of the tutorial will be about creating the operational wallet and saving the public and private keys for future reference.

Operational and cold storage wallet

As you have probably already read in the previous section, we are going to work with two wallets: an operational and a cold storage wallet. The operational wallet is used to send earns and spends. The cold storage wallet is where you save your big stash of Kin. In the event you are hacked somehow, the hacker can only steal the Kin on your operational wallet and everything in your cold storage wallet is safe. Here is some more information about why this is a good way to store your Kin.

The cold storage wallet is something you rarely touch and probably holds a lot of Kin, so please make sure you store that on a hardware wallet. Currently the Ledger is the only hardware wallet that supports Kin, so get yourself a Ledger if you plan to own a significant amount! You can send transactions via mykinwallet.org.

Install the Kin Python SDK

We are not going to use the Python SDK for the backend, but the Python bootstrap server. However, it is nice to actually see some of the Python code so you understand what is happening under the hood.

The first step towards creating a Kin account using the Python SDK is to install the SDK.

If you don’t have Python on your machine, please install that first. Make sure you install Python 3. If you type python3 in a console, you should see something like this:

Check if python is installed

Like always, I’m following the Kin docs quite closely. You can simply install the Kin SDK by typing this in a console:

pip3 install kin-sdk --user

I’m fond of installing python packages in the folder of the current user (by appending the user flag). Otherwise you might run into permission errors that people typically “solve” by putting sudo in front of their command (don’t do that).

To check if installation was successful, open the Python 3 console and type “import kin”. If that doesn’t give an error, installation was successful.

No message is a good sign!

Create a new python file with this in it:

import kin print('First we will create our KinClient object, and direct it to our test environment') client = kin.KinClient(kin.TEST_ENVIRONMENT)

print('

Environment: ')

print(client.environment)

Please note that the hello world SDK page uses asynchronous functions, but I will be using synchronous functions. This also works, and we are only going to use this locally anyway so performance is not that important. In addition, it’s much easier to explain what’s happening if we don’t also have to deal with asynchronous code. For the actual app we will use the Python bootstrap which uses the async implementation.

The code above first imports the Kin SDK and then creates a new kin client instance. The logic is similar to the Android SDK, so you’ll see the same concepts coming up. Next, we print the client environment to check if we’re actually on the testnet. It you run the python script (name the file something.py and then run python3 something.py in the console), you should see this:

Result of the first python code

The next step is to create a wallet locally. Just like the Android SDK, we first need to create a local wallet and then register it on the Kin blockchain. You can create the wallet by adding the following code to the script:

import kin print('First we will create our KinClient object, and direct it to our test environment') client = kin.KinClient(kin.TEST_ENVIRONMENT)

print('

Environment: ')

print(client.environment) keypair = kin.Keypair()

print(keypair.public_address)

print(client.does_account_exists(keypair.public_address))

Running that code should give the following result:

Creating a Kin address locally

First, a new keypair is created and we print the public address. Then, we ask the client if the account exists on the Kin blockchain, which returns False. This is because the account has not been registered on the blockchain yet (just like with the Android SDK tutorial).

So the next step is to actually register it on the blockchain:

import kin print('First we will create our KinClient object, and direct it to our test environment') client = kin.KinClient(kin.TEST_ENVIRONMENT)

print('

Environment: ')

print(client.environment) keypair = kin.Keypair()

print(keypair.public_address)

print(client.does_account_exists(keypair.public_address)) client.friendbot(keypair.public_address)

print(client.does_account_exists(keypair.public_address))

This is a very simple piece of extra code: we ask the friendbot to create the public address for us. Then, if you check if the public address exists, it should output True.

Save the credentials for later

The app backend needs access to the operational wallet. We are going to use the Kin Python bootstrap server in order to get our communication with the Kin blockchain ready. If you take a peek at the README, you’ll see we not only need to have the public address, but the seed. This is the private key that is necessary to sign transactions. You can view the private key by printing the whole keypair:

import kin client = kin.KinClient(kin.TEST_ENVIRONMENT) keypair = kin.Keypair()

print(keypair) # Register on the blockchain...

This snippet will create a local keypair and display the keypair as a whole. The result should look like this:

Example of a full keypair

Please don’t share that seed to anyone ever. This is just a random keypair I generated on the testnet so it holds no value, but in principle anyone can now take that account and do everything with it (including steal all Kin on it). If you ever think your seed is compromised, immediately create a new wallet and move over all Kin.

Wrapup

That concludes the fifth part of the tutorial series. We had a look in the Python SDK and created a wallet locally and registered it on the Kin blockchain. In part 6, we are going to set up the Kin Python bootstrap server to actually make a connection with the Kin blockchain!