|
1 | 1 | # SpringWallet |
2 | 2 |
|
3 | | -- [About](#about) |
4 | | -- [Usage](#usage) |
5 | | -- [Contributing](#contributing) |
| 3 | +- [About](#about) |
| 4 | +- [Usage](#usage) |
| 5 | +- [Contributing](#contributing) |
6 | 6 |
|
7 | 7 | ## About |
8 | 8 |
|
9 | | -SpringWallet - A simple wallet for flexible identity management |
| 9 | +SpringWallet - A simple wallet for flexible identity management for your frontend application |
10 | 10 |
|
11 | | -## Usage |
| 11 | +#### Basic Usage |
12 | 12 |
|
13 | 13 | 1. Install `springwallet` with `npm`. |
14 | 14 |
|
15 | | -```bash |
16 | | -npm install springwallet --save |
17 | | -``` |
| 15 | + ```npm install springwallet --save``` or ```yarn add springwallet``` |
18 | 16 |
|
19 | 17 | 2. Import springwallet into your project. |
20 | 18 |
|
21 | | -```js |
22 | | -const SpringWallet = require('springwallet') |
23 | | -``` |
24 | | - |
| 19 | + ```js |
| 20 | + import SpringWallet from '@springrole/springwallet'; |
| 21 | + ``` |
25 | 22 | 3. Generate 12 words random mnemonic |
26 | 23 |
|
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 | + |
115 | 101 | ## Contributing |
116 | 102 |
|
117 | 103 | TODO |
0 commit comments