Local Development Guide

This guide is for developers that want to make some changes on the Glittr Core, Glittr SDK, or the Wallet Extension. Often times you want to test your changes end-to-end, and that means you'll have to setup the whole Glittr setup locally.

Before we get into the guide, let's look at the overall flow. An end-to-end flow would look like this:

  1. User install and use Glittr Wallet

  2. User opens an application that uses glittr-sdk to build Glittr transaction

  3. That application will use Electrum API to broadcast a transaction

  4. After a transaction is broadcasted, it will be mined by a local Bitcoin node

  5. After it is mined, the transaction will be processed by the Glittr node

  6. The result of that processing will be reflected on the app and the wallet

This tutorial will guide you through the entire flow, entirely locally. So you can make changes and test end-to-end.

Table of Contents

1. Install bitcoind and run a local devnet bitcoin node

2. Install and run a Glittr node

3. Build and run electrum server

4. Build and run explorer

5. Build and run glittr-sdk

6. Building and running Example App

7. Building and using Glittr Wallet

1. Install bitcoind and run a local devnet bitcoin node

First, follow the tutorial here to install the bitcoin node Run a Bitcoin Node and start it running for local development.

Open up another terminal and then run the command below:

create_wallet.sh
$ bitcoin-cli -rpcpassword=password -rpcuser=user -chain=regtest createwallet default

After that, you probably want to mine the blocks automatically. Create a new file and paste the script below:

scripts/generate_blocks.sh
#!/bin/bash

while true
do 
    bitcoin-cli -rpcpassword=password -rpcuser=user -chain=regtest -generate 1 1000
    sleep 10
done

You will need to change the permissions to make the script executable, run:

chmod +x generate_blocks.sh

Then run the script:

./generate_blocks.sh

You can send Bitcoin to your address by using the bitcoin-cli, you'll find your Bitcoin address after you have your wallet.

$ bitcoin-cli -rpcpassword=password -rpcuser=user -chain=regtest -named sendtoaddress address="{bitcoin_address}" amount=0.5 fee_rate=1

If this your first time running bitcoind, generate 100 blocks as requirement for the coinbase maturity rule:

$ bitcoin-cli -rpcpassword=password -rpcuser=user  -chain=regtest -generate 100 1000

If you are following correctly, the bitcoin will open an RPC port at 127.0.0.1:18443, and the Glittr node will open up a port at 127.0.0.1:3001.

2. Install and run a Glittr node

And then follow this tutorial to run the Glittr node Run a Glittr Node. Make sure that the settings.yml use the correct Bitcoin rpc.

3. Build and run electrum server

Electrum is used to broadcast bitcoin transactions without directly using a bitcoin RPC. It's also used in wallets.

Open a new terminal and clone https://github.com/Glittrfi/electrs. Make sure rust is installed.

Make sure the bitcoin node already running, move to the directory where you installed Electrum and then do this to run the Electrum service:

RUST_BACKTRACE=1 cargo run --release --bin electrs -- -vvvv --daemon-rpc-addr 127.0.0.1:18443 --cors '*' --network regtest --cookie user:password --jsonrpc-import

The process will open a port at 127.0.0.1:3000. So now you'll have the following services,

Service
URL

Bitcoin RPC

127.0.0.1:18443

Glittr Core API

127.0.0.1:3001

Electrum API

127.0.0.1:3000

4. Build and run explorer

To run explorer on your local machine, first clone this repository https://github.com/Glittrfi/BTC-RPC-Explorer. Make sure you have install node (https://nodejs.org/en/download).

Change the directory to the cloned repository, and run this to install dependencies

npm install

Create a file named .env edit the endpoints according to your local ports

.env
NODE_ENV="local"
BTCEXP_HOST=127.0.0.1
BTCEXP_PORT=8080
BTCEXP_BITCOIND_URI=bitcoin://user:password@127.0.0.1:18443?timeout=10000
BTCEXP_BITCOIND_HOST=127.0.0.1
BTCEXP_BITCOIND_PORT=18443
BTCEXP_BITCOIND_USER=user
BTCEXP_BITCOIND_PASS=password
BTCEXP_BITCOIND_RPC_TIMEOUT=5000
GLITTR_API=http://127.0.0.1:3001
BTCEXP_ELECTRUM_TXINDEX=true
BTCEXP_ADDRESS_API=electrum
BTCEXP_ELECTRUM_SERVERS=tcp://127.0.0.1:60401

To start the explorer, run

npm start

Open the explorer at http://127.0.0.1:8080.

5. Build and run glittr-sdk

Clone the repository https://github.com/Glittrfi/glittr-sdk-public. This is a typescript library, so make sure you have install node (https://nodejs.org/en/download). This library will be imported by frontend apps, and also the wallet extension.

We're mostly using yarn as package manager, so let's install yarn.

npm i -g yarn

Go to the glittr-sdk-public directory and install the dependencies

yarn install

This project is a monorepo, so try to build the whole packages

yarn build

The glittr-sdk package is inside packages/sdk, and we have some tests available at apps/sdk-test. Let's create a new file inside apps/sdk-test/src let's name this file free-mint.ts . This file will create a new contract for free mint.

