-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mobile: use bitrefill new parse (#1366)
- Loading branch information
1 parent
cffce76
commit 1a0526e
Showing
6 changed files
with
70 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |