@@ -8,21 +8,44 @@ import {
88import {
99 apiv4_endpoint ,
1010 conetProvider ,
11- conetRpc ,
1211 localDatabaseName ,
1312} from "../utils/constants" ;
1413import contracts from "../utils/contracts" ;
1514import { CoNET_Data , setCoNET_Data } from "../utils/globals" ;
15+ import { Keypair } from "@solana/web3.js" ;
16+ import { mnemonicToSeedSync } from "bip39" ;
17+ import { sha512 } from "@noble/hashes/sha512" ;
1618
1719const PouchDB = require ( "pouchdb" ) . default ;
1820
1921let isGetFaucetProcess = false ;
2022
2123let getFaucetRoop = 0 ;
2224
25+ // Function to derive the keypair from mnemonic
26+ async function getSolanaKeypairFromMnemonic ( mnemonic : string ) {
27+ // Convert mnemonic to seed
28+ const seed = mnemonicToSeedSync ( mnemonic ) ;
29+
30+ // Derive the seed for the first account using Solana's HD wallet path
31+ const derivedSeed = await deriveSolanaSeed ( seed ) ;
32+
33+ // Generate a Keypair from the derived seed
34+ return Keypair . fromSeed ( derivedSeed ) ;
35+ }
36+
37+ // Function to derive the seed for the given derivation path
38+ async function deriveSolanaSeed ( seed : any ) {
39+ // Derive a 32-byte key from the seed using SHA512 (Solana's derivation process)
40+ const hash = sha512 ( seed ) ;
41+ return hash . slice ( 0 , 32 ) ; // Take the first 32 bytes as the private key
42+ }
43+
2344const createOrGetWallet = async ( secretPhrase : string | null ) => {
2445 await checkStorage ( ) ;
2546
47+ if ( secretPhrase ) setCoNET_Data ( null ) ;
48+
2649 if ( ! CoNET_Data || ! CoNET_Data ?. profiles ) {
2750 const acc = createKeyHDWallets ( secretPhrase ) ;
2851
@@ -44,6 +67,7 @@ const createOrGetWallet = async (secretPhrase: string | null) => {
4467 privateKeyArmor : acc . signingKey . privateKey ,
4568 hdPath : acc . path ,
4669 index : acc . index ,
70+ type : "ethereum" ,
4771 } ;
4872
4973 const data : any = {
@@ -54,55 +78,64 @@ const createOrGetWallet = async (secretPhrase: string | null) => {
5478 nonce : 0 ,
5579 } ;
5680
57- const primaryWallet = ethers . Wallet . fromPhrase ( data . mnemonicPhrase ) ;
58- const secondaryWallet = primaryWallet . deriveChild ( 0 ) ;
59-
60- const profile2 : profile = {
61- tokens : initProfileTokens ( ) ,
62- publicKeyArmor : secondaryWallet . publicKey ,
63- keyID : secondaryWallet . address ,
64- isPrimary : true ,
65- referrer : null ,
66- isNode : false ,
67- pgpKey : {
68- privateKeyArmor : key . privateKey ,
69- publicKeyArmor : key . publicKey ,
70- } ,
71- privateKeyArmor : secondaryWallet . signingKey . privateKey ,
72- hdPath : secondaryWallet . path ,
73- index : secondaryWallet . index ,
74- } ;
75-
76- data . profiles . push ( profile2 ) ;
81+ if ( acc ?. mnemonic ?. phrase ) {
82+ const secondaryWallet : Keypair = await getSolanaKeypairFromMnemonic (
83+ acc ?. mnemonic ?. phrase
84+ ) ;
85+
86+ const profile2 : profile = {
87+ tokens : initProfileTokens ( ) ,
88+ publicKeyArmor : secondaryWallet . publicKey . toString ( ) ,
89+ keyID : secondaryWallet . publicKey . toBase58 ( ) ,
90+ isPrimary : true ,
91+ referrer : null ,
92+ isNode : false ,
93+ pgpKey : {
94+ privateKeyArmor : key . privateKey ,
95+ publicKeyArmor : key . publicKey ,
96+ } ,
97+ privateKeyArmor : secondaryWallet . secretKey . toString ( ) ,
98+ hdPath : null ,
99+ index : 0 ,
100+ type : "solana" ,
101+ } ;
102+
103+ data . profiles . push ( profile2 ) ;
104+ }
77105
78106 setCoNET_Data ( data ) ;
79107 }
80108
81109 const tmpData = CoNET_Data ;
82-
83- if ( tmpData && tmpData ?. profiles . length < 2 ) {
84- const primaryWallet = ethers . Wallet . fromPhrase ( tmpData . mnemonicPhrase ) ;
85- const secondaryWallet = primaryWallet . deriveChild ( 0 ) ;
86-
110+ if ( tmpData ) tmpData . profiles . length = 2 ;
111+
112+ if (
113+ tmpData &&
114+ ( tmpData ?. profiles . length < 2 || tmpData ?. profiles [ 1 ] ?. type !== "solana" )
115+ ) {
116+ const secondaryWallet = await getSolanaKeypairFromMnemonic (
117+ tmpData . mnemonicPhrase
118+ ) ;
87119 const key = await createGPGKey ( "" , "" , "" ) ;
88120
89121 const profile2 : profile = {
90122 tokens : initProfileTokens ( ) ,
91- publicKeyArmor : secondaryWallet . publicKey ,
92- keyID : secondaryWallet . address ,
123+ publicKeyArmor : secondaryWallet . publicKey . toString ( ) ,
124+ keyID : secondaryWallet . publicKey . toBase58 ( ) ,
93125 isPrimary : true ,
94126 referrer : null ,
95127 isNode : false ,
96128 pgpKey : {
97129 privateKeyArmor : key . privateKey ,
98130 publicKeyArmor : key . publicKey ,
99131 } ,
100- privateKeyArmor : secondaryWallet . signingKey . privateKey ,
101- hdPath : secondaryWallet . path ,
102- index : secondaryWallet . index ,
132+ privateKeyArmor : secondaryWallet . secretKey . toString ( ) ,
133+ hdPath : null ,
134+ index : 0 ,
135+ type : "solana" ,
103136 } ;
104137
105- tmpData . profiles . push ( profile2 ) ;
138+ tmpData . profiles [ 1 ] = profile2 ;
106139 }
107140
108141 tmpData ?. profiles . forEach ( async ( n : profile ) => {
0 commit comments