def from_target_upper_bound(cls, target): n = bases.natural_to_string(target) if n and ord(n[0]) >= 128: n = '\x00' + n bits2 = (chr(len(n)) + (n + 3*chr(0))[:3])[::-1] bits = struct.unpack('<I', bits2)[0] return cls(bits)
def from_target_upper_bound(cls, target): n = bases.natural_to_string(target) if n and ord(n[0]) >= 128: n = '\x00' + n bits2 = (chr(len(n)) + (n + 3 * chr(0))[:3])[::-1] bits = struct.unpack('<I', bits2)[0] return cls(bits)
def test_all(self): for i in xrange(10): alphabet = generate_alphabet() for i in xrange(100): n = random.randrange(100000000000000000000000000000) s = bases.natural_to_string(n, alphabet) n2 = bases.string_to_natural(s, alphabet) #print n, s.encode('hex'), n2 self.assertEquals(n, n2)
def _target_to_bits(self, target, _check=True): n = bases.natural_to_string(target) if n and ord(n[0]) >= 128: n = '\x00' + n bits2 = (chr(len(n)) + (n + 3*chr(0))[:3])[::-1] bits = struct.unpack('<I', bits2)[0] if _check: if self._bits_to_target(bits) != target: raise ValueError(repr((target, self._bits_to_target(bits, _check=False)))) return bits
def base58_decode(b58data): b58data2 = b58data.lstrip(base58_alphabet[0]) return chr(0)*(len(b58data) - len(b58data2)) + bases.natural_to_string(bases.string_to_natural(b58data2, base58_alphabet))
def base58_encode(bindata): bindata2 = bindata.lstrip(chr(0)) return base58_alphabet[0]*(len(bindata) - len(bindata2)) + bases.natural_to_string(bases.string_to_natural(bindata2), base58_alphabet)
def base58_decode(data): return chr(0)*(len(data) - len(data.lstrip(base58_alphabet[0]))) + bases.natural_to_string(bases.string_to_natural(data, base58_alphabet))
def base58_encode(data): return base58_alphabet[0]*(len(data) - len(data.lstrip(chr(0)))) + bases.natural_to_string(bases.string_to_natural(data), base58_alphabet)