v12.0.0 - initial commit

This commit is contained in:
padreug 2025-12-31 19:04:13 +01:00
commit e2c49ea43c
1145 changed files with 97211 additions and 0 deletions

View file

@ -0,0 +1,53 @@
const _ = require('lodash/fp')
const kraken = require('../exchange/kraken')
const bitstamp = require('../exchange/bitstamp')
const itbit = require('../exchange/itbit')
const binanceus = require('../exchange/binanceus')
const cex = require('../exchange/cex')
const bitpay = require('../ticker/bitpay')
const binance = require('../exchange/binance')
const bitfinex = require('../exchange/bitfinex')
const ALL = {
cex: cex,
binanceus: binanceus,
kraken: kraken,
bitstamp: bitstamp,
itbit: itbit,
bitpay: bitpay,
binance: binance,
bitfinex: bitfinex,
}
function buildMarket(fiatCode, cryptoCode, serviceName) {
if (!_.includes(cryptoCode, ALL[serviceName].CRYPTO)) {
throw new Error('Unsupported crypto: ' + cryptoCode)
}
if (_.isNil(fiatCode))
throw new Error('Market pair building failed: Missing fiat code')
return cryptoCode + '/' + fiatCode
}
function verifyFiatSupport(fiatCode, serviceName) {
const fiat = ALL[serviceName].FIAT
return fiat === 'ALL_CURRENCIES' ? true : _.includes(fiatCode, fiat)
}
function isConfigValid(config, fields) {
const values = _.map(it => _.get(it)(config))(fields)
return _.every(it => it || it === 0)(values)
}
function defaultFiatMarket(serviceName) {
return ALL[serviceName].DEFAULT_FIAT_MARKET
}
module.exports = {
buildMarket,
ALL,
verifyFiatSupport,
isConfigValid,
defaultFiatMarket,
}

View file

@ -0,0 +1,151 @@
// JSON-RPC for bitcoind-like interfaces
const axios = require('axios')
const uuid = require('uuid')
const fs = require('fs')
const _ = require('lodash/fp')
const request = require('request-promise')
const { utils: coinUtils } = require('@lamassu/coins')
const logger = require('../../logger')
const { isRemoteWallet } = require('../../environment-helper')
const { isEnvironmentValid } = require('../../blockchain/install')
const BLOCKCHAIN_DIR = process.env.BLOCKCHAIN_DIR
module.exports = {
fetch,
fetchDigest,
parseConf,
rpcConfig,
}
function fetch(account = {}, method, params) {
params = _.defaultTo([], params)
return Promise.resolve(true)
.then(() => {
const data = {
method,
params,
id: uuid.v4(),
}
if (_.isNil(account.port))
throw new Error('port attribute required for jsonRpc')
const url = _.defaultTo(
`http://${account.host}:${account.port}`,
account.url,
)
return axios({
method: 'post',
auth: { username: account.username, password: account.password },
url,
data,
})
})
.then(r => {
if (r.error) throw r.error
return r.data.result
})
.catch(err => {
throw new Error(
JSON.stringify({
responseMessage: _.get('message', err),
message: _.get('response.data.error.message', err),
code: _.get('response.data.error.code', err),
}),
)
})
}
function generateDigestOptions(account = {}, method, params) {
const headers = {
'Content-Type': 'application/json',
}
const dataString = `{"jsonrpc":"2.0","id":"${uuid.v4()}","method":"${method}","params":${JSON.stringify(params)}}`
const options = {
url: `http://localhost:${account.port}/json_rpc`,
method: 'POST',
headers,
body: dataString,
forever: true,
auth: {
user: account.username,
pass: account.password,
sendImmediately: false,
},
}
return options
}
function fetchDigest(account = {}, method, params = []) {
return Promise.resolve(true).then(() => {
if (_.isNil(account.port))
throw new Error('port attribute required for jsonRpc')
const options = generateDigestOptions(account, method, params)
return request(options)
})
}
function split(str) {
const i = str.indexOf('=')
if (i === -1) return []
return [str.slice(0, i), str.slice(i + 1)]
}
function parseConf(confPath) {
const conf = fs.readFileSync(confPath)
const lines = conf.toString().split('\n')
const res = {}
for (let i = 0; i < lines.length; i++) {
const keyVal = split(lines[i])
// skip when value is empty
if (!keyVal[1]) continue
res[keyVal[0]] = keyVal[1]
}
return res
}
function rpcConfig(cryptoRec) {
try {
if (isRemoteWallet(cryptoRec) && isEnvironmentValid(cryptoRec)) {
return {
username: process.env[`${cryptoRec.cryptoCode}_NODE_USER`],
password: process.env[`${cryptoRec.cryptoCode}_NODE_PASSWORD`],
host: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_HOST`],
port: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_PORT`],
}
}
const configPath = coinUtils.configPath(cryptoRec, BLOCKCHAIN_DIR)
const config = parseConf(configPath)
return {
username: config.rpcuser,
password: config.rpcpassword,
host: 'localhost',
port: config.rpcport || cryptoRec.defaultPort,
}
} catch (err) {
if (!isEnvironmentValid(cryptoRec)) {
logger.error(
`Environment is not correctly setup for remote wallet usage!, ${err}`,
)
} else {
logger.error('Wallet is currently not installed!')
}
return {
port: cryptoRec.defaultPort,
}
}
}

View file

@ -0,0 +1,6 @@
module.exports = {
PENDING: 'PENDING',
RETRY: 'RETRY',
APPROVED: 'APPROVED',
REJECTED: 'REJECTED',
}

View file

@ -0,0 +1,31 @@
const uuid = require('uuid')
const { APPROVED } = require('../consts')
const CODE = 'mock-compliance'
const createLink = (settings, userId, level) => {
return Promise.resolve(
`https://mock-external-compliance.link/?user=${userId}&level=${level}`,
)
}
const getApplicantStatus = account => {
return Promise.resolve({
level: account.applicantLevel,
answer: APPROVED,
})
}
const createApplicant = () => {
return Promise.resolve({
id: uuid.v4(),
})
}
module.exports = {
CODE,
createApplicant,
getApplicantStatus,
createLink,
}

View file

@ -0,0 +1,37 @@
const axios = require('axios')
const crypto = require('crypto')
const _ = require('lodash/fp')
const FormData = require('form-data')
const axiosConfig = {
baseURL: 'https://api.sumsub.com',
}
const getSigBuilder = (apiToken, secretKey) => config => {
const timestamp = Math.floor(Date.now() / 1000)
const signature = crypto.createHmac('sha256', secretKey)
signature.update(`${timestamp}${_.toUpper(config.method)}${config.url}`)
if (config.data instanceof FormData) {
signature.update(config.data.getBuffer())
} else if (config.data) {
signature.update(JSON.stringify(config.data))
}
config.headers['X-App-Token'] = apiToken
config.headers['X-App-Access-Sig'] = signature.digest('hex')
config.headers['X-App-Access-Ts'] = timestamp
return config
}
const request = (account, config) => {
const instance = axios.create(axiosConfig)
instance.interceptors.request.use(
getSigBuilder(account.apiToken, account.secretKey),
Promise.reject,
)
return instance(config)
}
module.exports = request

View file

