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 accountconst_wallet=awaitMetaletWalletForBtc.create();// Access the wallet object's public propertiesconstaddress=_wallet.address // Get addressconstpubicKey=_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 addressawait_wallet.getAddressType() // Get wallet address typeawait_wallet.getPublicKey(path) // Get public key based on pathawait_wallet.getBalance() // Get balanceawait_wallet.signMessage(message) // Send signed messageawait_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:
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
import { btcConnect } from'@metaid/metaid';// Create a new connector based on the wallet objectconst _btcConnector: BtcConnector = await btcConnect({ wallet, network }: { wallet?: MetaIDWalletForBtc; network: BtcNetwork });
// If the wallet object is not empty, you can use the following method to check if the wallet has created a MetaID_btcConnector.hasMetaid()// The connector provides a series of methods for operating MetaID related datatypeBtcNetwork="testnet"|"regtest"|"livenet"// Inscription methodtypeOperation='init'|'create'|'modify'typeInscribeOptions= { operation:Operation body?:string|Buffer path?:string contentType?:string encryption?:'0'|'1'|'2' version?:string encoding?:BufferEncoding}await_btcConnector.inscribe(inscribeOptions:InscribeOptions[], noBroadcast:'yes'|'no')// The return type of this method is the same as the wallet inscribe method.// Create MetaID, the parameter avatar is processed into Buffer in chunks from the native File type in JS, and then converted to a base64 string
constmetaIdRes=await_btcConnector.createMetaid(body?: { network?: BtcNetwork, name?: string; avatar?: string })// Return type: metaIdRes: { metaid:string }// Get user information associated with MetaIDconstuser=await_btcConnector.getUser({ network, currentAddress }: { network: BtcNetwork; currentAddress?: string })// Update user information associated with MetaIDconstisUpdateSuccess=await_btcConnector.updateUserInfo(body: { name?: string; bio?: string; avatar?: string })// Get MetaIDconstcurrentMetaId=await_btcConnector.getMetaid()// Check the current connector status (whether the wallet is connected)constisConnected=await_btcConnector.isConnected()// Disconnect the current walletawait_btcConnector.disconnect()// Create an Entity object, this method bridges the Connector layer and the Entity layerawait_btcConnector.use(entitySymbol: string)
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 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).
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.
// Example of creating a buzzEntitytypeBtcNetwork="testnet"|"regtest"|"livenet"// Create a buzzEntity entity through the connectorconstbuzzEntity=await_btcConnector.use('buzz')// Get all buzzes sent by the currently connected account in a paginated mannerconstallBuzz=awaitbuzzEntity.list({ page, limit, network }: { page: number; limit: number; network?: BtcNetwork })// Get the details of a specific Pin by its pinIdconstpid='XXXXXXXXX'constpinDetail=awaitbuzzEntity.one({ pid, network }: { pid: string; network: BtcNetwork })// Count the total number of Pins sent under the current entity (buzzEntity)constpinTotal=awaitbuzzEntity.total({ network }: { network?: BtcNetwork })// Create a buzztypeCreateOptions= { body?:string|Buffer; contentType?:string; encryption?:"0"|"1"|"2"; version?:string; encoding?:BufferEncoding;}// The return type of this method is the same as the wallet inscribe method.constcreateRes=awaitbuzzEntity.create({ options, noBroadcast,}: { options: CreateOptions[] noBroadcast: 'yes'|'no'})
Notes on the Entity's 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 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:
(1) For the buzz entity, the option parameter should be:
{ body:"buzz content"}
(2) For the file entity, assuming you are passing an image file, the option parameter should be:
{ body:Buffer.from('image raw hex string',"hex").toString("base64"), contentType:"image/jpeg", encoding:"base64"}
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.