From 5daa9d89fc28f0c5ac8706bc8bde26d82eac0603 Mon Sep 17 00:00:00 2001 From: moneromooo Date: Mon, 9 Mar 2015 18:46:55 +0000 Subject: [PATCH] Payment IDs can now be deterministic or non-deterministic Also add a site salt to create them, so a given user ID will yield different payment IDs for different sites. --- README | 17 ++++++++++++----- monero/monero.py | 7 ++++--- monero/utils.py | 13 +++++++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/README b/README index 1d5553f..1e6675d 100644 --- a/README +++ b/README @@ -12,7 +12,13 @@ Calling raw module functions is undocumented and subject to change. * Initialization: - monero=Monero(daemon_host,daemon_port,wallet_host,wallet_port,redis_host,redis_port) + monero=Monero(daemon_host,daemon_port,wallet_host,wallet_port,redis_host,redis_port,site_salt) + + Site salt is a string that's specific to your particular use. + It should be random but constant. Its intent is to cause the + same recipient identifier to map to different payment IDs for + two different users of monero-python. It is recommended to + hard code some random string in the call. * Checking the current balance: @@ -27,15 +33,16 @@ Calling raw module functions is undocumented and subject to change. * Create a new payment ID for a recipient: - payment_id=monero.GetPaymentID(recipient) + payment_id=monero.GetPaymentID(recipient,deterministic=True) recipient is a string representing a user, destination, etc. The semantics of it are left to the API user. Numbers can be used if they're passed as strings. CheckForDeposits and other such functions will return that recipient when the associated payment ID is found in a transaction. - Payment IDs are deterministic, so asking for a payment ID for a - given recipient will always yield the same payment ID. + Payment IDs can be deterministic (so asking for a payment ID for a + given recipient will always yield the same payment ID) or not (so + multiple calls to GetPaymentID will yield different Payment IDs). * Get the recipient corresponding to a payment ID: @@ -43,7 +50,7 @@ Calling raw module functions is undocumented and subject to change. Returns the recipient associated with the given payment ID, or None if there is none. If there is one, it is the string that was passed - to a previous call to GetPaymentID. + to a previous call to GetPaymentID which returned this payment ID. * Check for new deposits: diff --git a/monero/monero.py b/monero/monero.py index 9997d3f..51fe6c7 100644 --- a/monero/monero.py +++ b/monero/monero.py @@ -33,18 +33,19 @@ import redisdb import payment class Monero: - def __init__(self,daemon_host,daemon_port,wallet_host,wallet_port,redis_host,redis_port): + def __init__(self,daemon_host,daemon_port,wallet_host,wallet_port,redis_host,redis_port,site_salt): redisdb.connect_to_redis(redis_host,redis_port) self.daemon_host=daemon_host self.daemon_port=daemon_port self.wallet_host=wallet_host self.wallet_port=wallet_port + self.site_salt=site_salt def GetWalletBalance(self): return utils.RetrieveWalletBalance(self.wallet_host,self.wallet_port) - def GetPaymentID(self,recipient): - return utils.GetPaymentIDFromUserID(recipient) + def GetPaymentID(self,recipient,deterministic=True): + return utils.GetPaymentIDFromUserID(recipient,deterministic,self.site_salt) def GetRecipient(self,payment_id): return utils.GetUserIDFromPaymentID(payment_id) diff --git a/monero/utils.py b/monero/utils.py index eac9d3b..5df119a 100644 --- a/monero/utils.py +++ b/monero/utils.py @@ -34,6 +34,7 @@ import json import httplib import time import string +from Crypto.Random.random import getrandbits from decimal import * from log import log_error, log_warn, log_info, log_log from redisdb import * @@ -45,11 +46,15 @@ cached_wallet_balance=None cached_wallet_unlocked_balance=None cached_wallet_balance_timestamp=None -def GetPaymentIDFromUserID(user_id): - salt="nveuweaiirv-" - p = hashlib.sha256(salt+user_id).hexdigest() +def GetPaymentIDFromUserID(user_id,deterministic,site_salt=''): + salt="nveuweaiirv-"+site_salt+'-' + if deterministic: + s = salt + user_id + else: + s = salt + str(getrandbits(128)) + '-' + user_id + p = hashlib.sha256(s).hexdigest() try: - redis_hset("paymentid",p,link.identity()) + redis_hset("paymentid",p,user_id) except Exception,e: log_error('GetPaymentIDFromUserID: failed to set payment ID for %s to redis: %s' % (user_id,str(e))) return p