Browse Source

Update test script

master
meriadec 7 years ago
parent
commit
278f18b727
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 57
      alix.js
  2. 1
      package.json
  3. 48
      src/internals/usb/wallet/scanAccountsOnDevice.js

57
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)

1
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",

48
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)
}
}

Loading…
Cancel
Save