From 176340c22d3dc0f8493ea26c7d942bf4fb0110a7 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 8 Aug 2013 17:50:12 -0400 Subject: [PATCH] added api command: getPubkeyByHash --- src/bitmessagemain.py | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 4e6b327f..cae8daeb 100644 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -743,11 +743,12 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # already been encrypted and had the necessary proof of work done for it to be # disseminated to the rest of the Bitmessage network. PyBitmessage accepts this msg # object and sends it out to the rest of the Bitmessage network as if it had generated the - # message itself. + # message itself. Please do not yet add this to the api doc. if len(params) != 1: return 'API Error 0000: I need 1 parameter!' encryptedPayload, = params encryptedPayload = encryptedPayload.decode('hex') + toStreamNumber = decodeVarint(encryptedPayload[16:26])[0] inventoryHash = calculateInventoryHash(encryptedPayload) objectType = 'msg' shared.inventory[inventoryHash] = ( @@ -760,22 +761,32 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # The device issuing this command to PyBitmessage supplies a pubkey object that has # already had the necessary proof of work done for it to be disseminated to the rest of the # Bitmessage network. PyBitmessage accepts this pubkey object and sends it out to the - # rest of the Bitmessage network as if it had generated the pubkey object itself. + # rest of the Bitmessage network as if it had generated the pubkey object itself. Please + # do not yet add this to the api doc. if len(params) != 1: return 'API Error 0000: I need 1 parameter!' payload, = params payload = payload.decode('hex') + pubkeyReadPosition = 8 # bypass the nonce + if payload[pubkeyReadPosition:pubkeyReadPosition+4] == '\x00\x00\x00\x00': # if this pubkey uses 8 byte time + pubkeyReadPosition += 8 + else: + pubkeyReadPosition += 4 + addressVersion, addressVersionLength = decodeVarint(payload[pubkeyReadPosition:pubkeyReadPosition+10]) + pubkeyReadPosition += addressVersionLength + pubkeyStreamNumber = decodeVarint(payload[pubkeyReadPosition:pubkeyReadPosition+10])[0] inventoryHash = calculateInventoryHash(payload) objectType = 'pubkey' shared.inventory[inventoryHash] = ( - objectType, streamNumber, payload, int(time.time())) + objectType, pubkeyStreamNumber, payload, int(time.time())) with shared.printLock: print 'broadcasting inv within API command disseminatePubkey with hash:', inventoryHash.encode('hex') shared.broadcastToSendDataQueues(( streamNumber, 'sendinv', inventoryHash)) - elif method == 'getMessageDataByDestinationRIPEHash': + elif method == 'getMessageDataByDestinationHash': # Method will eventually be used by a particular Android app to - # select relevant messages. + # select relevant messages. Do not yet add this to the api + # doc. if len(params) != 1: return 'API Error 0000: I need 1 parameter!' @@ -816,7 +827,26 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): data += json.dumps({'data':payload.encode('hex')}, indent=4, separators=(',', ': ')) data += ']}' return data - + elif method == 'getPubkeyByHash': + # Method will eventually be used by a particular Android app to + # retrieve pubkeys. Please do not yet add this to the api docs. + if len(params) != 1: + return 'API Error 0000: I need 1 parameter!' + requestedHash, = params + if len(requestedHash) != 40: + return 'API Error 0019: The length of hash should be 20 bytes (encoded in hex thus 40 characters).' + requestedHash = requestedHash.decode('hex') + parameters = (requestedHash,) + with shared.sqlLock: + shared.sqlSubmitQueue.put('''SELECT transmitdata FROM pubkeys WHERE hash = ? ; ''') + shared.sqlSubmitQueue.put(parameters) + queryreturn = shared.sqlReturnQueue.get() + data = '{"pubkey":[' + for row in queryreturn: + transmitdata, = row + data += json.dumps({'data':transmitdata.encode('hex')}, indent=4, separators=(',', ': ')) + data += ']}' + return data elif method == 'clientStatus': return '{ "networkConnections" : "%s" }' % str(len(shared.connectedHostsList)) else: