Skip to content

Latest commit

 

History

History
789 lines (718 loc) · 20.3 KB

dotbit.md

File metadata and controls

789 lines (718 loc) · 20.3 KB

DotBit

Table of Contents

constructor(config)

To create a new DotBit instance.

Note: For a better development experience, we suggest you using createInstance to initialize DotBit, since it has already set a bunch of default configs (e.g. network, indexer, builder and signer).

Parameter

config: DotBitConfig

  • (Optional) network: BitNetwork,
  • (Optional) cacheProvider: CacheProvider,
  • (Optional) bitIndexer: BitIndexer,
  • (Optional) bitBuilder: RemoteTxBuilder,
  • (Optional) signer: BitSigner,

Return Value

DotBit

Example

const { DotBit } = require('dotbit');

// To create a new DotBit instance.
const config = {
  network: "testnet",
  bitIndexer: new BitIndexer({
    uri: "https://test-indexer.did.id",
  }),
  bitBuilder: new RemoteTxBuilder({
    subAccountUri: "https://test-subaccount-api.did.id/v1",
    registerUri: "https://test-register-api.did.id/v1",
  }),
};
const dotbit = new DotBit(config);
console.log(dotbit);

// ...
// The printed result would be like:
DotBit {
  plugins: [],
  network: 'testnet',
  bitIndexer: BitIndexer {},
  bitBuilder: RemoteTxBuilder {},
}

network

To get the network of current DotBit instance.

Parameter

N/A

Return Value

'mainnet' | 'testnet'

Example

// To get the network of current DotBit instance.
console.log(dotbit.network)

// ...
// The printed result would be like:
mainnet

cacheProvider

To get the cache provider of current DotBit instance.

Parameter

N/A

Return Value

CacheProvider

Example

// To get the cache provider of current DotBit instance
console.log(dotbit.cacheProvider)

// ...
// The printed result would be like:
{ get: [Function: get], set: [Function: set] }

bitIndexer

To get the indexer of current DotBit instance.

Parameter

N/A

Return Value

BitIndexer

Example

// To get the indexer of current DotBit instance
console.log(dotbit.bitIndexer)

// ...
// The printed result would be like:
BitIndexer { rpc: JSONRPC { url: 'https://indexer-v1.did.id', id: 0 } }

bitBuilder

To get the builder of current DotBit instance.

Parameter

N/A

Return Value

RemoteTxBuilder

Example

// To get the builder of current DotBit instance.
console.log(dotbit.bitBuilder)

// ...
// The printed result would be like:
RemoteTxBuilder {
  subAccountAPI: SubAccountAPI {
    baseUri: 'https://subaccount-api.did.id/v1',
    net: Networking { baseUri: 'https://subaccount-api.did.id/v1' }
  },
  registerAPI: RegisterAPI {
    baseUri: 'https://register-api.did.id/v1',
    net: Networking { baseUri: 'https://register-api.did.id/v1' }
  }
}

signer

To get the signer of current DotBit instance.

Parameter

N/A

Return Value

BitSigner

Example

// To get the signer of current DotBit instance.
console.log(dotbit.signer)

plugins

To get all the installed plugins in current DotBit instance.

Parameter

N/A

Return Value

BitPluginBase[]

  • (Optional) version: string,
  • (Optional) name: string,
  • onInstall: (dotbit: DotBit) => void,
  • (Optional) onUninstall: (dotbit: DotBit) => void,
  • (Optional) onInitAccount: (bitAccount: BitAccount) => void,

Example

// To get all the installed plugins in current DotBit instance
console.log(dotbit.plugins)

// ...
// The printed result would be like:
undefined

installPlugin(plugin)

To install a plugin for .bit SDK.

Parameters

  • plugin: BitPluginBase
    • (Optional) version: string,
    • (Optional) name: string,
    • onInstall: (dotbit: DotBit) => void,
    • (Optional) onUninstall: (dotbit: DotBit) => void,
    • (Optional) onInitAccount: (bitAccount: BitAccount) => void,

Return Value

N/A

Example

