# CLI

**Read time - 30 minutes**

This guide contains the instructions for deploying relevant components and using the KRNL SDK and KRNL CLI. For this demo we will use a mock business application where you can submit data, analyze it using a combination of external APIs, run this dataset through an AI model, and get a final verified result to use in your application and the best part is we will do all of this in a single atomic transaction with cryptographic proof. This demo will help you understand the steps to follow to integrate the KRNL Protocol into your application and create a KRNL-powered workflow.

***

### Preparations

* **Install Docker**
  * Go to <https://www.docker.com/get-started/> and install Docker Desktop
  * Make sure you sign in/create an account for docker desktop
* **Etherscan (for verifying smart contracts on Sepolia network)** - Find the API key [here](https://etherscan.io/)
* **Sepolia testnet** - "Add a custom network" on your MetaMask wallet (You can download and install using this [link](https://metamask.io/en-GB/download))
* **Funds** - make sure you have a **SepoliaETH** by claiming them from [faucets here](/helpful-resources/testnet-faucets.md).
  * Sepolia token ≈ **0.1 SepoliaETH**
* **Privy App ID and Secret**
  * Go to [dashboard.privy.io](https://dashboard.privy.io/)
  * Create an app
  * Copy the App ID and secret

<figure><img src="/files/hhmYi2OSudy0Nd8oAG4E" alt=""><figcaption></figcaption></figure>

* **Pimlico API Key**
  * Go to <https://dashboard.pimlico.io/sign-in> and create an account/sign in with your existing account
  * Then navigate to <https://dashboard.pimlico.io/apikeys> and create your api key
  * Keep this API key for later use

### Deploy the Target Contract

#### 1. Install the KRNL CLI

Go to your terminal and run this command:

```bash
npm install -g @krnl-dev/krnl-cli
```

#### 2. Clone the repo

Go to your terminal and run this command:

```bash
git clone https://github.com/KRNL-Labs/poc-dapp-realestateinvestment-7702.git hello-krnl
```

#### 3. Go to contract folder

Run this command:

```bash
cd hello-krnl/contracts
```

#### Create the .env file

1.1 Copy .env.example to .env

```bash
cp .env.example .env
```

1.2 Fill the .env file with these values:

```bash
PRIVATE_KEY=<your_wallet_private_key> //Add your private key with 0x like 0x.....

MOCK_USDC_ADDRESS=0x038b5f24FF651d174411F15f8cc64C4156A9a6D0 //We have already deployed a mock USD for testing

DELEGATED_ACCOUNT_ADDRESS=0x9969827E2CB0582e08787B23F641b49Ca82bc774 //This is the SCA you will be authorizing to interact with KRNL

# Optional: Etherscan API key for contract verification
ETHERSCAN_API_KEY=<your_etherscan_api>

SEPOLIA_RPC_URL=https://ethereum-sepolia-rpc.publicnode.com //Use any Ethereum Sepolia RPC you have
```

{% hint style="info" %}
Follow [this](https://support.metamask.io/configure/accounts/how-to-export-an-accounts-private-key) guide to export MetaMask private key
{% endhint %}

#### Deploy the contract

1.1 Run the following command to install the forge dependencies:

```bash
forge install OpenZeppelin/openzeppelin-contracts 
forge install eth-infinitism/account-abstraction@v0.7.0 
forge install foundry-rs/forge-std 
```

1.2 Run the following command to deploy the contract:

```bash
krnl deploy --network sepolia --contract RealEstateInvestment --verify
```

1.3 After the deployment you get screen like this:

<figure><img src="/files/tucZBeIbCgnvgjO7Q87Z" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
You have to deploy your own target contract where your EOA will be used as the master key for signature verification, you will learn about this later in the docs
{% endhint %}

### Setup the Attestor Image

Attestor is a component which will attest and also resolve the secrets in your DSL workflow (don't worry we will learn about all of this in the core concepts)

#### 1. Run the Attestor&#x20;

```
krnl create-attestor
```

#### 2. Follow the onscreen instruction

2.1 Enter your project name:

<figure><img src="/files/KMrZhWseV8AfVlAaUqZD" alt=""><figcaption></figcaption></figure>

2.2 Enter Docker registry, it should `docker.io`&#x20;

<figure><img src="/files/4JE3B3S9fHThpWynXeTa" alt=""><figcaption></figcaption></figure>

2.3 Enter your docker username

<figure><img src="/files/5yyHkrzjEUOsNxlfAtAY" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
The script pushes an attestor image to your docker hub so make sure to sign in and ensure that your docker desktop is running in the background while this script is being executed
{% endhint %}

2.4 Enter your private key

<figure><img src="/files/kOylgUh7OLIHh4GVYTzZ" alt=""><figcaption></figcaption></figure>

2.5 Create a encryption secret which is a password to the safe carrying your private key to make your private key is handled safely. Press enter to auto generate

<figure><img src="/files/RzvB3eRwTszksNq3Cs6s" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
This is the same private key which you have derived from your MetaMask wallet
{% endhint %}

2.6 Pass the secrets to resolve in the DSL workflow during execution. For our real estate workflow example, we need:

* rpcSepoliaURL=\<ethereum\_sepolia\_rpc>
* pimlico-apikey=\<api\_key\_of\_your\_pimlico\_account>
* OPENAI\_API\_KEY=mock-api (We are using a mock api in our DSL for the sake of the demo)

<figure><img src="/files/Kxw6vDbr9RoiDKP8p4KO" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
Make sure to replace '<>' with actual values
{% endhint %}

{% hint style="info" %}
We will learn more about workflow DSL later in [Workflows](/core-concepts/workflows.md). For now make sure to add the exact key pair as a part of creating attestor image
{% endhint %}

2.7 Wait for few seconds and you will see a success screen with your attestor image&#x20;

<figure><img src="/files/MtGdJZTfxJyFfeXJjptS" alt=""><figcaption></figcaption></figure>

### Setup the dApp

#### 1. Go to frontend folder from the contract folder

```
cd ../frontend
```

#### 2. Create the .env file

2.1 Copy .env.example to .env

```
cp .env.example .env
```

2.2 Fill the .env file with these values:

```
VITE_PRIVY_APP_ID=<your_privy_app_id>
VITE_PRIVY_APP_SECRET=<your_privy_secret>

VITE_CHAIN_ID=11155111 //Chain ID for Ethereum Sepolia

VITE_DELEGATED_ACCOUNT_ADDRESS=0x9969827E2CB0582e08787B23F641b49Ca82bc774 // Do not change this

VITE_DELEGATE_OWNER=<your_eoa_address> // This should be the same address you used to create your attestor
VITE_REAL_ESTATE_INVESTMENT_ADDRESS=0x038b5f24FF651d174411F15f8cc64C4156A9a6D0 //Replace this with the target contract address we just deployed
VITE_MOCK_USDC_ADDRESS=0xaC66E9916dCe765405E4A4297DdDF61729CbDFF9

VITE_ATTESTOR_IMAGE=image://docker.io/ash20pk20/attestor-realestate:latest //Replace this image with your attestor image link, make to keep the image:// prefix

VITE_RPC_URL=https://ethereum-sepolia-rpc.publicnode.com //Use any Ethereum Sepolia RPC you have
```

#### 3. Install the dependencies

```
npm install
```

#### 4. Run the application

```
npm run dev
```

{% hint style="info" %}
Your app will be available at <http://localhost:5173/>
{% endhint %}

#### 5. Run your KRNL workflow&#x20;

{% hint style="info" %}
We are running this workflow from your user's point of view&#x20;
{% endhint %}

5.1 Connect your wallet

<figure><img src="/files/qehwakx9Ij2CgW1EB43Y" alt=""><figcaption></figcaption></figure>

5.2 Fund the embedded wallet with Sepolia ETH token

<figure><img src="/files/JhwGXlIgwKzayOhSokoi" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
This wallet is different from your external wallet and this is created automatically as soon as you sign in.
{% endhint %}

5.3 Delegate execution to KRNL smart contract account

<figure><img src="/files/kVf03XlOcQUVRQwhx6xL" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
This embedded wallet is completely independent of your external wallet to make sure your users' personal wallets are safe when delegating execution to our delegated account.
{% endhint %}

5.4 Run the workflow DSL

<figure><img src="/files/O394Aiu5NYthV0xEzV5x" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The time taken for the execution depends on the latency of the external services being used in the workflow.
{% endhint %}

5.5 Verify the execution in your target contract

<figure><img src="/files/9lJdMgLZUMg9XA4zTsCd" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The event signifies that the function was called with the KRNL response, the response signature was verified by the modifier and the response was decoded for the application.
{% endhint %}

These steps signify the happy path to deploying your first KRNL-powered application which lets you use functions across multiple environments as well as external APIs, letting you build applications without being limited by existing system design.

### What's next?

Congrats on completing your first hands-on tutorial using KRNL now it's time to learn the [Core Concepts](/core-concepts/workflows.md) to understand all the concepts and flows in detail.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.krnl.xyz/getting-started/getting-started-with-krnl/cli.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
