0.1.7 β€’ Published 1 year ago

@origyn/mintjs v0.1.7

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

ORIGYN Mint.js

Overview

Mint.js is the official JavaScript library for interacting with any Internet Computer canister running the Origyn NFT standard: https://github.com/origyn-sa/origyn_nft.

Compatibility

Each release of Mint.js is compatible with a specific version of the Origyn NFT standard. The table below shows the compatibility between the two.

Mint.js VersionOrigyn NFT Version
0.1.0 - 0.1.60.1.4
1.0.0-alpha.140.1.4
1.0.0-alpha.130.1.3

Installation

npm i @origyn/mintjs

Build

npm ci
npm run build

Run Unit Test

Requires Node version 18 or higher.

npm run test

Local testing of unpublished mint.js

If you have a local repository of the mint.js code, you can make npm point to its build using:

npm i <path-to-mint-js>

Usage

Start by importing the mintjs in your code.

import { getNftBalance } from '@origyn/mintjs';

Now, we can call the getNftBalance method anywhere in the code.

For instance, let's try to get the balance of NFTs in our wallet by passing our principal as a string. The principal argument can be both a string or a Principal type from @dfinity/principal.

const response = getNftBalance(principal.toText());

if (response.ok) console.log(response.ok.nfts);
else console.log(`There was an error while getting the balance: ${response.err}`);

❗ Using Mint.js in a Web Context

Mint.js servers both Node and Web contexts. Using this in a front-end application will require a few polyfills in order to specify browserified versions of a few packages.

Example for a webpack configuration:

webpack.config.js

module.exports = (env, argv) => ({
  resolve: {
    fallback: {
      fs: false,
      path: false,
      crypto: require.resolve('crypto-browserify'),
      stream: require.resolve('stream-browserify'),
    },
  },
});

Documentation

πŸ”— Initialization

The mint.js library is using a singleton in order to provide global configuration and access to canister configuration.

OrigynClient.getInstance().init(isProd, canisterId, auth) β‡’ void

