-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
377 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
test.js | ||
.vscode/launch.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
### 1.0.0 (2023-12-12) | ||
|
||
##### Initial wallet implementation | ||
|
||
- Implemented wallet functionality to enable account generation. | ||
- Added method to add a new account to the wallet object. | ||
- Added method to export the private key of an address | ||
- Added importWallet() to import account using privateKey. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
prefix: 'cosmos' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const { EventEmitter } = require('events') | ||
const ObservableStore = require('obs-store') | ||
const { prefix } = require('./config/index') | ||
const { DirectSecp256k1HdWallet } = require('@cosmjs/proto-signing') | ||
const crypto_1 = require("@cosmjs/crypto"); | ||
const encoding_1 = require("@cosmjs/encoding"); | ||
const amino_1 = require("@cosmjs/amino"); | ||
|
||
|
||
class KeyringController extends EventEmitter { | ||
|
||
constructor(opts) { | ||
super() | ||
this.store = new ObservableStore({ mnemonic: opts.mnemonic}) | ||
this.importedWallets = [] | ||
} | ||
|
||
async generateWallet() { | ||
const { mnemonic } = this.store.getState(); | ||
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic) | ||
this.updatePersistentStore({ wallet: wallet }) | ||
return wallet | ||
} | ||
|
||
async getAccounts() { | ||
const { wallet } = this.store.getState(); | ||
const WalletAccounts = await wallet.getAccounts() | ||
let accounts = [] | ||
WalletAccounts.map((account) => { | ||
accounts.push(account.address) | ||
}) | ||
return accounts | ||
} | ||
|
||
async addAccounts() { | ||
const { mnemonic } = this.store.getState(); | ||
const noOfAccounts = (await this.getAccounts()).length | ||
const hdPaths = this.makeHDPaths(noOfAccounts) | ||
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { hdPaths: hdPaths }) | ||
this.updatePersistentStore({ wallet: wallet }) | ||
let accounts = await this.getAccounts() | ||
return accounts.pop() | ||
} | ||
|
||
makeHDPaths(noOfAccounts) { | ||
const hdPaths = [] | ||
for (let index = 0; index < noOfAccounts + 1; index++) { | ||
hdPaths.push((0, amino_1.makeCosmoshubPath)(index)) | ||
} | ||
return hdPaths | ||
} | ||
|
||
async exportPrivateKey(_address) { | ||
const { wallet } = this.store.getState(); | ||
const accounts = await wallet.getAccountsWithPrivkeys(); | ||
const account = accounts.find((account) => account.address === _address) | ||
const pkey = Buffer.from(account.privkey).toString('hex'); | ||
return pkey | ||
} | ||
|
||
async importWallet(_privateKey) { | ||
const pkeyBuffer = Uint8Array.from(Buffer.from(_privateKey, 'hex')) | ||
const { pubkey } = await crypto_1.Secp256k1.makeKeypair(pkeyBuffer); | ||
const publicKey = crypto_1.Secp256k1.compressPubkey(pubkey) | ||
const address = (0, encoding_1.toBech32)(prefix, (0, amino_1.rawSecp256k1PubkeyToRawAddress)(publicKey)); | ||
this.importedWallets.push(address); | ||
return address | ||
} | ||
|
||
updatePersistentStore(obj) { | ||
this.store.updateState(obj) | ||
return true | ||
} | ||
|
||
} | ||
|
||
module.exports = { KeyringController } |