eosjs-signature-provider-interface
Version:
An abstract class that implements the EOSJS SignatureProvider interface, and provides helper methods for interacting with an authenticator using the EOSIO Authentication Transport Protocol Specification.
172 lines • 7.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var uuid_1 = require("uuid");
var instanceOf_1 = require("./instanceOf");
var interfaces_1 = require("./interfaces");
var utils = require("./utils");
// See https://github.com/EOSIO/eosio-authentication-transport-protocol-spec for protocol versions
var AUTH_TRANSPORT_PROTOCOL_VERSION = '0.0.1';
var REJECT_MESSAGE = {
NEW_REQUEST: 'A new request was received.',
MANUAL_CANCEL: 'Transaction was manually rejected.',
UNKNOWN_RESPONSE: 'The signature provider responded with an unknown response.',
};
var SignatureProviderInterface = /** @class */ (function () {
function SignatureProviderInterface(_a) {
var _this = this;
var declaredDomain = _a.declaredDomain, returnUrl = _a.returnUrl, _b = _a.callbackUrl, callbackUrl = _b === void 0 ? '' : _b, securityExclusions = _a.securityExclusions;
/**
* Response Handlers
*/
this.handleResponse = function (responseEnvelope) {
var response = responseEnvelope.response;
if (instanceOf_1.instanceOfSelectiveDisclosureResponse(response)) {
_this.handleSelectiveDisclosureResponse(response);
}
else if (instanceOf_1.instanceOfTransactionSignatureResponse(response)) {
_this.handleTransactionSignatureResponse(response);
}
else {
_this.pendingRequest.reject({
reason: REJECT_MESSAGE.UNKNOWN_RESPONSE,
errorCode: interfaces_1.ErrorCodes.unexpectedError,
contextualInfo: '',
});
}
_this.pendingRequest = null;
};
this.declaredDomain = declaredDomain;
this.returnUrl = returnUrl;
this.callbackUrl = callbackUrl;
this.securityExclusions = securityExclusions;
}
/**
* SignatureProvider Methods
*/
SignatureProviderInterface.prototype.getAvailableKeys = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var cachedKeys, request, requestEnvelope, keys;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
cachedKeys = this.getCachedKeys();
if (cachedKeys && cachedKeys.length > 0) {
return [2 /*return*/, cachedKeys];
}
request = this.createSelectiveDisclosureRequest([interfaces_1.SelectiveDisclosureType.AUTHORIZERS]);
requestEnvelope = this.createRequestEnvelope({ request: request });
return [4 /*yield*/, this.handleRequest(requestEnvelope)];
case 1:
keys = _a.sent();
this.setCachedKeys(keys);
return [2 /*return*/, keys];
}
});
});
};
SignatureProviderInterface.prototype.sign = function (params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var request, requestEnvelope;
return tslib_1.__generator(this, function (_a) {
request = this.createTransactionSignatureRequest(params);
requestEnvelope = this.createRequestEnvelope({ request: request });
return [2 /*return*/, this.handleRequest(requestEnvelope)];
});
});
};
SignatureProviderInterface.prototype.handleRequest = function (requestEnvelope) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var promise;
var _this = this;
return tslib_1.__generator(this, function (_a) {
this.cancelPendingRequest({
reason: REJECT_MESSAGE.NEW_REQUEST,
errorCode: interfaces_1.ErrorCodes.unexpectedError,
contextualInfo: '',
});
promise = new Promise(function (resolve, reject) {
_this.pendingRequest = { resolve: resolve, reject: reject };
});
this.sendRequest(requestEnvelope);
return [2 /*return*/, promise];
});
});
};
SignatureProviderInterface.prototype.handleSelectiveDisclosureResponse = function (response) {
if (response.selectiveDisclosure.error) {
this.pendingRequest.reject(response.selectiveDisclosure.error);
return;
}
var authorizers = response.selectiveDisclosure.authorizers;
var keys = authorizers.reduce(function (result, athorizers) { return result.concat(athorizers.publicKey); }, []);
this.pendingRequest.resolve(keys);
};
SignatureProviderInterface.prototype.handleTransactionSignatureResponse = function (response) {
if (response.transactionSignature.error) {
this.pendingRequest.reject(response.transactionSignature.error);
return;
}
var pushTransactionArgs = {
signatures: response.transactionSignature.signedTransaction.signatures,
serializedTransaction: utils.hexToArray(response.transactionSignature.signedTransaction.packedTrx)
};
this.pendingRequest.resolve(pushTransactionArgs);
};
/**
* Cancelling
*/
SignatureProviderInterface.prototype.cancelPendingRequest = function (reason) {
if (this.pendingRequest) {
this.pendingRequest.reject(reason);
this.pendingRequest = null;
}
};
SignatureProviderInterface.prototype.cancelRequest = function () {
this.cancelPendingRequest({
reason: REJECT_MESSAGE.MANUAL_CANCEL,
errorCode: interfaces_1.ErrorCodes.unexpectedError,
contextualInfo: '',
});
};
/**
* Struct Creation
*/
SignatureProviderInterface.prototype.createRequestEnvelope = function (_a) {
var request = _a.request;
return tslib_1.__assign({ version: AUTH_TRANSPORT_PROTOCOL_VERSION, id: uuid_1.v4(), declaredDomain: this.declaredDomain, returnUrl: this.returnUrl, request: request, callbackUrl: this.callbackUrl }, (this.securityExclusions && { securityExclusions: this.securityExclusions }));
};
SignatureProviderInterface.prototype.createSelectiveDisclosureRequest = function (disclosures) {
return {
selectiveDisclosure: {
disclosures: disclosures.map(function (disclosure) { return ({
type: disclosure
}); })
}
};
};
SignatureProviderInterface.prototype.createTransactionSignatureRequest = function (_a) {
var chainId = _a.chainId, requiredKeys = _a.requiredKeys, serializedTransaction = _a.serializedTransaction, abis = _a.abis;
var packedTrx = utils.arrayToHex(serializedTransaction);
var hexAbis = abis.map(function (abiObject) { return ({
accountName: abiObject.accountName,
abi: utils.arrayToHex(abiObject.abi),
}); });
return {
transactionSignature: {
chainId: chainId,
publicKeys: requiredKeys,
abis: hexAbis,
transaction: {
signatures: [],
compression: 0,
packedContextFreeData: '',
packedTrx: packedTrx,
},
},
};
};
return SignatureProviderInterface;
}());
exports.SignatureProviderInterface = SignatureProviderInterface;
//# sourceMappingURL=SignatureProviderInterface.js.map