@avalabs/hw-app-avalanche
Version:
Node API for Avalanche App (Ledger Nano S/X/S+)
152 lines (151 loc) • 6.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ERROR_DESCRIPTION = exports.LedgerError = exports.P2_VALUES = exports.P1_VALUES = exports.PAYLOAD_TYPE = exports.INS = exports.ED25519_PK_SIZE = exports.SIGNATURE_LENGTH_SIZE = exports.VERSION_SIZE = exports.TYPE_SIZE = exports.ALGORITHM_ID_SIZE = exports.ALGORITHM_ID_1 = exports.ADDRESS_LENGTH = exports.VERSION_1 = exports.TYPE_1 = exports.CONTRACT_ADDRESS_LEN = exports.CHAIN_ID_SIZE = exports.COLLECTION_NAME_MAX_LEN = exports.HASH_LEN = exports.NEXT_MESSAGE = exports.LAST_MESSAGE = exports.FIRST_MESSAGE = exports.APP_KEY = exports.CHUNK_SIZE = exports.CLA_ETH = exports.CLA = void 0;
exports.errorCodeToString = errorCodeToString;
exports.processErrorResponse = processErrorResponse;
exports.getVersion = getVersion;
exports.CLA = 0x80;
exports.CLA_ETH = 0xe0;
exports.CHUNK_SIZE = 250;
exports.APP_KEY = 'AVAX';
exports.FIRST_MESSAGE = 0x01;
exports.LAST_MESSAGE = 0x02;
exports.NEXT_MESSAGE = 0x03;
exports.HASH_LEN = 32;
exports.COLLECTION_NAME_MAX_LEN = 70;
exports.CHAIN_ID_SIZE = 8;
exports.CONTRACT_ADDRESS_LEN = 20;
exports.TYPE_1 = 1;
exports.VERSION_1 = 1;
exports.ADDRESS_LENGTH = 20;
exports.ALGORITHM_ID_1 = 1;
exports.ALGORITHM_ID_SIZE = 1;
exports.TYPE_SIZE = 1;
exports.VERSION_SIZE = 1;
exports.SIGNATURE_LENGTH_SIZE = 1;
exports.ED25519_PK_SIZE = 32;
exports.INS = {
GET_VERSION: 0x00,
WALLET_ID: 0x01,
GET_ADDR: 0x02,
GET_EXTENDED_PUBLIC_KEY: 0x03,
SIGN_HASH: 0x04,
SIGN: 0x05,
SIGN_MSG: 0x06,
ETH_PROVIDE_NFT_INFO: 0x14,
};
exports.PAYLOAD_TYPE = {
INIT: 0x00,
ADD: 0x01,
LAST: 0x02,
};
exports.P1_VALUES = {
ONLY_RETRIEVE: 0x00,
SHOW_ADDRESS_IN_DEVICE: 0x01,
};
exports.P2_VALUES = {
SECP256K1: 0x00,
ED25519: 0x01,
};
var LedgerError;
(function (LedgerError) {
LedgerError[LedgerError["U2FUnknown"] = 1] = "U2FUnknown";
LedgerError[LedgerError["U2FBadRequest"] = 2] = "U2FBadRequest";
LedgerError[LedgerError["U2FConfigurationUnsupported"] = 3] = "U2FConfigurationUnsupported";
LedgerError[LedgerError["U2FDeviceIneligible"] = 4] = "U2FDeviceIneligible";
LedgerError[LedgerError["U2FTimeout"] = 5] = "U2FTimeout";
LedgerError[LedgerError["Timeout"] = 14] = "Timeout";
LedgerError[LedgerError["NoErrors"] = 36864] = "NoErrors";
LedgerError[LedgerError["DeviceIsBusy"] = 36865] = "DeviceIsBusy";
LedgerError[LedgerError["ErrorDerivingKeys"] = 26626] = "ErrorDerivingKeys";
LedgerError[LedgerError["ExecutionError"] = 25600] = "ExecutionError";
LedgerError[LedgerError["WrongLength"] = 26368] = "WrongLength";
LedgerError[LedgerError["EmptyBuffer"] = 27010] = "EmptyBuffer";
LedgerError[LedgerError["OutputBufferTooSmall"] = 27011] = "OutputBufferTooSmall";
LedgerError[LedgerError["DataIsInvalid"] = 27264] = "DataIsInvalid";
LedgerError[LedgerError["ConditionsNotSatisfied"] = 27013] = "ConditionsNotSatisfied";
LedgerError[LedgerError["TransactionRejected"] = 27014] = "TransactionRejected";
LedgerError[LedgerError["BadKeyHandle"] = 27265] = "BadKeyHandle";
LedgerError[LedgerError["InvalidP1P2"] = 27392] = "InvalidP1P2";
LedgerError[LedgerError["InstructionNotSupported"] = 27904] = "InstructionNotSupported";
LedgerError[LedgerError["AppDoesNotSeemToBeOpen"] = 28161] = "AppDoesNotSeemToBeOpen";
LedgerError[LedgerError["UnknownError"] = 28416] = "UnknownError";
LedgerError[LedgerError["SignVerifyError"] = 28417] = "SignVerifyError";
})(LedgerError || (exports.LedgerError = LedgerError = {}));
exports.ERROR_DESCRIPTION = {
[LedgerError.U2FUnknown]: 'U2F: Unknown',
[LedgerError.U2FBadRequest]: 'U2F: Bad request',
[LedgerError.U2FConfigurationUnsupported]: 'U2F: Configuration unsupported',
[LedgerError.U2FDeviceIneligible]: 'U2F: Device Ineligible',
[LedgerError.U2FTimeout]: 'U2F: Timeout',
[LedgerError.Timeout]: 'Timeout',
[LedgerError.NoErrors]: 'No errors',
[LedgerError.DeviceIsBusy]: 'Device is busy',
[LedgerError.ErrorDerivingKeys]: 'Error deriving keys',
[LedgerError.ExecutionError]: 'Execution Error',
[LedgerError.WrongLength]: 'Wrong Length',
[LedgerError.EmptyBuffer]: 'Empty Buffer',
[LedgerError.OutputBufferTooSmall]: 'Output buffer too small',
[LedgerError.DataIsInvalid]: 'Data is invalid',
[LedgerError.ConditionsNotSatisfied]: 'Conditions not satisfied',
[LedgerError.TransactionRejected]: 'Transaction rejected',
[LedgerError.BadKeyHandle]: 'Bad key handle',
[LedgerError.InvalidP1P2]: 'Invalid P1/P2',
[LedgerError.InstructionNotSupported]: 'Instruction not supported',
[LedgerError.AppDoesNotSeemToBeOpen]: 'App does not seem to be open',
[LedgerError.UnknownError]: 'Unknown error',
[LedgerError.SignVerifyError]: 'Sign/verify error',
};
function errorCodeToString(statusCode) {
if (statusCode in exports.ERROR_DESCRIPTION)
return exports.ERROR_DESCRIPTION[statusCode];
return `Unknown Status Code: ${statusCode}`;
}
function isDict(v) {
return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date);
}
function processErrorResponse(response) {
if (response) {
if (isDict(response)) {
if (Object.prototype.hasOwnProperty.call(response, 'statusCode')) {
return {
returnCode: response.statusCode,
errorMessage: errorCodeToString(response.statusCode),
};
}
if (Object.prototype.hasOwnProperty.call(response, 'returnCode') && Object.prototype.hasOwnProperty.call(response, 'errorMessage')) {
return response;
}
}
return {
returnCode: 0xffff,
errorMessage: response.toString(),
};
}
return {
returnCode: 0xffff,
errorMessage: response.toString(),
};
}
async function getVersion(transport) {
return transport.send(exports.CLA, exports.INS.GET_VERSION, 0, 0).then(response => {
const errorCodeData = response.slice(-2);
const returnCode = (errorCodeData[0] * 256 + errorCodeData[1]);
let targetId = 0;
if (response.length >= 9) {
/* eslint-disable no-bitwise */
targetId = (response[5] << 24) + (response[6] << 16) + (response[7] << 8) + (response[8] << 0);
/* eslint-enable no-bitwise */
}
return {
returnCode,
errorMessage: errorCodeToString(returnCode),
testMode: response[0] !== 0,
major: response[1],
minor: response[2],
patch: response[3],
deviceLocked: response[4] === 1,
targetId: targetId.toString(16),
};
}, processErrorResponse);
}