Skip to content

Commit

Permalink
Merge pull request #108 from grassrootseconomics/philip/non-custodail…
Browse files Browse the repository at this point in the history
…-interplay

Accommodate non-custodial interplay with USSD.
  • Loading branch information
mango-habanero authored Nov 28, 2023
2 parents 95a1ad3 + 142cd90 commit 7ee8049
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/lib/graph/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ export async function getGraphAddressFromTill(graphql: GraphQLClient, till: stri
export async function getGraphAddressFromVpa(graphql: GraphQLClient, vpa: string) {
const query = `query GetAddressFromVpa($vpa: String!) {
vpa(where: {vpa: {_eq: $vpa}}) {
account {
blockchain_address
}
account {blockchain_address}
}
}`

const variables = {
Expand Down
21 changes: 18 additions & 3 deletions src/machines/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import { isBlocked, isValidPin, validatePin } from '@machines/auth';
import { custodialTransfer } from '@lib/custodail';
import { createTracker, TaskType } from '@db/models/custodailTasks';
import { BaseMachineError, ContextError, MachineError, SystemError } from '@lib/errors';
import {cashRounding, getAddressFromTill, sendSMS, validatePhoneNumber} from '@lib/ussd';
import {cashRounding, getAddressFromTill, getAddressFromVpa, sendSMS, validatePhoneNumber} from '@lib/ussd';
import {tHelpers, translate, tSMS} from '@i18n/translators';
import { config } from '@/config';
import {getPhoneNumberFromAddress} from "@services/account";
import {logger} from "@/app";
import {ethers} from "ethers";

const VPA_PATTERN = /^[a-zA-Z0-9]+@[a-zA-Z]+$/

enum TransferError {
INVALID_ADDRESS = 'INVALID_ADDRESS',
INVALID_RECIPIENT = 'INVALID_RECIPIENT',
Expand Down Expand Up @@ -293,7 +295,7 @@ function isValidAmount(context: TransferContext, event: any) {
}

function isValidIdentifier(context: TransferContext, event: any) {
return event.input.length === 6 || event.input.startsWith('0x') || isValidPhoneNumber(context, event)
return event.input.length === 6 || event.input.startsWith('0x') || isValidPhoneNumber(context, event) || VPA_PATTERN.test(event.input)
}

function saveAmount(context: TransferContext, event: any) {
Expand Down Expand Up @@ -337,7 +339,20 @@ async function validateRecipient(context: TransferContext, event: any) {
}
phoneNumber = await getPhoneNumberFromAddress(address, context.connections.db, context.connections.redis.persistent)
if(!phoneNumber){
throw new MachineError(BaseMachineError.UNKNOWN_ADDRESS, `Account not found for address: ${address}.`)
logger.warn(`No account found for till ${input}.`)
return { address: address, tag: address }
}
}

if (VPA_PATTERN.test(input)) {
const address = await getAddressFromVpa(context.connections.graphql, context.connections.redis.persistent, input)
if (!address) {
throw new MachineError(BaseMachineError.UNKNOWN_TILL_OR_VPA, `No account found for vpa: ${input}`)
}
phoneNumber = await getPhoneNumberFromAddress(address, context.connections.db, context.connections.redis.persistent)
if(!phoneNumber){
logger.warn(`No account found for VPA ${input}.`)
return { address: address, tag: address }
}
}

Expand Down

0 comments on commit 7ee8049

Please sign in to comment.