Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alby-js-sdk

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alby-js-sdk - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

dist/index.cjs

216

dist/index.d.ts

@@ -1,5 +0,211 @@

export * as auth from "./auth";
export * as types from './types';
export { Client } from "./client";
export { WebLNProvider } from "./WeblnProvider";
export { Client as default } from "./client";
declare type SuccessStatus = 200 | 201;
declare type ResponseType = "application/json";
interface AuthHeader {
Authorization: string;
}
interface GetTokenResponse {
/** Allows an application to obtain a new access token without prompting the user via the refresh token flow. */
refresh_token?: string;
/** Access tokens are the token that applications use to make API requests on behalf of a user. */
access_token?: string;
token_type?: string;
expires_in?: number;
/** Comma-separated list of scopes for the token */
scope?: string;
}
interface Token extends Omit<GetTokenResponse, "expires_in"> {
/** Date that the access_token will expire at. */
expires_at?: number;
}
declare type GenerateAuthUrlOptions = {
/** A random string you provide to verify against CSRF attacks. The length of this string can be up to 500 characters. */
state?: string;
/** Specifies the method you are using to make a request (S256 OR plain). */
code_challenge_method: "S256";
} | {
/** A random string you provide to verify against CSRF attacks. The length of this string can be up to 500 characters. */
state: string;
/** A PKCE parameter, a random secret for each request you make. */
code_challenge: string;
/** Specifies the method you are using to make a request (S256 OR plain). */
code_challenge_method?: "plain";
};
declare abstract class OAuthClient implements AuthClient {
abstract token?: Token;
abstract generateAuthURL(options: GenerateAuthUrlOptions): string;
abstract requestAccessToken(code?: string): Promise<{
token: Token;
}>;
abstract getAuthHeader(url?: string, method?: string): Promise<AuthHeader> | AuthHeader;
}
declare abstract class AuthClient {
abstract getAuthHeader(url?: string, method?: string): Promise<AuthHeader> | AuthHeader;
}
declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
declare type GetSuccess<T> = {
[K in SuccessStatus & keyof T]: GetContent<T[K]>;
}[SuccessStatus & keyof T];
declare type AlbyResponse<T> = UnionToIntersection<ExtractAlbyResponse<T>>;
declare type GetContent<T> = "content" extends keyof T ? ResponseType extends keyof T["content"] ? T["content"][ResponseType] : never : never;
declare type ExtractAlbyResponse<T> = "responses" extends keyof T ? GetSuccess<T["responses"]> : never;
declare type InvoiceRequestParams = {
description?: string;
description_hash?: string;
amount: number;
};
declare type KeysendRequestParams = {
amount: number;
destination: string;
memo?: string;
customRecords?: Record<string, string>;
};
declare type SendPaymentRequestParams = {
invoice: string;
amount?: number;
};
type types_SuccessStatus = SuccessStatus;
type types_ResponseType = ResponseType;
type types_AuthHeader = AuthHeader;
type types_GetTokenResponse = GetTokenResponse;
type types_Token = Token;
type types_GenerateAuthUrlOptions = GenerateAuthUrlOptions;
type types_OAuthClient = OAuthClient;
declare const types_OAuthClient: typeof OAuthClient;
type types_AuthClient = AuthClient;
declare const types_AuthClient: typeof AuthClient;
type types_UnionToIntersection<U> = UnionToIntersection<U>;
type types_GetSuccess<T> = GetSuccess<T>;
type types_AlbyResponse<T> = AlbyResponse<T>;
type types_GetContent<T> = GetContent<T>;
type types_ExtractAlbyResponse<T> = ExtractAlbyResponse<T>;
type types_InvoiceRequestParams = InvoiceRequestParams;
type types_KeysendRequestParams = KeysendRequestParams;
type types_SendPaymentRequestParams = SendPaymentRequestParams;
declare namespace types {
export {
types_SuccessStatus as SuccessStatus,
types_ResponseType as ResponseType,
types_AuthHeader as AuthHeader,
types_GetTokenResponse as GetTokenResponse,
types_Token as Token,
types_GenerateAuthUrlOptions as GenerateAuthUrlOptions,
types_OAuthClient as OAuthClient,
types_AuthClient as AuthClient,
types_UnionToIntersection as UnionToIntersection,
types_GetSuccess as GetSuccess,
types_AlbyResponse as AlbyResponse,
types_GetContent as GetContent,
types_ExtractAlbyResponse as ExtractAlbyResponse,
types_InvoiceRequestParams as InvoiceRequestParams,
types_KeysendRequestParams as KeysendRequestParams,
types_SendPaymentRequestParams as SendPaymentRequestParams,
};
}
interface RequestOptions extends Omit<RequestInit, "body"> {
auth?: AuthClient;
endpoint: string;
params?: Record<string, any>;
request_body?: Record<string, any>;
method?: string;
max_retries?: number;
base_url?: string;
}
declare type OAuth2Scopes = "account:read" | "invoices:create" | "invoices:read" | "transactions:read" | "balance:read" | "payments:send";
interface OAuth2UserOptions {
client_id: string;
client_secret?: string;
callback: string;
scopes: OAuth2Scopes[];
request_options?: Partial<RequestOptions>;
token?: Token;
}
declare class OAuth2User implements OAuthClient {
token?: Token;
options: OAuth2UserOptions;
code_verifier?: string;
code_challenge?: string;
constructor(options: OAuth2UserOptions);
/**
* Refresh the access token
*/
refreshAccessToken(): Promise<{
token: Token;
}>;
/**
* Check if an access token is expired
*/
isAccessTokenExpired(): boolean;
/**
* Request an access token
*/
requestAccessToken(code?: string): Promise<{
token: Token;
}>;
generateAuthURL(options: GenerateAuthUrlOptions): string;
getAuthHeader(): Promise<AuthHeader>;
}
declare class OAuth2Bearer implements AuthClient {
private bearer_token;
constructor(bearer_token: string);
getAuthHeader(): AuthHeader;
}
type auth_OAuth2Scopes = OAuth2Scopes;
type auth_OAuth2UserOptions = OAuth2UserOptions;
type auth_OAuth2User = OAuth2User;
declare const auth_OAuth2User: typeof OAuth2User;
type auth_OAuth2Bearer = OAuth2Bearer;
declare const auth_OAuth2Bearer: typeof OAuth2Bearer;
declare namespace auth {
export {
auth_OAuth2Scopes as OAuth2Scopes,
auth_OAuth2UserOptions as OAuth2UserOptions,
auth_OAuth2User as OAuth2User,
auth_OAuth2Bearer as OAuth2Bearer,
};
}
declare class Client {
auth: AuthClient;
defaultRequestOptions?: Partial<RequestOptions>;
constructor(auth: string | AuthClient, requestOptions?: Partial<RequestOptions>);
accountBalance(params: {}, request_options?: Partial<RequestOptions>): Promise<any>;
accountSummary(params: {}, request_options?: Partial<RequestOptions>): Promise<any>;
accountValue4Value(params: {}, request_options?: Partial<RequestOptions>): Promise<any>;
incomingInvoices(params: {}, request_options?: Partial<RequestOptions>): Promise<any>;
outgoingInvoices(params: {}, request_options?: Partial<RequestOptions>): Promise<any>;
getInvoice(paymentHash: string, request_options?: Partial<RequestOptions>): Promise<any>;
createInvoice(invoice: InvoiceRequestParams, request_options?: Partial<RequestOptions>): Promise<any>;
keysend(keysend: KeysendRequestParams, request_options?: Partial<RequestOptions>): Promise<any>;
sendPayment(params: SendPaymentRequestParams, request_options?: Partial<RequestOptions>): Promise<any>;
}
interface RequestInvoiceArgs {
amount: string | number;
defaultMemo?: string;
}
declare class WebLNProvider {
client: Client;
auth: OAuthClient;
constructor(auth: OAuthClient);
openAuthorization(): Promise<unknown>;
enable(): Promise<unknown>;
sendPayment(invoice: string): Promise<{
preimage: any;
}>;
keysend(params: KeysendRequestParams): Promise<{
preimage: any;
}>;
getInfo(): Promise<{
alias: string;
}>;
makeInvoice(params: RequestInvoiceArgs): Promise<{
paymentRequest: any;
}>;
}
export { Client, WebLNProvider, auth, Client as default, types };

