From 00079ae156e578f85e5c6e747f7a892b79076ba0 Mon Sep 17 00:00:00 2001 From: MMGen Date: Wed, 25 Apr 2018 13:14:28 +0000 Subject: [PATCH] Implement _b58tonum() as generator func; utf8 label test, bugfixes --- mmgen/protocol.py | 16 +++++++--------- mmgen/tx.py | 2 +- test/test.py | 28 +++++++++++++++------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 6da49aba..1607f8f3 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -42,16 +42,14 @@ def hash256(hexnum): # take hex, return hex - OP_HASH256 _b58a='123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' def _numtob58(num): - ret = [] - while num: - ret.append(_b58a[num % 58]) - num /= 58 - return ''.join(ret)[::-1] + def b58enc(n): + while n: + yield _b58a[n % 58] + n /= 58 + return ''.join(b58enc(num))[::-1] -def _b58tonum(b58num): - if [i for i in b58num if not i in _b58a]: - raise ValueError,'_b58tonum(): invalid b58 value' - return sum(_b58a.index(n) * (58**i) for i,n in enumerate(list(b58num[::-1]))) +def _b58tonum(b58str): + return sum(_b58a.index(ch) * 58**n for n,ch in enumerate(b58str[::-1])) def _b58chk_encode(hexstr): return _numtob58(int(hexstr+hash256(hexstr)[:8],16)) diff --git a/mmgen/tx.py b/mmgen/tx.py index e504413d..e498a4b8 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -722,7 +722,7 @@ class MMGenTX(MMGenObject): assert type(ti['witness']) == list and len(ti['witness']) == 2, 'malformed witness' assert len(ti['witness'][1]) == 66, 'incorrect witness pubkey length' assert mmti.mmid, fs.format('witness-type','non-MMGen') - assert mmti.mmid.mmtype in ('S','B'), fs.format('witness-type',mmti.mmid.mmtype) + assert mmti.mmid.mmtype == ('S','B')[ti['scriptSig']==''],fs.format('witness-type',mmti.mmid.mmtype) else: # non-witness if mmti.mmid: assert mmti.mmid.mmtype not in ('S','B'), fs.format('signature in',mmti.mmid.mmtype) diff --git a/test/test.py b/test/test.py index f42be974..de4291a4 100755 --- a/test/test.py +++ b/test/test.py @@ -738,6 +738,7 @@ cmd_group['regtest'] = ( ('regtest_fund_bob', "funding Bob's wallet"), ('regtest_fund_alice', "funding Alice's wallet"), ('regtest_bob_bal1', "Bob's balance"), + ('regtest_bob_add_label', "adding a 40-character UTF-8 encoded label"), ('regtest_bob_split1', "splitting Bob's funds"), ('regtest_generate', 'mining a block'), ('regtest_bob_bal2', "Bob's balance"), @@ -2480,7 +2481,8 @@ class MMGenTestSuite(object): pw='abc', no_send=False, do_label=False, - bad_locktime=False): + bad_locktime=False, + full_tx_view=False): os.environ['MMGEN_BOGUS_SEND'] = '' t = MMGenExpect(name,'mmgen-txdo', ['-d',cfg['tmpdir'],'-B','--'+user] + @@ -2495,14 +2497,10 @@ class MMGenTestSuite(object): t.expect('Enter transaction fee: ','{}\n'.format(tx_fee)) t.expect('OK? (Y/n): ','y') # fee OK? t.expect('OK? (Y/n): ','y') # change OK? - if do_label: - t.expect('Add a comment to transaction? (y/N): ','y') - t.expect('Comment: ',ref_tx_label.encode('utf8')+'\n') - t.expect('View decoded transaction\? .*?: ','n',regex=True) - else: - t.expect('Add a comment to transaction? (y/N): ','\n') - t.expect('View decoded transaction\? .*?: ','t',regex=True) - t.expect('to continue: ','\n') + t.expect('Add a comment to transaction? (y/N): ',('\n','y')[do_label]) + if do_label: t.expect('Comment: ',ref_tx_label.encode('utf8')+'\n') + t.expect('View decoded transaction\? .*?: ',('t','v')[full_tx_view],regex=True) + if not do_label: t.expect('to continue: ','\n') t.passphrase('MMGen wallet',pw) t.written_to_file('Signed transaction') if no_send: @@ -2517,7 +2515,7 @@ class MMGenTestSuite(object): def regtest_bob_split1(self,name): sid = self.regtest_user_sid('bob') outputs_cl = [sid+':C:1,100', sid+':L:2,200',sid+':'+rtBobOp3] - return self.regtest_user_txdo(name,'bob',rtFee[0],outputs_cl,'1',do_label=True) + return self.regtest_user_txdo(name,'bob',rtFee[0],outputs_cl,'1',do_label=True,full_tx_view=True) def get_addr_from_regtest_addrlist(self,user,sid,mmtype,idx,addr_range='1-5'): id_str = { 'L':'', 'S':'-S', 'C':'-C' }[mmtype] @@ -2663,6 +2661,13 @@ class MMGenTestSuite(object): t.expect('Removed label.*in tracking wallet',regex=True) t.ok() + utf8_label = u'Edited label (40 characters, UTF8) α-β-γ' + utf8_label_pat = ur'Edited label \(40 characters, UTF8\) ..-..-..' + + def regtest_bob_add_label(self,name): + sid = self.regtest_user_sid('bob') + return self.regtest_user_add_label(name,'bob',sid+':C:1',self.utf8_label) + def regtest_alice_add_label1(self,name): sid = self.regtest_user_sid('alice') return self.regtest_user_add_label(name,'alice',sid+':C:1','Original Label') @@ -2726,9 +2731,6 @@ class MMGenTestSuite(object): sid = self.regtest_user_sid('alice') return self.regtest_user_chk_label(name,'alice',sid+':C:1','Replacement Label') - utf8_label = u'Edited label (40 characters, UTF8) α-β-γ' - utf8_label_pat = ur'Edited label \(40 characters, UTF8\) ..-..-..' - def regtest_alice_edit_label1(self,name): return self.regtest_user_edit_label(name,'alice','1',self.utf8_label)