Skip to content

Commit c747e86

Browse files
authored
Merge pull request #12 from SpringRole/develop
Develop
2 parents e6fc25b + da8df81 commit c747e86

File tree

10 files changed

+900
-1817
lines changed

10 files changed

+900
-1817
lines changed

.eslintrc.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
{
2-
"parser": "babel-eslint",
32
"env": {
43
"browser": true,
5-
"commonjs": true,
64
"es6": true,
75
"node": true
86
},
9-
"extends": ["standard", "prettier", "prettier/standard"],
7+
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
108
"parserOptions": {
11-
"ecmaVersion": 2017,
9+
"ecmaVersion": 2018,
1210
"sourceType": "module"
1311
},
14-
"plugins": ["prettier", "standard"],
12+
"plugins": ["prettier"],
1513
"rules": {
1614
"no-useless-constructor": 0,
1715
"no-new": 0,

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
src/
33
.babelrc
44
.editorconfig
5+
.eslintcache
56
.eslintignore
67
.eslintrc.json
78
.prettierrc

.prettierrc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"printWidth": 120,
3-
"tabWidth": 4,
4-
"useTabs": false,
5-
"semi": true,
6-
"singleQuote": true,
7-
"trailingComma": "none",
8-
"bracketSpacing": false,
9-
"arrowParens": "always",
10-
"proseWrap": "never"
2+
"printWidth": 120,
3+
"tabWidth": 4,
4+
"useTabs": false,
5+
"semi": true,
6+
"singleQuote": true,
7+
"trailingComma": "none",
8+
"arrowParens": "always",
9+
"proseWrap": "never",
10+
"endOfLine": "lf"
1111
}

README.md

Lines changed: 86 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,103 @@
11
# SpringWallet
22

3-
- [About](#about)
4-
- [Usage](#usage)
5-
- [Contributing](#contributing)
3+
- [About](#about)
4+
- [Usage](#usage)
5+
- [Contributing](#contributing)
66

77
## About
88

9-
SpringWallet - A simple wallet for flexible identity management
9+
SpringWallet - A simple wallet for flexible identity management for your frontend application
1010

11-
## Usage
11+
#### Basic Usage
1212

1313
1. Install `springwallet` with `npm`.
1414

15-
```bash
16-
npm install springwallet --save
17-
```
15+
```npm install springwallet --save``` or ```yarn add springwallet```
1816

1917
2. Import springwallet into your project.
2018

21-
```js
22-
const SpringWallet = require('springwallet')
23-
```
24-
19+
```js
20+
import SpringWallet from '@springrole/springwallet';
21+
```
2522
3. Generate 12 words random mnemonic
2623

27-
```js
28-
const mnemonic = SpringWallet.generateMnemonic();
29-
```
30-
31-
4. Store encrypted Password in Client
32-
33-
```js
34-
SpringWallet.storePassword(password);
35-
```
36-
Note: Plain text password will be the input and the encrypted password will be stored in browser's sessionStorage at key 'wallet-session'
37-
38-
5. Encrypt mnemonic
39-
40-
```js
41-
SpringWallet.encryptMnemonic(mnemonic).then(function(encryptedMnemonic) {
42-
// Do Something like initialize wallet
43-
console.log("encryptedMnemonic:", encryptedMnemonic));
44-
SpringWallet.initializeAndUnlockWallet(encryptedMnemonic);
45-
})
46-
```
47-
Note: mnemonic will be encrypted with the stored password in client side.
48-
49-
6. Initalize a wallet and unlocks it simultaneously
50-
51-
```js
52-
SpringWallet.initializeAndUnlockWallet(encryptedMnemonic).then(function(walletAddress) {
53-
// Do Something
54-
})
55-
```
56-
Note: This function will initalize a wallet instance also this will store wallet address and encryptedMnemonic in localStorage at key 'wallet-session'
57-
58-
7. Fetch User's balance
59-
60-
```js
61-
SpringWallet.fetchWalletBalance().then(function(balance) {
62-
// Do something
63-
console.log("user balance:", balance);
64-
})
65-
```
66-
8. Generic sendTransaction function to interact with SpringChain
67-
68-
```js
69-
txParams = {
70-
from: "user address",
71-
to: "receiver address OR contract address",
72-
gasLimit: "gas limit",
73-
gasPrice: "gas price",
74-
value: "value to send",
75-
data: "abi encoded data"
76-
};
77-
78-
SpringWallet.sendTransaction(txParams).then(function(txHash) {
79-
// Do Something
80-
console.log("transaction hash:", txHash);
81-
})
82-
```
83-
84-
9. Call reserve function of Vanity contract of Springrole platform
85-
86-
```js
87-
txParams = {
88-
from: "user address",
89-
to: "VanityURL contract address",
90-
vanityUrl: "vanity url",
91-
springrole_id: "User springrole id"
92-
};
93-
94-
SpringWallet.sendVanityReserveTransaction(txParams).then(function(txHash) {
95-
// Do Something
96-
console.log("transaction hash:", txHash);
97-
})
98-
```
99-
10. Call write function of Attestation contract of Springrole platform
100-
101-
```js
102-
txParams = {
103-
from: "user address",
104-
to: "Attestation contract address",
105-
_type_: "type of attestation",
106-
_data: "Data"
107-
}
108-
109-
SpringWallet.sendAttestationTransaction(txParams).then(function(txHash) {
110-
// Do Something
111-
console.log("transaction hash:", txHash);
112-
})
113-
```
114-
24+
```js
25+
const mnemonic = SpringWallet.generateMnemonic();
26+
```
27+
4. Create a new wallet using plain text mnemonic and encrypt it with password
28+
29+
```js
30+
async function createWallet(plainTextMnemonic, password) {
31+
const encryptedMnemonic = await encryptMnemonic(plainTextMnemonic, password); // encrypting mnemonic
32+
const wallet = await SpringWallet.initializeWalletFromMnemonic(plainTextMnemonic); // initializing wallet
33+
const address = wallet.getChecksumAddressString(); // wallet address
34+
const key = wallet.getPrivateKey().toString('hex'); // private key
35+
await SpringWallet.setWalletSession(address, encryptedMnemonic); // saving wallet session in localStorage
36+
sessionStorage.setItem('wallet-session', key); // persist wallet private key in sessionStorage
37+
return true;
38+
}
39+
```
40+
41+
**Note**: encrypted mnemonic and address of the wallet will be store in localStorage at key 'wallet-session'
42+
43+
5. Fetch wallet's address and encrypted mnemonic
44+
45+
```js
46+
const { address, encryptedMnemonic } = SpringWallet.getWalletSession();
47+
```
48+
6. Decrypt encryptedMnemonic and unlock wallet
49+
50+
```js
51+
async function unlockWallet(encryptedMnemonic, password) {
52+
let plainTextMnemonic;
53+
try {
54+
plainTextMnemonic = await decryptMnemonic(encryptedMnemonic, password);
55+
} catch {
56+
return false;
57+
}
58+
return SpringWallet.unlockWallet(plainTextMnemonic);
59+
}
60+
```
61+
62+
7. Use SpringWallet provider with web3.js
63+
64+
```js
65+
const springwallet = new SpringWallet({
66+
rpcUrl: "http://localhost:8545",
67+
chainId: "1337"
68+
});
69+
70+
const web3 = new Web3(springwallet.provider);
71+
return web3;
72+
```
73+
**NOTE** SpringWallet needs to be unlocked before performing any web3 actions, like `getAccounts()`, `getBalance()`
74+
75+
#### Advance Usage
76+
77+
1. Change SpringWallet password
78+
79+
```js
80+
async function changeWalletPassword(address, encryptedMnemonic, oldPassword, newPassword) {
81+
const mnemonicPhrase = await decryptMnemonic(encryptedMnemonic, oldPassword);
82+
const newEncryptedMnemonic = await encryptMnemonic(mnemonicPhrase, newPassword);
83+
const status = await updateEncryptedMnemonic(address, newEncryptedMnemonic);
84+
return status;
85+
}
86+
```
87+
**NOTE** This will decrypt mnemonic with old password and reencrypts it using new password which will create new encrypted mnemonic
88+
89+
2. Reset SpringWallet password, needs the plaintext mnemonic
90+
91+
```js
92+
async function resetWalletPassword(plainTextMnemonic, newPassword) {
93+
const newEncryptedMnemonic = await encryptMnemonic(plainTextMnemonic, newPassword);
94+
const wallet = await SpringWallet.initializeWalletFromMnemonic(plainTextMnemonic);
95+
const walletAddress = wallet.getChecksumAddressString();
96+
const status = await updateEncryptedMnemonic(walletAddress, newEncryptedMnemonic);
97+
return status;
98+
}
99+
```
100+
115101
## Contributing
116102
117103
TODO

package.json

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@
22
"name": "@springrole/springwallet",
33
"version": "0.1.7-beta.7",
44
"description": "Wallet for SpringRole users",
5-
"main": "dist/SpringWallet.node.js",
6-
"browser": "dist/SpringWallet.umd.js",
7-
"browserslist": [
8-
"> 1%",
9-
"node 8",
10-
"not dead"
11-
],
12-
"engines": {
13-
"node": ">=8.0.0"
14-
},
5+
"main": "dist/index.js",
156
"scripts": {
16-
"dev": "webpack --mode development",
17-
"build": "npm run build:node && npm run build:webpack",
18-
"build:node": "babel src --out-dir lib",
19-
"build:webpack": "webpack --mode production",
20-
"lint": "eslint --cache --fix .",
21-
"format": "prettier --config ./.prettierrc --write ./src/{*.js,**/*.js}"
7+
"build": "./node_modules/.bin/babel src --out-dir dist",
8+
"lint": "./node_modules/.bin/eslint .",
9+
"lint:fix": "./node_modules/.bin/eslint --fix .",
10+
"format": "./node_modules/.bin/prettier --ignore-path .gitignore --write ./{*.json,**/*.json}"
2211
},
2312
"repository": {
2413
"type": "git",
@@ -50,7 +39,6 @@
5039
"dependencies": {
5140
"bip39": "^3.0.2",
5241
"ethereumjs-wallet": "^0.6.3",
53-
"sweetalert2": "^8.15.2",
5442
"web3-provider-engine": "^15.0.3"
5543
},
5644
"devDependencies": {
@@ -61,18 +49,26 @@
6149
"@babel/plugin-transform-runtime": "^7.6.0",
6250
"@babel/preset-env": "^7.6.0",
6351
"@babel/runtime": "^7.6.3",
64-
"babel-eslint": "^10.0.3",
65-
"babel-loader": "^8.0.6",
6652
"eslint": "^6.3.0",
6753
"eslint-config-prettier": "^6.2.0",
68-
"eslint-config-standard": "^14.1.0",
69-
"eslint-plugin-import": "^2.18.2",
70-
"eslint-plugin-node": "^10.0.0",
7154
"eslint-plugin-prettier": "^3.1.0",
72-
"eslint-plugin-promise": "^4.2.1",
73-
"eslint-plugin-standard": "^4.0.1",
74-
"prettier": "^1.18.2",
75-
"webpack": "^4.39.1",
76-
"webpack-cli": "^3.3.6"
55+
"husky": "^3.1.0",
56+
"lint-staged": "^9.5.0",
57+
"prettier": "^1.18.2"
58+
},
59+
"husky": {
60+
"hooks": {
61+
"pre-commit": "lint-staged"
62+
}
63+
},
64+
"lint-staged": {
65+
"*.js": [
66+
"./node_modules/.bin/eslint --ignore-pattern '!.eslintrc.js --fix",
67+
"git add"
68+
],
69+
"*.{json,css}": [
70+
"./node_modules/.bin/prettier --write",
71+
"git add"
72+
]
7773
}
7874
}

0 commit comments

Comments
 (0)