python-bitshares 边学边记 (九) / Memo类

in #python-bitshares6 years ago

在之前的几篇文章中,我们简单介绍了如何安装python-bitshares 、python-bitshares的钱包相关操作、BitShares类以及Account类、Market类、Dex类、Block类、Blockchain类。

详情可以参考文末的参考链接。

(图源 :pixabay)

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


Memo类

创建实例

我们可以使用以下代码创建Memo类实例
from bitshares.memo import Memo
m = Memo("from_account", "to_account")

其中:

  • from_account: 发送Memo的账户
  • to_account: 接收Memo的账户

因为Account类实例可以由帐户名创建,也可以由账户ID创建,所以我们可以传入类似test2018或者1.2.534782的形式。

加密Memo

加密Memo即将明文信息转换成为密文,我们可以使用encrypt方法加密Memo
from pprint import pprint
encrypted_msg = m.encrypt("foobar")
pprint(encrypted_msg)

加密Memo需要首先导入from_account的active key或者memo key,否则会出如下错误提示:

raise MissingKeyError("Memo key for %s missing!" % self.from_account["name"])
bitshares.exceptions.MissingKeyError: Memo key for test2018 missing!

导入私钥后重新执行上述代码:

解密Memo

加密Memo即将密文信息转换成为明文,我们可以使用decrypt方法解密Memo
plaintext_msg = m.decrypt(enc)
print(plaintext_msg)

解密Memo需要首先导入to_account的active key或者memo key,否则会出如下错误提示:

raise MissingKeyError("Memo key for %s missing!" % self.to_account["name"])
bitshares.exceptions.MissingKeyError: Memo key for oflyhigh-bts missing!

导入私钥后重新执行上述代码:

解密出来的内容和我们加密的内容是一致的。

测试

为了验证我们上述学习的内容,我使用测试账户转账并附加Memo

在活动记录中我们看到的操作信息以及明文Memo为:

在区块链浏览器上我们查看到的原始信息为:

from bitshares.memo import Memo
m = Memo("1.2.534782", "1.2.170436")
enc = {
        "from": "BTS6Eq6kGgYRuujDpFtYduCAjdgMfvkHZ3f8SbHVmsjSC9HgCnxFt",
        "message": "33b980c72e22f942454a8ae6b6740a54",
        "nonce": "387853483215441",
        "to": "BTS8HbXtZPbLACch1pvfrZEPH2xbt74VMPMD4ZSZwYeT96jwkpHFo"}

plaintext_msg = m.decrypt(enc)
print(plaintext_msg)

运行结果如下:

和我们输入的信息一般无二哦😀

总结

Memo类提供了bitshares区块链Memo信息加密和解密的方法。本文通过实例介绍了如何使用Memo类加密消息以及解密Memo。

在文末,本文以一个实际的例子来验证Memo类的功能。

文中信息仅供参考,使用文中代码造成损失概不负责!

参考信息

Sort:  

hard working post,, thanks

It would be so helpful if you also provided English translation. :(

Hello, I set up my blog on steemit. I like to take a selfie, travel, contact me, enter my blog and see what's new https://steemit.com/life/@beyonndkiller/asia-s-latest-selfie-sexy-goddess-daily-life4

Winderful post! Thanks for the information you shared concerning python bitshares, your message is very interest. Thanks. I follow and upvote you.

我刚接触bts。技术帖子

O老师你好

Wow man just amazing I like it post 👏👍

Good post about bitshares

Great post. If you provide English translation, it will be very helpful. It is not like saying. Thank you so much for posting this way.
Thanks for sharing
@oflyhigh

Such a complicated thing to master but interesting to learn :)

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 63318.34
ETH 3108.17
USDT 1.00
SBD 3.97