ParamTypeDefaultDescription
isProdbooleantrueSet to false if we want to interact with a local replica canister (will use http://localhost:8080 when creating the agent, if only identity was provided for auth)
canisterIdstringORIGYN_CANISTERId of the canister to communicate with.
authAuthType{}Optional parameter providing either an actor, an identity or an agent. If no parameter is provided, anonymous identity will be created.

Example

Authenticate using the seed phrase:

const auth = {
  key: {
    seed: 'SEED PHRASE OF THE WALLET/CONTROLLER',
  },
};

const IS_PROD = true;
const CANISTER_ID = 'frfol-iqaaa-aaaaj-acogq-cai';
await OrigynClient.getInstance().init(IS_PROD, CANISTER_ID, auth);

Authenticate using the .pem file, it can be either ed25519 or sec256k1 private key:

const auth = {
  key: {
    identityFile: './path/to/identity.pem',
  },
};

const IS_PROD = true;
const CANISTER_ID = 'frfol-iqaaa-aaaaj-acogq-cai';
await OrigynClient.getInstance().init(IS_PROD, CANISTER_ID, auth);

🌐 Check the web example where we handle the upload of the .pem file. /examples/ReactStageComponent.tsx#191

The auth object can have multiple ways to create an actor for the canister.

AuthType

type AuthType = {
  actor?: ActorSubclass<any>,
  identity?: Identity,
  agent?: HttpAgent,
  key?: PrivateIdentityKey,
};

PrivateIdentityKey

type PrivateIdentityKey = {
  identityFile?: Buffer | string,
  seed?: string,
};

set OrigynClient.getInstance().principal(principal) β‡’ void

We can dynamically change the principal which calls the methods within OrigynClient by changing the principal.

ParamTypeDefaultDescription
principalstring or PrincipalThe id of the principal or the object (as Principal).

get OrigynClient.getInstance().principal⇒ Principal

Returns: Principal - The current principal within the OrigynCleint

🎬 Staging & Minting

We can use mint.js in order to stage NFT collections to the canister we initialized OrigynClient for. In order to do this, the Identity that was used to initialize OrigynClient needs to be the controller of the canister.

stageCollection(stageConfigArgs: StageConfigArgs) β‡’ Promise<OrigynResponse<string[], GetNftErrors>>

StageConfigArgs Type

NameTypeDescription
environment'local' or 'prod'Whether the environment we want to stage to is local or production.
nftCanisterIdstringId of the canister, should be the same as the initalized canister
collectionIdstringWhat id we want the collection to have
collectionDisplayNamestringCollection name
tokenPrefixstringPrefix for NFTs in the collection
creatorPrincipalstringThe creator of the collection
soulboundbooleanWheter the NFTs are soulbound or not
collectionFilesCollectionLevelFile[]The files we want to upload at the collection level
nftsStageNft[]The NFTs we want to append to the collection

StageNft

type StageNft = {
  collectionFileReferences?: string[],
  files: StageFile[],
  quantity?: number,
};

CollectionLevelFile

type CollectionLevelFile = StageFile & {
  category: 'collection' | 'stage' | 'dapp',
};

StageFile

export type StageFile = {
  assetType?: AssetType,
  filename: string,
  index?: number,
  path: string,
  rawFile?: Buffer,
  size?: number,
  type?: string,
  title?: string,
  libraryId?: string,
  contentType?: string,
  webUrl?: string,
  immutable?: boolean,
};

Example

For examples, please check the test file of stage method (for node context) or /examples folder from the repository for web context.

stageNfts(stageNftsArgs: StageNftsArgs) β‡’ Promise<OrigynResponse<string[], GetNftErrors>>

Stage a single NFT or multiple NFTs contained in the nfts array after the collection was already created.

StageNftsArgs Type

NameTypeDescription
soulboundbooleanWheter the NFTs are soulbound or not
useProxybooleanWheter to use the proxy or not
nftsStageNft[]The NFTs we want to append to the collection

Example

await OrigynClient.getInstance().init(false, 'rrkah-fqaaa-aaaaa-aaaaq-cai', { key: { seed: WALLET_SEED } });
const examplePayload = {
  isProxy: true,
  soulbound: false,
  nfts: [
    {
      quantity: 1,
      files: [
        {
          filename: 'file_name.jpg',
          index: 0,
          path: '/home/user/Desktop/file_name.jpg.jpg',
        },
      ],
    },
  ],
};
const stageAssetResult = await stageNfts(examplePayload);

raw stageNftUsingMetadata(metadata: any) β‡’ Promise<OrigynResponse<string[], GetNftErrors>>

Stage a single NFT by passing raw metadata as argument. This is being used when staging an NFT which does not follow the standard.

Example

await OrigynClient.getInstance().init(false, 'rrkah-fqaaa-aaaaa-aaaaq-cai', { key: { seed: WALLET_SEED } });
const payload = {
  metadata: {
    Class: [
      { name: 'id', value: { Text: 'my-nft-1' }, immutable: true },
      {
        name: 'owner',
        value: { Principal: 'rrkah-fqaaa-aaaaa-aaaaq-cai' },
        immutable: false,
      },
      {
        name: 'is_soulbound',
        value: { Bool: false },
        immutable: false,
      },
    ],
  },
};
const stagingResult = await stageNftUsingMetadata(payload);

stageNewLibraryAsset(files: StageFile[], useProxy: boolean = false, tokenId?: string) β‡’ Promise<OrigynResponse<string[], GetNftErrors>>

Deprecated

Stages a new library asset for an already staged NFT. Use this when the library is not already in the NFT Metadata. This wil also create the library meta and inject it into the NFT metadata.

Example

await OrigynClient.getInstance().init(false, 'rrkah-fqaaa-aaaaa-aaaaq-cai', { key: { seed: WALLET_SEED } });
const payload = {
  tokenId: 'token-0', // This is the token id of the NFT we want to append library to
  files: [
    {
      filename: 'file_name.jpg',
      index: 0,
      path: '/home/user/Desktop/file_name.jpg.jpg',
    },
  ],
};
const stage_asset = await stageLibraryAsset(payload.files, payload.tokenId);

Note: The library field of the NFT Metadata needs to be true in order to be able to stage library assets after minting.

stageLibraryAsset(files: StageFile[], tokenId?: string) β‡’ Promise<OrigynResponse<string[], GetNftErrors>>

Stages a library asset for an already staged NFT. Use this for new libraries and existing libraries (the library is already in the NFT Metadata).

Example

await OrigynClient.getInstance().init(false, 'rrkah-fqaaa-aaaaa-aaaaq-cai', { key: { seed: WALLET_SEED } });
const payload = {
  tokenId: 'token-0', // This is the token id of the NFT we want to append library to
  files: [
    {
      filename: 'file_name.jpg',
      index: 0,
      path: '/home/user/Desktop/file_name.jpg.jpg',
    },
  ],
};
const stage_asset = await stageLibraryAsset(payload.files, payload.tokenId);

Library metadata:

{
  "name": "library",
  "value": {
    "Array": [
      {
        "Class": [
          {
            "name": "library_id",
            "value": {
              "Text": "file_name.jpg"
            },
            "immutable": true
          },
          {
            "name": "title",
            "value": {
              "Text": "File Name Image"
            },
            "immutable": true
          },
          .
          .
          .
        ]
      }
    ]
  },
}

stageCollectionLibraryAsset(tokenId?: string, files: StageFile[]) β‡’ Promise<OrigynResponse<any, StageLibraryAssetErrors | GetCollectionErrors>>

Stages a library reference to an existing collection-level library asset for an already staged NFT.

Example

await OrigynClient.getInstance().init(false, 'rrkah-fqaaa-aaaaa-aaaaq-cai', { key: { seed: WALLET_SEED } });
const payload = {
  tokenId: 'token-1', // This is the token id of the NFT we want to append library to
  files: [
    {
      libraryId: 'shared_file.png', // The id of an already staged file at the collection level
      title: 'A reference to my collection-level shared file',
      immutable: true,
    },
  ],
};
const stage_asset = await stageCollectionLibraryAsset(payload.tokenId, payload.files);

Library metadata:

{
  "name": "library",
  "value": {
    "Array": [
      {
        "Class": [
          {
            "name": "library_id",
            "value": {
              "Text": "shared_file.png"
            },
            "immutable": false
          },
          {
            "name": "title",
            "value": {
              "Text": "A reference to my collection-level shared file"
            },
            "immutable": false
          },
          {
            "name": "location_type",
            "value": {
              "Text": "collection"
            },
            "immutable": false
          },
          {
            "name": "location",
            "value": {
              "Text": "collection/-/shared_file.png"
            },
            "immutable": false
          },
          {
            "name": "content_type",
            "value": {
              "Text": "image/png"
            },
            "immutable": false
          },
          {
            "name": "content_hash",
            "value": {
              "Text": "4b56b5f3526ccf851a7130f1b83d6412bc33644e79b84810a5c23c00ca75ff9d"
            },
            "immutable": false
          },
          {
            "name": "size",
            "value": {
              "Nat": "0"
            },
            "immutable": false
          },
          {
            "name": "sort",
            "value": {
              "Nat": "4"
            },
            "immutable": false
          },
          {
            "name": "read",
            "value": {
              "Text": "public"
            },
            "immutable": false
          },
          {
            "name": "com.origyn.immutable_library",
            "value": {
              "Bool": true
            },
            "immutable": true
          }
        ]
      }
    ]
  },
  "immutable": false
}

stageWebLibraryAsset(tokenId?: string, files: StageFile[]) β‡’ Promise<OrigynResponse<any, StageLibraryAssetErrors | GetCollectionErrors>>

Stages a library reference to an existing collection-level library asset for an already staged NFT.

Example

await OrigynClient.getInstance().init(false, 'rrkah-fqaaa-aaaaa-aaaaq-cai', { key: { seed: WALLET_SEED } });
const payload = {
  tokenId: 'token-2', // This is the token id of the NFT we want to append library to
  files: [
    {
      libraryId: 'origyn-dev-talk-1',
      webUrl: 'https://www.youtube.com/watch?v=JlLdwoCDUL8', // A URL to any web resource
      title: 'Origyn Developer Talk #1',
      immutable: true,
    },
  ],
};
const stage_asset = await stageWebLibraryAsset(payload.tokenId, payload.files);

Library metadata:

{
  "name": "library",
  "value": {
    "Array": [
      {
        "Class": [
          {
            "name": "library_id",
            "value": {
              "Text": "origyn-dev-talk-1"
            },
            "immutable": false
          },
          {
            "name": "title",
            "value": {
              "Text": "Origyn Developer Talk #1"
            },
            "immutable": false
          },
          {
            "name": "location_type",
            "value": {
              "Text": "web"
            },
            "immutable": false
          },
          {
            "name": "location",
            "value": {
              "Text": "https://www.youtube.com/watch?v=JlLdwoCDUL8"
            },
            "immutable": false
          },
          {
            "name": "content_type",
            "value": {
              "Text": "text/html"
            },
            "immutable": false
          },
          {
            "name": "size",
            "value": {
              "Nat": "0"
            },
            "immutable": false
          },
          {
            "name": "sort",
            "value": {
              "Nat": "5"
            },
            "immutable": false
          },
          {
            "name": "read",
            "value": {
              "Text": "public"
            },
            "immutable": false
          },
          {
            "name": "com.origyn.immutable_library",
            "value": {
              "Bool": true
            },
            "immutable": true
          }
        ]
      }
    ]
  },
  "immutable": false
}

deleteLibraryAsset(tokenId?: string, libraryId: string) β‡’ Promise<OrigynResponse<any, StageLibraryAssetErrors>>

Deletes an existing library asset from an already staged NFT.

Example

await OrigynClient.getInstance().init(false, 'rrkah-fqaaa-aaaaa-aaaaq-cai', { key: { seed: WALLET_SEED } });
const response = await deleteLibraryAsset('token-2', 'origyn-dev-talk-1');

mintNft(tokenId: string, principal: Principal) β‡’ Promise<OrigynResponse<any, GetNftErrors>>

Will mint the NFT provided as argument tokenId to the principal.

Example

const auth = {
  key: {
    seed: 'SEED PHRASE OF THE WALLET/CONTROLLER',
  },
};

const IS_PROD = true;
const CANISTER_ID = 'frfol-iqaaa-aaaaj-acogq-cai';
await OrigynClient.getInstance().init(IS_PROD, CANISTER_ID, auth);

const mintResponse = await mintNft(config);

if(mintResponse.ok)
  // minting was succesfull
else
  throw Error(mintResponse.err.text)

🦾 Communication Functions

getNftBalance(principal) β‡’ Promise<OrigynResponse<BalanceOfNftOrigyn, GetBalanceErrors>>

Returns the NFTs balance of the provided principal. If no principal is provided, it will use the principal in the OrigynClient. If there is no principal at all, this method will throw a NO_PRINCIPAL_PROVIDED error.

Returns: Promise<OrigynResponse<BalanceOfNftOrigyn, GetBalanceErrors>> - JSON Response containing the response of the method.

ParamTypeDefaultDescription
principalstring or PrincipalOrigynClient.getInstance().principalPrincipal to request the balance for.

BalanceOfNftOrigyn Type

NameTypeDescription
nftsstring[]List with every owned NFT by the requested principal.
offersEscrowRecord[]Contains a list with received escrows.
salesEscrowRecord[]List with active sales started.
stakeStakeRecord[]Array with current stakes.
multi_canisterPrincipal[]
escrowsEscrowRecord[]Sent escrows by that principal.

GetBalanceErrors Enum

NameDescription
UNKNOWN_ERRORThe canister returned an error, or there was an error while connecting.
CANT_REACH_CANISTERThe canister is unavailable. It might happen when there is a 503 error.
NO_PRINCIPAL_PROVIDEDThere was not principal provided.

Usage example

const principalFromId = Principal.fromText('YOUR PRINCIPAL ID');
const response = await getNftBalance(principalFromId);

or

/* This will use the principal provided within OrigynClient */

const principalFromId = Principal.fromText('YOUR PRINCIPAL ID');
OrigynClient.getInstance().principal = principalFromId;
const response = await getNftBalance();

if (response.ok)
  // response.ok.nfts contains the list of nfts
else if (response.err)
  // something wrong happend

getNft(tokenId) β‡’ Promise<OrigynResponse<NftInfoStable, GetNftErrors>>

Returns all data for a nft, which is provided using the tokenId parameter.

NftInfoStable Type

NameTypeDescription
metadataMetadataMetadata object, contains information about the NFT.
current_saleAuction[]Contains a list with received escrows.

GetNftErrors Enumeration is the same as GetBalanceErrors .

Usage example:

const response = await getNft('nft-id');

if (response.ok) {
  const { metadata } = response.ok;
  const ownerField = metadata?.find((data) => data.name === 'owner');
  console.log(ownerField.value.Principal.toText());
  // This will output the principal id of the owner of the NFT.
}
else if (response.err)
  // something wrong happend

getNftHistory(tokenId, start, end) β‡’ Promise<OrigynResponse<TransactionType, GetNftErrors>>

Get transaction hitsory of an NFT between the start and the end date if provided.

ParamTypeDefaultDescription
tokenIdstringThe token id of the NFT.
startBigInt[]Bottom date of the transaction search.
endBigInt[]Upper date of the transaction search.

GetNftErrors Enumeration is the same as GetBalanceErrors .

Usage example:

const start = 1662638707000000000n; // Thursday, September 8, 2022
const end = 1662638707000000000n; // Friday, September 2, 2022
const response = await getNftHistory('nft-id', start, end);

if (response.ok) {
  const history: TransactionType[] = response.ok;
  for(const transaction in history) {
    const { tokenId, txn_type, timestamp, index } = transaction;
    console.log(`Token ${tokenId} had a transaction (#${index}) on ${timestamp}: ${txn_type}`);
  }
}
else if (response.err)
  // something wrong happend

When the request is successful, an array of TransactionType will be returned. Each transaction will be represented within the object of txn_type, being one of the following:

  • auction_bid
  • burn
  • canister_managers_updated
  • canister_network_updated
  • canister_owner_updated
  • data
  • escrow_deposit
  • escrow_withdraw
  • mint
  • owner_transfer
  • royalty_paid
  • sale_ended
  • sale_opened
  • sale_withdraw

getCanisterCycles(canisterId?: string) β‡’ Promise<BigInt>

Get cycles of the current canister or of the canister provided as argument.

ParamTypeDefaultDescription
canisterIdstringThe canisterId to get the cycle for. If it is not provided, the canister id from OrigynClient will be used.

Usage example:

const cycles = await getCanisterCycles();

console.log('OrigynClient canister cycles: ', cycles);
const cycles = await getCanisterCycles(canisterId);

console.log(`Available cycles: ${cycles} from canister ${canisterId}`);

getCanisterAvailableStorage(canisterId?: string) β‡’ Promise<BigInt>

Get cycles of the current canister or of the canister provided as argument.

ParamTypeDefaultDescription
canisterIdstringThe canisterId to get the available space for. If it is not provided, the canister id from OrigynClient will be used.

Usage example:

const availableSpace = await getCanisterAvailableStorage();

console.log('OrigynClient canister available space: ', cycles);
const availableSpace = await getCanisterAvailableStorage(canisterId);

console.log(`Available cycles: ${cycles} from canister ${canisterId}`);

getCollectionLibrary(libraryId) β‡’ Promise<Array<MetdataClass>>

Get the collection level library, specified as argument.

ParamTypeDefaultDescription
libraryIdstringThe library_id of the requested library.

Usage example:

const response = await getCollectionLibrary('dapp.html');

console.log('Library title: ', response.Class.library_title);

updateNftApps(tokenId: string, data: any) β‡’ Promise<boolean>

Update the nft __apps property, the data argument needs to be candy-parsed.

ParamTypeDefaultDescription
tokenIdstringThe tokenId to be updated.
datastringData to be updated

Usage example:

const payload = {
  Class: [
    {
      name: 'app_id',
      value: {
        Text: 'public.yourdata', // <--- this will be used as identifier
      },
      immutable: false,
    },
    {
      name: 'read',
      value: {
        Text: 'public',
      },
      immutable: false,
    },
    {
      name: 'data',
      value: {
        Class: [
          // place here the data
        ],
      },
      immutable: false,
    },
  ],
};
const response = await updateNftApps('token-01', payload);

updateLibraryMetadata(tokenId: string, libraryId: string, data: Record<string, string | number | boolean>) β‡’ Promise<Array<MetdataClass>>

Update the properties within the metadata of a library.

ParamTypeDefaultDescription
tokenIdstringThe tokenId which contains the library.
libraryIdstringThe libraryId of the library we want to update.
dataRecord<string, value>A key, value record containing the properties we want to update

Usage example:

const data = {
  title: 'New Wallet Title',
};
const response = await updateLibraryMetadata('token-01', 'wallet.html', data);

setLibraryImmutable(tokenId: string, libraryId: string) β‡’ Promise<OrigynResponse<undefined, StageLibraryAssetErrors >>

Update a mutable library to immutable.

ParamTypeDefaultDescription
tokenIdstringThe tokenId which contains the library.
libraryIdstringThe libraryId of the library we want to update.
const response = await setLibraryImmutable('token-01', 'wallet.html');

πŸ›’οΈ Canister

setCanisterStorage(storage: number) β‡’ Promise<OrigynResponse<ManageStorageResponse, GetCanisterError >>

Updates the canister owner

ParamTypeDefaultDescription
storagenumberAmount of storage to be set for the canister
const response = await setCanisterStorage(500_000_000);

setCanisterOwner(principal: Principal | string) β‡’ Promise<OrigynResponse<bool, GetCanisterError >>

Updates the canister owner

ParamTypeDefaultDescription
principalstring | PrincipalThe principal to be assigned as owner
const response = await setCanisterOwner('nayto-yhiky-76bvl-3qeh5-eupz7-sgmun-mn5fu-ezr5c-xfdcm-zpk37-6qe');