From 13eee8773fdd0d1de1829885d5fced2848b0d668 Mon Sep 17 00:00:00 2001 From: yerofey Date: Sun, 4 Feb 2024 11:55:09 +0100 Subject: [PATCH] Add support for `SOL` from mnemonic --- package.json | 3 ++- pnpm-lock.yaml | 20 +++++++++++++++++++- src/Wallet.js | 21 +++++++++++++++++---- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index c42b18c..9385038 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 098abaa..aa67970 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -59,6 +59,9 @@ dependencies: dotenv: specifier: ^16.4.1 version: 16.4.1 + ed25519-hd-key: + specifier: ^1.3.0 + version: 1.3.0 eth-lib: specifier: 0.1.29 version: 0.1.29 @@ -3580,6 +3583,14 @@ packages: resolution: {integrity: sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w==} engines: {node: '>=0.4.0'} hasBin: true + dev: true + + /acorn@8.11.3: + resolution: {integrity: sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==} + engines: {node: '>=0.4.0'} + hasBin: true + dev: false + optional: true /aes-js@3.1.2: resolution: {integrity: sha512-e5pEa2kBnBOgR4Y/p20pskXI74UEz7de8ZGVo58asOtvSVG5YAbJeELPZxOmt+Bnz3rX753YKhfIn4X4l1PPRQ==} @@ -5441,6 +5452,13 @@ packages: secp256k1: 3.8.0 dev: false + /ed25519-hd-key@1.3.0: + resolution: {integrity: sha512-IWwAyiiuJQhgu3L8NaHb68eJxTu2pgCwxIBdgpLJdKpYZM46+AXePSVTr7fkNKaUOfOL4IrjEUaQvyVRIDP7fg==} + dependencies: + create-hmac: 1.1.7 + tweetnacl: 1.0.3 + dev: false + /ee-first@1.1.1: resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==} dev: false @@ -10420,7 +10438,7 @@ packages: hasBin: true dependencies: '@jridgewell/source-map': 0.3.5 - acorn: 8.11.2 + acorn: 8.11.3 commander: 2.20.3 source-map-support: 0.5.21 dev: false diff --git a/src/Wallet.js b/src/Wallet.js index ac73b74..f4aed33 100644 --- a/src/Wallet.js +++ b/src/Wallet.js @@ -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 { @@ -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 @@ -443,6 +455,7 @@ class Wallet { privateKey: secretKeyString, }, ], + mnemonic, }); } else if (chain == 'TON') { // Validate mnemonic