2.5.1 • Published 8 months ago

nosft-core v2.5.1

Weekly downloads
-
License
Public domain
Repository
github
Last release
8 months ago

nosft-core

Tools for developing Nosft clients

Project anatomy

lib
 └ src                              → Application sources
    └ app                           → Business logic
       └ nosft                      → Nosft business logic to be exposed by library
       └ deezy                      → Deezy business logic to be exposed by library
       └ nostr                      → Nostr business logic to be exposed by library
       ...
    └ config                        → Constants, Env variables, configuration settings in general
    └ services                      → Application services layer
       └ nosft                      → Nosft integration service
       └ deezy                      → Deezy integration service
       └ nostr                      → Nostr integration service
       ...
 └ index.js                         → Main application entry point
 └ README.md                        → Library documentation.
 └ node_modules (generated)         → NPM dependencies
 └ test                             → Source folder for unit or functional tests
 └ .editorconfig                    → EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs
 └ .env.sample                      → Sample of for the .env file
 └ .gitignore                       → Files and folders to ignore by git
 └ .npmrc                           → Npm configuration file
 └ .prettierrc                      → Code formatter configuration
 └ jest.config.js                   → Jest configuration file
 └ tsconfig.json                    → Typescript configuration file
 └ tslint.js                        → Typescript linter
 └ build                            → Esbuild configuration file
 └ justfile                         → just configuration file

Getting inscriptions

import { getInscriptions, getAddress } from 'nosft-core';

const addresss = await getAddress();
const inscriptionts = await getInscriptions(address);

Developing

  1. Install just
  2. just -l

Local build

npm run watch

Usage

You can install the Nosft Core SDK by running:

npm install nosft-core

Configuration

You can ovewrite any nosft configuration variable available by passing the object on initialization.

import { Nosft } from 'nosft-core';

const localConfig = {
    // Overwrite RELAY URL
    NOSTR_RELAY_URL: 'ws://localhost:7006',
};

const nosft = Nosft({ ...localConfig });
  • Here you can see the list of available configurations.

Wallet

The connectWallet method allows you to connect your wallet to the Nosft SDK:

const { connectWallet } = nosft.wallet;
const pubKey = await connectWallet();

Address

You can retrieve address information using the getAddressInfo method:

const { getAddressInfo } = nosft.address;
const addressInfo = await getAddressInfo(address);

UTXO

To check if a UTXO contains an inscription, use the doesUtxoContainInscription method. You can also retrieve the UTXOs associated with an address using the getAddressUtxos method:

const { doesUtxoContainInscription, getAddressUtxos } = nosft.utxo;

const hasInscription = await doesUtxoContainInscription(utxo);
const utxos = await getAddressUtxos(address);

Inscriptions

The getInscription method allows you to retrieve a specific inscription, and the getInscriptions method retrieves all available inscriptions:

const { getInscription, getInscriptions } = nosft.inscriptions;

const inscription = await getInscription(inscriptionId);
const inscriptions = await getInscriptions();

PSBT

You can use the signPsbtMessage method to sign a PSBT message, and the broadcastTx method to broadcast a transaction to the Bitcoin network. The signAndBroadcastUtxo method signs and broadcasts a UTXO transaction. The getMetamaskSigner method retrieves the signer used by Metamask, and the signSigHash method signs a message with the sighash flag:

const { signPsbtMessage, broadcastTx, signAndBroadcastUtxo, getMetamaskSigner, signSigHash } = nosft.psbt;

const signedPsbt = await signPsbtMessage(psbt, wallet);
await broadcastTx(signedTxHex);
const signedAndBroadcastedTx = await signAndBroadcastUtxo(wallet, psbt);
const metamaskSigner = await getMetamaskSigner();
const signedSigHash = await signSigHash(wallet, sighash);

Open Ordex

