如何从steem transaction 获取txid?

in #steemdev6 years ago

前些天大神 @xeroc 发了个帖子 python-bitshares: How to derive transaction ids,让我对bitshares的txid有了更深入的了解。于是我就想相同的问题放到STEEM上会是如何呢?

python-bitshares 生成transaction_id

@xeroc 大神修改了python-graphenelib库,给Signed_Transaction类添加了一个新属性: id,而python-bitsharesSigned_Transaction类继承了python-graphenelibSigned_Transaction类。

也就是下边两句:
from graphenebase.signedtransactions import Signed_Transaction as GrapheneSigned_Transaction

class Signed_Transaction(GrapheneSigned_Transaction)

我们再来看看属性id如何实现:

也就是说,将Signed_Transaction的签名部分清空,序列号后生成摘要,取摘要的前20个字节,并转换成16禁止字符串形式(40个字节)。

STEEM 生成transaction_id

STEEM和Bitshares是一奶同胞啦,python-steem的前身piston也是 @xeroc 大神开发的。所以在python-steem上实现类似功能非常简单。

import hashlib
from binascii import hexlify
from steem import Steem
from steembase.transactions import SignedTransaction

steem = Steem()
block = steem.get_block(10000000)
tx = SignedTransaction(**block["transactions"][2])
tx.data.pop("signatures", None)
h = hashlib.sha256(bytes(tx)).digest()
print(hexlify(h[:20]).decode("ascii"))

上述示例代码将计算出STEEM区块链上第10000000 中第3笔transaction的txid。
(transaction编号从零开始)

结果为:

9f48f63edfd40e72ac6c9635dcdcad9d742e5d1c

STEEM 生成transaction_id 方法2

你是不是觉得上述方法很简单了?其实还有更简单的办法

from steem import Steem
steem = Steem()
block = steem.get_block(10000000)
txid = block['transaction_ids'][2]
print(txid)

结果为:

9f48f63edfd40e72ac6c9635dcdcad9d742e5d1c

完全一样,有木有,但是代码简单了好多有木有?

为什么?

既然获取txid如此简单,为何大神还要费此周折?答案在于steem区块链的get_block返回了transaction_ids列表,比如区块:10000000


排列顺序和transaction列表顺序完全一致,所以直接拿出来即可。

而bitshares区块链的get_block不返回上述信息,如果需要,只好自己计算哦。

不过读读 @xeroc 大神的代码,了解了一下 txid的生成机制,还是受益匪浅的。

参考链接

Sort:  

获取了有什么用?

great post, thanks for sharing

Thanks for sharing this valuable post with us . Thanks

噢,原來如此,獲益匪淺。

先生,这真是个好主意
先生,我认为你是一个思考这样的事情的天才
精彩的创新
好的工作先生

5000 followers Congratulation

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.034
BTC 66540.93
ETH 3186.50
USDT 1.00
SBD 4.11