@test-org122/hypernet-core
Version:
Hypernet Core. Represents the SDK for running the Hypernet Protocol.
122 lines • 5.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentIdUtils = void 0;
const errors_1 = require("@interfaces/objects/errors");
const types_1 = require("@interfaces/types");
const ethers_1 = require("ethers");
const neverthrow_1 = require("neverthrow");
/**
* An abstract class for creating & converting payment IDs, as well as verifying
* correctness, and extracting information from the ID (such as type, domain, UUID)
*
* A paymentID is a 64-length hexadecimal string:
* characters 0-19: domain (encoded as ascii text --> hex)
* characters 20-32: type (encoded as ascii text --> hex)
* characters 32-63: UUID (encoded as hex)
*/
class PaymentIdUtils {
/**
* Returns an ascii representation of the domain portion of the paymentID string.
* (characters 0-19 of the paymentIdString)
* @param paymentIdString
*/
getDomain(paymentIdString) {
const paymentIdValidRes = this.isValidPaymentId(paymentIdString);
if (paymentIdValidRes.isErr() || !paymentIdValidRes.value) {
return neverthrow_1.err(new errors_1.InvalidPaymentIdError(`Not a valid paymentId: '${paymentIdString}'`));
}
const domainHex = paymentIdString.substr(2, 20);
const domain = Buffer.from(domainHex, "hex").toString("ascii");
return neverthrow_1.ok(domain.trim());
}
/**
* Returns an ascii representation of the type portion of the paymentID string.
* (characters 20-31 of the paymentIdString)
* @param paymentIdString
*/
getType(paymentIdString) {
const paymentIdValidRes = this.isValidPaymentId(paymentIdString);
if (paymentIdValidRes.isErr() || !paymentIdValidRes.value) {
return neverthrow_1.err(new errors_1.InvalidPaymentIdError(`Not a valid paymentId: '${paymentIdString}'`));
}
const typeHex = paymentIdString.substr(22, 12);
const type = Buffer.from(typeHex, "hex").toString("ascii");
const trimmedType = type.trim();
if (trimmedType === types_1.EPaymentType.Pull) {
return neverthrow_1.ok(types_1.EPaymentType.Pull);
}
if (trimmedType === types_1.EPaymentType.Push) {
return neverthrow_1.ok(types_1.EPaymentType.Push);
}
return neverthrow_1.err(new errors_1.InvalidPaymentIdError(`Type did not correspond to a known EPaymentType, got '${type}'`));
}
/**
* Returns the UUID portion of the paymentID string.
* (characters 32-63 of the paymentIdString)
* @param paymentIdString
*/
getUUID(paymentIdString) {
const paymentIdValidRes = this.isValidPaymentId(paymentIdString);
if (paymentIdValidRes.isErr() || !paymentIdValidRes.value) {
return neverthrow_1.err(new errors_1.InvalidPaymentIdError(`Not a valid paymentId: '${paymentIdString}'`));
}
const UUID = paymentIdString.substr(34, 32);
return neverthrow_1.ok(UUID);
}
/**
* A valid payment ID is exactly 64 characters, hexadecimal, refixed with 0x.
* @param paymentIdString
*/
isValidPaymentId(paymentIdString) {
const overallRegex = /^0x[0-9A-Fa-f]{64}$/;
return neverthrow_1.ok(overallRegex.test(paymentIdString));
// TODO: Uses ethers library, may be better than regex
return neverthrow_1.ok(ethers_1.ethers.utils.isHexString(paymentIdString) && ethers_1.ethers.utils.hexDataLength(paymentIdString) == 64);
}
/**
* Given domain, type, and uuid, returns the computed paymentId
* @param domain Alphanumeric string of 10 characters or less
* @param type Alphanumeric string of 6 characters or less
* @param uuid Hex string of 32 characterx exactly
*/
makePaymentId(domain, type, uuid) {
const domainRegex = /^[0-9A-Za-z]{1,10}$/;
const typeRegex = /^[0-9A-Za-z]{1,6}$/;
const uuidRegex = /^[0-9A-Fa-f]{32}$/;
// strip out dashes from the uuid first
uuid = uuid.split("-").join("");
if (!domainRegex.test(domain)) {
return neverthrow_1.err(new errors_1.InvalidParametersError(`Domain must be 10 alphanumeric characters or less, got ${domain}`));
}
if (!typeRegex.test(type)) {
return neverthrow_1.err(new errors_1.InvalidParametersError(`Type must be 6 alphanumeric characters or less, got ${type}`));
}
if (!uuidRegex.test(uuid)) {
return neverthrow_1.err(new errors_1.InvalidParametersError(`UUID must be exactly 16 hex characters, got ${uuid}`));
}
// Pad with spaces to reach static lengths
domain = domain.padEnd(10);
type = type.padEnd(6);
// Convert domain and type to hex (/w ascii encoding)
const domainHex = Buffer.from(domain, "ascii").toString("hex");
// console.log(`Domain: ${domain}`)
// console.log(`DomainHex: ${domainHex}`)
const typeHex = Buffer.from(type, "ascii").toString("hex");
// Sanity check
if (domainHex.length !== 20) {
return neverthrow_1.err(new errors_1.InvalidParametersError(`Domain hex wasn't 20 chars long, got '${domainHex}'`));
}
if (typeHex.length !== 12) {
return neverthrow_1.err(new errors_1.InvalidParametersError(`Type hex wasn't 12 chars long, got '${typeHex}'`));
}
const paymentId = "0x" + domainHex + typeHex + uuid;
const isValidRes = this.isValidPaymentId(paymentId);
if (isValidRes.isOk() && isValidRes.value) {
return neverthrow_1.ok(paymentId);
}
// Either an error or invalid, either way, it's an invalid parameter issue for us
return neverthrow_1.err(new errors_1.InvalidParametersError(`Unable to create a valid payment ID from domain: ${domain}, type: ${type}, and uuid: ${uuid}`));
}
}
exports.PaymentIdUtils = PaymentIdUtils;
//# sourceMappingURL=PaymentIdUtils.js.map