-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.py
116 lines (93 loc) · 2.93 KB
/
transaction.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
#from collections import OrderedDict
import collections
import wallet
import node
import binascii
import datetime
import Crypto
import Crypto.Random
from Crypto.Hash import SHA384
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
import json
import base64
#from hashlib import sha256
import random
import requests
from flask import Flask, jsonify, request, render_template
def genesis_transaction(mywallet, participants):
t = Transaction(
sender = 0,
sender_private_key = mywallet.get_private_key(),
recipient = mywallet.get_public_key(),
amount = 100*participants,
inputs = [])
t.sign_trans()
t.outputs = [{
'id': t.index,
'who': t.sender,
'amount': t.amount
}]
mywallet.utxos[mywallet.get_public_key()] = [t.outputs[0]]
print("WHAT MOEY DO I HAVE???")
print(mywallet.utxos[mywallet.get_public_key()])
mywallet.transactions.append(t)
return t
# possible use of copy for dicitonaries
def create_transaction(mywallet, recipient, amount):
inputs = mywallet.utxos[mywallet.get_public_key()].copy()
balance = mywallet.calculate_balance()
if balance < amount:
raise Exception('not enough money')
t = Transaction(
sender = mywallet.get_public_key(),
sender_private_key = mywallet.get_private_key(),
recipient = recipient,
amount = amount,
inputs = inputs
)
t.sign_trans()
t.outputs = [{
'id': t.index,
'who': t.sender,
'amount': balance - amount
},{
'id': t.index,
'who': t.recipient,
'amount': amount
}]
#wallet.utxos[mywallet.get_public_key()] = [t.outputs[0]]
#mywallet.transactions.append(t)
print("create_transaction returns (t) : ")
print(t)
print("of type")
print(type(t))
print(t.signature)
return t
class Transaction:
def __init__(self, sender, sender_private_key, recipient, amount, inputs, signature=None, index=None):
##set
self.sender = sender
self.sender_private_key = sender_private_key
self.recipient = recipient
self.amount = amount
self.index = index
self.timestamp = str(datetime.datetime.utcnow())
self.inputs = inputs
self.signature = signature
self.outputs = []
# structure to use all infos as message in signature
def to_dict(self):
return json.dumps(dict(
sender = self.sender,
recipient = self.recipient,
amount = self.amount,
timestamp = self.timestamp,
), sort_keys = True)
def sign_trans(self):
myhash = SHA384.new(self.to_dict().encode())
p_key = RSA.importKey(self.sender_private_key)
signer = PKCS1_v1_5.new(p_key)
self.index = myhash.hexdigest()
# TODO: Fix!
self.signature = base64.b64encode(signer.sign(myhash)).decode()