Skip to content

Commit

Permalink
Merge pull request #20 from MickWang/layer2-login
Browse files Browse the repository at this point in the history
Layer2 login
  • Loading branch information
backslash47 authored Jul 8, 2019
2 parents cd0b25d + 3e42393 commit f5ab4d3
Show file tree
Hide file tree
Showing 13 changed files with 1,527 additions and 273 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@bugsnag/js": "^5.1.0",
"@bugsnag/plugin-react": "^5.1.0",
"@ont-community/hdkey-secp256r1": "^1.0.1",
"@ont-community/ontology-ts-sdk-ledger": "^1.0.8",
"autoprefixer": "7.1.6",
"axios": "^0.18.0",
Expand Down
20 changes: 20 additions & 0 deletions src/background/api/stateChannelApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import HDKey from '@ont-community/hdkey-secp256r1';
import { decryptAccount } from '../../api/accountApi';
import { getWallet } from '../../api/authApi';
import { StateChannelLoginRequest } from '../../redux/transactionRequests';
import { getStore } from '../redux';

export async function stateChannelLogin(request: StateChannelLoginRequest, password: string): Promise<string> {
const state = getStore().getState();
const wallet = getWallet(state.wallet.wallet!);

const privateKey = decryptAccount(wallet, password);

const seed = privateKey.key;
const hdk = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'));
const path = "m/44'/1024'/0'/0/0";
const derive = hdk.derive(path);
const derivedPrv = Buffer.from(derive.privateKey).toString('hex');

return derivedPrv;
}
4 changes: 2 additions & 2 deletions src/background/dapp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { messageApi as message } from './message';
import { networkApi as network } from './network';
import { providerApi } from './provider';
import { smartContractApi as smartContract } from './smartContract';
import { stateChannelApi as stateChannel } from './state-channel';
import { stateChannelApi as stateChannel } from './stateChannel';

export function initDApiProvider() {
provider.registerProvider({
Expand All @@ -18,7 +18,7 @@ export function initDApiProvider() {
provider: providerApi,
smartContract,
stateChannel,
utils: client.api.utils,
utils: client.api.utils
},
});
}
12 changes: 12 additions & 0 deletions src/background/dapp/stateChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Signature, StateChannelApi } from 'ontology-dapi';
import { getRequestsManager } from '../requestsManager';


export const stateChannelApi: StateChannelApi = {
login(): Promise<string> {
return getRequestsManager().initStateChannelLogin();
},
sign(): Promise<Signature> {
throw new Error(('Not supported now'));
}
}
12 changes: 11 additions & 1 deletion src/background/redux/transactionRequestsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
ScCallReadRequest,
ScCallRequest,
ScDeployRequest,
StateChannelLoginRequest,
SUBMIT_REQUEST,
SwapRequest,
TransactionRequestsState,
Expand All @@ -40,6 +41,7 @@ import { messageSign } from '../api/messageApi';
import { swapNep } from '../api/neoApi';
import { registerOntId, transfer, withdrawOng } from '../api/runtimeApi';
import { scCall, scCallRead, scDeploy } from '../api/smartContractApi';
import { stateChannelLogin } from '../api/stateChannelApi';
import { transferToken } from '../api/tokenApi';

const defaultState: TransactionRequestsState = { requests: [] };
Expand Down Expand Up @@ -121,6 +123,9 @@ export const transactionRequestsAliases = {
case 'message_sign':
result = await submitMessageSign(request as MessageSignRequest, password!);
break;
case 'stateChannel_login':
result = await submitStateChannelLogin(request as StateChannelLoginRequest, password!);
break;
}

// resolves request
Expand Down Expand Up @@ -238,7 +243,8 @@ async function submitScCall(request: ScCallRequest, password: string, dispatch:
(element: any) => element.States,
);
return {
result: notify,
// Fixme: The Response of smartContract.invoke is {results: Result[], transaction: string} https://github.com/ontio/ontology-dapi/blob/master/src/api/types.ts
results: notify,
transaction: response.Result.TxHash,
};
}
Expand All @@ -250,6 +256,10 @@ async function submitMessageSign(request: MessageSignRequest, password: string)
return timeout(messageSign(request, password), 15000);
}

async function submitStateChannelLogin(request: StateChannelLoginRequest, password: string) {
return timeout(stateChannelLogin(request, password), 15000);
}

async function submitScCallRead(request: ScCallReadRequest) {
const response = await timeout(scCallRead(request), 15000);

Expand Down
22 changes: 22 additions & 0 deletions src/background/requestsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ScCallReadRequest,
ScCallRequest,
ScDeployRequest,
StateChannelLoginRequest,
TransactionRequestsState,
TransferRequest,
} from '../redux/transactionRequests';
Expand Down Expand Up @@ -102,6 +103,25 @@ export class RequestsManager {
return deferred.promise;
}

public async initStateChannelLogin() {
const requestId = uuid();
// stores deferred object to resolve when the transaction is resolved
const deferred = new Deferred<any>();
this.requestDeferreds.set(requestId, deferred);

await this.store.dispatch(
Actions.transactionRequests.addRequest<StateChannelLoginRequest>({
id: requestId,
type: 'stateChannel_login',
}),
);

await this.popupManager.show();
await this.popupManager.callMethod('history_push', '/stateChannel-login', { requestId, locked: true });

return deferred.promise;
}

