___|  _ \   |  |    |   |_ _|\ \     / ____|
 |     |   |  |  |    |   |  |  \ \   /  __|
 |   | |   | ___ __|  ___ |  |   \ \ /   |
\____|\___/     _|   _|  _|___|   \_/   _____| 

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

python-bitshares 边学边记 (二) / 钱包操作

BY: @oflyhigh | CREATED: Dec. 9, 2017, 1:58 p.m. | VOTES: 191 | PAYOUT: $98.40 | [ VOTE ]

在之前的帖子中,介绍了python-bitshares 这个用于操作bitshares区块链的强大的python库。并介绍了python-bitshares的安装以及运行了一个简单的示例,并简单分析了python-bitshares与bitshares RPC的关系。

[IMAGE: https://steemitimages.com/DQmab7QPFcG6Q4U9Z1FMThkdVcDneSY5xR2bPxVWryEZfeX/image.png]
(图源 :pixabay)

这节我们来继续学习python-bitshares 。

导入私钥

在Uptick的介绍文章中,为了更好的使用uptick,我们将bitshares账户的私钥添加到了uptick钱包中。其实,钱包功能是在python-bitshares这个层次实现和访问的。uptick只是对相关功能进行了封装。

为了方便我们后续的学习,我们也要将私钥添加到python-bitshares的本地钱包中。如何获取账户私钥,可以参考这篇文章中的对应步骤:
* 又一把瑞士军刀? Uptick初体验(二):导入私钥

使用uptick 导入私钥

如果我们安装了uptick,那么可以直接使用uptick导入私钥的,非常方便。如何导入,在这篇文章已经做了详尽的介绍,就不再赘述了。

但是我直接安装的python-bitshares,是不包含uptick的,所以无法使用uptick导入私钥。

使用代码导入私钥

导入私钥之前我们需要先创建个钱包
from bitshares import BitShares
bitshares = BitShares(node="wss://openledger.hk/ws")
bitshares.wallet.create("passwd")

上述代码创建一个本地钱包,并设置密码为passwd
(密码仅供示例,出于安全考虑,实际使用时,建议设置复杂一点的密码)

再对钱包进行创造之前,我们首先需要解锁钱包
bitshares.wallet.unlock("passwd")

解锁钱包之后,我们就可以导入私钥
bitshares.wallet.addPrivateKey("5XXXXXXXX")
执行成功后,私钥就被导入到钱包中去了。

使用UNLOCK环境变量解锁

在以上例子中,我们使用了
bitshares.wallet.unlock("passwd")
来解锁钱包。

在代码中硬编码密码可不是一个好习惯,假设我们有多份代码,然后需要修改密码,这一定是一个很头疼的事。

那么还有什么方法指定密码呢?

那就是设置环境变量,比如在我的系统中,
export UNLOCK="passwd"
为了每次都生效,可以加入到对应用户的.bashrc文件中

这样我们就可以无需在程序中硬编码密码以及每次调用unlock()了。

钱包其它功能

一般情况,都是python-bitshares 和钱包打交道,我们当它透明的就好。但是偶尔可能也需要我们直接对钱包进行操作,比如说看看钱包中有哪些用户,或者从钱包中读出某个对应用户的私钥。

一些可能会被使用到的函数如下:
* changePassphrase,修改密码
* getAccounts, 列出钱包中所有用户
* getPublicKeys,列出钱包中所有公钥
* getPrivateKeyForPublicKey,列出公钥的对应私钥。

更多函数及功能,请参考:
https://github.com/xeroc/python-bitshares/blob/master/bitshares/wallet.py

钱包存储位置

有时候我们可能需要将钱包迁移到其它的账户下,这时候一个一个私钥重新添加是很苦恼的事情,如果能直接迁移钱包文件就好了。

不同的系统下,钱包存储位置是不同的
在Linux系统下,钱包文件路径为:~/.local/share/bitshares/bitshares.sqlite

从命名可以看出是一个sqlite数据库,库中还包含一些默认参数之类的设置,这节就不详聊了。

总结

python-bitshares 提供了一个加密的本地钱包,这样我们使用起来就更加便利了。本文介绍了python-bitshares的钱包相关操作,包括以下内容:

参考信息

TAGS: [ #python-bitshares ] [ #bitshares ] [ #python ] [ #cn ] [ #cn-programming ]

Replies

@ansarikamran | Dec. 9, 2017, 2:41 p.m. | Votes: 1 | [ VOTE ]

good keep it up.. nice post

@ladida | Dec. 9, 2017, 3:02 p.m. | Votes: 0 | [ VOTE ]

Hmm, interesting python guide. Would you recommend anywhere else to become more familiar with the language?

@deyu | Dec. 9, 2017, 3:24 p.m. | Votes: 0 | [ VOTE ]

It can be a really heart warming feeling to know that one single action you have made can make such a positive difference in a stranger or a friend’s day.

@nomita | Dec. 9, 2017, 3:57 p.m. | Votes: 0 | [ VOTE ]

Hmm, interesting python guide. Would you recommend anywhere else to become more familiar with the language?

good keep it up.. nice post.../..

@dsocer | Dec. 9, 2017, 4:21 p.m. | Votes: 0 | [ VOTE ]

good article

@letmesee | Dec. 9, 2017, 6 p.m. | Votes: 1 | [ VOTE ]

For those to lazy to use Google Translate.

In my previous post, I introduced python-bitshares, a powerful python library for manipulating the bitshares blockchain. And introduced python-bitshares installation and run a simple example, and a brief analysis of python-bitshares RPC with bitshares relationship.
This section we continue to learn python-bitshares.

Import private key
In Uptick's introductory article, to make better use of uptick, we added the bitshares account's private key to the uptick wallet. In fact, the wallet function is implemented and accessed at this level of python-bitshares. uptick just encapsulates the related functionality.

In order to facilitate our follow-up study, we also need to add the private key to the python-bitshares local wallet. How to get the account private key, you can refer to the corresponding steps in this article:

Another Swiss army knife? Uptick first experience (b): import the private key
Use uptick to import the private key
If we installed uptick, then you can use uptick import private key, very convenient. How to import, in this article has done a detailed introduction, will not repeat them.

But my direct installation of python-bitshares does not include uptick, so i can not use uptick to import the private key.

Use the code to import the private key
We need to create a wallet before importing the private key
from bitshares import BitShares
bitshares = BitShares (node = "wss: //openledger.hk/ws")
bitshares.wallet.create ("passwd")

The above code creates a local wallet and sets the password to passwd
(Password is for example only, for safety reasons, the actual use, it is recommended to set a complex password)

Before creating a wallet, we first need to unlock the wallet:
bitshares.wallet.unlock ("passwd")

After unlocking the wallet, we can import the private key
bitshares.wallet.addPrivateKey ("5XXXXXXXX")
After successful implementation, the private key is imported into the wallet.

Unlock using the UNLOCK environment variable
In the example above, we used it
bitshares.wallet.unlock ("passwd")
To unlock the wallet.

Hard-coded passwords in the code is not a good habit, if we have more than one code, and then need to change the password, this must be a very headache.

So what other ways to specify the password?

That is to set the environment variables, such as in my system,
export UNLOCK = "passwd"
For each entry into force, can be added to the corresponding user's. Bashrc file

This way we can eliminate the need to hard-code passwords in programs and unlock () each time.

Wallet other features
In general, all python-bitshares and wallet deal, we should be transparent as well. Occasionally, however, we may also need to manipulate the wallet directly, for example, to see which users are in the wallet or to read the private key of a corresponding user from the wallet.

Some of the functions that may be used are as follows:

changePassphrase, change the password
getAccounts, lists all the users in the wallet
getPublicKeys, lists all the public key in the wallet
getPrivateKeyForPublicKey, list the corresponding private key of the public key.
For more functions and functions, please refer to:
https://github.com/xeroc/python-bitshares/blob/master/bitshares/wallet.py

Wallet storage location
Sometimes we may need to migrate the wallet to other accounts, this time one by one to re-add the private key is a very troublesome thing, if you can directly migrate the wallet file just fine.

Different systems, the wallet storage location is different
Under Linux, the wallet file path is: ~ / .local / share / bitshares / bitshares.sqlite

Can be seen from the name is a sqlite database, the library also contains some default parameters like settings, this section is unknown chat.

to sum up
python-bitshares provides an encrypted local wallet, so that we can use it more convenient. This article describes the python-bitshares wallet-related operations, including the following:

Create a wallet
Unlock the wallet
Import private key
Use the UNLOCK environment variable
Other functions of the wallet
Wallet storage location
Reference Information
Python-bitshares learning by side (A): Introduction and installation
https://github.com/xeroc/python-bitshares

@abh12345 | Dec. 9, 2017, 7:32 p.m. | Votes: 0 | [ VOTE ]

Thank you, very helpful!

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>