-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mobile: use bitrefill new parse #1366
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { BigIntStr } from "@daimo/common"; | ||
import { polygonUSDC } from "@daimo/contract"; | ||
import { isAddress, getAddress } from "viem"; | ||
|
||
export function parsePaymentUri(uri: string) { | ||
const [protocol, rest] = uri.split(":"); | ||
if (protocol !== "ethereum") throw new Error("Invalid protocol"); | ||
|
||
const [tokenAddressAndChain, pathAndQuery] = rest.split("/"); | ||
const [tokenAddress, chainId] = tokenAddressAndChain.split("@"); | ||
if ( | ||
!isAddress(tokenAddress) || | ||
getAddress(tokenAddress) !== polygonUSDC.token | ||
) { | ||
throw new Error("Invalid token address"); | ||
} | ||
if (!chainId || chainId !== "137") { | ||
throw new Error("Unsupported chain ID"); | ||
} | ||
|
||
const [path, queryString] = pathAndQuery.split("?"); | ||
if (path !== "transfer") throw new Error("Invalid path"); | ||
|
||
const params = new URLSearchParams(queryString); | ||
const recipientAddress = params.get("address"); | ||
const amount = params.get("uint256"); | ||
|
||
if (!recipientAddress || !isAddress(recipientAddress)) { | ||
throw new Error("Invalid recipient address"); | ||
} | ||
if (!amount) throw new Error("Missing amount"); | ||
|
||
return { | ||
recipientAddress: getAddress(recipientAddress), | ||
amount: BigInt(Number(amount)).toString() as BigIntStr, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. saw it gets rounded up anyway in BitrefillBottomSheet.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, actually it's totally fine for USDC. issue only happens with 18-decimal tokens |
||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import { assert, zDollarStr } from "@daimo/common"; | ||
import { useContext } from "react"; | ||
import { View } from "react-native"; | ||
import { WebView } from "react-native-webview"; | ||
import { getAddress, isAddress } from "viem"; | ||
import { z } from "zod"; | ||
import { getAddress } from "viem"; | ||
|
||
import { DispatcherContext } from "../../../action/dispatch"; | ||
import { useNav } from "../../../common/nav"; | ||
import { i18NLocale } from "../../../i18n"; | ||
import { parsePaymentUri } from "../../../logic/paymentURI"; | ||
import { ScreenHeader } from "../../shared/ScreenHeader"; | ||
import { useTheme } from "../../style/theme"; | ||
|
||
|
@@ -22,29 +21,12 @@ export function BitrefillWebView() { | |
case "payment_intent": { | ||
console.log(`[BITREFILL] payment_intent ${paymentUri}`); | ||
|
||
const PaymentUriSchema = z.object({ | ||
protocol: z.literal("ethereum"), | ||
address: z.string().refine((addr) => isAddress(addr), { | ||
message: "Invalid Ethereum address", | ||
}), | ||
amount: zDollarStr, | ||
}); | ||
|
||
try { | ||
const [protocol, rest] = paymentUri.split(":"); | ||
assert(protocol === "ethereum"); | ||
const [address, queryString] = rest.split("?"); | ||
const params = new URLSearchParams(queryString); | ||
|
||
const parsedUri = PaymentUriSchema.parse({ | ||
protocol, | ||
address, | ||
amount: params.get("amount") || "", | ||
}); | ||
const parsedUri = parsePaymentUri(paymentUri); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
dispatcher.dispatch({ | ||
name: "bitrefill", | ||
address: getAddress(parsedUri.address), | ||
address: getAddress(parsedUri.recipientAddress), | ||
amount: parsedUri.amount, | ||
}); | ||
} catch (error) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { parsePaymentUri } from "../src/logic/paymentURI"; | ||
|
||
describe("Payment URI", () => { | ||
// https://github.com/daimo-eth/daimo/issues/1356 | ||
it("Parses Bitrefill URI", () => { | ||
const uri = parsePaymentUri( | ||
`ethereum:0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359@137/transfer?address=0xaCB6230043d1Fc3dE02a43Aa748540bb9F260931&uint256=1e8` | ||
); | ||
|
||
expect(uri.recipientAddress).toEqual( | ||
"0xaCB6230043d1Fc3dE02a43Aa748540bb9F260931" | ||
); | ||
expect(uri.amount).toEqual("100000000"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 |
||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