added methods for serialization of pow objects

This commit is contained in:
Kjistóf 2017-12-02 11:49:26 +01:00
parent 2301264fdd
commit b95982818e

View File

@ -1,5 +1,6 @@
from os import urandom from os import urandom
from itertools import count from itertools import count
from base64 import b64encode, b64decode
from hashlib import sha256 from hashlib import sha256
@ -28,6 +29,16 @@ class pow_base:
hexresult = self._hexdigest(self._base + str(nonce).encode()) hexresult = self._hexdigest(self._base + str(nonce).encode())
return int(hexresult, 16) < self._target return int(hexresult, 16) < self._target
def export(self):
return b64encode(str(self.difficulty).encode()
+ b':'
+ self.base).decode()
@classmethod
def load(cls, export):
difficulty, base = b64decode(export.encode()).split(b':', 1)
return cls(int(difficulty), base)
class pow_hashlib_base(pow_base): class pow_hashlib_base(pow_base):
def _hexdigest(self, data): def _hexdigest(self, data):