The getAvailableUtxosWithoutInscription method retrieves all available UTXOs without an inscription, and the generatePSBTListingInscriptionForBuy and generatePSBTListingInscriptionForSale methods generate PSBT messages for buying and selling, respectively. The getOrderInformation method retrieves order information:

const {
    getAvailableUtxosWithoutInscription,
    generatePSBTListingInscriptionForBuy,
    generatePSBTListingInscriptionForSale,
    getOrderInformation,
} = nosft.openOrdex;

Usage

Here is the list of all required methods to create a client just like nosft.

import { Nosft } from 'nosft-core';
import { localConfig } from '@lib/constants.config';

const nosft = Nosft({ ...localConfig });

const { connectWallet } = nosft.wallet;
const { getAddressInfo } = nosft.address;
const { doesUtxoContainInscription, getAddressUtxos } = nosft.utxo;
const { getInscription, getInscriptions } = nosft.inscriptions;
const { signPsbtMessage, broadcastTx, signAndBroadcastUtxo, getMetamaskSigner, signSigHash } = nosft.psbt;
const { signAndBroadcastEvent, getNostrInscription, subscribeOrders, unsubscribeOrders } = nosft.nostr;

const {
    getAvailableUtxosWithoutInscription,
    generatePSBTListingInscriptionForBuy,
    generatePSBTListingInscriptionForSale,
    getOrderInformation,
} = nosft.openOrdex;

const {
    shortenStr,
    satsToFormattedDollarString,
    fetchBitcoinPrice,
    outputValue,
    toXOnly,
    sortUtxos,
    parseOutpoint,
    fetchRecommendedFee,
    satToBtc,
    calculateFee,
    getTxHexById,
    tweakSigner,
} = nosft.crypto;

const { config } = nosft;

const {
    NOSTR_RELAY_URL,
    NOSTR_KIND_INSCRIPTION,
    INSCRIPTION_SEARCH_DEPTH,
    GITHUB_URL,
    DEFAULT_FEE_RATE,
    SENDS_ENABLED,
    TESTNET,
    ASSUMED_TX_BYTES,
    ORDINALS_EXPLORER_URL,
    RELAYS,
    MAX_ONSALE,

    BITCOIN_PRICE_API_URL,
    TURBO_API,
    BLOCKSTREAM_API,
    POOL_API_URL,
    MEMPOOL_API_URL,
    NETWORK,
    DEFAULT_DERIV_PATH,
    DUMMY_UTXO_VALUE,
    FEE_LEVEL,
    TAPROOT_MESSAGE,
} = config;

export default nosft;
export {
    getAddressInfo,
    connectWallet,

    // Crypto
    shortenStr,
    satsToFormattedDollarString,
    fetchBitcoinPrice,
    outputValue,
    toXOnly,
    sortUtxos,
    parseOutpoint,
    fetchRecommendedFee,
    satToBtc,
    calculateFee,
    getTxHexById,
    tweakSigner,

    // utxo
    doesUtxoContainInscription,
    getAddressUtxos,

    // inscriptions
    getInscription,
    getInscriptions,

    // psbt
    signPsbtMessage,
    broadcastTx,
    signAndBroadcastUtxo,
    getMetamaskSigner,
    signSigHash,

    // open ordex
    getAvailableUtxosWithoutInscription,
    generatePSBTListingInscriptionForBuy,
    generatePSBTListingInscriptionForSale,
    getOrderInformation,

    // nostr
    signAndBroadcastEvent,
    getNostrInscription,
    subscribeOrders,
    unsubscribeOrders,

    // Config variables
    TAPROOT_MESSAGE,
    NOSTR_RELAY_URL,
    NOSTR_KIND_INSCRIPTION,
    INSCRIPTION_SEARCH_DEPTH,
    GITHUB_URL,
    DEFAULT_FEE_RATE,
    SENDS_ENABLED,
    TESTNET,
    ASSUMED_TX_BYTES,
    ORDINALS_EXPLORER_URL,
    RELAYS,
    MAX_ONSALE,
    BITCOIN_PRICE_API_URL,
    TURBO_API,
    BLOCKSTREAM_API,
    POOL_API_URL,
    MEMPOOL_API_URL,
    NETWORK,
    DEFAULT_DERIV_PATH,
    DUMMY_UTXO_VALUE,
    FEE_LEVEL,
};

