From 3b7c6fde569024021fe3bbd887d6dce57903d1d1 Mon Sep 17 00:00:00 2001 From: meriadec Date: Fri, 11 May 2018 18:06:53 +0200 Subject: [PATCH] Scanning segwit accounts (not fully working yet LOL) --- .../accounts/scanAccountsOnDevice.js | 72 ++++++++++++++++--- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/src/internals/accounts/scanAccountsOnDevice.js b/src/internals/accounts/scanAccountsOnDevice.js index 154adf91..156e48b6 100644 --- a/src/internals/accounts/scanAccountsOnDevice.js +++ b/src/internals/accounts/scanAccountsOnDevice.js @@ -33,13 +33,38 @@ export default async function scanAccountsOnDevice(props: Props): Promise> Scanning account ${accountIndex}`) + console.log(`>> Scanning account ${accountIndex} - isSegwit: ${isSegwit.toString()}`) // create account only if account has not been scanned yet // if it has already been created, we just need to get it, and sync it @@ -88,6 +115,8 @@ async function scanNextAccount(props) { const query = njsAccount.queryOperations() const ops = await query.limit(OPS_LIMIT).execute() + console.log(`found ${ops.length} transactions`) + const account = await buildRawAccount({ njsAccount, accountIndex, @@ -111,7 +140,7 @@ async function scanNextAccount(props) { return scanNextAccount({ ...props, accountIndex: accountIndex + 1 }) } -async function getOrCreateWallet(WALLET_IDENTIFIER, currencyId) { +async function getOrCreateWallet(WALLET_IDENTIFIER, currencyId, isSegwit) { // TODO: investigate why importing it on file scope causes trouble const core = require('init-ledger-core')() try { @@ -119,7 +148,14 @@ async function getOrCreateWallet(WALLET_IDENTIFIER, currencyId) { return wallet } catch (err) { const currency = await core.getCurrency(currencyId) - const wallet = await core.createWallet(WALLET_IDENTIFIER, currency) + const walletConfig = isSegwit + ? { + KEYCHAIN_ENGINE: 'BIP49_P2SH', + KEYCHAIN_DERIVATION_SCHEME: "49'/'/'//
", + } + : undefined + const njsWalletConfig = core.createWalletConfig(walletConfig) + const wallet = await core.createWallet(WALLET_IDENTIFIER, currency, njsWalletConfig) return wallet } } @@ -143,12 +179,32 @@ async function buildRawAccount({ // $FlowFixMe ops: NJSOperation[], }) { + // const njsBalanceHistory = await njsAccount.getBalanceHistory( + // new Date('2018-05-01').toISOString(), + // new Date('2018-06-01').toISOString(), + // core.TIME_PERIODS.DAY, + // ) + + // const balanceHistory = njsBalanceHistory.map(njsAmount => njsAmount.toLong()) + + const njsBalance = await njsAccount.getBalance() + const balance = njsBalance.toLong() + const jsCurrency = getCryptoCurrencyById(currencyId) // retrieve xpub const { derivations } = await wallet.getAccountCreationInfo(accountIndex) const [walletPath, accountPath] = derivations - const { publicKey, chainCode, bitcoinAddress } = await hwApp.getWalletPublicKey(accountPath) + + console.log(`so, the account path is ${accountPath}`) + + const VERIFY = false + const isSegwit = true + const { publicKey, chainCode, bitcoinAddress } = await hwApp.getWalletPublicKey( + accountPath, + VERIFY, + isSegwit, + ) // TODO: wtf is happening? // @@ -199,7 +255,7 @@ async function buildRawAccount({ name: `Account ${accountIndex}`, // TODO: placeholder name? address: bitcoinAddress, // TODO: discuss about the utility of storing it here addresses, - balance: 0, + balance, blockHeight, archived: false, index: accountIndex,