Skip to content

Commit

Permalink
contrib: testgen: use base58 methods from test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
theStack committed Apr 5, 2022
1 parent 605fecf commit 219d2c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 121 deletions.
115 changes: 0 additions & 115 deletions contrib/testgen/base58.py

This file was deleted.

16 changes: 10 additions & 6 deletions contrib/testgen/gen_key_io_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
'''

import os
from itertools import islice
from base58 import b58encode_chk, b58decode_chk, b58chars
import os
import random
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '../../test/functional'))

from test_framework.address import base58_to_byte, byte_to_base58, b58chars # noqa: E402
from test_framework.script import OP_0, OP_1, OP_2, OP_3, OP_16, OP_DUP, OP_EQUAL, OP_EQUALVERIFY, OP_HASH160, OP_CHECKSIG # noqa: E402
from test_framework.segwit_addr import bech32_encode, decode_segwit_address, convertbits, CHARSET, Encoding # noqa: E402

Expand Down Expand Up @@ -108,8 +108,10 @@ 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:
try:
payload, version = base58_to_byte(v)
result = bytes([version]) + payload
except AssertionError: # thrown if checksum doesn't match
return is_valid_bech32(v)
for template in templates:
prefix = bytearray(template[0])
Expand All @@ -133,7 +135,8 @@ def gen_valid_base58_vector(template):
suffix = bytearray(template[2])
dst_prefix = bytearray(template[4])
dst_suffix = bytearray(template[5])
rv = b58encode_chk(prefix + payload + suffix)
assert len(prefix) == 1
rv = byte_to_base58(payload + suffix, prefix[0])
return rv, dst_prefix + payload + dst_suffix

def gen_valid_bech32_vector(template):
Expand Down Expand Up @@ -184,7 +187,8 @@ def gen_invalid_base58_vector(template):
else:
suffix = bytearray(template[2])

val = b58encode_chk(prefix + payload + suffix)
assert len(prefix) == 1
val = byte_to_base58(payload + suffix, prefix[0])
if random.randint(0,10)<1: # line corruption
if randbool(): # add random character to end
val += random.choice(b58chars)
Expand Down

0 comments on commit 219d2c7

Please sign in to comment.