From ffa56852d4717b7fa2243cdaeed5608e65015152 Mon Sep 17 00:00:00 2001 From: Arthur B Date: Wed, 4 Apr 2018 10:00:46 +0200 Subject: [PATCH] Script: generate prefix for base58 encoding --- scripts/b58_prefix.py | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 scripts/b58_prefix.py diff --git a/scripts/b58_prefix.py b/scripts/b58_prefix.py new file mode 100755 index 000000000..39870e986 --- /dev/null +++ b/scripts/b58_prefix.py @@ -0,0 +1,44 @@ +#! /usr/bin/env python + +import sys +import bitcoin + +alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' + + +def b58dec(word): + x = 0 + for c in word: + x *= 58 + x += alphabet.find(c) + return x + +def asciidec(val): + word = [] + while val > 0: + word.append(val % 256) + val /= 256 + return word[-1::-1] + +if __name__ == '__main__': + + prefix = sys.argv[1] + length = int(sys.argv[2]) + target = b58dec(prefix) + + shift = 8*(length+4) + + for m in range(1,1000): + lo = target * 58**m + lo = (lo >> shift) + (0 if lo == ((lo >> shift) << shift) else 1) + hi = (target + 1) * 58**m - (1 << shift) +1 + hi = hi >> shift + if hi >= lo: + # test + for bt in '\x00\xff': + s = bitcoin.bin_to_b58check(bt * length, magicbyte=lo) + assert s.startswith(prefix) + assert len(s) == m + len(prefix) + + print m + len(prefix), lo, asciidec(lo) + exit(0)