Skip to content

Commit

Permalink
Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
gantunesr committed Dec 13, 2022
1 parent 92dbeb0 commit 7e93e07
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
29 changes: 28 additions & 1 deletion app/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import { isZero } from '../util/lodash';
import AnalyticsV2 from '../util/analyticsV2';
import WebviewExecutionService from '../components/Views/Wallet/WebviewExecutionService';
import { SnapBridge } from './SnapExecutionService';
import { getRpcMethodMiddleware } from './RPCMethods/RPCMethodMiddleware';


const NON_EMPTY = 'NON_EMPTY';

Expand Down Expand Up @@ -215,10 +217,32 @@ class Engine {
});

this.setupSnapProvider = (snapId, connectionStream) => {
console.log('method setupSnapProvider', !!connectionStream, snapId);
const bridge = new SnapBridge({
snapId,
connectionStream,
getRPCMethodMiddleware: (args) => null,
getRPCMethodMiddleware: ({ hostname, getProviderState }) =>
getRpcMethodMiddleware({
hostname,
getProviderState,
navigation: null,
getApprovedHosts: () => null,
setApprovedHosts: () => null,
approveHost: () => null,
// Website info
url: 'https://www.google.com',
title: 'Snap',
icon: null,
// Bookmarks
isHomepage: false,
// Show autocomplete
fromHomepage: false,
toggleUrlModal: () => null,
// Wizard
wizardScrollAdjusted: () => null,
tabId: false,
isWalletConnect: true,
}),
});

bridge.setupProviderConnection();
Expand Down Expand Up @@ -920,4 +944,7 @@ export default {
Object.freeze(instance);
return instance;
},
get snapExecutionService() {
return instance.snapExecutionService;
},
};
1 change: 1 addition & 0 deletions app/core/RPCMethods/RPCMethodMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ export const getRpcMethodMiddleware = ({
* initialization.
*/
metamask_getProviderState: async () => {
console.log('========> metamask_getProviderState');
res.result = {
...getProviderState(),
accounts: await getAccounts(),
Expand Down
41 changes: 36 additions & 5 deletions app/core/SnapExecutionService/SnapBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const pump = require('pump');

interface ISnapBridgeProps {
snapId: string;
stream: Duplex;
connectionStream: Duplex;
getRPCMethodMiddleware: (args: any) => any;
}

Expand All @@ -43,9 +43,14 @@ export default class SnapBridge {
#providerProxy: any;
#blockTrackerProxy: any;

constructor({ snapId, stream, getRPCMethodMiddleware }: ISnapBridgeProps) {
constructor({
snapId,
connectionStream,
getRPCMethodMiddleware,
}: ISnapBridgeProps) {
console.log('SnapBridge constructor', snapId, !!connectionStream);
this.snapId = snapId;
this.stream = stream;
this.stream = connectionStream;
this.getRPCMethodMiddleware = getRPCMethodMiddleware;

const { NetworkController, PreferencesController } = Engine.context as any;
Expand Down Expand Up @@ -125,8 +130,8 @@ export default class SnapBridge {
engine.emit('notification', message),
);

engine.push(createOriginMiddleware({ origin: this.snapId }));
engine.push(createLoggerMiddleware({ origin: this.snapId }));
// engine.push(createOriginMiddleware({ origin: this.snapId }));
// engine.push(createLoggerMiddleware({ origin: this.snapId }));

// Filter and subscription polyfills
engine.push(filterMiddleware);
Expand Down Expand Up @@ -188,4 +193,30 @@ export default class SnapBridge {
selectedAddress,
};
};

getProviderNetworkState({ network }: { network: string }) {
const { NetworkController } = Engine.context as any;
const networkType = NetworkController.state.provider.type;
const networkProvider = NetworkController.state.provider;

const isInitialNetwork =
networkType && getAllNetworks().includes(networkType);
let chainId;

if (isInitialNetwork) {
chainId = NetworksChainId[networkType];
} else if (networkType === 'rpc') {
chainId = networkProvider.chainId;
}
if (chainId && !chainId.startsWith('0x')) {
// Convert to hex
chainId = `0x${parseInt(chainId, 10).toString(16)}`;
}

const result = {
networkVersion: network,
chainId,
};
return result;
}
}

0 comments on commit 7e93e07

Please sign in to comment.