From c7496d9ef1bdb9a1df0182d7246628a67e1d9f47 Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Tue, 28 Feb 2023 14:01:34 +0000 Subject: [PATCH 1/2] Add optional redirect to transfer request encode URL --- core/src/encodeURL.ts | 9 ++++++++- core/src/types.ts | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/encodeURL.ts b/core/src/encodeURL.ts index 2689d637..fc777f77 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,8 @@ 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; } /** @@ -68,6 +70,7 @@ function encodeTransferRequestURL({ label, message, memo, + redirect, }: TransferRequestURLFields): URL { const pathname = recipient.toBase58(); const url = new URL(SOLANA_PROTOCOL + pathname); @@ -102,5 +105,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; From 4302705e9bcf7ea231895908fd946c08f16d176d Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Tue, 28 Feb 2023 15:13:03 +0000 Subject: [PATCH 2/2] Add support for encoding message sign URL --- core/src/encodeURL.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/core/src/encodeURL.ts b/core/src/encodeURL.ts index fc777f77..a12f0ed2 100644 --- a/core/src/encodeURL.ts +++ b/core/src/encodeURL.ts @@ -35,16 +35,30 @@ export interface TransferRequestURLFields { 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; +} + /** * Encode a Solana Pay URL. * * @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(/\/\?/, '?'))