@@ -1,34 +0,457 @@

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = 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]; } };
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/auth.ts
var auth_exports = {};
__export(auth_exports, {
OAuth2Bearer: () => OAuth2Bearer,
OAuth2User: () => OAuth2User
});
// src/OAuth2User.ts
import sha256 from "crypto-js/sha256";
import CryptoJS from "crypto-js";
import Base64 from "crypto-js/enc-base64";
// src/utils.ts
function buildQueryString(query) {
return Object.entries(query).map(
([key, value]) => key && value ? `${key}=${value}` : ""
).join("&");
}
function basicAuthHeader(client_id, client_secret) {
return `Basic ${btoa(`${client_id}:${client_secret}`)}`;
}
// src/request.ts
import fetch from "cross-fetch";
var BASE_URL = "https://api.getalby.com";
async function fetchWithRetries(url, init, max_retries = 0) {
const res = await fetch(url, init);
if (res.status === 429 && max_retries > 0) {
const rateLimitReset = Number(res.headers.get("x-rate-limit-reset"));
const rateLimitRemaining = Number(res.headers.get("x-rate-limit-remaining"));
const timeTillReset = rateLimitReset * 1e3 - Date.now();
let timeToWait = 1e3;
if (rateLimitRemaining === 0)
timeToWait = timeTillReset;
await new Promise((resolve) => setTimeout(resolve, timeToWait));
return fetchWithRetries(url, init, max_retries - 1);
}
return res;
}
var AlbyResponseError = class extends Error {
constructor(status, statusText, headers, error) {
super();
this.status = status;
this.statusText = statusText;
this.headers = headers;
this.error = error;
}
};
async function request({
auth,
endpoint,
params: query = {},
request_body,
method,
max_retries,
base_url = BASE_URL,
headers,
...options
}) {
const url = new URL(base_url + endpoint);
url.search = buildQueryString(query);
const isPost = method === "POST" && !!request_body;
const authHeader = auth ? await auth.getAuthHeader(url.href, method) : void 0;
const response = await fetchWithRetries(
url.toString(),
{
headers: {
...isPost ? { "Content-Type": "application/json; charset=utf-8" } : void 0,
...authHeader,
...headers
},
method,
body: isPost ? JSON.stringify(request_body) : void 0,
...options
},
max_retries
);
if (!response.ok) {
const error = await response.json();
throw new AlbyResponseError(
response.status,
response.statusText,
response.headers,
error
);
}
return response;
}
async function rest(args) {
const response = await request(args);
return response.json();
}
// src/OAuth2User.ts
var AUTHORIZE_URL = "https://getalby.com/oauth";
function processTokenResponse(token) {
const { expires_in, ...rest2 } = token;
return {
...rest2,
...!!expires_in && {
expires_at: Date.now() + expires_in * 1e3
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
};
}
var OAuth2User = class {
constructor(options) {
const { token, ...defaultOptions } = options;
this.options = { client_secret: "", ...defaultOptions };
this.token = token;
}
async refreshAccessToken() {
var _a;
const refresh_token = (_a = this.token) == null ? void 0 : _a.refresh_token;
const { client_id, client_secret, request_options } = this.options;
if (!client_id) {
throw new Error("client_id is required");
}
if (!refresh_token) {
throw new Error("refresh_token is required");
}
const data = await rest({
...request_options,
endpoint: `/oauth/token`,
params: {
client_id,
grant_type: "refresh_token",
refresh_token
},
method: "POST",
headers: {
...request_options == null ? void 0 : request_options.headers,
"Content-type": "application/x-www-form-urlencoded",
...{
Authorization: basicAuthHeader(client_id, client_secret)
}
}
});
const token = processTokenResponse(data);
this.token = token;
return { token };
}
isAccessTokenExpired() {
var _a, _b;
const refresh_token = (_a = this.token) == null ? void 0 : _a.refresh_token;
const expires_at = (_b = this.token) == null ? void 0 : _b.expires_at;
if (!expires_at)
return true;
return !!refresh_token && expires_at <= Date.now() + 1e3;
}
async requestAccessToken(code) {
const { client_id, client_secret, callback, request_options } = this.options;
const code_verifier = this.code_verifier;
if (!client_id) {
throw new Error("client_id is required");
}
if (!callback) {
throw new Error("callback is required");
}
const params = {
code,
grant_type: "authorization_code",
code_verifier,
client_id,
redirect_uri: callback
};
const data = await rest({
...request_options,
endpoint: `/oauth/token`,
params,
method: "POST",
headers: {
...request_options == null ? void 0 : request_options.headers,
"Content-Type": "application/x-www-form-urlencoded",
...{
Authorization: basicAuthHeader(client_id, client_secret)
}
}
});
const token = processTokenResponse(data);
this.token = token;
return { token };
}
generateAuthURL(options) {
const { client_id, callback, scopes } = this.options;
if (!callback)
throw new Error("callback required");
if (!scopes)
throw new Error("scopes required");
if (options.code_challenge_method === "S256") {
const code_verifier = CryptoJS.lib.WordArray.random(64);
this.code_verifier = code_verifier.toString();
this.code_challenge = sha256(this.code_verifier).toString(Base64).replace(/\+/g, "-").replace(/\//g, "_").replace(/\=+$/, "");
} else {
this.code_challenge = options.code_challenge;
this.code_verifier = options.code_challenge;
}
const code_challenge = this.code_challenge;
const url = new URL(AUTHORIZE_URL);
url.search = buildQueryString({
...options,
client_id,
scope: scopes.join(" "),
response_type: "code",
redirect_uri: callback,
code_challenge_method: options.code_challenge_method || "plain",
code_challenge
});
return url.toString();
}
async getAuthHeader() {
var _a;
if (!((_a = this.token) == null ? void 0 : _a.access_token))
throw new Error("access_token is required");
if (this.isAccessTokenExpired())
await this.refreshAccessToken();
return {
Authorization: `Bearer ${this.token.access_token}`
};
}
};
// src/OAuth2Bearer.ts
var OAuth2Bearer = class {
constructor(bearer_token) {
this.bearer_token = bearer_token;
}
getAuthHeader() {
return {
Authorization: `Bearer ${this.bearer_token}`
};
}
};
// src/types.ts
var types_exports = {};
__export(types_exports, {
AuthClient: () => AuthClient,
OAuthClient: () => OAuthClient
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
var OAuthClient = class {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = exports.WebLNProvider = exports.Client = exports.types = exports.auth = void 0;
exports.auth = __importStar(require("./auth"));
exports.types = __importStar(require("./types"));
var client_1 = require("./client");
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
var WeblnProvider_1 = require("./WeblnProvider");
Object.defineProperty(exports, "WebLNProvider", { enumerable: true, get: function () { return WeblnProvider_1.WebLNProvider; } });
var client_2 = require("./client");
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return client_2.Client; } });
var AuthClient = class {
};
// src/client.ts
var Client = class {
constructor(auth, requestOptions) {
this.auth = typeof auth === "string" ? new OAuth2Bearer(auth) : auth;
this.defaultRequestOptions = {
...requestOptions,
headers: {
"User-Agent": "alby-api",
...requestOptions == null ? void 0 : requestOptions.headers
}
};
}
accountBalance(params, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/balance`,
params,
method: "GET"
});
}
accountSummary(params, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/user/summary`,
params,
method: "GET"
});
}
accountValue4Value(params, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/user/value4value`,
params,
method: "GET"
});
}
incomingInvoices(params, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/invoices/incoming`,
params,
method: "GET"
});
}
outgoingInvoices(params, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/invoices/outgoing`,
params,
method: "GET"
});
}
getInvoice(paymentHash, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/invoices/${paymentHash}`,
method: "GET"
});
}
createInvoice(invoice, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/invoices`,
request_body: invoice,
method: "POST"
});
}
keysend(keysend, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/payments/keysend`,
request_body: keysend,
method: "POST"
});
}
sendPayment(params, request_options) {
return rest({
auth: this.auth,
...this.defaultRequestOptions,
...request_options,
endpoint: `/payments/bolt11`,
request_body: params,
method: "POST"
});
}
};
// src/WeblnProvider.ts
var isBrowser = () => typeof window !== "undefined" && typeof window.document !== "undefined";
var WebLNProvider = class {
constructor(auth) {
this.auth = auth;
this.client = new Client(auth);
}
openAuthorization() {
const height = 700;
const width = 600;
const top = window.outerHeight / 2 + window.screenY - height / 2;
const left = window.outerWidth / 2 + window.screenX - width / 2;
const url = this.auth.generateAuthURL({ code_challenge_method: "S256" });
return new Promise((resolve, reject) => {
const popup = window.open(
url,
`${document.title} - WebLN enable`,
`height=${height},width=${width},top=${top},left=${left}`
);
window.addEventListener("message", async (message) => {
const data = message.data;
if (data && data.type === "alby:oauth:success" && message.origin === `${document.location.protocol}//${document.location.host}`) {
const code = data.payload.code;
try {
this.auth.requestAccessToken(code);
this.client = new Client(this.auth);
if (popup) {
popup.close();
}
resolve({ enabled: true });
} catch (e) {
console.error(e);
reject({ enabled: false });
}
}
});
});
}
async enable() {
var _a;
if ((_a = this.auth.token) == null ? void 0 : _a.access_token) {
return { enabled: true };
}
if (isBrowser()) {
return this.openAuthorization();
} else {
throw new Error("Missing access token");
}
}
async sendPayment(invoice) {
try {
const result = await this.client.sendPayment({ invoice });
if (result.error) {
throw new Error(result.message);
}
return {
preimage: result.payment_preimage
};
} catch (error) {
let message = "Unknown Error";
if (error instanceof Error)
message = error.message;
throw new Error(message);
}
}
async keysend(params) {
try {
const result = await this.client.keysend(params);
if (result.error) {
throw new Error(result.message);
}
return {
preimage: result.payment_preimage
};
} catch (error) {
let message = "Unknown Error";
if (error instanceof Error)
message = error.message;
throw new Error(message);
}
}
async getInfo() {
return {
alias: "Alby"
};
}
async makeInvoice(params) {
const result = await this.client.createInvoice({
amount: parseInt(params.amount.toString()),
description: params.defaultMemo
});
return {
paymentRequest: result.payment_request
};
}
};
export {
Client,
WebLNProvider,
auth_exports as auth,
Client as default,
types_exports as types
};
//# sourceMappingURL=index.js.map

23

package.json
{
"name": "alby-js-sdk",
"version": "1.0.0",
"version": "1.0.1",
"description": "Alby OAuth2 Client",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"files": [

@@ -11,10 +12,20 @@ "dist"

"scripts": {
"build": "tsc",
"build": "tsup",
"prebuild": "yarn clean",
"generate": "ts-node scripts/generate.ts",
"prepublishOnly": "yarn test",
"build:watch": "tsc --watch",
"test": "echo tests are missing",
"clean": "rm -rf dist"
},
"tsup": {
"entry": [
"src/index.ts"
],
"splitting": false,
"sourcemap": true,
"dts": true,
"format": [
"esm",
"cjs"
]
},
"dependencies": {

@@ -27,3 +38,5 @@ "cross-fetch": "^3.1.5",

"@types/node": "^18.11.0",
"express": "^4.18.2"
"express": "^4.18.2",
"tsup": "^6.2.3",
"typescript": "^4.8.4"
},

@@ -30,0 +43,0 @@ "engines": {

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