create account

如何从steem transaction 获取txid? by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh · (edited)
$161.82
如何从steem transaction 获取txid?
前些天大神 @xeroc 发了个帖子 [python-bitshares: How to derive transaction ids](https://steemit.com/bitshares/@xeroc/python-bitshares-how-to-derive-transaction-ids),让我对bitshares的txid有了更深入的了解。于是我就想相同的问题放到STEEM上会是如何呢?

![](https://steemitimages.com/DQmdFCHtL8t3uN2xUef6HYnrTJbqZG6n9Sm8ciy6rw12dXP/image.png)

# [python-bitshares](https://github.com/xeroc/python-bitshares) 生成transaction_id

@xeroc 大神修改了[python-graphenelib](https://github.com/xeroc/python-graphenelib)库,给`Signed_Transaction`类添加了一个新属性: `id`,而[python-bitshares](https://github.com/xeroc/python-bitshares)的`Signed_Transaction`类继承了[python-graphenelib](https://github.com/xeroc/python-graphenelib)的`Signed_Transaction`类。

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

`class Signed_Transaction(GrapheneSigned_Transaction)`

我们再来看看属性`id`如何实现:
![](https://steemitimages.com/DQmQCipLHana33mf6dNHpHNZzVimMUrZNJafTnrsXZNSL9W/image.png)

也就是说,将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
![](https://steemitimages.com/DQmYEXNFDHwSdKP33Q1XRiGpuhGy1TLNa7ge8wg8dLwP5mT/image.png)
# STEEM 生成transaction_id 方法2

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

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

结果为:
>9f48f63edfd40e72ac6c9635dcdcad9d742e5d1c
![](https://steemitimages.com/DQmYEXNFDHwSdKP33Q1XRiGpuhGy1TLNa7ge8wg8dLwP5mT/image.png)

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


# 为什么?

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

![](https://steemitimages.com/DQmdKL9uGqcMcfDa1MgeNwtVvXFCqvGNLS5mqrsG6uxBrBG/image.png)
排列顺序和transaction列表顺序完全一致,所以直接拿出来即可。

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


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

# 参考链接

*  [python-bitshares: How to derive transaction ids by @xeroc](https://steemit.com/bitshares/@xeroc/python-bitshares-how-to-derive-transaction-ids)
*  [通过事务ID(transaction id) 获取事务(transaction) / Database API: get_transaction](https://steemit.com/cn-programming/@oflyhigh/id-transaction-id-transaction-database-api-gettransaction)
*  [获取共同操作账户某个操作的真实操作者,附简单脚本 / Get real operator of a transaction made by the ID which operated by multi accounts, script attached](https://steemit.com/cn/@oflyhigh/get-real-operator-of-a-transaction-made-by-the-id-which-operated-by-multi-accounts-script-attached)
*  https://github.com/xeroc/python-bitshares
*  https://github.com/xeroc/python-graphenelib
*  https://github.com/steemit/steem-python
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 80 others
properties (23)
authoroflyhigh
permlinksteem-transaction-txid
categorysteemdev
json_metadata{"tags":["steem","transaction","cn-programming","cn"],"users":["xeroc"],"image":["https://steemitimages.com/DQmdFCHtL8t3uN2xUef6HYnrTJbqZG6n9Sm8ciy6rw12dXP/image.png","https://steemitimages.com/DQmQCipLHana33mf6dNHpHNZzVimMUrZNJafTnrsXZNSL9W/image.png","https://steemitimages.com/DQmYEXNFDHwSdKP33Q1XRiGpuhGy1TLNa7ge8wg8dLwP5mT/image.png","https://steemitimages.com/DQmdKL9uGqcMcfDa1MgeNwtVvXFCqvGNLS5mqrsG6uxBrBG/image.png"],"links":["https://steemit.com/bitshares/@xeroc/python-bitshares-how-to-derive-transaction-ids"],"app":"hiveblog/0.1","format":"markdown"}
created2018-01-27 12:43:27
last_update2021-03-03 04:48:12
depth0
children8
last_payout2018-02-03 12:43:27
cashout_time1969-12-31 23:59:59
total_payout_value140.319 HBD
curator_payout_value21.505 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,073
author_reputation6,395,038,437,449,292
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,730,502
net_rshares18,674,512,261,933
author_curate_reward""
vote details (144)
@deanliu ·
噢,原來如此,獲益匪淺。
properties (22)
authordeanliu
permlinkre-oflyhigh-steem-transaction-txid-20180127t151316469z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 15:13:15
last_update2018-01-27 15:13:15
depth1
children0
last_payout2018-02-03 15:13:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation3,100,979,937,647,200
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,762,448
net_rshares0
@dropthebeat ·
super great!
properties (22)
authordropthebeat
permlinkre-oflyhigh-steem-transaction-txid-20180127t145245380z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 14:53:18
last_update2018-01-27 14:53:18
depth1
children0
last_payout2018-02-03 14:53:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation4,668,255,551,618
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,758,295
net_rshares0
@hrishikesh ·
Thanks for sharing this valuable post with us . Thanks
properties (22)
authorhrishikesh
permlinkre-oflyhigh-steem-transaction-txid-20180127t125442378z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 12:54:45
last_update2018-01-27 12:54:45
depth1
children0
last_payout2018-02-03 12:54:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length54
author_reputation936,726,318,626
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,732,898
net_rshares0
@jahangirwifii ·
5000 followers Congratulation
properties (22)
authorjahangirwifii
permlinkre-oflyhigh-steem-transaction-txid-20180127t171238616z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 17:12:39
last_update2018-01-27 17:12:39
depth1
children0
last_payout2018-02-03 17:12:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length29
author_reputation37,205,590,144,986
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,787,960
net_rshares0
@maiyude ·
$0.18
获取了有什么用?
👍  
properties (23)
authormaiyude
permlinkre-oflyhigh-steem-transaction-txid-20180127t140549298z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 14:05:57
last_update2018-01-27 14:05:57
depth1
children0
last_payout2018-02-03 14:05:57
cashout_time1969-12-31 23:59:59
total_payout_value0.152 HBD
curator_payout_value0.023 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8
author_reputation8,970,701,360,387
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,748,168
net_rshares20,455,538,654
author_curate_reward""
vote details (1)
@momin109 ·
good post
properties (22)
authormomin109
permlinkre-oflyhigh-steem-transaction-txid-20180127t131511867z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 13:15:15
last_update2018-01-27 13:15:15
depth1
children0
last_payout2018-02-03 13:15:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation-50,585,271,679
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,737,148
net_rshares0
@newssource ·
先生,这真是个好主意
先生,我认为你是一个思考这样的事情的天才
精彩的创新
好的工作先生
properties (22)
authornewssource
permlinkre-oflyhigh-steem-transaction-txid-20180127t151753601z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 15:18:18
last_update2018-01-27 15:18:18
depth1
children0
last_payout2018-02-03 15:18:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length44
author_reputation97,443,418,432
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,763,501
net_rshares0
@ritue ·
great post, thanks for sharing
properties (22)
authorritue
permlinkre-oflyhigh-steem-transaction-txid-20180127t125558997z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-27 12:49:45
last_update2018-01-27 12:49:45
depth1
children0
last_payout2018-02-03 12:49:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation404,864,529,395
root_title"如何从steem transaction 获取txid?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,731,854
net_rshares0