aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/inpage-provider.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2016-09-01 06:40:05 +0800
committerkumavis <aaron@kumavis.me>2016-09-01 06:40:05 +0800
commit532737a1014d75f263854763f8bf1a2280a5526b (patch)
treecf91f5959df927447e8dfce48577d268fedd5d34 /app/scripts/lib/inpage-provider.js
parentd26dd53fe18f43fc509f828348e013147159da91 (diff)
downloadtangerine-wallet-browser-532737a1014d75f263854763f8bf1a2280a5526b.tar
tangerine-wallet-browser-532737a1014d75f263854763f8bf1a2280a5526b.tar.gz
tangerine-wallet-browser-532737a1014d75f263854763f8bf1a2280a5526b.tar.bz2
tangerine-wallet-browser-532737a1014d75f263854763f8bf1a2280a5526b.tar.lz
tangerine-wallet-browser-532737a1014d75f263854763f8bf1a2280a5526b.tar.xz
tangerine-wallet-browser-532737a1014d75f263854763f8bf1a2280a5526b.tar.zst
tangerine-wallet-browser-532737a1014d75f263854763f8bf1a2280a5526b.zip
inpage - remap ids on response
Diffstat (limited to 'app/scripts/lib/inpage-provider.js')
-rw-r--r--app/scripts/lib/inpage-provider.js22
1 files changed, 18 insertions, 4 deletions
diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js
index 65354cd3d..3b7d76c7d 100644
--- a/app/scripts/lib/inpage-provider.js
+++ b/app/scripts/lib/inpage-provider.js
@@ -33,15 +33,29 @@ function MetamaskInpageProvider (connectionStream) {
})
asyncProvider.on('error', console.error.bind(console))
self.asyncProvider = asyncProvider
+
+ self.idMap = {}
// handle sendAsync requests via asyncProvider
self.sendAsync = function(payload, cb){
// rewrite request ids
- var request = jsonrpcMessageTransform(payload, (message) => {
- message.id = createRandomId()
+ var request = eachJsonMessage(payload, (message) => {
+ var newId = createRandomId()
+ self.idMap[newId] = message.id
+ message.id = newId
return message
})
// forward to asyncProvider
- asyncProvider.sendAsync(request, cb)
+ asyncProvider.sendAsync(request, function(err, res){
+ if (err) return cb(err)
+ // transform messages to original ids
+ eachJsonMessage(res, (message) => {
+ var oldId = self.idMap[message.id]
+ delete self.idMap[message.id]
+ message.id = oldId
+ return message
+ })
+ cb(null, res)
+ })
}
}
@@ -111,7 +125,7 @@ function createRandomId(){
return datePart + extraPart
}
-function jsonrpcMessageTransform(payload, transformFn){
+function eachJsonMessage(payload, transformFn){
if (Array.isArray(payload)) {
return payload.map(transformFn)
} else {