Skip to content

Commit

Permalink
chore: payment flow restructure (#37)
Browse files Browse the repository at this point in the history
* update payment flow, restructure into different files and prepare for unit test, remove unused code files

* remove console.log - move rotateToken

* chore: code review updates
  • Loading branch information
ionutanin authored Jan 8, 2024
1 parent 8386c32 commit 4aa460a
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 537 deletions.
176 changes: 0 additions & 176 deletions src/background/grant.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/background/grant/confirmPayment.ts
Original file line number Diff line number Diff line change
@@ -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<string>(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.')
}
})
}
})
}
})
}
44 changes: 44 additions & 0 deletions src/background/grant/createQuote.ts
Original file line number Diff line number Diff line change
@@ -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<any>

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
}
34 changes: 34 additions & 0 deletions src/background/grant/getContinuationRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { AxiosInstance } from 'axios'

import { getHeaders } from './getHeaders'

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,
{
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,
}
}
5 changes: 5 additions & 0 deletions src/background/grant/getHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getHeaders = (gnapToken: string) => ({
headers: {
Authorization: `GNAP ${gnapToken}`,
},
})
36 changes: 36 additions & 0 deletions src/background/grant/getIcomingPaymentGrant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { AxiosInstance } from 'axios'

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: [
{
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
}
30 changes: 30 additions & 0 deletions src/background/grant/getIncomingPaymentUrlId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AxiosInstance } from 'axios'

import { getHeaders } from './getHeaders'

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',
{
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
}
Loading

0 comments on commit 4aa460a

Please sign in to comment.