0.9.10 • Published 2 months ago

@contextprotocol/storage-sdk v0.9.10

Weekly downloads
-
License
-
Repository
-
Last release
2 months ago

context-sdk

Connect to Testnet Arabasta

First Create a .env file (copy from .env.example)

# Context
RPC_PROVIDER=Alchemy RPC URL
DOMAIN_NAME=Domain used as a default domain to interact with Context.
DOMAIN_PRIVKEY=Owner of the default DOMAIN.

# Biconomy
BICONOMY_BUNDLER=URL to interact wuth the Bundlr SDK.
BICONOMY_PAYMASTER_APIKEY=API KEY of the Paymaster to sponsot all transactions to the 

Registry.
# Storage
IRYS_PRIVKEY=WALLET to interact with the IRYS Storage, needs to have matic deposited.

# Storage - Binance Greenfield
BNB_ADDRESS=WALLET to interact with the Binance Greenfield Storage.
BNB_PRIVKEY=Pivate key of the Binance Greenfield Wallet.
BNB_BUCKETID=Binance Greenfield Bucket ID.
BNB_TEMP_PATH=/tmp

Now you are ready to connect.

import { Context, Connection } from '@contextprotocol/sdk';

const context = new Context(Connection.ARABASTA);

Read a domain

const result = await context.domain('ctx_api');
if ('error' in result) throw new Error(result.error);
const domain = result.domain;
console.log(`Domain exists: ${domain.exists}`);
console.log(`Domain Name: ${domain.domainName}`);
console.log(`Domain Hash: : ${domain.domainHash}`);
console.log(`Tokens deposited: : ${domain.tokens}`);
console.log(`Owner: ${domain.owner}`);
console.log(`Editor: ${domain.editor}`);

Create a new Smart Account with Biconomy

const wallet = await context.getSmartAcccount();
if ('error' in wallet) throw new Error(wallet.error);
console.log(`
    Private KEY: ${wallet.privateKey}
    \nAccount Address: ${wallet.address}
    \nDomain Name: ${domainName}`
);

Write a domain

const resultDomain = await context.domain('new_domain');
if ('error' in resultDomain) throw new Error(resultDomain.error);
if (resultDomain.domain.exists) throw new Error('domain already exists');

const domain = resultDomain.domain;
const owner = '0x...'; // The Address that will own this domain
const resultSave = await result.domain.save(owner);
if ('error' in resultSave) throw new Error(resultSave.error);

Write a Document (1st time)

const resultDocument = await context.document('new_domain/doc_1');
if ('error' in resultDocument) throw new Error(resultDocument.error);
let doc = resultDocument.document;

// Write and save the document.
await doc.setAccount(wallet.account);
await doc.write({
    name: 'New Document',
    test: true,
    friends: []
});

const resultDoc = await doc.save();
if ('error' in resultDoc) throw new Error(resultDoc.error);

Update the Document

const resultDocument = await context.document('new_domain/doc_1');
if ('error' in resultDocument) throw new Error(resultDocument.error);
let doc = resultDocument.document;

// Write and save the document.
await doc.setAccount(wallet.account);

// Install schema.
await doc.install('core/organization');

// update field in the document
await doc.update('name', 'New Document Updated');

// Delete a field in the document
await doc.delete('test');

// Add an item to an array field
await doc.addItem('friends', 'Mike');

// Remove an item from an array field
await doc.removeItem('friends', 'Mike');

// Upload an asset called "image" with the name "Personal Avatar"
await doc.upload('image', './test/image.png', 'Personal Avatar', 'image/png');

const resultDoc = await doc.save();
if ('error' in resultDoc) throw new Error(resultDoc.error);