# Decoding DeFi Transactions

{% embed url="<https://www.youtube.com/watch?v=ElIM7-YPsMY>" %}

## **I. What is an ABI?**

An ABI (application binary interface) is an interface that obtains information between two binary program modules. In the Ethereum ecosystem, an ABI is like a “tool” that decodes DeFi transactions.

For more information, visit our [blog](https://blog.keyst.one/are-you-at-risk-using-defi-with-your-hardware-wallet-8a3d81a27572).

## **II. Why did we implement this?**

Keystone’s vision and mission has always been to make transactions more secure and reliable.&#x20;

In light of the needs of DeFi projects and the potential consequences of [blind signing](https://blog.keyst.one/blind-signing-a-security-black-hole-for-the-ethereum-community-13f909b848b6), Keystone has developed a function of implementing an ABI on a microSD card to decode DeFi transactions. Our [blog](https://blog.keyst.one/why-are-hardware-wallets-out-dated-for-defi-8a3d81a27572) explains this feature in detail and also contains a follow-up development plan.

## **III. Keystone's Solution**

Firmware versions V1.3.0 (Multi-Coin) or newer supports the decoding of DeFi transactions using ABI via a microSD card.

### A. Preparation

1\. [Firmware](https://keyst.one/firmware?locale=en) version V1.3.0 (Multi-Cion) or newer running on Keystone Essential or Pro.

&#x20;   (Please see [firmware upgrade](https://support.keyst.one/getting-started/firmware-upgrading) on for more help.)

{% hint style="warning" %}
**Important Note**：

Firmware V1.3.0 (Multi-Cion) has upgraded the QR code encoding/decoding to a new format. This is **an incompatible change** and you will need to upgrade your MetaMask Chrome Extension accordingly to the latest version.&#x20;
{% endhint %}

2\. Bind “MetaMask Extension” with Keystone Hardware Wallet. ([Tutorial](https://support.keyst.one/3rd-party-wallets/altcoin-wallets/bind-metamask))

3\. MicroSD card (Requirements: Default FAT 32 format and capacity not exceeding 512GB. MicroSD card is not included with Keystone.)

{% hint style="info" %}
**Tip**: Keystone can format microSD cards to FAT32. ([Tutorial](https://support.keyst.one/getting-started/firmware-upgrading/how-to-format-a-microsd-card-to-fat32))
{% endhint %}

4\. A microSD card reader (Not necessary if your laptop has an in-built one).

### **B. Decoding DeFi Transactions**

1\. Bind “MetaMask Extension” with the Keystone Hardware Wallet. ([Tutorial](https://support.keyst.one/3rd-party-wallets/eth-and-web3-wallets-keystone/bind-metamask-with-keystone))

2\. Insert the microSD card into your computer and download [the latest ABI Pack from our GitHub](https://github.com/KeystoneHQ/Smart-Contract-Metadata-Registry/releases) to the microSD card. Unzip the "contracts.zip" file to your microSD card's root directory.

{% hint style="info" %}
**Tips:** You can use an open source tool to verify the data in the ABI SQLite.&#x20;

&#x20;      [ Click here to download the open source tool from "GitHub > REDEME".](https://github.com/sqlitebrowser/sqlitebrowser)
{% endhint %}

<figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVozfoQsxc6YFPQfVTB%2F-Mf6v5KRMS_dkIFQabAn%2F-Mf7AOPrKuWESYh7e6Ci%2F8-1.jpg?alt=media&#x26;token=c79545de-9aa1-479b-ba91-ab22a5e618a9" alt="Keystone-ABI-1"><figcaption></figcaption></figure>

<figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVozfoQsxc6YFPQfVTB%2F-Mf6v5KRMS_dkIFQabAn%2F-Mf7ARMHe7b7w0PY7ztq%2F9-1.jpg?alt=media&#x26;token=1084e66a-bd18-4ea3-b43a-6a8c7cbe6801" alt="Keystone-ABI-2"><figcaption></figcaption></figure>

3\. Insert the microSD card into the Keystone and turn the device on.

<div align="center"><figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrQXQQRT6etffwhkFpfZG%2Fuploads%2Fn5P75mRCc8ha6JIFITIY%2FTF%E6%8F%92%E5%85%A5KV.jpg?alt=media&#x26;token=a16ea940-bed4-4cd5-a666-1331a21aff1b" alt="Keystone-ABI-3"><figcaption></figcaption></figure></div>

4\. Create a DeFi transaction and get the unsigned transaction data in QR code format.

&#x20;   ([How to Bind MetaMask with Keystone?](https://support.keyst.one/3rd-party-wallets/eth-and-web3-wallets-keystone/bind-metamask-with-keystone))

<figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrQXQQRT6etffwhkFpfZG%2Fuploads%2FQIwHdghx3ydOWkQ3JWI0%2F1-1%EF%BC%88web3)-%E7%BC%A9%E5%B0%8F35%25.jpg?alt=media&#x26;token=defb56a9-891e-4071-bd18-2ab7c8cbae6e" alt="Keystone-ABI-4"><figcaption></figcaption></figure>

5\. Keystone: Select \[Menu] > \[Watch-only Wallet] > \[MetaMask / DeFi / Web3] > \[Confirm]. Then select the "scan" icon and scan the QR code shown by the "MetaMask Extension".

<figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrQXQQRT6etffwhkFpfZG%2Fuploads%2FR9G1gZSPNCElnvJivCNm%2F2-M-5.0.jpg?alt=media&#x26;token=3a77d3ec-17ea-4033-a19c-e782d624d77f" alt="Keystone-ABI-5"><figcaption></figcaption></figure>

{% hint style="info" %}
**Tips:** Keystone supports the switching of wallet paths (ETH) to Ledger Live and Legacy formats in the MetaMask / DeFi / Web3 mode. Please check [here](https://support.keyst.one/advanced-features/how-to-switch-eth-path-to-ledger-live-legacy-format) for tutorial and more details.
{% endhint %}

6\. Your Keystone will find the relevant ABI and successfully decode your transactions. Keystone will also show the transaction details.

<figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrQXQQRT6etffwhkFpfZG%2Fuploads%2FcURas1kxZUbpQpi77nEN%2F3-%E7%BC%A9%E5%B0%8F70%25.jpg?alt=media&#x26;token=426b347b-eadd-4728-bd74-623490c06f4f" alt="Keystone-ABI-6"><figcaption></figcaption></figure>

{% hint style="warning" %}
**Attention**:

1\. During the verification process, please keep the microSD card plugged inside the Keystone device at all times. Never pull out the microSD card until you have completed all the signing steps.

2\. Even if Keystone has previously read the necessary ABI, it will not remember it. You still need to insert the microSD with the relevant ABI list each time you sign a transaction for this function to work. &#x20;
{% endhint %}

7\. If your Keystone failed to find the relevant ABI, it will show the coded version of the transaction. You can always add the relevant ABI by yourself. Please go to Chapter IV for specific operation details.

<figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrQXQQRT6etffwhkFpfZG%2Fuploads%2F0DuzWtsAUhPHtAyy4WMt%2F4-%E7%BC%A9%E5%B0%8F70%25.jpg?alt=media&#x26;token=96cef367-0074-4467-93d4-882c00e36d1d" alt="Keystone-ABI-7"><figcaption></figcaption></figure>

## IV. Advanced Operation

Keystone supports users who would like to add their own ABIs onto their microSD cards to address their personal needs. The specific operation is as follows:

{% hint style="info" %}
**Note:** This is an advanced feature. It’s not recommended for crypto newbies.
{% endhint %}

1\. Each contract has a json file with the contract address as its name.

&#x20;   For each json file, there are the following fields:

```
{
    "address":"",
    "name":"",
    "metadata":{
        "output":{
            "abi":[]
        }
    }
}
```

2\.  Edit a json file according to the above file format:&#x20;

1\) Both the “address” and “abi” are required. The address follows the [EIP 55 address checksum format](https://github.com/ethereum/EIPs/issues/55).

2\) The “name” field is not required. You can leave this field empty or delete it if the smart contract you are using doesn’t have a name.

{% hint style="danger" %}
**Attention :**

1\. All letters in the "address" must be lowercased.

2\. The “abi” field must be edited strictly in accordance with the hierarchical relationship shown below：

```aspnet
"metadata":{
        "output":{
            "abi":[]
        }
    }
```

{% endhint %}

The following is an edited example:

```aspnet
{
    "address":"0x28f886Fa1751e623ff4D8422A230d8B49879Bf61",
    "name":"instaDApp",
    "metadata":{
        "output":{
            "abi":[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"erc20","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenAmt","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"getId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"setId","type":"uint256"}],"name":"LogDeposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"erc20","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenAmt","type":"uint256"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"getId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"setId","type":"uint256"}],"name":"LogWithdraw","type":"event"},{"inputs":[],"name":"connectorID","outputs":[{"internalType":"uint256","name":"_type","type":"uint256"},{"internalType":"uint256","name":"_id","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"erc20","type":"address"},{"internalType":"uint256","name":"tokenAmt","type":"uint256"},{"internalType":"uint256","name":"getId","type":"uint256"},{"internalType":"uint256","name":"setId","type":"uint256"}],"name":"deposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"getEthAddr","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getEventAddr","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getMemoryAddr","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"erc20","type":"address"},{"internalType":"uint256","name":"tokenAmt","type":"uint256"},{"internalType":"address payable","name":"to","type":"address"},{"internalType":"uint256","name":"getId","type":"uint256"},{"internalType":"uint256","name":"setId","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"payable","type":"function"}]
        }
    }
}
```

3\. After making the required edits, be sure the file name includes the contact address and ends with “.json” (e.g. 0x28f886fa1751e623ff4d8422a230d8b49879bf61.json)

{% hint style="danger" %}
**Attention**:&#x20;

1\. All letters in the  “address.json” must be lowercase.

2\. Verify that you’ve changed the files to the .json format after editing them.
{% endhint %}

&#x20;Save the .json file to the "contracts > self\_define" path of the microSD card.

<figure><img src="https://2420738387-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrQXQQRT6etffwhkFpfZG%2Fuploads%2FkLBuCgh4IhuXIBiYowFm%2F10-1.png?alt=media&#x26;token=e37c3c3b-db50-44f6-b8df-17a302b1e35c" alt="Keystone-ABI-8"><figcaption></figcaption></figure>

&#x20;Recommended Instructions: [How to Use Uniswap with Keystone?](https://support.keyst.one/3rd-party-wallets/defi/how-to-use-uniswap)
