-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
021bfd6
commit c7e899f
Showing
9 changed files
with
495 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
Oops, something went wrong.