Browse Source

Flow

master
meriadec 7 years ago
parent
commit
ac807aea8a
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 86
      src/internals/usb/wallet/scanAccountsOnDevice.js

86
src/internals/usb/wallet/scanAccountsOnDevice.js

@ -14,6 +14,7 @@ import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies'
import type { AccountRaw } from '@ledgerhq/live-common/lib/types'
import type { NJSAccount } from 'ledger-core/src/ledgercore_doc'
import { toHexInt, encodeBase58Check } from 'helpers/generic'
@ -36,7 +37,7 @@ async function getOrCreateWallet(currencyId) {
}
}
async function scanNextAccount({ wallet, hwApp, accountIndex = 0, accountsCount, accounts = [] }) {
async function scanNextAccount({ wallet, hwApp, accountIndex, accountsCount, accounts }) {
const core = require('ledger-core')
// create account only if account has not been scanned yet
@ -64,44 +65,60 @@ async function scanNextAccount({ wallet, hwApp, accountIndex = 0, accountsCount,
hwApp,
accountIndex: accountIndex + 1,
accounts,
accountsCount,
})
}
export default async function scanAccountsOnDevice(props: Props): AccountRaw[] {
try {
const core = require('ledger-core')
const { devicePath, currencyId } = props
const wallet = await getOrCreateWallet(currencyId)
const accountsCount = await wallet.getAccountCount()
const transport = await CommNodeHid.open(devicePath)
const hwApp = new Btc(transport)
// retrieve native accounts
const njsAccounts = await scanNextAccount({ wallet, hwApp, accountsCount })
// create AccountRaw[]
const rawAccounts = await njsAccounts.reduce(async (promise, njsAccount, i) => {
const rawAccounts = await promise
const rawAccount = await buildRawAccount({
njsAccount,
accountIndex: i,
wallet,
currencyId,
core,
hwApp,
})
return [...rawAccounts, rawAccount]
}, Promise.resolve([]))
return rawAccounts
} catch (err) {
console.log(err)
return []
const core = require('ledger-core')
const { devicePath, currencyId } = props
const wallet = await getOrCreateWallet(currencyId)
const accountsCount = await wallet.getAccountCount()
const transport = await CommNodeHid.open(devicePath)
const hwApp = new Btc(transport)
// retrieve native accounts
const njsAccounts = await scanNextAccount({
wallet,
hwApp,
accountsCount,
accountIndex: 0,
accounts: [],
})
// create AccountRaw[], looping on every njsAccount and transform it to an AccountRaw
const rawAccounts = []
for (let i = 0; i < njsAccounts.length; i++) {
const rawAccount = await buildRawAccount({
njsAccount: njsAccounts[i],
accountIndex: i,
wallet,
currencyId,
core,
hwApp,
})
rawAccounts.push(rawAccount)
}
return rawAccounts
}
async function buildRawAccount({ njsAccount, wallet, currencyId, core, hwApp, accountIndex }) {
async function buildRawAccount({
njsAccount,
wallet,
currencyId,
core,
hwApp,
accountIndex,
}: {
njsAccount: NJSAccount,
wallet: NJSWallet,
currencyId: string,
accountIndex: number,
core: Object,
hwApp: Object,
}) {
const jsCurrency = getCryptoCurrencyById(currencyId)
// retrieve xpub
@ -116,8 +133,12 @@ async function buildRawAccount({ njsAccount, wallet, currencyId, core, hwApp, ac
const network = Buffer.from(bitcoinLikeNetworkParameters.XPUBVersion).readUIntBE(0, 4)
const xpub = createXPUB(depth, fingerprint, childNum, chainCode, publicKey, network)
// blockHeight
const { height: blockHeight } = await njsAccount.getLastBlock()
// get a bunch of fresh addresses
const addresses = []
const rawAccount: AccountRaw = {
id: xpub,
xpub,
@ -125,6 +146,7 @@ async function buildRawAccount({ njsAccount, wallet, currencyId, core, hwApp, ac
rootPath: walletPath, // TODO: this should be `walletPath` in Account/AccountRaw types
name: '', // TODO: placeholder name?
address: bitcoinAddress, // TODO: discuss about the utility of storing it here
addresses,
balance: 0,
blockHeight,
archived: false,

Loading…
Cancel
Save