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