# DFNS

Dfns is a cybersecurity company providing crypto wallet infrastructure. The company develops a wallet-as-a-service infrastructure in Web3. \
\
To use DFNS with expand.network [<mark style="color:blue;">**SDK**</mark>](https://github.com/expand-network/sdk-nodejs.git), follow these steps:-

### **Step 1:** Installation

&#x20;User needs to install the expand.network SDK using the following command:&#x20;

```git
npm i expand-network
```

### **Step 2: Wallet Initialisation**

```javascript
const { WalletDFNS , prepareTransaction } = require('expand-network');
async function initialiseDFNSWallet(){
    const options = {};
    options.privateKey = process.env.DFNS_PRIVATE_KEY;
    options.credId = process.env.DFNS_CRED_ID;
    options.xApiKey = process.env.X_API_KEY;
    options.appId = process.env.DFNS_APP_ID;
    options.authToken = process.env.DFNS_ACCESS_TOKEN;
    options.baseUrl = process.env.DFNS_API_URL;
    options.walletId = process.env.WALLET_ID;
    options.appOrigin = 'http://localhost:3000';
    const wallet  = await new WalletDFNS(options);
    return wallet;
}

const wallet = await initialiseDFNSWallet();    // Initialise the wallet client
```

### **Step 3: Prepare Transaction**

```javascript
// Preparing the approve transaction from expand.network
const prepareApproveTx = await prepareTransaction('https://api.expand.network/fungibletoken/approve', {       
        "from": "OWNER_WALLET_ADDRESS",
        "tokenAddress": "TOKEN_CONTRACT_ADDRESS",
        "to": "SPENDER_WALLET_ADDRESS",
        "amount": "100000",
        "gas": "25000",
        "xApiKey": "YOUR_API_KEY"     // Replace this with your API Key
    });
prepareApproveTx.chainId = '1'; 
```

### **Step 4: Sign Transaction**

```javascript
// Sign the transaction locally using sdk client
const approveSignedTx = await wallet.signTransaction(prepareApproveTx);         
approveSignedTx.chainId = '1';  // Replace with other chainId to toggle between chains
```

### **Step 5: Send Transaction**

```javascript
// Send the signed transaction
let txHash = await wallet.sendTransaction(approveSignedTx);         
console.log(`approve transaction: https://etherscan.io/tx/${txHash.TxHash}`);  // Confirmation
```

### Sample code to perform a swap using DFNS:&#x20;

```javascript
const { WalletDFNS , prepareTransaction } = require('expand-network');
const dotenv = require('dotenv');
async function initialiseDFNSWallet(){
    const options = {};
    options.privateKey = process.env.DFNS_PRIVATE_KEY;
    options.credId = process.env.DFNS_CRED_ID;
    options.xApiKey = process.env.X_API_KEY;
    options.appId = process.env.DFNS_APP_ID;
    options.authToken = process.env.DFNS_ACCESS_TOKEN
    options.baseUrl = process.env.DFNS_API_URL;
    options.walletId = process.env.WALLET_ID;
    options.appOrigin = 'http://localhost:3000';
    const wallet  = await new WalletDFNS(options);
    return wallet;
}

async function main() {
    dotenv.config();            
    const wallet = await initialiseDFNSWallet();            // Initialise the wallet client
    const xApiKey = process.env.X_API_KEY;      
    const prepareApproveTx = await prepareTransaction('https://api.expand.network/fungibletoken/approve', {       // Prepare the approve transaction from expand.network
        "from": "OWNER_WALLET_ADDRESS",
        "tokenAddress": "TOKEN_CONTRACT_ADDRESS",
        "to": "SPENDER_WALLET_ADDRESS",
        "amount": "100000",
        "gas": "25000",
        "xApiKey": "YOUR_API_KEY"                     // Replace this with your API Key
    });
    prepareApproveTx.chainId = '1';          // Replace with other chainId to toggle between chains

    const approveSignedTx = await wallet.signTransaction(prepareApproveTx);             // Sign the transaction locally using sdk client

    approveSignedTx.chainId = '1';          // Replace with other chainId to toggle between chains
              

    let txHash = await wallet.sendTransaction(approveSignedTx);                 // Send the transaction
    console.log(`approve transaction: https://etherscan.io/tx/${txHash.TxHash}`);          // Confirmation
    
    const swapTransaction = await prepareTransaction('https://api.expand.network/dex/swap',{            // Simlarly build the swap Tx
            "dexId":"1000",  // Replace with other dexId to toggle between dexs
            "amountIn": "100000",
            "amountOutMin": "0",
            "path": ["TOKEN_ADDRESS_TO_SWAP_FROM","TOKEN_ADDRESS_TO_SWAP_TO"],
            "to": "RECEIVER_WALLET_ADDRESS",
            "deadline": "1965990894",
            "from": "SENDER_WALLET_ADDRESS",
            "gas": "173376",
            "xApiKey": "YOUR_API_KEY"               // Replace this with your API Key
    });
    console.log(swapTransaction);
    swapTransaction.chainId = '1';              // Replace with other chainId to toggle between chains

    const swapSignedTx = await wallet.signTransaction(swapTransaction);
    swapSignedTx.chainId = '1'                  // Replace with other chainId to toggle between chains

    const txHash1 = await wallet.sendTransaction(swapSignedTx);
    console.log(`swap transaction: https://etherscan.io/tx/${txHash1.TxHash}`);
}
main();
```


---

# 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.expand.network/~/changes/CwwHZ0oIeFsEWwiEWd1b/integrations/wallets/mpc-wallets/dfns.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.
