Core API

MetaID-TS-SDK 源代码已发布在 Github 上,相关 API 和示例代码请以 Github 最新页面为准

https://github.com/metaid-developers/metaid

Overview

The MetaID SDK currently targets the BTC chain, providing corresponding API methods from three dimensions: Wallet Layer, Connector Layer, and Entity Layer. Below is a detailed introduction to the relevant API methods.

Each layer's responsibilities and relationships are outlined.

Wallet Layer API

import { MetaletWalletForBtc } from '@metaid/metaid';
// Create a wallet object based on the currently logged-in wallet account
const _wallet = await MetaletWalletForBtc.create();

// Access the wallet object's public properties
const address = _wallet.address // Get address
const pubicKey = _wallet.pub // Get public key

// Access a series of methods provided by the wallet object (provided the wallet is connected, otherwise returns {status: 'not-connected' })
await _wallet.getAddress() // Get wallet address

await _wallet.getAddressType() // Get wallet address type

await _wallet.getPublicKey(path) // Get public key based on path

await _wallet.getBalance() // Get balance

await _wallet.signMessage(message) // Send signed message

await _wallet.signPsbt({
  psbtHex,
  options,
}: {
  psbtHex: string
  options?: { toSignInputs?: ToSignInput[]; autoFinalized: boolean }
}) // Sign the input psbtHex

// This is a low-level inscription API method; unless you have very customized inscription needs, it is not recommended to call it directly. 
// The connector layer has abstracted and encapsulated this method, along with related parameter descriptions.
await _wallet.inscribe({data, options} : \
  { data: InscriptionRequest, options: {noBroadcast : boolean }) 

Notes on Wallet Method Parameters and Return Types:

  1. Example return value for getBalance:

where confirmed and unconfirmed represent the confirmed and unconfirmed balances respectively (unit: satoshi).

  1. Example return value for getAddressType:

BTC address types mainly include four types: Legacy (P2PKH), Nested SegWit (P2SH), Native SegWit (Bech32), and Taproot addresses.

  1. Explanation of inscribe method parameters:

  1. The inscribe method returns different transaction data formats depending on whether broadcasting is performed:

  • If noBroadcast is set to yes, meaning no broadcasting, the return format is:

  • If noBroadcast is set to no, meaning broadcasting is performed, the return format is:

Where, if not broadcasting, the transaction result is returned in txHex format; otherwise, the transaction result is returned as txid. The sum of commitCost and revealCost represents the estimated fee required for the current inscription transaction.

Connector Layer API

Notes on Connector Layer API:

  1. From the perspective of the MetaID Specification, issuing or modifying on-chain data involves sending a PIN, and the inscription interface handles this task. Whether it's creating a MetaID, updating user information, or calling the create method after generating an Entity object, it essentially sends a PIN to put the data on-chain.

  2. For the createMetaid method of the connector object, if you pass in a complete body parameter including name, bio, and avatar, the SDK will sequentially perform the following actions: first initialize at the root path / (operation=init), then create corresponding information under the paths /info/name, /info/bio, and /info/avatar (operation=create).

  3. Regarding data timeliness, when modifying on-chain data (operation=modify/revoke), there are relevant matters to note. Please refer to the second convention in the MetaID Specification regarding modify/revoke operations.

Entity Layer API

Once you create an entity through the connector, you can access a series of properties and methods provided by that entity.

Currently, the MetaID SDK provides basic entity calls based on the on-chain microblog example application, including buzzEntity, fileEntity, and likeEntity. If developers have their own customization needs, they can create their data protocol on the MetaProtocols website, and the MetaID SDK will automatically create the corresponding entity for that protocol.

Notes on the Entity's create Method:

  1. According to the MetaID Specification, you can think of the create method as creating new files under a certain folder (file path), essentially calling the connector's inscribe method. The results vary depending on the broadcast parameter, and the specific data format can be found in the connector's inscribe method description.

  2. Explanation of the option parameter corresponding to the MetaProtocols protocol:

    • The body parameter's specific fields should refer to the corresponding MetaProtocol protocol description for that entity (e.g., buzzEntity corresponds to the SimpleBuzz protocol).

    • The contentType field represents the data format, e.g., text/plain, image/png.

    • The encryption field indicates the encryption type of the content: 0 for no encryption, 1 for ECIES encryption, and 2 for ECDH negotiated key encryption.

    • The version field specifies the protocol version number of MetaProtocols.

    • The encoding field refers to the encoding format, which aligns with the global BufferEncoding parameter in TypeScript:

      Examples for the Option Parameter

      (1) For the buzz entity, the option parameter should be:

      (2) For the file entity, assuming you are passing an image file, the option parameter should be:

3. Instructions for Batch Creation (Inscription): You may have noticed that the CreateOptions parameter is passed in as an array. This means you can create multiple pieces of data for a particular type of entity simultaneously. Here's a specific example: when you post a buzz with multiple image attachments, you only need to call the inscription interface twice. First, batch inscribe multiple images using the fileEntity. The generated transaction hash array is then passed into the attachments field of the buzzEntity create method for the second inscription.

Last updated