Skip to content

Commit

Permalink
chore: code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ionutanin committed Dec 7, 2023
1 parent c9450fe commit 88e090b
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 88 deletions.
16 changes: 11 additions & 5 deletions src/background/grant/confirmPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ export const confirmPayment = async (url: string) => {
tabs.create({ url }).then(tab => {
if (tab.id) {
tabs.onUpdated.addListener((tabId, changeInfo) => {
if (tabId === tab.id && changeInfo.url?.includes('interact_ref')) {
const interactRef = changeInfo.url.split('interact_ref=')[1]
tabs.update(currentTabId, { active: true })
tabs.remove(tab.id)
resolve(interactRef)
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.')
}
})
}
Expand Down
32 changes: 21 additions & 11 deletions src/background/grant/createQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ import { AxiosInstance } from 'axios'

import { getHeaders } from './getHeaders'

export const createQuote = async (
receiver: string,
walletAddress: string,
sendingUrl: string,
token: string,
instance: AxiosInstance,
) => {
type TCreateQuote = (_params: {
receiver: string
walletAddress: any
sendingUrl: string
token: string
amount: string
instance: AxiosInstance
}) => Promise<any>

export const createQuote: TCreateQuote = async ({
receiver,
walletAddress,
sendingUrl,
token,
amount,
instance,
}) => {
const payload = {
method: 'ilp',
receiver,
walletAddress,
walletAddress: walletAddress.id,
debitAmount: {
value: '1000000', // 0.001 USD
assetCode: 'USD',
assetScale: 9,
value: amount, // 0.001 USD
assetCode: walletAddress.assetCode, // 'USD'
assetScale: walletAddress.assetScale, // 9
},
}

Expand Down
19 changes: 13 additions & 6 deletions src/background/grant/getContinuationRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import { AxiosInstance } from 'axios'

import { getHeaders } from './getHeaders'

export const getContinuationRequest = async (
url: string,
interactRef: string,
token: string,
instance: AxiosInstance,
) => {
type TGetContinuationRequest = (_params: {
url: string
interactRef: any
token: string
instance: AxiosInstance
}) => Promise<any>

export const getContinuationRequest: TGetContinuationRequest = async ({
url,
interactRef,
token,
instance,
}) => {
const continuationRequest = await instance.post(
url,
{
Expand Down
19 changes: 13 additions & 6 deletions src/background/grant/getIcomingPaymentGrant.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { AxiosInstance } from 'axios'

export const getIncomingPaymentGrant = async (
client: string,
identifier: string,
wallet: Record<string, any>,
instance: AxiosInstance,
): Promise<string> => {
type TGetIncomingPaymentGrant = (_params: {
client: string
identifier: string
wallet: Record<string, any>
instance: AxiosInstance
}) => Promise<any>

export const getIncomingPaymentGrant: TGetIncomingPaymentGrant = async ({
client,
identifier,
wallet,
instance,
}): Promise<string> => {
const payload = {
access_token: {
access: [
Expand Down
18 changes: 12 additions & 6 deletions src/background/grant/getIncomingPaymentUrlId.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Axios } from 'axios'
import { AxiosInstance } from 'axios'

import { getHeaders } from './getHeaders'

export const getIncomingPaymentUrlId = async (
walletAddress: string,
token: string,
instance: Axios,
): Promise<string> => {
type TGetIncomingPaymentUrlId = (_params: {
walletAddress: string
token: string
instance: AxiosInstance
}) => Promise<any>

export const getIncomingPaymentUrlId: TGetIncomingPaymentUrlId = async ({
walletAddress,
token,
instance,
}) => {
const incomingPayment = await instance.post(
new URL(walletAddress).origin + '/incoming-payments',
{
Expand Down
28 changes: 18 additions & 10 deletions src/background/grant/getOutgoingPaymentGrant.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { AxiosInstance } from 'axios'

export const getOutgoingPaymentGrant = async (
client: string,
identifier: string,
wallet: Record<string, any>,
amount: string | number,
instance: AxiosInstance,
) => {
type TGetOutgoingPaymentGrant = (_params: {
client: string
identifier: string
wallet: Record<string, any>
amount: string | number
instance: AxiosInstance
}) => Promise<any>

export const getOutgoingPaymentGrant: TGetOutgoingPaymentGrant = async ({
client,
identifier,
wallet,
amount,
instance,
}) => {
// const receivingPaymentPointerDetails = await this.axiosInstance.get(
// this.receivingPaymentPointerUrl,
// )
Expand All @@ -15,13 +23,13 @@ export const getOutgoingPaymentGrant = async (
access: [
{
type: 'outgoing-payment',
actions: ['list', 'list-all', 'read', 'read-all', 'create'],
actions: ['list', 'read', 'create'],
identifier, // sendingPaymentPointerUrl
limits: {
debitAmount: {
value: String(Number(amount) * 10 ** 9), // '1000000000',
assetScale: 9,
assetCode: 'USD',
assetScale: wallet.assetScale, // 9
assetCode: wallet.assetCode, // 'USD'
},
},
},
Expand Down
14 changes: 8 additions & 6 deletions src/background/grant/getQuoteGrant.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { AxiosInstance } from 'axios'

export const getQuoteGrant = async (
client: string,
identifier: string,
wallet: Record<string, any>,
instance: AxiosInstance,
): Promise<string> => {
type TGetQuoteGrant = (_params: {
client: string
identifier: string
wallet: Record<string, any>
instance: AxiosInstance
}) => Promise<any>

export const getQuoteGrant: TGetQuoteGrant = async ({ client, identifier, wallet, instance }) => {
const quotePayload = {
access_token: {
access: [
Expand Down
8 changes: 7 additions & 1 deletion src/background/grant/rotateToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { AxiosInstance } from 'axios'

import { getHeaders } from '@/background/grant/getHeaders'

export const rotateToken = async (url: string, token: string, instance: AxiosInstance) => {
type TRotateToken = (_params: {
url: string
token: string
instance: AxiosInstance
}) => Promise<any>

export const rotateToken: TRotateToken = async ({ url, token, instance }) => {
const response = await instance.post(
url, // this.manageUrl
undefined,
Expand Down
75 changes: 38 additions & 37 deletions src/background/grantFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,45 @@ export class PaymentFlowService {
this.sendingWalletAddress = await this.getWalletAddress(this.sendingPaymentPointerUrl)
this.receivingWalletAddress = await this.getWalletAddress(this.receivingPaymentPointerUrl)

this.clientAuthToken = await getIncomingPaymentGrant(
WM_PAYMENT_POINTER_URL,
this.receivingPaymentPointerUrl,
this.receivingWalletAddress,
this.axiosInstance,
)
this.clientAuthToken = await getIncomingPaymentGrant({
client: WM_PAYMENT_POINTER_URL,
identifier: this.receivingPaymentPointerUrl,
wallet: this.receivingWalletAddress,
instance: this.axiosInstance,
})

this.incomingPaymentUrlId = await getIncomingPaymentUrlId(
this.receivingPaymentPointerUrl,
this.clientAuthToken,
this.axiosInstance,
)
this.incomingPaymentUrlId = await getIncomingPaymentUrlId({
walletAddress: this.receivingPaymentPointerUrl,
token: this.clientAuthToken,
instance: this.axiosInstance,
})

this.quoteGrantToken = await getQuoteGrant(
WM_PAYMENT_POINTER_URL,
this.sendingPaymentPointerUrl,
this.sendingWalletAddress,
this.axiosInstance,
)
this.quoteGrantToken = await getQuoteGrant({
client: WM_PAYMENT_POINTER_URL,
identifier: this.sendingPaymentPointerUrl,
wallet: this.sendingWalletAddress,
instance: this.axiosInstance,
})

const outgoingData = await getOutgoingPaymentGrant(
WM_PAYMENT_POINTER_URL,
this.sendingPaymentPointerUrl,
this.sendingWalletAddress,
this.amount,
this.axiosInstance,
)
const outgoingData = await getOutgoingPaymentGrant({
client: WM_PAYMENT_POINTER_URL,
identifier: this.sendingPaymentPointerUrl,
wallet: this.sendingWalletAddress,
amount: this.amount,
instance: this.axiosInstance,
})

this.outgoingPaymentGrantToken = outgoingData.outgoingPaymentGrantToken
this.outgoingPaymentGrantData = outgoingData.outgoingPaymentGrantData

this.interactRef = await confirmPayment(this.outgoingPaymentGrantData.interact.redirect)

const continuationRequest = await getContinuationRequest(
this.outgoingPaymentGrantData.continue.uri,
this.interactRef,
this.outgoingPaymentGrantToken,
this.axiosInstance,
)
const continuationRequest = await getContinuationRequest({
url: this.outgoingPaymentGrantData.continue.uri,
interactRef: this.interactRef,
token: this.outgoingPaymentGrantToken,
instance: this.axiosInstance,
})

this.manageUrl = continuationRequest.manageUrl
this.continuationRequestToken = continuationRequest.continuationRequestToken
Expand Down Expand Up @@ -141,13 +141,14 @@ export class PaymentFlowService {
}

async sendPayment() {
this.quoteUrlId = await createQuote(
this.incomingPaymentUrlId,
this.sendingPaymentPointerUrl,
this.sendingPaymentPointerUrl,
this.quoteGrantToken,
this.axiosInstance,
)
this.quoteUrlId = await createQuote({
receiver: this.incomingPaymentUrlId,
walletAddress: this.sendingWalletAddress,
sendingUrl: this.sendingPaymentPointerUrl,
token: this.quoteGrantToken,
amount: '1000000',
instance: this.axiosInstance,
})

await this.runPayment()
}
Expand Down

0 comments on commit 88e090b

Please sign in to comment.