X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=lib%2Fcommands.py;h=22bccafe2f9f7bc7b22b55ea9047b9b494f9c70b;hb=2e1d24939ccb46123974f3338963a65191883981;hp=95b72e1e42304daa4889c7916146b9d4a2992207;hpb=b28a0bcdd4d6489001b72c177c6b8204a7d6c9a6;p=electrum-nvc.git diff --git a/lib/commands.py b/lib/commands.py index 95b72e1..22bccaf 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -16,13 +16,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import datetime import time -from util import * -from bitcoin import * +import copy +from util import print_msg, format_satoshis +from bitcoin import is_valid, hash_160_to_bc_address, hash_160 from decimal import Decimal import bitcoin from transaction import Transaction + class Command: def __init__(self, name, min_args, max_args, requires_network, requires_wallet, requires_password, description, syntax = '', options_syntax = ''): self.name = name @@ -35,7 +38,10 @@ class Command: self.syntax = syntax self.options = options_syntax + known_commands = {} + + def register_command(*args): global known_commands name = args[0] @@ -105,8 +111,6 @@ register_command('getutxoaddress', 2, 2, True, False, False, 'get the addr register_command('sweep', 2, 3, True, False, False, 'Sweep a private key.', 'sweep privkey addr [fee]') - - class Commands: def __init__(self, wallet, network, callback = None): @@ -115,7 +119,6 @@ class Commands: self._callback = callback self.password = None - def _run(self, method, args, password_getter): cmd = known_commands[method] if cmd.requires_password and self.wallet.use_encryption: @@ -127,40 +130,33 @@ class Commands: apply(self._callback, ()) return result - def getaddresshistory(self, addr): return self.network.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0] - def daemon(self, arg): if arg=='stop': return self.network.stop() elif arg=='status': - return { - 'server':self.network.main_server(), + return { + 'server':self.network.main_server(), 'connected':self.network.is_connected() } else: return "unknown command \"%s\""% arg - def listunspent(self): - import copy l = copy.deepcopy(self.wallet.get_unspent_coins()) for i in l: i["value"] = str(Decimal(i["value"])/100000000) return l - def getaddressunspent(self, addr): return self.network.synchronous_get([ ('blockchain.address.listunspent',[addr]) ])[0] - def getutxoaddress(self, txid, num): r = self.network.synchronous_get([ ('blockchain.utxo.get_address',[txid, num]) ]) - if r: + if r: return {'address':r[0] } - def createrawtransaction(self, inputs, outputs): for i in inputs: i['prevout_hash'] = i['txid'] @@ -169,7 +165,6 @@ class Commands: tx = Transaction.from_io(inputs, outputs) return tx - def signrawtransaction(self, raw_tx, private_keys): tx = Transaction(raw_tx) self.wallet.signrawtransaction(tx, private_keys, self.password) @@ -188,10 +183,10 @@ class Commands: redeem_script = Transaction.multisig_script(pubkeys, num) address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5) return {'address':address, 'redeemScript':redeem_script} - + def freeze(self,addr): return self.wallet.freeze(addr) - + def unfreeze(self,addr): return self.wallet.unfreeze(addr) @@ -212,10 +207,9 @@ class Commands: def getpubkeys(self, addr): out = { 'address':addr } - out['pubkeys'] = self.wallet.getpubkeys(addr) + out['pubkeys'] = self.wallet.get_public_keys(addr) return out - def getbalance(self, account= None): if account is None: c, u = self.wallet.get_balance() @@ -232,7 +226,6 @@ class Commands: out["unconfirmed"] = str(Decimal(out["unconfirmed"])/100000000) return out - def getproof(self, addr): p = self.network.synchronous_get([ ('blockchain.address.get_proof',[addr]) ])[0] out = [] @@ -246,9 +239,9 @@ class Commands: return self.network.get_servers() def getversion(self): - import electrum + import electrum # Needs to stay here to prevent ciruclar imports return electrum.ELECTRUM_VERSION - + def getmpk(self): return self.wallet.get_master_public_keys() @@ -264,20 +257,16 @@ class Commands: out = "Error: Keypair import failed: " + str(e) return out - def sweep(self, privkey, to_address, fee = 0.0001): fee = int(Decimal(fee)*100000000) return Transaction.sweep([privkey], self.network, to_address, fee) - def signmessage(self, address, message): return self.wallet.sign_message(address, message, self.password) - def verifymessage(self, address, signature, message): return bitcoin.verify_message(address, signature, message) - def _mktx(self, outputs, fee = None, change_addr = None, domain = None): for to_address, amount in outputs: @@ -292,7 +281,7 @@ class Commands: for addr in domain: if not is_valid(addr): raise Exception("invalid Bitcoin address", addr) - + if not self.wallet.is_mine(addr): raise Exception("address not in wallet", addr) @@ -310,11 +299,10 @@ class Commands: amount = int(100000000*amount) final_outputs.append((to_address, amount)) - + if fee: fee = int(100000000*fee) return self.wallet.mktx(final_outputs, self.password, fee , change_addr, domain) - def mktx(self, to_address, amount, fee = None, change_addr = None, domain = None): tx = self._mktx([(to_address, amount)], fee, change_addr, domain) return tx @@ -323,7 +311,6 @@ class Commands: tx = self._mktx(outputs, fee, change_addr, domain) return tx - def payto(self, to_address, amount, fee = None, change_addr = None, domain = None): tx = self._mktx([(to_address, amount)], fee, change_addr, domain) r, h = self.wallet.sendtx( tx ) @@ -334,9 +321,7 @@ class Commands: r, h = self.wallet.sendtx( tx ) return h - def history(self): - import datetime balance = 0 out = [] for item in self.wallet.get_tx_history(): @@ -351,20 +336,15 @@ class Commands: out.append({'txid':tx_hash, 'date':"%16s"%time_str, 'label':label, 'value':format_satoshis(value)}) return out - - def setlabel(self, key, label): self.wallet.set_label(key, label) - - def contacts(self): c = {} for addr in self.wallet.addressbook: c[addr] = self.wallet.labels.get(addr) return c - def listaddresses(self, show_all = False, show_label = False): out = [] for addr in self.wallet.addresses(True): @@ -379,7 +359,7 @@ class Commands: item = addr out.append( item ) return out - + def help(self, cmd=None): if cmd not in known_commands: print_msg("\nList of commands:", ', '.join(sorted(known_commands))) @@ -390,9 +370,7 @@ class Commands: if cmd.options: print_msg("options:\n" + cmd.options) return None - def getrawtransaction(self, tx_hash): - import transaction if self.wallet: tx = self.wallet.transactions.get(tx_hash) if tx: @@ -400,17 +378,12 @@ class Commands: r = self.network.synchronous_get([ ('blockchain.transaction.get',[tx_hash]) ])[0] if r: - return transaction.Transaction(r) + return Transaction(r) else: return "unknown transaction" - def encrypt(self, pubkey, message): return bitcoin.encrypt_message(message, pubkey) - def decrypt(self, pubkey, message): return self.wallet.decrypt_message(pubkey, message, self.password) - - -