Socket
Socket
Sign inDemoInstall

@solana/pay

Package Overview
Dependencies
Maintainers
14
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solana/pay - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

lib/cjs/createTransfer.js

6

lib/cjs/constants.js

@@ -6,8 +6,10 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.TEN = exports.SOL_DECIMALS = exports.MEMO_PROGRAM_ID = exports.URL_PROTOCOL = void 0;
exports.TEN = exports.SOL_DECIMALS = exports.MEMO_PROGRAM_ID = exports.HTTPS_PROTOCOL = exports.SOLANA_PROTOCOL = void 0;
const web3_js_1 = require("@solana/web3.js");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
/** @internal */
exports.URL_PROTOCOL = 'solana:';
exports.SOLANA_PROTOCOL = 'solana:';
/** @internal */
exports.HTTPS_PROTOCOL = 'https:';
/** @internal */
exports.MEMO_PROGRAM_ID = new web3_js_1.PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr');

@@ -14,0 +16,0 @@ /** @internal */

@@ -11,6 +11,6 @@ "use strict";

*
* @param url - The URL to encode in the QR code.
* @param size - Size of canvas in `px`.
* @param background - Background color for QR code.
* @param color - Color for QR code pattern.
* @param url - The URL to encode.
* @param size - Width and height in pixels.
* @param background - Background color, which should be light for device compatibility.
* @param color - Foreground color, which should be dark for device compatibility.
*/

