# 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&#x20;
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

[#id-1.-install-bitcoind-and-run-a-local-devnet-bitcoin-node](#id-1.-install-bitcoind-and-run-a-local-devnet-bitcoin-node "mention")

[#id-2.-install-and-run-a-glittr-node](#id-2.-install-and-run-a-glittr-node "mention")

[#id-3.-build-and-run-electrum-server](#id-3.-build-and-run-electrum-server "mention")

[#id-4.-build-and-run-explorer](#id-4.-build-and-run-explorer "mention")

[#id-5.-build-and-run-glittr-sdk](#id-5.-build-and-run-glittr-sdk "mention")

[#id-6.-building-and-running-example-app](#id-6.-building-and-running-example-app "mention")

[#id-7.-building-and-using-glittr-wallet](#id-7.-building-and-using-glittr-wallet "mention")

### 1. Install bitcoind and run a local devnet bitcoin node

First, follow the tutorial here to install the bitcoin node [run-a-bitcoin-node](https://docs.glittr.fi/node/run-a-bitcoin-node "mention") and start it running for local development.&#x20;

Open up another terminal and then run the command below:

{% code title="create\_wallet.sh" overflow="wrap" %}

```bash
$ bitcoin-cli -rpcpassword=password -rpcuser=user -chain=regtest createwallet default
```

{% endcode %}

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

{% code title="scripts/generate\_blocks.sh" overflow="wrap" lineNumbers="true" %}

```bash
#!/bin/bash

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

{% endcode %}

You will need to change the permissions to make the script executable, run:&#x20;

```
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.

{% code overflow="wrap" %}

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

{% endcode %}

If this your first time running bitcoind, generate 100 blocks as requirement for the [coinbase maturity rule](https://learnmeabitcoin.com/technical/mining/coinbase-transaction/):

{% code overflow="wrap" %}

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

{% endcode %}

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](https://docs.glittr.fi/node/run-a-glittr-node "mention"). Make sure that the `settings.yml` use the correct Bitcoin rpc.&#x20;

### 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.&#x20;

Open a new terminal and clone <https://github.com/Glittrfi/electrs>. Make sure [rust is installed](https://doc.rust-lang.org/beta/book/ch01-01-installation.html).&#x20;

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

{% code overflow="wrap" %}

```bash
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
```

{% endcode %}

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>).&#x20;

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

```bash
npm install
```

Create a file named `.env` edit the endpoints according to your local ports&#x20;

{% code title=".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
```

{% endcode %}

To start the explorer, run

```bash
npm start
```

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

### 5. Build and run glittr-sdk&#x20;

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.

```bash
npm i -g yarn
```

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

```bash
yarn install
```

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

```bash
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.

{% code title="free-mint.ts" lineNumbers="true" %}

```typescript
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();

```

{% endcode %}

This example has a hardcoded Bitcoin wallet, let's see what's the address is (let's use the [P2PKH address](https://learnmeabitcoin.com/technical/script/p2pkh/))&#x20;

{% code title="check-address.ts" lineNumbers="true" %}

```typescript
import { Account } from "@glittr-sdk/sdk";

const NETWORK = "regtest"

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

console.log(creatorAccount.p2pkh());

```

{% endcode %}

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

{% code overflow="wrap" %}

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

{% endcode %}

Wait for a bit (a minute for a block to be mined). And then `yarn build`and 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.

```bash
yarn install
yarn build
```

Change the .env.local to your local setting

{% code title=".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
```

{% endcode %}

Run `yarn dev`to 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/sdk`do `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:

```bash
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.

<pre class="language-typescript"><code class="lang-typescript"><strong>  [ChainType.GLITTR_DEVNET]: {
</strong>    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: ''
  }
</code></pre>

Make some changes and then build for chrome:

```bash
yarn build:chrome
```

For firefox:

```bash
yarn build:chrome
```

For brave:

```bash
yarn build:brave
```

The build result would be inside `dist/` directory. Install the wallet by following this tutorial [installing-glittr-wallet](https://docs.glittr.fi/building-on-glittr/installing-glittr-wallet "mention"). After you make some changes again, you can do this for chrome to build the extension faster,

```bash
yarn build:chrome:dev
```

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