Browse Source

Fix segwit to not be scanned is not supported

also refactor a bit how name get generated. more consistently to eth / xrp impls

for dev, you have a way to generate legacy account with SHOW_LEGACY_NEW_ACCOUNT=1
master
Gaëtan Renaudeau 7 years ago
parent
commit
07f204b7b9
  1. 45
      src/helpers/libcore.js

45
src/helpers/libcore.js

@ -21,8 +21,11 @@ type Props = {
onAccountScanned: AccountRaw => void, onAccountScanned: AccountRaw => void,
} }
const { SHOW_LEGACY_NEW_ACCOUNT } = process.env
export function scanAccountsOnDevice(props: Props): Promise<AccountRaw[]> { export function scanAccountsOnDevice(props: Props): Promise<AccountRaw[]> {
const { devicePath, currencyId, onAccountScanned } = props const { devicePath, currencyId, onAccountScanned } = props
const currency = getCryptoCurrencyById(currencyId)
return withDevice(devicePath)(async transport => { return withDevice(devicePath)(async transport => {
const hwApp = new Btc(transport) const hwApp = new Btc(transport)
@ -34,16 +37,25 @@ export function scanAccountsOnDevice(props: Props): Promise<AccountRaw[]> {
devicePath, devicePath,
} }
// scan segwit AND non-segwit accounts let allAccounts = []
const nonSegwitAccounts = await scanAccountsOnDeviceBySegwit({ const nonSegwitAccounts = await scanAccountsOnDeviceBySegwit({
...commonParams, ...commonParams,
showNewAccount: !!SHOW_LEGACY_NEW_ACCOUNT || !currency.supportsSegwit,
isSegwit: false, isSegwit: false,
}) })
const segwitAccounts = await scanAccountsOnDeviceBySegwit({ ...commonParams, isSegwit: true }) allAccounts = allAccounts.concat(nonSegwitAccounts)
const accounts = [...nonSegwitAccounts, ...segwitAccounts] if (currency.supportsSegwit) {
const segwitAccounts = await scanAccountsOnDeviceBySegwit({
...commonParams,
showNewAccount: true,
isSegwit: true,
})
allAccounts = allAccounts.concat(segwitAccounts)
}
return accounts return allAccounts
}) })
} }
@ -71,12 +83,14 @@ async function scanAccountsOnDeviceBySegwit({
onAccountScanned, onAccountScanned,
devicePath, devicePath,
isSegwit, isSegwit,
showNewAccount,
}: { }: {
hwApp: Object, hwApp: Object,
currencyId: string, currencyId: string,
onAccountScanned: AccountRaw => void, onAccountScanned: AccountRaw => void,
devicePath: string, devicePath: string,
isSegwit: boolean, isSegwit: boolean,
showNewAccount: boolean,
}): Promise<AccountRaw[]> { }): Promise<AccountRaw[]> {
// compute wallet identifier // compute wallet identifier
const WALLET_IDENTIFIER = await getWalletIdentifier({ hwApp, isSegwit, currencyId, devicePath }) const WALLET_IDENTIFIER = await getWalletIdentifier({ hwApp, isSegwit, currencyId, devicePath })
@ -96,6 +110,7 @@ async function scanAccountsOnDeviceBySegwit({
accounts: [], accounts: [],
onAccountScanned, onAccountScanned,
isSegwit, isSegwit,
showNewAccount,
}) })
return accounts return accounts
@ -111,6 +126,7 @@ async function scanNextAccount(props: {
accounts: AccountRaw[], accounts: AccountRaw[],
onAccountScanned: AccountRaw => void, onAccountScanned: AccountRaw => void,
isSegwit: boolean, isSegwit: boolean,
showNewAccount: boolean,
}): Promise<AccountRaw[]> { }): Promise<AccountRaw[]> {
const { const {
wallet, wallet,
@ -121,6 +137,7 @@ async function scanNextAccount(props: {
accounts, accounts,
onAccountScanned, onAccountScanned,
isSegwit, isSegwit,
showNewAccount,
} = props } = props
// TODO: investigate why importing it on file scope causes trouble // TODO: investigate why importing it on file scope causes trouble
@ -157,10 +174,10 @@ async function scanNextAccount(props: {
const isEmpty = ops.length === 0 const isEmpty = ops.length === 0
// trigger event if (!isEmpty || showNewAccount) {
onAccountScanned(account) onAccountScanned(account)
accounts.push(account)
accounts.push(account) }
// returns if the current index points on an account with no ops // returns if the current index points on an account with no ops
if (isEmpty) { if (isEmpty) {
@ -247,14 +264,18 @@ async function buildAccountRaw({
const { str: freshAddress, path: freshAddressPath } = addresses[0] const { str: freshAddress, path: freshAddressPath } = addresses[0]
const operations = ops.map(op => buildOperationRaw({ core, op, xpub })) const operations = ops.map(op => buildOperationRaw({ core, op, xpub }))
const currency = getCryptoCurrencyById(currencyId)
const name =
operations.length === 0
? `New Account ${currency.supportsSegwit && !isSegwit ? ' (legacy)' : ''}`
: `Account ${accountIndex}`
const rawAccount: AccountRaw = { const rawAccount: AccountRaw = {
id: xpub, // FIXME for account id you might want to prepend the crypto currency id to this because it's not gonna be unique. id: xpub, // FIXME for account id you might want to prepend the crypto currency id to this because it's not gonna be unique.
xpub, xpub,
path: walletPath, path: walletPath,
name: `${operations.length === 0 ? 'New ' : ''}Account ${accountIndex}${ name,
isSegwit ? ' (segwit)' : ''
}`, // TODO: placeholder name?
isSegwit, isSegwit,
freshAddress, freshAddress,
freshAddressPath, freshAddressPath,

Loading…
Cancel
Save