support sending funds to external wallet
This commit is contained in:
parent
dd04b9bb91
commit
be53de7326
5 changed files with 2446 additions and 1718 deletions
89
bin/lamassu-send-coins
Executable file
89
bin/lamassu-send-coins
Executable file
|
|
@ -0,0 +1,89 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const settingsLoader = require('../lib/settings-loader')
|
||||||
|
const configManager = require('../lib/config-manager')
|
||||||
|
const wallet = require('../lib/wallet')
|
||||||
|
const coinUtils = require('../lib/coin-utils')
|
||||||
|
const BN = require('../lib/bn')
|
||||||
|
const inquirer = require('inquirer')
|
||||||
|
const ticker = require('../lib/ticker')
|
||||||
|
|
||||||
|
const [toAddress, cryptoValue, cryptoCode] = process.argv.slice(2)
|
||||||
|
|
||||||
|
function computeCrypto (cryptoCode, value) {
|
||||||
|
try {
|
||||||
|
const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
|
||||||
|
const unitScale = cryptoRec.unitScale
|
||||||
|
|
||||||
|
return BN(value).shift(unitScale)
|
||||||
|
} catch (err) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!toAddress || !cryptoValue || !cryptoCode) {
|
||||||
|
console.log('Usage: lamassu-send-coins <address> <amount> <coin>')
|
||||||
|
console.log('Example: lamassu-send-coins 3FUv7vKaP11idnsUKyQ2pxdWxCDMyr5HKJ 0.009 BTC')
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
const cryptoAtoms = computeCrypto(cryptoCode, cryptoValue)
|
||||||
|
|
||||||
|
if (!cryptoAtoms) {
|
||||||
|
console.log(`Unsupported coin: ${cryptoCode}.\n`)
|
||||||
|
console.log('Usage: lamassu-send-coins <address> <amount> <coin>')
|
||||||
|
console.log('Example: lamassu-send-coins 3FUv7vKaP11idnsUKyQ2pxdWxCDMyr5HKJ 0.009 BTC')
|
||||||
|
process.exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Loading ticker...')
|
||||||
|
|
||||||
|
settingsLoader.loadLatest()
|
||||||
|
.then(settings => {
|
||||||
|
const config = configManager.unscoped(settings.config)
|
||||||
|
const fiatCode = config.fiatCurrency
|
||||||
|
|
||||||
|
return wallet.isStrictAddress(settings, cryptoCode, toAddress)
|
||||||
|
.then(isValid => {
|
||||||
|
if (!isValid) {
|
||||||
|
console.log(`Invalid ${cryptoCode} address: ${toAddress}.`)
|
||||||
|
console.log('Please check your command.\n')
|
||||||
|
console.log('Usage: lamassu-send-coins <address> <amount> <coin>')
|
||||||
|
console.log('Example: lamassu-send-coins 3FUv7vKaP11idnsUKyQ2pxdWxCDMyr5HKJ 0.009 BTC')
|
||||||
|
process.exit(3)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(() => ticker.getRates(settings, fiatCode, cryptoCode))
|
||||||
|
.then(rates => {
|
||||||
|
const fiatAmount = rates.rates.ask.times(cryptoValue).toFixed(2)
|
||||||
|
|
||||||
|
const questions = [
|
||||||
|
{
|
||||||
|
type: 'confirm',
|
||||||
|
name: 'confirm',
|
||||||
|
message: `Are you sure you want to send ${cryptoValue} ${cryptoCode} (${fiatAmount} ${fiatCode}) to the address ${toAddress}?`,
|
||||||
|
default: false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
console.log('\nPlease look over this transaction carefully!')
|
||||||
|
|
||||||
|
return inquirer.prompt(questions)
|
||||||
|
.then(answers => {
|
||||||
|
if (!answers.confirm) {
|
||||||
|
console.log('Transaction cancelled.')
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Sending...')
|
||||||
|
return wallet.sendCoins(settings, toAddress, cryptoAtoms, cryptoCode)
|
||||||
|
.then(() => {
|
||||||
|
console.log('Success.')
|
||||||
|
process.exit(0)
|
||||||
|
})
|
||||||
|
.catch(console.log)
|
||||||
|
})
|
||||||
|
.catch(console.log)
|
||||||
|
})
|
||||||
|
.catch(console.log)
|
||||||
|
})
|
||||||
|
|
@ -4,6 +4,7 @@ const Web3 = require('web3')
|
||||||
const web3 = new Web3()
|
const web3 = new Web3()
|
||||||
const hdkey = require('ethereumjs-wallet/hdkey')
|
const hdkey = require('ethereumjs-wallet/hdkey')
|
||||||
const Tx = require('ethereumjs-tx')
|
const Tx = require('ethereumjs-tx')
|
||||||
|
const util = require('ethereumjs-util')
|
||||||
const pify = require('pify')
|
const pify = require('pify')
|
||||||
|
|
||||||
const coinUtils = require('../../../coin-utils')
|
const coinUtils = require('../../../coin-utils')
|
||||||
|
|
@ -27,7 +28,8 @@ module.exports = {
|
||||||
defaultAddress,
|
defaultAddress,
|
||||||
supportsHd: true,
|
supportsHd: true,
|
||||||
newFunding,
|
newFunding,
|
||||||
privateKey
|
privateKey,
|
||||||
|
isStrictAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!web3.isConnected()) {
|
if (!web3.isConnected()) {
|
||||||
|
|
@ -40,6 +42,10 @@ function privateKey (account) {
|
||||||
return defaultWallet(account).getPrivateKey()
|
return defaultWallet(account).getPrivateKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isStrictAddress (cryptoCode, toAddress) {
|
||||||
|
return cryptoCode === 'ETH' && util.isValidChecksumAddress(toAddress)
|
||||||
|
}
|
||||||
|
|
||||||
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
|
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
|
||||||
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false)
|
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false)
|
||||||
.then(pify(web3.eth.sendRawTransaction))
|
.then(pify(web3.eth.sendRawTransaction))
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ const fs = pify(require('fs'))
|
||||||
|
|
||||||
const options = require('./options')
|
const options = require('./options')
|
||||||
const ph = require('./plugin-helper')
|
const ph = require('./plugin-helper')
|
||||||
const logger = require('./logger')
|
|
||||||
|
|
||||||
const FETCH_INTERVAL = 5000
|
const FETCH_INTERVAL = 5000
|
||||||
const INSUFFICIENT_FUNDS_CODE = 570
|
const INSUFFICIENT_FUNDS_CODE = 570
|
||||||
|
|
@ -153,6 +152,16 @@ function cryptoNetwork (settings, cryptoCode) {
|
||||||
return wallet.cryptoNetwork(account, cryptoCode)
|
return wallet.cryptoNetwork(account, cryptoCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isStrictAddress (settings, cryptoCode, toAddress) {
|
||||||
|
// Note: For now, only for wallets that specifically check for this.
|
||||||
|
|
||||||
|
return fetchWallet(settings, cryptoCode)
|
||||||
|
.then(r => {
|
||||||
|
if (!r.wallet.isStrictAddress) return true
|
||||||
|
return r.wallet.isStrictAddress(cryptoCode, toAddress)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const balance = mem(_balance, {
|
const balance = mem(_balance, {
|
||||||
maxAge: FETCH_INTERVAL,
|
maxAge: FETCH_INTERVAL,
|
||||||
cacheKey: (settings, cryptoCode) => cryptoCode
|
cacheKey: (settings, cryptoCode) => cryptoCode
|
||||||
|
|
@ -163,6 +172,7 @@ module.exports = {
|
||||||
sendCoins,
|
sendCoins,
|
||||||
newAddress,
|
newAddress,
|
||||||
getStatus,
|
getStatus,
|
||||||
|
isStrictAddress,
|
||||||
sweep,
|
sweep,
|
||||||
isHd,
|
isHd,
|
||||||
newFunding,
|
newFunding,
|
||||||
|
|
|
||||||
4049
package-lock.json
generated
4049
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -20,6 +20,7 @@
|
||||||
"console-log-level": "^1.4.0",
|
"console-log-level": "^1.4.0",
|
||||||
"cookie-parser": "^1.4.3",
|
"cookie-parser": "^1.4.3",
|
||||||
"ethereumjs-tx": "^1.3.3",
|
"ethereumjs-tx": "^1.3.3",
|
||||||
|
"ethereumjs-util": "^5.2.0",
|
||||||
"ethereumjs-wallet": "^0.6.0",
|
"ethereumjs-wallet": "^0.6.0",
|
||||||
"express": "^4.15.4",
|
"express": "^4.15.4",
|
||||||
"express-limiter": "^1.6.0",
|
"express-limiter": "^1.6.0",
|
||||||
|
|
@ -27,7 +28,7 @@
|
||||||
"express-ws": "^3.0.0",
|
"express-ws": "^3.0.0",
|
||||||
"got": "^7.1.0",
|
"got": "^7.1.0",
|
||||||
"helmet": "^3.8.1",
|
"helmet": "^3.8.1",
|
||||||
"inquirer": "^3.2.1",
|
"inquirer": "^5.2.0",
|
||||||
"kraken-api": "github:DeX3/npm-kraken-api",
|
"kraken-api": "github:DeX3/npm-kraken-api",
|
||||||
"lnd-async": "^1.0.1",
|
"lnd-async": "^1.0.1",
|
||||||
"lodash": "^4.17.2",
|
"lodash": "^4.17.2",
|
||||||
|
|
@ -76,7 +77,8 @@
|
||||||
"lamassu-nuke-db": "./bin/lamassu-nuke-db",
|
"lamassu-nuke-db": "./bin/lamassu-nuke-db",
|
||||||
"lamassu-coins": "./bin/lamassu-coins",
|
"lamassu-coins": "./bin/lamassu-coins",
|
||||||
"lamassu-update": "./bin/lamassu-update",
|
"lamassu-update": "./bin/lamassu-update",
|
||||||
"lamassu-ofac-update": "./bin/lamassu-ofac-update"
|
"lamassu-ofac-update": "./bin/lamassu-ofac-update",
|
||||||
|
"lamassu-send-coins": "./bin/lamassu-send-coins"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node bin/lamassu-server",
|
"start": "node bin/lamassu-server",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue