@gandlaf21/bc-ur
Version:
A JS implementation of the Uniform Resources (UR) specification from Blockchain Commons
144 lines (143 loc) • 5.66 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var fountainDecoder_1 = __importDefault(require("./fountainDecoder"));
var bytewords_1 = __importDefault(require("./bytewords"));
var utils_1 = require("./utils");
var errors_1 = require("./errors");
var ur_1 = __importDefault(require("./ur"));
var fountainEncoder_1 = require("./fountainEncoder");
var buffer_1 = require("buffer");
var URDecoder = /** @class */ (function () {
function URDecoder(fountainDecoder, type) {
if (fountainDecoder === void 0) { fountainDecoder = new fountainDecoder_1.default(); }
if (type === void 0) { type = 'bytes'; }
this.fountainDecoder = fountainDecoder;
this.type = type;
if (!utils_1.isURType(type)) {
throw new Error("Invalid UR type");
}
this.expected_type = '';
}
URDecoder.decodeBody = function (type, message) {
var cbor = bytewords_1.default.decode(message, bytewords_1.default.STYLES.MINIMAL);
return new ur_1.default(buffer_1.Buffer.from(cbor, 'hex'), type);
};
URDecoder.prototype.validatePart = function (type) {
if (this.expected_type) {
return this.expected_type === type;
}
if (!utils_1.isURType(type)) {
return false;
}
this.expected_type = type;
return true;
};
URDecoder.decode = function (message) {
var _a = this.parse(message), type = _a[0], components = _a[1];
if (components.length === 0) {
throw new errors_1.InvalidPathLengthError();
}
var body = components[0];
return URDecoder.decodeBody(type, body);
};
URDecoder.parse = function (message) {
var lowercase = message.toLowerCase();
var prefix = lowercase.slice(0, 3);
if (prefix !== 'ur:') {
throw new errors_1.InvalidSchemeError();
}
var components = lowercase.slice(3).split('/');
var type = components[0];
if (components.length < 2) {
throw new errors_1.InvalidPathLengthError();
}
if (!utils_1.isURType(type)) {
throw new errors_1.InvalidTypeError();
}
return [type, components.slice(1)];
};
URDecoder.parseSequenceComponent = function (s) {
var components = s.split('-');
if (components.length !== 2) {
throw new errors_1.InvalidSequenceComponentError();
}
var seqNum = utils_1.toUint32(Number(components[0]));
var seqLength = Number(components[1]);
if (seqNum < 1 || seqLength < 1) {
throw new errors_1.InvalidSequenceComponentError();
}
return [seqNum, seqLength];
};
URDecoder.prototype.receivePart = function (s) {
if (this.result !== undefined) {
return false;
}
var _a = URDecoder.parse(s), type = _a[0], components = _a[1];
if (!this.validatePart(type)) {
return false;
}
// If this is a single-part UR then we're done
if (components.length === 1) {
this.result = URDecoder.decodeBody(type, components[0]);
return true;
}
if (components.length !== 2) {
throw new errors_1.InvalidPathLengthError();
}
var seq = components[0], fragment = components[1];
var _b = URDecoder.parseSequenceComponent(seq), seqNum = _b[0], seqLength = _b[1];
var cbor = bytewords_1.default.decode(fragment, bytewords_1.default.STYLES.MINIMAL);
var part = fountainEncoder_1.FountainEncoderPart.fromCBOR(cbor);
if (seqNum !== part.seqNum || seqLength !== part.seqLength) {
return false;
}
if (!this.fountainDecoder.receivePart(part)) {
return false;
}
if (this.fountainDecoder.isSuccess()) {
this.result = new ur_1.default(this.fountainDecoder.resultMessage(), type);
}
else if (this.fountainDecoder.isFailure()) {
this.error = new errors_1.InvalidSchemeError();
}
return true;
};
URDecoder.prototype.resultUR = function () {
return this.result ? this.result : new ur_1.default(buffer_1.Buffer.from([]));
};
URDecoder.prototype.isComplete = function () {
return this.result && this.result.cbor.length > 0 ? true : false;
};
URDecoder.prototype.isSuccess = function () {
return !this.error && this.isComplete();
};
URDecoder.prototype.isError = function () {
return this.error !== undefined;
};
URDecoder.prototype.resultError = function () {
return this.error ? this.error.message : '';
};
URDecoder.prototype.expectedPartCount = function () {
return this.fountainDecoder.expectedPartCount();
};
URDecoder.prototype.expectedPartIndexes = function () {
return this.fountainDecoder.getExpectedPartIndexes();
};
URDecoder.prototype.receivedPartIndexes = function () {
return this.fountainDecoder.getReceivedPartIndexes();
};
URDecoder.prototype.lastPartIndexes = function () {
return this.fountainDecoder.getLastPartIndexes();
};
URDecoder.prototype.estimatedPercentComplete = function () {
return this.fountainDecoder.estimatedPercentComplete();
};
URDecoder.prototype.getProgress = function () {
return this.fountainDecoder.getProgress();
};
return URDecoder;
}());
exports.default = URDecoder;