BinanceChain.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { BscConnector } from '@binance-chain/bsc-connector';
  2. import EventDispatcher from './EventDispatcher.js';
  3. let bscConnector;
  4. let isRegister = false;
  5. let CodeEnum;
  6. let walletMgr;
  7. async function init(_walletMgr,fun){
  8. walletMgr = _walletMgr;
  9. CodeEnum = walletconfig.ReturnCodeEnum;
  10. bscConnector = new BscConnector({
  11. supportedChainIds: [56, 97]
  12. })
  13. try {
  14. registerWalletEvent();
  15. } catch (error) {
  16. //alert('Please install the MetaMask plug-in and unlock your Ethereum account');
  17. return ;
  18. }
  19. connectWallectAccount(fun)
  20. }
  21. async function connectWallectAccount(fun){
  22. let act = await bscConnector.activate();
  23. let account = await bscConnector.getAccount();
  24. walletMgr.currentAccount = account;
  25. let chainId = await bscConnector.getChainId();
  26. walletMgr.currentChaidId = chainId;
  27. walletMgr.crateContract(act.provider);
  28. // func({code:CodeEnum.SUCCESS,data:account});
  29. if(walletMgr.currentChaidId == walletMgr.currentChain[0].chainId && walletMgr.currentAccount != null){
  30. fun({code:CodeEnum.SUCCESS,data:walletMgr.currentAccount});
  31. walletMgr.crateContract(provider)
  32. }else{
  33. fun({code:CodeEnum.NOT_CURRENT_CHAIN,data:"Please switch the chain"});
  34. }
  35. }
  36. function registerWalletEvent(){
  37. if(isRegister) return;
  38. else isRegister = true;
  39. BinanceChain.on('chainChanged', onChainChanged);
  40. BinanceChain.on('accountsChanged',onAccountsChanged);
  41. BinanceChain.on('disconnect',onDisConnect);
  42. }
  43. function close(){
  44. }
  45. function onDisConnect(e){
  46. EventDispatcher.Dispatch(EventDispatcher.Enum.loginOut);
  47. }
  48. function onChainChanged(e){
  49. if(walletMgr.getWalletCurType() != walletMgr.WalletEnum.BinanceChain)return;
  50. walletMgr.currentChaidId = e;
  51. if ( walletMgr.currentChain[0].chainId != e)
  52. EventDispatcher.Dispatch(EventDispatcher.Enum.loginOut);
  53. EventDispatcher.Dispatch(EventDispatcher.Enum.chainChanged_wallet, e);
  54. }
  55. function onAccountsChanged(e){
  56. if(walletMgr.getWalletCurType() != walletMgr.WalletEnum.BinanceChain)return;
  57. if(!(typeof e=='string'))e = e[0];
  58. walletMgr.currentAccount = e;
  59. if (localStorage.getItem('authaddress') != e)
  60. EventDispatcher.Dispatch(EventDispatcher.Enum.loginOut);
  61. EventDispatcher.Dispatch(EventDispatcher.Enum.accountsChanged_wallet, e);
  62. }
  63. function sign(msg,func){
  64. BinanceChain.request({method: "eth_sign", params: [walletMgr.currentAccount, msg]}).then((sig) => {
  65. console.log('SIGNED:' + sig)
  66. func({code:CodeEnum.SUCCESS,data:sig});
  67. }).catch((error)=>{
  68. console.log('SIGNED:' , error)
  69. func({code:CodeEnum.SIGNATURE_FAILED,data:error});
  70. })
  71. }
  72. export default{
  73. connectWallectAccount,
  74. init,
  75. close,
  76. sign,
  77. }