> For the complete documentation index, see [llms.txt](https://docs.safle.com/safleid/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.safle.com/safleid/safle-vault/installation-and-initialisation.md).

# Installation & Initialisation

**NPM Link :** <https://www.npmjs.com/package/@getsafle/safle-vault>

### **Installation**&#x20;

<pre class="language-jsx"><code class="lang-jsx"><strong>npm install @getsafle/safle-vault
</strong></code></pre>

### **Package import & initialisation:**

```javascript
const Vault = require('@getsafle/safle-vault');

const vault = new Vault({ 
    vault,
    customEncryptor: {
    // An optional object for defining encryption schemes:
    // Defaults to crypto-js library
    encrypt(password, object) {
      return encryptedString;
    },
    decrypt(password, encryptedString) {
      return decryptedData;
    },
  },
  platform,
  storage,
});

```

* `vault` (optional) - If the user already has a vault string generated, then it can be passed as the first parameter. If the vault string is not passed, then the vault has to be generated using the `generateVault()` function.
* `customEncryptor` (optional) - If the user wants to use their own custom encryption/decryption function.
* `platform` (optional) - The platform on which the vault sdk is integrated with. This data will be helpful for logging purpose.
* `storage` (optional) - The storage mechanism for vault. Can be an array of strings incase there are multiple storage mechanisms.

<mark style="color:yellow;">`💡 If the vault is not yet generated, then pass the vault parameter as null.`</mark>

### Functions :

1. **Generate Mnemonic** This method is used to generate the 12 word seed phrase for the vault.

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const mnemonic = await vault.generateMnemonic(entropy);
   </code></pre>

   Returns a 12 word seed phrase

   * `entropy` (optional) - The entropy used to generate the 12 word seed phrase. (Uses crypto.randomBytes under the hood). Defaults to 128-bits of entropy.<br>
2. **Generate Vault** This method is used to generate a vault using a specific mnemonic and encrypted with the user's password and PIN.

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const userVault = await vault.generateVault(encryptionKey, pin, mnemonic);
   </code></pre>

   Returns the encrypted vault string

   * `encryptionKey` - 64 bytes Uint8Array used to encrypt the vault.
   * `pin` - The pin to access the vault's private functions.
   * `mnemonic` - The mnemonic to generate the vault from.<br>
3. Recover Vault

   This method is used to recover the vault using the mnemonic phrase. The new vault will be re-encrypted using the pin and encryption key.

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const userVault = await vault.recoverVault(mnemonic, encryptionKey, pin, unmarshalApiKey, recoverMechanism = 'transactions', logs = {})
   </code></pre>

   Returns the encrypted vault string

   * `mnemonic` - The mnemonic of the vault to be recovered.
   * `encryptionKey` - The encryption key used to encrypt/decrypt the vault.
   * `pin` - The pin to access the vault's private functions.
   * `unmarshalApiKey` - API Key of unmarshal api.
   * `recoveryMechanism` - logs/transactions. Defaults to transactions. `unmarshalApiKey` required if recovery mechanism is transactions
   * `logs` : Array of vault logs objects<br>
4. Get Supported chains

   This method is used to fetch a list of chains supported by vault SDK

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const chains = await vault.getSupportedChains();
   </code></pre>

   Returns a list of all chains supported by Safle Vault<br>
5. Change network: Change the active network

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">await vault.changeNetwork(chain)
   </code></pre>

   * `chain` : Name of chain from the supported chains<br>
6. **Add Account** This method adds a new account to the keyring.

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const userVault = await vault.addAccount(encryptionKey, pin);
   </code></pre>

   Returns the encrypted vault string and newly generated wallet address

   * `encryptionKey` - The encryption key used to decrypt the vault.
   * `pin` - The pin to access the vault's private functions.<br>
7. **Get Accounts** This method is used to get the list of all the accounts in the vault.

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const accounts = await vault.getAccounts(encryptionKey);
   </code></pre>

   Returns an array of accounts

   * `encryptionKey` - 64 bytes Uint8Array used to encrypt the vault.<br>
8. **Delete Account** This method is used to delete an account from the Keyring.

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const userVault = await vault.deleteAccount(encryptionKey, address, pin);
   </code></pre>

   Returns the new vault string with an account deleted

   * `encryptionKey` - The encryption key used to decrypt the vault.
   * `pin` - The pin to access the vault's private functions.
   * `address` - The public address of the account to be deleted<br>
9. **Import Account** This method is used to import an account with a private key

   <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const userVault = await vault.importWallet(privateKey, pin, encryptionKey);
   </code></pre>

   Returns the new vault string with an account imported

   * `encryptionKey` - The encryption key used to decrypt the vault.
   * `pin` - The pin to access the vault's private functions.
   * `privateKey` - The private key of the account to be imported<br>
10. **Export Mnemonic** This method is used to export the 12 word seed phrase used to generate the account keypairs in the vault.

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const mnemonic = await vault.exportMnemonic(pin);
    </code></pre>

    Returns the 12 word seed phrase with which the vault is generated

    * `pin` - The pin to access the vault's private functions.<br>
11. **Export Private Key** This method is used to export the private key of a specific wallet in the vault

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const privateKey = await vault.exportPrivateKey(address, pin);
    </code></pre>

    Returns the private key for the requested wallet

    * `pin` - The pin to access the vault's private functions.
    * `address` - The public address of the account to be deleted<br>
12. Update Wallet Label: This method is used to update the wallet label.

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const updatedVault = await vault.updateLabel(address, encryptionKey, newLabel);
    </code></pre>

    Returns the updated vault string

    * `address` - The address for which the label is to be updated.
    * `encryptionKey` - The encryption key used to encrypt/decrypt the vault.
    * `newLabel` - The new label to be added.<br>
13. Change Pin: This method is used to change the pin of the vault.

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">await vault.changePin(currentPin, newPin, encryptionKey);
    </code></pre>

    * `currentPin` - The existing vault pin.
    * `newPin` - The new vault pin.
    * `encryptionKey` - The encryption key used to encrypt/decrypt the vault.<br>
14. Get Logs: This method retrieves all the logs of all the vault changes.

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const logs = await vault.getLogs();
    </code></pre>

    Returns array of vault logs objects<br>
15. Get Fees: This method returns an object containing gas limit, gas price wrt the speed of transaction confirmation

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const fees = await vault.getFees(rawTx, rpcUrl); 
    </code></pre>

    * `rawTx` - The raw transaction object.
    * `rpcUrl` - The RPC URL for the chain.<br>
16. **Sign Message** This method is used to sign a message and return the signed message string

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const signedMessage = await vault.signMessage(address, data, pin);
    </code></pre>

    Returns the signed message string

    * `address` - The address for which the message is to be signed.
    * `data` - The message to be signed
    * `pin` - The pin to access the vault's private functions.<br>
17. **Sign Transaction** This method is used to sign a raw transaction and return the signed transaction string

    <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const signedTransaction = await vault.signTransaction(rawTx, pin, chain);
    </code></pre>

    Returns the signed transaction string

    * `chain` - The chain for which the transaction is to be signed.
    * `rawTx` - The raw transaction object to be signed
    * `pin` - The pin to access the vault's private functions.
