Skip to content

Commit

Permalink
Refactor types in APIService
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Nov 21, 2023
1 parent 6e8fa81 commit 15f2638
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
56 changes: 32 additions & 24 deletions src/background/service/APIService.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ const encryptUtils = require('@metamask/browser-passworder')
const RETRY_TIME = 4

/**
* @typedef {{
* address: string;
* evmAddress?: string;
* privateKey?: string;
* publicKey: string;
* type: keyof typeof ACCOUNT_TYPE;
* hdPath: number;
* accountName: string;
* typeIndex: number;
* }} Account
* @typedef {EncryptedData['encryptedType'][number]['accounts'][number]} Account
* @typedef {{
* accounts: {
* commonList: Account[],
Expand All @@ -54,6 +45,13 @@ class APIService {
this.memStore = new ObservableStore(this.initLockedState())
this.statusTimer = {}
this.runtimeStatusTimer = {}
/**
* @type {{
* encrypt<T>(password: string, data: T): Promise<EncryptedString<T>>
* decrypt<T>(password: string, encrypted: EncryptedString<T>): Promise<T>
* }}
*/
// @ts-expect-error Forcefully extending encrypted string type
this.encryptor = encryptUtils
}
getStore = () => {
Expand Down Expand Up @@ -124,11 +122,7 @@ class APIService {
return {
isUnlocked: false,
/**
* @type { Array<{
* mnemonic: string;
* accounts: Account[];
* currentAddress: string;
* }> }
* @type { EncryptedData['encryptedType'] }
*/
data: /** @type {any} */ (undefined),
password: '',
Expand Down Expand Up @@ -258,6 +252,7 @@ class APIService {
return wallet
}

/** @param {string} accountName */
addHDNewAccount = async (accountName) => {
let data = this.getStore().data
let accounts = data[0].accounts
Expand Down Expand Up @@ -365,7 +360,8 @@ class APIService {
}
}
/**
* import wallet by private key
* import wallet by private key
* @param {typeof ACCOUNT_TYPE.WALLET_OUTSIDE | typeof ACCOUNT_TYPE.WALLET_OUTSIDE_SECP256K1} accountType
*/
addImportAccount = async (privateKey, accountName,accountType) => {
try {
Expand Down Expand Up @@ -400,16 +396,21 @@ class APIService {

let privKeyEncrypt = await this.encryptor.encrypt(this.getStore().password, wallet.privKey_hex)
/** @type {Account} */
let account = {
let account = currentAccountType === ACCOUNT_TYPE.WALLET_OUTSIDE_SECP256K1 ? {
address: wallet.address,
evmAddress: wallet.evmAddress,
privateKey: privKeyEncrypt,
publicKey: wallet.publicKey,
type: currentAccountType,
accountName,
typeIndex
} : {
address: wallet.address,
privateKey: privKeyEncrypt,
publicKey: wallet.publicKey,
type: currentAccountType,
accountName,
typeIndex
}
if(wallet.evmAddress){
account.evmAddress = wallet.evmAddress
}
data[0].currentAddress = account.address
data[0].accounts.push(account)
Expand All @@ -426,6 +427,13 @@ class APIService {
}
/**
* import ledger wallet
* @param {Array<{
* ledgerHdIndex: number;
* path: number[];
* publicKey: string;
* address: string;
* }>} addressList
* @param {string} accountName
*/
addLedgerAccount = async (addressList, accountName) => {
try {
Expand Down Expand Up @@ -611,13 +619,13 @@ class APIService {
let newAccounts = []
for (let index = 0; index < accounts.length; index++) {
const account = accounts[index];
let privateKeyEn = account.privateKey
if (privateKeyEn) {
if ('privateKey' in account && account.privateKey) {
const privateKeyEn = account.privateKey
let privateKey = await this.encryptor.decrypt(oldPwd, privateKeyEn)
privateKey = await this.encryptor.encrypt(pwd, privateKey)
const newPrivateKeyEn = await this.encryptor.encrypt(pwd, privateKey)
newAccounts.push({
...account,
privateKey,
privateKey: newPrivateKeyEn,
})
} else {
newAccounts.push({
Expand Down
15 changes: 9 additions & 6 deletions src/background/storage/storageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const extensionStorage = extension.storage && extension.storage.local

/**
* Stored in local storage
* @param {{keyringData: EncryptedData}} value
*/
export function save(value) {
return extensionStorage.set(value, () => {
Expand All @@ -16,26 +17,28 @@ export function save(value) {

/**
* Get the local stored value
* @param {*} value
* @param {'keyringData'} key
* @returns {Promise<{keyringData: EncryptedData}>}
*/
export function get(value) {
export function get(key) {
return new Promise((resolve, reject) => {
extensionStorage.get(value, items => {
extensionStorage.get(key, items => {
let error = extension.runtime.lastError
if (error) {
reject(error);
}
// @ts-expect-error Forcefully extending encrypted string type
resolve(items);
});
});
}

/**
* Remove local stored value
* @param {*} value
* @param {'keyringData'} key
*/
export function removeValue(value) {
return extensionStorage.remove(value, () => {
export function removeValue(key) {
return extensionStorage.remove(key, () => {
let error = extension.runtime.lastError
if (error) {
throw error;
Expand Down
5 changes: 5 additions & 0 deletions src/popup/pages/LedgerAddresses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class LedgerAddresses extends React.Component {
})
}
}
/**
* @template {{address: string, isSelect: boolean, isExist: boolean}} T
* @param {T[]} list
* @returns T[]
*/
addressFilter = (list) => {
let newList = []
let selectList = this.state.selectList.map((item) => {
Expand Down

0 comments on commit 15f2638

Please sign in to comment.