diff --git a/core/src/encodeURL.ts b/core/src/encodeURL.ts index 2689d637..a12f0ed2 100644 --- a/core/src/encodeURL.ts +++ b/core/src/encodeURL.ts @@ -1,5 +1,5 @@ import { SOLANA_PROTOCOL } from './constants.js'; -import type { Amount, Label, Memo, Message, Recipient, References, SPLToken } from './types.js'; +import type { Amount, Label, Memo, Message, Recipient, References, SPLToken, Redirect } from './types.js'; /** * Fields of a Solana Pay transaction request URL. @@ -31,6 +31,16 @@ export interface TransferRequestURLFields { message?: Message; /** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo). */ memo?: Memo; + /** `redirect` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC1.1.md#redirect). */ + redirect?: Redirect; +} + +/** + * Fields of a Solana Pay message sign request URL. + */ +export interface MessageSignRequestURLFields { + /** `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/message-signing-spec.md#link). */ + link: URL; } /** @@ -38,11 +48,17 @@ export interface TransferRequestURLFields { * * @param fields Fields to encode in the URL. */ -export function encodeURL(fields: TransactionRequestURLFields | TransferRequestURLFields): URL { - return 'link' in fields ? encodeTransactionRequestURL(fields) : encodeTransferRequestURL(fields); +export function encodeURL( + fields: TransactionRequestURLFields | TransferRequestURLFields | MessageSignRequestURLFields +): URL { + return 'link' in fields ? encodeTransactionOrMessageSignRequestURL(fields) : encodeTransferRequestURL(fields); } -function encodeTransactionRequestURL({ link, label, message }: TransactionRequestURLFields): URL { +function encodeTransactionOrMessageSignRequestURL({ + link, + label, + message, +}: TransactionRequestURLFields | (MessageSignRequestURLFields & { label: undefined; message: undefined })): URL { // Remove trailing slashes const pathname = link.search ? encodeURIComponent(String(link).replace(/\/\?/, '?')) @@ -68,6 +84,7 @@ function encodeTransferRequestURL({ label, message, memo, + redirect, }: TransferRequestURLFields): URL { const pathname = recipient.toBase58(); const url = new URL(SOLANA_PROTOCOL + pathname); @@ -102,5 +119,9 @@ function encodeTransferRequestURL({ url.searchParams.append('memo', memo); } + if (redirect) { + url.searchParams.append('redirect', redirect.toString()); + } + return url; } diff --git a/core/src/types.ts b/core/src/types.ts index 4b3a7a8d..9d6eb636 100644 --- a/core/src/types.ts +++ b/core/src/types.ts @@ -25,5 +25,8 @@ export type Message = string; /** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo). */ export type Memo = string; +/** `redirect` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC1.1.md#redirect). */ +export type Redirect = URL; + /** `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#link). */ export type Link = URL;