@wishknish/knishio-client-js
v0.7.8
Published
JavaScript implementation of the Knish.IO SDK to consume Knish.IO GraphQL APIs.
Downloads
438
Maintainers
Readme
Knish.IO Javascript Client SDK
This is the official Javascript / NodeJS implementation of the Knish.IO client SDK. Its purpose is to expose class libraries for building and signing Knish.IO Molecules, composing Atoms, generating Wallets, and much more.
Installation
The SDK can be installed via either of the following:
yarn add @wishknish/knishio-client-jsnpm install @wishknish/knishio-client-js --save
(Note: For installations in a Vite-based environment, you will need to install the https://www.npmjs.com/package/vite-plugin-node-polyfills plugin to ensure compatibility with Node.js libraries.)
Basic Usage
The purpose of the Knish.IO SDK is to expose various ledger functions to new or existing applications.
There are two ways to take advantage of these functions:
The easy way: use the
KnishIOClientwrapper classThe granular way: build
AtomandMoleculeinstances and broadcast GraphQL messages yourself
This document will explain both ways.
The Easy Way: KnishIOClient Wrapper
Include the wrapper class in your application code:
import { KnishIOClient } from '@wishknish/knishio-client-js'Instantiate the class with your node URI:
const client = new KnishIOClient({ uri: myNodeURI, cellSlug: myCellSlug, serverSdkVersion: 3, // Optional, defaults to 3 logging: false // Optional, enables logging });Request authorization token from the node:
await client.requestAuthToken({ seed: 'myTopSecretCode', encrypt: true // Optional, enables encryption });(Note: The
seedparameter can be a salted combination of username + password, a biometric hash, an existing user identifier from an external authentication process, for example)Begin using
clientto trigger commands described below...
KnishIOClient Methods
Query metadata for a Wallet Bundle. Omit the
bundleparameter to query your own Wallet Bundle:const result = await client.queryBundle({ bundle: 'c47e20f99df190e418f0cc5ddfa2791e9ccc4eb297cfa21bd317dc0f98313b1d', }); console.log(result); // Raw MetadataQuery metadata for a Meta Asset:
const result = await client.queryMeta({ metaType: 'Vehicle', metaId: null, // Meta ID key: 'LicensePlate', value: '1H17P', latest: true, // Limit meta values to latest per key throughAtom: true // Optional, query through Atom (default: true) }); console.log(result); // Raw MetadataWriting new metadata for a Meta Asset:
const result = await client.createMeta({ metaType: 'Pokemon', metaId: 'Charizard', meta: { type: 'fire', weaknesses: [ 'rock', 'water', 'electric' ], immunities: [ 'ground', ], hp: 78, attack: 84, }, policy: {} // Optional policy object }); if (result.success()) { // Do things! } console.log(result.data()); // Raw responseQuery Wallets associated with a Wallet Bundle:
const result = await client.queryWallets({ bundle: 'c47e20f99df190e418f0cc5ddfa2791e9ccc4eb297cfa21bd317dc0f98313b1d', token: 'FOO', // Optional, filter by token unspent: true // Optional, limit results to unspent wallets }); console.log(result); // Raw responseDeclaring new Wallets:
(Note: If Tokens are sent to undeclared Wallets, Shadow Wallets will be used (placeholder Wallets that can receive, but cannot send) to store tokens until they are claimed.)
const result = await client.createWallet({ token: 'FOO' // Token Slug for the wallet we are declaring }); if (result.success()) { // Do things! } console.log(result.data()); // Raw responseIssuing new Tokens:
const result = await client.createToken({ token: 'CRZY', // Token slug (ticker symbol) amount: '100000000', // Initial amount to issue meta: { name: 'CrazyCoin', // Public name for the token fungibility: 'fungible', // Fungibility style (fungible / nonfungible / stackable) supply: 'limited', // Supply style (limited / replenishable) decimals: '2' // Decimal places }, units: [], // Optional, for stackable tokens batchId: null // Optional, for stackable tokens }); if (result.success()) { // Do things! } console.log(result.data()); // Raw responseTransferring Tokens to other users:
const result = await client.transferToken({ bundleHash: '7bf38257401eb3b0f20cabf5e6cf3f14c76760386473b220d95fa1c38642b61d', // Recipient's bundle hash token: 'CRZY', // Token slug amount: '100', units: [], // Optional, for stackable tokens batchId: null // Optional, for stackable tokens }); if (result.success()) { // Do things! } console.log(result.data()); // Raw responseCreating a new Rule:
const result = await client.createRule({ metaType: 'MyMetaType', metaId: 'MyMetaId', rule: [ // Rule definition ], policy: {} // Optional policy object }); if (result.success()) { // Do things! } console.log(result.data()); // Raw responseQuerying Atoms:
const result = await client.queryAtom({ molecularHash: 'hash', bundleHash: 'bundle', isotope: 'V', tokenSlug: 'CRZY', latest: true, queryArgs: { limit: 15, offset: 1 } }); console.log(result.data()); // Raw responseWorking with Buffer Tokens:
// Deposit to buffer const depositResult = await client.depositBufferToken({ tokenSlug: 'CRZY', amount: 100, tradeRates: { 'OTHER_TOKEN': 0.5 } }); // Withdraw from buffer const withdrawResult = await client.withdrawBufferToken({ tokenSlug: 'CRZY', amount: 50 }); console.log(depositResult.data(), withdrawResult.data()); // Raw responsesGetting client fingerprint:
const fingerprint = await client.getFingerprint(); console.log(fingerprint); const fingerprintData = await client.getFingerprintData(); console.log(fingerprintData);
DataBraid: Embedding Status (Observability)
When the validator has DataBraid embeddings enabled (EMBEDDING_ENABLED=true), the SDK can query the embedding state of meta assets. This allows apps to render UI indicators such as spinner badges for in-progress embeddings or completion checkmarks.
The SDK automatically detects whether the connected server supports this feature. If it does not, queryEmbeddingStatus() returns null instead of throwing an error.
Query embedding status for a single Meta Asset:
const response = await client.queryEmbeddingStatus({ metaType: 'Vehicle', metaId: 'VIN-12345' }); if (response) { const items = response.payload(); // items[0] = { // metaType: 'Vehicle', // metaId: 'VIN-12345', // state: 'COMPLETE', // 'PENDING' | 'STALE' | 'COMPLETE' // totalMetas: 5, // Total meta rows for this instance // embeddedCount: 5, // Rows with embeddings // embeddedAt: 1713100800, // Unix timestamp of last embedding // model: 'nomic-embed-text-v1.5' // } } else { // Server does not support embedding status }Bulk embedding status for multiple assets in a single request:
const response = await client.queryEmbeddingStatus({ instances: [ { metaType: 'Vehicle', metaId: 'VIN-12345' }, { metaType: 'Vehicle', metaId: 'VIN-67890' }, { metaType: 'Profile', metaId: 'user_42' } ] }); if (response) { const items = response.payload(); // items.length === 3, one per input, in the same order for (const item of items) { console.log(`${item.metaType}:${item.metaId} → ${item.state}`); } }Capability detection — check if the server supports a query field before calling it:
const supported = await client.hasQueryField('embeddingStatus'); // true if the server's GraphQL schema includes the field, false otherwise // Result is cached per URI — no repeated network round-tripsEmbedding States: | State | Meaning | Suggested UI | |-------|---------|-------------| |
PENDING| No embeddings generated yet | Spinner / gray badge | |STALE| Embeddings exist but model has changed | Refresh indicator | |COMPLETE| All meta rows have current-model embeddings | Green checkmark |
Advanced Usage: Working with Molecules
For more granular control, you can work directly with Molecules:
Create a new Molecule:
const molecule = await client.createMolecule();Create a custom Mutation:
const mutation = await client.createMoleculeMutation({ mutationClass: MyCustomMutationClass });Sign and check a Molecule:
molecule.sign(); if (!molecule.check()) { // Handle error }Execute a custom Query or Mutation:
const result = await client.executeQuery(myQueryOrMutation, variables);
The Hard Way: DIY Everything
This method involves individually building Atoms and Molecules, triggering the signature and validation processes, and communicating the resulting signed Molecule mutation or Query to a Knish.IO node via your favorite GraphQL client.
Include the relevant classes in your application code:
import { Molecule, Wallet, Atom } from '@wishknish/knishio-client-js'Generate a 2048-symbol hexadecimal secret, either randomly, or via hashing login + password + salt, OAuth secret ID, biometric ID, or any other static value.
(optional) Initialize a signing wallet with:
const wallet = new Wallet({ secret: mySecret, token: tokenSlug, position: myCustomPosition, // (optional) instantiate specific wallet instance vs. random characters: myCharacterSet // (optional) override the character set used by the wallet })WARNING 1: If ContinuID is enabled on the node, you will need to use a specific wallet, and therefore will first need to query the node to retrieve the
positionfor that wallet.WARNING 2: The Knish.IO protocol mandates that all C and M transactions be signed with a
USERtoken wallet.Build your molecule with:
const molecule = new Molecule({ secret: mySecret, sourceWallet: mySourceWallet, // (optional) wallet for signing remainderWallet: myRemainderWallet, // (optional) wallet to receive remainder tokens cellSlug: myCellSlug, // (optional) used to point a transaction to a specific branch of the ledger version: 4 // (optional) specify the molecule version });Either use one of the shortcut methods provided by the
Moleculeclass (which will buildAtominstances for you), or createAtominstances yourself.DIY example:
// This example records a new Wallet on the ledger // Define metadata for our new wallet const newWalletMeta = { address: newWallet.address, token: newWallet.token, bundle: newWallet.bundle, position: newWallet.position, batchId: newWallet.batchId, } // Build the C isotope atom const walletCreationAtom = new Atom({ position: sourceWallet.position, walletAddress: sourceWallet.address, isotope: 'C', token: sourceWallet.token, metaType: 'wallet', metaId: newWallet.address, meta: newWalletMeta, index: molecule.generateIndex() }) // Add the atom to our molecule molecule.addAtom(walletCreationAtom) // Adding a ContinuID / remainder atom molecule.addContinuIdAtom();Molecule shortcut method example:
// This example commits metadata to some Meta Asset // Defining our metadata const metadata = { foo: 'Foo', bar: 'Bar' } molecule.initMeta({ meta: metadata, metaType: 'MyMetaType', metaId: 'MetaId123', policy: {} // Optional policy object });Sign the molecule with the stored user secret:
molecule.sign()Make sure everything checks out by verifying the molecule:
try { molecule.check(); // If we're validating a V isotope transaction, // add the source wallet as a parameter molecule.check(sourceWallet); } catch (error) { console.error('Molecule check failed:', error); // Handle the error }Broadcast the molecule to a Knish.IO node:
// Build our query object using the KnishIOClient wrapper const mutation = await client.createMoleculeMutation({ mutationClass: MutationProposeMolecule, molecule: molecule }); // Send the query to the node and get a response const response = await client.executeQuery(mutation);Inspect the response...
// For basic queries, we look at the data property: console.log(response.data()) // For mutations, check if the molecule was accepted by the ledger: console.log(response.success()) // We can also check the reason for rejection console.log(response.reason()) // Some queries may also produce a payload, with additional data: console.log(response.payload())Payloads are provided by responses to the following queries:
QueryBalanceandQueryContinuId-> returns aWalletinstanceQueryWalletList-> returns a list ofWalletinstancesMutationProposeMolecule,MutationRequestAuthorization,MutationCreateIdentifier,MutationLinkIdentifier,MutationClaimShadowWallet,MutationCreateToken,MutationRequestTokens, andMutationTransferTokens-> returns molecule metadataQueryEmbeddingStatus-> returns an array of{ metaType, metaId, state, totalMetas, embeddedCount, embeddedAt, model }objects
Getting Help
Knish.IO is under active development, and our team is ready to assist with integration questions. The best way to seek help is to stop by our Telegram Support Channel. You can also send us a contact request via our website.