import { createInstance } from 'dotbit'
import { BitPluginAvatar } from '@dotbit/plugin-avatar'

const dotbit = createInstance()
// To install the BitPluginAvatar plugin
dotbit.installPlugin(new BitPluginAvatar())
dotbit.avatar('imac.bit').then(console.log) // { url: 'https://thiscatdoesnotexist.com' }

uninstallPlugin(plugin)

To uninstall a plugin for .bit SDK.

Parameters

  • plugin: BitPluginBase
    • (Optional) version: string,
    • (Optional) name: string,
    • onInstall: (dotbit: DotBit) => void,
    • (Optional) onUninstall: (dotbit: DotBit) => void,
    • (Optional) onInitAccount: (bitAccount: BitAccount) => void,

Return Value

N/A

Example

import { createInstance } from 'dotbit'
import { BitPluginAvatar } from '@dotbit/plugin-avatar'

const dotbit = createInstance()
const avatarPlugin = new BitPluginAvatar()
dotbit.installPlugin(avatarPlugin)

// ...
// To uninstall the BitPluginAvatar plugin
dotbit.uninstallPlugin(avatarPlugin)

serverInfo()

To get the server info of current .bit indexer.

Parameters

N/A

Return Value

Promise

  • is_latest_block_number: boolean,
  • current_block_number: number,

Example

// To get the server info of current .bit indexer
dotbit.serverInfo().then(console.log);

// ...
// The printed result would be like:
{ is_latest_block_number: true, current_block_number: 8834119 }

reverse(keyInfo)

To get the .bit alias for a given blockchain address

Note: Only when .bit alias is set at https://app.did.id/alias by user, reverse record is valid.

Parameters

  • keyInfo: KeyInfo
    • key: string. The address on a certain blockchain
    • (Optional) coin_type: string. (60: ETH, 195: TRX, 714: BNB, 966: Matic). See What is coin_type? in FAQ for more details.

Return Value

Promise

Example

// To get the BitAccount instance of Ethereum addresss '0x1d643fac9a463c9d544506006a6348c234da485f'
dotbit.reverse({
  key: '0x1d643fac9a463c9d544506006a6348c234da485f',
  coin_type: "60" // The coin type of ETH
}).then(console.log)

// ...
// The printed result would be like:
BitAccount {
  account: 'jeffx.bit',
  bitIndexer: BitIndexer {},
  bitBuilder: RemoteTxBuilder {},
  signer: // Your signer instance
}

alias

The API is the same as reverse(keyInfo).

accountsOfOwner(keyInfo)

List all .bit accounts (including SubDID accounts) of a given blockchain owner address

Parameters

  • keyInfo: KeyInfo
    • key: string. The address on a certain blockchain
    • (Optional) coin_type: string. (60: ETH, 195: TRX, 714: BNB, 966: Matic). See What is coin_type? in FAQ for more details.

Return Value

Promise<BitAccountListItem[]>

Example

// To get all BitAccount instances of Ethereum addresss '0x1d643fac9a463c9d544506006a6348c234da485f'
dotbit.accountsOfOwner({
  key: "0x1d643fac9a463c9d544506006a6348c234da485f",
  coin_type: "60" // The coin type of ETH
}).then(console.log);

// ...
// The printed result would be like:
[
  ...,
  {
    "account": "gaoyuanyuan.bit",
    "display_name": "gaoyuanyuan.bit",
    "registered_at": 1626962335,
    "expired_at": 1721570335
  }
]

accountsOfManager(keyInfo)

List all .bit accounts (including SubDID accounts) of a given blockchain manager address

Parameters

  • keyInfo: KeyInfo
    • key: string. The address on a certain blockchain
    • (Optional) coin_type: string. (60: ETH, 195: TRX, 714: BNB, 966: Matic). See What is coin_type? in FAQ for more details.

Return Value

Promise<BitAccountListItem[]>

Example

// To get all BitAccount instances of Ethereum addresss '0x1d643fac9a463c9d544506006a6348c234da485f'
dotbit.accountsOfManager({
  key: "0x1d643fac9a463c9d544506006a6348c234da485f",
  coin_type: "60" // The coin type of ETH
}).then(console.log);

