def register(myNymId, serverId): myNymId = str(myNymId) serverId = str(serverId) objEasy = otapi.OT_ME() result = objEasy.register_nym(serverId, myNymId) if result: return {} # OK else: return {'error': 'Failed registering nym\n' + result}
def refresh(myAccId): myAccId = str(myAccId) myNymId = otapi.OTAPI_Wrap_GetAccountWallet_NymID(myAccId) serverId = otapi.OTAPI_Wrap_GetAccountWallet_ServerID(myAccId) objEasy = otapi.OT_ME() result = objEasy.retrieve_account(serverId, myNymId, myAccId, True) if not result: return {'error': 'Failed trying to refresh wallet.'} return {} # OK
def register(serverId, myNymId): serverId = str(serverId) myNymId = str(myNymId) objEasy = otapi.OT_ME() strResponse = objEasy.register_nym(serverId, myNymId) nSuccess = int(strResponse) if nSuccess is 1: return {} # OK else: if strResponse: return {'error': 'Error in register_nym! ' + strResponse} else: return {'error': 'Error in register_nym!'}
def issue(myNymId, serverId, contract): myNymId = str(myNymId) serverId = str(serverId) contract = str(contract) if not otapi.OTAPI_Wrap_IsNym_RegisteredAtServer(myNymId, serverId): # If the Nym's not registered at the server, then register him first. result = ot_nym.register(myNymId, serverId) if 'error' in result: return result objEasy = otapi.OT_ME() result = objEasy.issue_asset_type(serverId, myNymId, contract) if result: return {} # OK else: return {'error': 'Failed trying to issue an asset\n'+result}
def create_account(myNymId, serverId, assetId): myNymId = str(myNymId) serverId = str(serverId) assetId = str(assetId) if not otapi.OTAPI_Wrap_IsNym_RegisteredAtServer(myNymId, serverId): # If the Nym's not registered at the server, then register him first. result = ot_nym.register(myNymId, serverId) if 'error' in result: return result objEasy = otapi.OT_ME() result = objEasy.create_asset_acct(serverId, myNymId, assetId) if result: return {} # OK else: return {'error': 'Failed trying to create account\n'+result}
def deposit_cheque(serverId, myAccId, myNymId, instrument, type): strAssetTypeID = otapi.OTAPI_Wrap_Instrmnt_GetAssetID(instrument) if not strAssetTypeID: return {'error': "Unable to find Asset Type ID on the instrument."} strAssetTypeAcct = otapi.OTAPI_Wrap_GetAccountWallet_AssetTypeID(myAccId) if strAssetTypeID != strAssetTypeAcct: errorMessage = ( "Asset Type ID on the instrument ( "+strAssetTypeID+" ) " "doesn't match the one on the MyAcct "+strAssetTypeAcct ) return {'error': errorMessage} # Here, we send the deposit cheque request to the server objEasy = otapi.OT_ME() strResponse = objEasy.deposit_cheque(serverId, myNymId, myAccId, instrument) strAttempt = 'deposit_cheque' # Here, we interpret the server reply, whether success, fail, or error... nInterpretReply = objEasy.InterpretTransactionMsgReply(serverId, myNymId, myAccId, strAttempt, strResponse) if nInterpretReply == 1: # Download all the intermediary files (account balance, inbox, outbox, etc) # since they have probably changed from this operation. # bForceDownload defaults to false. bRetrieved = objEasy.retrieve_account(serverId, myNymId, myAccId, True) if not bRetrieved: logMessage = ( "Failed retrieving intermediary files from account." ) return {'log': logMessage} return {} # OK return {'error': "Failed to deposit cheque."}
def send_transfer(myAccId, hisAccId, amount, memo): myAccId = str(myAccId) hisAccId = str(hisAccId) amount = str(amount) memo = str(memo) myNymId = otapi.OTAPI_Wrap_GetAccountWallet_NymID(myAccId) if not myNymId: errorMessage = ("Unable to find NymID (for sender) based on myAccId.\n" "The designated asset account must be yours." "OT will find the Nym based on the account.") return {'error': errorMessage} myServerId = otapi.OTAPI_Wrap_GetAccountWallet_ServerID(myAccId) if not myServerId: errorMessage = ("Unable to find ServerID based on myAccId.\n" "The designated asset account must be yours. " "OT will find the Server based on the account.") return {'error': errorMessage} hisServerId = otapi.OTAPI_Wrap_GetAccountWallet_ServerID(hisAccId) if not hisServerId: print 'hisAcctId is not in the wallet, so I\'m assuming it\'s on the same server as myAccId. (Proceeding.)' hisServerId = myServerId if myServerId != hisServerId: errorMessage = ( "hisAccId is not on the same server as myAccId " "(he's on " + hisServerId + " but myAccId is on " + myServerId + "). " "You must choose either a different sender account or a " "different recipient account") return {'error': errorMessage} assetTypeId = otapi.OTAPI_Wrap_GetAccountWallet_AssetTypeID(myAccId) assetAmount = otapi.OTAPI_Wrap_StringToAmount(assetTypeId, amount) objEasy = otapi.OT_ME() response = objEasy.send_transfer(myServerId, myNymId, myAccId, hisAccId, assetAmount, memo) attempt = 'send_transfer' interpretReply = objEasy.InterpretTransactionMsgReply( myServerId, myNymId, myAccId, attempt, response) if int(interpretReply) is not -1: # Download all the intermediary files # (account balance, inbox, outbox, etc) # since they have probably changed from this operation. retrieved = objEasy.retrieve_account(myServerId, myNymId, myAccId, True) print 'Server response (' + attempt + '): SUCCESS sending transfer!' if retrieved: print "Success retrieving intermediary files for account." else: print "Failed retrieving intermediary files for account." return {} # OK else: errorMessage = ( "Unexpected error. Verify if the accounts really exist.") return {'error': errorMessage}
def handle_payment_index(myAccId, nIndex, strPaymentType, strInbox): myNymId = otapi.OTAPI_Wrap_GetAccountWallet_NymID(myAccId) if not myNymId: errorMessage = ( "Unable to find NymID based on myAccId\n" "The designated asset account must be yours. OT will find " "the Nym based on the account." ) return {'error': errorMessage} serverId = otapi.OTAPI_Wrap_GetAccountWallet_ServerID(myAccId) if not serverId: errorMessage = ( "Unable to find Server ID based on myAccId.\n" "The designated asset account must be yours. OT will find " "the Server based on the account." ) return {'error': errorMessage} instrument = '' if nIndex == -1: return {'error': "You must specify an index in the payments inbox"} else: # Use an instrument from the payments inbox, since a valid index was provided. objEasy = otapi.OT_ME() # strInbox is optional and avoids having to load it multiple times. This function will just load it itself, if it has to. instrument = objEasy.get_payment_instrument(serverId, myNymId, nIndex, strInbox) if not instrument: errorMessage = ( "Unable to get payment instrument based on index: "+nIndex ) return {'error': errorMessage} # By this point, instrument is a valid string (whether we got it from the payments inbox, # or whether we got it from stdin.) type = otapi.OTAPI_Wrap_Instrmnt_GetType(instrument) if not type: errorMessage = ( "Unable to determine instrument's type. " "Expected CHEQUE, VOUCHER, INVOICE, or (cash) PURSE." ) return {'error': errorMessage} strIndexErrorMsg = '' if nIndex != -1: strIndexErrorMsg = 'at index '+nIndex # If there's a payment type, # and it's not "ANY", and it's the wrong type, # then skip this one. if not strPaymentType and (strPaymentType != 'ANY') and (strPaymentType != type): if not ((('CHEQUE' == strPaymentType) and ('VOUCHER' == type)) or (('VOUCHER' == strPaymentType) and ('CHEQUE' == type))): errorMessage = ( "The instrument "+strIndexErrorMsg+"is not a " ""+strPaymentType+". (It's a "+type+". Skipping.)" ) return {'error': errorMessage} # in this case we allow it to drop through. # By this point, we know the invoice has the right asset type for the account # we're trying to use (to pay it from.) # # But we need to make sure the invoice is made out to myNymId (or to no one.) # Because if it IS endorsed to a Nym, and myNymId is NOT that nym, then the # transaction will fail. So let's check, before we bother sending it... strRecipientUserID = otapi.OTAPI_Wrap_Instrmnt_GetRecipientUserID(instrument) # Not all instruments have a specified recipient. But if they do, let's make # sure the Nym matches. if strRecipientUserID and (strRecipientUserID != myNymId): errorMessage = ( "The instrument "+strIndexErrorMsg+" is endorsed to a " "specific recipient ("+strRecipientUserID+") and that " "doesn't match the account's owner Nym ("+myNymId+"). " "(Skipping.)\nTry specifying a different account" ) return {'error': errorMessage} # At this point I know the invoice isn't made out to anyone, or if it is, it's properly # made out to the owner of the account which I'm trying to use to pay the invoice from. # So let's pay it! P.S. strRecipientUserID might be NULL, but myNymId is guaranteed # to be good. instrumentAssetType = otapi.OTAPI_Wrap_Instrmnt_GetAssetID(instrument) strAccountAssetID = otapi.OTAPI_Wrap_GetAccountWallet_AssetTypeID(myAccId) if instrumentAssetType and (strAccountAssetID != instrumentAssetType): errorMessage = ( "The instrument at index "+nIndex+" has a different " "asset type than the selected account. (Skipping.)\n" "Try specifying a different account" ) return {'error': errorMessage} tFrom = otapi.OTAPI_Wrap_Instrmnt_GetValidFrom(instrument) tTo = otapi.OTAPI_Wrap_Instrmnt_GetValidTo(instrument) tTime = otapi.OTAPI_Wrap_GetTime() if (tTime < tFrom): errorMessage = ( "The instrument at index "+nIndex+" is not yet within " "its valid date range. (Skipping.)" ) return {'error': errorMessage} if (tTo > 0) and (tTime > tTo): print "The instrument at index "+nIndex+" is expired. \ (Moving it to the record box.)" # Since this instrument is expired, remove it from the payments inbox, and move to record box. # Note: this harvests # bSaveCopy = true. (Since it's expired, it'll go into the expired box.) if (nIndex >= 0) and otapi.OTAPI_Wrap_RecordPayment(serverId, myNymId, true, nIndex, true): return {'handled': True} return {'error': "Failed trying to record payment"} # TODO, IMPORTANT: After the below deposits are completed successfully, the wallet # will receive a "successful deposit" server reply. When that happens, OT (internally) # needs to go and see if the deposited item was a payment in the payments inbox. If so, # it should REMOVE it from that box and move it to the record box. # # That's why you don't see me messing with the payments inbox even when these are successful. # They DO need to be removed from the payments inbox, but just not here in the script. (Rather, # internally by OT itself.) if type == 'CHEQUE': return deposit_cheque(serverId, myAccId, myNymId, instrument, type) elif type == "VOUCHER": return deposit_cheque(serverId, myAccId, myNymId, instrument, type) elif type == "INVOICE": return deposit_cheque(serverId, myAccId, myNymId, instrument, type) elif type == "PURSE": nDepositPurse = deposit_purse(serverId, myAccId, myNymId, instrument, '') # strIndices is left blank in this case # if nIndex != (-1), go ahead and call RecordPayment on the purse at that index, to # remove it from payments inbox and move it to the recordbox. # if nIndex != -1 and nDepositPurse == 1: nRecorded = otapi.OTAPI_Wrap_RecordPayment(serverId, myNymId, true, nIndex, true) # bSaveCopy=true. return nDepositPurse else: errorMessage = ( "Skipping this instrument: Expected CHEQUE, " "VOUCHER, INVOICE, or (cash) PURSE." ) return {'error': errorMessage}
def accept_inbox_items(myAccId, nItemType, strIndices): myAccId = str(myAccId) nItemType = int(nItemType) strIndices = str(strIndices) myNymId = otapi.OTAPI_Wrap_GetAccountWallet_NymID(myAccId) if not myNymId: errorMessage = ( "Unable to find NymID based on the specified account "+myAccId ) return {'error': errorMessage} serverId = otapi.OTAPI_Wrap_GetAccountWallet_ServerID(myAccId) if not serverId: errorMessage = ( "Unable to find Server ID based on the specified account "+myAccId ) return {'error': errorMessage} # User may have already chosen indices (passed in) so we don't want to # re-download the inbox unless we HAVE to. But if the hash has changed, that's # one clear-cut case where we _do_ have to. Otherwise our balance agreement # will fail anyway. So hopefully we can let OT "be smart about it" here instead # of just forcing it to download every time even when unnecessary. objEasy = otapi.OT_ME() result = objEasy.retrieve_account(serverId, myNymId, myAccId, False) if not result: return {'error': 'Unable to download the intermediary files.'} # Make sure we have at least one transaction number (to process the inbox with.) # # NOTE: Normally we don't have to do this, because the high-level API is smart # enough, when sending server transaction requests, to grab new transaction numbers # if it is running low. # But in this case, we need the numbers available BEFORE sending the transaction # request, because the call to otapi.OTAPI_Wrap_Ledger_CreateResponse is where the number # is first needed, and that call is made long before the server transaction request # is actually sent. if not objEasy.make_sure_enough_trans_nums(10, serverId, myNymId): return {'error': 'Unable to have at least one transaction number'} # Returns NULL, or an inbox. strInbox = otapi.OTAPI_Wrap_LoadInbox(serverId, myNymId, myAccId) if not strInbox: return {'error': 'otapi.OTAPI_Wrap_LoadInbox: Failed.'} else: nCount = otapi.OTAPI_Wrap_Ledger_GetCount(serverId, myNymId, myAccId, strInbox) if nCount and nCount > 0: # NOTE!!! DO **NOT** create the response ledger until the FIRST iteration of the below loop that actually # creates a transaction response! If that "first iteration" never comes (due to receipts being skipped, etc) # then otapi.OTAPI_Wrap_Transaction_CreateResponse will never get called, and therefore Ledger_CreateResponse should # also not be called, either. (Nor should otapi.OTAPI_Wrap_Ledger_FinalizeResponse, etc.) strResponseLEDGER = '' nIndicesCount = otapi.OTAPI_Wrap_NumList_Count(strIndices) if strIndices else 0 for i in range(nCount): strTrans = otapi.OTAPI_Wrap_Ledger_GetTransactionByIndex(serverId, myNymId, myAccId, strInbox, i) # ---------------------------------------------------------- # nItemType == 0 for all, 1 for transfers only, 2 for receipts only. # strIndices == "" for "all indices" # print 'P6' if nItemType > 0: # 0 means "all", so we don't have to skip anything based on type, if it's 0. strTransType = otapi.OTAPI_Wrap_Transaction_GetType(serverId, myNymId, myAccId, strTrans) # incoming transfer print 'P4' if strTransType == 'pending' and nItemType != 1: continue # receipt print 'P5' if strTransType != 'pending' and nItemType != 2: # if it is NOT an incoming transfer, then it's a receipt. If we're not doing receipts, then skip it. continue # ---------------------------------------------------------- # - If NO indices are specified, process them ALL. # # - If indices are specified, but the current index is not on # that list, then continue... # if nIndicesCount > 0 and not otapi.OTAPI_Wrap_NumList_VerifyQuery(strIndices, str(i)): continue # By this point we know we actually have to call otapi.OTAPI_Wrap_Transaction_CreateResponse # Therefore, if otapi.OTAPI_Wrap_Ledger_CreateResponse has not yet been called (which it won't # have been, the first time we hit this in this loop), then we call it here this one # time, to get things started... # print 'P1' if not strResponseLEDGER: strResponseLEDGER = otapi.OTAPI_Wrap_Ledger_CreateResponse(serverId, myNymId, myAccId, strInbox) print 'P2' if not strResponseLEDGER: errorMessage = ( "otapi.OTAPI_Wrap_Ledger_CreateResponse " "returned NULL." ) return {'error': errorMessage} # ---------------------------- # By this point, we know the ledger response exists, and we know we have to create # a transaction response to go inside of it, so let's do that next... # accept = True (versus rejecting a pending transfer, for example.) strNEW_ResponseLEDGER = otapi.OTAPI_Wrap_Transaction_CreateResponse(serverId, myNymId, myAccId, strResponseLEDGER, strTrans, True) print 'P3' if not strNEW_ResponseLEDGER: errorMessage = ( "otapi.OTAPI_Wrap_Transaction_CreateResponse " "returned NULL." ) return {'error': errorMessage} strResponseLEDGER = strNEW_ResponseLEDGER if not strResponseLEDGER: # This means there were receipts in the box, but they were skipped. # And after the skipping was done, there were no receipts left. # So we can't just say "the box is empty" because it's not. But nevertheless, # we aren't actually processing any of them, so we return 0 AS IF the box # had been empty. (Because this is not an error condition. Just a "no op".) logMessage = ( "There were receipts in the box, but they were skipped." ) return {'log': logMessage} # ------------------------------------------- # Below this point, we know strResponseLEDGER needs to be sent, # so let's finalize it. # strFinalizedResponse = otapi.OTAPI_Wrap_Ledger_FinalizeResponse(serverId, myNymId, myAccId, strResponseLEDGER) if not strFinalizedResponse: errorMessage = ( "otapi.OTAPI_Wrap_Ledger_FinalizeResponse returned NULL." ) return {'error': errorMessage} # Server communications are handled here... strResponse = objEasy.process_inbox(serverId, myNymId, myAccId, strFinalizedResponse) strAttempt = 'process_inbox' nInterpretReply = objEasy.InterpretTransactionMsgReply(serverId, myNymId, myAccId, strAttempt, strResponse) if nInterpretReply == 1: # Download all the intermediary files (account balance, inbox, outbox, etc) # since they have probably changed from this operation. bRetrieved = objEasy.retrieve_account(serverId, myNymId, myAccId, True) if not bRetrieved: logMessage = ( "Failed retrieving intermediary files for account." ) return {'log': logMessage} return {} # OK if not nInterpretReply: return {'error': "Failed accepting inbox items."} return {'log': 'empty'}