Wallet APIs and Callbacks

Once the user has been signed into Singularity Wallet (using either Singularity Auth or Custom Auth), you can use the APIs and Callbacks below to perform all standard wallet related actions.

Singularity Wallet API calls

You can use the following APIs to use the Singularity Drawer's feature set:

1) open: Use this to kick open the Singularity Drawer at any time. For new users, this will start the sign-in flow.

window.SingularityEvent.open();

2) close: Use this to close the Singularity Drawer.

window.SingularityEvent.close();

3) getConnectUserInfo: Use this to get the logged in user's details.

const data = await window.SingularityEvent.getConnectUserInfo();

4) logOut: Use this to log out the user.

window.SingularityEvent.logOut();

5) requestPersonalSignature: Use this to request the user to sign a message. This works for both types of wallets - those created by Singularity and even if the user has linked an external wallet to sign into your app. For Singularity created wallets, this will automatically open the Singularity Drawer to get the user to sign the message.

const signature = await window.SingularityEvent.requestPersonalSignature("message")

6) Request Native Token Transaction Signature: Use this to request the user to sign a native token transaction and get back the signed transaction for you to process on your own. You can pass all the required data to this object (https://web3js.readthedocs.io/en/v1.10.0/web3-eth-accounts.html?highlight=signtransaction#signtransaction)

const signature = await window.SingularityEvent.signTransaction(
   {from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
    gasPrice: '20000000000',
    gas: '21000',
    to: '0x3535353535353535353535353535353535353535',
    value: '10000000000000',
    data: ''});

7) Sign and Send Native Token Transaction : Use this to request the user to sign a native token transaction and directly broadcast it to the blockchain for processing. You can pass all the required data to this object (https://web3js.readthedocs.io/en/v1.10.0/web3-eth-accounts.html?highlight=signtransaction#signtransaction)

const signature = await window.SingularityEvent.signAndSendTransaction(
   {to: '0x3535353535353535353535353535353535353535',
    value: '0.1'});

8) Sign and Send Non-native Token Transaction: Use this to request the user to sign a non-native ERC-20 token transaction and directly broadcast it to the blockchain for processing.

const signature = await window.SingularityEvent.sendNonNativeToken(
    {recipient:'0x236B0bDC580d1Dd1bC1C182c925719b025aD239B',
    tokenAddress:'0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // Contract address of the ERC-20 token you want the user to transfer on that chain
    amount:'0.001'});

9) requestTypedSignature: Use this to request the user to sign a structured data (e.g. Approval or other transactions) and you will get back the signed transaction for you to process on your own.

const domain = {
    name: 'Game Name',
    version: '1',
    chainId: 97,
    verifyingContract: '0xED975dB5192aB41713f0080E7306E08188e53E7f'
  };

  const types = {
    bid: [
      { name: 'bidder', type: 'address' },
      { name: 'collectableId', type: 'uint256' },
      { name: 'amount', type: 'uint256' },
      { name: 'nounce', type: 'uint' }
    ]
  };

  const values = {
    bidder: '0xAa81f641d4b3546F05260F49DEc69Eb0314c47De',
    collectableId: 1,
    amount: 100,
    nounce: 1
  };
  
  const primaryType ="bid";
  
  const signature = await window.SingularityEvent.requestTypedSignature(domain, primaryType, types, values) 

9) Send NFT Transaction: Use this to request the user to sign a transaction to send an NFT to another address, and submit the transaction to the blockchain.

<coming soon>
Setup Subscriptions for Callbacks

You can subscribe to various events inside Singularity SDK. This is optional, you can also just use the APIs mentioned in the previous step to communicate with the Singularity SDK. To subscribe to a Singularity event, add the following call:

SingularityEvent.subscribe(eventName, callback);

The following are the names of the events that can be subscribed to, the names should be self-explanatory:

  • SingularityEvent-open. When the Singularity Drawer is opened by the user or by your application. Example code:

    window.SingularityEvent.subscribe('SingularityEvent-open', () =>{ console.log('SingularityEvent-open') });

  • SingularityEvent-close. When the Singularity Drawer is closed by the user or by your application. Example code:

    window.SingularityEvent.subscribe('SingularityEvent-close', () => { console.log('SingularityEvent-close'); });

  • SingularityEvent-login. When the user logs in using one of the allowed methods inside the Singularity Drawer. Example code:

    window.SingularityEvent.subscribe('SingularityEvent-login', data => { const userData = JSON.parse(data) console.log('SingularityEvent-login: userData --->', userData); });

  • SingularityEvent-logout. When the user or your application logs the user out. Example code:

    window.SingularityEvent.subscribe('SingularityEvent-logout', () => { console.log('Received logout') });

Sample Implementation

https://github.com/coinbrix/demo-app/

Last updated