Get owned ordinals

This function returns all of the user utxo's, if you only need inscription, filter them /to the ones that has inscriptionId defined.

import { connectWallet, getInscriptions } from '@services/nosft';

const pubKey = await connectWallet(metamask);

const utxos = await getInscriptions(nostrAddress);

Get specific inscription

const inscriptionId = 'YOUR INSCRIPTION ID';
const { inscription, collection } = await getInscription(slug);

Get list of listed inscriptions in nostr

import { subscribeOrders as subscribeNosftOrders, unsubscribeOrders } from '@services/nosft';

const callback = (err, data) => { ... } // your callback

const orderEvent = (err, event) => {
   if (err) {
         callback(err);
   } else {
         callback(null, event);
   }
};

// Returns subs
const subscriptionOrders = subscribeNosftOrders({ callback: orderEvent, limit });

// To unsuscribe
if (subscriptionOrders) {
      unsubscribeOrders();
      subscriptionOrders.unsub();
      subscriptionOrders = null;
}

Sell your own inscription

import {
    shortenStr,
    fetchBitcoinPrice,
    satsToFormattedDollarString,
    generatePSBTListingInscriptionForSale,
    signAndBroadcastEvent,
} from '@services/nosft';

const psbt = await generatePSBTListingInscriptionForSale({
    utxo,
    paymentAddress: destinationBtcAddress,
    price: ordinalValue,
});

const signedPsbt = await signPsbtMessage(psbt);

await signAndBroadcastEvent({
    utxo,
    ordinalValue,
    signedPsbt: signedPsbt.toBase64(),
    pubkey: nostrPublicKey,
});

Buy an inscription

For buying we need to get the signed psbt from nostr first. To get the inscription and nostr information we can do as follows

import { getAddressInfo, getInscription, getNostrInscription } from '@services/nosft';

const fetchInscription = async (inscriptionId) => {
    const { inscription: _inscription, collection: _collection } = await getInscription(inscriptionId);
    setInscription(_inscription);
    setCollection(_collection);
};

const inscription = await fetchInscription(inscriptionId);
const nostr = await getNostrInscription(inscriptionId);

Once we have the inscription and nostr data we can continue with the buy.

import {
    signPsbtMessage,
    broadcastTx,
    getAvailableUtxosWithoutInscription,
    generatePSBTListingInscriptionForBuy,
} from '@services/nosft';

// If buying with BTC, user should get at least 2 dummy utxos
const updatePayerAddress = async (address) => {
    try {
        const { selectedUtxos: _selectedUtxos, dummyUtxos: _dummyUtxos } = await getAvailableUtxosWithoutInscription({
            address,
            price: utxo.value,
        });

        if (_dummyUtxos.length < 2) {
            throw new Error('No dummy UTXOs found. Please create them before continuing.');
        }

        setSelectedUtxos(_selectedUtxos);
        setDummyUtxos(_dummyUtxos);
    } catch (e) {
        setSelectedUtxos([]);
        throw e;
    }
};

const buy = async () => {
    try {
        await updatePayerAddress(destinationBtcAddress);
    } catch (e) {
        setIsBtcInputAddressValid(false);
        toast.error(e.message);
        return;
    }

    try {
        const sellerSignedPsbt = bitcoin.Psbt.fromBase64(nostr.content, { network: NETWORK });

        const psbt = await generatePSBTListingInscriptionForBuy({
            payerAddress: destinationBtcAddress,
            receiverAddress: destinationBtcAddress,
            price: nostr.value,
            paymentUtxos: selectedUtxos,
            dummyUtxos,
            sellerSignedPsbt,
            inscription: utxo,
        });

        const tx = await signPsbtMessage(psbt);
        const txId = await broadcastTx(tx);
    } catch (e) {
        toast.error(e.message);
    }
};

