Transaction
Transaction
import base64
class Transaction:
self.change = 0
self.user_id = ids
if not genesis:
total_utxo = int(User.query.filter_by(id=ids).first().total_htbc)
if self.change < 0:
self.change = 0
else:
self.change = -self.amount
self.transaction_outputs.append(
Transaction._id += 1
self.transaction_outputs.append(
else:
self.transaction_outputs.append({"0"+str(Transaction._id): (self.receiver_address,
self.amount)})
Transaction._id += 1
def to_od(self):
# Convert object to ordered dictionary (so it produces same results every time)
od = OrderedDict([
('sender_address', self.sender_address),
('receiver_address', self.receiver_address),
('amount', self.amount),
('transaction_id', self.transaction_id),
('transaction_inputs', self.transaction_inputs),
('transaction_outputs', self.transaction_outputs),
('signature', self.signature),
('change', self.change),
('user_id', self.user_id)
])
return od
def to_json(self):
def get_hash(self):
return self.get_hash_obj().hexdigest()
def get_hash_obj(self):
return sha512(str(self.to_json()).encode('utf-8'))
priv_key = RSA.importKey(private_key)
my_sign = PKCS1_v1_5.new(priv_key)
transaction = self.to_od()
h = SHA.new(json.dumps(transaction, default=str).encode('utf8'))
self.signature = base64.b64encode(my_sign.sign(h)).decode('utf8')