From d7eccc8046d227bf963e220bf2c91dcb239e1aec Mon Sep 17 00:00:00 2001 From: meriadec Date: Mon, 30 Apr 2018 18:46:55 +0200 Subject: [PATCH] Push transaction script & skeleton of sync --- README.md | 3 + alix.js | 94 +++++++++++++++++++ src/components/DashboardPage/index.js | 34 ++++++- src/internals/usb/wallet/index.js | 18 ++++ .../usb/wallet/scanAccountsOnDevice.js | 30 ++++++ yarn.lock | 28 ++++-- 6 files changed, 195 insertions(+), 12 deletions(-) create mode 100644 alix.js create mode 100644 src/internals/usb/wallet/scanAccountsOnDevice.js diff --git a/README.md b/README.md index 58ab95cf..1c22f810 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ DEV_TOOLS_MODE=bottom # Filter debug output DEBUG=lwd*,-lwd:syncb + +# hide the dev window +HIDE_DEV_WINDOW=0 ``` #### Development commands diff --git a/alix.js b/alix.js new file mode 100644 index 00000000..e41ad3d8 --- /dev/null +++ b/alix.js @@ -0,0 +1,94 @@ +const CommNodeHid = require('@ledgerhq/hw-transport-node-hid').default +const Btc = require('@ledgerhq/hw-app-btc').default + +const CREATE_ACCOUNT = false + +const { + createWallet, + createAccount, + createAmount, + getCurrency, + getWallet, + syncAccount, + signTransaction, + EVENT_CODE, +} = require('ledger-core') + +waitForDevices(async device => { + try { + console.log(`> Creating transport`) + const transport = await CommNodeHid.open(device.path) + + // transport.setDebugMode(true) + + console.log(`> Instanciate BTC app`) + const hwApp = new Btc(transport) + + console.log(`> Get currency`) + const currency = await getCurrency('bitcoin_testnet') + + console.log(`> Create wallet`) + const wallet = CREATE_ACCOUNT + ? await createWallet('khalil', currency) + : await getWallet('khalil') + + console.log(`> Create account`) + const account = CREATE_ACCOUNT ? await createAccount(wallet, hwApp) : await wallet.getAccount(0) + + console.log(`> Sync account`) + if (true || CREATE_ACCOUNT) { + await syncAccount(account) + } + + console.log(`> Create transaction`) + const transaction = await createTransaction(wallet, account) + const signedTransaction = await signTransaction(hwApp, transaction) + + await account.asBitcoinLikeAccount().broadcastRawTransaction(signedTransaction) + // console.log(signedTransaction); + + process.exit(0) + // console.log(account.getIndex()); + // console.log(account.isSynchronizing()); + } catch (err) { + console.log(err.message) + process.exit(1) + } +}) + +function waitForDevices(onDevice) { + console.log(`> Waiting for device...`) + CommNodeHid.listen({ + error: () => {}, + complete: () => {}, + next: async e => { + if (!e.device) { + return + } + if (e.type === 'add') { + console.log(`> Detected ${e.device.manufacturer} ${e.device.product}`) + onDevice(e.device) + } + if (e.type === 'remove') { + console.log(`removed ${JSON.stringify(e)}`) + } + }, + }) +} + +async function createTransaction(wallet, account) { + const ADDRESS_TO_SEND = 'mqg5p9otMX9davdpNATcxjpKsPnopPtNvL' + + const bitcoinLikeAccount = account.asBitcoinLikeAccount() + const walletCurrency = wallet.getCurrency() + const amount = createAmount(walletCurrency, 109806740) + const fees = createAmount(walletCurrency, 10) + + const transactionBuilder = bitcoinLikeAccount.buildTransaction() + transactionBuilder.sendToAddress(amount, ADDRESS_TO_SEND) + // TODO: don't use hardcoded value for sequence (and first also maybe) + transactionBuilder.pickInputs(0, 0xffffff) + transactionBuilder.setFeesPerByte(fees) + + return transactionBuilder.build() +} diff --git a/src/components/DashboardPage/index.js b/src/components/DashboardPage/index.js index 090ac7dd..52bcf513 100644 --- a/src/components/DashboardPage/index.js +++ b/src/components/DashboardPage/index.js @@ -6,18 +6,18 @@ import { compose } from 'redux' import { translate } from 'react-i18next' import { connect } from 'react-redux' import { push } from 'react-router-redux' +import chunk from 'lodash/chunk' + import { formatCurrencyUnit, getFiatCurrencyByTicker, } from '@ledgerhq/live-common/lib/helpers/currencies' import type { Account } from '@ledgerhq/live-common/lib/types' - -import chunk from 'lodash/chunk' - import type { T } from 'types/common' import { colors } from 'styles/theme' +import { runJob } from 'renderer/events' import { getVisibleAccounts } from 'reducers/accounts' import { getCounterValueCode, localeSelector } from 'reducers/settings' @@ -36,6 +36,7 @@ import AccountCard from './AccountCard' import AccountsOrder from './AccountsOrder' const mapStateToProps = state => ({ + devices: state.devices, accounts: getVisibleAccounts(state), counterValue: getCounterValueCode(state), locale: localeSelector(state), @@ -132,13 +133,38 @@ class DashboardPage extends PureComponent { _cacheBalance = null render() { - const { push, accounts, t, counterValue } = this.props + const { push, accounts, t, counterValue, devices } = this.props const { accountsChunk, selectedTime, daysCount } = this.state const timeFrame = this.handleGreeting() const totalAccounts = accounts.length + const { currentDevice } = devices + return ( + {currentDevice && ( + + {currentDevice.path} + + + )} + diff --git a/src/internals/usb/wallet/index.js b/src/internals/usb/wallet/index.js index 3414b6cc..40be605e 100644 --- a/src/internals/usb/wallet/index.js +++ b/src/internals/usb/wallet/index.js @@ -4,8 +4,26 @@ import CommNodeHid from '@ledgerhq/hw-transport-node-hid' import Btc from '@ledgerhq/hw-app-btc' import { getPath, verifyAddress } from './accounts' +import scanAccountsOnDevice from './scanAccountsOnDevice' export default (sendEvent: Function) => ({ + /** + * Scan all the accounts for the given device and currency and returns them + */ + scanAccountsOnDevice: async ({ + devicePath, + currencyId, + }: { + devicePath: string, + currencyId: string, + }) => { + try { + const accounts = await scanAccountsOnDevice({ devicePath, currencyId }) + sendEvent('wallet.scanAccountsOnDevice.success', accounts) + } catch (err) { + sendEvent('wallet.scanAccountsOnDevice.fail', err) + } + }, getAccounts: async ({ pathDevice, currencyId, diff --git a/src/internals/usb/wallet/scanAccountsOnDevice.js b/src/internals/usb/wallet/scanAccountsOnDevice.js new file mode 100644 index 00000000..c8f8e27a --- /dev/null +++ b/src/internals/usb/wallet/scanAccountsOnDevice.js @@ -0,0 +1,30 @@ +// @flow + +// Scan accounts on device +// ----------------------- +// +// _ ,--() +// ( )-'-.------|> +// " `--[] +// + +import ledgercore from 'ledger-core' + +import type { Account } from '@ledgerhq/wallet-common/lib/types' + +type Props = { + devicePath: string, + currencyId: string, +} + +async function getOrCreateWallet() { + // const wallet = awat +} + +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 [] +} diff --git a/yarn.lock b/yarn.lock index 6bb24984..3f10dc01 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6896,7 +6896,11 @@ homedir-polyfill@^1.0.1: dependencies: parse-passwd "^1.0.0" -hosted-git-info@^2.1.4, hosted-git-info@^2.6.0: +hosted-git-info@^2.1.4: + version "2.5.0" + resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.5.0.tgz#6d60e34b3abbc8313062c3b798ef8d901a07af3c" + +hosted-git-info@^2.6.0: version "2.6.0" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.6.0.tgz#23235b29ab230c576aab0d4f13fc046b0b038222" @@ -7139,7 +7143,11 @@ ignore-walk@^3.0.1: dependencies: minimatch "^3.0.4" -ignore@^3.3.3, ignore@^3.3.5: +ignore@^3.3.3: + version "3.3.7" + resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021" + +ignore@^3.3.5: version "3.3.8" resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.8.tgz#3f8e9c35d38708a3a7e0e9abb6c73e7ee7707b2b" @@ -8446,7 +8454,11 @@ lodash.uniq@^4.5.0: version "4.5.0" resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773" -lodash@^4.0.1, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.5.1: +lodash@^4.0.1, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.5.1: + version "4.17.5" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" + +lodash@^4.17.10: version "4.17.10" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7" @@ -10658,15 +10670,15 @@ read-pkg@^3.0.0: path-type "^3.0.0" "readable-stream@1 || 2", readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.4, readable-stream@^2.0.6, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.2.9, readable-stream@^2.3.3, readable-stream@^2.3.5: - version "2.3.6" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" + version "2.3.5" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.5.tgz#b4f85003a938cbb6ecbce2a124fb1012bd1a838d" dependencies: core-util-is "~1.0.0" inherits "~2.0.3" isarray "~1.0.0" process-nextick-args "~2.0.0" safe-buffer "~5.1.1" - string_decoder "~1.1.1" + string_decoder "~1.0.3" util-deprecate "~1.0.1" readable-stream@1.0: @@ -10972,8 +10984,8 @@ request-promise-native@^1.0.5: tough-cookie ">=2.3.3" request@2, request@^2.45.0, request@^2.83.0: - version "2.85.0" - resolved "https://registry.yarnpkg.com/request/-/request-2.85.0.tgz#5a03615a47c61420b3eb99b7dba204f83603e1fa" + version "2.83.0" + resolved "https://registry.yarnpkg.com/request/-/request-2.83.0.tgz#ca0b65da02ed62935887808e6f510381034e3356" dependencies: aws-sign2 "~0.7.0" aws4 "^1.6.0"