Core API
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:
Example return value for
getBalance:
where confirmed and unconfirmed represent the confirmed and unconfirmed balances respectively (unit: satoshi).
Example return value for
getAddressType:
BTC address types mainly include four types: Legacy (P2PKH), Nested SegWit (P2SH), Native SegWit (Bech32), and Taproot addresses.
Explanation of
inscribemethod parameters:
The
inscribemethod returns different transaction data formats depending on whether broadcasting is performed:
If
noBroadcastis set toyes, meaning no broadcasting, the return format is:
If
noBroadcastis set tono, 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:
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.
For the
createMetaidmethod of the connector object, if you pass in a completebodyparameter 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).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/revokeoperations.
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:
create Method: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.
Explanation of the
optionparameter corresponding to the MetaProtocols protocol:The
bodyparameter's specific fields should refer to the corresponding MetaProtocol protocol description for that entity (e.g., buzzEntity corresponds to the SimpleBuzz protocol).The
contentTypefield represents the data format, e.g.,text/plain,image/png.The
encryptionfield indicates the encryption type of the content:0for no encryption,1for ECIES encryption, and2for ECDH negotiated key encryption.The
versionfield specifies the protocol version number of MetaProtocols.The
encodingfield refers to the encoding format, which aligns with the globalBufferEncodingparameter in TypeScript:Examples for the
OptionParameter(1) For the
buzzentity, theoptionparameter should be:(2) For the
fileentity, assuming you are passing an image file, theoptionparameter 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