@ -0,0 +1,102 @@
const request = require('./request')
const createApplicant = (account, userId, level) => {
if (!userId || !level) {
return Promise.reject(
`Missing required fields: userId: ${userId}, level: ${level}`,
)
}
const config = {
method: 'POST',
url: `/resources/applicants?levelName=${level}`,
data: {
externalUserId: userId,
sourceKey: 'lamassu',
},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
}
return request(account, config)
}
const createLink = (account, userId, level) => {
if (!userId || !level) {
return Promise.reject(
`Missing required fields: userId: ${userId}, level: ${level}`,
)
}
const config = {
method: 'POST',
url: `/resources/sdkIntegrations/levels/${level}/websdkLink?ttlInSecs=${600}&externalUserId=${userId}`,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
}
return request(account, config)
}
const getApplicantByExternalId = (account, id) => {
if (!id) {
return Promise.reject('Missing required fields: id')
}
const config = {
method: 'GET',
url: `/resources/applicants/-;externalUserId=${id}/one`,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
return request(account, config)
}
const getApplicantStatus = (account, id) => {
if (!id) {
return Promise.reject(`Missing required fields: id`)
}
const config = {
method: 'GET',
url: `/resources/applicants/${id}/status`,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
return request(account, config)
}
const getApplicantById = (account, id) => {
if (!id) {
return Promise.reject(`Missing required fields: id`)
}
const config = {
method: 'GET',
url: `/resources/applicants/${id}/one`,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
return request(account, config)
}
module.exports = {
createLink,
createApplicant,
getApplicantByExternalId,
getApplicantById,
getApplicantStatus,
}

View file

@ -0,0 +1,58 @@
const _ = require('lodash/fp')
const sumsubApi = require('./sumsub.api')
const { PENDING, RETRY, APPROVED, REJECTED } = require('../consts')
const CODE = 'sumsub'
const getApplicantByExternalId = (account, userId) => {
return sumsubApi.getApplicantByExternalId(account, userId).then(r => r.data)
}
const createApplicant = (account, userId, level) => {
return sumsubApi
.createApplicant(account, userId, level)
.then(r => r.data)
.catch(err => {
if (err.response.status === 409)
return getApplicantByExternalId(account, userId)
throw err
})
}
const createLink = (account, userId, level) => {
return sumsubApi.createLink(account, userId, level).then(r => r.data.url)
}
const getApplicantStatus = (account, userId) => {
return sumsubApi.getApplicantByExternalId(account, userId).then(r => {
const levelName = _.get('data.review.levelName', r)
const reviewStatus = _.get('data.review.reviewStatus', r)
const reviewAnswer = _.get('data.review.reviewResult.reviewAnswer', r)
const reviewRejectType = _.get(
'data.review.reviewResult.reviewRejectType',
r,
)
// if last review was from a different level, return the current level and RETRY
if (levelName !== account.applicantLevel)
return { level: account.applicantLevel, answer: RETRY }
let answer = PENDING
if (reviewStatus === 'init') answer = RETRY
if (reviewAnswer === 'GREEN' && reviewStatus === 'completed')
answer = APPROVED
if (reviewAnswer === 'RED' && reviewRejectType === 'RETRY') answer = RETRY
if (reviewAnswer === 'RED' && reviewRejectType === 'FINAL')
answer = REJECTED
return { level: levelName, answer }
})
}
module.exports = {
CODE,
createApplicant,
getApplicantStatus,
createLink,
}

View file

@ -0,0 +1,37 @@
const Mailgun = require('mailgun-js')
const NAME = 'Mailgun'
function sendMessage({ apiKey, domain, fromEmail, toEmail }, rec) {
const mailgun = Mailgun({ apiKey, domain })
const to = rec.email.toEmail ?? toEmail
const emailData = {
from: `Lamassu Server ${fromEmail}`,
to,
subject: rec.email.subject,
text: rec.email.body,
}
return mailgun.messages().send(emailData)
}
function sendCustomerMessage({ apiKey, domain, fromEmail }, rec) {
const mailgun = Mailgun({ apiKey, domain })
const to = rec.email.toEmail
const emailData = {
from: fromEmail,
to,
subject: rec.email.subject,
text: rec.email.body,
}
return mailgun.messages().send(emailData)
}
module.exports = {
NAME,
sendMessage,
sendCustomerMessage,
}

View file

@ -0,0 +1,15 @@
const NAME = 'mock-email'
function sendMessage(settings, rec) {
console.log('sending email', rec)
}
function sendCustomerMessage(settings, rec) {
console.log('sending email', rec)
}
module.exports = {
NAME,
sendMessage,
sendCustomerMessage,
}

View file

@ -0,0 +1,28 @@
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, BCH, XMR, ETH, LTC, ZEC, LN } = COINS
const CRYPTO = [BTC, ETH, LTC, ZEC, BCH, XMR, LN]
const FIAT = ['EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const loadConfig = account => {
const mapper = {
privateKey: 'secret',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
}

View file

@ -0,0 +1,28 @@
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, BCH, DASH, ETH, LTC, ZEC, USDT, USDT_TRON, LN, USDC } = COINS
const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH, USDT, USDT_TRON, LN, USDC]
const FIAT = ['USD']
const DEFAULT_FIAT_MARKET = 'USD'
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const loadConfig = account => {
const mapper = {
privateKey: 'secret',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
}

View file

@ -0,0 +1,30 @@
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, ETH, LTC, BCH, USDT, LN, USDC } = COINS
const CRYPTO = [BTC, ETH, LTC, BCH, USDT, LN, USDC]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 8
const REQUIRED_CONFIG_FIELDS = ['key', 'secret']
const loadConfig = account => {
const mapper = {
key: 'apiKey',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = {
loadConfig,
REQUIRED_CONFIG_FIELDS,
DEFAULT_FIAT_MARKET,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -0,0 +1,31 @@
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, ETH, LTC, BCH, USDT, LN, USDC } = COINS
const CRYPTO = [BTC, ETH, LTC, BCH, USDT, LN, USDC]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 8
const REQUIRED_CONFIG_FIELDS = ['key', 'secret', 'clientId', 'currencyMarket']
const loadConfig = account => {
const mapper = {
key: 'apiKey',
clientId: 'uid',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -0,0 +1,129 @@
const { utils: coinUtils } = require('@lamassu/coins')
const _ = require('lodash/fp')
const ccxt = require('ccxt')
const mem = require('mem')
const { buildMarket, ALL, isConfigValid } = require('../common/ccxt')
const { ORDER_TYPES } = require('./consts')
const logger = require('../../logger')
const { currencies } = require('../../new-admin/config')
const T = require('../../time')
const DEFAULT_PRICE_PRECISION = 2
const DEFAULT_AMOUNT_PRECISION = 8
function trade(side, account, tradeEntry, exchangeName) {
const { cryptoAtoms, cryptoCode: _cryptoCode, tradeId } = tradeEntry
try {
const cryptoCode = coinUtils.getEquivalentCode(_cryptoCode)
const exchangeConfig = ALL[exchangeName]
if (!exchangeConfig) throw Error('Exchange configuration not found')
const {
USER_REF,
loadOptions,
loadConfig = _.noop,
REQUIRED_CONFIG_FIELDS,
ORDER_TYPE,
AMOUNT_PRECISION,
} = exchangeConfig
if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS))
throw Error('Invalid config')
const selectedFiatMarket = account.currencyMarket
const symbol = buildMarket(selectedFiatMarket, cryptoCode, exchangeName)
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)
const amount = coinUtils.toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
const accountOptions = _.isFunction(loadOptions) ? loadOptions(account) : {}
const withCustomKey = USER_REF ? { [USER_REF]: tradeId } : {}
const options = _.assign(accountOptions, withCustomKey)
const exchange = new ccxt[exchangeName](loadConfig(account))
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
return exchange.createOrder(
symbol,
ORDER_TYPES.MARKET,
side,
amount,
null,
options,
)
}
return exchange.fetchOrderBook(symbol).then(orderBook => {
const price = calculatePrice(side, amount, orderBook).toFixed(
DEFAULT_PRICE_PRECISION,
)
return exchange.createOrder(
symbol,
ORDER_TYPES.LIMIT,
side,
amount,
price,
options,
)
})
} catch (e) {
return Promise.reject(e)
}
}
function calculatePrice(side, amount, orderBook) {
const book = side === 'buy' ? 'asks' : 'bids'
let collected = 0.0
for (const entry of orderBook[book]) {
collected += parseFloat(entry[1])
if (collected >= amount) return parseFloat(entry[0])
}
throw new Error('Insufficient market depth')
}
function _getMarkets(exchangeName, availableCryptos) {
const prunedCryptos = _.compose(
_.uniq,
_.map(coinUtils.getEquivalentCode),
)(availableCryptos)
try {
const exchange = new ccxt[exchangeName]()
const cryptosToQuoteAgainst = ['USDT']
const currencyCodes = _.concat(
_.map(it => it.code, currencies),
cryptosToQuoteAgainst,
)
return exchange
.fetchMarkets()
.then(_.filter(it => it.type === 'spot' || it.spot))
.then(res =>
_.reduce(
(acc, value) => {
if (
_.includes(value.base, prunedCryptos) &&
_.includes(value.quote, currencyCodes)
) {
if (value.quote === value.base) return acc
if (_.isNil(acc[value.quote])) {
return { ...acc, [value.quote]: [value.base] }
}
acc[value.quote].push(value.base)
}
return acc
},
{},
res,
),
)
} catch (e) {
logger.debug(`No CCXT exchange found for ${exchangeName}. ${e}`)
}
}
const getMarkets = mem(_getMarkets, {
maxAge: T.week,
cacheKey: exchangeName => exchangeName,
})
module.exports = { trade, getMarkets }

View file

@ -0,0 +1,28 @@
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, BCH, DASH, ETH, LTC, USDT, TRX, USDT_TRON, LN } = COINS
const CRYPTO = [BTC, ETH, LTC, DASH, BCH, USDT, TRX, USDT_TRON, LN]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const loadConfig = account => {
const mapper = {
privateKey: 'secret',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
}

View file

@ -0,0 +1,6 @@
const ORDER_TYPES = {
MARKET: 'market',
LIMIT: 'limit',
}
module.exports = { ORDER_TYPES }

View file

@ -0,0 +1,42 @@
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const { COINS } = require('@lamassu/coins')
const ORDER_TYPE = ORDER_TYPES.LIMIT
const { BTC, ETH, USDT, LN } = COINS
const CRYPTO = [BTC, ETH, USDT, LN]
const FIAT = ['USD']
const DEFAULT_FIAT_MARKET = 'USD'
const AMOUNT_PRECISION = 4
const REQUIRED_CONFIG_FIELDS = [
'clientKey',
'clientSecret',
'userId',
'walletId',
'currencyMarket',
]
const loadConfig = account => {
const mapper = {
clientKey: 'apiKey',
clientSecret: 'secret',
userId: 'uid',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(
_.omit(['walletId'], account),
)
return { ...mapped, timeout: 3000 }
}
const loadOptions = ({ walletId }) => ({ walletId })
module.exports = {
loadOptions,
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -0,0 +1,56 @@
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const { COINS } = require('@lamassu/coins')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, BCH, DASH, ETH, LTC, ZEC, XMR, USDT, TRX, USDT_TRON, LN, USDC } =
COINS
const CRYPTO = [
BTC,
ETH,
LTC,
DASH,
ZEC,
BCH,
XMR,
USDT,
TRX,
USDT_TRON,
LN,
USDC,
]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 6
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const USER_REF = 'userref'
const loadConfig = account => {
const mapper = {
privateKey: 'secret',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return {
...mapped,
timeout: 3000,
nonce: function () {
return this.microseconds()
},
}
}
const loadOptions = () => ({ expiretm: '+60' })
module.exports = {
USER_REF,
loadOptions,
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -0,0 +1,24 @@
module.exports = {
buy,
sell,
}
function buy(cryptoAtoms, fiatCode, cryptoCode) {
console.log(
'[mock] buying %s %s for %s',
cryptoAtoms.toString(),
cryptoCode,
fiatCode,
)
return Promise.resolve()
}
function sell(cryptoAtoms, fiatCode, cryptoCode) {
console.log(
'[mock] selling %s %s for %s',
cryptoAtoms.toString(),
cryptoCode,
fiatCode,
)
return Promise.resolve()
}

View file

@ -0,0 +1,47 @@
const axios = require('axios')
const NAME = 'InforU'
function sendMessage(account, rec) {
const username = account.username
const apiKey = account.apiKey
const to = rec.sms.toNumber || account.toNumber
const text = rec.sms.body
const from = account.fromNumber
const url = 'https://capi.inforu.co.il/api/v2/SMS/SendSms'
const config = {
auth: {
username: username,
password: apiKey,
},
maxBodyLength: Infinity,
headers: {
'Content-Type': 'application/json',
},
}
const data = {
Message: text,
Recipients: [
{
Phone: to,
},
],
Settings: {
Sender: from,
},
}
axios.post(url, data, config).catch(err => {
// console.log(err)
throw new Error(`inforu error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -0,0 +1,19 @@
const _ = require('lodash/fp')
const NAME = 'MockSMS'
function sendMessage(account, rec) {
console.log('Sending SMS: %j', rec)
return new Promise((resolve, reject) => {
if (_.endsWith('666', _.getOr(false, 'sms.toNumber', rec))) {
reject(new Error(`${exports.NAME} mocked error!`))
} else {
setTimeout(resolve, 10)
}
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -0,0 +1,20 @@
const Telnyx = require('telnyx')
const NAME = 'Telnyx'
function sendMessage(account, rec) {
const telnyx = Telnyx(account.apiKey)
const from = account.fromNumber
const text = rec.sms.body
const to = rec.sms.toNumber || account.toNumber
return telnyx.messages.create({ from, to, text }).catch(err => {
throw new Error(`Telnyx error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -0,0 +1,47 @@
const twilio = require('twilio')
const _ = require('lodash/fp')
const NAME = 'Twilio'
const BAD_NUMBER_CODES = [
21201, 21202, 21211, 21214, 21216, 21217, 21219, 21408, 21610, 21612, 21614,
21608,
]
function sendMessage(account, rec) {
return Promise.resolve()
.then(() => {
// to catch configuration errors like
// "Error: username is required"
const client = twilio(account.accountSid, account.authToken)
const body = rec.sms.body
const _toNumber = rec.sms.toNumber || account.toNumber
const from =
_.startsWith('+')(account.fromNumber) ||
!_.isNumber(String(account.fromNumber).replace(/\s/g, ''))
? account.fromNumber
: `+${account.fromNumber}`
const opts = {
body: body,
to: _toNumber,
from,
}
return client.messages.create(opts)
})
.catch(err => {
if (_.includes(err.code, BAD_NUMBER_CODES)) {
const badNumberError = new Error(err.message)
badNumberError.name = 'BadNumberError'
throw badNumberError
}
throw new Error(`Twilio error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -0,0 +1,25 @@
const { Auth } = require('@vonage/auth')
const { SMS } = require('@vonage/sms')
const NAME = 'Vonage'
function sendMessage(account, rec) {
const credentials = new Auth({
apiKey: account.apiKey,
apiSecret: account.apiSecret,
})
const from = account.fromNumber
const text = rec.sms.body
const to = rec.sms.toNumber || account.toNumber
const smsClient = new SMS(credentials)
smsClient.send({ from, text, to }).catch(err => {
throw new Error(`Vonage error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -0,0 +1,41 @@
const axios = require('axios')
const NAME = 'Whatsapp'
function sendMessage(account, rec) {
const phoneId = account.phoneId
const token = account.apiKey
const to = rec.sms.toNumber || account.toNumber
const template = rec.sms.template
const url = `https://graph.facebook.com/v17.0/${phoneId}/messages`
const config = {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
}
const data = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
type: 'template',
to,
template: {
name: template,
language: { code: 'en_US' },
},
}
axios.post(url, data, config).catch(err => {
// console.log(err)
throw new Error(`Whatsapp error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -0,0 +1,30 @@
const axios = require('axios')
const { COINS } = require('@lamassu/coins')
const BN = require('../../bn')
const { BTC, BCH, LN } = COINS
const CRYPTO = [BTC, BCH, LN]
const FIAT = 'ALL_CURRENCIES'
function ticker(fiatCode, cryptoCode) {
return axios
.get('https://bitpay.com/rates/' + cryptoCode + '/' + fiatCode)
.then(r => {
const data = r.data.data
const price = new BN(data.rate.toString())
return {
rates: {
ask: price,
bid: price,
},
}
})
}
module.exports = {
ticker,
name: 'BitPay',
CRYPTO,
FIAT,
}

View file

@ -0,0 +1,77 @@
const ccxt = require('ccxt')
const BN = require('../../bn')
const {
buildMarket,
verifyFiatSupport,
defaultFiatMarket,
} = require('../common/ccxt')
const { getRate } = require('../../../lib/forex')
const RETRIES = 2
const tickerObjects = {}
// This is probably fixed on upstream ccxt
// but we need to udpate node to get on the latest version
const sanityCheckRates = (ask, bid, tickerName) => {
if (new BN(0).eq(ask) || new BN(0).eq(bid)) {
throw new Error(`Failure fetching rates for ${tickerName}`)
}
}
function ticker(fiatCode, cryptoCode, tickerName) {
if (!tickerObjects[tickerName]) {
tickerObjects[tickerName] = new ccxt[tickerName]({
timeout: 3000,
enableRateLimit: false,
})
}
const ticker = tickerObjects[tickerName]
if (verifyFiatSupport(fiatCode, tickerName)) {
return getCurrencyRates(ticker, fiatCode, cryptoCode)
}
return getRate(RETRIES, fiatCode, defaultFiatMarket(tickerName)).then(
({ fxRate }) => {
try {
return getCurrencyRates(
ticker,
defaultFiatMarket(tickerName),
cryptoCode,
).then(res => ({
rates: {
ask: res.rates.ask.times(fxRate),
bid: res.rates.bid.times(fxRate),
},
}))
} catch (e) {
return Promise.reject(e)
}
},
)
}
function getCurrencyRates(ticker, fiatCode, cryptoCode) {
try {
if (!ticker.has['fetchTicker']) {
throw new Error('Ticker not available')
}
const symbol = buildMarket(fiatCode, cryptoCode, ticker.id)
return ticker.fetchTicker(symbol).then(res => {
sanityCheckRates(res.ask, res.bid, cryptoCode)
return {
rates: {
ask: new BN(res.ask),
bid: new BN(res.bid),
},
}
})
} catch (e) {
return Promise.reject(e)
}
}
module.exports = { ticker }

View file

@ -0,0 +1,12 @@
const BN = require('../../bn')
function ticker() {
return Promise.resolve({
rates: {
ask: new BN(100),
bid: new BN(95),
},
})
}
module.exports = { ticker }

View file

@ -0,0 +1,27 @@
const https = require('https')
const axios = require('axios').create({
// TODO: get rejectUnauthorized true to work
baseURL: `${process.env.TICKER_URL}/api/rates/`,
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
})
const BN = require('../../../bn')
function ticker(account, fiatCode, cryptoCode) {
return axios.get(`${cryptoCode}/${fiatCode}`).then(({ data }) => {
if (data.error) throw new Error(JSON.stringify(data.error))
return {
rates: {
ask: BN(data.ask),
bid: BN(data.bid),
signature: data.signature,
},
}
})
}
module.exports = {
ticker,
}

View file

@ -0,0 +1,272 @@
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "version",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
},
{
"name": "_extraData",
"type": "bytes"
}
],
"name": "approveAndCall",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "remaining",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "_initialAmount",
"type": "uint256"
},
{
"name": "_tokenName",
"type": "string"
},
{
"name": "_decimalUnits",
"type": "uint8"
},
{
"name": "_tokenSymbol",
"type": "string"
}
],
"type": "constructor"
},
{
"payable": false,
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_from",
"type": "address"
},
{
"indexed": true,
"name": "_to",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_owner",
"type": "address"
},
{
"indexed": true,
"name": "_spender",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
]

View file

@ -0,0 +1,3 @@
const ERC20 = require('./erc20.abi')
module.exports = { ERC20 }

View file

@ -0,0 +1,101 @@
const { AML } = require('elliptic-sdk')
const _ = require('lodash/fp')
const NAME = 'Elliptic'
const HOLLISTIC_COINS = {
BTC: 'BTC',
ETH: 'ETH',
USDT: 'USDT',
USDT_TRON: 'USDT',
LTC: 'LTC',
TRX: 'TRX',
}
const SINGLE_ASSET_COINS = {
ZEC: {
asset: 'ZEC',
blockchain: 'zcash',
},
BCH: {
asset: 'BCH',
blockchain: 'bitcoin_cash',
},
}
const TYPE = {
TRANSACTION: 'transaction',
ADDRESS: 'address',
}
const SUPPORTED_COINS = { ...HOLLISTIC_COINS, ...SINGLE_ASSET_COINS }
function rate(account, objectType, cryptoCode, objectId) {
return isWalletScoringEnabled(account, cryptoCode).then(isEnabled => {
if (!isEnabled) return Promise.resolve(null)
const aml = new AML({
key: account.apiKey,
secret: account.apiSecret,
})
const isHolistic = Object.keys(HOLLISTIC_COINS).includes(cryptoCode)
const requestBody = {
subject: {
asset: isHolistic ? 'holistic' : SINGLE_ASSET_COINS[cryptoCode].asset,
blockchain: isHolistic
? 'holistic'
: SINGLE_ASSET_COINS[cryptoCode].blockchain,
type: objectType,
hash: objectId,
},
type: objectType === TYPE.ADDRESS ? 'wallet_exposure' : 'source_of_funds',
}
const threshold = account.scoreThreshold
const endpoint =
objectType === TYPE.ADDRESS
? '/v2/wallet/synchronous'
: '/v2/analysis/synchronous'
return aml.client.post(endpoint, requestBody).then(res => {
const resScore = res.data?.risk_score
// elliptic returns 0-1 score, but we're accepting 0-100 config
// normalize score to 0-10 where 0 is the lowest risk
// elliptic score can be null and contains decimals
return {
score: (resScore || 0) * 10,
isValid: (resScore || 0) * 100 < threshold,
}
})
})
}
function rateTransaction(account, cryptoCode, transactionId) {
return rate(account, TYPE.TRANSACTION, cryptoCode, transactionId)
}
function rateAddress(account, cryptoCode, address) {
return rate(account, TYPE.ADDRESS, cryptoCode, address)
}
function isWalletScoringEnabled(account, cryptoCode) {
const isAccountEnabled = !_.isNil(account) && account.enabled
if (!isAccountEnabled) return Promise.resolve(false)
if (!Object.keys(SUPPORTED_COINS).includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
return Promise.resolve(true)
}
module.exports = {
NAME,
rateAddress,
rateTransaction,
isWalletScoringEnabled,
}

View file

@ -0,0 +1,32 @@
const NAME = 'FakeScoring'
const { WALLET_SCORE_THRESHOLD } = require('../../../constants')
function rateAddress(account, cryptoCode, address) {
return new Promise(resolve => {
setTimeout(() => {
console.log(
'[WALLET-SCORING] DEBUG: Mock scoring rating wallet address %s',
address,
)
return Promise.resolve(2).then(score =>
resolve({ address, score, isValid: score < WALLET_SCORE_THRESHOLD }),
)
}, 100)
})
}
function isWalletScoringEnabled() {
return new Promise(resolve => {
setTimeout(() => {
return resolve(true)
}, 100)
})
}
module.exports = {
NAME,
rateAddress,
rateTransaction: rateAddress,
isWalletScoringEnabled,
}

View file

@ -0,0 +1,82 @@
const axios = require('axios')
const _ = require('lodash/fp')
const NAME = 'Scorechain'
const SUPPORTED_COINS = {
BTC: 'BITCOIN',
ETH: 'ETHEREUM',
USDT: 'ETHEREUM',
BCH: 'BITCOINCASH',
LTC: 'LITECOIN',
DASH: 'DASH',
TRX: 'TRON',
USDT_TRON: 'TRON',
}
const TYPE = {
TRANSACTION: 'TRANSACTION',
ADDRESS: 'ADDRESS',
}
function rate(account, objectType, cryptoCode, objectId) {
return isWalletScoringEnabled(account, cryptoCode).then(isEnabled => {
if (!isEnabled) return Promise.resolve(null)
const threshold = account.scoreThreshold
const payload = {
analysisType: 'ASSIGNED',
objectType,
objectId,
blockchain: SUPPORTED_COINS[cryptoCode],
coin: 'ALL',
}
const headers = {
accept: 'application/json',
'X-API-KEY': account.apiKey,
'Content-Type': 'application/json',
}
return axios
.post(`https://api.scorechain.com/v1/scoringAnalysis`, payload, {
headers,
})
.then(res => {
const resScore = res.data?.analysis?.assigned?.result?.score
if (!resScore)
throw new Error('Failed to get score from Scorechain API')
// normalize score to 0-10 where 0 is the lowest risk
return { score: (100 - resScore) / 10, isValid: resScore >= threshold }
})
.catch(err => {
throw err
})
})
}
function rateTransaction(account, cryptoCode, transactionId) {
return rate(account, TYPE.TRANSACTION, cryptoCode, transactionId)
}
function rateAddress(account, cryptoCode, address) {
return rate(account, TYPE.ADDRESS, cryptoCode, address)
}
function isWalletScoringEnabled(account, cryptoCode) {
const isAccountEnabled = !_.isNil(account) && account.enabled
if (!isAccountEnabled) return Promise.resolve(false)
if (!Object.keys(SUPPORTED_COINS).includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
return Promise.resolve(true)
}
module.exports = {
NAME,
rateAddress,
rateTransaction,
isWalletScoringEnabled,
}

View file

@ -0,0 +1,163 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const BN = require('../../../bn')
const E = require('../../../error')
const { utils: coinUtils } = require('@lamassu/coins')
const cryptoRec = coinUtils.getCryptoCurrency('BCH')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'BCH')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically.
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main',
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => fetch('listreceivedbyaddress', [0, true, true, address]))
.then(txsByAddress =>
Promise.all(
_.map(
id => fetch('getrawtransaction', [id]),
_.flatMap(it => it.txids, txsByAddress),
),
),
)
.then(_.map(({ hash }) => hash))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,252 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const { getSatBEstimateFee } = require('../../../blockexplorers/mempool.space')
const BN = require('../../../bn')
const E = require('../../../error')
const logger = require('../../../logger')
const { utils: coinUtils } = require('@lamassu/coins')
const cryptoRec = coinUtils.getCryptoCurrency('BTC')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
const SUPPORTS_BATCHING = true
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -5:
return logger.error(`${err}`)
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'BTC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalances'))
.then(({ mine }) =>
new BN(mine.trusted).shiftedBy(unitScale).decimalPlaces(0),
)
.catch(errorHandle)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalances'))
.then(({ mine }) =>
new BN(mine.untrusted_pending)
.plus(mine.immature)
.shiftedBy(unitScale)
.decimalPlaces(0),
)
.catch(errorHandle)
}
// We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically.
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function estimateFee() {
return getSatBEstimateFee()
.then(result => BN(result))
.catch(err => {
logger.error('failure estimating fes', err)
})
}
function calculateFeeDiscount(feeMultiplier = 1, unitScale) {
// 0 makes bitcoind do automatic fee selection
const AUTOMATIC_FEE = 0
return estimateFee().then(estimatedFee => {
if (!estimatedFee) {
logger.info(
'failure estimating fee, using bitcoind automatic fee selection',
)
return AUTOMATIC_FEE
}
// transform from sat/vB to BTC/kvB and apply the multipler
const newFee = estimatedFee.shiftedBy(-unitScale + 3).times(feeMultiplier)
if (newFee.lt(0.00001) || newFee.gt(0.1)) {
logger.info(
'fee outside safety parameters, defaulting to automatic fee selection',
)
return AUTOMATIC_FEE
}
return newFee.toFixed(8)
})
}
function sendCoins(account, tx, settings, operatorId, feeMultiplier) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => calculateFeeDiscount(feeMultiplier, unitScale))
.then(newFee => fetch('settxfee', [newFee]))
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function sendCoinsBatch(account, txs, cryptoCode, feeMultiplier) {
return checkCryptoCode(cryptoCode)
.then(() => calculateFeeDiscount(feeMultiplier, unitScale))
.then(newFee => fetch('settxfee', [newFee]))
.then(() =>
_.reduce(
(acc, value) => ({
...acc,
[value.toAddress]: _.isNil(acc[value.toAddress])
? BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8)
: BN(acc[value.toAddress]).plus(
BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8),
),
}),
{},
txs,
),
)
.then(obj => fetch('sendmany', ['', obj]))
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => ({
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}))
.catch(errorHandle)
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
.catch(errorHandle)
}
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
.catch(errorHandle)
}
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
.catch(errorHandle)
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main',
)
}
function fetchRBF(txId) {
return fetch('getmempoolentry', [txId])
.then(res => {
return [txId, res['bip125-replaceable']]
})
.catch(err => {
errorHandle(err)
return [txId, true]
})
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => fetch('listreceivedbyaddress', [0, true, true, address]))
.then(txsByAddress =>
Promise.all(
_.map(
id => fetch('getrawtransaction', [id]),
_.flatMap(it => it.txids, txsByAddress),
),
),
)
.then(_.map(({ hash }) => hash))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
fetchRBF,
sendCoinsBatch,
checkBlockchainStatus,
getTxHashesByAddress,
fetch,
SUPPORTS_BATCHING,
}