// ...
// The printed result would be like:
[
  ...,
    {
      "account": "gaoyuanyuan.bit",
      "display_name": "gaoyuanyuan.bit",
      "registered_at": 1626962335,
      "expired_at": 1721570335
    }
]

account(account)

To get the BitAccount instance of a given account name.

Parameters

  • account: string

Return Value

BitAccount (Note: It is not a promise.)

Example

// To get the BitAccount instance of "west.bit"
const result = dotbit.account("west.bit");
console.log(result);

// ...
// The printed result would be like:
BitAccount {
  account: 'west.bit',
  bitIndexer: BitIndexer {},
  bitBuilder: RemoteTxBuilder {},
  signer: // Your signer instance
}

exist(account)

To determine whether a given account exists.

Parameters

  • account: string

Return Value

Promise<boolean>

Example

// To check if the account "west.bit" exists
dotbit.exist("west.bit").then(console.log)

// ...
// The printed result would be like:
true

accountById(accountId)

To get the BitAccount instance of a given account ID.

Parameters

  • accountId: string. Remember it is a hexadecimal ID, not the account name.

Return Value

Promise<BitAccount>

Example

// To get the BitAccount instance of account ID '0x5728088435fb8788472a9ca601fbc0b9cbea8be3'
dotbit.accountById("0x5728088435fb8788472a9ca601fbc0b9cbea8be3").then(console.log);

// ...
// The printed result would be like:
BitAccount {
  account: 'imac.bit',
  bitIndexer: BitIndexer {},
  bitBuilder: RemoteTxBuilder {},
  signer: // Your signer instance
}

records(account, key)

To get all records of a given account.

Parameters

  • account: string
  • (Optional) key: string. The key related to a specific record, only the matched record will be displayed.

Return Value

Promise<BitAccountRecordExtended[]>

  • key: string,
  • value: string,
  • label: string,
  • ttl: string,
  • type: string,
  • subtype: string,

Example

// Get all records of "west.bit"
dotbit.records("west.bit").then(console.log)

// ...
// The printed result would be like:
[
  ...
  {
    key: 'address.polygon',
    label: 'Usually',
    value: '0xB2bE2887A26f44555835EEaCC47d65B88b6B42c2',
    ttl: '300',
    type: 'address',
    subtype: 'polygon'
  },
  {
    key: 'profile.discord',
    label: 'Discord Username',
    value: 'west.bit#8906',
    ttl: '300',
    type: 'profile',
    subtype: 'discord'
  },
  ...
]

// Get record of profile.discord of "west.bit"
dotbit.records("west.bit", "profile.discord").then(console.log)

// ...
// The printed result would be like:
[
  {
    key: 'profile.discord',
    label: 'Discord Username',
    value: 'west.bit#8906',
    ttl: '300',
    type: 'profile',
    subtype: 'discord'
  }
]

Note: If no record with the given key exists, an empty array will be returned.

accountInfo(account)

To get the account info of a given account.

Parameters

  • account: string

Return Value

Promise<AccountInfo>

  • account: string,
  • account_alias: string,
  • account_id_hex: string,
  • next_account_id_hex: string,
  • create_at_unix: number,
  • expired_at_unix: number,
  • status: number,
  • das_lock_arg_hex: string,
  • owner_algorithm_id: number,
  • owner_key: string,
  • manager_algorithm_id: number,
  • manager_key: string,
  • enable_sub_account: number,

Example

// Get the account info of "imac.bit"
dotbit.accountInfo("imac.bit").then(console.log)

// ...
// The printed result would be like:
{
  account: 'imac.bit',
  account_alias: 'imac.bit',
  account_id_hex: '0x5728088435fb8788472a9ca601fbc0b9cbea8be3',
  next_account_id_hex: '0x57280ab92f213d74c7a185e9b9d26d0a795108de',
  create_at_unix: 1671164348,
  expired_at_unix: 1702700348,
  status: 0,
  das_lock_arg_hex: '0x05b2be2887a26f44555835eeacc47d65b88b6b42c205b2be2887a26f44555835eeacc47d65b88b6b42c2',
  owner_algorithm_id: 5,
  owner_key: '0xb2be2887a26f44555835eeacc47d65b88b6b42c2',
  manager_algorithm_id: 5,
  manager_key: '0xb2be2887a26f44555835eeacc47d65b88b6b42c2',
  enable_sub_account: 0
}

