Browse Source

Calculate balance by date

master
meriadec 7 years ago
parent
commit
89643860ff
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 1
      .flowconfig
  2. 2
      package.json
  3. 20
      src/components/DashboardPage/index.js
  4. 1
      src/internals/accounts/index.js
  5. 140
      src/internals/accounts/scanAccountsOnDevice.js
  6. 4
      src/internals/devices/checkIfAppOpened.js
  7. 4
      yarn.lock

1
.flowconfig

@ -1,6 +1,5 @@
[ignore] [ignore]
<PROJECT_ROOT>/node_modules/bcryptjs/src/bower.json <PROJECT_ROOT>/node_modules/bcryptjs/src/bower.json
<PROJECT_ROOT>/node_modules/@ledgerhq/hw-app-btc/lib/Btc.js.flow
<PROJECT_ROOT>/node_modules/config-chain/test/broken.json <PROJECT_ROOT>/node_modules/config-chain/test/broken.json
<PROJECT_ROOT>/node_modules/npm/node_modules/config-chain/test/broken.json <PROJECT_ROOT>/node_modules/npm/node_modules/config-chain/test/broken.json

2
package.json

@ -57,7 +57,7 @@
"i18next": "^11.2.2", "i18next": "^11.2.2",
"i18next-node-fs-backend": "^1.0.0", "i18next-node-fs-backend": "^1.0.0",
"invariant": "^2.2.4", "invariant": "^2.2.4",
"ledger-core": "meriadec/lib-ledger-core-node-bindings#5236e84", "ledger-core": "meriadec/lib-ledger-core-node-bindings#697ec09",
"lodash": "^4.17.5", "lodash": "^4.17.5",
"moment": "^2.22.1", "moment": "^2.22.1",
"object-path": "^0.11.4", "object-path": "^0.11.4",

20
src/components/DashboardPage/index.js

@ -16,6 +16,8 @@ import {
import type { Account } from '@ledgerhq/live-common/lib/types' import type { Account } from '@ledgerhq/live-common/lib/types'
import type { T } from 'types/common' import type { T } from 'types/common'
import runJob from 'renderer/runJob'
import { colors } from 'styles/theme' import { colors } from 'styles/theme'
import { getVisibleAccounts } from 'reducers/accounts' import { getVisibleAccounts } from 'reducers/accounts'
@ -138,6 +140,24 @@ class DashboardPage extends PureComponent<Props, State> {
return ( return (
<Box flow={7}> <Box flow={7}>
<button
onClick={async () => {
console.log(`syncing the accounts`)
const accounts = await runJob({
channel: 'accounts',
job: 'scan',
successResponse: 'accounts.scanAccountsOnDevice.success',
errorResponse: 'accounts.scanAccountsOnDevice.fail',
data: {
devicePath: '/dev/hidraw0',
currencyId: 'bitcoin_testnet',
},
})
console.log(accounts)
}}
>
sync the accounts
</button>
<Box horizontal alignItems="flex-end"> <Box horizontal alignItems="flex-end">
<Box grow> <Box grow>
<Text color="dark" ff="Museo Sans" fontSize={7}> <Text color="dark" ff="Museo Sans" fontSize={7}>

1
src/internals/accounts/index.js

@ -32,6 +32,7 @@ export default {
}) })
send('accounts.scanAccountsOnDevice.success', accounts) send('accounts.scanAccountsOnDevice.success', accounts)
} catch (err) { } catch (err) {
console.log(err)
send('accounts.scanAccountsOnDevice.fail', formatErr(err)) send('accounts.scanAccountsOnDevice.fail', formatErr(err))
} }
}, },

140
src/internals/accounts/scanAccountsOnDevice.js

