@springrole/springwallet
v0.2.2
Published
Wallet for SpringRole users
Downloads
5
Readme
SpringWallet
About
SpringWallet - A simple wallet for flexible identity management for your frontend application
Basic Usage
Install
springwallet
withnpm
.npm install @springrole/springwallet --save
oryarn add @springrole/springwallet
Import springwallet into your project.
import { SpringWallet } from '@springrole/springwallet';
Generate 12 words random mnemonic
const mnemonic = SpringWallet.generateMnemonic();
Create a new wallet using plain text mnemonic and encrypt it with password
async function createWallet(plainTextMnemonic, password) { const encryptedMnemonic = await SpringWallet.encryptMnemonic(plainTextMnemonic, password); // encrypting mnemonic const wallet = await SpringWallet.initializeWalletFromMnemonic(plainTextMnemonic); // initializing wallet const address = wallet.getChecksumAddressString(); // wallet address const key = wallet.getPrivateKey().toString('hex'); // private key await SpringWallet.setWalletSession(address, encryptedMnemonic); // saving wallet session in localStorage sessionStorage.setItem('wallet-session', key); // persist wallet private key in sessionStorage return true; }
Note: encrypted mnemonic and address of the wallet will be store in localStorage at key 'wallet-session'
Fetch wallet's address and encrypted mnemonic
const { address, encryptedMnemonic } = SpringWallet.getWalletSession();
Decrypt encryptedMnemonic and unlock wallet
async function unlockWallet(encryptedMnemonic, password) { let plainTextMnemonic; try { plainTextMnemonic = await SpringWallet.decryptMnemonic(encryptedMnemonic, password); } catch { return false; } return SpringWallet.unlockWallet(plainTextMnemonic); }
Use SpringWallet provider with web3.js
const springwallet = new SpringWallet({ rpcUrl: "http://localhost:8545", chainId: "1337" }); const web3 = new Web3(springwallet.provider); return web3;
NOTE SpringWallet needs to be unlocked before performing any web3 actions, like
getAccounts()
,getBalance()
Advance Usage
Change SpringWallet password
async function changeWalletPassword(address, encryptedMnemonic, oldPassword, newPassword) { const mnemonicPhrase = await SpringWallet.decryptMnemonic(encryptedMnemonic, oldPassword); const newEncryptedMnemonic = await SpringWallet.encryptMnemonic(mnemonicPhrase, newPassword); return true; }
NOTE This will decrypt mnemonic with old password and reencrypts it using new password which will create new encrypted mnemonic
Reset SpringWallet password, needs the plaintext mnemonic
async function resetWalletPassword(plainTextMnemonic, newPassword) { const newEncryptedMnemonic = await SpringWallet.encryptMnemonic(plainTextMnemonic, newPassword); const wallet = await SpringWallet.initializeWalletFromMnemonic(plainTextMnemonic); const walletAddress = wallet.getChecksumAddressString(); return true; }
Contributing
TODO