https://github.com/springrole/springwallet
Wallet for SpringRole users
https://github.com/springrole/springwallet
Last synced: 5 months ago
JSON representation
Wallet for SpringRole users
- Host: GitHub
- URL: https://github.com/springrole/springwallet
- Owner: SpringRole
- Created: 2019-04-09T13:44:31.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-19T01:06:04.000Z (almost 3 years ago)
- Last Synced: 2025-02-16T17:55:13.353Z (over 1 year ago)
- Language: JavaScript
- Size: 1.52 MB
- Stars: 0
- Watchers: 7
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SpringWallet
- [About](#about)
- [Usage](#usage)
- [Contributing](#contributing)
## About
SpringWallet - A simple wallet for flexible identity management for your frontend application
#### Basic Usage
1. Install `springwallet` with `npm`.
```npm install @springrole/springwallet --save``` or ```yarn add @springrole/springwallet```
2. Import springwallet into your project.
```js
import { SpringWallet } from '@springrole/springwallet';
```
3. Generate 12 words random mnemonic
```js
const mnemonic = SpringWallet.generateMnemonic();
```
4. Create a new wallet using plain text mnemonic and encrypt it with password
```js
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'
5. Fetch wallet's address and encrypted mnemonic
```js
const { address, encryptedMnemonic } = SpringWallet.getWalletSession();
```
6. Decrypt encryptedMnemonic and unlock wallet
```js
async function unlockWallet(encryptedMnemonic, password) {
let plainTextMnemonic;
try {
plainTextMnemonic = await SpringWallet.decryptMnemonic(encryptedMnemonic, password);
} catch {
return false;
}
return SpringWallet.unlockWallet(plainTextMnemonic);
}
```
7. Use SpringWallet provider with web3.js
```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
1. Change SpringWallet password
```js
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
2. Reset SpringWallet password, needs the plaintext mnemonic
```js
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