Example #1
0
    def run_test(self, name, ut):

        msg_r('Testing BIP39 conversion routines...')
        qmsg('')

        from mmgen.bip39 import bip39

        bip39.check_wordlists()
        bip39.check_wordlist('bip39')

        vmsg('')
        qmsg('Checking seed to mnemonic conversion:')
        for v in self.vectors:
            chk = tuple(v[1].split())
            vmsg('    ' + v[1])
            res = bip39.fromhex(v[0], 'bip39')
            assert res == chk, 'mismatch:\nres: {}\nchk: {}'.format(res, chk)

        vmsg('')
        qmsg('Checking mnemonic to seed conversion:')
        for v in self.vectors:
            chk = v[0]
            vmsg('    ' + chk)
            res = bip39.tohex(v[1].split(), 'bip39')
            assert res == chk, 'mismatch:\nres: {}\nchk: {}'.format(res, chk)

        vmsg('')
        qmsg('Checking error handling:')

        good_mn = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong".split()
        bad_len_mn = "zoo zoo zoo".split()
        bad_chksum_mn = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo".split(
        )
        bad_word_mn = "admire zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo".split(
        )
        bad_seed = 'deadbeef'
        good_seed = 'deadbeef' * 4

        th = bip39.tohex
        fh = bip39.fromhex
        bad_data = (
            ('hex', 'AssertionError', 'not a hexadecimal',
             lambda: fh('xx', 'bip39')),
            ('id (tohex)', 'AssertionError', "must be 'bip39'",
             lambda: fh(good_seed, 'foo')),
            ('seed len', 'AssertionError', 'invalid seed bit',
             lambda: fh(bad_seed, 'bip39')),
            ('mnemonic type', 'AssertionError', 'must be list',
             lambda: th('string', 'bip39')),
            ('id (fromhex)', 'AssertionError', "must be 'bip39'",
             lambda: th(good_mn, 'foo')),
            ('arg (tostr=True)', 'AssertionError', "'tostr' must be",
             lambda: fh(good_seed, 'bip39', tostr=True)),
            ('pad len (fromhex)', 'AssertionError', "invalid pad len",
             lambda: fh(good_seed, 'bip39', pad=23)),
            ('pad len (tohex)', 'AssertionError', "invalid pad len",
             lambda: th(good_mn, 'bip39', pad=23)),
            ('word', 'MnemonicError', "not in the BIP39",
             lambda: th(bad_word_mn, 'bip39')),
            ('checksum', 'MnemonicError', "checksum",
             lambda: th(bad_chksum_mn, 'bip39')),
            ('seed phrase len', 'MnemonicError', "phrase len",
             lambda: th(bad_len_mn, 'bip39')),
        )

        ut.process_bad_data(bad_data)

        vmsg('')
        msg('OK')

        return True
Example #2
0
		def bad10(): bip39.tohex(bad_len_mn,'bip39')

		for i in range(len(bad_data)):
Example #3
0
	def run_test(self,name):

		msg_r('Testing BIP39 conversion routines...')
		qmsg('')

		from mmgen.bip39 import bip39

		bip39.check_wordlists()
		bip39.check_wordlist('bip39')

		vmsg('')
		qmsg('Checking seed to mnemonic conversion:')
		for v in self.vectors:
			chk = tuple(v[1].split())
			vmsg('    '+v[1])
			res = bip39.fromhex(v[0],'bip39')
			assert res == chk, 'mismatch:\nres: {}\nchk: {}'.format(res,chk)

		vmsg('')
		qmsg('Checking mnemonic to seed conversion:')
		for v in self.vectors:
			chk = v[0]
			vmsg('    '+chk)
			res = bip39.tohex(v[1].split(),'bip39')
			assert res == chk, 'mismatch:\nres: {}\nchk: {}'.format(res,chk)

		vmsg('')
		qmsg('Checking error handling:')

		bad_data = (
			('bad hex',                  'AssertionError',   'not a hexadecimal'),
			('bad id (tohex)',           'AssertionError',   "must be 'bip39'"),
			('bad seed len',             'AssertionError',   'invalid seed bit length'),
			('bad mnemonic type',        'AssertionError',   'must be list'),
			('bad id (fromhex)',         'AssertionError',   "must be 'bip39'"),
			('tostr = True',             'AssertionError',   "'tostr' must be"),
			('bad pad length (fromhex)', 'AssertionError',   "invalid pad len"),
			('bad pad length (tohex)',   'AssertionError',   "invalid pad len"),
			('bad word',                 'MnemonicError',    "not in the BIP39 word list"),
			('bad checksum',             'MnemonicError',    "checksum"),
			('bad seed phrase length',   'MnemonicError',    "phrase len"),
		)

		good_mn = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong".split()
		bad_len_mn = "zoo zoo zoo".split()
		bad_chksum_mn = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo".split()
		bad_word_mn = "admire zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo".split()
		bad_seed = 'deadbeef'
		good_seed = 'deadbeef' * 4

		def bad0(): bip39.fromhex('xx','bip39')
		def bad1(): bip39.fromhex(good_seed,'foo')
		def bad2(): bip39.fromhex(bad_seed,'bip39')
		def bad3(): bip39.tohex('string','bip39')
		def bad4(): bip39.tohex(good_mn,'foo')
		def bad5(): bip39.fromhex(good_seed,'bip39',tostr=True)
		def bad6(): bip39.fromhex(good_seed,'bip39',pad=23)
		def bad7(): bip39.tohex(good_mn,'bip39',pad=23)
		def bad8(): bip39.tohex(bad_word_mn,'bip39')
		def bad9(): bip39.tohex(bad_chksum_mn,'bip39')
		def bad10(): bip39.tohex(bad_len_mn,'bip39')

		for i in range(len(bad_data)):
			try:
				vmsg_r('    {:26}'.format(bad_data[i][0]+':'))
				locals()['bad'+str(i)]()
			except Exception as e:
				n = type(e).__name__
				vmsg(' {:15} [{}]'.format(n,e.args[0]))
				assert n == bad_data[i][1]
				assert bad_data[i][2] in e.args[0]
			else:
				rdie(3,"\nillegal action '{}' failed to raise exception".format(bad_data[n][0]))

		vmsg('')
		msg('OK')

		return True
Example #4
0
		def bad9(): bip39.tohex(bad_chksum_mn,'bip39')
		def bad10(): bip39.tohex(bad_len_mn,'bip39')
Example #5
0
		def bad8(): bip39.tohex(bad_word_mn,'bip39')
		def bad9(): bip39.tohex(bad_chksum_mn,'bip39')
Example #6
0
		def bad7(): bip39.tohex(good_mn,'bip39',pad=23)
		def bad8(): bip39.tohex(bad_word_mn,'bip39')
Example #7
0
		def bad4(): bip39.tohex(good_mn,'foo')
		def bad5(): bip39.fromhex(good_seed,'bip39',tostr=True)
Example #8
0
		def bad3(): bip39.tohex('string','bip39')
		def bad4(): bip39.tohex(good_mn,'foo')