Futureverse Asset Register Core SDK
Installation
NPM:
npm install @futureverse/asset-registry --save
Yarn:
yarn add @futureverse/asset-registry
Usage
Create Schema
import { Wallet } from 'ethers'
import { AssetRegistry } from '@futureverse/asset-registry'
import { Namespace } from '@futureverse/asset-registry/types'
const domain = '<domain for the authentication>'
const origin = '<origin for the authentication>'
const chainId = 5
// get the wallet instant from futurepass
const wallet = Wallet.createRandom()
const walletAddress = wallet.address
const assetRegistrySdk = new AssetRegistry({
url: 'https://ar.futureverse.app/graphql',
auth: {
sign: async (message) => {
return await wallet.signMessage(message)
},
storage: {
set: (key: string, value: string) => localStorage.setItem(key, value),
get: (key: string) => localStorage.getItem(key),
},
domain,
origin,
chainId,
walletAddress,
},
})
// CreateSchema
assetRegistrySdk.createSchema({
namespace: 'fv.test' as Namespace,
schema: '',
version: 0.1,
})
Asset Linking
import {
AssetRegistry,
AssetTransactionMessage,
ChainAddress,
Signature,
TransactionHash,
} from '@futureverse/asset-registry'
import { ARTM, STATEMENTS } from '@futureverse/artm'
import { Wallet } from 'ethers'
const GQL_API_URL = 'http://localhost:8080/graphql'
const domain = '<domain for the authentication>'
const origin = '<origin for the authentication>'
const chainId = 5
// get the wallet instant from futurepass
const wallet = Wallet.createRandom()
const walletAddress = wallet.address
const boxerDid =
'did:fv-asset:5:evm:0x5085cc0236ae108812571eadf24beee4fe8e0c50:4132'
const asmGenTwoBrainDid =
'did:fv-asset:5:evm:0x51cea47135c93eee33a6c15c22ea3532aec0540a:375'
let nonce = 0
// Define the asset register instance
const assetRegistrySdk = new AssetRegistry({
url: GQL_API_URL,
auth: {
sign: async (message) => {
return await wallet.signMessage(message)
},
storage: {
set: (key: string, value: string) => localStorage.setItem(key, value),
get: (key: string) => localStorage.getItem(key),
},
domain,
origin,
chainId,
walletAddress: walletAddress,
},
})
//STEP 1: get nonce
nonce = await assetRegistrySdk.nonceForChainAddress(
walletAddress as ChainAddress,
)
//STEP 2: Create a message
const artmMessage = new ARTM({
address: walletAddress,
statement: STATEMENTS.OWNERSHIP,
nonce: nonce,
})
const operation = 'create'
artmMessage.operations = [
{
type: 'asset-link',
action: operation,
args: ['equipWith_asmBrain', boxerDid, asmGenTwoBrainDid],
},
]
const signature = await wallet.signMessage(artmMessage.getMessageToSign())
const input = {
signature: signature as Signature,
transaction: artmMessage.message as AssetTransactionMessage,
}
// STEP 3: Send the message to the server
const response = await assetRegistrySdk.submitTransaction(input)
// STEP 4: Check the transaction status
const transactionStatus = await assetRegistrySdk.transaction({
transactionHash: response.transactionHash as TransactionHash,
})
Create off-chain Asset
import { AssetRegistry } from '@futureverse/asset-registry'
import { Wallet } from 'ethers'
import { v4 as uuidv4 } from 'uuid'
const GQL_API_URL = 'http://localhost:8080/graphql'
const domain = '<domain for the authentication>'
const origin = '<origin for the authentication>'
const chainId = 5
// get the wallet instant from futurepass
const wallet = Wallet.createRandom()
const walletAddress = wallet.address
const CREATOR_ID = '72fe2ff1-5f69-40e3-90b3-5335c7126552'
/*
Define the asset registry instance
Only url is required. Other params are auth related which are not required if you don't call methods need authentication.
You can set them afterward by assetRegistrySdk.setAuthorizationParameters().
*/
const assetRegistrySdk = new AssetRegistry({
url: GQL_API_URL,
auth: {
sign: async (message) => {
return await wallet.signMessage(message)
},
storage: {
set: (key: string, value: string) => localStorage.setItem(key, value),
get: (key: string) => localStorage.getItem(key),
},
domain,
origin,
chainId,
walletAddress: walletAddress,
},
})
// Register an off chain sft when collection is created without a token id
const creatorCollectionId = uuidv4()
const sftInput = {
creatorCollectionId: creatorCollectionId,
creatorId: CREATOR_ID,
}
const response = await assetRegistrySdk.registerOffChainAsset(sftInput)
// register an off chain nft when collection is created with a token id
const tokenId = uuidv4()
const creatorCollectionId2 = uuidv4()
const nftInput = {
creatorCollectionId: creatorCollectionId2,
creatorId: CREATOR_ID,
tokenId: tokenId,
}
const response2 = await assetRegistrySdk.registerOffChainAsset(nftInput)
Get Asset Tree
import { AssetRegistry } from '@futureverse/asset-registry'
import {
CollectionId,
} from '@futureverse/asset-registry/types'
import { Wallet } from 'ethers'
const GQL_API_URL = 'http://localhost:8080/graphql'
const domain = '<domain for the authentication>'
const origin = '<origin for the authentication>'
const chainId = 5
// get the wallet instant from futurepass
const wallet = Wallet.createRandom()
const walletAddress = wallet.address
const boxerDid =
'did:fv-asset:5:evm:0x5085cc0236ae108812571eadf24beee4fe8e0c50:4132'
const [_did, _fvAsset, chainId, chainType, contractAddress, tokenId] = boxerDid.split(':')
// Define the asset register instance
const assetRegistrySdk = new AssetRegistry({
url: GQL_API_URL,
auth: {
sign: async (message) => {
return await wallet.signMessage(message)
},
storage: {
set: (key: string, value: string) => localStorage.setItem(key, value),
get: (key: string) => localStorage.getItem(key),
},
domain,
origin,
chainId,
walletAddress: walletAddress,
}
})
const collectionId = `${chainId}:${chainType}:${contractAddress}` as CollectionId
const assetTree = await ar.getAssetTree({
tokenId
collectionId
})
// List the paths to the relative assetTree,
// The paths should looks like below
// const paths = [
// 'http://schema.futureverse.com#equippedWith_accessoryClothing',
// 'http://schema.futureverse.com#equippedWith_accessoryEyewear',
// 'http://schema.futureverse.com#equippedWith_accessoryHead',
// 'http://schema.futureverse.com#equippedWith_accessoryMouth',
// 'http://schema.futureverse.com#equippedWith_accessoryNose',
// 'http://schema.futureverse.com/fvp#sft_link_owner_0x225b5333c2d8ec41f40d6463d44141786c2c4463',
// ];
const paths = assetTree?.paths
// To fetch the relative assetTee, just use the `getPath` method.
const clothingAssetTree = assetTree.getPath('http://schema.futureverse.com#equippedWith_accessoryClothing')