Installation & Initialisation
NPM Link : https://www.npmjs.com/package/@getsafle/safle-vault
Installation
npm install @getsafle/safle-vault
Package import & initialisation:
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 thegenerateVault()
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.
💡 If the vault is not yet generated, then pass the vault parameter as null.
Functions :
Generate Mnemonic This method is used to generate the 12 word seed phrase for the vault.
const mnemonic = await vault.generateMnemonic(entropy);
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.
Generate Vault This method is used to generate a vault using a specific mnemonic and encrypted with the user's password and PIN.
const userVault = await vault.generateVault(encryptionKey, pin, mnemonic);
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.
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, recoverMechanism = 'transactions', logs = {})
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 transactionslogs
: Array of vault logs objects
Get Supported chains
This method is used to fetch a list of chains supported by vault SDK
const chains = await vault.getSupportedChains();
Returns a list of all chains supported by Safle Vault
Change network: Change the active network
await vault.changeNetwork(chain)
chain
: Name of chain from the supported chains
Add Account This method adds a new account to the keyring.
const userVault = await vault.addAccount(encryptionKey, pin);
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.
Get Accounts This method is used to get the list of all the accounts in the vault.
const accounts = await vault.getAccounts(encryptionKey);
Returns an array of accounts
encryptionKey
- 64 bytes Uint8Array used to encrypt the vault.
Delete Account This method is used to delete an account from the Keyring.
const userVault = await vault.deleteAccount(encryptionKey, address, pin);
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
Import Account This method is used to import an account with a private key
const userVault = await vault.importWallet(privateKey, pin, encryptionKey);
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
Export Mnemonic This method is used to export the 12 word seed phrase used to generate the account keypairs in the vault.
const mnemonic = await vault.exportMnemonic(pin);
Returns the 12 word seed phrase with which the vault is generated
pin
- The pin to access the vault's private functions.
Export Private Key This method is used to export the private key of a specific wallet in the vault
const privateKey = await vault.exportPrivateKey(address, pin);
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
Update Wallet Label: This method is used to update the wallet label.
const updatedVault = await vault.updateLabel(address, encryptionKey, newLabel);
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.
Change Pin: This method is used to change the pin of the vault.
await vault.changePin(currentPin, newPin, encryptionKey);
currentPin
- The existing vault pin.newPin
- The new vault pin.encryptionKey
- The encryption key used to encrypt/decrypt the vault.
Get Logs: This method retrieves all the logs of all the vault changes.
const logs = await vault.getLogs();
Returns array of vault logs objects
Get Fees: This method returns an object containing gas limit, gas price wrt the speed of transaction confirmation
const fees = await vault.getFees(rawTx, rpcUrl);
rawTx
- The raw transaction object.rpcUrl
- The RPC URL for the chain.
Sign Message This method is used to sign a message and return the signed message string
const signedMessage = await vault.signMessage(address, data, pin);
Returns the signed message string
address
- The address for which the message is to be signed.data
- The message to be signedpin
- The pin to access the vault's private functions.
Sign Transaction This method is used to sign a raw transaction and return the signed transaction string
const signedTransaction = await vault.signTransaction(rawTx, pin, chain);
Returns the signed transaction string
chain
- The chain for which the transaction is to be signed.rawTx
- The raw transaction object to be signedpin
- The pin to access the vault's private functions.
Last updated