View file

@ -0,0 +1,191 @@
const _ = require('lodash/fp')
const { BitGoAPI } = require('@bitgo/sdk-api')
const { toLegacyAddress, toCashAddress } = require('bchaddrjs')
const BN = require('../../../bn')
const E = require('../../../error')
const pjson = require('../../../../package.json')
const userAgent = 'Lamassu-Server/' + pjson.version
const NAME = 'BitGo'
const BITGO_MODULES = {
BCH: require('@bitgo/sdk-coin-bch'),
BTC: require('@bitgo/sdk-coin-btc'),
DASH: require('@bitgo/sdk-coin-dash'),
LTC: require('@bitgo/sdk-coin-ltc'),
ZEC: require('@bitgo/sdk-coin-zec'),
}
const SUPPORTED_COINS = _.keys(BITGO_MODULES)
const BCH_CODES = ['BCH', 'TBCH']
const getWallet = (account, cryptoCode) => {
const accessToken = account.token.trim()
const env = account.environment === 'test' ? 'test' : 'prod'
const walletId = account[`${cryptoCode}WalletId`]
const bitgo = new BitGoAPI({ accessToken, env, userAgent })
BITGO_MODULES[cryptoCode].register(bitgo)
cryptoCode = cryptoCode.toLowerCase()
const coin = env === 'test' ? `t${cryptoCode}` : cryptoCode
return bitgo.coin(coin).wallets().get({ id: walletId })
}
function checkCryptoCode(cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
return Promise.resolve()
}
function getLegacyAddress(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
return toLegacyAddress(address)
}
function getCashAddress(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
return toCashAddress(address)
}
function formatToGetStatus(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
const [part1, part2] = getLegacyAddress(address, cryptoCode).split(':')
return part2 || part1
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet => {
const params = {
address: getLegacyAddress(toAddress, cryptoCode),
amount: cryptoAtoms.toNumber(),
walletPassphrase: account[`${cryptoCode}WalletPassphrase`],
enforceMinConfirmsForChange: false,
}
return wallet.send(params)
})
.then(result => {
let fee = parseFloat(result.transfer.feeString)
let txid = result.transfer.txid
return { txid: txid, fee: new BN(fee).decimalPlaces(0) }
})
.catch(err => {
if (err.message === 'insufficient funds')
throw new E.InsufficientFundsError()
throw err
})
}
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet => new BN(wallet._wallet.spendableBalanceString))
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => getWallet(account, info.cryptoCode))
.then(wallet => {
return wallet.createAddress().then(result => {
const address = result.address
// If a label was provided, set the label
if (info.label) {
return wallet
.updateAddress({ address: address, label: info.label })
.then(() => getCashAddress(address, info.cryptoCode))
}
return getCashAddress(address, info.cryptoCode)
})
})
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet =>
wallet.transfers({
type: 'receive',
address: formatToGetStatus(toAddress, cryptoCode),
}),
)
.then(({ transfers }) => {
const filterConfirmed = _.filter(
it => it.state === 'confirmed' && it.type === 'receive',
)
const filterPending = _.filter(
it =>
(it.state === 'confirmed' || it.state === 'unconfirmed') &&
it.type === 'receive',
)
const sum = _.reduce((acc, val) => val.plus(acc), new BN(0))
const toBn = _.map(it => new BN(it.valueString))
const confirmed = _.compose(sum, toBn, filterConfirmed)(transfers)
const pending = _.compose(sum, toBn, filterPending)(transfers)
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => {
return getWallet(account, cryptoCode).then(wallet => {
return wallet.createAddress().then(result => {
const fundingAddress = result.address
return wallet
.updateAddress({ address: fundingAddress, label: 'Funding Address' })
.then(() => ({
fundingPendingBalance: new BN(wallet._wallet.balance).minus(
wallet._wallet.confirmedBalance,
),
fundingConfirmedBalance: new BN(wallet._wallet.confirmedBalance),
fundingAddress: getCashAddress(fundingAddress, cryptoCode),
}))
})
})
})
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
account.environment === 'test' ? 'test' : 'main',
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
NAME,
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
}

