@ton.js/core
Version:
TonWeb - JavaScript API for TON blockchain
101 lines • 3.85 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatTransferUrl = exports.parseTransferUrl = void 0;
const bn_js_1 = __importDefault(require("bn.js"));
const address_1 = require("./address");
// @todo: should we export this?
const tonTransferPrefix = 'ton://transfer/';
/**
* Parses the specified TON-transfer URL into its individual
* parts, throws error if URL format is invalid.
*/
function parseTransferUrl(url) {
// Examples:
// ton://transfer/EQA0i8-CdGnF_DhUHHf92R1ONH6sIA9vLZ_WLcCIhfBBXwtG
// ton://transfer/EQA0i8-CdGnF_DhUHHf92R1ONH6sIA9vLZ_WLcCIhfBBXwtG?amount=1000000000
// ton://transfer/EQA0i8-CdGnF_DhUHHf92R1ONH6sIA9vLZ_WLcCIhfBBXwtG?amount=1000000000&text=data
// ton://transfer/EQA0i8-CdGnF_DhUHHf92R1ONH6sIA9vLZ_WLcCIhfBBXwtG?amount=1000000000&text=foo%3A%2F%2Fbar%2C%2Fbaz%3Famount%3D1%26text%3D%D1%80%D1%83
// -----
if (!url.startsWith(tonTransferPrefix)) {
throw new Error(`TON transfer URL must start with ${tonTransferPrefix}`);
}
const parts = url.substring(tonTransferPrefix.length).split('?');
if (parts.length > 2) {
throw new Error(`TON transfer URL could contain only one "?" character`);
}
const [address, rest] = parts;
if (!address_1.Address.isValid(address)) {
throw new Error(`TON transfer URL contains an incorrect address: ${address}`);
}
const result = {
address,
};
// @todo: use the `URLSearchParams` to parse the query string!
if (!rest) {
return result;
}
const pairs = (rest.split('&')
.map(parts => parts.split('=')));
const processedKeys = new Set();
for (const pair of pairs) {
if (pair.length !== 2) {
throw new Error(`TON transfer URL contains invalid query string parameter`);
}
const [key, value] = pair;
if (processedKeys.has(key)) {
throw new Error(`Duplicate query string parameters (${key}) are ` +
`not supported in TON transfer URL`);
}
switch (key) {
case 'amount':
try {
const bn = new bn_js_1.default(value);
if (bn.isNeg()) {
// noinspection ExceptionCaughtLocallyJS
throw new Error();
}
}
catch (error) {
throw new Error(`Incorrect amount specified in the ` +
`TON transfer URL. The amount should ` +
`be a positive nanograms value`);
}
result.amount = value;
break;
case 'text':
result.text = decodeURIComponent(value);
break;
default:
throw new Error(`Unrecognized TON transfer URL query ` +
`string parameter: ${key}`);
}
processedKeys.add(key);
}
return result;
}
exports.parseTransferUrl = parseTransferUrl;
/**
* Formats TON transfer URL from the specified individual parts.
*
* @todo: pass all the parts as a single argument of `ParsedTransferUrl` type
*/
function formatTransferUrl(address, amount, text) {
let url = tonTransferPrefix + address;
// @todo: use the `URLSearchParams` to build the query string!
const params = [];
if (amount) {
params.push(`amount=${amount}`);
}
if (text) {
params.push(`text=${encodeURIComponent(text)}`);
}
if (params.length === 0) {
return url;
}
return url + '?' + params.join('&');
}
exports.formatTransferUrl = formatTransferUrl;
//# sourceMappingURL=transfer-url.js.map