addresses(account, chain)

Parameters

  • account: string
  • (Optional) chain: string. It could be the coin types defined in slip44(e.g. '60' for ETH / '9006' for BSC). It could also be the symbols of coin (e.g. 'eth' / 'bsc')

Return Value

Promise<BitAccountRecordAddress[]>

  • key: string,
  • value: string,
  • label: string,
  • ttl: string,
  • type: string,
  • subtype: string,
  • coin_type: string,

Example

// Get all addresses of "west.bit"
dotbit.addresses("west.bit").then(console.log)

// ...
// The printed result would be like:
[
  {
    key: 'address.bsc',
    label: 'HyperPay',
    value: '0x44A042eFA495A10E10d47269BB33bcB4991b80f4',
    ttl: '300',
    type: 'address',
    subtype: 'bsc',
    coin_type: '9006'
  },
  {
    key: 'address.trx',
    label: 'HyperPay',
    value: 'TJBr5JqG8mUX6PdKAbM4Qy3n1eZe7wP8bv',
    ttl: '300',
    type: 'address',
    subtype: 'trx',
    coin_type: '195'
  },
  ...
]

// Get the Ethereum address of "west.bit" by passing coin types
dotbit.addresses("west.bit", "60").then(console.log);

// Get the Ethereum address of "west.bit" by passing coin symbols
// Note: Developers are encouraged to use coin_type instead of plain symbol like 'eth' as coin_type is a more standard way to identify a chain/coin, and there will only be coin_type on chain in the future.
dotbit.addresses("west.bit", "eth").then(console.log);

// ...
// The printed result would be the same:
[
  {
    key: 'address.eth',
    label: 'Private Account',
    value: '0xB2bE2887A26f44555835EEaCC47d65B88b6B42c2',
    ttl: '300',
    type: 'address',
    subtype: 'eth',
    coin_type: '60'
  }
]

addrs(account, chain)

An alias for API addresses(account, chain).

dwebs(account, key)

Get all DWebs of a given account

Parameters

  • account: string
  • (Optional) key: string. Only records of matched subtype will be displayed.

Return Value

Promise<BitAccountRecordExtended[]>

  • key: string,
  • value: string,
  • label: string,
  • ttl: string,
  • type: string,
  • subtype: string,

Example

// Get all DWebs of "code.bit"
dotbit.dwebs("code.bit").then(console.log)

// ...
// The printed result would be like:
[
  {
    key: 'dweb.ipns',
    label: '',
    value: 'k51qzi5uqu5dgqjy1i78mz3oumplzt0cye32w9m8ix8hg9chpz5trvj8luwv0c',
    ttl: '300',
    type: 'dweb',
    subtype: 'ipns'
  }
]

// Get all ipns DWebs of "code.bit"
dotbit.dwebs("code.bit", "ipns").then(console.log);

// ...
// The printed result would be like:
[
  {
    key: 'dweb.ipns',
    label: '',
    value: 'k51qzi5uqu5dgqjy1i78mz3oumplzt0cye32w9m8ix8hg9chpz5trvj8luwv0c',
    ttl: '300',
    type: 'dweb',
    subtype: 'ipns'
  }
]

Note: If no record with the given key exists, an empty array will be returned.

dweb(account)

Get the first DWeb of a given account. If multiple DWebs are owned by an account, the result will be displayed in this order of priority: 'ipns', 'ipfs', 'skynet', 'resilio'.

Parameters

  • account: string

Return Value

Promise<BitAccountRecordExtended>

  • key: string,
  • value: string,
  • label: string,
  • ttl: string,
  • type: string,
  • subtype: string,

Example

// Get the first DWeb of "code.bit"
dotbit.dweb("code.bit").then(console.log)

