@tiplink/api
Version:
Api for creating and sending TipLinks
146 lines (145 loc) • 8.76 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReceiverEmail = exports.createReceiverTipLink = exports.mailEscrow = exports.mail = exports.getRecordedEscrowActionsFromTx = exports.getRecordedEscrowActionsFromVault = exports.deserializeRecordedEscrowActions = exports.serializeRecordedEscrowActions = exports.EscrowActionType = exports.getAllRecordedEscrowActions = exports.parseEscrowTx = exports.parseEscrowIx = exports.ESCROW_PROGRAM_ID = exports.PRIO_FEES_LAMPORTS = exports.getEscrowReceiverTipLink = exports.EscrowTipLink = exports.attachTheme = exports.TipLinkClient = exports.TipLink = exports.TIPLINK_ORIGIN = void 0;
const web3_js_1 = require("@solana/web3.js");
const libsodium_wrappers_sumo_1 = __importDefault(require("libsodium-wrappers-sumo"));
const bs58_1 = require("bs58");
const DEFAULT_TIPLINK_KEYLENGTH = 12;
const DEFAULT_HASHLESS_TIPLINK_KEYLENGTH = 16; // 16 bytes = 128 bits
const DEFAULT_ORIGIN = "https://tiplink.io";
exports.TIPLINK_ORIGIN = process !== undefined && process.env !== undefined
? (_a = process.env.TIPLINK_ORIGIN_OVERRIDE) !== null && _a !== void 0 ? _a : DEFAULT_ORIGIN
: DEFAULT_ORIGIN;
const TIPLINK_PATH = "/i";
const VERSION_DELIMITER = "_";
const VALID_VERSIONS = new Set([0, 1]);
const getSodium = () => __awaiter(void 0, void 0, void 0, function* () {
yield libsodium_wrappers_sumo_1.default.ready;
return libsodium_wrappers_sumo_1.default;
});
const kdf = (fullLength, pwShort, salt) => __awaiter(void 0, void 0, void 0, function* () {
const sodium = yield getSodium();
return sodium.crypto_pwhash(fullLength, pwShort, salt, sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE, sodium.crypto_pwhash_ALG_DEFAULT);
});
const randBuf = (l) => __awaiter(void 0, void 0, void 0, function* () {
const sodium = yield getSodium();
return sodium.randombytes_buf(l);
});
const kdfz = (fullLength, pwShort) => __awaiter(void 0, void 0, void 0, function* () {
const sodium = yield getSodium();
const salt = new Uint8Array(sodium.crypto_pwhash_SALTBYTES);
return yield kdf(fullLength, pwShort, salt);
});
const pwToKeypair = (pw) => __awaiter(void 0, void 0, void 0, function* () {
const sodium = yield getSodium();
const seed = yield kdfz(sodium.crypto_sign_SEEDBYTES, pw);
return web3_js_1.Keypair.fromSeed(seed);
});
const pwToKeypairV1 = (pw) => __awaiter(void 0, void 0, void 0, function* () {
const sodium = yield getSodium();
const seed = sodium.pad(pw, sodium.crypto_sign_SEEDBYTES);
return web3_js_1.Keypair.fromSeed(seed);
});
class TipLink {
constructor(url, keypair) {
this.url = url;
this.keypair = keypair;
}
static create(version = 0) {
return __awaiter(this, void 0, void 0, function* () {
if (!VALID_VERSIONS.has(version)) {
throw Error("invalid version");
}
yield getSodium();
if (version === 1) {
const b = yield randBuf(DEFAULT_HASHLESS_TIPLINK_KEYLENGTH);
const keypair = yield pwToKeypairV1(b);
const hash = (0, bs58_1.encode)(b);
const urlString = `${exports.TIPLINK_ORIGIN}${TIPLINK_PATH}#${VERSION_DELIMITER}${hash}`;
// can't assign hash as it causes an error in React Native
const link = new URL(urlString);
const tiplink = new TipLink(link, keypair);
return tiplink;
}
else {
// version === 0
const b = yield randBuf(DEFAULT_TIPLINK_KEYLENGTH);
const keypair = yield pwToKeypair(b);
const hash = (0, bs58_1.encode)(b);
const urlString = `${exports.TIPLINK_ORIGIN}${TIPLINK_PATH}#${hash}`;
// can't assign hash as it causes an error in React Native
const link = new URL(urlString);
const tiplink = new TipLink(link, keypair);
return tiplink;
}
});
}
static fromUrl(url) {
return __awaiter(this, void 0, void 0, function* () {
let slug = url.hash.slice(1);
let version = 0;
if (slug.includes(VERSION_DELIMITER)) {
const versionString = slug.split(VERSION_DELIMITER, 1)[0];
if (versionString.length === 0) {
version = 1;
// } else {
// version = Number(versionString);
}
slug = slug.split(VERSION_DELIMITER).slice(1).join(VERSION_DELIMITER);
}
const pw = Uint8Array.from((0, bs58_1.decode)(slug));
if (version === 1) {
const keypair = yield pwToKeypairV1(pw);
const tiplink = new TipLink(url, keypair);
return tiplink;
}
else {
const keypair = yield pwToKeypair(pw);
const tiplink = new TipLink(url, keypair);
return tiplink;
}
});
}
static fromLink(link) {
return __awaiter(this, void 0, void 0, function* () {
const url = new URL(link);
return this.fromUrl(url);
});
}
}
exports.TipLink = TipLink;
const client_1 = require("./client");
Object.defineProperty(exports, "TipLinkClient", { enumerable: true, get: function () { return client_1.TipLinkClient; } });
const themes_1 = require("./lib/themes");
Object.defineProperty(exports, "attachTheme", { enumerable: true, get: function () { return themes_1.attachTheme; } });
const escrow_1 = require("./escrow");
Object.defineProperty(exports, "EscrowTipLink", { enumerable: true, get: function () { return escrow_1.EscrowTipLink; } });
Object.defineProperty(exports, "getEscrowReceiverTipLink", { enumerable: true, get: function () { return escrow_1.getEscrowReceiverTipLink; } });
Object.defineProperty(exports, "PRIO_FEES_LAMPORTS", { enumerable: true, get: function () { return escrow_1.PRIO_FEES_LAMPORTS; } });
Object.defineProperty(exports, "ESCROW_PROGRAM_ID", { enumerable: true, get: function () { return escrow_1.ESCROW_PROGRAM_ID; } });
Object.defineProperty(exports, "parseEscrowIx", { enumerable: true, get: function () { return escrow_1.parseEscrowIx; } });
Object.defineProperty(exports, "parseEscrowTx", { enumerable: true, get: function () { return escrow_1.parseEscrowTx; } });
Object.defineProperty(exports, "getAllRecordedEscrowActions", { enumerable: true, get: function () { return escrow_1.getAllRecordedEscrowActions; } });
Object.defineProperty(exports, "EscrowActionType", { enumerable: true, get: function () { return escrow_1.EscrowActionType; } });
Object.defineProperty(exports, "serializeRecordedEscrowActions", { enumerable: true, get: function () { return escrow_1.serializeRecordedEscrowActions; } });
Object.defineProperty(exports, "deserializeRecordedEscrowActions", { enumerable: true, get: function () { return escrow_1.deserializeRecordedEscrowActions; } });
Object.defineProperty(exports, "getRecordedEscrowActionsFromVault", { enumerable: true, get: function () { return escrow_1.getRecordedEscrowActionsFromVault; } });
Object.defineProperty(exports, "getRecordedEscrowActionsFromTx", { enumerable: true, get: function () { return escrow_1.getRecordedEscrowActionsFromTx; } });
const enclave_1 = require("./enclave");
Object.defineProperty(exports, "mail", { enumerable: true, get: function () { return enclave_1.mail; } });
Object.defineProperty(exports, "mailEscrow", { enumerable: true, get: function () { return enclave_1.mailEscrow; } });
Object.defineProperty(exports, "createReceiverTipLink", { enumerable: true, get: function () { return enclave_1.createReceiverTipLink; } });
Object.defineProperty(exports, "getReceiverEmail", { enumerable: true, get: function () { return enclave_1.getReceiverEmail; } });
;