1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00

Merge pull request #572 from MetaMask/i479

inpage provider - reassign incomming ids
This commit is contained in:
kumavis 2016-08-22 19:17:18 -07:00 committed by GitHub
commit f9c58c0746

View File

@ -33,8 +33,16 @@ function MetamaskInpageProvider (connectionStream) {
}) })
asyncProvider.on('error', console.error.bind(console)) asyncProvider.on('error', console.error.bind(console))
self.asyncProvider = asyncProvider self.asyncProvider = asyncProvider
// overwrite own sendAsync method // handle sendAsync requests via asyncProvider
self.sendAsync = asyncProvider.sendAsync.bind(asyncProvider) self.sendAsync = function(payload, cb){
// rewrite request ids
var request = jsonrpcMessageTransform(payload, (message) => {
message.id = createRandomId()
return message
})
// forward to asyncProvider
asyncProvider.sendAsync(request, cb)
}
} }
MetamaskInpageProvider.prototype.send = function (payload) { MetamaskInpageProvider.prototype.send = function (payload) {
@ -92,3 +100,21 @@ function remoteStoreWithLocalStorageCache (storageKey) {
return store return store
} }
function createRandomId(){
const extraDigits = 3
// 13 time digits
const datePart = new Date().getTime() * Math.pow(10, extraDigits)
// 3 random digits
const extraPart = Math.floor(Math.random() * Math.pow(10, extraDigits))
// 16 digits
return datePart + extraPart
}
function jsonrpcMessageTransform(payload, transformFn){
if (Array.isArray(payload)) {
return payload.map(transformFn)
} else {
return transformFn(payload)
}
}