@@ -27,3 +27,3 @@ function createQR(url, size = 512, background = 'white', color = 'black') {

height: size,
data: url,
data: String(url),
margin: 16,

@@ -30,0 +30,0 @@ qrOptions: {

"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -17,31 +6,32 @@ exports.encodeURL = void 0;

/**
* Encode a Solana Pay URL from required and optional components.
* Encode a Solana Pay URL.
*
* @param {EncodeURLComponents} components
*
* @param components.recipient
* @param components.amount
* @param components.splToken
* @param components.reference
* @param components.label
* @param components.message
* @param components.memo
* @param fields Fields to encode in the URL.
*/
function encodeURL(_a) {
var { recipient } = _a, params = __rest(_a, ["recipient"]);
let url = constants_1.URL_PROTOCOL + encodeURIComponent(recipient.toBase58());
const encodedParams = encodeURLParams(params);
if (encodedParams) {
url += '?' + encodedParams;
function encodeURL(fields) {
return 'link' in fields ? encodeTransactionRequestURL(fields) : encodeTransferRequestURL(fields);
}
exports.encodeURL = encodeURL;
function encodeTransactionRequestURL({ link, label, message }) {
// Remove trailing slashes
const pathname = link.search
? encodeURIComponent(String(link).replace(/\/\?/, '?'))
: String(link).replace(/\/$/, '');
const url = new URL(constants_1.SOLANA_PROTOCOL + pathname);
if (label) {
url.searchParams.append('label', label);
}
if (message) {
url.searchParams.append('message', message);
}
return url;
}
exports.encodeURL = encodeURL;
function encodeURLParams({ amount, splToken, reference, label, message, memo }) {
const params = [];
function encodeTransferRequestURL({ recipient, amount, splToken, reference, label, message, memo, }) {
const pathname = recipient.toBase58();
const url = new URL(constants_1.SOLANA_PROTOCOL + pathname);
if (amount) {
params.push(['amount', amount.toFixed(amount.decimalPlaces())]);
url.searchParams.append('amount', amount.toFixed(amount.decimalPlaces()));
}
if (splToken) {
params.push(['spl-token', splToken.toBase58()]);
url.searchParams.append('spl-token', splToken.toBase58());
}

@@ -53,16 +43,16 @@ if (reference) {

for (const pubkey of reference) {
params.push(['reference', pubkey.toBase58()]);
url.searchParams.append('reference', pubkey.toBase58());
}
}
if (label) {
params.push(['label', label]);
url.searchParams.append('label', label);
}
if (message) {
params.push(['message', message]);
url.searchParams.append('message', message);
}
if (memo) {
params.push(['memo', memo]);
url.searchParams.append('memo', memo);
}
return params.map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&');
return url;
}
//# sourceMappingURL=encodeURL.js.map
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {

@@ -15,7 +19,9 @@ if (k2 === undefined) k2 = k;

__exportStar(require("./createQR"), exports);
__exportStar(require("./createTransaction"), exports);
__exportStar(require("./createTransfer"), exports);
__exportStar(require("./encodeURL"), exports);
__exportStar(require("./findTransactionSignature"), exports);
__exportStar(require("./fetchTransaction"), exports);
__exportStar(require("./findReference"), exports);
__exportStar(require("./parseURL"), exports);
__exportStar(require("./validateTransactionSignature"), exports);
__exportStar(require("./types"), exports);
__exportStar(require("./validateTransfer"), exports);
//# sourceMappingURL=index.js.map

@@ -21,16 +21,34 @@ "use strict";

/**
* Parse the components of a Solana Pay URL.
* Parse a Solana Pay URL.
*
* **Reference** implementation for wallet providers.
* @param url - URL to parse.
*
* @param url - A Solana Pay URL
* @throws {ParseURLError}
*/
function parseURL(url) {
if (url.length > 2048)
throw new ParseURLError('length invalid');
const { protocol, pathname, searchParams } = new URL(url);
if (protocol !== constants_1.URL_PROTOCOL)
if (typeof url === 'string') {
if (url.length > 2048)
throw new ParseURLError('length invalid');
url = new URL(url);
}
if (url.protocol !== constants_1.SOLANA_PROTOCOL)
throw new ParseURLError('protocol invalid');
if (!pathname)
throw new ParseURLError('recipient missing');
if (!url.pathname)
throw new ParseURLError('pathname missing');
return /[:%]/.test(url.pathname) ? parseTransactionRequestURL(url) : parseTransferRequestURL(url);
}
exports.parseURL = parseURL;
function parseTransactionRequestURL({ pathname, searchParams }) {
const link = new URL(decodeURIComponent(pathname));
if (link.protocol !== constants_1.HTTPS_PROTOCOL)
throw new ParseURLError('link invalid');
const label = searchParams.get('label') || undefined;
const message = searchParams.get('message') || undefined;
return {
link,
label,
message,
};
}
function parseTransferRequestURL({ pathname, searchParams }) {
let recipient;

@@ -61,10 +79,10 @@ try {

catch (error) {
throw new ParseURLError('token invalid');
throw new ParseURLError('spl-token invalid');
}
}
let reference;
const referenceParam = searchParams.getAll('reference');
if (referenceParam.length) {
const referenceParams = searchParams.getAll('reference');
if (referenceParams.length) {
try {
reference = referenceParam.map((reference) => new web3_js_1.PublicKey(reference));
reference = referenceParams.map((reference) => new web3_js_1.PublicKey(reference));
}

@@ -88,3 +106,2 @@ catch (error) {

}
exports.parseURL = parseURL;
//# sourceMappingURL=parseURL.js.map

@@ -5,9 +5,9 @@ import QRCodeStyling, { Options } from '@solana/qr-code-styling';

*
* @param url - The URL to encode in the QR code.
* @param size - Size of canvas in `px`.
* @param background - Background color for QR code.
* @param color - Color for QR code pattern.
* @param url - The URL to encode.
* @param size - Width and height in pixels.
* @param background - Background color, which should be light for device compatibility.
* @param color - Foreground color, which should be dark for device compatibility.
*/
export declare function createQR(url: string, size?: number, background?: string, color?: string): QRCodeStyling;
export declare function createQR(url: string | URL, size?: number, background?: string, color?: string): QRCodeStyling;
/** @ignore */
export declare function createQROptions(url: string, size?: number, background?: string, color?: string): Options;
export declare function createQROptions(url: string | URL, size?: number, background?: string, color?: string): Options;

@@ -1,40 +0,37 @@

import { PublicKey } from '@solana/web3.js';
import BigNumber from 'bignumber.js';
import { Amount, Label, Memo, Message, Recipient, References, SPLToken } from './types';
/**
* Optional query parameters to encode in a Solana Pay URL.
* Fields of a Solana Pay transaction request URL.
*/
export interface EncodeURLParams {
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount) */
amount?: BigNumber;
/** `splToken` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token) */
splToken?: PublicKey;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference) */
reference?: PublicKey | PublicKey[];
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label) */
label?: string;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message) */
message?: string;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo) */
memo?: string;
export interface TransactionRequestURLFields {
/** `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#link). */
link: URL;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label-1). */
label?: Label;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message-1). */
message?: Message;
}
/**
* Required and optional URL components to encode in a Solana Pay URL.
* Fields of a Solana Pay transfer request URL.
*/
export interface EncodeURLComponents extends EncodeURLParams {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient) */
recipient: PublicKey;
export interface TransferRequestURLFields {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient). */
recipient: Recipient;
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount). */
amount?: Amount;
/** `spl-token` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token). */
splToken?: SPLToken;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference). */
reference?: References;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label). */
label?: Label;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message). */
message?: Message;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo). */
memo?: Memo;
}
/**
* Encode a Solana Pay URL from required and optional components.
* Encode a Solana Pay URL.
*
* @param {EncodeURLComponents} components
*
* @param components.recipient
* @param components.amount
* @param components.splToken
* @param components.reference
* @param components.label
* @param components.message
* @param components.memo
* @param fields Fields to encode in the URL.
*/
export declare function encodeURL({ recipient, ...params }: EncodeURLComponents): string;
export declare function encodeURL(fields: TransactionRequestURLFields | TransferRequestURLFields): URL;
export * from './constants';
export * from './createQR';
export * from './createTransaction';
export * from './createTransfer';
export * from './encodeURL';
export * from './findTransactionSignature';
export * from './fetchTransaction';
export * from './findReference';
export * from './parseURL';
export * from './validateTransactionSignature';
export * from './types';
export * from './validateTransfer';