License

Public domain.

2.4.3

8 months ago

2.5.0

8 months ago

2.5.1

8 months ago

1.2.0

11 months ago

1.1.29

11 months ago

1.2.8

10 months ago

1.2.7

10 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

1.3.50

10 months ago

2.4.1

8 months ago

2.4.0

8 months ago

1.3.53

10 months ago

1.1.30

11 months ago

2.4.2

8 months ago

1.3.51

10 months ago

1.3.52

10 months ago

1.1.34

11 months ago

1.1.33

11 months ago

1.1.32

11 months ago

1.1.31

11 months ago

1.1.38

11 months ago

1.1.37

11 months ago

1.1.36

11 months ago

1.1.35

11 months ago

1.1.41

11 months ago

1.1.40

11 months ago

1.1.45

11 months ago

1.2.10

10 months ago

1.1.48

11 months ago

1.1.47

11 months ago

1.1.46

11 months ago

1.3.31

10 months ago

1.3.32

10 months ago

1.3.30

10 months ago

1.3.35

10 months ago

1.3.36

10 months ago

1.3.33

10 months ago

1.3.34

10 months ago

1.3.39

10 months ago

1.3.37

10 months ago

1.3.38

10 months ago

1.3.42

10 months ago

2.3.56

9 months ago

1.3.43

10 months ago

1.3.40

10 months ago

1.3.41

10 months ago

1.2.9

10 months ago

1.3.46

10 months ago

1.3.47

10 months ago

1.1.22

11 months ago

2.3.55

9 months ago

1.3.44

10 months ago

2.3.54

10 months ago

1.3.45

10 months ago

1.3.48

10 months ago

1.3.49

10 months ago

1.0.19

11 months ago

1.0.18

11 months ago

1.0.17

11 months ago

1.0.16

11 months ago

1.0.9

12 months ago

1.0.7

12 months ago

1.3.92

10 months ago

2.3.68

9 months ago

2.3.67

9 months ago

2.3.69

9 months ago

2.3.64

9 months ago

1.0.22

11 months ago

2.3.63

9 months ago

1.0.21

11 months ago

2.3.66

9 months ago

1.0.20

11 months ago

2.3.65

9 months ago

2.3.60

9 months ago

2.3.62

9 months ago

1.0.24

11 months ago

2.3.61

9 months ago

1.0.23

11 months ago

2.3.78

9 months ago

2.3.75

9 months ago

2.3.74

9 months ago

2.3.77

9 months ago

2.3.76

9 months ago

2.3.71

9 months ago

1.3.28

10 months ago

2.3.70

9 months ago

1.3.29

10 months ago

2.3.73

9 months ago

1.3.26

10 months ago

2.3.72

9 months ago

1.3.27

10 months ago

2.3.80

9 months ago

1.1.52

11 months ago

2.3.89

8 months ago

1.1.50

11 months ago

2.3.86

8 months ago

1.1.56

11 months ago

2.3.85

9 months ago

1.1.55

11 months ago

2.3.88

8 months ago

1.1.54

11 months ago

2.3.87

8 months ago

1.1.53

11 months ago

2.3.82

9 months ago

2.3.81

9 months ago

1.1.59

11 months ago

2.3.84

9 months ago

1.1.58

11 months ago

2.3.83

9 months ago

1.1.57

11 months ago

2.3.91

8 months ago

2.3.90

8 months ago

2.3.97

8 months ago

1.0.11

12 months ago

2.3.96

8 months ago

1.0.10

12 months ago

2.3.99

8 months ago

2.3.98

8 months ago

2.3.93

8 months ago

1.0.15

11 months ago

2.3.92

8 months ago

1.0.14

12 months ago

2.3.95

8 months ago

2.3.94

8 months ago

1.0.12

12 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

0.0.0

1 year ago