Initial Vexta Core v0.1.0 release
This commit is contained in:
commit
25c454329d
2481 changed files with 689289 additions and 0 deletions
8
contrib/testgen/README.md
Normal file
8
contrib/testgen/README.md
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
### TestGen ###
|
||||
|
||||
Utilities to generate test vectors for the data-driven DigiByte tests.
|
||||
|
||||
Usage:
|
||||
|
||||
PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
|
||||
PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
|
||||
116
contrib/testgen/base58.py
Normal file
116
contrib/testgen/base58.py
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
# Copyright (c) 2012-2018 The Bitcoin Core developers
|
||||
# Copyright (c) 2012-2020 The DigiByte Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
DigiByte base58 encoding and decoding.
|
||||
|
||||
Based on https://digibytetalk.org/index.php?topic=1026.0 (public domain)
|
||||
'''
|
||||
import hashlib
|
||||
|
||||
# for compatibility with following code...
|
||||
class SHA256:
|
||||
new = hashlib.sha256
|
||||
|
||||
if str != bytes:
|
||||
# Python 3.x
|
||||
def ord(c):
|
||||
return c
|
||||
def chr(n):
|
||||
return bytes( (n,) )
|
||||
|
||||
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
||||
__b58base = len(__b58chars)
|
||||
b58chars = __b58chars
|
||||
|
||||
def b58encode(v):
|
||||
""" encode v, which is a string of bytes, to base58.
|
||||
"""
|
||||
long_value = 0
|
||||
for (i, c) in enumerate(v[::-1]):
|
||||
if isinstance(c, str):
|
||||
c = ord(c)
|
||||
long_value += (256**i) * c
|
||||
|
||||
result = ''
|
||||
while long_value >= __b58base:
|
||||
div, mod = divmod(long_value, __b58base)
|
||||
result = __b58chars[mod] + result
|
||||
long_value = div
|
||||
result = __b58chars[long_value] + result
|
||||
|
||||
# DigiByte does a little leading-zero-compression:
|
||||
# leading 0-bytes in the input become leading-1s
|
||||
nPad = 0
|
||||
for c in v:
|
||||
if c == 0:
|
||||
nPad += 1
|
||||
else:
|
||||
break
|
||||
|
||||
return (__b58chars[0]*nPad) + result
|
||||
|
||||
def b58decode(v, length = None):
|
||||
""" decode v into a string of len bytes
|
||||
"""
|
||||
long_value = 0
|
||||
for i, c in enumerate(v[::-1]):
|
||||
pos = __b58chars.find(c)
|
||||
assert pos != -1
|
||||
long_value += pos * (__b58base**i)
|
||||
|
||||
result = bytes()
|
||||
while long_value >= 256:
|
||||
div, mod = divmod(long_value, 256)
|
||||
result = chr(mod) + result
|
||||
long_value = div
|
||||
result = chr(long_value) + result
|
||||
|
||||
nPad = 0
|
||||
for c in v:
|
||||
if c == __b58chars[0]:
|
||||
nPad += 1
|
||||
continue
|
||||
break
|
||||
|
||||
result = bytes(nPad) + result
|
||||
if length is not None and len(result) != length:
|
||||
return None
|
||||
|
||||
return result
|
||||
|
||||
def checksum(v):
|
||||
"""Return 32-bit checksum based on SHA256"""
|
||||
return SHA256.new(SHA256.new(v).digest()).digest()[0:4]
|
||||
|
||||
def b58encode_chk(v):
|
||||
"""b58encode a string, with 32-bit checksum"""
|
||||
return b58encode(v + checksum(v))
|
||||
|
||||
def b58decode_chk(v):
|
||||
"""decode a base58 string, check and remove checksum"""
|
||||
result = b58decode(v)
|
||||
if result is None:
|
||||
return None
|
||||
if result[-4:] == checksum(result[:-4]):
|
||||
return result[:-4]
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_bcaddress_version(strAddress):
|
||||
""" Returns None if strAddress is invalid. Otherwise returns integer version of address. """
|
||||
addr = b58decode_chk(strAddress)
|
||||
if addr is None or len(addr)!=21:
|
||||
return None
|
||||
version = addr[0]
|
||||
return ord(version)
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Test case (from http://gitorious.org/digibyte/python-base58.git)
|
||||
assert get_bcaddress_version('15VjRaDX9zpbA8LVnbrCAFzrVzN7ixHNsC') == 0
|
||||
_ohai = 'o hai'.encode('ascii')
|
||||
_tmp = b58encode(_ohai)
|
||||
assert _tmp == 'DYB3oMS'
|
||||
assert b58decode(_tmp, 5) == _ohai
|
||||
print("Tests passed")
|
||||
132
contrib/testgen/gen_base58_test_vectors.py
Executable file
132
contrib/testgen/gen_base58_test_vectors.py
Executable file
|
|
@ -0,0 +1,132 @@
|
|||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2012-2018 The DigiByte Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
Generate valid and invalid base58 address and private key test vectors.
|
||||
|
||||
Usage:
|
||||
gen_base58_test_vectors.py valid 50 > ../../src/test/data/base58_keys_valid.json
|
||||
gen_base58_test_vectors.py invalid 50 > ../../src/test/data/base58_keys_invalid.json
|
||||
'''
|
||||
# 2012 Wladimir J. van der Laan
|
||||
# Released under MIT License
|
||||
import os
|
||||
from itertools import islice
|
||||
from base58 import b58encode_chk, b58decode_chk, b58chars
|
||||
import random
|
||||
from binascii import b2a_hex
|
||||
|
||||
# key types
|
||||
PUBKEY_ADDRESS = 0
|
||||
SCRIPT_ADDRESS = 5
|
||||
PUBKEY_ADDRESS_TEST = 111
|
||||
SCRIPT_ADDRESS_TEST = 196
|
||||
PRIVKEY = 128
|
||||
PRIVKEY_TEST = 239
|
||||
|
||||
metadata_keys = ['isPrivkey', 'isTestnet', 'addrType', 'isCompressed']
|
||||
# templates for valid sequences
|
||||
templates = [
|
||||
# prefix, payload_size, suffix, metadata
|
||||
# None = N/A
|
||||
((PUBKEY_ADDRESS,), 20, (), (False, False, 'pubkey', None)),
|
||||
((SCRIPT_ADDRESS,), 20, (), (False, False, 'script', None)),
|
||||
((PUBKEY_ADDRESS_TEST,), 20, (), (False, True, 'pubkey', None)),
|
||||
((SCRIPT_ADDRESS_TEST,), 20, (), (False, True, 'script', None)),
|
||||
((PRIVKEY,), 32, (), (True, False, None, False)),
|
||||
((PRIVKEY,), 32, (1,), (True, False, None, True)),
|
||||
((PRIVKEY_TEST,), 32, (), (True, True, None, False)),
|
||||
((PRIVKEY_TEST,), 32, (1,), (True, True, None, True))
|
||||
]
|
||||
|
||||
def is_valid(v):
|
||||
'''Check vector v for validity'''
|
||||
result = b58decode_chk(v)
|
||||
if result is None:
|
||||
return False
|
||||
for template in templates:
|
||||
prefix = bytearray(template[0])
|
||||
suffix = bytearray(template[2])
|
||||
if result.startswith(prefix) and result.endswith(suffix):
|
||||
if (len(result) - len(prefix) - len(suffix)) == template[1]:
|
||||
return True
|
||||
return False
|
||||
|
||||
def gen_valid_vectors():
|
||||
'''Generate valid test vectors'''
|
||||
while True:
|
||||
for template in templates:
|
||||
prefix = bytearray(template[0])
|
||||
payload = bytearray(os.urandom(template[1]))
|
||||
suffix = bytearray(template[2])
|
||||
rv = b58encode_chk(prefix + payload + suffix)
|
||||
assert is_valid(rv)
|
||||
metadata = {x: y for x, y in zip(metadata_keys,template[3]) if y is not None}
|
||||
hexrepr = b2a_hex(payload)
|
||||
if isinstance(hexrepr, bytes):
|
||||
hexrepr = hexrepr.decode('utf8')
|
||||
yield (rv, hexrepr, metadata)
|
||||
|
||||
def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt_suffix):
|
||||
'''Generate possibly invalid vector'''
|
||||
if corrupt_prefix:
|
||||
prefix = os.urandom(1)
|
||||
else:
|
||||
prefix = bytearray(template[0])
|
||||
|
||||
if randomize_payload_size:
|
||||
payload = os.urandom(max(int(random.expovariate(0.5)), 50))
|
||||
else:
|
||||
payload = os.urandom(template[1])
|
||||
|
||||
if corrupt_suffix:
|
||||
suffix = os.urandom(len(template[2]))
|
||||
else:
|
||||
suffix = bytearray(template[2])
|
||||
|
||||
return b58encode_chk(prefix + payload + suffix)
|
||||
|
||||
def randbool(p = 0.5):
|
||||
'''Return True with P(p)'''
|
||||
return random.random() < p
|
||||
|
||||
def gen_invalid_vectors():
|
||||
'''Generate invalid test vectors'''
|
||||
# start with some manual edge-cases
|
||||
yield "",
|
||||
yield "x",
|
||||
while True:
|
||||
# kinds of invalid vectors:
|
||||
# invalid prefix
|
||||
# invalid payload length
|
||||
# invalid (randomized) suffix (add random data)
|
||||
# corrupt checksum
|
||||
for template in templates:
|
||||
val = gen_invalid_vector(template, randbool(0.2), randbool(0.2), randbool(0.2))
|
||||
if random.randint(0,10)<1: # line corruption
|
||||
if randbool(): # add random character to end
|
||||
val += random.choice(b58chars)
|
||||
else: # replace random character in the middle
|
||||
n = random.randint(0, len(val))
|
||||
val = val[0:n] + random.choice(b58chars) + val[n+1:]
|
||||
if not is_valid(val):
|
||||
yield val,
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
import json
|
||||
iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors}
|
||||
try:
|
||||
uiter = iters[sys.argv[1]]
|
||||
except IndexError:
|
||||
uiter = gen_valid_vectors
|
||||
try:
|
||||
count = int(sys.argv[2])
|
||||
except IndexError:
|
||||
count = 0
|
||||
|
||||
data = list(islice(uiter(), count))
|
||||
json.dump(data, sys.stdout, sort_keys=True, indent=4)
|
||||
sys.stdout.write('\n')
|
||||
|
||||
268
contrib/testgen/gen_key_io_test_vectors.py
Executable file
268
contrib/testgen/gen_key_io_test_vectors.py
Executable file
|
|
@ -0,0 +1,268 @@
|
|||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2012-2020 The DigiByte Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
Generate valid and invalid base58/bech32(m) address and private key test vectors.
|
||||
|
||||
Usage:
|
||||
PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
|
||||
PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
|
||||
'''
|
||||
# 2012 Wladimir J. van der Laan
|
||||
# Released under MIT License
|
||||
import os
|
||||
from itertools import islice
|
||||
from base58 import b58encode_chk, b58decode_chk, b58chars
|
||||
import random
|
||||
from segwit_addr import bech32_encode, decode_segwit_address, convertbits, CHARSET, Encoding
|
||||
|
||||
# key types
|
||||
PUBKEY_ADDRESS = 0
|
||||
SCRIPT_ADDRESS = 5
|
||||
PUBKEY_ADDRESS_TEST = 111
|
||||
SCRIPT_ADDRESS_TEST = 196
|
||||
PUBKEY_ADDRESS_REGTEST = 111
|
||||
SCRIPT_ADDRESS_REGTEST = 196
|
||||
PRIVKEY = 128
|
||||
PRIVKEY_TEST = 239
|
||||
PRIVKEY_REGTEST = 239
|
||||
|
||||
# script
|
||||
OP_0 = 0x00
|
||||
OP_1 = 0x51
|
||||
OP_2 = 0x52
|
||||
OP_3 = 0x53
|
||||
OP_16 = 0x60
|
||||
OP_DUP = 0x76
|
||||
OP_EQUAL = 0x87
|
||||
OP_EQUALVERIFY = 0x88
|
||||
OP_HASH160 = 0xa9
|
||||
OP_CHECKSIG = 0xac
|
||||
pubkey_prefix = (OP_DUP, OP_HASH160, 20)
|
||||
pubkey_suffix = (OP_EQUALVERIFY, OP_CHECKSIG)
|
||||
script_prefix = (OP_HASH160, 20)
|
||||
script_suffix = (OP_EQUAL,)
|
||||
p2wpkh_prefix = (OP_0, 20)
|
||||
p2wsh_prefix = (OP_0, 32)
|
||||
p2tr_prefix = (OP_1, 32)
|
||||
|
||||
metadata_keys = ['isPrivkey', 'chain', 'isCompressed', 'tryCaseFlip']
|
||||
# templates for valid sequences
|
||||
templates = [
|
||||
# prefix, payload_size, suffix, metadata, output_prefix, output_suffix
|
||||
# None = N/A
|
||||
((PUBKEY_ADDRESS,), 20, (), (False, 'main', None, None), pubkey_prefix, pubkey_suffix),
|
||||
((SCRIPT_ADDRESS,), 20, (), (False, 'main', None, None), script_prefix, script_suffix),
|
||||
((PUBKEY_ADDRESS_TEST,), 20, (), (False, 'test', None, None), pubkey_prefix, pubkey_suffix),
|
||||
((SCRIPT_ADDRESS_TEST,), 20, (), (False, 'test', None, None), script_prefix, script_suffix),
|
||||
((PUBKEY_ADDRESS_TEST,), 20, (), (False, 'signet', None, None), pubkey_prefix, pubkey_suffix),
|
||||
((SCRIPT_ADDRESS_TEST,), 20, (), (False, 'signet', None, None), script_prefix, script_suffix),
|
||||
((PUBKEY_ADDRESS_REGTEST,), 20, (), (False, 'regtest', None, None), pubkey_prefix, pubkey_suffix),
|
||||
((SCRIPT_ADDRESS_REGTEST,), 20, (), (False, 'regtest', None, None), script_prefix, script_suffix),
|
||||
((PRIVKEY,), 32, (), (True, 'main', False, None), (), ()),
|
||||
((PRIVKEY,), 32, (1,), (True, 'main', True, None), (), ()),
|
||||
((PRIVKEY_TEST,), 32, (), (True, 'test', False, None), (), ()),
|
||||
((PRIVKEY_TEST,), 32, (1,), (True, 'test', True, None), (), ()),
|
||||
((PRIVKEY_TEST,), 32, (), (True, 'signet', False, None), (), ()),
|
||||
((PRIVKEY_TEST,), 32, (1,), (True, 'signet', True, None), (), ()),
|
||||
((PRIVKEY_REGTEST,), 32, (), (True, 'regtest', False, None), (), ()),
|
||||
((PRIVKEY_REGTEST,), 32, (1,), (True, 'regtest', True, None), (), ())
|
||||
]
|
||||
# templates for valid bech32 sequences
|
||||
bech32_templates = [
|
||||
# hrp, version, witprog_size, metadata, encoding, output_prefix
|
||||
('bc', 0, 20, (False, 'main', None, True), Encoding.BECH32, p2wpkh_prefix),
|
||||
('bc', 0, 32, (False, 'main', None, True), Encoding.BECH32, p2wsh_prefix),
|
||||
('bc', 1, 32, (False, 'main', None, True), Encoding.BECH32M, p2tr_prefix),
|
||||
('bc', 2, 2, (False, 'main', None, True), Encoding.BECH32M, (OP_2, 2)),
|
||||
('tb', 0, 20, (False, 'test', None, True), Encoding.BECH32, p2wpkh_prefix),
|
||||
('tb', 0, 32, (False, 'test', None, True), Encoding.BECH32, p2wsh_prefix),
|
||||
('tb', 1, 32, (False, 'test', None, True), Encoding.BECH32M, p2tr_prefix),
|
||||
('tb', 3, 16, (False, 'test', None, True), Encoding.BECH32M, (OP_3, 16)),
|
||||
('tb', 0, 20, (False, 'signet', None, True), Encoding.BECH32, p2wpkh_prefix),
|
||||
('tb', 0, 32, (False, 'signet', None, True), Encoding.BECH32, p2wsh_prefix),
|
||||
('tb', 1, 32, (False, 'signet', None, True), Encoding.BECH32M, p2tr_prefix),
|
||||
('tb', 3, 32, (False, 'signet', None, True), Encoding.BECH32M, (OP_3, 32)),
|
||||
('bcrt', 0, 20, (False, 'regtest', None, True), Encoding.BECH32, p2wpkh_prefix),
|
||||
('bcrt', 0, 32, (False, 'regtest', None, True), Encoding.BECH32, p2wsh_prefix),
|
||||
('bcrt', 1, 32, (False, 'regtest', None, True), Encoding.BECH32M, p2tr_prefix),
|
||||
('bcrt', 16, 40, (False, 'regtest', None, True), Encoding.BECH32M, (OP_16, 40))
|
||||
]
|
||||
# templates for invalid bech32 sequences
|
||||
bech32_ng_templates = [
|
||||
# hrp, version, witprog_size, encoding, invalid_bech32, invalid_checksum, invalid_char
|
||||
('tc', 0, 20, Encoding.BECH32, False, False, False),
|
||||
('bt', 1, 32, Encoding.BECH32M, False, False, False),
|
||||
('tb', 17, 32, Encoding.BECH32M, False, False, False),
|
||||
('bcrt', 3, 1, Encoding.BECH32M, False, False, False),
|
||||
('bc', 15, 41, Encoding.BECH32M, False, False, False),
|
||||
('tb', 0, 16, Encoding.BECH32, False, False, False),
|
||||
('bcrt', 0, 32, Encoding.BECH32, True, False, False),
|
||||
('bc', 0, 16, Encoding.BECH32, True, False, False),
|
||||
('tb', 0, 32, Encoding.BECH32, False, True, False),
|
||||
('bcrt', 0, 20, Encoding.BECH32, False, False, True),
|
||||
('bc', 0, 20, Encoding.BECH32M, False, False, False),
|
||||
('tb', 0, 32, Encoding.BECH32M, False, False, False),
|
||||
('bcrt', 0, 20, Encoding.BECH32M, False, False, False),
|
||||
('bc', 1, 32, Encoding.BECH32, False, False, False),
|
||||
('tb', 2, 16, Encoding.BECH32, False, False, False),
|
||||
('bcrt', 16, 20, Encoding.BECH32, False, False, False),
|
||||
]
|
||||
|
||||
def is_valid(v):
|
||||
'''Check vector v for validity'''
|
||||
if len(set(v) - set(b58chars)) > 0:
|
||||
return is_valid_bech32(v)
|
||||
result = b58decode_chk(v)
|
||||
if result is None:
|
||||
return is_valid_bech32(v)
|
||||
for template in templates:
|
||||
prefix = bytearray(template[0])
|
||||
suffix = bytearray(template[2])
|
||||
if result.startswith(prefix) and result.endswith(suffix):
|
||||
if (len(result) - len(prefix) - len(suffix)) == template[1]:
|
||||
return True
|
||||
return is_valid_bech32(v)
|
||||
|
||||
def is_valid_bech32(v):
|
||||
'''Check vector v for bech32 validity'''
|
||||
for hrp in ['bc', 'tb', 'bcrt']:
|
||||
if decode_segwit_address(hrp, v) != (None, None):
|
||||
return True
|
||||
return False
|
||||
|
||||
def gen_valid_base58_vector(template):
|
||||
'''Generate valid base58 vector'''
|
||||
prefix = bytearray(template[0])
|
||||
payload = bytearray(os.urandom(template[1]))
|
||||
suffix = bytearray(template[2])
|
||||
dst_prefix = bytearray(template[4])
|
||||
dst_suffix = bytearray(template[5])
|
||||
rv = b58encode_chk(prefix + payload + suffix)
|
||||
return rv, dst_prefix + payload + dst_suffix
|
||||
|
||||
def gen_valid_bech32_vector(template):
|
||||
'''Generate valid bech32 vector'''
|
||||
hrp = template[0]
|
||||
witver = template[1]
|
||||
witprog = bytearray(os.urandom(template[2]))
|
||||
encoding = template[4]
|
||||
dst_prefix = bytearray(template[5])
|
||||
rv = bech32_encode(encoding, hrp, [witver] + convertbits(witprog, 8, 5))
|
||||
return rv, dst_prefix + witprog
|
||||
|
||||
def gen_valid_vectors():
|
||||
'''Generate valid test vectors'''
|
||||
glist = [gen_valid_base58_vector, gen_valid_bech32_vector]
|
||||
tlist = [templates, bech32_templates]
|
||||
while True:
|
||||
for template, valid_vector_generator in [(t, g) for g, l in zip(glist, tlist) for t in l]:
|
||||
rv, payload = valid_vector_generator(template)
|
||||
assert is_valid(rv)
|
||||
metadata = {x: y for x, y in zip(metadata_keys,template[3]) if y is not None}
|
||||
hexrepr = payload.hex()
|
||||
yield (rv, hexrepr, metadata)
|
||||
|
||||
def gen_invalid_base58_vector(template):
|
||||
'''Generate possibly invalid vector'''
|
||||
# kinds of invalid vectors:
|
||||
# invalid prefix
|
||||
# invalid payload length
|
||||
# invalid (randomized) suffix (add random data)
|
||||
# corrupt checksum
|
||||
corrupt_prefix = randbool(0.2)
|
||||
randomize_payload_size = randbool(0.2)
|
||||
corrupt_suffix = randbool(0.2)
|
||||
|
||||
if corrupt_prefix:
|
||||
prefix = os.urandom(1)
|
||||
else:
|
||||
prefix = bytearray(template[0])
|
||||
|
||||
if randomize_payload_size:
|
||||
payload = os.urandom(max(int(random.expovariate(0.5)), 50))
|
||||
else:
|
||||
payload = os.urandom(template[1])
|
||||
|
||||
if corrupt_suffix:
|
||||
suffix = os.urandom(len(template[2]))
|
||||
else:
|
||||
suffix = bytearray(template[2])
|
||||
|
||||
val = b58encode_chk(prefix + payload + suffix)
|
||||
if random.randint(0,10)<1: # line corruption
|
||||
if randbool(): # add random character to end
|
||||
val += random.choice(b58chars)
|
||||
else: # replace random character in the middle
|
||||
n = random.randint(0, len(val))
|
||||
val = val[0:n] + random.choice(b58chars) + val[n+1:]
|
||||
|
||||
return val
|
||||
|
||||
def gen_invalid_bech32_vector(template):
|
||||
'''Generate possibly invalid bech32 vector'''
|
||||
no_data = randbool(0.1)
|
||||
to_upper = randbool(0.1)
|
||||
hrp = template[0]
|
||||
witver = template[1]
|
||||
witprog = bytearray(os.urandom(template[2]))
|
||||
encoding = template[3]
|
||||
|
||||
if no_data:
|
||||
rv = bech32_encode(encoding, hrp, [])
|
||||
else:
|
||||
data = [witver] + convertbits(witprog, 8, 5)
|
||||
if template[4] and not no_data:
|
||||
if template[2] % 5 in {2, 4}:
|
||||
data[-1] |= 1
|
||||
else:
|
||||
data.append(0)
|
||||
rv = bech32_encode(encoding, hrp, data)
|
||||
|
||||
if template[5]:
|
||||
i = len(rv) - random.randrange(1, 7)
|
||||
rv = rv[:i] + random.choice(CHARSET.replace(rv[i], '')) + rv[i + 1:]
|
||||
if template[6]:
|
||||
i = len(hrp) + 1 + random.randrange(0, len(rv) - len(hrp) - 4)
|
||||
rv = rv[:i] + rv[i:i + 4].upper() + rv[i + 4:]
|
||||
|
||||
if to_upper:
|
||||
rv = rv.swapcase()
|
||||
|
||||
return rv
|
||||
|
||||
def randbool(p = 0.5):
|
||||
'''Return True with P(p)'''
|
||||
return random.random() < p
|
||||
|
||||
def gen_invalid_vectors():
|
||||
'''Generate invalid test vectors'''
|
||||
# start with some manual edge-cases
|
||||
yield "",
|
||||
yield "x",
|
||||
glist = [gen_invalid_base58_vector, gen_invalid_bech32_vector]
|
||||
tlist = [templates, bech32_ng_templates]
|
||||
while True:
|
||||
for template, invalid_vector_generator in [(t, g) for g, l in zip(glist, tlist) for t in l]:
|
||||
val = invalid_vector_generator(template)
|
||||
if not is_valid(val):
|
||||
yield val,
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
import json
|
||||
iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors}
|
||||
try:
|
||||
uiter = iters[sys.argv[1]]
|
||||
except IndexError:
|
||||
uiter = gen_valid_vectors
|
||||
try:
|
||||
count = int(sys.argv[2])
|
||||
except IndexError:
|
||||
count = 0
|
||||
|
||||
data = list(islice(uiter(), count))
|
||||
json.dump(data, sys.stdout, sort_keys=True, indent=4)
|
||||
sys.stdout.write('\n')
|
||||
|
||||
45
contrib/testgen/key_tests.test_vectors.md
Normal file
45
contrib/testgen/key_tests.test_vectors.md
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
## Test Vectors
|
||||
Private key and address test vectors for test `key_tests` were crafted by hand.
|
||||
The following document illustrates how they have been chosen.
|
||||
|
||||
### compressed private key
|
||||
```
|
||||
80111111111111111111111111111111111111111111111111111111111111111101
|
||||
969b59a7 d30dd0eb7b64944926f4006e84a74a4bdd1f49c30f651082425c0747
|
||||
80111111111111111111111111111111111111111111111111111111111111111101969b59a7
|
||||
KwntMbt59tTsj8xqpqYqRRWufyjGunvhSyeMo3NTYpFYzZbXJ5Hp
|
||||
DU9umLs2Ze8eNRo69wbSj5HeufphJawFPh
|
||||
```
|
||||
|
||||
### uncompressed private key
|
||||
```
|
||||
801111111111111111111111111111111111111111111111111111111111111111
|
||||
e5ce7258 ab95e95003a854ccfca9f027108d9b9b35bc113a9095aeef3e1bc836
|
||||
801111111111111111111111111111111111111111111111111111111111111111e5ce7258
|
||||
5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh
|
||||
DS1P3gwq6MFtatWprr2e2J32xGPd89n75C
|
||||
```
|
||||
|
||||
// 2
|
||||
### compressed private key
|
||||
```
|
||||
80222222222222222222222222222222222222222222222222222222222222222201
|
||||
c2d1512d d4570cc56481d9bf6dbe147ed964a486b7b229f426b1320f77070c52
|
||||
80222222222222222222222222222222222222222222222222222222222222222201c2d1512d
|
||||
KxN4XYdzu6f9j3EMryaMwZvUVLk3y29M4QZ2xwPoFP2zwka1aWxU
|
||||
DCiLe5VAcdLpGXURTDsaoryMWdMVBP9NoN
|
||||
```
|
||||
|
||||
### uncompressed private key
|
||||
```
|
||||
802222222222222222222222222222222222222222222222222222222222222222
|
||||
d15081bf 4cb85f0f73cd32c4b21dace0e3ed3ffce7020023fe448d97b20b278d
|
||||
802222222222222222222222222222222222222222222222222222222222222222d15081bf
|
||||
5J5KUK3VXP8HUefNVYPxwxVRokScZdWXpu1Tj8LfaAXMqHzMmbk
|
||||
DMQ3t1L7BmTwL5wpZ9mek1KUaCRLJSXJWV
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
56
contrib/testgen/miner_gen.cpp
Normal file
56
contrib/testgen/miner_gen.cpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
pblocktemplate->block.hashPrevBlock = uint256S("7497ea1b465eb39f1c8f507bc877078fe016d6fcb6dfad3a64c98dcc6e1e8496");
|
||||
|
||||
while(true) {
|
||||
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
|
||||
std::cout << pblock->hashPrevBlock.ToString() << std::endl;
|
||||
std::cout << chainActive.Tip()->GetBlockHash().ToString() << std::endl;
|
||||
std::cout << "H:" << chainActive.Tip()->nHeight << std::endl;
|
||||
|
||||
pblock->nVersion = 1;
|
||||
pblock->nTime = chainActive.Tip()->GetMedianTimePast()+1;
|
||||
// pblock->nBits = UintToArith256(uint256S("7ffff00000000000000000000000000000000000000000000000000000000000")).GetCompact();
|
||||
// CMutableTransaction txCoinbase(*pblock->vtx[0]);
|
||||
bool processBlock = false;
|
||||
unsigned int a = 17000000;
|
||||
|
||||
while (!processBlock) {
|
||||
pblock->nNonce = a++;
|
||||
if(a % 100000 == 0) {
|
||||
std::cout << "at count " << a << std::endl;
|
||||
}
|
||||
|
||||
for(int j=1; j<7; j++) {
|
||||
// CBlock *pblock = &pblocktemplate->block; // pointer for convenience
|
||||
// pblock->nVersion = 1;
|
||||
// pblock->nTime = chainActive.Tip()->GetMedianTimePast()+1;
|
||||
CMutableTransaction txCoinbase(*pblock->vtx[0]);
|
||||
txCoinbase.nVersion = 1;
|
||||
txCoinbase.vin[0].scriptSig = CScript();
|
||||
txCoinbase.vin[0].scriptSig.push_back(j); //blockinfo[i].extranonce);
|
||||
txCoinbase.vin[0].scriptSig.push_back(chainActive.Height());
|
||||
txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
|
||||
txCoinbase.vout[0].scriptPubKey = CScript();
|
||||
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
|
||||
if (txFirst.size() == 0)
|
||||
baseheight = chainActive.Height();
|
||||
if (txFirst.size() < 4)
|
||||
txFirst.push_back(pblock->vtx[0]);
|
||||
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
|
||||
//pblock->nNonce = blockinfo[i].nonce;
|
||||
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
|
||||
processBlock = ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
|
||||
//BOOST_CHECK(processBlock);
|
||||
if(processBlock) {
|
||||
std::cout << "nounce is " << shared_pblock->nNonce << std::endl;
|
||||
std::cout << "extra nounce is " << j << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
|
||||
// bool processBlock = ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
|
||||
// BOOST_CHECK(ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
|
||||
pblock->hashPrevBlock = pblock->GetHash();
|
||||
}
|
||||
|
||||
return;
|
||||
Loading…
Reference in a new issue