Browse Source

Add utility script

master
meriadec 7 years ago
parent
commit
34e54f814c
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 1
      package.json
  2. 131
      scripts/hey.js
  3. 122
      src/commands/libcoreSignAndBroadcast.js
  4. 19
      src/helpers/libcore.js
  5. 32
      yarn.lock

1
package.json

@ -144,6 +144,7 @@
"flow-typed": "^2.4.0",
"hard-source-webpack-plugin": "^0.6.0",
"husky": "^0.14.3",
"inquirer": "^6.0.0",
"jest": "^22.4.3",
"js-yaml": "^3.10.0",
"node-loader": "^0.6.0",

131
scripts/hey.js

@ -0,0 +1,131 @@
require('babel-polyfill')
require('babel-register')
const chalk = require('chalk')
const inquirer = require('inquirer')
const path = require('path')
const TransportNodeHid = require('@ledgerhq/hw-transport-node-hid').default
const { serializeAccounts, encodeAccount, decodeAccount } = require('../src/reducers/accounts')
const { doSignAndBroadcast } = require('../src/commands/libcoreSignAndBroadcast')
const coreHelper = require('../src/helpers/libcore')
const withLibcore = require('../src/helpers/withLibcore').default
if (!process.env.LEDGER_LIVE_SQLITE_PATH) {
throw new Error('you must define process.env.LEDGER_LIVE_SQLITE_PATH first')
}
const LOCAL_DIRECTORY_PATH = path.resolve(process.env.LEDGER_LIVE_SQLITE_PATH, '../')
gimmeDeviceAndLibCore(async ({ device, core, njsWalletPool }) => {
const raw = require(path.join(LOCAL_DIRECTORY_PATH, 'accounts.json')) // eslint-disable-line import/no-dynamic-require
const accounts = serializeAccounts(raw.data)
const accountToUse = await chooseAccount('Which account to use?', accounts)
await actionLoop({ account: accountToUse, accounts, core, njsWalletPool, device })
process.exit(0)
})
async function actionLoop(props) {
try {
const { account, accounts, core, njsWalletPool, device } = props
const actionToDo = await chooseAction(`What do you want to do with [${account.name}] ?`)
if (actionToDo === 'send funds') {
const transport = await TransportNodeHid.open(device.path)
const accountToReceive = await chooseAccount('To which account?', accounts)
const receiveAddress = await getFreshAddress({
account: accountToReceive,
core,
njsWalletPool,
})
console.log(`the receive address is ${receiveAddress}`)
const rawAccount = encodeAccount(account)
console.log(`trying to sign and broadcast...`)
const rawOp = await doSignAndBroadcast({
account: rawAccount,
transaction: {
amount: 4200000,
recipient: receiveAddress,
feePerByte: 16,
isRBF: false,
},
deviceId: device.path,
core,
transport,
})
console.log(rawOp)
} else if (actionToDo === 'sync') {
console.log(`\nLaunch sync...\n`)
const rawAccount = encodeAccount(account)
const syncedAccount = await coreHelper.syncAccount({ rawAccount, core, njsWalletPool })
console.log(`\nEnd sync...\n`)
console.log(`updated account: `, displayAccount(syncedAccount, 'red'))
} else if (actionToDo === 'quit') {
return true
}
} catch (err) {
console.log(`x Something went wrong`)
console.log(err)
process.exit(1)
}
return actionLoop(props)
}
async function chooseInList(msg, list, formatItem = i => i) {
const choices = list.map(formatItem)
const { choice } = await inquirer.prompt([
{
type: 'list',
name: 'choice',
message: msg,
choices,
},
])
const index = choices.indexOf(choice)
return list[index]
}
async function chooseAction(msg) {
return chooseInList(msg, ['sync', 'send funds', 'quit'])
}
function chooseAccount(msg, accounts) {
return chooseInList(msg, accounts, acc => displayAccount(acc))
}
async function gimmeDeviceAndLibCore(cb) {
withLibcore((core, njsWalletPool) => {
TransportNodeHid.listen({
error: () => {},
complete: () => {},
next: async e => {
if (!e.device) {
return
}
if (e.type === 'add') {
const { device } = e
cb({ device, core, njsWalletPool })
}
},
})
})
}
function displayAccount(acc, color = null) {
const isRawAccount = typeof acc.lastSyncDate === 'string'
if (isRawAccount) {
acc = decodeAccount(acc)
}
const str = `[${acc.name}] ${acc.isSegwit ? '' : '(legacy) '}${acc.unit.code} ${acc.balance} - ${
acc.operations.length
} txs`
return color ? chalk[color](str) : str
}
async function getFreshAddress({ account, core, njsWalletPool }) {
const njsAccount = await coreHelper.getNJSAccount({ account, njsWalletPool })
const unsub = await core.syncAccount(njsAccount)
unsub()
const rawAddresses = await njsAccount.getFreshPublicAddresses()
return rawAddresses[0]
}

