Skip to content

Commit

Permalink
snaps logic form #7526
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Nov 29, 2023
1 parent fe58be8 commit b1d7ed7
Show file tree
Hide file tree
Showing 33 changed files with 1,900 additions and 52 deletions.
4 changes: 4 additions & 0 deletions app/components/Nav/Main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { addHexPrefix, toHexadecimal } from '../../../util/number';
import { NETWORKS_CHAIN_ID } from '../../../constants/network';
import WarningAlert from '../../../components/UI/WarningAlert';
import { GOERLI_DEPRECATED_ARTICLE } from '../../../constants/urls';
import { SnapsExecutionWebView } from '../../UI/SnapsExecutionWebView';

const Stack = createStackNavigator();

Expand Down Expand Up @@ -351,6 +352,9 @@ const Main = (props) => {
) : (
renderLoader()
)}
<View>
<SnapsExecutionWebView />
</View>
<GlobalAlert />
<FadeOutOverlay />
<Notification navigation={props.navigation} />
Expand Down
62 changes: 62 additions & 0 deletions app/components/UI/SnapsExecutionWebView/SnapsExecutionWebView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React, { useRef } from 'react';
import { View, ScrollView } from 'react-native';
import WebView from 'react-native-webview';
import { snapsState, WebviewPostMessageStream } from '../../../core/Snaps';
import { createStyles } from './styles';

let stream: any;

const SnapsExecutionWebView = () => {
const styles = createStyles();

const webviewRef = useRef();

const setWebviewPostMessage = () => {
stream = new WebviewPostMessageStream({
name: 'rnside',
target: 'webview',
targetOrigin: '*',
targetWindow: webviewRef.current,
});

// eslint-disable-next-line no-console
stream.on('data', (data: any) =>
// eslint-disable-next-line no-console
console.log(
'[APP LOG] setWebviewPostMessage: Message from Webview ' + data,
),
);

snapsState.stream = stream;
snapsState.webview = webviewRef.current;
};

const messageFromWebview = (data: any) => {
stream?._onMessage(data);
};

const envURI = {
prod: 'https://gantunesr.github.io/mobile-execution-environment/',
localIOS: 'http://localhost:3001/mobile-execution-environment',
localAndroid: 'http://10.0.2.2:3001/mobile-execution-environment',
};

return (
<ScrollView style={styles.container}>
<View style={styles.webview}>
<WebView
ref={webviewRef}
source={{
uri: envURI.prod,
}}
onMessage={messageFromWebview}
onLoadEnd={setWebviewPostMessage}
originWhitelist={['*']}
javaScriptEnabled
/>
</View>
</ScrollView>
);
};

export default SnapsExecutionWebView;
4 changes: 4 additions & 0 deletions app/components/UI/SnapsExecutionWebView/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import SnapsExecutionWebView from './SnapsExecutionWebView';

// eslint-disable-next-line import/prefer-default-export
export { SnapsExecutionWebView };
20 changes: 20 additions & 0 deletions app/components/UI/SnapsExecutionWebView/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable react-native/no-color-literals */
import { StyleSheet } from 'react-native';

