Browse Source

Create barebone for libcore sync account

master
meriadec 7 years ago
parent
commit
d8e2f57a8f
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 22
      src/bridge/LibcoreBridge.js
  2. 2
      src/commands/index.js
  3. 28
      src/commands/libcoreSyncAccount.js
  4. 24
      src/helpers/libcore.js

22
src/bridge/LibcoreBridge.js

@ -4,6 +4,7 @@ import { map } from 'rxjs/operators'
import { decodeAccount, encodeAccount } from 'reducers/accounts'
import FeesBitcoinKind from 'components/FeesField/BitcoinKind'
import libcoreScanAccounts from 'commands/libcoreScanAccounts'
import libcoreSyncAccount from 'commands/libcoreSyncAccount'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
// import AdvancedOptionsBitcoinKind from 'components/AdvancedOptions/BitcoinKind'
import type { WalletBridge, EditProps } from './types'
@ -49,7 +50,24 @@ const LibcoreBridge: WalletBridge<Transaction> = {
.subscribe(observer)
},
synchronize(_initialAccount, _observer) {
synchronize(account, { next, complete, error }) {
;(async () => {
try {
const rawAccount = encodeAccount(account)
const rawSyncedAccount = await libcoreSyncAccount.send({ rawAccount }).toPromise()
const syncedAccount = decodeAccount(rawSyncedAccount)
next(account => ({
...account,
balance: syncedAccount.balance,
blockHeight: syncedAccount.blockHeight,
operations: syncedAccount.operations, // TODO: is a simple replace enough?
lastSyncDate: new Date(),
}))
complete()
} catch (e) {
error(e)
}
})()
// FIXME TODO: use next(), to actually emit account updates.....
// - need to sync the balance
// - need to sync block height & block hash
@ -61,7 +79,7 @@ const LibcoreBridge: WalletBridge<Transaction> = {
// then we probably should trash them out? it's a complex question for UI
return {
unsubscribe() {
console.warn('LibcoreBridge: sync not implemented')
console.warn('LibcoreBridge: unsub sync not implemented')
},
}
},

2
src/commands/index.js

@ -16,6 +16,7 @@ import isCurrencyAppOpened from 'commands/isCurrencyAppOpened'
import libcoreGetVersion from 'commands/libcoreGetVersion'
import libcoreScanAccounts from 'commands/libcoreScanAccounts'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
import libcoreSyncAccount from 'commands/libcoreSyncAccount'
import listApps from 'commands/listApps'
import listenDevices from 'commands/listenDevices'
import signTransaction from 'commands/signTransaction'
@ -39,6 +40,7 @@ const all: Array<Command<any, any>> = [
libcoreGetVersion,
libcoreScanAccounts,
libcoreSignAndBroadcast,
libcoreSyncAccount,
listApps,
listenDevices,
signTransaction,

28
src/commands/libcoreSyncAccount.js

@ -1,37 +1,21 @@
// @flow
import type { AccountRaw } from '@ledgerhq/live-common/lib/types'
import { fromPromise } from 'rxjs/observable/fromPromise'
import { createCommand, Command } from 'helpers/ipc'
import { Observable } from 'rxjs'
import { syncAccount } from 'helpers/libcore'
import withLibcore from 'helpers/withLibcore'
type Input = {
core: Object,
rawAccount: AccountRaw,
}
type Result = AccountRaw
const cmd: Command<Input, Result> = createCommand(
'accounts',
'libcoreSyncAccount',
({ rawAccount }) =>
Observable.create(o => {
// TODO scanAccountsOnDevice should directly return a Observable so we just have to pass-in
syncAccount({ rawAccount }).then(
() => {
o.complete()
},
e => {
o.error(e)
},
)
function unsubscribe() {
// FIXME not implemented
}
return unsubscribe
}),
const cmd: Command<Input, Result> = createCommand('libcoreSyncAccount', ({ rawAccount }) =>
fromPromise(withLibcore(core => syncAccount({ rawAccount, core }))),
)
export default cmd

24
src/helpers/libcore.js

@ -326,3 +326,27 @@ function buildOperationRaw({
date: op.getDate().toISOString(),
}
}
export async function syncAccount({ rawAccount }: { rawAccount: AccountRaw }) {
// AWWWWW.. little problem here.
//
// we need to get account from libcore db. in order to do that we have to:
// 1) get wallet using a wallet identifier
// 2) get account from wallet using `rawAccount.index`
//
// Here is the problem: the wallet identifier is currently built like that:
// `${publicKey}__${currencyId}${isSegwit ? '_segwit' : ''}`
//
// and to get the `publicKey` we need the device.
//
// BUT we don't want the device to be required to access ledger-live
// SO.. it's a problem.
//
// Solution 1: store wallet identifier inside the Account (uurgh...)
// Solution 2: stop this project
return {
...rawAccount,
balance: 424242424242,
}
}

Loading…
Cancel
Save