@helium/http
Version:
HTTP library for interacting with the Helium blockchain API
104 lines • 5.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathResult = void 0;
const DataModel_1 = __importDefault(require("./DataModel"));
var PathResult;
(function (PathResult) {
PathResult["SUCCESS"] = "success";
PathResult["FAILURE"] = "failure";
PathResult["UNTESTED"] = "untested";
})(PathResult || (exports.PathResult = PathResult = {}));
const isValidWitness = (w) => (w.is_valid === undefined ? true : w.is_valid);
const constructPath = (path) => {
let hasFailedPath = false;
const isBeacon = path.length === 1;
return path.map((pathObject, i) => {
const hasReceipt = pathObject.receipt;
const hasGeocode = pathObject.geocode;
const hasValidWitness = pathObject.witnesses.some(isValidWitness);
const hasReceiptOrValidWitnesses = hasReceipt || hasValidWitness;
const nextElement = path[i + 1];
const nextElementHasReceiptOrValidWitness = nextElement
&& (nextElement.receipt || nextElement.witnesses.some(isValidWitness));
const isFirstElement = i === 0;
const isValidBeacon = isBeacon && hasValidWitness;
const isValidChallenge = !isBeacon
&& (isFirstElement
? hasValidWitness || nextElementHasReceiptOrValidWitness
: hasReceiptOrValidWitnesses || nextElementHasReceiptOrValidWitness);
const isFailure = isFirstElement ? !hasValidWitness : !hasReceipt && !hasValidWitness;
let result = PathResult.UNTESTED;
if (!hasFailedPath && (isValidBeacon || isValidChallenge)) {
result = PathResult.SUCCESS;
}
else if (!hasFailedPath && isFailure) {
result = PathResult.FAILURE;
hasFailedPath = true;
}
return {
witnesses: pathObject.witnesses.map((witness) => (Object.assign(Object.assign({ timestamp: witness.timestamp, snr: witness.snr, signal: witness.signal, packetHash: witness.packet_hash, owner: witness.owner, location: witness.location, locationHex: witness.location_hex, isValid: isValidWitness(witness) }, (Object.prototype.hasOwnProperty.call(witness, 'invalid_reason') && { invalidReason: witness.invalid_reason })), { gateway: witness.gateway, frequency: witness.frequency, datarate: witness.datarate, channel: witness.channel }))),
receipt: hasReceipt
? {
timestamp: pathObject.receipt.timestamp,
snr: pathObject.receipt.snr,
signal: pathObject.receipt.signal,
origin: pathObject.receipt.origin,
gateway: pathObject.receipt.gateway,
frequency: pathObject.receipt.frequency,
datarate: pathObject.receipt.datarate,
data: pathObject.receipt.data,
channel: pathObject.receipt.channel,
}
: undefined,
geocode: hasGeocode
? {
shortStreet: pathObject.geocode.short_street,
shortState: pathObject.geocode.short_state,
shortCountry: pathObject.geocode.short_country,
shortCity: pathObject.geocode.short_city,
longStreet: pathObject.geocode.long_street,
longState: pathObject.geocode.long_state,
longCountry: pathObject.geocode.long_country,
longCity: pathObject.geocode.long_city,
cityId: pathObject.geocode.city_id,
}
: undefined,
challengeeOwner: pathObject.challengee_owner,
challengeeLon: pathObject.challengee_lon,
challengeeLocation: pathObject.challengee_location,
challengeeLocationHex: pathObject.challengee_location_hex,
challengeeLat: pathObject.challengee_lat,
challengee: pathObject.challengee,
result,
};
});
};
class Challenge extends DataModel_1.default {
constructor(challenge) {
super();
this.type = challenge.type;
this.time = challenge.time;
this.secret = challenge.secret;
this.requestBlockHash = challenge.request_block_hash;
this.path = constructPath(challenge.path);
this.onionKeyHash = challenge.onion_key_hash;
this.height = challenge.height;
this.hash = challenge.hash;
this.fee = challenge.fee;
this.challengerOwner = challenge.challenger_owner;
this.challengerLon = challenge.challenger_lon;
this.challengerLocation = challenge.challenger_location;
this.challengerLocationHex = challenge.challenger_location_hex;
this.challengerLat = challenge.challenger_lat;
this.challenger = challenge.challenger;
}
get data() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return this;
}
}
exports.default = Challenge;
//# sourceMappingURL=Challenge.js.map