Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warnings before paratime transactions #236

Merged
merged 9 commits into from
Jan 22, 2022
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@ledgerhq/hw-transport-webusb": "6.11.2",
"@metamask/browser-passworder": "3.0.0",
"@metamask/jazzicon": "2.0.0",
"@metamask/obs-store": "^7.0.0",
"@oasisprotocol/client": "0.1.0-alpha7",
"@oasisprotocol/client-ext-utils": "0.1.0-alpha3",
"@oasisprotocol/client-rt": "0.2.0-alpha10",
Expand All @@ -28,7 +29,6 @@
"ethereumjs-util": "7.1.3",
"extensionizer": "1.0.1",
"i18next": "19.8.4",
"obs-store": "4.0.3",
"prop-types": "15.7.2",
"qrcode-generator": "1.4.4",
"react": "17.0.1",
Expand Down
72 changes: 53 additions & 19 deletions src/background/service/APIService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,37 @@ import { amountDecimals, getEvmBech32Address, getExplorerUrl, getRuntimeConfig,
import { getRuntimeTxDetail, getSubmitStatus } from '../api';
import { buildParatimeTxBody, buildTxBody, getChainContext, submitTx } from '../api/txHelper';
import { get, removeValue, save } from '../storage/storageService';
import { ObservableStore } from '@metamask/obs-store'

const EthUtils = require('ethereumjs-util');
const ObservableStore = require('obs-store')
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 {{
* accounts: {
* commonList: Account[],
* evmList: Account[],
* },
* currentAddress: string,
* }} GetAllAccountsResponse
* @typedef {{
* accounts: {
* commonList: Account[],
* },
* currentAddress: string,
* }} GetAllAccountsDappResponse
*/

const default_account_name = "Account 1"
class APIService {
Expand Down Expand Up @@ -98,12 +123,22 @@ class APIService {
initLockedState=()=>{
return {
isUnlocked: false,
data: '',
/**
* @type { Array<{
* mnemonic: string;
* accounts: Account[];
* currentAddress: string;
* }> }
*/
data: /** @type {any} */ (undefined),
password: '',
currentAccount: {},
/**
* @type {Account}
*/
currentAccount: /** @type {any} */ ({}),
mne: ""
};
}
}

resetWallet=()=>{
this.memStore.putState(this.initLockedState())
Expand Down Expand Up @@ -162,6 +197,9 @@ class APIService {

return this.getAccountWithoutPrivate(account)
}
/**
* @returns {GetAllAccountsResponse | GetAllAccountsDappResponse}
*/
getAllAccount = (isDapp = false) => {
let data = this.getStore().data
let accountList = data[0].accounts
Expand All @@ -176,6 +214,7 @@ class APIService {
currentAddress: currentAccount.address
}
}
/** @param {Account[]} accountList */
accountSort = (accountList, isDapp = false) => {
let newList = accountList
let commonList = []
Expand Down Expand Up @@ -353,14 +392,13 @@ class APIService {
let importList = accounts.filter((item, index) => {
return item.type === currentAccountType
})
let typeIndex = ""
if (importList.length == 0) {
typeIndex = 1
} else {
let typeIndex = 1
if (importList.length > 0) {
typeIndex = importList[importList.length - 1].typeIndex + 1
}

let privKeyEncrypt = await this.encryptor.encrypt(this.getStore().password, wallet.privKey_hex)
/** @type {Account} */
let account = {
address: wallet.address,
privateKey: privKeyEncrypt,
Expand Down Expand Up @@ -397,10 +435,8 @@ class APIService {
let ledgerList = accounts.filter((item, index) => {
return item.type === ACCOUNT_TYPE.WALLET_LEDGER
})
let typeIndex = ""
if (ledgerList.length === 0) {
typeIndex = 1
} else {
let typeIndex = 1
if (ledgerList.length > 0) {
typeIndex = ledgerList[ledgerList.length - 1].typeIndex + 1
}
let ledgerAccountList = []
Expand All @@ -416,7 +452,7 @@ class APIService {
ledgerHdIndex: ledgerAccount.ledgerHdIndex,
type: ACCOUNT_TYPE.WALLET_LEDGER,
accountName: !index ? accountName : accountName + "-" + index,
typeIndex: parseInt(typeIndex) + index
typeIndex: typeIndex + index
})

}
Expand Down Expand Up @@ -456,14 +492,12 @@ class APIService {
if (error.error) {
return error
}
let ledgerList = accounts.filter((item, index) => {
let observeList = accounts.filter((item, index) => {
return item.type === ACCOUNT_TYPE.WALLET_OBSERVE
})
let typeIndex = ""
if (ledgerList.length === 0) {
typeIndex = 1
} else {
typeIndex = ledgerList[ledgerList.length - 1].typeIndex + 1
let typeIndex = 1
if (observeList.length > 0) {
typeIndex = observeList[observeList.length - 1].typeIndex + 1
}

const account = {
Expand Down
2 changes: 1 addition & 1 deletion src/background/service/ExtDappService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import extension from 'extensionizer';
import ObservableStore from 'obs-store';
import { ObservableStore } from '@metamask/obs-store';
import { QUERY_TAB_TYPE } from '../../constant/specifyType';
import { BACKGROUND_KEYS_CHANGED, DAPP_ACTION_CLOSE_WINDOW, DAPP_ACTION_GET_ACCOUNT, DAPP_ACTION_SEND_TRANSACTION, DAPP_CLOSE_POPUP_WINDOW, FROM_BACK_TO_RECORD } from '../../constant/types';
import { getActiveTab, sendMsg } from '../../utils/commonMsg';
Expand Down
13 changes: 7 additions & 6 deletions src/constant/walletType.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* Wallet type
*/
export const ACCOUNT_TYPE = {
WALLET_INSIDE: "WALLET_INSIDE",
WALLET_OUTSIDE: "WALLET_OUTSIDE",
WALLET_LEDGER: "WALLET_LEDGER",
WALLET_OBSERVE: "WALLET_OBSERVE",
WALLET_INSIDE: /** @type {const} */ ("WALLET_INSIDE"), // Generated from mnemonic
WALLET_OUTSIDE: /** @type {const} */ ("WALLET_OUTSIDE"), // Imported from private key
WALLET_LEDGER: /** @type {const} */ ("WALLET_LEDGER"), // Ledger
WALLET_OBSERVE: /** @type {const} */ ("WALLET_OBSERVE"), // Watch

WALLET_OUTSIDE_SECP256K1: "WALLET_OUTSIDE_SECP256K1",

WALLET_OUTSIDE_SECP256K1: /** @type {const} */ ("WALLET_OUTSIDE_SECP256K1"), // Imported from metamask private key
};
/**
* type on the account management page
Expand Down Expand Up @@ -62,4 +63,4 @@ export const TRANSACTION_TYPE = {
export const TRANSACTION_RUNTIME_TYPE = {
Deposit: "consensus.Deposit",
Withdraw: "consensus.Withdraw",
};
};
7 changes: 5 additions & 2 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
"sendDetail": "Send Details",
"sendAddressError": "Please enter a valid wallet address",
"amountError": "The format of the amount you entered is incorrect, please check and re-enter",
"confirmSendingToValidatorDescription": "This is a validator wallet address. Transfers to this address do not stake your funds with the validator.",
"confirmSendingToValidatorTitle": "Are you sure you want to continue?",
"confirmTransferringToValidator": "This is a validator wallet address. Transfers to this address do not stake your funds with the validator.",
pro-wh marked this conversation as resolved.
Show resolved Hide resolved
"confirmWantToContinue": "Are you sure you want to continue?",
"confirmWithdrawingFromParatimeToForeignAccount": "Destination account is not in your wallet! Some automated systems, e.g., those used for tracking exchange deposits, may be unable to accept funds through ParaTime withdrawals. For better compatibility, cancel, withdraw into your own account, and transfer from there.",
"confirmDepositingToParatimeToForeignAccount": "Destination account is not in your wallet! We recommend you always deposit into your own ParaTime account, then transfer from there.",
"confirmDepositingToParatimeToLedgerAccount": "Destination account was imported from Ledger! Ledger accounts do not support withdrawing from ParaTime.",
"txHash": "Transaction Hash",
"receiveTitle": "Receive",
"walletAddress": "Wallet Address",
Expand Down
10 changes: 9 additions & 1 deletion src/popup/pages/Paratime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class Paratime extends React.Component {
onClickWithdraw = (item) => {
let { currentAccount } = this.props
if (currentAccount.type === ACCOUNT_TYPE.WALLET_OBSERVE) {
Toast.info(getLanguage('observeAccountTip'))
return
}
if (currentAccount.type === ACCOUNT_TYPE.WALLET_LEDGER) {
Toast.info(getLanguage('ledgerNotSupportTip'))
return
}
Expand All @@ -166,6 +170,10 @@ class Paratime extends React.Component {
onClickDeposit = (item) => {
let { currentAccount } = this.props
if (currentAccount.type === ACCOUNT_TYPE.WALLET_OBSERVE) {
Toast.info(getLanguage('observeAccountTip'))
return
}
if (currentAccount.type === ACCOUNT_TYPE.WALLET_LEDGER) {
Toast.info(getLanguage('ledgerNotSupportTip'))
return
}
Expand Down Expand Up @@ -274,4 +282,4 @@ function mapDispatchToProps(dispatch) {
};
}

export default connect(mapStateToProps, mapDispatchToProps)(Paratime);
export default connect(mapStateToProps, mapDispatchToProps)(Paratime);
Loading