-
Notifications
You must be signed in to change notification settings - Fork 1
/
createTranscation.py
400 lines (361 loc) · 12.8 KB
/
createTranscation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#tron 线下生成transcation
import ecdsa
import time
import hashlib
import base58
from enum import Enum
from pprint import pprint
from tronpy import keys
from tronpy.keys import PrivateKey
from tronpy.keys import PublicKey
from tronpy.providers import HTTPProvider
from Crypto.Hash import keccak
class ContractType(Enum) :
AccountCreateContract = 0;
TransferContract = 1;
TransferAssetContract = 2;
VoteAssetContract = 3;
VoteWitnessContract = 4;
WitnessCreateContract = 5;
AssetIssueContract = 6;
WitnessUpdateContract = 8;
ParticipateAssetIssueContract = 9;
AccountUpdateContract = 10;
FreezeBalanceContract = 11;
UnfreezeBalanceContract = 12;
WithdrawBalanceContract = 13;
UnfreezeAssetContract = 14;
UpdateAssetContract = 15;
ProposalCreateContract = 16;
ProposalApproveContract = 17;
ProposalDeleteContract = 18;
SetAccountIdContract = 19;
CustomContract = 20;
CreateSmartContract = 30;
TriggerSmartContract = 31;
GetContract = 32;
UpdateSettingContract = 33;
ExchangeCreateContract = 41;
ExchangeInjectContract = 42;
ExchangeWithdrawContract = 43;
ExchangeTransactionContract = 44;
UpdateEnergyLimitContract = 45;
AccountPermissionUpdateContract = 46;
ClearABIContract = 48;
UpdateBrokerageContract = 49;
ShieldedTransferContract = 51;
MarketSellAssetContract = 52;
MarketCancelOrderContract = 53;
owner_address = "TE7vGbxLHPGzW9LXdtLCXaRNv3a49PcDYv"
to_address = "TVswGrmmPeaa9qxDq7FQWrhJ6A2bzxLvWp"
amount = 10000000
#1.通过私钥获取公钥
private_key_string = "ace2bf8f884680d036d8a855e338cc5af6a93473608b8d9a2b943019c2a3f42a"
private_key_bytes = bytes.fromhex(private_key_string) #十六进制字符串转换成十六进制字符数组
pri_key = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1)
public_key_bytes = pri_key.get_verifying_key().to_string() #获取公钥并转换成字符串
public_key = PublicKey(public_key_bytes)
private_key = PrivateKey(private_key_bytes)
#2.构建交易
owner_address_hex = keys.to_hex_address(owner_address)
to_address_hex = keys.to_hex_address(to_address)
type = "TransferContract"
type_url = "type.googleapis.com/protocol." + type
type_url_byte = b"type.googleapis.com/protocol." + b"TransferContract"
type_url_hex = []
for i in range(len(type_url_byte)):
type_url_hex.append((type_url_byte[i]))
#3.获取最新快块hash以及最新块bytes
fee_limit = 10000000
timeout = 10.0
network = "https://api.nileex.io"
provider = HTTPProvider(network, timeout)
info = provider.make_request("wallet/getnodeinfo")
ref_block_id = info["solidityBlock"].split(",ID:")[-1]
ref_block_bytes = ref_block_id[12:16]
ref_block_hash = ref_block_id[16:32]
txID = ""
timestamp = int(time.time() * 1000)
expiration = timestamp + 60000
signature = []
token_id = 1000496
asset_name = str(token_id).encode().hex()
constact = [
{
"parameter":{
"value":{
"owner_address" : owner_address_hex,
"to_address" :to_address_hex,
"amount": amount,
# "asset_name": asset_name,
},
"type_url":type_url
},
"type": type
}
]
raw_data = {
"contract": constact,
"ref_block_bytes": ref_block_bytes,
"ref_block_hash": ref_block_hash,
"expiration": expiration,
"timestamp": timestamp,
}
transcation = {
"txID" : txID,
"raw_data" : raw_data,
"signature": signature
}
# #4.方法一,不需要自己组装raw_data_protobuf
# signweight = provider.make_request("wallet/getsignweight", transcation)
# transcation["txID"] = signweight["transaction"]["txid"]
# txID_bytes = bytes.fromhex(transcation["txID"])
# sig = private_key.sign_msg_hash(txID_bytes)
# signature.append(sig.hex())
# #4.方法一结束
#5.方法二,需要自己组装raw_data_protobuf,直接对交易的rawdata进行转十六进制进行hash,对hash结果转十六进制字符串得到txID,对hash结果进行签名即为对交易进行签名
#5.1 根据protobuf协议生成constact数据
rawdata_constact_parameter_value_hex_protbuf = []
def str2hex(strIN):
outhex = []
leng = len(strIN)
if leng % 2 != 0:
strIN = "0" + strIN
leng = leng + 1
for i in range(0, leng, 2):
temp = ord(strIN[i])
if temp >= 0x30 and temp <= 0x39:
temp = temp - 0x30
elif temp >= 0x61 and temp <= 0x66:
temp = temp - 0x61 + 10
temp1 = ord(strIN[i + 1])
if temp1 >= 0x30 and temp1 <= 0x39:
temp1 = temp1 - 0x30
elif temp1 >= 0x61 and temp1 <= 0x66:
temp1 = temp1 - 0x61 + 10
temp = (temp << 4) | temp1
if temp > 0x80:
temp = temp - 1
temp = (~temp) & 0xFF
temp = 0 - temp
outhex.append(temp)
return outhex
owner_address_hex_data = str2hex(owner_address_hex)
filenum = 1
wiretype = 0x2
owner_address_hex_data_probuf = []
owner_address_hex_data_probuf.append(filenum << 3 | wiretype)
owner_address_hex_data_probuf.append(len(owner_address_hex_data))
owner_address_hex_data_probuf.extend(owner_address_hex_data)
to_address_hex_data = str2hex(to_address_hex)
filenum = 2
wiretype = 0x2
to_address_hex_data_probuf = []
to_address_hex_data_probuf.append(filenum << 3 | wiretype)
to_address_hex_data_probuf.append(len(to_address_hex_data))
to_address_hex_data_probuf.extend(to_address_hex_data)
filenum = 3
wiretype = 0
amount_probuf = []
amount_probuf.append(filenum << 3 | wiretype)
while True:
if amount > 0x7F:
temp = amount & 0x7F | 0x80
if temp > 0x7F:
temp = temp - 1
temp = (~temp) & 0xFF
temp = 0 - temp
else:
temp = 0 -temp
amount_probuf.append(temp)
amount = amount >>7
else:
amount_probuf.append(amount)
break
rawdata_constact_parameter_value_hex_protbuf.extend(owner_address_hex_data_probuf)
rawdata_constact_parameter_value_hex_protbuf.extend(to_address_hex_data_probuf)
rawdata_constact_parameter_value_hex_protbuf.extend(amount_probuf)
# 根据protobuf协议生成raw_data_protobuf数据
raw_data_protobuf = []
ref_block_bytes_hex = str2hex(ref_block_bytes)
filenum = 1
wiretype = 0x2
raw_data_protobuf.append(filenum << 3 | wiretype)
raw_data_protobuf.append(len(ref_block_bytes_hex))
raw_data_protobuf.extend(ref_block_bytes_hex)
ref_block_hash_hex = str2hex(ref_block_hash)
filenum = 4
wiretype = 0x2
raw_data_protobuf.append(filenum << 3 | wiretype)
raw_data_protobuf.append(len(ref_block_hash_hex))
raw_data_protobuf.extend(ref_block_hash_hex)
filenum = 8
wiretype = 0
expiration_probuf = []
while True:
if expiration > 0x7F:
temp = expiration & 0x7F | 0x80
if temp > 0x7F:
temp = temp - 1
temp = (~temp) & 0xFF
temp = 0 - temp
else:
temp = 0 -temp
expiration_probuf.append(temp)
expiration = expiration >>7
else:
expiration_probuf.append(expiration)
break
raw_data_protobuf.append(filenum << 3 | wiretype)
raw_data_protobuf.extend(expiration_probuf)
####组装rawdata_constact_protobuf数据
raw_data_contract_type_protobuf = []
filenum = 1
wiretype = 0
_type = ContractType.TransferContract.value
raw_data_contract_type_protobuf.append(filenum << 3 | wiretype)
raw_data_contract_type_protobuf.append(_type)
raw_data_contract_parameter_typeurl_protobuf = []
filenum = 1
wiretype = 2
length = len(type_url)
raw_data_contract_parameter_typeurl_protobuf.append(filenum << 3 | wiretype)
raw_data_contract_parameter_typeurl_protobuf.append(length)
raw_data_contract_parameter_typeurl_protobuf.extend(type_url_hex)
rawdata_constact_parameter_value_protbuf = []
filenum = 2
wiretype = 2
rawdata_constact_parameter_value_protbuf.append(filenum << 3 | wiretype)
rawdata_constact_parameter_value_protbuf.append(len(rawdata_constact_parameter_value_hex_protbuf))
rawdata_constact_parameter_value_protbuf.extend(rawdata_constact_parameter_value_hex_protbuf)
rawdata_constact_parameter_protbuf = []
filenum = 2
wiretype = 2
rawdata_constact_parameter_protbuf.append(filenum << 3 | wiretype)
rawdata_constact_parameter_protbuf.append(len(raw_data_contract_parameter_typeurl_protobuf) + len(rawdata_constact_parameter_value_protbuf))
rawdata_constact_parameter_protbuf.extend(raw_data_contract_parameter_typeurl_protobuf)
rawdata_constact_parameter_protbuf.extend(rawdata_constact_parameter_value_protbuf)
rawdata_constact_protbuf = []
filenum = 11
wiretype = 2
rawdata_constact_protbuf.append(filenum << 3 | wiretype)
rawdata_constact_protbuf.append(len(raw_data_contract_type_protobuf) + len(rawdata_constact_parameter_protbuf))
rawdata_constact_protbuf.extend(raw_data_contract_type_protobuf)
rawdata_constact_protbuf.extend(rawdata_constact_parameter_protbuf)
####组装rawdata_constact_protobuf数据结束
raw_data_protobuf.extend(rawdata_constact_protbuf)
filenum = 14
wiretype = 0
timestamp_probuf = []
while True:
if timestamp > 0x7F:
temp = timestamp & 0x7F | 0x80
if temp > 0x7F:
temp = temp - 1
temp = (~temp) & 0xFF
temp = 0 - temp
else:
temp = 0 -temp
timestamp_probuf.append(temp)
timestamp = timestamp >>7
else:
timestamp_probuf.append(timestamp)
break
raw_data_protobuf.append(filenum << 3 | wiretype)
raw_data_protobuf.extend(timestamp_probuf)
for i in range(len(raw_data_protobuf)): #注意一定要转换成字节数组,保证每个元素是0到255之间
if raw_data_protobuf[i] < 0:
raw_data_protobuf[i] = 256 + raw_data_protobuf[i]
#组装raw_data_protobuf结束
# #6. 方法二(一)开始
# def keccak256(data):
# hasher = keccak.new(digest_bits=256)
# hasher.update(data)
# return hasher.digest()
#
# def verifying_key_to_addr(key):
# pub_key = key.to_string()
# primitive_addr = b'\x41' + keccak256(pub_key)[-20:]
# addr = base58.b58encode_check(primitive_addr)
# return addr
#
# print("=> my addr key")
# raw_priv_key = bytes.fromhex(private_key_string)
#
# priv_key = ecdsa.SigningKey.from_string(raw_priv_key, curve=ecdsa.SECP256k1)
# pub_key = priv_key.get_verifying_key().to_string()
# print('Pub Key:', pub_key.hex())
#
# primitive_addr = b'\x41' + keccak256(pub_key)[-20:]
# addr = base58.b58encode_check(primitive_addr)
# print('My Addr:', addr)
#
# raw_data_protobuf_str = ""
# for i in range(len(raw_data_protobuf)):
# if raw_data_protobuf[i] > 0xF:
# strtemp = str(hex(raw_data_protobuf[i]))[2:]
# elif raw_data_protobuf[i] < 0:
# if raw_data_protobuf[i] < -0xF:
# strtemp = str(hex(raw_data_protobuf[i]))[3:]
# else:
# strtemp = "0" + str(hex(raw_data_protobuf[i]))[3:]
# else:
# strtemp = "0" + str(hex(raw_data_protobuf[i]))[2:]
# raw_data_protobuf_str = raw_data_protobuf_str + strtemp
# len_raw_data_protobuf_str = len(raw_data_protobuf_str)
# print(raw_data_protobuf_str)
# raw_data = bytes.fromhex(raw_data_protobuf_str)
# signature_temp = priv_key.sign_deterministic(raw_data, hashfunc=hashlib.sha256)
#
# # recover address to get rec_id
# pub_keys = ecdsa.VerifyingKey.from_public_key_recovery(
# signature_temp[:64], raw_data, curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256
# )
# for v, pk in enumerate(pub_keys):
# if verifying_key_to_addr(pk) == addr:
# break
#
# signature_temp += bytes([v])
# signature_temp_hex = (signature_temp.hex())
# signature.append(signature_temp_hex)
# #6.方法二(一)结束
#7.方法二(二)开始
#7.1 进行hash
raw_data_protobuf_str = ""
for i in range(len(raw_data_protobuf)):
if raw_data_protobuf[i] > 0xF:
strtemp = str(hex(raw_data_protobuf[i]))[2:]
elif raw_data_protobuf[i] < 0:
if raw_data_protobuf[i] < -0xF:
strtemp = str(hex(raw_data_protobuf[i]))[3:]
else:
strtemp = "0" + str(hex(raw_data_protobuf[i]))[3:]
else:
strtemp = "0" + str(hex(raw_data_protobuf[i]))[2:]
raw_data_protobuf_str = raw_data_protobuf_str + strtemp
len_raw_data_protobuf_str = len(raw_data_protobuf_str)
print(raw_data_protobuf_str)
raw_data_str = bytes.fromhex(raw_data_protobuf_str)
hash_result = hashlib.sha256()
hash_result.update(raw_data_str)
hash_result_y = (hash_result.digest())
hash_r = []
for i in range(len(hash_result_y)):
hash_r.append(hash_result_y[i])
#7.2 获取txID
txID_self = ""
for i in range(len(hash_r)):
if hash_r[i] > 0xF:
strtemp = str(hex(hash_r[i]))[2:]
else:
strtemp = "0" + str(hex(hash_r[i]))[2:]
txID_self = txID_self + strtemp
transcation["txID"] = txID_self
#7.3 对交易进行签字
sig_self = private_key.sign_msg_hash(bytes(hash_r))
signature.append(sig_self.hex())
#方法二(二)结束
#8.对交易进行广播
result_broadcast = provider.make_request("/wallet/broadcasttransaction", transcation)
pprint(transcation)
print("createTranscation End!!!")