View file

@ -0,0 +1,157 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const { utils: coinUtils } = require('@lamassu/coins')
const BN = require('../../../bn')
const E = require('../../../error')
const cryptoRec = coinUtils.getCryptoCurrency('DASH')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'DASH')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically.
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => fetch('listreceivedbyaddress', [0, true, true, true, address]))
.then(txsByAddress =>
Promise.all(
_.map(
id => fetch('getrawtransaction', [id]),
_.flatMap(it => it.txids, txsByAddress),
),
),
)
.then(_.map(({ hash }) => hash))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,436 @@
const _ = require('lodash/fp')
const axios = require('axios')
const { utils: coinUtils } = require('@lamassu/coins')
const NAME = 'LN'
const SUPPORTED_COINS = ['LN']
const BN = require('../../../bn')
function request(graphqlQuery, token, endpoint) {
const headers = {
'content-type': 'application/json',
'X-API-KEY': token,
}
return axios({
method: 'post',
url: endpoint,
headers: headers,
data: graphqlQuery,
})
.then(r => {
if (r.error) throw r.error
return r.data
})
.catch(err => {
throw new Error(err)
})
}
function checkCryptoCode(cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
return Promise.resolve()
}
function getTransactionsByAddress(token, endpoint, walletId, address) {
const accountInfo = {
operationName: 'me',
query: `query me($walletId: WalletId!, , $address: OnChainAddress!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
transactionsByAddress (address: $address) {
edges {
node {
direction
settlementAmount
status
}
}
}
}
}
}
}`,
variables: { walletId, address },
}
return request(accountInfo, token, endpoint)
.then(r => {
return r.data.me.defaultAccount.walletById.transactionsByAddress
})
.catch(err => {
throw new Error(err)
})
}
function getGaloyWallet(token, endpoint, walletId) {
const accountInfo = {
operationName: 'me',
query: `query me($walletId: WalletId!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
id
walletCurrency
balance
}
}
}
}`,
variables: { walletId },
}
return request(accountInfo, token, endpoint)
.then(r => {
return r.data.me.defaultAccount.walletById
})
.catch(err => {
throw new Error(err)
})
}
function isLnInvoice(address) {
return address.toLowerCase().startsWith('lnbc')
}
function isLnurl(address) {
return address.toLowerCase().startsWith('lnurl')
}
function sendFundsOnChain(walletId, address, cryptoAtoms, token, endpoint) {
const sendOnChain = {
operationName: 'onChainPaymentSend',
query: `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) {
onChainPaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
variables: { input: { address, amount: cryptoAtoms.toString(), walletId } },
}
return request(sendOnChain, token, endpoint).then(result => {
return result.data.onChainPaymentSend
})
}
function sendFundsLNURL(walletId, lnurl, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
operationName: 'lnurlPaymentSend',
query: `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
lnurlPaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
variables: {
input: {
lnurl: `${lnurl}`,
walletId: `${walletId}`,
amount: `${cryptoAtoms}`,
},
},
}
return request(sendLnNoAmount, token, endpoint).then(
result => result.data.lnurlPaymentSend,
)
}
function sendFundsLN(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
operationName: 'lnNoAmountInvoicePaymentSend',
query: `mutation lnNoAmountInvoicePaymentSend($input: LnNoAmountInvoicePaymentInput!) {
lnNoAmountInvoicePaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
variables: {
input: {
paymentRequest: invoice,
walletId,
amount: cryptoAtoms.toString(),
},
},
}
return request(sendLnNoAmount, token, endpoint).then(
result => result.data.lnNoAmountInvoicePaymentSend,
)
}
function sendProbeRequest(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendProbeNoAmount = {
operationName: 'lnNoAmountInvoiceFeeProbe',
query: `mutation lnNoAmountInvoiceFeeProbe($input: LnNoAmountInvoiceFeeProbeInput!) {
lnNoAmountInvoiceFeeProbe(input: $input) {
amount
errors {
message
path
}
}
}`,
variables: {
input: {
paymentRequest: invoice,
walletId,
amount: cryptoAtoms.toString(),
},
},
}
return request(sendProbeNoAmount, token, endpoint).then(
result => result.data.lnNoAmountInvoiceFeeProbe,
)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => {
if (isLnInvoice(toAddress)) {
return sendFundsLN(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
}
if (isLnurl(toAddress)) {
return sendFundsLNURL(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
}
return sendFundsOnChain(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
})
.then(result => {
switch (result.status) {
case 'ALREADY_PAID':
throw new Error('Transaction already exists!')
case 'FAILURE':
throw new Error('Transaction failed!', JSON.stringify(result.errors))
case 'SUCCESS':
return '<galoy transaction>'
case 'PENDING':
return '<galoy transaction>'
default:
throw new Error(
`Transaction failed: ${_.head(result.errors).message}`,
)
}
})
}
function probeLN(account, cryptoCode, invoice) {
const probeHardLimits = [200000, 1000000, 2000000]
const promises = probeHardLimits.map(limit => {
return sendProbeRequest(
account.walletId,
invoice,
limit,
account.apiSecret,
account.endpoint,
).then(r => _.isEmpty(r.errors))
})
return Promise.all(promises).then(results =>
_.zipObject(probeHardLimits, results),
)
}
function newOnChainAddress(walletId, token, endpoint) {
const createOnChainAddress = {
operationName: 'onChainAddressCreate',
query: `mutation onChainAddressCreate($input: OnChainAddressCreateInput!) {
onChainAddressCreate(input: $input) {
address
errors {
message
path
}
}
}`,
variables: { input: { walletId } },
}
return request(createOnChainAddress, token, endpoint).then(result => {
return result.data.onChainAddressCreate.address
})
}
function newInvoice(walletId, cryptoAtoms, token, endpoint) {
const createInvoice = {
operationName: 'lnInvoiceCreate',
query: `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) {
lnInvoiceCreate(input: $input) {
errors {
message
path
}
invoice {
paymentRequest
}
}
}`,
variables: { input: { walletId, amount: cryptoAtoms.toString() } },
}
return request(createInvoice, token, endpoint).then(result => {
return result.data.lnInvoiceCreate.invoice.paymentRequest
})
}
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
return new BN(wallet.balance || 0)
})
}
function newAddress(account, info, tx) {
const { cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode).then(() =>
newInvoice(
account.walletId,
cryptoAtoms,
account.apiSecret,
account.endpoint,
),
)
}
function getInvoiceStatus(token, endpoint, address) {
const query = {
operationName: 'lnInvoicePaymentStatus',
query: `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
lnInvoicePaymentStatus(input: $input) {
status
}
}`,
variables: { input: { paymentRequest: address } },
}
return request(query, token, endpoint)
.then(r => {
return r?.data?.lnInvoicePaymentStatus?.status
})
.catch(err => {
throw new Error(err)
})
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const getBalance = _.reduce(
(acc, value) => {
acc[value.node.status] = acc[value.node.status].plus(
new BN(value.node.settlementAmount),
)
return acc
},
{ SUCCESS: new BN(0), PENDING: new BN(0), FAILURE: new BN(0) },
)
return checkCryptoCode(cryptoCode).then(() => {
const address = coinUtils.parseUrl(
cryptoCode,
account.environment,
toAddress,
false,
)
if (isLnInvoice(address)) {
return getInvoiceStatus(
account.apiSecret,
account.endpoint,
address,
).then(it => {
const isPaid = it === 'PAID'
if (isPaid)
return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' }
return { receivedCryptoAtoms: BN(0), status: 'notSeen' }
})
}
// On-chain and intra-ledger transactions
return getTransactionsByAddress(
account.apiSecret,
account.endpoint,
account.walletId,
address,
).then(transactions => {
const { SUCCESS: confirmed, PENDING: pending } = getBalance(
transactions.edges,
)
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding(account, cryptoCode) {
// Regular BTC address
return checkCryptoCode(cryptoCode)
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
return newOnChainAddress(
account.walletId,
account.apiSecret,
account.endpoint,
).then(onChainAddress => [onChainAddress, wallet.balance])
})
.then(([onChainAddress, balance]) => {
return {
// with the old api is not possible to get pending balance
fundingPendingBalance: new BN(0),
fundingConfirmedBalance: new BN(balance),
fundingAddress: onChainAddress,
}
})
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
account.environment === 'test' ? 'test' : 'main',
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
NAME,
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
probeLN,
}

View file

@ -0,0 +1,399 @@
'use strict'
const _ = require('lodash/fp')
const Web3 = require('web3')
const web3 = new Web3()
const hdkey = require('ethereumjs-wallet/hdkey')
const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx')
const { default: Common, Chain, Hardfork } = require('@ethereumjs/common')
const { default: PQueue } = require('p-queue')
const util = require('ethereumjs-util')
const coins = require('@lamassu/coins')
const _pify = require('pify')
const BN = require('../../../bn')
const ABI = require('../../tokens')
const logger = require('../../../logger')
const paymentPrefixPath = "m/44'/60'/0'/0'"
const defaultPrefixPath = "m/44'/60'/1'/0'"
let lastUsedNonces = {}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
sweep,
defaultAddress,
supportsHd: true,
newFunding,
privateKey,
isStrictAddress,
connect,
checkBlockchainStatus,
getTxHashesByAddress,
_balance,
}
const SWEEP_QUEUE = new PQueue({
concurrency: 3,
interval: 250,
})
const SEND_QUEUE = new PQueue({
concurrency: 1,
})
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const QUEUE_EXECUTION_DELAY = 250
const nodeCalls = {}
const pify = _function => {
if (_.isString(_function.call)) logInfuraCall(_function.call)
return _pify(_function)
}
const logInfuraCall = call => {
_.isNil(nodeCalls[call]) ? (nodeCalls[call] = 1) : nodeCalls[call]++
logger.info(
`Calling web3 method ${call}. Current count for this session: ${JSON.stringify(nodeCalls)}`,
)
}
function connect(url) {
web3.setProvider(new web3.providers.HttpProvider(url))
}
const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
function privateKey(account) {
return defaultWallet(account).getPrivateKey()
}
function isStrictAddress(cryptoCode, toAddress) {
return checkCryptoCode(cryptoCode).then(() =>
util.isValidChecksumAddress(toAddress),
)
}
function getTxHashesByAddress() {
throw new Error(
`Transactions hash retrieval is not implemented for this coin!`,
)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const isErc20Token = coins.utils.isErc20Token(cryptoCode)
return SEND_QUEUE.add(() =>
delay(QUEUE_EXECUTION_DELAY)
.then(() => {
return (isErc20Token ? generateErc20Tx : generateTx)(
toAddress,
defaultWallet(account),
cryptoAtoms,
false,
cryptoCode,
)
})
.then(pify(web3.eth.sendSignedTransaction))
.then(txid => {
return pify(web3.eth.getTransaction)(txid).then(tx => {
if (!tx) {
logger.warn(`sendCoins: Transaction ${txid} not found on chain`)
return { txid }
}
const fee = new BN(tx.gas).times(new BN(tx.gasPrice)).decimalPlaces(0)
return { txid, fee }
})
})
.catch(error => {
logger.error(`sendCoins: Error occurred - ${error.message}`, error)
throw error
}),
)
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode === 'ETH' || coins.utils.isErc20Token(cryptoCode)) {
return Promise.resolve(cryptoCode)
}
return Promise.reject(new Error('cryptoCode must be ETH'))
}
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code =>
confirmedBalance(defaultAddress(account), code),
)
}
const pendingBalance = (address, cryptoCode) => {
const promises = [
_balance(true, address, cryptoCode),
_balance(false, address, cryptoCode),
]
return Promise.all(promises).then(([pending, confirmed]) =>
BN(pending).minus(confirmed),
)
}
const confirmedBalance = (address, cryptoCode) =>
_balance(false, address, cryptoCode)
function _balance(includePending, address, cryptoCode) {
if (coins.utils.isErc20Token(cryptoCode)) {
const contract = new web3.eth.Contract(
ABI.ERC20,
coins.utils.getErc20Token(cryptoCode).contractAddress,
)
return contract.methods
.balanceOf(address.toLowerCase())
.call((_, balance) => {
return contract.methods
.decimals()
.call((_, decimals) => BN(balance).div(10 ** decimals))
})
}
const block = includePending ? 'pending' : undefined
return (
pify(web3.eth.getBalance)(address.toLowerCase(), block)
/* NOTE: Convert bn.js bignum to bignumber.js bignum */
.then(balance => (balance ? BN(balance) : BN(0)))
)
}
function generateErc20Tx(_toAddress, wallet, amount, includesFee, cryptoCode) {
const fromAddress = '0x' + wallet.getAddress().toString('hex')
const toAddress = coins.utils.getErc20Token(cryptoCode).contractAddress
const contract = new web3.eth.Contract(ABI.ERC20, toAddress)
const contractData = contract.methods.transfer(
_toAddress.toLowerCase(),
hex(amount),
)
const txTemplate = {
from: fromAddress,
to: toAddress,
value: hex(BN(0)),
data: contractData.encodeABI(),
}
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const promises = [
pify(contractData.estimateGas)(txTemplate),
pify(web3.eth.getTransactionCount)(fromAddress),
pify(web3.eth.getBlock)('pending'),
]
return Promise.all(promises)
.then(([gas, txCount, { baseFeePerGas }]) => [
BN(gas),
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
BN(baseFeePerGas),
])
.then(([gas, txCount, baseFeePerGas]) => {
lastUsedNonces[fromAddress] = txCount
const maxPriorityFeePerGas = new BN(web3.utils.toWei('1.0', 'gwei')) // web3 default value
const maxFeePerGas = new BN(2)
.times(baseFeePerGas)
.plus(maxPriorityFeePerGas)
logger.info(
`generateErc20Tx: Building transaction - nonce: ${txCount}, maxFeePerGas: ${maxFeePerGas.toString()}, gasLimit: ${gas.toString()}`,
)
const rawTx = {
chainId: 1,
nonce: txCount,
maxPriorityFeePerGas: web3.utils.toHex(maxPriorityFeePerGas),
maxFeePerGas: web3.utils.toHex(maxFeePerGas),
gasLimit: hex(gas),
to: toAddress,
from: fromAddress,
value: hex(BN(0)),
data: contractData.encodeABI(),
}
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
const privateKey = wallet.getPrivateKey()
const signedTx = tx.sign(privateKey)
return '0x' + signedTx.serialize().toString('hex')
})
.catch(error => {
logger.error(
`generateErc20Tx: Error during transaction generation - ${error.message}`,
error,
)
throw error
})
}
function generateTx(_toAddress, wallet, amount, includesFee) {
const fromAddress = '0x' + wallet.getAddress().toString('hex')
const toAddress = _toAddress.toLowerCase()
const txTemplate = {
from: fromAddress,
to: toAddress,
value: amount.toString(),
}
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const promises = [
pify(web3.eth.estimateGas)(txTemplate),
pify(web3.eth.getGasPrice)(),
pify(web3.eth.getTransactionCount)(fromAddress),
pify(web3.eth.getBlock)('pending'),
]
return Promise.all(promises)
.then(([gas, gasPrice, txCount, { baseFeePerGas }]) => [
BN(gas),
BN(gasPrice),
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
BN(baseFeePerGas),
])
.then(([gas, , txCount, baseFeePerGas]) => {
lastUsedNonces[fromAddress] = txCount
const maxPriorityFeePerGas = new BN(web3.utils.toWei('1.0', 'gwei')) // web3 default value
const maxFeePerGas = baseFeePerGas.times(2).plus(maxPriorityFeePerGas)
const toSend = includesFee
? new BN(amount).minus(maxFeePerGas.times(gas))
: amount
const rawTx = {
chainId: 1,
nonce: txCount,
maxPriorityFeePerGas: web3.utils.toHex(maxPriorityFeePerGas),
maxFeePerGas: web3.utils.toHex(maxFeePerGas),
gasLimit: hex(gas),
to: toAddress,
from: fromAddress,
value: hex(toSend),
}
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
const privateKey = wallet.getPrivateKey()
const signedTx = tx.sign(privateKey)
return '0x' + signedTx.serialize().toString('hex')
})
.catch(error => {
logger.error(
`generateTx: Error during transaction generation - ${error.message}`,
error,
)
throw error
})
}
function defaultWallet(account) {
return defaultHdNode(account).deriveChild(0).getWallet()
}
function defaultAddress(account) {
return defaultWallet(account).getChecksumAddressString()
}
function sweep(account, txId, cryptoCode, hdIndex) {
const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet()
const fromAddress = wallet.getChecksumAddressString()
return SWEEP_QUEUE.add(() =>
delay(QUEUE_EXECUTION_DELAY)
.then(() => confirmedBalance(fromAddress, cryptoCode))
.then(r => {
if (r.eq(0)) return
return generateTx(
defaultAddress(account),
wallet,
r,
true,
cryptoCode,
txId,
).then(signedTx => pify(web3.eth.sendSignedTransaction)(signedTx))
}),
)
}
function newAddress(account, info) {
const childNode = paymentHdNode(account).deriveChild(info.hdIndex)
return Promise.resolve(childNode.getWallet().getChecksumAddressString())
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(code => Promise.all([confirmedBalance(toAddress, code), code]))
.then(([confirmed, code]) => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, code).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'published' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function paymentHdNode(account) {
const masterSeed = account.seed
if (!masterSeed) throw new Error('No master seed!')
const key = hdkey.fromMasterSeed(masterSeed)
return key.derivePath(paymentPrefixPath)
}
function defaultHdNode(account) {
const masterSeed = account.seed
if (!masterSeed) throw new Error('No master seed!')
const key = hdkey.fromMasterSeed(masterSeed)
return key.derivePath(defaultPrefixPath)
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code => {
const fundingAddress = defaultAddress(account)
const promises = [
pendingBalance(fundingAddress, code),
confirmedBalance(fundingAddress, code),
]
return Promise.all(promises).then(
([fundingPendingBalance, fundingConfirmedBalance]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
})
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() =>
connect(
`http://localhost:${coins.utils.getCryptoCurrency(cryptoCode).defaultPort}`,
),
)
.then(() => web3.eth.syncing)
.then(res => (res === false ? 'ready' : 'syncing'))
}