@@ -1,4 +0,33 @@

import { PublicKey } from '@solana/web3.js';
import BigNumber from 'bignumber.js';
import { Amount, Label, Link, Memo, Message, Recipient, Reference, SPLToken } from './types';
/**
* A Solana Pay transaction request URL.
*/
export interface TransactionRequestURL {
/** `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#link). */
link: Link;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label-1). */
label: Label | undefined;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message-1). */
message: Message | undefined;
}
/**
* A Solana Pay transfer request URL.
*/
export interface TransferRequestURL {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient). */
recipient: Recipient;
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount). */
amount: Amount | undefined;
/** `spl-token` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token). */
splToken: SPLToken | undefined;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference). */
reference: Reference[] | undefined;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label). */
label: Label | undefined;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message). */
message: Message | undefined;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo). */
memo: Memo | undefined;
}
/**
* Thrown when a URL can't be parsed as a Solana Pay URL.

@@ -10,27 +39,8 @@ */

/**
* Parsed components of a Solana Pay URL.
*/
export interface ParsedURL {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient) */
recipient: PublicKey;
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount) */
amount: BigNumber | undefined;
/** `splToken` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token) */
splToken: PublicKey | undefined;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference) */
reference: PublicKey[] | undefined;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label) */
label: string | undefined;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message) */
message: string | undefined;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo) */
memo: string | undefined;
}
/**
* Parse the components of a Solana Pay URL.
* Parse a Solana Pay URL.
*
* **Reference** implementation for wallet providers.
* @param url - URL to parse.
*
* @param url - A Solana Pay URL
* @throws {ParseURLError}
*/
export declare function parseURL(url: string): ParsedURL;
export declare function parseURL(url: string | URL): TransactionRequestURL | TransferRequestURL;
{
"name": "@solana/pay",
"version": "0.1.3",
"version": "0.2.0",
"author": "Solana Maintainers <maintainers@solana.foundation>",

@@ -40,5 +40,8 @@ "repository": "https://github.com/solana-labs/solana-pay",

"@solana/qr-code-styling": "^1.6.0-beta.0",
"@solana/spl-token": "^0.2.0-alpha.1",
"@solana/web3.js": "^1.31.0",
"bignumber.js": "^9.0.2"
"@solana/spl-token": "^0.2.0",
"@solana/web3.js": "^1.36.0",
"bignumber.js": "^9.0.2",
"cross-fetch": "^3.1.5",
"js-base64": "^3.7.2",
"tweetnacl": "^1.0.3"
},