free-mint.ts
import {
  Account,
  GlittrSDK,
  Output,
  OpReturnMessage,
} from "@glittr-sdk/sdk";

const NETWORK = "regtest";
const client = new GlittrSDK({
  network: NETWORK,
  apiKey: "",
  glittrApi: "http://127.0.0.1:3001",
  electrumApi: "http://127.0.0.1:3000"
});

const creatorAccount = new Account({
  wif: "cW84FgWG9U1MpKvdzZMv4JZKLSU7iFAzMmXjkGvGUvh5WvhrEASj",
  network: NETWORK,
});

async function deployFreeMint() {
  const tx: OpReturnMessage = {
    contract_creation: {
      contract_type: {
        moa: {
          ticker: "TEST",
          divisibility: 18,
          live_time: 0,
          supply_cap: "1000",
          mint_mechanism: {
            free_mint: {
              supply_cap: "400",
              amount_per_mint: "10",
            },
          },
        },
      },
    },
  };

  const outputs: Output[] = [
    { address: creatorAccount.p2pkh().address, value: 546 },
  ];

  const txid = await client.createAndBroadcastTx({
    account: creatorAccount.p2pkh(),
    tx,
    outputs,
  });

  console.log(`TXID : ${txid}`);
}

deployFreeMint();

This example has a hardcoded Bitcoin wallet, let's see what's the address is (let's use the P2PKH address)

check-address.ts
import { Account } from "@glittr-sdk/sdk";

const NETWORK = "regtest"

const creatorAccount = new Account({
  wif: "cW84FgWG9U1MpKvdzZMv4JZKLSU7iFAzMmXjkGvGUvh5WvhrEASj",
  network: NETWORK,
});

console.log(creatorAccount.p2pkh());

Let's do yarn buildand then node apps/sdk-test/dist/check-address.js . You will see that the address is "mroHGEtVBLxKoo34HSHbHdmKz1ooJdA3ew". Now send some Bitcoin to that address:

$ bitcoin-cli -rpcpassword=password -rpcuser=user -chain=regtest -named sendtoaddress address="mroHGEtVBLxKoo34HSHbHdmKz1ooJdA3ew" amount=0.5 fee_rate=1

Wait for a bit (a minute for a block to be mined). And then yarn buildand run the free-mint example from above `node apps/sdk-test/dist/free-mint.js` . If you see your transaction ID, that means you have created a Glittr contract, congrats!

6. Building and running Example App

Let's use the glittr boilerplate app. Clone the repo at https://github.com/Glittrfi/glittr-examples. Let's go to the glittr-free-mint-vite-react. Do the following to install dependencies and test build.

yarn install
yarn build

Change the .env.local to your local setting

.env.local
VITE_PUBLIC_NETWORK=regtest
VITE_PUBLIC_GLITTR_API=http://127.0.0.1:3001
VITE_PUBLIC_WALLET_API=http://127.0.0.1:3000
VITE_PUBLIC_EXPLORER=http://127.0.0.1:8080

Run yarn devto test the app locally, it might open a port at http://localhost:5173.

NOTE: If you want to test your edited glittr-sdk , you can use yarn link.

  1. Inside glittr-sdk/packages/sdkdo yarn link , ensure that you have run yarn build

  2. Go to the glittr example directory, do yarn link @glittr-sdk/sdk

  3. Your app project is now using the local glittr-sdk

7. Building and using Glittr Wallet

If you want to make changes to the wallet, you can first clone the https://github.com/Glittrfi/wallet-extension. Install the required dependencies:

yarn install

Change the endpoints inside src/shared/constant/index.ts, on line 369-387 change the devnet endpoints. When you are using the wallet, select Devnet, just put anything for the Api Key since we are on local.

  [ChainType.GLITTR_DEVNET]: {
    enum: ChainType.GLITTR_DEVNET,
    label: 'Glittr Devnet',
    iconLabel: 'Bitcoin',
    icon: './images/artifacts/bitcoin-testnet.svg',
    unit: 'rBTC',
    networkType: NetworkType.REGTEST,
    endpoints: ['http://127.0.0.1:3000'], // change this to local Electrum API
    mempoolSpaceUrl: 'http://127.0.0.1:8080', // change this to local explorer URL
    unisatUrl: 'https://signet.unisat.io',
    ordinalsUrl: '',
    unisatExplorerUrl: '',
    okxExplorerUrl: '',
    showPrice: false,
    defaultExplorer: 'mempool-space',
    glittrApi: 'http://127.0.0.1:3001', // change this to local Glittr API
    electrumApi: 'http://127.0.0.1:3000', // change this to local Electrum API
    glittrApiKey: ''
  }

Make some changes and then build for chrome:

yarn build:chrome

For firefox:

yarn build:chrome

For brave:

yarn build:brave

The build result would be inside dist/ directory. Install the wallet by following this tutorial Installing Glittr Wallet. After you make some changes again, you can do this for chrome to build the extension faster,

yarn build:chrome:dev

You can then refresh the chrome extension to reflect the changes.

Last updated