Skip to content

Commit

Permalink
initial cosmos wallet implemetation
Browse files Browse the repository at this point in the history
  • Loading branch information
SDargarh committed Dec 12, 2023
1 parent d9ba890 commit da42d1e
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
test.js
.vscode/launch.json
8 changes: 8 additions & 0 deletions CHANGELOG.md
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.
281 changes: 280 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT"
"license": "MIT",
"dependencies": {
"@cosmjs/crypto": "^0.32.1",
"@cosmjs/proto-signing": "^0.32.1",
"obs-store": "^4.0.3"
}
}
3 changes: 3 additions & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
prefix: 'cosmos'
}
77 changes: 77 additions & 0 deletions src/index.js
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 }

0 comments on commit da42d1e

Please sign in to comment.