Skip to main content

dotbit

DotBit​

DotBit Tutorials

Installation​

install das-sdk-js https://docs.did.id/zh/developers/integration-frontend

npm install das-sdk
or
yarn add das-sdk

Install Web3MQ's JS SDK using a package manager of your choice

npm install @web3mq/client
or
yarn add @web3mq/client

Initialize Client and Connect to Web3MQ Network​

In order to connect to the Web3MQ Network, both users and developers authenticate through wallet signatures, we demonstrate below with an Ethereum wallet via Metamask, but Web3MQ is built to be compatible with wallets across different chains.

Initialize Client​

note

While we are committed to building an open and collectively owned public good, our early stage testnet requires an API key in order to connect. This is to control capacity to make sure that each early partner and developer is able to build a great experience on top of Web3MQ. Apply here.

As Web3MQ is a federated network, our default JS SDK client has a method to help you connect to the best node for you.

Simply calling Client.init without connectUrl or an empty string returns a url of the best node determined for you, and this url can be stored locally.

import { Client } from '@web3mq/client';

// You can save the bestEndpointUrl locally to skip endpoint search next time, which will save time, and
const bestEndpointUrl = await Client.init({
connectUrl: '', //
app_key: 'app_key', // temporary authorization key obtained by applying, will be removed in future testnets and mainnet
});

Calling Client.init with a specific connectUrl forces the client to connect to that specific node. When bestEndpointUrl is stored, it might be time-saving to connect directly instead of running the search again.

import { Client } from '@web3mq/client';

const fastUrl = await Client.init({
connectUrl: bestEndpointUrl, // takes in a valid endpoint url as input, when this paramter is given, client will always connect to that specific node.
app_key: 'app_key', // Appkey applied from our team
});

API endpoints​

During this initial testing phase, we've hosted complete networks of Web3MQ nodes in different regions around the globe. Connect to these endpoints below, to access the Web3MQ Testnet.

Get ETH address by .bit​

import { Das } from 'das-sdk';

const das = new Das({
url: 'https://indexer-v1.did.id',
});
const getAddress = async () => {
const data = await das.records('dasloveckb.bit');
let ethAddress = data && data.find((item) => item.key === 'address.eth');
console.log(ethAddress?.value || '');
return ethAddress?.value || '';
};

Sign with wallet to register user and obtain message encryption keys​

For any first-time user of Web3MQ's network, you'll need to register on Web3MQ's network. Web3MQ's JS SDK takes care of the key generation process and subsequent wallet signing process. Client.register.signMetaMask is a utility method that does this automatically.

Code​

// get your eth address 
const address = getAddress();

// You must ensure that the Client.init initialization is complete before running this
const { PrivateKey, PublicKey } = await Client.register.signMetaMask({
signContentURI: 'https://www.web3mq.com', // your signContent URI
EthAddress: address, // your eth address
});

console.log(PrivateKey, PublicKey);

// Keep your private key in a safe place, this is for demo purposes only
localStorage.setItem('PRIVATE_KEY', PrivateKey);
localStorage.setItem('PUBLICKEY', PublicKey);

Get Client Instance​

Code​

const keys = { PrivateKey, PublicKey };

const client = Client.getInstance(keys);

Create room​

After initializing the client and registering your user, the next step is to connect to a room

Code​

client.channel.createRoom();
<button
onClick={() => {
client.channel.createRoom();
}}>
createGroup
</button>

Send message​

Code​

client.message.sendMessage('Hello World');
<button
onClick={() => {
client.message.sendMessage('Hello World');
}}>
sendMessage
</button>