{"id":2235,"date":"2023-11-10T18:01:45","date_gmt":"2023-11-10T10:01:45","guid":{"rendered":"https:\/\/bianchenghao6.com\/2235.html"},"modified":"2023-11-10T18:02:40","modified_gmt":"2023-11-10T10:02:40","slug":"%e5%8c%ba%e5%9d%97%e9%93%betransaction","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/2235.html","title":{"rendered":"\u533a\u5757\u94feTransaction"},"content":{"rendered":"
\n
Python\u533a\u5757\u94feTransaction\u7c7b\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n <\/body>
\n Transaction <\/b>\u7c7b\uff0c\u4ee5\u4fbf\u5ba2\u6237\u80fd\u591f\u5411\u4ed6\u4eba\u6c47\u6b3e\u3002\u8bf7\u6ce8\u610f\uff0c\u5ba2\u6237\u53ef\u4ee5\u662f\u8fd9\u7b14\u94b1\u7684\u53d1\u9001\u8005\u6216\u63a5\u6536\u8005\u3002\u5f53\u60a8\u60f3\u6536\u6b3e\u65f6\uff0c\u5176\u4ed6\u6c47\u6b3e\u4eba\u4f1a\u521b\u5efa\u4ea4\u6613\u5e76\u5728\u5176\u4e2d\u6307\u5b9a\u60a8\u7684
\n \u516c\u5f00<\/b>\u5730\u5740\u3002\u6211\u4eec\u5b9a\u4e49\u4e8b\u52a1\u7c7b\u7684\u521d\u59cb\u5316\u5982\u4e0b-\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
def <\/span>__init__(self, sender, recipient, value):
self.sender = sender
self.recipient = recipient
self.value = value
self.time = datetime.datetime.now<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u516c\u5f00<\/b>\u5bc6\u94a5\uff0c\u6536\u4ef6\u4eba\u7684
\n \u516c\u5f00<\/b>\u5bc6\u94a5\u4ee5\u53ca\u8981\u53d1\u9001\u7684\u91d1\u989d\u3002\u8fd9\u4e9b\u5b58\u50a8\u5728\u5b9e\u4f8b\u53d8\u91cf\u4e2d\uff0c\u4ee5\u4f9b\u5176\u4ed6\u65b9\u6cd5\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u6211\u4eec\u518d\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u5b58\u50a8\u4ea4\u6613\u65f6\u95f4\u3002\n <\/div>\n
\n to_dict <\/b>\u7684\u5b9e\u7528\u7a0b\u5e8f\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5c06\u5b57\u5178\u4e2d\u6240\u6709\u4e0a\u8ff0\u56db\u4e2a\u5b9e\u4f8b\u53d8\u91cf\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u8fd9\u53ea\u662f\u4e3a\u4e86\u4f7f\u6574\u4e2a\u4ea4\u6613\u4fe1\u606f\u53ef\u901a\u8fc7\u5355\u4e2a\u53d8\u91cf\u8bbf\u95ee\u3002\n <\/div>\n
\n Genesis <\/b>\u5757\u3002\u521b\u4e16\u5757\u5305\u542b\u7531\u533a\u5757\u94fe\u521b\u5efa\u8005\u53d1\u8d77\u7684\u7b2c\u4e00\u7b14\u4ea4\u6613\u3002\u50cf\u6bd4\u7279\u5e01\u4e00\u6837\uff0c\u6b64\u4eba\u7684\u8eab\u4efd\u53ef\u4ee5\u4fdd\u5bc6\u3002\u56e0\u6b64\uff0c\u5f53\u521b\u5efa\u7b2c\u4e00\u7b14\u4ea4\u6613\u65f6\uff0c\u521b\u5efa\u8005\u53ef\u80fd\u53ea\u662f\u5c06\u5176\u8eab\u4efd\u53d1\u9001\u4e3a
\n \u521b\u4e16\u8bb0<\/b>\u3002\u56e0\u6b64\uff0c\u5728\u521b\u5efa\u5b57\u5178\u65f6\uff0c\u6211\u4eec\u68c0\u67e5\u53d1\u4ef6\u4eba\u662f\u5426\u662f
\n Genesis <\/b>\uff0c\u5982\u679c\u662f\uff0c\u6211\u4eec\u53ea\u9700\u4e3a\u8eab\u4efd\u53d8\u91cf\u5206\u914d\u4e00\u4e9b\u5b57\u7b26\u4e32\u503c\u5373\u53ef\uff1b\u5426\u5219\uff0c\u6211\u4eec\u5c06\u53d1\u4ef6\u4eba\u7684\u8eab\u4efd\u5206\u914d\u7ed9
\n identity <\/b>\u53d8\u91cf\u3002\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
if <\/span>self.sender == \"Genesis\"<\/span><\/span>:
identity = \"Genesis\"
else:<\/span>
identity = self.sender.identity
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
return <\/span>collections.OrderedDict<\/span>({
'sender'<\/span>: identity,
'recipient'<\/span>: self.recipient,
'value'<\/span>: self.value,
'time'<\/span> : self.time})
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
def <\/span>to_dict(self):
if <\/span>self.sender == \"Genesis\"<\/span><\/span>:
identity = \"Genesis\"
else:<\/span>
identity = self.sender.identity
return <\/span>collections.OrderedDict<\/span>({
'sender'<\/span>: identity,
'recipient'<\/span>: self.recipient,
'value'<\/span>: self.value,
'time'<\/span> : self.time})
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n sign_transaction <\/b>\u65b9\u6cd5\u4ee3\u7801\u663e\u793a\u5728\u6b64\u5904-\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
def <\/span>sign_transaction(self):
private_key = self.sender._private_key
signer = PKCS1_v1_5.new<\/span>(private_key)
h = SHA.new<\/span>(str(self.to_dict<\/span>()).encode<\/span>('utf8'<\/span>))
return <\/span>binascii.hexlify<\/span>(signer.sign<\/span>(h)).decode<\/span>('ascii'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Transaction <\/b>\u7c7b\u3002\n <\/div>\n\u6d4b\u8bd5\u4ea4\u6613\u7c7b<\/h2>\n
\n Dinesh <\/b>\u548c
\n Ramesh <\/b>\u3002 Dinesh\u5c06\u53d1\u90015\u4e2aTPCoins\u5230Ramesh\u3002\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u540d\u4e3aDinesh\u548cRamesh\u7684\u5ba2\u6237\u7aef\u3002\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
Dinesh = Client()
Ramesh = Client()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Client <\/b>\u7c7b\u65f6\uff0c\u5c06\u521b\u5efa\u8be5\u5ba2\u6237\u673a\u552f\u4e00\u7684
\n \u516c\u94a5\u548c<\/b>\u79c1\u94a5\u3002\u5f53Dinesh\u5411Ramesh\u53d1\u9001\u4ed8\u6b3e\u65f6\uff0c\u4ed6\u5c06\u9700\u8981Ramesh\u7684\u516c\u94a5\uff0c\u8be5\u516c\u94a5\u662f\u901a\u8fc7\u4f7f\u7528\u5ba2\u6237\u7aef\u7684identity\u5c5e\u6027\u83b7\u5f97\u7684\u3002\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
t = Transaction(
Dinesh,
Ramesh.identity,
5.0
)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n sign_transaction <\/b>\u65b9\u6cd5\u4ece\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e2d\u68c0\u7d22\u53d1\u4ef6\u4eba\u7684\u79c1\u94a5\uff0c\u4ee5\u89e6\u53d1\u4ea4\u6613\u3002\n <\/div>\n
\n sign_transaction <\/b>\u65b9\u6cd5\u5bf9\u5176\u8fdb\u884c\u7b7e\u540d\u3002\u6b64\u65b9\u6cd5\u4ee5\u53ef\u6253\u5370\u683c\u5f0f\u8fd4\u56de\u751f\u6210\u7684\u7b7e\u540d\u3002\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u4e24\u884c\u4ee3\u7801\u751f\u6210\u5e76\u6253\u5370\u7b7e\u540d-\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
signature = t.sign_transaction<\/span>()
print <\/span>(signature)
<\/span><\/code><\/pre>\n<\/p><\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-28<\/span>
7c7e3c97629b218e9ec6e86b01f9abd8e361fd69e7d373c38420790b655b9abe3b575e343c7
13703ca1aee781acd7157a0624db3d57d7c2f1172730ee3f45af943338157f899965856f6b0
0e34db240b62673ad5a08c8e490f880b568efbc36035cae2e748f1d802d5e8e66298be826f5
c6363dc511222fb2416036ac04eb972
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[],"class_list":["post-2235","post","type-post","status-publish","format-standard","hentry","category-python_qkl"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2235"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=2235"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2235\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=2235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=2235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=2235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}