122
src/commands/libcoreSignAndBroadcast.js

@ -29,65 +29,73 @@ const cmd: Command<Input, Result> = createCommand(
({ account, transaction, deviceId }) =>
fromPromise(
withDevice(deviceId)(transport =>
withLibcore(async core => {
const hwApp = new Btc(transport)
const WALLET_IDENTIFIER = await getWalletIdentifier({
hwApp,
isSegwit: !!account.isSegwit,
currencyId: account.currencyId,
devicePath: deviceId,
})
const njsWallet = await core.getWallet(WALLET_IDENTIFIER)
const njsAccount = await njsWallet.getAccount(account.index)
const bitcoinLikeAccount = njsAccount.asBitcoinLikeAccount()
const njsWalletCurrency = njsWallet.getCurrency()
const amount = core.createAmount(njsWalletCurrency, transaction.amount)
const fees = core.createAmount(njsWalletCurrency, transaction.feePerByte)
const transactionBuilder = bitcoinLikeAccount.buildTransaction()
// TODO: check if is valid address. if not, it will fail silently on invalid
transactionBuilder.sendToAddress(amount, transaction.recipient)
// TODO: don't use hardcoded value for sequence (and first also maybe)
transactionBuilder.pickInputs(0, 0xffffff)
transactionBuilder.setFeesPerByte(fees)
const builded = await transactionBuilder.build()
const sigHashType = core.helpers.bytesToHex(
njsWalletCurrency.bitcoinLikeNetworkParameters.SigHash,
)
const currency = getCryptoCurrencyById(account.currencyId)
const signedTransaction = await core.signTransaction({
hwApp,
transaction: builded,
sigHashType: parseInt(sigHashType, 16).toString(),
supportsSegwit: !!currency.supportsSegwit,
isSegwit: account.isSegwit,
})
const txHash = await njsAccount
.asBitcoinLikeAccount()
.broadcastRawTransaction(signedTransaction)
// optimistic operation
return {
id: txHash,
hash: txHash,
type: 'OUT',
value: amount,
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: account.id,
date: new Date().toISOString(),
}
}),
withLibcore(core =>
doSignAndBroadcast({
account,
transaction,
deviceId,
core,
transport,
}),
),
),
),
)
export async function doSignAndBroadcast({ account, transaction, deviceId, core, transport }) {
const hwApp = new Btc(transport)
const WALLET_IDENTIFIER = await getWalletIdentifier({
hwApp,
isSegwit: !!account.isSegwit,
currencyId: account.currencyId,
devicePath: deviceId,
})
const njsWallet = await core.getWallet(WALLET_IDENTIFIER)
const njsAccount = await njsWallet.getAccount(account.index)
const bitcoinLikeAccount = njsAccount.asBitcoinLikeAccount()
const njsWalletCurrency = njsWallet.getCurrency()
const amount = core.createAmount(njsWalletCurrency, transaction.amount)
const fees = core.createAmount(njsWalletCurrency, transaction.feePerByte)
const transactionBuilder = bitcoinLikeAccount.buildTransaction()
// TODO: check if is valid address. if not, it will fail silently on invalid
transactionBuilder.sendToAddress(amount, transaction.recipient)
// TODO: don't use hardcoded value for sequence (and first also maybe)
transactionBuilder.pickInputs(0, 0xffffff)
transactionBuilder.setFeesPerByte(fees)
const builded = await transactionBuilder.build()
const sigHashType = core.helpers.bytesToHex(
njsWalletCurrency.bitcoinLikeNetworkParameters.SigHash,
)
const currency = getCryptoCurrencyById(account.currencyId)
const signedTransaction = await core.signTransaction({
hwApp,
transaction: builded,
sigHashType: parseInt(sigHashType, 16).toString(),
supportsSegwit: !!currency.supportsSegwit,
isSegwit: account.isSegwit,
})
const txHash = await njsAccount.asBitcoinLikeAccount().broadcastRawTransaction(signedTransaction)
// optimistic operation
return {
id: txHash,
hash: txHash,
type: 'OUT',
value: amount,
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: account.id,
date: new Date().toISOString(),
}
}
export default cmd

