Skip to content

Commit

Permalink
Update background controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
slient-coder committed Mar 4, 2023
1 parent 021bfd6 commit c7e899f
Show file tree
Hide file tree
Showing 9 changed files with 495 additions and 25 deletions.
45 changes: 25 additions & 20 deletions src/background/controller/base.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { cloneDeep } from 'lodash';

import { keyringService, preferenceService } from '../service';
import { Account } from '../service/preference';

class BaseController {
// getCurrentAccount = async () => {
// let account = preferenceService.getCurrentAccount()
// if (account) {
// const accounts = await this.getAccounts()
// const matchAcct = accounts.find((acct) => account!.address === acct.address)
// if (!matchAcct) account = undefined
// }
// if (!account) {
// ;[account] = await this.getAccounts()
// if (!account) return null
// preferenceService.setCurrentAccount(account)
// }
// return cloneDeep(account) as Account
// }
// syncGetCurrentAccount = () => {
// return preferenceService.getCurrentAccount() || null
// }
// getAccounts = (): Promise<Account[]> => {
// return keyringService.getAllVisibleAccountsArray()
// }
getCurrentAccount = async () => {
let account = preferenceService.getCurrentAccount();
if (account) {
const accounts = await this.getAccounts();
const matchAcct = accounts.find((acct) => account!.address === acct.address);
if (!matchAcct) account = undefined;
}
if (!account) {
[account] = await this.getAccounts();
if (!account) return null;
preferenceService.setCurrentAccount(account);
}
return cloneDeep(account) as Account;
};
syncGetCurrentAccount = () => {
return preferenceService.getCurrentAccount() || null;
};
getAccounts = (): Promise<Account[]> => {
return keyringService.getAllVisibleAccountsArray();
};
}

export default BaseController;
1 change: 1 addition & 0 deletions src/background/controller/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as walletController } from './wallet';
export { default as providerController } from './provider';
114 changes: 114 additions & 0 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

import { permissionService, sessionService } from '@/background/service';
import { CHAINS } from '@/shared/constant';

import BaseController from '../base';
import wallet from '../wallet';
import { publicKeyToAddress } from '@/background/utils/tx-utils';



class ProviderController extends BaseController {

connect = async ({ session: { origin } }) => {
console.log('hasPermiss',origin,permissionService.hasPermission(origin))
if (!permissionService.hasPermission(origin)) {
// throw ethErrors.provider.unauthorized();
}

const _account = await this.getCurrentAccount();
const account = _account ? [_account.address.toLowerCase()] : [];
sessionService.broadcastEvent('accountsChanged', account);
const connectSite = permissionService.getConnectedSite(origin);
if (connectSite) {
const chain = CHAINS[connectSite.chain];
sessionService.broadcastEvent(
'chainChanged',
{
networkVersion: chain.network
},
origin
);
}
return _account
};

@Reflect.metadata('SAFE', true)
getNetwork = async () => {
const network = wallet.getNetworkType()
return network;
};

@Reflect.metadata('SAFE', true)
getAddress = async () => {
const account = await wallet.getCurrentAccount();
if(!account) return ''
const addressType = wallet.getAddressType();
const networkType = wallet.getNetworkType()
const address = publicKeyToAddress(account.address,addressType,networkType)
return address;
};

@Reflect.metadata('SAFE', true)
getPublicKey = async () => {
const account = await wallet.getCurrentAccount();
if(!account) return ''
return account.address;
};

@Reflect.metadata('SAFE', true)
getBalance = async () => {
const account = await this.getCurrentAccount();
if (!account) return null;
const balance = await wallet.getAddressBalance(account.address)
return balance;
};

@Reflect.metadata('APPROVAL', ['SendBitcoin', () => {
// todo check
}])
sendBitcoin = async () => {
// todo
}

@Reflect.metadata('APPROVAL', ['SendInscription', () => {
// todo check
}])
sendInscription = async () => {
// todo
}

@Reflect.metadata('APPROVAL', ['SignText', () => {
// todo check
}])
signText = async () => {
// todo
}

@Reflect.metadata('APPROVAL', ['SignTx', () => {
// ttodo check
}])
signTx = async () => {
const account = await this.getCurrentAccount();
console.log(account,'go')
}

@Reflect.metadata('SAFE',true)
pushTx = async () => {
// todo
}

@Reflect.metadata('APPROVAL', ['SignPsbt', () => {
// todo check
}])
signPsbt = async () => {
// todo
}

@Reflect.metadata('SAFE', true)
pushPsbt = async () => {
// todo
}
}

export default new ProviderController();
29 changes: 29 additions & 0 deletions src/background/controller/provider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ethErrors } from 'eth-rpc-errors';

import { sessionService, keyringService } from '@/background/service';
import { tab } from '@/background/webapi';

import internalMethod from './internalMethod';
import rpcFlow from './rpcFlow';

tab.on('tabRemove', (id) => {
sessionService.deleteSession(id);
});

export default async (req) => {
const {
data: { method }
} = req;

if (internalMethod[method]) {
return internalMethod[method](req);
}

const hasVault = keyringService.hasVault();
if (!hasVault) {
throw ethErrors.provider.userRejectedRequest({
message: 'wallet must has at least one account'
});
}
return rpcFlow(req);
};
32 changes: 32 additions & 0 deletions src/background/controller/provider/internalMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { keyringService } from '@/background/service';

import providerController from './controller';

const tabCheckin = ({
data: {
params: { origin, name, icon }
},
session
}) => {
session.setProp({ origin, name, icon });
};

const getProviderState = async (req) => {
const {
session: { origin }
} = req;

const isUnlocked = keyringService.memStore.getState().isUnlocked;

return {
network: 'mainnet',
isUnlocked,
accounts: isUnlocked ? await providerController.getAddress() : [],
networkVersion: ''
};
};

export default {
tabCheckin,
getProviderState
};
Loading

0 comments on commit c7e899f

Please sign in to comment.