-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWallet.py
45 lines (37 loc) · 1.59 KB
/
Wallet.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
from Crypto import Signature
from Transaction import Transaction
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from BlockchainUtils import BlockChainUtils
from Block import Block
class Wallet():
def __init__(self):
self.keyPair = RSA.generate(2048)
def sign(self, data):
dataHash = BlockChainUtils.hash(data)
signatureSchemeObject = PKCS1_v1_5.new(self.keyPair)
signature = signatureSchemeObject.sign(dataHash)
return signature.hex()
@staticmethod
def signatureValid(data, signature, publicKeyString):
signature = bytes.fromhex(signature)
dataHash = BlockChainUtils.hash(data)
publicKey = RSA.importKey(publicKeyString)
signatureSchemeObject = PKCS1_v1_5.new(publicKey)
signatureValid = signatureSchemeObject.verify(dataHash, signature)
return signatureValid
def publicKeyString(self):
publicKeyString = self.keyPair.publickey().exportKey('PEM').decode('utf-8')
return publicKeyString
def createTransaction(self, receiver, amount, type):
transaction = Transaction(
self.publicKeyString(), receiver, amount, type)
signature = self.sign(transaction.payload())
transaction.sign(signature)
return transaction
def createBlock(self, transactions, lastHash, blockCount):
block = Block(transactions, lastHash,
self.publicKeyString(), blockCount)
signature = self.sign(block.payload())
block.sign(signature)
return block