@payburner/keyburner-sidewinder-core
Version:
Core library for Keyburner Sidewinder
80 lines • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreProcessor = void 0;
const npm_1 = require("@payburner/keyburner-core/dist/npm");
const CreateTokenTransactionProcessor_1 = require("./CreateTokenTransactionProcessor");
const UpdateTokenTransactionProcessor_1 = require("./UpdateTokenTransactionProcessor");
const UpdateTokenAccountTransactionProcessor_1 = require("./UpdateTokenAccountTransactionProcessor");
const TransferTransactionProcessor_1 = require("./TransferTransactionProcessor");
const CommonErrorCodes_1 = require("../model/CommonErrorCodes");
class CoreProcessor {
constructor(globalAccountService, tokenService) {
this.transactionProcessors = [];
this.keyburner = null;
this.globalAccountService = null;
this.keyburner = new npm_1.KeyBurner();
this.globalAccountService = globalAccountService;
this.transactionProcessors.push(new CreateTokenTransactionProcessor_1.CreateTokenTransactionProcessor(globalAccountService, tokenService), new TransferTransactionProcessor_1.TransferTransactionProcessor(globalAccountService, tokenService), new UpdateTokenTransactionProcessor_1.UpdateTokenTransactionProcessor(globalAccountService, tokenService), new UpdateTokenAccountTransactionProcessor_1.UpdateTokenAccountTransactionProcessor(globalAccountService, tokenService));
}
decodeTransaction(signedTransaction) {
const self = this;
return new Promise((resolve, reject) => {
const decodedTransaction = self.keyburner.decodeTransaction(signedTransaction);
self.globalAccountService.setEnvironmentAddressState(decodedTransaction.address, decodedTransaction.payload.environment, decodedTransaction.payload.sequence, decodedTransaction.id).then((result) => {
if (result) {
resolve(decodedTransaction);
}
else {
reject('Invalid sequence.');
}
}).catch((error) => {
reject('Invalid sequence.');
});
});
}
getTransactionType(decodedTransaction) {
return decodedTransaction.payload.transaction_type;
}
decodeAndProcessTransaction(signedTransaction) {
const self = this;
return new Promise((resolve, reject) => {
self.decodeTransaction(signedTransaction).then((decodedTransaction) => {
self.processTransaction(decodedTransaction).then((response) => {
resolve(response);
}).catch((error) => {
resolve(CommonErrorCodes_1.CommonErrorCodes.SYSTEM_PROBLEM_UNKNOWN);
});
}).catch((error) => {
resolve(CommonErrorCodes_1.CommonErrorCodes.INVALID_SEQUENCE);
});
});
}
processTransaction(decodedTransaction) {
const self = this;
return new Promise((resolve, reject) => {
if (decodedTransaction.verified) {
const transactionType = this.getTransactionType(decodedTransaction);
let found = false;
self.transactionProcessors.forEach((transactionProcessor) => {
if (transactionProcessor.getTransactionType() === transactionType) {
found = true;
transactionProcessor.doProcess(decodedTransaction)
.then((result) => {
resolve(result);
}).catch((error) => {
resolve(error);
});
}
});
if (!found) {
resolve(CommonErrorCodes_1.CommonErrorCodes.UNKNOWN_TRANSACTION_TYPE);
}
}
else {
resolve(CommonErrorCodes_1.CommonErrorCodes.TRANSACTION_INVALID);
}
});
}
}
exports.CoreProcessor = CoreProcessor;
//# sourceMappingURL=CoreProcessor.js.map