forked from unstoppabledomains/MEWconnect-web-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to intercept and transform message requests and responses
- Loading branch information
1 parent
5c510a9
commit 1ce4b90
Showing
7 changed files
with
136 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import debugLogger from 'debug'; | ||
const debug = debugLogger('MEWconnect:webRTC-interceptor'); | ||
|
||
|
||
export default class Interceptor { | ||
constructor() { | ||
this.address = false; | ||
this.SIGN_TX = 'signTx'; | ||
this.ADDRESS = 'address'; | ||
} | ||
|
||
isJSON(value) { | ||
try { | ||
JSON.parse(value); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
setWalletAddress(address) { | ||
try { | ||
if (address.address) { | ||
this.address = address.address; | ||
} else { | ||
this.address = address; | ||
} | ||
} catch (e) { | ||
debug(e); | ||
} | ||
} | ||
|
||
convertToJson(arg) { | ||
let convertBack = false; | ||
try { | ||
if (typeof arg === 'string') { | ||
if (this.isJSON(arg)) { | ||
convertBack = true; | ||
return [JSON.parse(arg), convertBack] | ||
} | ||
} | ||
return [arg, convertBack] | ||
} catch (e) { | ||
debug(e); | ||
return [arg, convertBack] | ||
} | ||
} | ||
|
||
InspectOutgoingMessageString(arg) { | ||
try { | ||
let temp = this.convertToJson(arg); | ||
let data2 = temp[0]; | ||
if (data2.type === this.SIGN_TX) { | ||
let temp2 = this.convertToJson(data2.data); | ||
let dataUpdated = this.checkForFromValueInSignTx(temp2[0]); | ||
if(temp2[1]){ | ||
data2.data = JSON.stringify(dataUpdated); | ||
} | ||
} | ||
if(temp[1]){ | ||
return JSON.stringify(data2); | ||
} | ||
return arg; | ||
} catch (e) { | ||
debug(e); | ||
return arg; | ||
} | ||
} | ||
|
||
InspectOutgoingMessage({type, data, id}) { | ||
|
||
try { | ||
if (type === this.SIGN_TX) { | ||
let temp = this.convertToJson(data); | ||
let data2 = this.checkForFromValueInSignTx(temp[0]); | ||
if(temp[1]){ | ||
data2 = JSON.stringify(data2); | ||
} | ||
return {type, data: data2, id}; | ||
} | ||
return {type, data, id}; | ||
} catch (e) { | ||
debug(e); | ||
return {type, data, id}; | ||
} | ||
} | ||
|
||
InspectIncomingMessage(data) { | ||
try { | ||
if (data.type === this.ADDRESS) { | ||
this.setWalletAddress(data.data); | ||
} | ||
return data; | ||
} catch (e) { | ||
debug(e); | ||
return data; | ||
} | ||
} | ||
|
||
checkForFromValueInSignTx(data) { | ||
try { | ||
if (!data.from) { | ||
if (this.address) { | ||
data.from = this.address; | ||
return data; | ||
} | ||
} | ||
return data; | ||
} catch (e) { | ||
debug(e); | ||
return data; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/connectProvider/web3Provider/networks/tokens/tokens-eth.json
Large diffs are not rendered by default.
Oops, something went wrong.