def getScriptForUserString(userStr, wltMap, lboxList):
   """
   NOTE: Just like getDisplayStringForScript(), this used to be in ArmoryQt
   but I can envision that it would be useful for reading user input in a
   context other than the GUI.

   The user has entered a string in one of the following ways:
      
      18cnJQ493jnZn99A3QnHggkl832   (addr str)
      3Hgz4nmKasE32W3drXx719cBnM3   (P2SH str)
      Lockbox[Abcd1234]             (Lockbox: P2SH)
      Lockbox[Bare:Abcd1234]        (Lockbox: Plain Multisig)
      04187322ac7f91a3bb0...        (Plain public key)

   If they enter a lockbox by ID and we don't recognize the ID, we don't
   return anything at all.  Mainly because this is to return the script
   to you and there is no way to know what the script is.

   This returns four values:

      1. The script to which the user was intending to send their funds
      If the script is non-empty
         2. A wallet ID if it is part of one of our wallets (or None)
         3. A lockbox ID if it is part of one of our lockboxes (or None)
         4. An indication of whether the entered string has an address in it

   #4 is irrelevant in most contexts and should be ignored.  It is mainly
   for the getDisplayStringForScript() method, which will show you ID if
   an address string is entered, but will show you the address string instead
   if an ID was entered.
   """

   def getWltIDForScrAddr(scrAddr, walletMap):
      for iterID,iterWlt in walletMap.iteritems():
         if iterWlt.hasScrAddr(scrAddr):
            return iterID
      return None

   # Now try to figure it out
   try:
      userStr = userStr.strip()
      outScript = None
      wltID = None
      lboxID = None
      hasAddrInIt = True

      # Check if this corresponds to a lockbox
      if isBareLockbox(userStr) or isP2SHLockbox(userStr):
         parsedLboxID = readLockboxEntryStr(userStr)
         for iterLbox in lboxList:
            # Search for a lockbox with the same ID
            if iterLbox.uniqueIDB58 == parsedLboxID:
               outScript = iterLbox.getScript()
               lboxID = parsedLboxID
               hasAddrInIt = False
               break
      elif len(userStr) in [66,130]:
         # Make hexidecimal numbers not case sensitive
         userStr = userStr.lower()
         # This might be a public key. Confirm it's valid before proceeding.
         if isValidPK(userStr, True):
            sbdKey = SecureBinaryData(hex_to_binary(userStr))
            a160 = sbdKey.getHash160()
            outScript = hash160_to_p2pkhash_script(a160)
            hasAddrInIt = False

            # Check if it's ours
            scrAddr = script_to_scrAddr(outScript)
            wltID = getWltIDForScrAddr(scrAddr, wltMap)
      else:
         scrAddr = addrStr_to_scrAddr(userStr, ADDRBYTE, P2SHBYTE)
         a160 = scrAddr_to_hash160(scrAddr)[1]
         outScript = scrAddr_to_script(scrAddr)
         hasAddrInIt = True

         # Check if it's a wallet scrAddr
         wltID  = getWltIDForScrAddr(scrAddr, wltMap)

         # Check if it's a known P2SH
         for lbox in lboxList:
            if lbox.getAddr() == scrAddr:
               lboxID = lbox.uniqueIDB58
               break

      # Caller might be expecting to see None, instead of '' (empty string)
      wltID  = None if not wltID  else wltID
      lboxID = None if not lboxID else lboxID
      return {'Script': outScript, 
              'WltID':  wltID, 
              'LboxID': lboxID, 
              'ShowID': hasAddrInIt}
   except:
      #LOGEXCEPT('Invalid user string entered')
      return {'Script': None,
              'WltID':  None,
              'LboxID': None,
              'ShowID': None}
Exemple #2
0
def getScriptForUserString(userStr, wltMap, lboxList):
    """
   NOTE: Just like getDisplayStringForScript(), this used to be in ArmoryQt
   but I can envision that it would be useful for reading user input in a
   context other than the GUI.

   The user has entered a string in one of the following ways:
      
      18cnJQ493jnZn99A3QnHggkl832   (addr str)
      3Hgz4nmKasE32W3drXx719cBnM3   (P2SH str)
      Lockbox[Abcd1234]             (Lockbox: P2SH)
      Lockbox[Bare:Abcd1234]        (Lockbox: Plain Multisig)
      04187322ac7f91a3bb0...        (Plain public key)

   If they enter a lockbox by ID and we don't recognize the ID, we don't
   return anything at all.  Mainly because this is to return the script
   to you and there is no way to know what the script is.

   This returns four values:

      1. The script to which the user was intending to send their funds
      If the script is non-empty
         2. A wallet ID if it is part of one of our wallets (or None)
         3. A lockbox ID if it is part of one of our lockboxes (or None)
         4. An indication of whether the entered string has an address in it

   #4 is irrelevant in most contexts and should be ignored.  It is mainly
   for the getDisplayStringForScript() method, which will show you ID if
   an address string is entered, but will show you the address string instead
   if an ID was entered.
   """
    def getWltIDForScrAddr(scrAddr, walletMap):
        for iterID, iterWlt in walletMap.iteritems():
            if iterWlt.hasScrAddr(scrAddr):
                return iterID
        return None

    # Now try to figure it out
    try:
        userStr = userStr.strip()
        outScript = None
        wltID = None
        lboxID = None
        hasAddrInIt = True

        # Check if this corresponds to a lockbox
        if isBareLockbox(userStr) or isP2SHLockbox(userStr):
            parsedLboxID = readLockboxEntryStr(userStr)
            for iterLbox in lboxList:
                # Search for a lockbox with the same ID
                if iterLbox.uniqueIDB58 == parsedLboxID:
                    outScript = iterLbox.binScript
                    if isP2SHLockbox(userStr):
                        outScript = script_to_p2sh_script(iterLbox.binScript)
                    lboxID = parsedLboxID
                    hasAddrInIt = False
                    break
        elif len(userStr) in [66, 130]:
            # Make hexidecimal numbers not case sensitive
            userStr = userStr.lower()
            # This might be a public key. Confirm it's valid before proceeding.
            if isValidPK(userStr, True):
                sbdKey = SecureBinaryData(hex_to_binary(userStr))
                a160 = sbdKey.getHash160()
                outScript = hash160_to_p2pkhash_script(a160)
                hasAddrInIt = False

                # Check if it's ours
                scrAddr = script_to_scrAddr(outScript)
                wltID = getWltIDForScrAddr(scrAddr, wltMap)
        else:
            scrAddr = addrStr_to_scrAddr(userStr)
            a160 = scrAddr_to_hash160(scrAddr)[1]
            outScript = scrAddr_to_script(scrAddr)
            hasAddrInIt = True

            # Check if it's a wallet scrAddr
            wltID = getWltIDForScrAddr(scrAddr, wltMap)

            # Check if it's a known P2SH
            for lbox in lboxList:
                if lbox.p2shScrAddr == scrAddr:
                    lboxID = lbox.uniqueIDB58
                    break

        # Caller might be expecting to see None, instead of '' (empty string)
        wltID = None if not wltID else wltID
        lboxID = None if not lboxID else lboxID
        return {
            'Script': outScript,
            'WltID': wltID,
            'LboxID': lboxID,
            'ShowID': hasAddrInIt
        }
    except:
        #LOGEXCEPT('Invalid user string entered')
        return {'Script': None, 'WltID': None, 'LboxID': None, 'ShowID': None}