@@ -51,4 +54,4 @@ "devDependencies": {

"@types/prettier": "^2.4.2",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"eslint": "^8.6.0",

@@ -62,5 +65,6 @@ "eslint-config-prettier": "^8.3.0",

"ts-jest": "^27.1.2",
"ts-node": "^10.4.0",
"ts-node": "^10.7.0",
"tsc-esm": "^1.0.4",
"tslib": "^2.3.1",
"typedoc": "^0.22.10",
"typedoc": "^0.22.13",
"typescript": "^4.5.4",

@@ -67,0 +71,0 @@ "typescript-esm": "^2.0.0"

@@ -5,5 +5,8 @@ import { PublicKey } from '@solana/web3.js';

/** @internal */
export const URL_PROTOCOL = 'solana:';
export const SOLANA_PROTOCOL = 'solana:';
/** @internal */
export const HTTPS_PROTOCOL = 'https:';
/** @internal */
export const MEMO_PROGRAM_ID = new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr');

@@ -10,0 +13,0 @@

@@ -15,8 +15,8 @@ import QRCodeStyling, {

*
* @param url - The URL to encode in the QR code.
* @param size - Size of canvas in `px`.
* @param background - Background color for QR code.
* @param color - Color for QR code pattern.
* @param url - The URL to encode.
* @param size - Width and height in pixels.
* @param background - Background color, which should be light for device compatibility.
* @param color - Foreground color, which should be dark for device compatibility.
*/
export function createQR(url: string, size = 512, background = 'white', color = 'black'): QRCodeStyling {
export function createQR(url: string | URL, size = 512, background = 'white', color = 'black'): QRCodeStyling {
return new QRCodeStyling(createQROptions(url, size, background, color));

@@ -26,3 +26,3 @@ }

/** @ignore */
export function createQROptions(url: string, size = 512, background = 'white', color = 'black'): Options {
export function createQROptions(url: string | URL, size = 512, background = 'white', color = 'black'): Options {
return {

@@ -32,3 +32,3 @@ type: 'svg' as DrawType,

height: size,
data: url,
data: String(url),
margin: 16,

@@ -35,0 +35,0 @@ qrOptions: {

@@ -1,64 +0,81 @@

import { PublicKey } from '@solana/web3.js';
import BigNumber from 'bignumber.js';
import { URL_PROTOCOL } from './constants';
import { SOLANA_PROTOCOL } from './constants';
import { Amount, Label, Memo, Message, Recipient, References, SPLToken } from './types';
/**
* Optional query parameters to encode in a Solana Pay URL.
* Fields of a Solana Pay transaction request URL.
*/
export interface EncodeURLParams {
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount) */
amount?: BigNumber;
/** `splToken` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token) */
splToken?: PublicKey;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference) */
reference?: PublicKey | PublicKey[];
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label) */
label?: string;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message) */
message?: string;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo) */
memo?: string;
export interface TransactionRequestURLFields {
/** `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#link). */
link: URL;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label-1). */
label?: Label;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message-1). */
message?: Message;
}
/**
* Required and optional URL components to encode in a Solana Pay URL.
* Fields of a Solana Pay transfer request URL.
*/
export interface EncodeURLComponents extends EncodeURLParams {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient) */
recipient: PublicKey;
export interface TransferRequestURLFields {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient). */
recipient: Recipient;
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount). */
amount?: Amount;
/** `spl-token` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token). */
splToken?: SPLToken;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference). */
reference?: References;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label). */
label?: Label;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message). */
message?: Message;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo). */
memo?: Memo;
}
/**
* Encode a Solana Pay URL from required and optional components.
* Encode a Solana Pay URL.
*
* @param {EncodeURLComponents} components
*
* @param components.recipient
* @param components.amount
* @param components.splToken
* @param components.reference
* @param components.label
* @param components.message
* @param components.memo
* @param fields Fields to encode in the URL.
*/
export function encodeURL({ recipient, ...params }: EncodeURLComponents): string {
let url = URL_PROTOCOL + encodeURIComponent(recipient.toBase58());
export function encodeURL(fields: TransactionRequestURLFields | TransferRequestURLFields): URL {
return 'link' in fields ? encodeTransactionRequestURL(fields) : encodeTransferRequestURL(fields);
}
const encodedParams = encodeURLParams(params);
if (encodedParams) {
url += '?' + encodedParams;
function encodeTransactionRequestURL({ link, label, message }: TransactionRequestURLFields): URL {
// Remove trailing slashes
const pathname = link.search
? encodeURIComponent(String(link).replace(/\/\?/, '?'))
: String(link).replace(/\/$/, '');
const url = new URL(SOLANA_PROTOCOL + pathname);
if (label) {
url.searchParams.append('label', label);
}
if (message) {
url.searchParams.append('message', message);
}
return url;
}
function encodeURLParams({ amount, splToken, reference, label, message, memo }: EncodeURLParams): string {
const params: [string, string][] = [];
function encodeTransferRequestURL({
recipient,
amount,
splToken,
reference,
label,
message,
memo,
}: TransferRequestURLFields): URL {
const pathname = recipient.toBase58();
const url = new URL(SOLANA_PROTOCOL + pathname);
if (amount) {
params.push(['amount', amount.toFixed(amount.decimalPlaces())]);
url.searchParams.append('amount', amount.toFixed(amount.decimalPlaces()));
}
if (splToken) {
params.push(['spl-token', splToken.toBase58()]);
url.searchParams.append('spl-token', splToken.toBase58());
}

@@ -72,3 +89,3 @@

for (const pubkey of reference) {
params.push(['reference', pubkey.toBase58()]);
url.searchParams.append('reference', pubkey.toBase58());
}

@@ -78,14 +95,14 @@ }

if (label) {
params.push(['label', label]);
url.searchParams.append('label', label);
}
if (message) {
params.push(['message', message]);
url.searchParams.append('message', message);
}
if (memo) {
params.push(['memo', memo]);
url.searchParams.append('memo', memo);
}
return params.map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&');
return url;
}
export * from './constants';
export * from './createQR';
export * from './createTransaction';
export * from './createTransfer';
export * from './encodeURL';
export * from './findTransactionSignature';
export * from './fetchTransaction';
export * from './findReference';
export * from './parseURL';
export * from './validateTransactionSignature';
export * from './types';
export * from './validateTransfer';
import { PublicKey } from '@solana/web3.js';
import BigNumber from 'bignumber.js';
import { URL_PROTOCOL } from './constants';
import { HTTPS_PROTOCOL, SOLANA_PROTOCOL } from './constants';
import { Amount, Label, Link, Memo, Message, Recipient, Reference, SPLToken } from './types';
/**
* Thrown when a URL can't be parsed as a Solana Pay URL.
* A Solana Pay transaction request URL.
*/
export class ParseURLError extends Error {
name = 'ParseURLError';
export interface TransactionRequestURL {
/** `link` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#link). */
link: Link;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label-1). */
label: Label | undefined;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message-1). */
message: Message | undefined;
}
/**
* Parsed components of a Solana Pay URL.
* A Solana Pay transfer request URL.
*/
export interface ParsedURL {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient) */
recipient: PublicKey;
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount) */
amount: BigNumber | undefined;
/** `splToken` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token) */
splToken: PublicKey | undefined;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference) */
reference: PublicKey[] | undefined;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label) */
label: string | undefined;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message) */
message: string | undefined;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo) */
memo: string | undefined;
export interface TransferRequestURL {
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient). */
recipient: Recipient;
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount). */
amount: Amount | undefined;
/** `spl-token` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token). */
splToken: SPLToken | undefined;
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference). */
reference: Reference[] | undefined;
/** `label` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#label). */
label: Label | undefined;
/** `message` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#message). */
message: Message | undefined;
/** `memo` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#memo). */
memo: Memo | undefined;
}
/**
* Parse the components of a Solana Pay URL.
* Thrown when a URL can't be parsed as a Solana Pay URL.
*/
export class ParseURLError extends Error {
name = 'ParseURLError';
}
/**
* Parse a Solana Pay URL.
*
* **Reference** implementation for wallet providers.
* @param url - URL to parse.
*
* @param url - A Solana Pay URL
* @throws {ParseURLError}
*/
export function parseURL(url: string): ParsedURL {
if (url.length > 2048) throw new ParseURLError('length invalid');
export function parseURL(url: string | URL): TransactionRequestURL | TransferRequestURL {
if (typeof url === 'string') {
if (url.length > 2048) throw new ParseURLError('length invalid');
url = new URL(url);
}
const { protocol, pathname, searchParams } = new URL(url);
if (protocol !== URL_PROTOCOL) throw new ParseURLError('protocol invalid');
if (!pathname) throw new ParseURLError('recipient missing');
if (url.protocol !== SOLANA_PROTOCOL) throw new ParseURLError('protocol invalid');
if (!url.pathname) throw new ParseURLError('pathname missing');
return /[:%]/.test(url.pathname) ? parseTransactionRequestURL(url) : parseTransferRequestURL(url);
}
function parseTransactionRequestURL({ pathname, searchParams }: URL): TransactionRequestURL {
const link = new URL(decodeURIComponent(pathname));
if (link.protocol !== HTTPS_PROTOCOL) throw new ParseURLError('link invalid');
const label = searchParams.get('label') || undefined;
const message = searchParams.get('message') || undefined;
return {
link,
label,
message,
};
}
function parseTransferRequestURL({ pathname, searchParams }: URL): TransferRequestURL {
let recipient: PublicKey;
try {
recipient = new PublicKey(pathname);
} catch (error) {
} catch (error: any) {
throw new ParseURLError('recipient invalid');

@@ -69,3 +102,3 @@ }

} catch (error) {
throw new ParseURLError('token invalid');
throw new ParseURLError('spl-token invalid');
}

@@ -75,6 +108,6 @@ }

let reference: PublicKey[] | undefined;
const referenceParam = searchParams.getAll('reference');
if (referenceParam.length) {
const referenceParams = searchParams.getAll('reference');
if (referenceParams.length) {
try {
reference = referenceParam.map((reference) => new PublicKey(reference));
reference = referenceParams.map((reference) => new PublicKey(reference));
} catch (error) {

@@ -81,0 +114,0 @@ throw new ParseURLError('reference invalid');

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc