modifications to API commands used by mobile device

This commit is contained in:
Jonathan Warren 2013-09-04 16:51:19 -04:00
parent 7ccdd14418
commit 1bbb8240ed

View File

@ -36,6 +36,7 @@ from debug import logger
# Helper Functions # Helper Functions
import helper_bootstrap import helper_bootstrap
import proofofwork
import sys import sys
if sys.platform == 'darwin': if sys.platform == 'darwin':
@ -687,14 +688,28 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
return data return data
elif method == 'disseminatePreEncryptedMsg': elif method == 'disseminatePreEncryptedMsg':
# The device issuing this command to PyBitmessage supplies a msg object that has # The device issuing this command to PyBitmessage supplies a msg object that has
# already been encrypted and had the necessary proof of work done for it to be # already been encrypted but which still needs the POW to be done. PyBitmessage
# disseminated to the rest of the Bitmessage network. PyBitmessage accepts this msg # accepts this msg object and sends it out to the rest of the Bitmessage network
# object and sends it out to the rest of the Bitmessage network as if it had generated the # as if it had generated the message itself. Please do not yet add this to the
# message itself. Please do not yet add this to the api doc. # api doc.
if len(params) != 1: if len(params) != 3:
raise APIError(0, 'I need 1 parameter!') raise APIError(0, 'I need 3 parameter!')
encryptedPayload, = params encryptedPayload, requiredAverageProofOfWorkNonceTrialsPerByte, requiredPayloadLengthExtraBytes = params
encryptedPayload = self._decode(encryptedPayload, "hex") encryptedPayload = self._decode(encryptedPayload, "hex")
# Let us do the POW and attach it to the front
target = 2**64 / ((len(encryptedPayload)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte)
with shared.printLock:
print '(For msg message via API) Doing proof of work. Total required difficulty:', float(requiredAverageProofOfWorkNonceTrialsPerByte) / shared.networkDefaultProofOfWorkNonceTrialsPerByte, 'Required small message difficulty:', float(requiredPayloadLengthExtraBytes) / shared.networkDefaultPayloadLengthExtraBytes
powStartTime = time.time()
initialHash = hashlib.sha512(encryptedPayload).digest()
trialValue, nonce = proofofwork.run(target, initialHash)
with shared.printLock:
print '(For msg message via API) Found proof of work', trialValue, 'Nonce:', nonce
try:
print 'POW took', int(time.time() - powStartTime), 'seconds.', nonce / (time.time() - powStartTime), 'nonce trials per second.'
except:
pass
encryptedPayload = pack('>Q', nonce) + encryptedPayload
toStreamNumber = decodeVarint(encryptedPayload[16:26])[0] toStreamNumber = decodeVarint(encryptedPayload[16:26])[0]
inventoryHash = calculateInventoryHash(encryptedPayload) inventoryHash = calculateInventoryHash(encryptedPayload)
objectType = 'msg' objectType = 'msg'
@ -705,15 +720,25 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
shared.broadcastToSendDataQueues(( shared.broadcastToSendDataQueues((
toStreamNumber, 'sendinv', inventoryHash)) toStreamNumber, 'sendinv', inventoryHash))
elif method == 'disseminatePubkey': elif method == 'disseminatePubkey':
# The device issuing this command to PyBitmessage supplies a pubkey object that has # The device issuing this command to PyBitmessage supplies a pubkey object to be
# already had the necessary proof of work done for it to be disseminated to the rest of the # disseminated to the rest of the Bitmessage network. PyBitmessage accepts this
# Bitmessage network. PyBitmessage accepts this pubkey object and sends it out to the # pubkey object and sends it out to the rest of the Bitmessage network as if it
# rest of the Bitmessage network as if it had generated the pubkey object itself. Please # had generated the pubkey object itself. Please do not yet add this to the api
# do not yet add this to the api doc. # doc.
if len(params) != 1: if len(params) != 1:
raise APIError(0, 'I need 1 parameter!') raise APIError(0, 'I need 1 parameter!')
payload, = params payload, = params
payload = self._decode(payload, "hex") payload = self._decode(payload, "hex")
# Let us do the POW
target = 2 ** 64 / ((len(payload) + shared.networkDefaultPayloadLengthExtraBytes +
8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte)
print '(For pubkey message via API) Doing proof of work...'
initialHash = hashlib.sha512(payload).digest()
trialValue, nonce = proofofwork.run(target, initialHash)
print '(For pubkey message via API) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q', nonce) + payload
pubkeyReadPosition = 8 # bypass the nonce pubkeyReadPosition = 8 # bypass the nonce
if payload[pubkeyReadPosition:pubkeyReadPosition+4] == '\x00\x00\x00\x00': # if this pubkey uses 8 byte time if payload[pubkeyReadPosition:pubkeyReadPosition+4] == '\x00\x00\x00\x00': # if this pubkey uses 8 byte time
pubkeyReadPosition += 8 pubkeyReadPosition += 8