MCPcopy Index your code
hub / github.com/Bitmessage/PyBitmessage / requestPubKey

Method requestPubKey

src/class_singleWorker.py:884–959  ·  view source on GitHub ↗
(self, toAddress)

Source from the content-addressed store, hash-verified

882 call([apiNotifyPath, "newMessage"])
883
884 def requestPubKey(self, toAddress):
885 toStatus, addressVersionNumber, streamNumber, ripe = decodeAddress(
886 toAddress)
887 if toStatus != 'success':
888 logger.error('Very abnormal error occurred in requestPubKey. toAddress is: ' + repr(
889 toAddress) + '. Please report this error to Atheros.')
890 return
891
892 queryReturn = sqlQuery(
893 '''SELECT retrynumber FROM sent WHERE toaddress=? AND (status='doingpubkeypow' OR status='awaitingpubkey') LIMIT 1''',
894 toAddress)
895 if len(queryReturn) == 0:
896 logger.critical("BUG: Why are we requesting the pubkey for %s if there are no messages in the sent folder to that address?" % toAddress)
897 return
898 retryNumber = queryReturn[0][0]
899
900 if addressVersionNumber <= 3:
901 state.neededPubkeys[toAddress] = 0
902 elif addressVersionNumber >= 4:
903 # If the user just clicked 'send' then the tag (and other information) will already
904 # be in the neededPubkeys dictionary. But if we are recovering from a restart
905 # of the client then we have to put it in now.
906 privEncryptionKey = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[:32] # Note that this is the first half of the sha512 hash.
907 tag = hashlib.sha512(hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()).digest()[32:] # Note that this is the second half of the sha512 hash.
908 if tag not in state.neededPubkeys:
909 state.neededPubkeys[tag] = (toAddress, highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))) # We'll need this for when we receive a pubkey reply: it will be encrypted and we'll need to decrypt it.
910
911 TTL = 2.5*24*60*60 # 2.5 days. This was chosen fairly arbitrarily.
912 TTL *= 2**retryNumber
913 if TTL > 28*24*60*60:
914 TTL = 28*24*60*60
915 TTL = TTL + random.randrange(-300, 300) # add some randomness to the TTL
916 embeddedTime = int(time.time() + TTL)
917 payload = pack('>Q', embeddedTime)
918 payload += '\x00\x00\x00\x00' # object type: getpubkey
919 payload += encodeVarint(addressVersionNumber)
920 payload += encodeVarint(streamNumber)
921 if addressVersionNumber <= 3:
922 payload += ripe
923 logger.info('making request for pubkey with ripe: %s', hexlify(ripe))
924 else:
925 payload += tag
926 logger.info('making request for v4 pubkey with tag: %s', hexlify(tag))
927
928 # print 'trial value', trialValue
929 statusbar = 'Doing the computations necessary to request the recipient\'s public key.&#x27;
930 queues.UISignalQueue.put(('updateStatusBar', statusbar))
931 queues.UISignalQueue.put(('updateSentItemStatusByToAddress', (
932 toAddress, tr._translate("MainWindow",'Doing work necessary to request encryption key.'))))
933
934 target = 2 ** 64 / (defaults.networkDefaultProofOfWorkNonceTrialsPerByte*(len(payload) + 8 + defaults.networkDefaultPayloadLengthExtraBytes + ((TTL*(len(payload)+8+defaults.networkDefaultPayloadLengthExtraBytes))/(2 ** 16))))
935 initialHash = hashlib.sha512(payload).digest()
936 trialValue, nonce = proofofwork.run(target, initialHash)
937 logger.info('Found proof of work ' + str(trialValue) + ' Nonce: ' + str(nonce))
938
939 payload = pack('>Q', nonce) + payload
940 inventoryHash = calculateInventoryHash(payload)
941 objectType = 1

Callers 1

sendMsgMethod · 0.95

Calls 11

InventoryClass · 0.90
PendingUploadClass · 0.90
decodeAddressFunction · 0.85
sqlQueryFunction · 0.85
encodeVarintFunction · 0.85
calculateInventoryHashFunction · 0.85
sqlExecuteFunction · 0.85
putMethod · 0.80
addMethod · 0.80
argMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected