mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-12 03:54:13 +00:00
Remove unused lib
This commit is contained in:
parent
50a960f62f
commit
82302b92f6
@ -1,52 +0,0 @@
|
||||
import {
|
||||
createRpcRequestedEv,
|
||||
createRpcProcessedEv,
|
||||
sendOnChannel,
|
||||
idGeneratorFactory,
|
||||
receiveOnChannelFactory
|
||||
} from './utils';
|
||||
import { EnclaveEvents, MatchingIdHandler, RpcEvent, RpcEventSuccess } from './types';
|
||||
import { Reject, Resolve } from 'mycrypto-shepherd/dist/lib/types';
|
||||
|
||||
const genId = idGeneratorFactory();
|
||||
|
||||
const matchOnId = (id: number | undefined, cb: MatchingIdHandler) => (
|
||||
ev: string,
|
||||
args: RpcEvent
|
||||
) => {
|
||||
if (id === args.id) {
|
||||
cb(ev, args);
|
||||
}
|
||||
};
|
||||
|
||||
type PromiseHandler<T> = (res: Resolve, rej: Reject) => MatchingIdHandler<T>;
|
||||
|
||||
const isRpcSuccess = (args: RpcEvent): args is RpcEventSuccess => args.payload;
|
||||
|
||||
const handleServerResponse: PromiseHandler<RpcEvent> = (res, rej) => (_, args) => {
|
||||
if (isRpcSuccess(args)) {
|
||||
res(args.payload);
|
||||
} else {
|
||||
rej(args.errMsg);
|
||||
}
|
||||
};
|
||||
|
||||
const clientRecieve = receiveOnChannelFactory((res, rej, id) =>
|
||||
matchOnId(id, handleServerResponse(res, rej))
|
||||
);
|
||||
|
||||
export const createClientRpcHandler = <Payload = any, Ret = any>(
|
||||
target: any,
|
||||
channel: EnclaveEvents,
|
||||
sender: any,
|
||||
receiver: any
|
||||
) => (payload: Payload): Promise<Ret> => {
|
||||
const id = genId();
|
||||
const sendingChannel = createRpcRequestedEv(channel);
|
||||
const receivingChannel = createRpcProcessedEv(channel);
|
||||
// send request event on channel to be processed by server
|
||||
sendOnChannel(sendingChannel, { payload, id }, target, sender);
|
||||
|
||||
// return a promise that resolves/rejects when a matching id response comes back from server
|
||||
return clientRecieve(receivingChannel, id, target, receiver);
|
||||
};
|
@ -1,40 +0,0 @@
|
||||
import {
|
||||
SignRawTransactionParams,
|
||||
ElectronInjectedGlobals,
|
||||
EnclaveEvents,
|
||||
SignRawTransaction
|
||||
} from './types';
|
||||
import { createClientRpcHandler } from './client-utils';
|
||||
import EventEmitter from 'events';
|
||||
|
||||
/**
|
||||
* This is what the web facing side would use as a package to interact with the enclave
|
||||
* It contains logic to differentiate between electron and browser / node enviroments
|
||||
* And then its functions communicate with the "server" to perform RPC calls
|
||||
* This file is the equivalent to a Provider for shepherd / INode implementation
|
||||
*/
|
||||
|
||||
const eventEmitter = new EventEmitter();
|
||||
function isElectronEnv() {
|
||||
return process.env.BUILD_ELECTRON;
|
||||
}
|
||||
|
||||
function getElectronWindow() {
|
||||
return window as ElectronInjectedGlobals;
|
||||
}
|
||||
|
||||
export function signRawTransaction(params: SignRawTransactionParams) {
|
||||
// if we're in electron env then use the globally injected signRawTransaction from the preload script
|
||||
if (isElectronEnv) {
|
||||
const w = getElectronWindow();
|
||||
w.signRawTransaction(params);
|
||||
} else {
|
||||
// otherwise create an rpc event handler based on "events" package
|
||||
createClientRpcHandler<SignRawTransactionParams, SignRawTransaction>(
|
||||
eventEmitter,
|
||||
EnclaveEvents.SIGN_RAW_TRANSACTION,
|
||||
eventEmitter.emit,
|
||||
eventEmitter.on
|
||||
);
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { EnclaveEvents, SignRawTransactionParams, SignRawTransaction } from './types';
|
||||
import { createClientRpcHandler } from './client-utils';
|
||||
|
||||
/**
|
||||
* Contains scripts to inject handlers into the web at runtime via preload
|
||||
* which "client" then can use to communicate with ipcMain process
|
||||
* to call methods on the enclave.
|
||||
* This package would be in the preload section of electron
|
||||
*/
|
||||
|
||||
const createElectronRpcHandler = <T = any, R = any>(channel: EnclaveEvents) =>
|
||||
createClientRpcHandler<T, R>(ipcRenderer, channel, ipcRenderer.send, ipcRenderer.once);
|
||||
|
||||
export const signRawTransaction = createElectronRpcHandler<
|
||||
SignRawTransactionParams,
|
||||
SignRawTransaction
|
||||
>(EnclaveEvents.SIGN_RAW_TRANSACTION);
|
@ -1,18 +0,0 @@
|
||||
import { createRpcRequestedEv, receiveOnChannelFactory } from './utils';
|
||||
import { EnclaveEvents, RpcEventServer } from './types';
|
||||
|
||||
// uses factory component, skips the promise handling and id parameter as those are only used on client side
|
||||
// cb instead handles the receiving event, and on success/failure emits on EnclaveEvent channel with its response
|
||||
const serverRecieve = (cb: any) => receiveOnChannelFactory<RpcEventServer, void, void>(() => cb);
|
||||
|
||||
// create a receiving channel for incoming events of Enclave events type
|
||||
// calls cb when a the channel has an event emitted to it
|
||||
export const createServerRpcHandler = (
|
||||
target: any,
|
||||
channel: EnclaveEvents,
|
||||
receiver: any,
|
||||
cb: any
|
||||
) => {
|
||||
const receivingChannel = createRpcRequestedEv(channel);
|
||||
serverRecieve(cb)(receivingChannel, target, receiver);
|
||||
};
|
@ -1,45 +0,0 @@
|
||||
import { createRpcProcessedEv, sendOnChannel } from './utils';
|
||||
import {
|
||||
EnclaveEvents,
|
||||
RpcEventServer,
|
||||
RpcEventHandler,
|
||||
SignRawTransactionParams,
|
||||
RpcEventSuccess,
|
||||
SignRawTransaction
|
||||
} from './types';
|
||||
import { ipcMain } from 'electron';
|
||||
import { createServerRpcHandler } from './server-utils';
|
||||
|
||||
/**
|
||||
* Contains the "server" implementation, currently only for electron
|
||||
* this should be run in ipcMain process to have access to node-hid
|
||||
* to perform signing actions for hardware wallets
|
||||
*/
|
||||
|
||||
// mock
|
||||
// this hard codes electron specific sending atm (ev.sender.send)
|
||||
export const signRawTransactionRequest: RpcEventHandler<
|
||||
RpcEventServer<SignRawTransactionParams>,
|
||||
void
|
||||
> = (ev: any, args) => {
|
||||
// "processing steps"
|
||||
// this is where you would sign the transaction
|
||||
const res: RpcEventSuccess<SignRawTransaction> = {
|
||||
payload: { r: '1', s: '1', v: '1' },
|
||||
id: args.id,
|
||||
errMsg: null
|
||||
};
|
||||
|
||||
// emit result back to client as presonse
|
||||
// pull this out later on into server-utils
|
||||
const sendingChannel = createRpcProcessedEv(EnclaveEvents.SIGN_RAW_TRANSACTION);
|
||||
sendOnChannel(sendingChannel, res, ev, ev.sender.send);
|
||||
};
|
||||
|
||||
// create the handler for SIGN_RAW_TRANSACTION
|
||||
createServerRpcHandler(
|
||||
ipcMain,
|
||||
EnclaveEvents.SIGN_RAW_TRANSACTION,
|
||||
ipcMain.on,
|
||||
signRawTransactionRequest
|
||||
);
|
@ -1,43 +0,0 @@
|
||||
export enum EnclaveEvents {
|
||||
SIGN_RAW_TRANSACTION = 'sign-raw-transaction'
|
||||
}
|
||||
|
||||
export interface RpcEventFailure {
|
||||
payload: null;
|
||||
errMsg: string;
|
||||
id: number;
|
||||
}
|
||||
|
||||
export interface RpcEventSuccess<T = any> {
|
||||
payload: T;
|
||||
errMsg: null;
|
||||
id: number;
|
||||
}
|
||||
|
||||
export type RpcEvent<T = any> = RpcEventFailure | RpcEventSuccess<T>;
|
||||
|
||||
export interface RpcEventServer<T = any> {
|
||||
payload: T;
|
||||
id: number;
|
||||
}
|
||||
|
||||
export type RpcEventHandler<A = any, R = any> = (event: EnclaveEvents, args: A) => R;
|
||||
|
||||
export type MatchingIdHandler<A = any> = (event: string, args: A) => void;
|
||||
|
||||
export interface SignRawTransactionParams {
|
||||
path: string;
|
||||
rawTxHex: string;
|
||||
}
|
||||
|
||||
export interface SignRawTransaction {
|
||||
s: string;
|
||||
v: string;
|
||||
r: string;
|
||||
}
|
||||
|
||||
export interface EnclaveProvider {
|
||||
signRawTransaction(params: SignRawTransactionParams): Promise<SignRawTransaction>;
|
||||
}
|
||||
|
||||
export type ElectronInjectedGlobals = Window & EnclaveProvider;
|
@ -1,34 +0,0 @@
|
||||
import { EnclaveEvents, RpcEventHandler } from './types';
|
||||
|
||||
export const idGeneratorFactory = () => {
|
||||
let callId = 0;
|
||||
return () => {
|
||||
const currValue = callId;
|
||||
callId += 1;
|
||||
return currValue;
|
||||
};
|
||||
};
|
||||
|
||||
export const createRpcRequestedEv = (e: EnclaveEvents) => `${e}-requested`;
|
||||
export const createRpcProcessedEv = (e: EnclaveEvents) => `${e}-processed`;
|
||||
|
||||
type Resolve<T = any> = (value?: T | PromiseLike<T>) => void;
|
||||
type Reject<T = any> = (reason?: T) => void;
|
||||
|
||||
export type ReceiveCb<Arguments, Ret> = (
|
||||
res: Resolve,
|
||||
rej: Reject,
|
||||
id?: number
|
||||
) => RpcEventHandler<Arguments, Ret>;
|
||||
|
||||
export const receiveOnChannelFactory = <CbArgs = any, CbRet = any, Ret = any>(
|
||||
cb: ReceiveCb<CbArgs, CbRet>
|
||||
) => (channel: string, target: any, on: any, id?: number): Promise<Ret> =>
|
||||
new Promise((res, rej) => on.call(target, channel, cb(res, rej, id)));
|
||||
|
||||
export const sendOnChannel = (
|
||||
channel: string,
|
||||
payload: any,
|
||||
target: any,
|
||||
emit: (args: any) => void
|
||||
) => emit.call(target, channel, payload);
|
Loading…
x
Reference in New Issue
Block a user