@test-org122/hypernet-core
Version:
Hypernet Core. Represents the SDK for running the Hypernet Protocol.
50 lines • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkUtils = void 0;
const objects_1 = require("@interfaces/objects");
const errors_1 = require("@interfaces/objects/errors");
const neverthrow_1 = require("neverthrow");
/**
* Provides functions to go from a set of payments into a set of HypernetLinks, and similar.
*/
class LinkUtils {
constructor(contextProvider) {
this.contextProvider = contextProvider;
}
/**
* Given an array of Payment objects, return the corresponding Hypernet Links
* Internally, calls transfersToPayments()
* @param payments the payments to get the associated Links for
* @param context instance of HypernetContext
*/
paymentsToHypernetLinks(payments) {
return this.contextProvider.getInitializedContext().andThen((context) => {
const linksByCounterpartyId = new Map();
for (const payment of payments) {
// Now that it's converted, we can stick it in the hypernet link
const counterpartyId = payment.to === context.publicIdentifier ? payment.from : payment.to;
let link = linksByCounterpartyId.get(counterpartyId);
if (link == null) {
link = new objects_1.HypernetLink(counterpartyId, [], [], [], [], []);
linksByCounterpartyId.set(counterpartyId, link);
}
link.payments.push(payment);
if (payment instanceof objects_1.PullPayment) {
link.pullPayments.push(payment);
link.activePullPayments.push(payment);
}
else if (payment instanceof objects_1.PushPayment) {
link.pushPayments.push(payment);
link.activePushPayments.push(payment);
}
else {
return neverthrow_1.errAsync(new errors_1.InvalidParametersError("Unknown payment type!"));
}
}
// Convert to an array for return
return neverthrow_1.okAsync(Array.from(linksByCounterpartyId.values()));
});
}
}
exports.LinkUtils = LinkUtils;
//# sourceMappingURL=LinkUtils.js.map