View file

@ -0,0 +1,15 @@
const _ = require('lodash/fp')
const base = require('./base')
const { utils: coinUtils } = require('@lamassu/coins')
const cryptoRec = coinUtils.getCryptoCurrency('ETH')
const defaultPort = cryptoRec.defaultPort
const NAME = 'geth'
function run() {
base.connect(`http://localhost:${defaultPort}`)
}
module.exports = _.merge(base, { NAME, run })

View file

@ -0,0 +1,79 @@
const _ = require('lodash/fp')
const NodeCache = require('node-cache')
const base = require('../geth/base')
const T = require('../../../time')
const { BALANCE_FETCH_SPEED_MULTIPLIER } = require('../../../constants')
const NAME = 'infura'
function run(account) {
if (!account.endpoint)
throw new Error('Need to configure API endpoint for Infura')
const endpoint = _.startsWith('https://')(account.endpoint)
? account.endpoint
: `https://${account.endpoint}`
base.connect(endpoint)
}
const txsCache = new NodeCache({
stdTTL: T.hour / 1000,
checkperiod: T.minute / 1000,
deleteOnExpire: true,
})
function shouldGetStatus(tx) {
const timePassedSinceTx = Date.now() - new Date(tx.created)
const timePassedSinceReq =
Date.now() - new Date(txsCache.get(tx.id).lastReqTime)
if (timePassedSinceTx < 3 * T.minutes)
return (
_.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 10 * T.seconds
)
if (timePassedSinceTx < 5 * T.minutes)
return (
_.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 20 * T.seconds
)
if (timePassedSinceTx < 30 * T.minutes)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.minute
if (timePassedSinceTx < 1 * T.hour)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 2 * T.minute
if (timePassedSinceTx < 3 * T.hours)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 5 * T.minute
if (timePassedSinceTx < 1 * T.day)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.hour
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.hour
}
// Override geth's getStatus function to allow for different polling timing
function getStatus(account, tx, requested, settings, operatorId) {
if (_.isNil(txsCache.get(tx.id))) {
txsCache.set(tx.id, { lastReqTime: Date.now() })
}
// return last available response
if (!shouldGetStatus(tx)) {
return Promise.resolve(txsCache.get(tx.id).res)
}
return base
.getStatus(account, tx, requested, settings, operatorId)
.then(res => {
if (res.status === 'confirmed') {
txsCache.del(tx.id) // Transaction reached final status, can trim it from the caching obj
} else {
txsCache.set(tx.id, { lastReqTime: Date.now(), res })
txsCache.ttl(tx.id, T.hour / 1000)
}
return res
})
}
module.exports = _.merge(base, {
NAME,
run,
getStatus,
fetchSpeed: BALANCE_FETCH_SPEED_MULTIPLIER.SLOW,
})

