From dd55cda986dd68a290a687e635d7dfff0d58ce2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Tue, 5 Apr 2022 17:54:11 +0100 Subject: [PATCH] feat: retrieve account info --- lib/plugins/wallet/galoy/galoy.js | 175 ++++++++++++++++++++++-------- 1 file changed, 132 insertions(+), 43 deletions(-) diff --git a/lib/plugins/wallet/galoy/galoy.js b/lib/plugins/wallet/galoy/galoy.js index 0c771602..a9540c37 100644 --- a/lib/plugins/wallet/galoy/galoy.js +++ b/lib/plugins/wallet/galoy/galoy.js @@ -3,8 +3,9 @@ const _ = require('lodash/fp') const axios = require('axios') const NAME = 'LN' -const SUPPORTED_COINS = ['LN'] +const SUPPORTED_COINS = ['LN', 'BTC'] const PHONE = '+3500000000000' +const TEST_AUTH_TOKEN = '' const URI = 'https://api.staging.galoy.io/graphql' @@ -13,7 +14,7 @@ const BN = require('../../../bn') function request (graphqlQuery, token) { const headers = { 'content-type': 'application/json', - 'Authorization': token || '' + 'Authorization': `Bearer ${token || TEST_AUTH_TOKEN}` } return Promise.resolve(true) .then(() => { @@ -25,7 +26,6 @@ function request (graphqlQuery, token) { }) }) .then(r => { - console.log(r) if (r.error) throw r.error return r.data }) @@ -49,7 +49,48 @@ function getGaloyAccount (token) { me { createdAt defaultAccount { - ...AccountFragment + defaultWalletId + wallets { + id + walletCurrency + balance + transactions { + edges { + node { + createdAt + direction + id + settlementAmount + settlementFee + status + initiationVia { + ... on InitiationViaIntraLedger { + counterPartyUsername + counterPartyWalletId + } + ... on InitiationViaLn { + paymentHash + } + ... on InitiationViaOnChain { + address + } + } + settlementVia { + ... on SettlementViaIntraLedger { + counterPartyUsername + counterPartyWalletId + } + ... on SettlementViaLn { + preImage + } + ... on SettlementViaOnChain { + transactionHash + } + } + } + } + } + } } id phone @@ -61,7 +102,7 @@ function getGaloyAccount (token) { } return request(accountInfo, token) .then(r => { - return r.data.data.defaultAccount + return r.data.me.defaultAccount }) .catch(err => { throw new Error(err) @@ -76,7 +117,8 @@ function fetchAuthToken (config) { 'query': `mutation userRequestAuthCode($input: UserRequestAuthCodeInput!) { userRequestAuthCode(input: $input) { errors { - ...ErrorFragment + message + path } success } @@ -88,32 +130,41 @@ function fetchAuthToken (config) { 'query': `mutation captchaCreateChallenge { captchaCreateChallenge { errors { - ...ErrorFragment + message + path } result { - ...CaptchaCreateChallengeResultFragment + challengeCode + id } } }`, 'variables': {} } - const captchaRequestAuthCode = { + const captchaRequestAuthCode = code => ({ 'operationName': 'captchaRequestAuthCode', 'query': `mutation captchaRequestAuthCode($input: CaptchaRequestAuthCodeInput!) { captchaRequestAuthCode(input: $input) { errors { - ...ErrorFragment + message + path } success } }`, - 'variables': {} - } + 'variables': { 'input': { 'phone': `${phone}`, 'challengeCode': `${code.challengeCode}`, 'secCode': `${code.challengeCode.slice(0, 5)}`, 'validationCode': `${code.challengeCode.slice(0, 5)}` } } + }) - return request(regularRequestAuthCode) + return request(createCaptcha) .then(r => { - console.log(r) - return r + const code = r.data.captchaCreateChallenge.result + if (code) { + // captchaRequestAuthCode parameters have to be processed by geetest + return request(captchaRequestAuthCode(code)) + } + }) + .catch(err => { + throw new Error(err) }) } @@ -121,43 +172,66 @@ function isLightning (address) { return address.substr(0, 2) === 'ln' } -function sendCoins (account, tx, settings, operatorId) { - const { toAddress, cryptoAtoms, cryptoCode } = tx - // Is walletId a mandatory field? +function sendFundsOnChain (walletId, address, cryptoAtoms, token) { const sendOnChain = { 'operationName': 'onChainPaymentSend', 'query': `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) { onChainPaymentSend(input: $input) { errors { - ...ErrorFragment + message + path } status } }`, - 'variables': { 'input': { 'address': `${toAddress}`, 'amount': `${cryptoAtoms}` } } + 'variables': { 'input': { 'address': `${address}`, 'amount': `${cryptoAtoms}`, 'walletId': `${walletId}` } } } + return request(sendOnChain, token) + .then(result => { + return result.data.onChainPaymentSend + }) + .catch(err => { + throw err + }) +} + +function sendFundsLN (walletId, invoice, token) { const sendLN = { 'operationName': 'lnInvoicePaymentSend', 'query': `mutation lnInvoicePaymentSend($input: LnInvoicePaymentInput!) { lnInvoicePaymentSend(input: $input) { errors { - ...ErrorFragment + message + path } status } }`, - 'variables': { 'input': { 'paymentRequest': `${toAddress}` } } + 'variables': { 'input': { 'paymentRequest': `${invoice}`, 'walletId': `${walletId}` } } } + return request(sendLN, token) + .then(result => { + return result.data.onChainPaymentSend + }) + .catch(err => { + throw err + }) +} + +function sendCoins (account, tx, settings, operatorId) { + const { toAddress, cryptoAtoms, cryptoCode } = tx return checkCryptoCode(cryptoCode) - .then(() => fetchAuthToken({ phone: PHONE })) - .then(authToken => { - if (isLightning) { - return request(sendLN, authToken) + // .then(() => fetchAuthToken({ phone: PHONE })) + .then(authToken => Promise.all([getGaloyAccount(authToken), authToken])) + .then(([account, authToken]) => { + const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets)) + if (isLightning(toAddress)) { + return sendFundsLN(wallet.id, toAddress, authToken) } - return request(sendOnChain, authToken) + return sendFundsOnChain(wallet.id, toAddress, cryptoAtoms, authToken) }) .then(result => { - return result.data + return result }) .catch(err => { throw err @@ -171,7 +245,8 @@ function newOnChainAddress (walletId, token) { onChainAddressCreate(input: $input) { address errors { - ...ErrorFragment + message + path } } }`, @@ -192,10 +267,11 @@ function newInvoice (walletId, cryptoAtoms, token) { 'query': `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) { lnInvoiceCreate(input: $input) { errors { - ...ErrorFragment + message + path } invoice { - ...LnInvoiceFragment + paymentRequest } } }`, @@ -212,12 +288,14 @@ function newInvoice (walletId, cryptoAtoms, token) { function balance (account, cryptoCode, settings, operatorId) { return checkCryptoCode(cryptoCode) - .then(() => fetchAuthToken({ phone: PHONE })) + // .then(() => fetchAuthToken({ phone: PHONE })) .then(authToken => getGaloyAccount(authToken)) .then(account => { + console.log(account) // account has a list of wallets, should we consider the balance of each one? // for now we'll pick the first BTC wallet that matches the defaultWalletId - const wallet = _.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets) + const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets)) + console.log(wallet) return new BN(wallet.balance || 0) }) .catch(err => { @@ -227,11 +305,11 @@ function balance (account, cryptoCode, settings, operatorId) { function newAddress (account, info, tx, settings, operatorId) { const { cryptoAtoms, cryptoCode } = tx - return checkCryptoCode(info.cryptoCode) - .then(() => fetchAuthToken({ phone: PHONE })) - .then(authToken => [getGaloyAccount(authToken), authToken]) + return checkCryptoCode(cryptoCode) + // .then(() => fetchAuthToken({ phone: PHONE })) + .then(authToken => Promise.all([getGaloyAccount(authToken), authToken])) .then(([account, authToken]) => { - const wallet = _.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets) + const wallet = _.head(_.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets)) const promises = [ newOnChainAddress(wallet.id, authToken), newInvoice(wallet.id, cryptoAtoms, authToken) @@ -251,7 +329,7 @@ function getStatus (account, tx, requested, settings, operatorId) { function newFunding (account, cryptoCode, settings, operatorId) { // Has to be a regular BTC address return checkCryptoCode(cryptoCode) - .then(() => fetchAuthToken({ phone: PHONE })) + // .then(() => fetchAuthToken({ phone: PHONE })) .then(authToken => [getGaloyAccount(authToken), authToken]) .then(([account, authToken]) => { const wallet = _.filter(wallet => wallet.walletCurrency === cryptoCode && wallet.id === account.defaultWalletId)(account.wallets) @@ -277,11 +355,22 @@ function checkBlockchainStatus (cryptoCode) { .then(() => Promise.resolve('ready')) } -sendCoins({}, { - toAddress: 'lnbc10n1p3z7tpkpp5fjkptx4xtnh5n5vyrrhrw25f86mv0rwyu9a8tm3lrwnkl4u58zqdqg23jhxap3cqzpgxqyz5vqsp5g239kch2r7q9dty8rgs2h94h0d6tp8ssws9zte9qzvss2fr729zs9qyyssq86khhqz86dhftteqd0ymad32dfrdfdmdac8jw359wn4s0fx5gfyrectl4e49pt38gculfk3xeljv5ygd8ddry0m0z38lqt23j8aytycqrd4med', - cryptoCode: 'LN', - cryptoAtoms: 123123 -}, {}, {}) +// sendCoins({}, { +// toAddress: 'tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt', +// cryptoCode: 'BTC', +// cryptoAtoms: 1000 +// }, {}, {}) +// .then(r => console.log(r)) + +// balance({}, 'BTC', {}, {}) +// .then(r => console.log()) +// getGaloyAccount() +// newAddress({}, {}, { cryptoCode: 'BTC', cryptoAtoms: 1 }, {}) +// .then(r => console.log(r)) + +// Test faucet addresses +// tb1qfyt7cgds7z8ssthtnhh35s608059yzk0hwgcqd 1) +// tb1qmflph389nz5ev05jypzvcglyguyrl2qhyaag6r 2) module.exports = { NAME,