enkryptcom / enKrypt

Enkrypt: Ethereum, Polkadot and Web3 Wallet
https://www.enkrypt.com/
MIT License
289 stars 165 forks source link

Tron Support #319

Closed wsdt closed 1 year ago

wsdt commented 1 year ago

Hi we are currently adding Tron support

We are quite progressed, we just struggle with sending the right events to the DApp / between the chrome extension components to notify it that Tron has been setup.

Would really appreciate some guidance.

Here is the current non refactored "tron/inject.ts" file:



  async request(request: TronRequest): Promise<TronResponse> {
    /*if (this.chainId === null) {
      await this.sendMessageHandler(
        this.name,
        JSON.stringify({
          method: "tron_chainId",
        })
      ).then((res) => {
        this.chainId = res;
        this.networkVersion = res;
      });
    }*/
    if (
      this.selectedAddress === null &&
      request.method === "tron_requestAccounts"
    ) {
      return this.sendMessageHandler(this.name, JSON.stringify(request))
        .then((res) => {
          console.log("GOT ACC: ", res);
          // TODO: Hex & type
          this.defaultAddress = {
            base58: res[0],
            hex: `0x${res[0]}`,
            name: "Account 1",
            type: "3",
          };
          this.selectedAddress = res[0];
          this.ready = true;
          const eventVal: TronLinkMessageEvent = {
            data: {
              isTronLink: true,
              message: {
                data: { address: res[0] },
                action: "connect",
              },
            },
          };
          this.emit("tunnel", eventVal);
          this.setAddress(res[0], "Account 1", "3");
          return {
            code: TronResponseCode.ok,
            message: res[0],
          };
        })
        .catch((err: Error) => {
          // TODO: 4000 or 4001 code?
          /*
          *  if (res.code === 4000) {
                        throw new WalletConnectionError(
                            'The same DApp has already initiated a request to connect to TronLink wallet, and the pop-up window has not been closed.'
                        );
                    }
                    if (res.code === 4001) {
                        throw new WalletConnectionError('The user rejected connection.');*/
          return {
            code: TronResponseCode.existingRequest,
            message: err.message,
          };
        });
    }
    return this.sendMessageHandler(this.name, JSON.stringify(request));
  }
  init(): Promise<any> {
    return this.enable();
  }

  enable(): Promise<any> {
    return this.request({ method: "tron_requestAccounts" });
  }
  isConnected(): boolean {
    return this.ready;
  }
  //deprecated
  send(
    method: string | JsonRpcRequest,
    params?: Array<unknown> | CallbackFunction
  ): Promise<TronResponse> | void {
    if ((method as JsonRpcRequest).method) {
      return this.sendAsync(
        method as JsonRpcRequest,
        params as CallbackFunction
      );
    } else {
      return this.request({
        method: method as string,
        params: params as Array<unknown>,
      });
    }
  }
  // //deprecated
  sendAsync(data: JsonRpcRequest, callback: CallbackFunction): void {
    const { method, params } = data as TronRequest;
    this.request({ method, params })
      .then((res) => {
        callback(null, {
          id: data.id,
          jsonrpc: "2.0",
          result: res,
        });
      })
      .catch((err) => callback(err));
  }
  handleMessage(msg: string): void {
    handleIncomingMessage(this, msg);
  }
}
wsdt commented 1 year ago

right now DApps are already able to read from the tronLink/tronWeb provider, but it doesn't recognize the wallet as being connected (nor does the wallet itself)

kvhnuke commented 1 year ago

can you reach out to me on telegram @kvhnuke itll be lot easier to debug there

wsdt commented 1 year ago

can you reach out to me on telegram @kvhnuke itll be lot easier to debug there

done thanks!