// ...
// The printed result would be like:
{
  key: 'dweb.ipns',
  label: '',
  value: 'k51qzi5uqu5dgqjy1i78mz3oumplzt0cye32w9m8ix8hg9chpz5trvj8luwv0c',
  ttl: '300',
  type: 'dweb',
  subtype: 'ipns'
}

// Get the first DWeb of "west.bit"
dotbit.dweb("west.bit").then(console.log)

// ...
// The printed result would be null since no DWeb exists for "west.bit"
null

profiles(account, key)

Get all profiles of a given account.

Parameters

  • account: string
  • (Optional) key: string. Only profiles of matched subtype will be displayed.

Return Value

Promise<BitAccountRecordExtended[]>

  • key: string,
  • value: string,
  • label: string,
  • ttl: string,
  • type: string,
  • subtype: string,

Example

// Get all profiles of "west.bit"
dotbit.profiles("west.bit").then(console.log);

// ...
// The printed result would be like:
[
  {
    key: 'profile.discord',
    label: 'Discord Username',
    value: 'west.bit#8906',
    ttl: '300',
    type: 'profile',
    subtype: 'discord'
  },
  {
    key: 'profile.twitter',
    label: '',
    value: 'kylexiang',
    ttl: '300',
    type: 'profile',
    subtype: 'twitter'
  },
  ...
] 

// Get Discord profile of "west.bit"
dotbit.profiles("west.bit", "discord").then(console.log);

// ...
// The printed result would be like:
[
  {
    key: 'profile.discord',
    label: 'Discord Username',
    value: 'west.bit#8906',
    ttl: '300',
    type: 'profile',
    subtype: 'discord'
  }
] 

avatar(account)

Get the avatar of a given account.

Note: You have to install @dotbit/plugin-avatar before using this API. Otherwise an error will be thrown.

Parameters

  • account: string

For details, please refer to the documentation of @dotbit/plugin-avatar

verifyAddrsByAccount(address, mainAccount, subAccount, verifyType)

Verify whether the provided address has associated SubDIDs within the given main account, serving as a gateway for community member participation.

Parameters

  • address: string Address to be verified.
  • mainAccount: string Specified .bit account.
  • (Optional)subAccount: string Specified SubDID. subAccount is an optional field. If subAccount is empty, it will default to checking if the given address has at least one SubDID under any specified account.
  • (Optional)verifyType: number The verifyType is an optional field with a default value of 0, where 0 represents that the address is the owner address of the SubDID, and 1 represents that the address is the manager address of the SubDID.

Return Value

Promise<boolean>

Example

// Check if the owner address has the SubDID 'leon.leonx.bit' under the main account 'leonx.bit'.
dotbit.verifyAddrsByAccount(
  '0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
  'leonx.bit',
  'leon.leonx.bit',
)

// The printed result would be like:
true

// Check if the manager address has any SubDID under the main account 'leonx.bit'.
dotbit.verifyAddrsByAccount(
  '0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
  'leonx.bit',
  null,
  1
)

// The printed result would be like:
true

validDotbitAliasAddresses (account)

Query the valid alias address using the .bit alias.

Parameters

  • account: string. a .bit alias.

Return Value

Promise<BitKeyInfo[]>

Example

dotbit.validDotbitAliasAddresses('imac.bit').then(console.log)

// The printed result would be like:
[
  {
    "type":"blockchain",
    "key_info":{
      "coin_type":"60",
      "key":"0x15a33588908cf8edb27d1abe3852bf287abd3891"
    }
  }
]

batchAccountInfo (accounts)

Batch query of account information. Currently, only information about whether the account can be registered is returned. A maximum of 50 accounts can be queried at a time.

Parameters

  • accounts: string[]. List of accounts to be queried.

Return Value

Promise<BatchAccountInfo[]>

Example

dotbit.batchAccountInfo(['imac.bit', 'imac-1.bit']).then(console.log)

// The printed result would be like:
[
  {
    "account": "imac.bit",
    "can_register": false
  },
  {
    "account": "imac-1.bit",
    "can_register": true
  },
  ...
]