Esempio n. 1
0
    def create_wallet(self, method, input, aes_pw):
        privkey = ''
        if method == 'wif':
            fmt = bc.get_privkey_format(input)
            if fmt == 'wif':
                privkey = bc.encode_privkey(input, 'hex')
            elif fmt == 'wif_compressed':
                privkey = bc.encode_privkey(input, 'hex_compressed')
            else:
                raise Exception('Unrecoginized format for private key.')
                
        elif method == 'random':
            privkey = bc.random_key()
        else:
            raise Exception('Unsupported method: {0}.'.format(method))
        
        wal_addr = bc.privtoaddr(privkey, self.magic_byte)
        encr_privkey = wallet.encrypt_privkey(privkey, aes_pw)
        wallet.create_wallet_file(self.wallet_filename, encr_privkey, wal_addr)
        
        # Read back from wallet to ensure consistency
        encr_privkey2, wal_addr2 = wallet.read_from_wallet_file(self.wallet_filename)
        privkey2 = wallet.decrypt_privkey(encr_privkey2, aes_pw)
        
        if encr_privkey2 != encr_privkey or wal_addr2 != wal_addr:
            raise Exception('Inconsistency in reading from/writing to wallet!')

        if privkey2 != privkey:
            raise Exception('Inconsistency in encrypting/decrypting private key!')
        
        return
Esempio n. 2
0
 def load_wallet(self):
     
     if not os.path.exists(self.wallet_filename):
         raise OSError('Wallet filename not found')
     
     self.encr_privkey, self.addr = wallet.read_from_wallet_file(self.wallet_filename)
     
     if self.user_mode == 'demo':
         self.unspent = self.demo_unspent
     elif self.user_mode == 'mainnet':
         try:
             self.unspent = bc.unspent(self.addr)
         except:
             try:
                 self.unspent = bc.blockr_unspent(self.addr)
             except:
                 raise IOError('Could not get unspent outputs.')
     elif self.user_mode == 'testnet':
         try:
             self.unspent = bc.blockr_unspent(self.addr, 'testnet')
         except:
             raise IOError('Could not get unspent outputs.')
     else:
         raise Exception('Unsupported user_mode ' + self.user_mode)
     
     self.balance = core.get_balance(self.unspent)
     self.is_wallet_loaded = True
     
     return