View file

@ -0,0 +1,147 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const { utils: coinUtils } = require('@lamassu/coins')
const BN = require('../../../bn')
const E = require('../../../error')
const cryptoRec = coinUtils.getCryptoCurrency('LTC')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'LTC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically.
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress() {
throw new Error(`Transactions hash retrieval not implemented for this coin!`)
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,175 @@
const _ = require('lodash/fp')
const BN = require('../../../bn')
const E = require('../../../error')
const { utils: coinUtils } = require('@lamassu/coins')
const NAME = 'FakeWallet'
const SECONDS = 1000
const PUBLISH_TIME = 3 * SECONDS
const AUTHORIZE_TIME = PUBLISH_TIME + 5 * SECONDS
const CONFIRM_TIME = AUTHORIZE_TIME + 10 * SECONDS
const SUPPORTED_COINS = coinUtils.cryptoCurrencies()
const SUPPORTS_BATCHING = true
let t0
const checkCryptoCode = cryptoCode =>
!_.includes(cryptoCode, SUPPORTED_COINS)
? Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
: Promise.resolve()
function _balance(cryptoCode) {
const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
const unitScale = cryptoRec.unitScale
return new BN(10).shiftedBy(unitScale).decimalPlaces(0)
}
function balance(account, cryptoCode) {
return Promise.resolve().then(() => _balance(cryptoCode))
}
function pendingBalance(account, cryptoCode) {
return balance(account, cryptoCode).then(b => b.times(1.1))
}
function confirmedBalance(account, cryptoCode) {
return balance(account, cryptoCode)
}
// Note: This makes it easier to test insufficient funds errors
let sendCount = 100
function isInsufficient(cryptoAtoms, cryptoCode) {
const b = _balance(cryptoCode)
return cryptoAtoms.gt(b.div(1000).times(sendCount))
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
sendCount++
return new Promise((resolve, reject) => {
setTimeout(() => {
if (isInsufficient(cryptoAtoms, cryptoCode)) {
console.log(
'[%s] DEBUG: Mock wallet insufficient funds: %s',
cryptoCode,
cryptoAtoms.toString(),
)
return reject(new E.InsufficientFundsError())
}
console.log(
'[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s',
cryptoCode,
cryptoAtoms.toString(),
toAddress,
)
return resolve({ txid: '<txHash>', fee: new BN(0) })
}, 2000)
})
}
function sendCoinsBatch(account, txs, cryptoCode) {
sendCount = sendCount + txs.length
return new Promise((resolve, reject) => {
setTimeout(() => {
const cryptoSum = _.reduce(
(acc, value) => acc.plus(value.crypto_atoms),
BN(0),
txs,
)
if (isInsufficient(cryptoSum, cryptoCode)) {
console.log(
'[%s] DEBUG: Mock wallet insufficient funds: %s',
cryptoCode,
cryptoSum.toString(),
)
return reject(new E.InsufficientFundsError())
}
console.log(
'[%s] DEBUG: Mock wallet sending %s cryptoAtoms in a batch',
cryptoCode,
cryptoSum.toString(),
)
return resolve({ txid: '<txHash>', fee: BN(0) })
}, 2000)
})
}
function newAddress() {
t0 = Date.now()
return Promise.resolve("<Fake address, don't send>")
}
function newFunding(account, cryptoCode) {
const promises = [
pendingBalance(account, cryptoCode),
confirmedBalance(account, cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises).then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
const elapsed = Date.now() - t0
if (elapsed < PUBLISH_TIME)
return Promise.resolve({
receivedCryptoAtoms: new BN(0),
status: 'notSeen',
})
if (elapsed < AUTHORIZE_TIME)
return Promise.resolve({
receivedCryptoAtoms: requested,
status: 'published',
})
if (elapsed < CONFIRM_TIME)
return Promise.resolve({
receivedCryptoAtoms: requested,
status: 'authorized',
})
console.log(
'[%s] DEBUG: Mock wallet has confirmed transaction [%s]',
cryptoCode,
toAddress.slice(0, 5),
)
return Promise.resolve({ status: 'confirmed' })
}
function getTxHashesByAddress() {
return new Promise(resolve => {
setTimeout(() => {
return resolve([]) // TODO: should return something other than empty list?
}, 100)
})
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
NAME,
SUPPORTS_BATCHING,
balance,
sendCoinsBatch,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,303 @@
const fs = require('fs')
const path = require('path')
const _ = require('lodash/fp')
const { COINS, utils } = require('@lamassu/coins')
const { default: PQueue } = require('p-queue')
const BN = require('../../../bn')
const E = require('../../../error')
const logger = require('../../../logger')
const jsonRpc = require('../../common/json-rpc')
const BLOCKCHAIN_DIR = process.env.BLOCKCHAIN_DIR
const cryptoRec = utils.getCryptoCurrency(COINS.XMR)
const configPath = utils.configPath(cryptoRec, BLOCKCHAIN_DIR)
const walletDir = path.resolve(
utils.cryptoDir(cryptoRec, BLOCKCHAIN_DIR),
'wallets',
)
const DIGEST_QUEUE = new PQueue({
concurrency: 1,
interval: 150,
})
function createDigestRequest(account = {}, method, params = []) {
return DIGEST_QUEUE.add(() =>
jsonRpc.fetchDigest(account, method, params).then(res => {
const r = JSON.parse(res)
if (r.error) throw r.error
return r.result
}),
)
}
function rpcConfig() {
try {
const config = jsonRpc.parseConf(configPath)
return {
username: config['rpc-login'].split(':')[0],
password: config['rpc-login'].split(':')[1],
port: cryptoRec.walletPort || cryptoRec.defaultPort,
}
} catch (err) {
logger.error(`Wallet is currently not installed! ${err}`)
return {
username: '',
password: '',
port: cryptoRec.walletPort || cryptoRec.defaultPort,
}
}
}
function fetch(method, params) {
return createDigestRequest(rpcConfig(), method, params)
}
function handleError(error, method) {
switch (error.code) {
case -13: {
if (
fs.existsSync(path.resolve(walletDir, 'Wallet')) &&
fs.existsSync(path.resolve(walletDir, 'Wallet.keys'))
) {
logger.debug('Found wallet! Opening wallet...')
return openWallet()
}
logger.debug("Couldn't find wallet! Creating...")
return createWallet()
}
case -21:
throw new Error('Wallet already exists!')
case -22:
try {
return openWalletWithPassword()
} catch {
throw new Error('Invalid wallet password!')
}
case -17:
throw new E.InsufficientFundsError()
case -37:
throw new E.InsufficientFundsError()
default:
throw new Error(
_.join(' ', [
`json-rpc::${method} error:`,
JSON.stringify(_.get('message', error, '')),
JSON.stringify(_.get('response.data.error', error, '')),
]),
)
}
}
function openWallet() {
return fetch('open_wallet', { filename: 'Wallet' }).catch(() =>
openWalletWithPassword(),
)
}
function openWalletWithPassword() {
return fetch('open_wallet', {
filename: 'Wallet',
password: rpcConfig().password,
})
}
function createWallet() {
return fetch('create_wallet', { filename: 'Wallet', language: 'English' })
.then(() => new Promise(() => setTimeout(() => openWallet(), 3000)))
.then(() => fetch('auto_refresh'))
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'XMR')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function refreshWallet() {
return fetch('refresh').catch(err => handleError(err, 'refreshWallet'))
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() =>
fetch('get_balance', { account_index: 0, address_indices: [0] }),
)
.then(res => {
return BN(res.unlocked_balance).decimalPlaces(0)
})
.catch(err => handleError(err, 'accountBalance'))
}
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() =>
fetch('transfer_split', {
destinations: [{ amount: cryptoAtoms, address: toAddress }],
account_index: 0,
subaddr_indices: [],
priority: 0,
mixin: 6,
ring_size: 7,
unlock_time: 0,
get_tx_hex: false,
new_algorithm: false,
get_tx_metadata: false,
}),
)
.then(res => ({
fee: BN(res.fee_list[0]).abs(),
txid: res.tx_hash_list[0],
}))
.catch(err => handleError(err, 'sendCoins'))
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('create_address', { account_index: 0 }))
.then(res => res.address)
.catch(err => handleError(err, 'newAddress'))
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() => fetch('get_address_index', { address: toAddress }))
.then(addressRes =>
fetch('get_transfers', {
in: true,
pool: true,
account_index: addressRes.index.major,
subaddr_indices: [addressRes.index.minor],
}),
)
.then(transferRes => {
const confirmedToAddress = _.filter(
it => it.address === toAddress,
transferRes.in ?? [],
)
const pendingToAddress = _.filter(
it => it.address === toAddress,
transferRes.pool ?? [],
)
const confirmed = _.reduce(
(acc, value) => acc.plus(value.amount),
BN(0),
confirmedToAddress,
)
const pending = _.reduce(
(acc, value) => acc.plus(value.amount),
BN(0),
pendingToAddress,
)
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
.catch(err => handleError(err, 'getStatus'))
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() =>
Promise.all([
fetch('get_balance', { account_index: 0, address_indices: [0] }),
fetch('create_address', { account_index: 0 }),
fetch('get_transfers', { pool: true, account_index: 0 }),
]),
)
.then(([balanceRes, addressRes, transferRes]) => {
const memPoolBalance = _.reduce(
(acc, value) => acc.plus(value.amount),
BN(0),
transferRes.pool,
)
return {
fundingPendingBalance: BN(balanceRes.balance)
.minus(balanceRes.unlocked_balance)
.plus(memPoolBalance),
fundingConfirmedBalance: BN(balanceRes.unlocked_balance),
fundingAddress: addressRes.address,
}
})
.catch(err => handleError(err, 'newFunding'))
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => {
switch (parseInt(rpcConfig().port, 10)) {
case 18082:
return 'main'
case 28082:
return 'test'
case 38083:
return 'stage'
default:
return ''
}
})
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => {
try {
const config = jsonRpc.parseConf(configPath)
// Daemon uses a different connection of the wallet
const rpcConfig = {
username: config['rpc-login'].split(':')[0],
password: config['rpc-login'].split(':')[1],
port: cryptoRec.defaultPort,
}
return jsonRpc
.fetchDigest(rpcConfig, 'get_info')
.then(res => (res.synchronized ? 'ready' : 'syncing'))
} catch (err) {
throw new Error(`XMR daemon is currently not installed. ${err}`)
}
})
}
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() => fetch('get_address_index', { address: address }))
.then(addressRes =>
fetch('get_transfers', {
in: true,
pool: true,
pending: true,
account_index: addressRes.index.major,
subaddr_indices: [addressRes.index.minor],
}),
)
.then(_.map(({ txid }) => txid))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,221 @@
const TronWeb = require('tronweb')
const coins = require('@lamassu/coins')
const { default: PQueue } = require('p-queue')
const BN = require('../../../bn')
let tronWeb = null
const DEFAULT_PREFIX_PATH = "m/44'/195'/0'/0"
const PAYMENT_PREFIX_PATH = "m/44'/195'/1'/0"
const SWEEP_QUEUE = new PQueue({
concurrency: 3,
interval: 250,
})
function checkCryptoCode(cryptoCode) {
if (cryptoCode === 'TRX' || coins.utils.isTrc20Token(cryptoCode)) {
return Promise.resolve(cryptoCode)
}
return Promise.reject(new Error('cryptoCode must be TRX'))
}
function defaultWallet(account) {
const mnemonic = account.mnemonic
if (!mnemonic) throw new Error('No mnemonic seed!')
return TronWeb.fromMnemonic(
mnemonic.replace(/[\r\n]/gm, ' ').trim(),
`${DEFAULT_PREFIX_PATH}/0`,
)
}
function paymentWallet(account, index) {
const mnemonic = account.mnemonic
if (!mnemonic) throw new Error('No mnemonic seed!')
return TronWeb.fromMnemonic(
mnemonic.replace(/[\r\n]/gm, ' ').trim(),
`${PAYMENT_PREFIX_PATH}/${index}`,
)
}
function newAddress(account, info) {
const wallet = paymentWallet(account, info.hdIndex)
return Promise.resolve(wallet.address)
}
function defaultAddress(account) {
return defaultWallet(account).address
}
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code =>
confirmedBalance(defaultAddress(account), code),
)
}
const confirmedBalance = (address, cryptoCode) => _balance(address, cryptoCode)
const _balance = async (address, cryptoCode) => {
if (coins.utils.isTrc20Token(cryptoCode)) {
const contractAddress =
coins.utils.getTrc20Token(cryptoCode).contractAddress
const { abi } = await tronWeb.trx.getContract(contractAddress)
const contract = tronWeb.contract(abi.entrys, contractAddress)
const balance = await contract.methods.balanceOf(address).call()
return BN(balance.toString())
}
const balance = await tronWeb.trx.getBalance(address)
return balance ? BN(balance) : BN(0)
}
const sendCoins = async (account, tx) => {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
const txFunction = isTrc20Token ? generateTrc20Tx : generateTx
const rawTx = await txFunction(
toAddress,
defaultWallet(account),
cryptoAtoms.toString(),
cryptoCode,
)
let response = null
try {
response = await tronWeb.trx.sendRawTransaction(rawTx)
if (!response.result) throw new Error(response.code)
} catch (err) {
// for some reason err here is just a string
throw new Error(err)
}
const transaction = response.transaction
const txid = transaction.txID
const transactionInfo = tronWeb.trx.getTransactionInfo(txid)
if (!transactionInfo || !transactionInfo.fee) return { txid }
const fee = new BN(transactionInfo.fee).decimalPlaces(0)
return { txid, fee }
}
const generateTrc20Tx = async (toAddress, wallet, amount, cryptoCode) => {
const contractAddress = coins.utils.getTrc20Token(cryptoCode).contractAddress
const functionSelector = 'transfer(address,uint256)'
const parameters = [
{ type: 'address', value: tronWeb.address.toHex(toAddress) },
{ type: 'uint256', value: amount },
]
const tx = await tronWeb.transactionBuilder.triggerSmartContract(
contractAddress,
functionSelector,
{},
parameters,
wallet.address,
)
return tronWeb.trx.sign(tx.transaction, wallet.privateKey.slice(2))
}
const generateTx = async (toAddress, wallet, amount) => {
const transaction = await tronWeb.transactionBuilder.sendTrx(
toAddress,
amount,
wallet.address,
)
const privateKey = wallet.privateKey
// their api return a hex string starting with 0x but expects without it
return tronWeb.trx.sign(transaction, privateKey.slice(2))
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code => {
const fundingAddress = defaultAddress(account)
return confirmedBalance(fundingAddress, code).then(balance => ({
fundingPendingBalance: BN(0),
fundingConfirmedBalance: balance,
fundingAddress,
}))
})
}
function sweep(account, txId, cryptoCode, hdIndex) {
const wallet = paymentWallet(account, hdIndex)
const fromAddress = wallet.address
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
const txFunction = isTrc20Token ? generateTrc20Tx : generateTx
return SWEEP_QUEUE.add(async () => {
const r = await confirmedBalance(fromAddress, cryptoCode)
if (r.eq(0)) return
const signedTx = await txFunction(
defaultAddress(account),
wallet,
r.toString(),
cryptoCode,
)
let response = null
try {
response = await tronWeb.trx.sendRawTransaction(signedTx)
if (!response.result) throw new Error(response.code)
} catch (err) {
// for some reason err here is just a string
throw new Error(err)
}
return response
})
}
function connect(account) {
if (tronWeb != null) return
const endpoint = account.endpoint
const apiKey = account.apiKey
tronWeb = new TronWeb({
fullHost: endpoint,
headers: { 'TRON-PRO-API-KEY': apiKey },
privateKey: '01',
})
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(code => confirmedBalance(toAddress, code))
.then(confirmed => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gt(0))
return { receivedCryptoAtoms: confirmed, status: 'insufficientFunds' }
return { receivedCryptoAtoms: 0, status: 'notSeen' }
})
}
function getTxHashesByAddress() {
throw new Error(
`Transactions hash retrieval is not implemented for this coin!`,
)
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
sweep,
defaultAddress,
supportsHd: true,
newFunding,
connect,
getTxHashesByAddress,
}

