From 278f18b727c9d97ff45e3cffd25a3e6165d5e3ec Mon Sep 17 00:00:00 2001 From: meriadec Date: Wed, 2 May 2018 13:27:29 +0200 Subject: [PATCH] Update test script --- alix.js | 57 ++++++++++++++++--- package.json | 1 + .../usb/wallet/scanAccountsOnDevice.js | 48 +++++++++++++--- 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/alix.js b/alix.js index 99a019a6..7ff9c693 100644 --- a/alix.js +++ b/alix.js @@ -1,3 +1,5 @@ +/* eslint-disable no-console */ + const CommNodeHid = require('@ledgerhq/hw-transport-node-hid').default const Btc = require('@ledgerhq/hw-app-btc').default @@ -11,10 +13,53 @@ const { getWallet, syncAccount, signTransaction, - EVENT_CODE, } = require('ledger-core') +async function getOrCreateWallet(currencyId) { + try { + const wallet = await getWallet(currencyId) + return wallet + } catch (err) { + const currency = await getCurrency(currencyId) + const wallet = await createWallet(currencyId, currency) + return wallet + } +} + +async function scanNextAccount(wallet, hwApp, accountIndex = 0) { + console.log(`creating an account with index ${accountIndex}`) + const account = await createAccount(wallet, hwApp) + console.log(`synchronizing account ${accountIndex}`) + await syncAccount(account) + console.log(`finished sync`) + const utxoCount = await account.asBitcoinLikeAccount().getUTXOCount() + console.log(`utxoCount = ${utxoCount}`) +} + +async function scanAccountsOnDevice(props) { + try { + const { devicePath, currencyId } = props + console.log(`get or create wallet`) + const wallet = await getOrCreateWallet(currencyId) + console.log(`open device`) + const transport = await CommNodeHid.open(devicePath) + console.log(`create app`) + const hwApp = new Btc(transport) + console.log(`scan account`) + const accounts = await scanNextAccount(wallet, hwApp) + console.log(accounts) + return [] + } catch (err) { + console.log(err) + } +} + waitForDevices(async device => { + // const accounts = await scanAccountsOnDevice({ + // devicePath: device.path, + // currencyId: 'bitcoin_testnet', + // }) + // console.log(accounts) try { console.log(`> Creating transport`) const transport = await CommNodeHid.open(device.path) @@ -28,9 +73,7 @@ waitForDevices(async device => { const currency = await getCurrency('bitcoin_testnet') console.log(`> Create wallet`) - const wallet = CREATE - ? await createWallet('khalil', currency) - : await getWallet('khalil') + const wallet = CREATE ? await createWallet('khalil', currency) : await getWallet('khalil') console.log(`> Create account`) const account = CREATE ? await createAccount(wallet, hwApp) : await wallet.getAccount(0) @@ -77,12 +120,12 @@ function waitForDevices(onDevice) { } async function createTransaction(wallet, account) { - const ADDRESS_TO_SEND = 'mqg5p9otMX9davdpNATcxjpKsPnopPtNvL' + const ADDRESS_TO_SEND = 'n2jdejywRogCunR2ozZAfXp1jMnfGpGXGR' const bitcoinLikeAccount = account.asBitcoinLikeAccount() const walletCurrency = wallet.getCurrency() - const amount = createAmount(walletCurrency, 109806740) - const fees = createAmount(walletCurrency, 10) + const amount = createAmount(walletCurrency, 10000) + const fees = createAmount(walletCurrency, 1000) const transactionBuilder = bitcoinLikeAccount.buildTransaction() transactionBuilder.sendToAddress(amount, ADDRESS_TO_SEND) diff --git a/package.json b/package.json index 88163d0c..1a46e414 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "i18next": "^11.2.2", "i18next-node-fs-backend": "^1.0.0", "invariant": "^2.2.4", + "ledger-core": "meriadec/lib-ledger-core-node-bindings", "lodash": "^4.17.5", "moment": "^2.22.1", "object-path": "^0.11.4", diff --git a/src/internals/usb/wallet/scanAccountsOnDevice.js b/src/internals/usb/wallet/scanAccountsOnDevice.js index c8f8e27a..9e7521fd 100644 --- a/src/internals/usb/wallet/scanAccountsOnDevice.js +++ b/src/internals/usb/wallet/scanAccountsOnDevice.js @@ -8,7 +8,9 @@ // " `--[] // -import ledgercore from 'ledger-core' +import core from 'ledger-core' +import Btc from '@ledgerhq/hw-app-btc' +import CommNodeHid from '@ledgerhq/hw-transport-node-hid' import type { Account } from '@ledgerhq/wallet-common/lib/types' @@ -17,14 +19,42 @@ type Props = { currencyId: string, } -async function getOrCreateWallet() { - // const wallet = awat +async function getOrCreateWallet(walletPool, currencyId) { + try { + const wallet = await core.getWallet(walletPool, currencyId) + return wallet + } catch (err) { + const currency = await core.getCurrency(walletPool, currencyId) + const wallet = await core.createWallet(walletPool, currencyId, currency) + return wallet + } } -export default function scanAccountsOnDevice(props: Props): Account[] { - const { devicePath, currencyId } = props - console.log(ledgercore) - console.log(`[[[[scanning accounts on device]]]] ${devicePath} ${currencyId}`) - console.log(props) - return [] +async function scanNextAccount(wallet, hwApp, accountIndex = 0) { + console.log(`creating an account with index ${accountIndex}`) + const account = await core.createAccount(wallet, hwApp) + console.log(`synchronizing account ${accountIndex}`) + await core.syncAccount(account) + console.log(`finished sync`) + const utxoCount = await account.asBitcoinLikeAccount().getUTXOCount() + console.log(`utxoCount = ${utxoCount}`) +} + +export default async function scanAccountsOnDevice(props: Props): Account[] { + try { + const { devicePath, currencyId } = props + const walletPool = core.createWalletPool() + console.log(`get or create wallet`) + const wallet = await getOrCreateWallet(walletPool, currencyId) + console.log(`open device`) + const transport = await CommNodeHid.open(devicePath) + console.log(`create app`) + const hwApp = new Btc(transport) + console.log(`scan account`) + const accounts = await scanNextAccount(wallet, hwApp) + console.log(accounts) + return [] + } catch (err) { + console.log(err) + } }