Skip to content

Commit

Permalink
add ability to intercept and transform message requests and responses
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMieskoski committed Sep 4, 2020
1 parent 5c510a9 commit 1ce4b90
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Release v2.1.5-beta.1
- add mechanism to intercept and transform requests and responses before transmission to mobile
- add check for from value in sendTx message and add from value if missing

### Release v2.1.4
- add missing personal_ecRecover method
- Fix flipped parameters for persona_sign
Expand Down
2 changes: 1 addition & 1 deletion example/app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ export default {
this.web3.eth.getTransactionCount(this.userAddress).then(nonce => {
this.web3.eth
.sendTransaction({
from: this.userAddress,
// from: this.userAddress,
to: this.tokenAddress,
nonce,
value: 0,
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@myetherwallet/mewconnect-web-client",
"homepage": "https://github.com/myetherwallet/MEWconnect-web-client",
"version": "2.1.4",
"version": "2.1.5-beta.1",
"main": "./dist/index.js",
"module": "./src/index.js",
"scripts": {
Expand Down
17 changes: 14 additions & 3 deletions src/connectClient/WebRtcCommunication.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isBrowser } from 'browser-or-node';

import uuid from 'uuid/v4';
import MewConnectCommon from './MewConnectCommon';
import Interceptor from './interceptor';

const debug = debugLogger('MEWconnect:webRTC-communication');
const debugPeer = debugLogger('MEWconnectVerbose:peer-instances');
Expand All @@ -16,6 +17,7 @@ const logger = createLogger('WebRtcCommunication');
export default class WebRtcCommunication extends MewConnectCommon {
constructor(mewCrypto) {
super();
this.Interceptor = new Interceptor();
this.Peer = SimplePeer;
this.mewCrypto = mewCrypto;
this.peersCreated = {};
Expand Down Expand Up @@ -60,7 +62,7 @@ export default class WebRtcCommunication extends MewConnectCommon {
this.usingVersion = version;
}

setActiveInitiatorId(id){
setActiveInitiatorId(id) {
this.activeInitiatorId = id;
}

Expand Down Expand Up @@ -307,12 +309,18 @@ export default class WebRtcCommunication extends MewConnectCommon {
}
if (this.isJSON(decryptedData)) {
const parsed = JSON.parse(decryptedData);
this.emit('data', { type: parsed.type, data: parsed.data, id: parsed.id });
this.Interceptor.InspectIncomingMessage(parsed);
this.emit('data', {
type: parsed.type,
data: parsed.data,
id: parsed.id
});
} else {
this.Interceptor.InspectIncomingMessage(decryptedData);
this.emit('data', {
type: decryptedData.type,
data: decryptedData.data,
id: decryptedData.id
id: decryptedData.id
});
}
} catch (e) {
Expand Down Expand Up @@ -363,6 +371,7 @@ export default class WebRtcCommunication extends MewConnectCommon {
sendRtcMessage(type, msg, id) {
debug(msg);
debug(`[SEND RTC MESSAGE] type: ${type}, message: ${msg}, id: ${id}`);
// const values = this.Interceptor.InspectOutgoingMessage({type, data: msg})
this.rtcSend(JSON.stringify({ type, data: msg, id }));
}

Expand All @@ -389,8 +398,10 @@ export default class WebRtcCommunication extends MewConnectCommon {
if (this.isAlive()) {
let encryptedSend;
if (typeof arg === 'string') {
arg = this.Interceptor.InspectOutgoingMessageString(arg);
encryptedSend = await this.mewCrypto.encrypt(arg);
} else {
arg = this.Interceptor.InspectOutgoingMessage(arg);
encryptedSend = await this.mewCrypto.encrypt(JSON.stringify(arg));
}
debug('SENDING RTC');
Expand Down
114 changes: 114 additions & 0 deletions src/connectClient/interceptor.js
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;
}
}
}

Large diffs are not rendered by default.

0 comments on commit 1ce4b90

Please sign in to comment.