19
src/helpers/libcore.js

@ -7,7 +7,7 @@ import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currenc
import type { AccountRaw, OperationRaw, OperationType } from '@ledgerhq/live-common/lib/types'
import type { NJSAccount, NJSOperation } from '@ledgerhq/ledger-core/src/ledgercore_doc'
import * as accountId from 'helpers/accountId'
import * as accountIdHelper from 'helpers/accountId'
type Props = {
core: Object,
@ -262,7 +262,7 @@ async function buildAccountRaw({
}
const rawAccount: AccountRaw = {
id: accountId.encode({ type: 'libcore', xpub, walletName: wallet.getName() }),
id: accountIdHelper.encode({ type: 'libcore', xpub, walletName: wallet.getName() }),
xpub,
path: walletPath,
name,
@ -321,6 +321,19 @@ function buildOperationRaw({
}
}
export async function getNJSAccount({
account,
njsWalletPool,
}: {
accountId: string,
njsWalletPool: any,
}) {
const decodedAccountId = accountIdHelper.decode(account.id)
const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName)
const njsAccount = await njsWallet.getAccount(account.index)
return njsAccount
}
export async function syncAccount({
rawAccount,
core,
@ -330,7 +343,7 @@ export async function syncAccount({
rawAccount: AccountRaw,
njsWalletPool: Object,
}) {
const decodedAccountId = accountId.decode(rawAccount.id)
const decodedAccountId = accountIdHelper.decode(rawAccount.id)
const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName)
const njsAccount = await njsWallet.getAccount(rawAccount.index)

32
yarn.lock

@ -4099,6 +4099,10 @@ chardet@^0.4.0:
version "0.4.2"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2"
chardet@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.5.0.tgz#fe3ac73c00c3d865ffcc02a0682e2c20b6a06029"
charenc@~0.0.1:
version "0.0.2"
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
@ -6374,6 +6378,14 @@ external-editor@^2.0.4, external-editor@^2.1.0:
iconv-lite "^0.4.17"
tmp "^0.0.33"
external-editor@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-3.0.0.tgz#dc35c48c6f98a30ca27a20e9687d7f3c77704bb6"
dependencies:
chardet "^0.5.0"
iconv-lite "^0.4.22"
tmp "^0.0.33"
extglob@^0.3.1:
version "0.3.2"
resolved "https://registry.yarnpkg.com/extglob/-/extglob-0.3.2.tgz#2e18ff3d2f49ab2765cec9023f011daa8d8349a1"
@ -7539,7 +7551,7 @@ i18next@^11.2.2:
version "11.3.2"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-11.3.2.tgz#4a1a7bb14383ba6aed4abca139b03681fc96e023"
iconv-lite@0.4, iconv-lite@^0.4.17, iconv-lite@^0.4.23, iconv-lite@^0.4.4, iconv-lite@~0.4.13:
iconv-lite@0.4, iconv-lite@^0.4.17, iconv-lite@^0.4.22, iconv-lite@^0.4.23, iconv-lite@^0.4.4, iconv-lite@~0.4.13:
version "0.4.23"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.23.tgz#297871f63be507adcfbfca715d0cd0eed84e9a63"
dependencies:
@ -7697,6 +7709,24 @@ inquirer@^5.2.0:
strip-ansi "^4.0.0"
through "^2.3.6"
inquirer@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-6.0.0.tgz#e8c20303ddc15bbfc2c12a6213710ccd9e1413d8"
dependencies:
ansi-escapes "^3.0.0"
chalk "^2.0.0"
cli-cursor "^2.1.0"
cli-width "^2.0.0"
external-editor "^3.0.0"
figures "^2.0.0"
lodash "^4.3.0"
mute-stream "0.0.7"
run-async "^2.2.0"
rxjs "^6.1.0"
string-width "^2.1.0"
strip-ansi "^4.0.0"
through "^2.3.6"
insert-css@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/insert-css/-/insert-css-2.0.0.tgz#eb5d1097b7542f4c79ea3060d3aee07d053880f4"

Loading…
Cancel
Save