Skip to content

Commit

Permalink
Add support for SOL from mnemonic
Browse files Browse the repository at this point in the history
  • Loading branch information
yerofey committed Feb 4, 2024
1 parent fe6911f commit 13eee87
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yerofey/cryptowallet-cli",
"version": "1.19.0",
"version": "1.20.0",
"description": "Crypto wallet generator CLI tool",
"type": "module",
"homepage": "https://github.com/yerofey/cryptowallet-cli",
Expand Down Expand Up @@ -120,6 +120,7 @@
"commander": "11.1.0",
"csv-writer": "^1.6.0",
"dotenv": "^16.4.1",
"ed25519-hd-key": "^1.3.0",
"eth-lib": "0.1.29",
"ethereum-bip84": "0.0.3",
"ethereum-mnemonic-privatekey-utils": "1.0.5",
Expand Down
20 changes: 19 additions & 1 deletion pnpm-lock.yaml

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

21 changes: 17 additions & 4 deletions src/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Keypair as SolanaKeypair,
PublicKey as SolanaPublickey,
} from '@solana/web3.js';
import { derivePath } from 'ed25519-hd-key';
import bs58 from 'bs58';
import TonWeb from 'tonweb';
import {
Expand Down Expand Up @@ -428,10 +429,21 @@ class Wallet {
mnemonic,
});
} else if (chain == 'SOL') {
// TODO: generate wallet from mnemonic
const wallet = SolanaKeypair.generate();
const publicKeyString = new SolanaPublickey(wallet.publicKey).toBase58();
const secretKeyString = bs58.encode(wallet.secretKey);
// Validate mnemonic
if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
return {
error: 'mnemonic is not valid',
};
}

const mnemonic = mnemonicString || generateMnemonicString(24);
const seed = await bip39.mnemonicToSeed(mnemonic);
const derivationPath = "m/44'/501'/0'/0'";
const derivedSeed = derivePath(derivationPath, seed.toString('hex')).key;
const keypair = SolanaKeypair.fromSeed(derivedSeed);
const publicKey = new SolanaPublickey(keypair.publicKey);
const publicKeyString = publicKey.toString();
const secretKeyString = bs58.encode(keypair.secretKey);

// TODO: add support for multiple addresses

Expand All @@ -443,6 +455,7 @@ class Wallet {
privateKey: secretKeyString,
},
],
mnemonic,
});
} else if (chain == 'TON') {
// Validate mnemonic
Expand Down

0 comments on commit 13eee87

Please sign in to comment.