added reconstructhash for working with block hashes without their leading zeroes

This commit is contained in:
Kevin Froman 2019-06-27 21:43:50 -05:00
parent dce453db81
commit 03fbf32af9
4 changed files with 65 additions and 1 deletions

0
docs/usage/install.md Normal file
View File

View File

@ -77,7 +77,7 @@ function openThread(bHash, sender, date, sigBool, pubkey, subjectLine){
var sigMsg = 'signature'
// show add unknown contact button if peer is unknown but still has pubkey
if (sender === pubkey && sender !== myPub){
if (sender === pubkey && sender !== myPub && sigBool){
addUnknownContact.style.display = 'inline'
}

View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
import unittest, sys
sys.path.append(".")
from utils import reconstructhash
class ZFill_Hash(unittest.TestCase):
def test_reconstruct(self):
h = b"4d20d791cbc293999b97cc627aa011692d317dede3d0fbd390c763210b0d"
self.assertEqual(reconstructhash.reconstruct_hash(h), b"0000" + h)
h = b"4d20d791cbc293999b97cc627aa011692d317dede3d0fbd390c763210b0d"
self.assertEqual(reconstructhash.reconstruct_hash(h, 62), b"00" + h)
def test_deconstruct(self):
h = b"0000e918d24999ad9b0ff00c1d414f36b74afc93871a0ece4bd452f82b56af87"
h_no = b"e918d24999ad9b0ff00c1d414f36b74afc93871a0ece4bd452f82b56af87"
self.assertEqual(reconstructhash.deconstruct_hash(h), h_no)
h = "0000e918d24999ad9b0ff00c1d414f36b74afc93871a0ece4bd452f82b56af87"
h_no = "e918d24999ad9b0ff00c1d414f36b74afc93871a0ece4bd452f82b56af87"
self.assertEqual(reconstructhash.deconstruct_hash(h), h_no)
unittest.main()

View File

@ -0,0 +1,43 @@
'''
Onionr - Private P2P Communication
z-fill (zero fill) a string to a specific length, intended for reconstructing block hashes
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
def reconstruct_hash(hex_hash, length=64):
return hex_hash.zfill(length)
def deconstruct_hash(hex_hash):
new_hash = ''
ret_bytes = False
try:
hex_hash = hex_hash.decode()
ret_bytes = True
except AttributeError:
pass
c = 0
for x in hex_hash:
if x == '0':
c += 1
else:
break
new_hash = hex_hash[c:]
if ret_bytes:
new_hash = new_hash.encode()
return new_hash