getsafle / safle-vault

Safle Vault is a non-custodial, flexible and highly available crypto wallet.
MIT License
6 stars 2 forks source link

Safle Vault SDK

Safle Vault is a non-custodial, flexible and highly available crypto wallet which can be used to access dapps, send/receive crypto and store identity. Vault SDK is used to manage the vault and provide methods to generate vault, add new accounts, update the state and also enable the user to perform several vault related operations.

Installation and Usage

Installation

Install the package by running the command,

npm install @getsafle/safle-vault

Import the package into your project using,

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

Initialise the vault class,

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,
});

If the vault is not yet generated, then pass the vault parameter as null.

Methods

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

const mnemonic = await vault.generateMnemonic(entropy);

Change Network This method is used to switch the network if a transaction has to be signed for any network other than Ethereum

await changeNetwork(chain)

Generate Vault: This method is used to generate the vault using the mnemonic passed as parameter and encrypted by the encryption key and pin.

const userVault = await vault.generateVault(encryptionKey, pin, mnemonic);

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.

const userVault = await vault.recoverVault(mnemonic, encryptionKey, pin, unmarshalApiKey);

Get supported chains: Returns the list of all the supported EVM and non-EVM chains.

const supportedChains = vault.getSupportedChains();

Export Mnemonic: This method is used to export the 12 word mnemonic phrase which was used to initialize the vault.

const mnemonic = await vault.exportMnemonic(pin);

Validate PIN This method is used to validate the PIN of the user's vault

const isPinValid = await vault.validatePin(pin);

Validate Mnemonic: This method is used to validate the user's mnemonic by querying the first 0th address for its safleId.

const isMnemonicValid = await vault.validateMnemonic(mnemonic, safleID, network, polygonRpcUrl);

Get Accounts: This method is used to get the list of all the accounts in the vault.

const accounts = await vault.getAccounts(encryptionKey);

Export Private Key: This method is used to export the private key of a particular address.

const privateKey = await vault.exportPrivateKey(address, pin);

Add Account: This method is used to add an another account to the vault.

const userVault = await vault.addAccount(encryptionKey, pin);

Sign Message: This method is used to sign a message.

const signedMessage = await vault.signMessage(address, data, pin, encryptionKey)

Sign Transaction: This method is used to sign a transaction.

const signedTransaction = await vault.signTransaction(rawTx, pin, rpcUrl);

Restore Keyring State This method is used to restore the vault state in the keyring controller.

await vault.restoreKeyringState(vault, pin, encryptionKey);

Delete Account: This method is used to delete an account from the vault.

const userVault = await vault.deleteAccount(encryptionKey, address, pin);

Import Wallet: This method is used to import a new wallet using private key for all supported chains.

const userVault = await vault.importWallet(privateKey, pin, encryptionKey);

List Active Chains: This method is used to list all the chains for which the user has generated or imported a wallet.

const chainArray = await vault.getActiveChains();

Get Vault Details: This method is used to get the list of all the accounts (imported and generated) of all the supported chains.

const details = await vault.getVaultDetails(encryptionKey);

Get Native Asset Balance: This method is used to get the native asset balance of an address present in the vault.

const balance = await vault.getBalance(address, rpcUrl);

Sign a rawTx or message and get signature object as output: This method is used to sign the rawTx object or message and get signature object as output.

const signedMessage = await vault.sign(data, address, pin, rpcUrl);

Update Wallet Label: This method is used to update the wallet label.

const updatedVault = await vault.updateLabel(address, encryptionKey, newLabel);

Change Pin: This method is used to change the pin of the vault.

const newPin = await vault.changePin(currentPin, newPin, encryptionKey);

Get Logs: This method retrieves all the logs of all the vault changes.

const logs = await vault.getLogs();

Get Fees: This method returns an object containing gas limit, gas price wrt the speed of transaction confirmation

async getFees(rawTx, rpcUrl);