@ -13,8 +13,10 @@ import padStart from 'lodash/padStart'
import CommNodeHid from '@ledgerhq/hw-transport-node-hid' import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies'
import type Transport from '@ledgerhq/hw-transport'
import type { AccountRaw } from '@ledgerhq/live-common/lib/types' import type { AccountRaw } from '@ledgerhq/live-common/lib/types'
import type { NJSAccount } from 'ledger-core/src/ledgercore_doc' import type { NJSAccount, NJSOperation } from 'ledger-core/src/ledgercore_doc'
import { toHexInt, encodeBase58Check } from 'helpers/generic' import { toHexInt, encodeBase58Check } from 'helpers/generic'
@ -30,7 +32,7 @@ export default async function scanAccountsOnDevice(props: Props): Promise<Accoun
const { devicePath, currencyId, onAccountScanned } = props const { devicePath, currencyId, onAccountScanned } = props
// instanciate app on device // instanciate app on device
const transport = await CommNodeHid.open(devicePath) const transport: Transport<*> = await CommNodeHid.open(devicePath)
const hwApp = new Btc(transport) const hwApp = new Btc(transport)
const commonParams = { const commonParams = {
@ -115,9 +117,7 @@ async function scanNextAccount(props) {
const query = njsAccount.queryOperations() const query = njsAccount.queryOperations()
const ops = await query.limit(OPS_LIMIT).execute() const ops = await query.limit(OPS_LIMIT).execute()
console.log(`found ${ops.length} transactions`) const account = await buildAccountRaw({
const account = await buildRawAccount({
njsAccount, njsAccount,
isSegwit, isSegwit,
accountIndex, accountIndex,
@ -161,7 +161,7 @@ async function getOrCreateWallet(WALLET_IDENTIFIER, currencyId, isSegwit) {
} }
} }
async function buildRawAccount({ async function buildAccountRaw({
njsAccount, njsAccount,
isSegwit, isSegwit,
wallet, wallet,
@ -182,13 +182,13 @@ async function buildRawAccount({
// $FlowFixMe // $FlowFixMe
ops: NJSOperation[], ops: NJSOperation[],
}) { }) {
const njsBalanceHistory = await njsAccount.getBalanceHistory( const balanceByDay = ops.length
new Date('2018-05-01').toISOString(), ? await getBalanceByDaySinceOperation({
new Date('2018-06-01').toISOString(), njsAccount,
core.TIME_PERIODS.DAY, njsOperation: ops[0],
) core,
})
const balanceHistory = njsBalanceHistory.map(njsAmount => njsAmount.toLong()) : {}
const njsBalance = await njsAccount.getBalance() const njsBalance = await njsAccount.getBalance()
const balance = njsBalance.toLong() const balance = njsBalance.toLong()
@ -199,26 +199,10 @@ async function buildRawAccount({
const { derivations } = await wallet.getAccountCreationInfo(accountIndex) const { derivations } = await wallet.getAccountCreationInfo(accountIndex)
const [walletPath, accountPath] = derivations const [walletPath, accountPath] = derivations
console.log(`so, the account path is ${accountPath}`)
const isVerify = false const isVerify = false
const { publicKey, chainCode, bitcoinAddress } = await hwApp.getWalletPublicKey( const { bitcoinAddress } = await hwApp.getWalletPublicKey(accountPath, isVerify, isSegwit)
accountPath,
isVerify,
isSegwit,
)
const nativeDerivationPath = core.createDerivationPath(accountPath) const xpub = 'abcd'
const depth = nativeDerivationPath.getDepth()
// const depth = 'depth'
const childNum = nativeDerivationPath.getChildNum(accountIndex)
// const childNum = 'childNum'
const fingerprint = core.createBtcFingerprint(publicKey)
// const fingerprint = 'fingerprint'
const { bitcoinLikeNetworkParameters } = wallet.getCurrency()
const network = Buffer.from(bitcoinLikeNetworkParameters.XPUBVersion).readUIntBE(0, 4)
const xpub = createXPUB(depth, fingerprint, childNum, chainCode, publicKey, network)
// blockHeight // blockHeight
const { height: blockHeight } = await njsAccount.getLastBlock() const { height: blockHeight } = await njsAccount.getLastBlock()
@ -231,21 +215,12 @@ async function buildRawAccount({
path: `${accountPath}/${i}'`, path: `${accountPath}/${i}'`,
})) }))
const operations = ops.map(op => { const operations = ops.map(op =>
const hash = op.getUid() buildOperationRaw({
return { op,
id: hash, xpub,
hash, }),
address: '', )
senders: op.getSenders(),
recipients: op.getRecipients(),
blockHeight: op.getBlockHeight(),
accountId: xpub,
date: op.getDate().toISOString(),
amount: op.getAmount().toLong(),
}
})
const rawAccount: AccountRaw = { const rawAccount: AccountRaw = {
id: xpub, id: xpub,
@ -260,7 +235,7 @@ async function buildRawAccount({
blockHeight, blockHeight,
archived: false, archived: false,
index: accountIndex, index: accountIndex,
balanceByDay: {}, balanceByDay,
operations, operations,
currencyId, currencyId,
unitMagnitude: jsCurrency.units[0].magnitude, unitMagnitude: jsCurrency.units[0].magnitude,
@ -270,19 +245,60 @@ async function buildRawAccount({
return rawAccount return rawAccount
} }
/** function buildOperationRaw({ op, xpub }: { op: NJSOperation, xpub: string }) {
* TODO: should be calculated by the lib core const hash = op.getUid()
* why? because the xpub generated here seems invalid return {
*/ id: hash,
function createXPUB(depth, fingerprint, childnum, chaincode, publicKey, network) { hash,
let xpub = toHexInt(network) address: '',
// $FlowFixMe senders: op.getSenders(),
xpub += padStart(depth.toString(16), 2, '0') recipients: op.getRecipients(),
// $FlowFixMe blockHeight: op.getBlockHeight(),
xpub += padStart(fingerprint.toString(16), 8, '0') accountId: xpub,
// $FlowFixMe date: op.getDate().toISOString(),
xpub += padStart(childnum.toString(16), 8, '0') amount: op.getAmount().toLong(),
xpub += chaincode }
xpub += publicKey }
return encodeBase58Check(xpub)
async function getBalanceByDaySinceOperation({
njsAccount,
njsOperation,
core,
}: {
njsAccount: NJSAccount,
njsOperation: NJSOperation,
core: Object,
}) {
const startDate = njsOperation.getDate()
// set end date to tomorrow
const endDate = new Date()
endDate.setDate(endDate.getDate() + 1)
const njsBalanceHistory = await njsAccount.getBalanceHistory(
startDate.toISOString(),
endDate.toISOString(),
core.TIME_PERIODS.DAY,
)
let i = 0
const res = {}
while (!areSameDay(startDate, endDate)) {
const dateSQLFormatted = startDate.toISOString().substr(0, 10)
const balanceDay = njsBalanceHistory[i]
if (balanceDay) {
res[dateSQLFormatted] = njsBalanceHistory[i].toLong()
} else {
console.warn(`No balance for day ${dateSQLFormatted}. This is a bug.`) // eslint-disable-line no-console
}
startDate.setDate(startDate.getDate() + 1)
i++
}
return res
}
function areSameDay(date1, date2) {
return (
date1.getYear() === date2.getYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
)
} }

4
src/internals/devices/checkIfAppOpened.js

@ -3,6 +3,8 @@
import CommNodeHid from '@ledgerhq/hw-transport-node-hid' import CommNodeHid from '@ledgerhq/hw-transport-node-hid'
import Btc from '@ledgerhq/hw-app-btc' import Btc from '@ledgerhq/hw-app-btc'
import type Transport from '@ledgerhq/hw-transport'
import type { IPCSend } from 'types/electron' import type { IPCSend } from 'types/electron'
import { getPath } from 'internals/accounts/helpers' import { getPath } from 'internals/accounts/helpers'
@ -24,7 +26,7 @@ export default async (
}, },
) => { ) => {
try { try {
const transport = await CommNodeHid.open(devicePath) const transport: Transport<*> = await CommNodeHid.open(devicePath)
const btc = new Btc(transport) const btc = new Btc(transport)
if (accountPath) { if (accountPath) {
const { bitcoinAddress } = await btc.getWalletPublicKey(accountPath, false, segwit) const { bitcoinAddress } = await btc.getWalletPublicKey(accountPath, false, segwit)

4
yarn.lock

@ -8494,9 +8494,9 @@ lcid@^1.0.0:
dependencies: dependencies:
invert-kv "^1.0.0" invert-kv "^1.0.0"
ledger-core@meriadec/lib-ledger-core-node-bindings#5236e84: ledger-core@meriadec/lib-ledger-core-node-bindings#697ec09:
version "1.0.0" version "1.0.0"
resolved "https://codeload.github.com/meriadec/lib-ledger-core-node-bindings/tar.gz/5236e84e98656891f9ffe4cfa4a17ad41d07a9b6" resolved "https://codeload.github.com/meriadec/lib-ledger-core-node-bindings/tar.gz/697ec09daa223a40a04267239c70a9a0e82cb15f"
dependencies: dependencies:
"@ledgerhq/hw-app-btc" "^4.7.3" "@ledgerhq/hw-app-btc" "^4.7.3"
"@ledgerhq/hw-transport-node-hid" "^4.7.6" "@ledgerhq/hw-transport-node-hid" "^4.7.6"

Loading…
Cancel
Save