diff --git a/src/background/grant.ts b/src/background/grant.ts deleted file mode 100644 index 48c091ac..00000000 --- a/src/background/grant.ts +++ /dev/null @@ -1,176 +0,0 @@ -// import * as openPayment from '@interledger/open-payments' -// -// const KEY_ID = '530c7caf-47a2-4cbd-844e-b8ed53e5c0d7' -// const PRIVATE_KEY = atob( -// 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1DNENBUUF3QlFZREsyVndCQ0lFSU1xYkZodTlNZHpjNXZROXBoVDY0aGZ4Z0pRazM2TFVyR1VqL1cwbHRTWG0KLS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLQo=', -// ) -// // const SENDING_PAYMENT_POINTER_URL = 'https://ilp.rafiki.money/wmuser' // cel din extensie al userului -// // const RECEIVING_PAYMENT_POINTER_URL = 'https://ilp.rafiki.money/web-page' // cel din dom -// const WM_PAYMENT_POINTER_URL = 'https://ilp.rafiki.money/web-monetization' // intermediarul -// -// export const runPayment = async ( -// receivingPaymentPointerUrl: string, -// sendingPaymentPointerUrl: string, -// ) => { -// const client = await openPayment.createAuthenticatedClient({ -// keyId: KEY_ID, -// privateKey: PRIVATE_KEY, -// paymentPointerUrl: WM_PAYMENT_POINTER_URL, -// }) -// -// const sendingPaymentPointer = await client.paymentPointer.get({ -// url: sendingPaymentPointerUrl, -// }) // intoarce auth server -// console.log('sendingPaymentPointer', sendingPaymentPointer) -// -// const receivingPaymentPointer = await client.paymentPointer.get({ -// url: receivingPaymentPointerUrl, -// }) -// console.log('receivingPaymentPointer', receivingPaymentPointer) -// -// const incomingPaymentGrant = await client.grant.request( -// { -// url: receivingPaymentPointer.authServer, -// }, -// { -// access_token: { -// access: [{ type: 'incoming-payment', actions: ['create', 'read', 'list'] }], -// }, -// }, -// ) -// console.log('incomingPaymentGrant', incomingPaymentGrant) -// -// if (openPayment.isPendingGrant(incomingPaymentGrant)) { -// throw new Error('Expected non-interactive grant') -// } -// -// const incomingPayment = await client.incomingPayment.create( -// { -// accessToken: incomingPaymentGrant.access_token.value, -// paymentPointer: receivingPaymentPointer.id, -// }, -// { -// metadata: { -// description: 'Incoming payment for WM', -// }, -// expiresAt: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes -// }, -// ) -// console.log('incomingPayment', incomingPayment) -// -// const quoteGrant = await client.grant.request( -// { -// url: sendingPaymentPointer.authServer, -// }, -// { -// access_token: { -// access: [{ type: 'quote', actions: ['create'] }], -// }, -// }, -// ) -// console.log('quoteGrant', quoteGrant) -// -// if (openPayment.isPendingGrant(quoteGrant)) { -// throw new Error('Expected non-interactive grant') -// } -// -// const outgoingPaymentGrant = await client.grant.request( -// { -// url: sendingPaymentPointer.authServer, -// }, -// { -// access_token: { -// access: [ -// { -// type: 'outgoing-payment', -// actions: ['create', 'read', 'list'], -// identifier: sendingPaymentPointerUrl, -// limits: { -// debitAmount: { -// value: '2000', -// assetScale: sendingPaymentPointer.assetScale, -// assetCode: sendingPaymentPointer.assetCode, -// }, -// }, -// }, -// ], -// }, -// interact: { -// start: ['redirect'], -// finish: { -// method: 'redirect', -// uri: `https://localhost:3000/`, -// nonce: new Date().getTime().toString(), -// }, -// }, -// }, -// ) -// console.log('outgoingPaymentGrant', outgoingPaymentGrant) -// -// if (!openPayment.isPendingGrant(outgoingPaymentGrant)) { -// console.error('Expected interactive outgoing payment grant.') -// return -// } -// -// console.log('Interaction URL', outgoingPaymentGrant.interact.redirect) -// // -// // const ref = await inquirer -// // .prompt({ -// // type: 'input', -// // name: 'finish_url', -// // message: 'finish_url', -// // }) -// // .then((ans: any) => { -// // const url = new URL(ans.finish_url) -// // const interactRef = url.searchParams.get('interact_ref') -// // if (!interactRef) throw new Error('missing interact_ref') -// // return interactRef -// // }) -// // -// // const continuationUri = outgoingPaymentGrant.continue.uri.replace('auth/', '') -// // const continuation = await client.grant.continue( -// // { -// // accessToken: outgoingPaymentGrant.continue.access_token.value, -// // url: continuationUri, -// // }, -// // { -// // interact_ref: ref, -// // }, -// // ) -// // -// // // setInterval(async () => { -// // const quote = await client.quote.create( -// // { -// // accessToken: quoteGrant.access_token.value, -// // paymentPointer: sendingPaymentPointer.id, -// // }, -// // { -// // receiver: incomingPayment.id, -// // debitAmount: { -// // value: '2', -// // assetCode: 'USD', -// // assetScale: 2, -// // }, -// // }, -// // ) -// // -// // const op = await client.outgoingPayment.create( -// // { -// // paymentPointer: sendingPaymentPointer.id, -// // accessToken: continuation.access_token.value, -// // }, -// // { -// // metadata: { -// // description: 'Outgoing Payment WM Event', -// // }, -// // quoteId: quote.id, -// // }, -// // ) -// // -// // console.log( -// // `[${new Date().toLocaleString()}] Sent ${op.debitAmount.value} -> Received: ${ -// // op.receiveAmount.value -// // }`, -// // ) -// // // }, 60000); -// } diff --git a/src/background/grant/confirmPayment.ts b/src/background/grant/confirmPayment.ts new file mode 100644 index 00000000..b7374727 --- /dev/null +++ b/src/background/grant/confirmPayment.ts @@ -0,0 +1,33 @@ +import { tabs } from 'webextension-polyfill' + +const getCurrentActiveTabId = async () => { + const activeTabs = await tabs.query({ active: true, currentWindow: true }) + return activeTabs[0].id +} + +export const confirmPayment = async (url: string) => { + const currentTabId = await getCurrentActiveTabId() + + return await new Promise(resolve => { + if (url) { + tabs.create({ url }).then(tab => { + if (tab.id) { + tabs.onUpdated.addListener((tabId, changeInfo) => { + try { + const tabUrl = new URL(changeInfo.url || '') + const interactRef = tabUrl.searchParams.get('interact_ref') + + if (tabId === tab.id && interactRef) { + tabs.update(currentTabId, { active: true }) + tabs.remove(tab.id) + resolve(interactRef) + } + } catch (e) { + throw new Error('Invalid interact ref url.') + } + }) + } + }) + } + }) +} diff --git a/src/background/grant/createQuote.ts b/src/background/grant/createQuote.ts new file mode 100644 index 00000000..5262bdd4 --- /dev/null +++ b/src/background/grant/createQuote.ts @@ -0,0 +1,44 @@ +import { AxiosInstance } from 'axios' + +import { getHeaders } from './getHeaders' + +type TCreateQuote = (_params: { + receiver: string + walletAddress: any + sendingUrl: string + token: string + amount: string + instance: AxiosInstance +}) => Promise + +export const createQuote: TCreateQuote = async ({ + receiver, + walletAddress, + sendingUrl, + token, + amount, + instance, +}) => { + const payload = { + method: 'ilp', + receiver, + walletAddress: walletAddress.id, + debitAmount: { + value: amount, // 0.001 USD + assetCode: walletAddress.assetCode, // 'USD' + assetScale: walletAddress.assetScale, // 9 + }, + } + + const quote = await instance.post( + new URL(sendingUrl).origin + '/quotes', + payload, + getHeaders(token), + ) + + if (!quote.data.id) { + throw new Error('No quote url id') + } + + return quote.data.id +} diff --git a/src/background/grant/getContinuationRequest.ts b/src/background/grant/getContinuationRequest.ts new file mode 100644 index 00000000..6947f267 --- /dev/null +++ b/src/background/grant/getContinuationRequest.ts @@ -0,0 +1,34 @@ +import { AxiosInstance } from 'axios' + +import { getHeaders } from './getHeaders' + +type TGetContinuationRequest = (_params: { + url: string + interactRef: any + token: string + instance: AxiosInstance +}) => Promise + +export const getContinuationRequest: TGetContinuationRequest = async ({ + url, + interactRef, + token, + instance, +}) => { + const continuationRequest = await instance.post( + url, + { + interact_ref: interactRef, + }, + getHeaders(token), + ) + + if (!continuationRequest.data.access_token.value) { + throw new Error('No continuation request') + } + + return { + manageUrl: continuationRequest.data.access_token.manage, + continuationRequestToken: continuationRequest.data.access_token.value, + } +} diff --git a/src/background/grant/getHeaders.ts b/src/background/grant/getHeaders.ts new file mode 100644 index 00000000..803f7b03 --- /dev/null +++ b/src/background/grant/getHeaders.ts @@ -0,0 +1,5 @@ +export const getHeaders = (gnapToken: string) => ({ + headers: { + Authorization: `GNAP ${gnapToken}`, + }, +}) diff --git a/src/background/grant/getIcomingPaymentGrant.ts b/src/background/grant/getIcomingPaymentGrant.ts new file mode 100644 index 00000000..df0e2f09 --- /dev/null +++ b/src/background/grant/getIcomingPaymentGrant.ts @@ -0,0 +1,36 @@ +import { AxiosInstance } from 'axios' + +type TGetIncomingPaymentGrant = (_params: { + client: string + identifier: string + wallet: Record + instance: AxiosInstance +}) => Promise + +export const getIncomingPaymentGrant: TGetIncomingPaymentGrant = async ({ + client, + identifier, + wallet, + instance, +}): Promise => { + const payload = { + access_token: { + access: [ + { + type: 'incoming-payment', + actions: ['create', 'read', 'list'], + identifier, // receivingPaymentPointerUrl + }, + ], + }, + client, // WM_PAYMENT_POINTER_URL + } + + const response = await instance.post(wallet.authServer + '/', payload) + + if (!response.data.access_token.value) { + throw new Error('No client auth') + } + + return response.data.access_token.value +} diff --git a/src/background/grant/getIncomingPaymentUrlId.ts b/src/background/grant/getIncomingPaymentUrlId.ts new file mode 100644 index 00000000..18fbbe9c --- /dev/null +++ b/src/background/grant/getIncomingPaymentUrlId.ts @@ -0,0 +1,30 @@ +import { AxiosInstance } from 'axios' + +import { getHeaders } from './getHeaders' + +type TGetIncomingPaymentUrlId = (_params: { + walletAddress: string + token: string + instance: AxiosInstance +}) => Promise + +export const getIncomingPaymentUrlId: TGetIncomingPaymentUrlId = async ({ + walletAddress, + token, + instance, +}) => { + const incomingPayment = await instance.post( + new URL(walletAddress).origin + '/incoming-payments', + { + walletAddress, // receivingPaymentPointerUrl + expiresAt: new Date(Date.now() + 6000 * 60 * 10).toISOString(), + }, + getHeaders(token), + ) + + if (!incomingPayment?.data?.id) { + throw new Error('No incoming payment id') + } + + return incomingPayment.data.id +} diff --git a/src/background/grant/getOutgoingPaymentGrant.ts b/src/background/grant/getOutgoingPaymentGrant.ts new file mode 100644 index 00000000..a9b258c2 --- /dev/null +++ b/src/background/grant/getOutgoingPaymentGrant.ts @@ -0,0 +1,59 @@ +import { AxiosInstance } from 'axios' + +type TGetOutgoingPaymentGrant = (_params: { + client: string + identifier: string + wallet: Record + amount: string | number + instance: AxiosInstance +}) => Promise + +export const getOutgoingPaymentGrant: TGetOutgoingPaymentGrant = async ({ + client, + identifier, + wallet, + amount, + instance, +}) => { + // const receivingPaymentPointerDetails = await this.axiosInstance.get( + // this.receivingPaymentPointerUrl, + // ) + const payload = { + access_token: { + access: [ + { + type: 'outgoing-payment', + actions: ['list', 'read', 'create'], + identifier, // sendingPaymentPointerUrl + limits: { + debitAmount: { + value: String(Number(amount) * 10 ** 9), // '1000000000', + assetScale: wallet.assetScale, // 9 + assetCode: wallet.assetCode, // 'USD' + }, + }, + }, + ], + }, + client, + interact: { + start: ['redirect'], + finish: { + method: 'redirect', + uri: `http://localhost:3035`, + nonce: new Date().getTime().toString(), + }, + }, + } + + const outgoingPaymentGrant = await instance.post(wallet.authServer + '/', payload) + + if (!outgoingPaymentGrant.data.interact.redirect) { + throw new Error('No redirect') + } + + return { + outgoingPaymentGrantToken: outgoingPaymentGrant.data.continue.access_token.value, + outgoingPaymentGrantData: outgoingPaymentGrant.data, + } +} diff --git a/src/background/grant/getQuoteGrant.ts b/src/background/grant/getQuoteGrant.ts new file mode 100644 index 00000000..1c449e35 --- /dev/null +++ b/src/background/grant/getQuoteGrant.ts @@ -0,0 +1,30 @@ +import { AxiosInstance } from 'axios' + +type TGetQuoteGrant = (_params: { + client: string + identifier: string + wallet: Record + instance: AxiosInstance +}) => Promise + +export const getQuoteGrant: TGetQuoteGrant = async ({ client, identifier, wallet, instance }) => { + const quotePayload = { + access_token: { + access: [ + { + type: 'quote', + actions: ['create'], + identifier, + }, + ], + }, + client, // WM_PAYMENT_POINTER_URL + } + const quoteGrant = await instance.post(wallet.authServer + '/', quotePayload) + + if (!quoteGrant.data?.access_token?.value) { + throw new Error('No quote grant') + } + + return quoteGrant.data.access_token.value +} diff --git a/src/background/grant/index.ts b/src/background/grant/index.ts new file mode 100644 index 00000000..7364d9b5 --- /dev/null +++ b/src/background/grant/index.ts @@ -0,0 +1,21 @@ +import { confirmPayment } from './confirmPayment' +import { createQuote } from './createQuote' +import { getContinuationRequest } from './getContinuationRequest' +import { getHeaders } from './getHeaders' +import { getIncomingPaymentGrant } from './getIcomingPaymentGrant' +import { getIncomingPaymentUrlId } from './getIncomingPaymentUrlId' +import { getOutgoingPaymentGrant } from './getOutgoingPaymentGrant' +import { getQuoteGrant } from './getQuoteGrant' +import { rotateToken } from './rotateToken' + +export { + confirmPayment, + createQuote, + getContinuationRequest, + getHeaders, + getIncomingPaymentGrant, + getIncomingPaymentUrlId, + getOutgoingPaymentGrant, + getQuoteGrant, + rotateToken, +} diff --git a/src/background/grant/rotateToken.ts b/src/background/grant/rotateToken.ts new file mode 100644 index 00000000..b4d8087f --- /dev/null +++ b/src/background/grant/rotateToken.ts @@ -0,0 +1,26 @@ +import { AxiosInstance } from 'axios' + +import { getHeaders } from '@/background/grant/getHeaders' + +type TRotateToken = (_params: { + url: string + token: string + instance: AxiosInstance +}) => Promise + +export const rotateToken: TRotateToken = async ({ url, token, instance }) => { + const response = await instance.post( + url, // this.manageUrl + undefined, + getHeaders(token), // this.continuationRequestToken + ) + + if (!response.data.access_token.value) { + throw new Error('No continuation request') + } + + return { + manageUrl: response.data.access_token.manage, + continuationRequestToken: response.data.access_token.value, + } +} diff --git a/src/background/grantFlow.ts b/src/background/grantFlow.ts index aa0f891b..18fac5fb 100644 --- a/src/background/grantFlow.ts +++ b/src/background/grantFlow.ts @@ -1,6 +1,16 @@ import { tabs } from 'webextension-polyfill' import { WM_WALLET_ADDRESS } from '@/background/config' +import { + createQuote, + getHeaders, + getIncomingPaymentGrant, + getIncomingPaymentUrlId, + getOutgoingPaymentGrant, + getQuoteGrant, +} from '@/background/grant' +import { confirmPayment } from '@/background/grant/confirmPayment' +import { getContinuationRequest } from '@/background/grant/getContinuationRequest' import { getAxiosInstance } from '@/background/requestConfig' export class PaymentFlowService { @@ -18,7 +28,6 @@ export class PaymentFlowService { outgoingPaymentGrantToken: string continuationRequestToken: string interactRef: string - walletAddressId: string manageUrl: string @@ -41,194 +50,65 @@ export class PaymentFlowService { this.sendingWalletAddress = await this.getWalletAddress(this.sendingPaymentPointerUrl) this.receivingWalletAddress = await this.getWalletAddress(this.receivingPaymentPointerUrl) - await this.getIncomingPaymentGrant() - await this.getIncomingPaymentUrlId() - await this.getQuoteGrant() - await this.getOutgoingPaymentGrant() - await this.confirmPayment() - await this.getContinuationRequest() - - const currentTabId = await this.getCurrentActiveTabId() - await tabs.sendMessage(currentTabId ?? 0, { type: 'START_PAYMENTS' }) - - // await this.createQuote() - // await this.runPayment() - } - - getHeaders(gnapToken: string) { - return { - headers: { - Authorization: `GNAP ${gnapToken}`, - }, - } - } - - async getWalletAddress(paymentPointerUrl: string) { - const response = await this.axiosInstance.get(paymentPointerUrl, { - headers: { - Accept: 'application/json', - }, + this.clientAuthToken = await getIncomingPaymentGrant({ + client: WM_WALLET_ADDRESS, + identifier: this.receivingPaymentPointerUrl, + wallet: this.receivingWalletAddress, + instance: this.axiosInstance, }) - if (!response?.data?.id) { - throw new Error('No client auth') - } - - return response.data - } + this.incomingPaymentUrlId = await getIncomingPaymentUrlId({ + walletAddress: this.receivingPaymentPointerUrl, + token: this.clientAuthToken, + instance: this.axiosInstance, + }) - async getIncomingPaymentGrant() { - const payload = { - access_token: { - access: [ - { - type: 'incoming-payment', - actions: ['create', 'read', 'list'], - identifier: this.receivingPaymentPointerUrl, - }, - ], - }, + this.quoteGrantToken = await getQuoteGrant({ client: WM_WALLET_ADDRESS, - } - - const response = await this.axiosInstance.post( - this.receivingWalletAddress.authServer + '/', - payload, - ) - - if (!response.data.access_token.value) { - throw new Error('No client auth') - } - this.clientAuthToken = response.data.access_token.value - } - - async getIncomingPaymentUrlId() { - const incomingPayment = await this.axiosInstance.post( - new URL(this.receivingPaymentPointerUrl).origin + '/incoming-payments', - { - walletAddress: this.receivingPaymentPointerUrl, - expiresAt: new Date(Date.now() + 6000 * 60 * 10).toISOString(), - }, - this.getHeaders(this.clientAuthToken), - ) - - if (!incomingPayment?.data?.id) { - throw new Error('No incoming payment id') - } - - this.incomingPaymentUrlId = incomingPayment.data.id - } + identifier: this.sendingPaymentPointerUrl, + wallet: this.sendingWalletAddress, + instance: this.axiosInstance, + }) - async getQuoteGrant() { - const quotePayload = { - access_token: { - access: [ - { - type: 'quote', - actions: ['create'], - identifier: this.sendingPaymentPointerUrl, - }, - ], - }, + const outgoingData = await getOutgoingPaymentGrant({ client: WM_WALLET_ADDRESS, - } - const quoteGrant = await this.axiosInstance.post( - this.sendingWalletAddress.authServer + '/', - quotePayload, - ) - - if (!quoteGrant.data?.access_token?.value) { - throw new Error('No quote grant') - } - - this.quoteGrantToken = quoteGrant.data.access_token.value - } - - async createQuote() { - const payload = { - method: 'ilp', - receiver: this.incomingPaymentUrlId, - walletAddress: this.sendingPaymentPointerUrl, - debitAmount: { - value: '1000000', // 0.001 USD - assetCode: 'USD', - assetScale: 9, - }, - } - - const quote = await this.axiosInstance.post( - new URL(this.sendingPaymentPointerUrl).origin + '/quotes', - payload, - this.getHeaders(this.quoteGrantToken), - ) - - if (!quote.data.id) { - throw new Error('No quote url id') - } + identifier: this.sendingPaymentPointerUrl, + wallet: this.sendingWalletAddress, + amount: this.amount, + instance: this.axiosInstance, + }) - this.quoteUrlId = quote.data.id - } + this.outgoingPaymentGrantToken = outgoingData.outgoingPaymentGrantToken + this.outgoingPaymentGrantData = outgoingData.outgoingPaymentGrantData - async getOutgoingPaymentGrant() { - // const receivingPaymentPointerDetails = await this.axiosInstance.get( - // this.receivingPaymentPointerUrl, - // ) - const payload = { - access_token: { - access: [ - { - type: 'outgoing-payment', - actions: ['list', 'list-all', 'read', 'read-all', 'create'], - identifier: this.sendingPaymentPointerUrl, // sendingPaymentPointerUrl - limits: { - debitAmount: { - value: String(Number(this.amount) * 10 ** 9), // '1000000000', - assetScale: 9, - assetCode: 'USD', - }, - }, - }, - ], - }, - client: WM_WALLET_ADDRESS, - interact: { - start: ['redirect'], - finish: { - method: 'redirect', - uri: `http://localhost:3035`, - nonce: new Date().getTime().toString(), - }, - }, - } + this.interactRef = await confirmPayment(this.outgoingPaymentGrantData.interact.redirect) - const outgoingPaymentGrant = await this.axiosInstance.post( - this.sendingWalletAddress.authServer + '/', - payload, - ) + const continuationRequest = await getContinuationRequest({ + url: this.outgoingPaymentGrantData.continue.uri, + interactRef: this.interactRef, + token: this.outgoingPaymentGrantToken, + instance: this.axiosInstance, + }) - if (!outgoingPaymentGrant.data.interact.redirect) { - throw new Error('No redirect') - } + this.manageUrl = continuationRequest.manageUrl + this.continuationRequestToken = continuationRequest.continuationRequestToken - this.outgoingPaymentGrantToken = outgoingPaymentGrant.data.continue.access_token.value - this.outgoingPaymentGrantData = outgoingPaymentGrant.data + const currentTabId = await this.getCurrentActiveTabId() + await tabs.sendMessage(currentTabId ?? 0, { type: 'START_PAYMENTS' }) } - async getContinuationRequest() { - const continuationRequest = await this.axiosInstance.post( - this.outgoingPaymentGrantData.continue.uri, - { - interact_ref: this.interactRef, + async getWalletAddress(paymentPointerUrl: string) { + const response = await this.axiosInstance.get(paymentPointerUrl, { + headers: { + Accept: 'application/json', }, - this.getHeaders(this.outgoingPaymentGrantToken), - ) + }) - if (!continuationRequest.data.access_token.value) { - throw new Error('No continuation request') + if (!response?.data?.id) { + throw new Error('No client auth') } - this.manageUrl = continuationRequest.data.access_token.manage - this.continuationRequestToken = continuationRequest.data.access_token.value + return response.data } async runPayment() { @@ -240,7 +120,7 @@ export class PaymentFlowService { const response = await this.axiosInstance.post( new URL(this.sendingPaymentPointerUrl).origin + '/outgoing-payments', payload, - this.getHeaders(this.continuationRequestToken), + getHeaders(this.continuationRequestToken), ) const { @@ -256,46 +136,16 @@ export class PaymentFlowService { // console.log('outgoingPayment', outgoingPayment) } - async confirmPayment() { - const currentTabId = await this.getCurrentActiveTabId() - - return await new Promise(resolve => { - if (this.outgoingPaymentGrantData.interact.redirect) { - const url = this.outgoingPaymentGrantData.interact.redirect - - tabs.create({ url }).then(tab => { - if (tab.id) { - tabs.onUpdated.addListener((tabId, changeInfo) => { - if (tabId === tab.id && changeInfo.url?.includes('interact_ref')) { - this.interactRef = changeInfo.url.split('interact_ref=')[1] - tabs.update(currentTabId, { active: true }) - tabs.remove(tab.id) - resolve(true) - } - }) - } - }) - } + async sendPayment() { + this.quoteUrlId = await createQuote({ + receiver: this.incomingPaymentUrlId, + walletAddress: this.sendingWalletAddress, + sendingUrl: this.sendingPaymentPointerUrl, + token: this.quoteGrantToken, + amount: '1000000', + instance: this.axiosInstance, }) - } - - async rotateToken() { - const response = await this.axiosInstance.post( - this.manageUrl, - undefined, - this.getHeaders(this.continuationRequestToken), - ) - - if (!response.data.access_token.value) { - throw new Error('No continuation request') - } - this.manageUrl = response.data.access_token.manage - this.continuationRequestToken = response.data.access_token.value - } - - async sendPayment() { - await this.createQuote() await this.runPayment() } diff --git a/src/background/paymentFlow.ts b/src/background/paymentFlow.ts deleted file mode 100644 index 0a69d2ff..00000000 --- a/src/background/paymentFlow.ts +++ /dev/null @@ -1,150 +0,0 @@ -import { WM_WALLET_ADDRESS } from '@/background/config' -import { getAxiosInstance } from '@/background/requestConfig' - -export const initPaymentFlow = async ( - sendingPaymentPointerUrl: string, - receivingPaymentPointerUrl: string, -) => { - const axiosInstance = getAxiosInstance() - - const payload = { - access_token: { - access: [ - { - type: 'incoming-payment', - actions: ['create', 'read', 'list'], - identifier: receivingPaymentPointerUrl, - }, - ], - }, - client: WM_WALLET_ADDRESS, - } - const clientAuth = await axiosInstance.post('https://auth.rafiki.money/', payload) - - if (clientAuth.data?.access_token?.value) { - const headers = { - headers: { - Authorization: `GNAP ${clientAuth.data.access_token.value}`, - }, - } - - const incomingPayment = await axiosInstance.post( - `${receivingPaymentPointerUrl}/incoming-payments`, - {}, - headers, - ) - const incomingPaymentUrlId = incomingPayment?.data?.id - - if (incomingPaymentUrlId) { - const quotePayload = { - access_token: { - access: [ - { - type: 'quote', - actions: ['create'], - identifier: sendingPaymentPointerUrl, - }, - ], - }, - client: WM_WALLET_ADDRESS, - } - const quoteGrant = await axiosInstance.post('https://auth.rafiki.money/', quotePayload) - - if (quoteGrant.data?.access_token?.value) { - const headersQuote = { - headers: { - Authorization: `GNAP ${quoteGrant.data.access_token.value}`, - }, - } - - const quote = await axiosInstance.post( - `${sendingPaymentPointerUrl}/quotes`, - { - receiver: incomingPaymentUrlId, - debitAmount: { - value: '1000', - assetCode: 'USD', - assetScale: 2, - }, - }, - { ...headersQuote }, - ) - - const quoteUrlId = quote.data.id - - if (quoteUrlId) { - const outgoingGrantPayload = { - access_token: { - access: [ - { - type: 'outgoing-payment', - actions: ['create', 'read', 'list'], - identifier: sendingPaymentPointerUrl, - limits: { - debitAmount: { - value: '2000', - assetScale: 2, - assetCode: 'USD', - }, - }, - }, - ], - }, - client: WM_WALLET_ADDRESS, - interact: { - start: ['redirect'], - finish: { - method: 'redirect', - uri: `https://localhost:3000/`, - nonce: new Date().getTime().toString(), - }, - }, - } - - const outgoingPaymentGrant = await axiosInstance.post( - 'https://auth.rafiki.money/', - outgoingGrantPayload, - ) - console.log('outgoingPaymentGrant', outgoingPaymentGrant.data.interact.redirect) - - // `https://rafiki.money/?hash=SKENFpVdQFenQ0rMiaHCFTkFq11SEFYSAUBhCrw8xvU%3D&interact_ref=10e7a3ab-496c-4d50-bea1-593189be4b75` - const interactRef = '10e7a3ab-496c-4d50-bea1-593189be4b75' - - if (outgoingPaymentGrant.data.interact.redirect) { - const continuationRequestHeaders = { - headers: { - Authorization: `GNAP ${outgoingPaymentGrant.data.continue.access_token.value}`, - }, - } - - const continuationRequest = await axiosInstance.post( - outgoingPaymentGrant.data.continue.uri, - { - interact_ref: interactRef, - }, - { ...continuationRequestHeaders }, - ) - - console.log('continuationRequest', continuationRequest.data.access_token.value) - - const outgoingPaymentHeaders = { - headers: { - Authorization: `GNAP ${continuationRequest.data.access_token.value}`, - }, - } - - const outgoingPayment = await axiosInstance.post( - `${sendingPaymentPointerUrl}/outgoing-payments`, - { - quoteId: quoteUrlId, - }, - { ...outgoingPaymentHeaders }, - ) - - console.log('outgoingPayment', outgoingPayment) - } - } - } - } - } -}