// eslint-disable-next-line import/prefer-default-export
export const createStyles = () =>
StyleSheet.create({
webview: {
height: 0,
// marginBottom: 50,
// borderWidth: 1,
// borderStyle: 'dashed',
// borderColor: 'red',
},
container: {
// flex: 1,
// borderWidth: 1,
// borderStyle: 'dashed',
// borderColor: 'green',
},
});
13 changes: 10 additions & 3 deletions app/constants/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
enum USER_INTENT {
export const EndowmentPermissions = Object.freeze({
'endowment:network-access': 'endowment:network-access',
'endowment:long-running': 'endowment:long-running',
'endowment:transaction-insight': 'endowment:transaction-insight',
'endowment:cronjob': 'endowment:cronjob',
'endowment:ethereum-provider': 'endowment:ethereum-provider',
'endowment:rpc': 'endowment:rpc',
} as const);

export enum USER_INTENT {
None,
Create,
CreateMultiple,
Expand All @@ -7,5 +16,3 @@ enum USER_INTENT {
Import,
ConnectHW,
}

export default USER_INTENT;
114 changes: 114 additions & 0 deletions app/constants/snaps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* eslint-disable import/prefer-default-export */
import type { SupportedCurve } from '@metamask/key-tree';

export type SnapsDerivationPathType = ['m', ...string[]];

export interface SnapsDerivationPath {
path: SnapsDerivationPathType;
curve: SupportedCurve;
name: string;
}

// Copy of extension mapping: https://github.com/MetaMask/metamask-extension/blob/49f8052b157374370ac71373708933c6e639944e/shared/constants/snaps.ts#L52
export const SNAPS_DERIVATION_PATHS: SnapsDerivationPath[] = [
{
path: ['m', `44'`, `0'`],
curve: 'ed25519',
name: 'Test BIP-32 Path (ed25519)',
},
{
path: ['m', `44'`, `1'`],
curve: 'secp256k1',
name: 'Test BIP-32 Path (secp256k1)',
},
{
path: ['m', `44'`, `0'`],
curve: 'secp256k1',
name: 'Bitcoin Legacy',
},
{
path: ['m', `49'`, `0'`],
curve: 'secp256k1',
name: 'Bitcoin Nested SegWit',
},
{
path: ['m', `49'`, `1'`],
curve: 'secp256k1',
name: 'Bitcoin Testnet Nested SegWit',
},
{
path: ['m', `84'`, `0'`],
curve: 'secp256k1',
name: 'Bitcoin Native SegWit',
},
{
path: ['m', `84'`, `1'`],
curve: 'secp256k1',
name: 'Bitcoin Testnet Native SegWit',
},
{
path: ['m', `44'`, `501'`],
curve: 'secp256k1',
name: 'Solana',
},
{
path: ['m', `44'`, `2'`],
curve: 'secp256k1',
name: 'Litecoin',
},
{
path: ['m', `44'`, `3'`],
curve: 'secp256k1',
name: 'Dogecoin',
},
{
path: ['m', `44'`, `60'`],
curve: 'secp256k1',
name: 'Ethereum',
},
{
path: ['m', `44'`, `118'`],
curve: 'secp256k1',
name: 'Atom',
},
{
path: ['m', `44'`, `145'`],
curve: 'secp256k1',
name: 'Bitcoin Cash',
},
{
path: ['m', `44'`, `714'`],
curve: 'secp256k1',
name: 'Binance (BNB)',
},
{
path: ['m', `44'`, `931'`],
curve: 'secp256k1',
name: 'THORChain (RUNE)',
},
{
path: ['m', `44'`, `330'`],
curve: 'secp256k1',
name: 'Terra (LUNA)',
},
{
path: ['m', `44'`, `459'`],
curve: 'secp256k1',
name: 'Kava',
},
{
path: ['m', `44'`, `529'`],
curve: 'secp256k1',
name: 'Secret Network',
},
{
path: ['m', `44'`, `397'`],
curve: 'ed25519',
name: 'NEAR Protocol',
},
{
path: ['m', `44'`, `1'`, `0'`],
curve: 'ed25519',
name: 'NEAR Protocol Testnet',
},
];
10 changes: 10 additions & 0 deletions app/core/BackgroundBridge/BackgroundBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
selectLegacyNetwork,
} from '../../selectors/networkController';
import { store } from '../../store';
import snapMethodMiddlewareBuilder from '../Snaps/SnapsMethodMiddleware';

const createFilterMiddleware = require('eth-json-rpc-filters');
const createSubscriptionManager = require('eth-json-rpc-filters/subscriptionManager');
Expand Down Expand Up @@ -314,6 +315,15 @@ export class BackgroundBridge extends EventEmitter {
engine.push(subscriptionManager.middleware);
// watch asset

// Snaps middleware
engine.push(
snapMethodMiddlewareBuilder(
Engine.context,
Engine.controllerMessenger,
origin,
),
);

// user-facing RPC methods
engine.push(
this.createMiddleware({
Expand Down
Loading

0 comments on commit b1d7ed7

Please sign in to comment.