-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove gaia call when unlocking wallet
closes #1877
- Loading branch information
1 parent
6e2933a
commit b717b03
Showing
8 changed files
with
182 additions
and
13 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
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,36 @@ | ||
import { decryptMnemonic } from '@background/crypto/mnemonic-encryption'; | ||
import { getWallet } from './wallet-utils'; | ||
|
||
export async function getDecryptedWalletDetails( | ||
encryptedSecretKey: string, | ||
password: string, | ||
salt: string | undefined | ||
) { | ||
const hasSetPassword = password !== undefined; | ||
const decryptedData = await decryptMnemonic({ | ||
encryptedSecretKey, | ||
password, | ||
salt, | ||
}); | ||
|
||
const keyInfo = { | ||
secretKey: decryptedData.secretKey, | ||
encryptedSecretKey: encryptedSecretKey, | ||
currentAccountIndex: 0, | ||
hasSetPassword, | ||
}; | ||
|
||
const wallet = await getWallet({ | ||
secretKey: decryptedData.secretKey, | ||
salt: decryptedData.salt, | ||
password, | ||
}); | ||
|
||
const result = { | ||
...keyInfo, | ||
wallet, | ||
salt: decryptedData.salt, | ||
encryptedSecretKey: decryptedData.encryptedSecretKey, | ||
}; | ||
return result; | ||
} |
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,80 @@ | ||
import { Account, deriveAccount, generateWallet, WalletConfig } from '@stacks/wallet-sdk'; | ||
import { Wallet as SDKWallet } from '@stacks/wallet-sdk'; | ||
import { mnemonicToSeed } from 'bip39'; | ||
import { fromSeed } from 'bip32'; | ||
import { BIP32Interface } from 'bitcoinjs-lib'; | ||
|
||
function getSavedWalletConfig() { | ||
const walletConfig = localStorage.getItem('walletConfig'); | ||
if (typeof walletConfig !== 'string') return; | ||
try { | ||
return JSON.parse(walletConfig) as WalletConfig; | ||
} catch (e) { | ||
return; | ||
} | ||
} | ||
|
||
function accountsFromWalletConfig( | ||
walletConfig: WalletConfig, | ||
rootNode: BIP32Interface, | ||
salt: string | ||
) { | ||
return walletConfig.accounts.map((account, index) => { | ||
const existingAccount = deriveAccount({ | ||
rootNode, | ||
index, | ||
salt, | ||
}); | ||
return { | ||
...existingAccount, | ||
username: account.username, | ||
}; | ||
}); | ||
} | ||
|
||
async function rootNodeFromSecretKey(secretKey: string) { | ||
const rootPrivateKey = await mnemonicToSeed(secretKey); | ||
return fromSeed(rootPrivateKey); | ||
} | ||
|
||
async function getSavedWalletAccounts({ | ||
secretKey, | ||
walletConfig, | ||
salt, | ||
}: { | ||
secretKey: string; | ||
walletConfig: WalletConfig; | ||
salt: string; | ||
}): Promise<Account[]> { | ||
// Restore from existing config | ||
const rootNode = await rootNodeFromSecretKey(secretKey); | ||
if (!walletConfig) return [] as Account[]; | ||
return accountsFromWalletConfig(walletConfig, rootNode, salt); | ||
} | ||
|
||
interface GetWalletParams { | ||
secretKey: string; | ||
salt: string; | ||
password: string; | ||
} | ||
|
||
export async function getWallet(params: GetWalletParams): Promise<SDKWallet | undefined> { | ||
const { secretKey, salt, password } = params; | ||
const wallet = await generateWallet({ | ||
secretKey, | ||
password, | ||
}); | ||
|
||
const walletConfig = getSavedWalletConfig(); | ||
if (!walletConfig) return; | ||
|
||
const accounts = await getSavedWalletAccounts({ | ||
secretKey, | ||
walletConfig, | ||
salt, | ||
}); | ||
|
||
if (accounts.length === 0) return wallet; | ||
|
||
return { ...wallet, accounts }; | ||
} |
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,26 @@ | ||
import { setToLocalstorageIfDefined } from '@common/storage'; | ||
import { WalletConfig } from '@stacks/wallet-sdk'; | ||
import { createWalletGaiaConfig, getOrCreateWalletConfig, Wallet } from '@stacks/wallet-sdk'; | ||
import { gaiaUrl } from '@common/constants'; | ||
import { logger } from '@common/logger'; | ||
|
||
export function saveWalletConfigLocally(walletConfig: WalletConfig) { | ||
setToLocalstorageIfDefined('walletConfig', JSON.stringify(walletConfig)); | ||
return walletConfig; | ||
} | ||
|
||
export async function getWalletConfig(wallet: Wallet) { | ||
try { | ||
const gaiaHubConfig = await createWalletGaiaConfig({ gaiaHubUrl: gaiaUrl, wallet }); | ||
const walletConfig = await getOrCreateWalletConfig({ | ||
wallet, | ||
gaiaHubConfig, | ||
skipUpload: true, | ||
}); | ||
saveWalletConfigLocally(walletConfig); | ||
return walletConfig; | ||
} catch (e) { | ||
logger.error('useWalletConfig error', e); | ||
return; | ||
} | ||
} |
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