View file

@ -0,0 +1,12 @@
const _ = require('lodash/fp')
const base = require('../tron/base')
const NAME = 'trongrid'
function run(account) {
const endpoint = 'https://api.trongrid.io'
base.connect({ ...account, endpoint })
}
module.exports = _.merge(base, { NAME, run })

View file

@ -0,0 +1,185 @@
const _ = require('lodash/fp')
const pRetry = require('p-retry')
const jsonRpc = require('../../common/json-rpc')
const { utils: coinUtils } = require('@lamassu/coins')
const BN = require('../../../bn')
const E = require('../../../error')
const cryptoRec = coinUtils.getCryptoCurrency('ZEC')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'ZEC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically.
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
const checkSendStatus = function (opid) {
return new Promise((resolve, reject) => {
fetch('z_getoperationstatus', [[opid]]).then(res => {
const status = _.get('status', res[0])
switch (status) {
case 'success':
resolve(res[0])
break
case 'failed':
throw new pRetry.AbortError(res[0].error)
case 'executing':
reject(new Error('operation still executing'))
break
}
})
})
}
const checker = opid =>
pRetry(() => checkSendStatus(opid), {
retries: 20,
minTimeout: 300,
factor: 1.05,
})
return checkCryptoCode(cryptoCode)
.then(() =>
fetch('z_sendmany', [
'ANY_TADDR',
[{ address: toAddress, amount: coins }],
null,
null,
'NoPrivacy',
]),
)
.then(checker)
.then(res => {
return {
fee: _.get('params.fee', res),
txid: _.get('result.txid', res),
}
})
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initial_block_download_complete'] ? 'ready' : 'syncing'))
}
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode).then(() => fetch('getaddresstxids', [address]))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,68 @@
const qs = require('querystring')
const axios = require('axios')
const _ = require('lodash/fp')
const { fetchRBF } = require('../../wallet/bitcoind/bitcoind')
module.exports = { authorize }
function highConfidence(confidence, txref, txRBF) {
if (txref.double_spend) return 0
if (txRBF) return 0
if (txref.confirmations > 0 || txref.confidence * 100 >= confidence)
return txref.value
return 0
}
function authorize(
account,
toAddress,
cryptoAtoms,
cryptoCode,
isBitcoindAvailable,
) {
return Promise.resolve().then(() => {
if (cryptoCode !== 'BTC')
throw new Error('Unsupported crypto: ' + cryptoCode)
const query = qs.stringify({
token: account.token,
includeConfidence: true,
})
const confidence = account.confidenceFactor
const isRBFEnabled = account.rbf
const url = `https://api.blockcypher.com/v1/btc/main/addrs/${toAddress}?${query}`
return axios.get(url).then(r => {
const data = r.data
if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) {
const promises = _.map(
unconfirmedTxref => fetchRBF(unconfirmedTxref.tx_hash),
data.unconfirmed_txrefs,
)
return Promise.all(promises).then(values => {
const unconfirmedTxsRBF = _.fromPairs(values)
const sumTxRefs = txrefs =>
_.sumBy(
txref =>
highConfidence(
confidence,
txref,
unconfirmedTxsRBF[txref.tx_hash],
),
txrefs,
)
const authorizedValue =
sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
})
}
const sumTxRefs = txrefs =>
_.sumBy(txref => highConfidence(confidence, txref), txrefs)
const authorizedValue =
sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
})
})
}

View file

@ -0,0 +1,11 @@
module.exports = { authorize }
function authorize(account, toAddress, cryptoAtoms, cryptoCode) {
return Promise.resolve().then(() => {
if (cryptoCode !== 'BTC')
throw new Error('Unsupported crypto: ' + cryptoCode)
const isAuthorized = false
return isAuthorized
})
}