public async initScCall(args: {
contract: string;
method: string;
Expand Down Expand Up @@ -233,6 +253,8 @@ export class RequestsManager {
}
}



let requestsManager: RequestsManager;

export function initRequestsManager(store: GlobalStore, popupManager: PopupManager) {
Expand Down
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ declare module 'uuid';
declare module 'websocket-as-promised';
declare module '@ledgerhq/hw-transport-node-hid';
declare module '@ledgerhq/hw-transport-u2f';
declare module '@ont-community/hdkey-secp256r1'
1 change: 1 addition & 0 deletions src/popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const unsubscribe = store.subscribe(() => {
<Route path="/transfers" exact={true} component={Pages.Transfers} />

<Route path="/message-sign" exact={true} component={Pages.MessageSign} />
<Route path="/stateChannel-login" exact={true} component={Pages.StateChannelLogin} />

<Route path="/" exact={true} component={Pages.Home} />
<Route path="/new" exact={true} component={Pages.New} />
Expand Down
1 change: 1 addition & 0 deletions src/popup/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ export { AccountsDel } from './accountsDel/accountsDel';
export { Identities } from './identity/identities/identities';
export { IdentitiesAdd } from './identity/identitiesAdd/identitiesAdd';
export { IdentitiesDel } from './identity/identitiesDel/identitiesDel';
export { StateChannelLogin } from './stateChannelLogin/stateChannelLogin';
55 changes: 55 additions & 0 deletions src/popup/pages/stateChannelLogin/stateChannelLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2018 Matus Zamborsky
* This file is part of The Ontology Wallet&ID.
*
* The The Ontology Wallet&ID is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Ontology Wallet&ID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The Ontology Wallet&ID. If not, see <http://www.gnu.org/licenses/>.
*/
import { get } from 'lodash';
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
import { bindActionCreators, Dispatch } from 'redux';
import { dummy, reduxConnect, withProps } from '../../compose';
import { Actions } from '../../redux';
import { Props, StateChannelLoginView } from './stateChannelLoginView';

const mapDispatchToProps = (dispatch: Dispatch) =>
bindActionCreators(
{
resolveRequest: Actions.transactionRequests.resolveRequest,
},
dispatch,
);

const enhancer = (Component: React.ComponentType<Props>) => (props: RouteComponentProps<any>) =>
reduxConnect(dummy, mapDispatchToProps, (reduxProps, actions) =>
withProps(
{
handleCancel: async () => {
props.history.goBack();

const requestId: string = get(props.location, 'state.requestId');
await actions.resolveRequest(requestId, 'CANCELED');
},
handleConfirm: async () => {
const requestId: string = get(props.location, 'state.requestId');

props.history.push('/confirm', { requestId, redirectSucess: '/dashboard', redirectFail: '/dashboard' });
},
locked: get(props.location, 'state.locked', false)
},
(injectedProps) => <Component {...injectedProps} />,
),
);

export const StateChannelLogin = enhancer(StateChannelLoginView);
46 changes: 46 additions & 0 deletions src/popup/pages/stateChannelLogin/stateChannelLoginView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2018 Matus Zamborsky
* This file is part of The Ontology Wallet&ID.
*
* The The Ontology Wallet&ID is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Ontology Wallet&ID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The Ontology Wallet&ID. If not, see <http://www.gnu.org/licenses/>.
*/
import * as React from 'react';
import { Button } from 'semantic-ui-react';
import { AccountLogoHeader, Filler, StatusBar, View } from '../../components';


export interface Props {
locked: boolean;
handleConfirm: () => void;
handleCancel: () => void;
}

export const StateChannelLoginView: React.SFC<Props> = (props) => (
<View orientation="column" fluid={true}>
<View orientation="column" className="part gradient">
<AccountLogoHeader title="Login State Channel Client" />
<View content={true} className="spread-around">
<View>You are about to login a state channel client.</View>
</View>
</View>
<View orientation="column" fluid={true} content={true}>
<Filler />
<View className="buttons">
<Button onClick={props.handleConfirm}>Continue</Button>
<Button onClick={props.handleCancel}>Cancel</Button>
</View>
</View>
<StatusBar />
</View>
);
8 changes: 7 additions & 1 deletion src/redux/transactionRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export type TransactionType =
| 'sc_call'
| 'sc_call_read'
| 'sc_deploy'
| 'message_sign';
| 'message_sign'
| 'stateChannel_login';

export interface TransactionRequest {
id: string;
Expand Down Expand Up @@ -57,6 +58,11 @@ export interface MessageSignRequest extends TransactionRequest {
message: string;
}

// tslint:disable-next-line:no-empty-interface
export interface StateChannelLoginRequest extends TransactionRequest {

}

export interface RegisterOntIdRequest extends TransactionRequest {
identity: string;
encryptedWif: string;
Expand Down
Loading

0 comments on commit f5ab4d3

Please sign in to comment.