Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WCIP-3: WalletConnect Instant #3

Open
pedrouid opened this issue May 11, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@pedrouid
Copy link
Member

commented May 11, 2019

WalletConnect Instant ⚡

What's an Instant Request?

WalletConnect Instant feature introduces the ability to make an ephemeral session that bypasses the process of session approval by the wallet, displaying to the user a call request to be signed from the Dapp right after scanning the QR Code. This is useful for one-time use-cases like payments, topping-up or withdrawals. This is still highly experimental and hasn't been published to the official library. A PoC to aggregate feedback on this feature!

Source code available at WalletConnect/walletconnect-monorepo on walletconnect-instant branch.

Install

yarn add @walletconnect/browser @walletconnect/qrcode-modal

# OR

npm install --save @walletconnect/browser @walletconnect/qrcode-modal

Create Instant Request

import WalletConnect from "@walletconnect/browser";
import WalletConnectQRCodeModal from "@walletconnect/qrcode-modal";

/**
 *  Create a walletConnector
 */
const walletConnector = new WalletConnect({
  bridge: "https://bridge.walletconnect.org" // Required
});

/**
 *  Draft Instant Request
 */
const instantRequest = {
  id: 1,
  jsonrpc: "2.0",
  method: "eth_sendTransaction",
  params: [
    {
      from: "0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",
      to: "0x0000000000000000000000000000000000000000",
      nonce: 1,
      gas: 100000,
      value: 0,
      data: "0x0"
    }
  ]
};

/**
 *  Subscribe to Instant Request URI
 */
walletConnector.on("instant_uri", (error, payload) => {
  if (error) {
    throw error;
  }

  const uri = payload.params[0].uri;

  /**
   *  Display QR Code Modal
   */
  WalletConnectQRCodeModal.open(uri, () => {
    console.log("QR Code Modal closed");
  });
});

/**
 *  Create Instant Request
 */
walletConnector
  .createInstantRequest(instantRequest)
  .then(result => {
    /**
     *  Get Instant Request Result
     */
    console.log(result);

    /**
     *  Close QR Code Modal
     */

    WalletConnectQRCodeModal.close();
  })
  .catch(error => {
    /**
     *  Handle Error or Rejection
     */

    console.error(error);
  });
@pedrouid

This comment has been minimized.

Copy link
Member Author

commented May 11, 2019

Typescript SDK implementation

 public async createInstantRequest (
    instantRequest: Partial<IJsonRpcRequest>
  ): Promise<void> {
    this._key = await this._generateKey()

     const request: IJsonRpcRequest = this._formatRequest({
      method: 'wc_instantRequest',
      params: [
        {
          peerId: this.clientId,
          peerMeta: this.clientMeta,
          request: this._formatRequest(instantRequest)
        }
      ]
    })

    this.handshakeId = request.id
    this.handshakeTopic = uuid()

     this._eventManager.trigger({
      event: 'instant_uri',
      params: [{ uri: this.uri }]
    })

     try {
      const result = await this._sendCallRequest(request)

       if (result) {
        this.killSession()
      }

       return result
    } catch (error) {
      throw error
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.