UNPKG

@avalabs/hw-app-avalanche

Version:

Node API for Avalanche App (Ledger Nano S/X/S+)

142 lines (140 loc) 5.74 kB
const CLA = 128; const CHUNK_SIZE = 250; const FIRST_MESSAGE = 1; const LAST_MESSAGE = 2; const NEXT_MESSAGE = 3; const HASH_LEN = 32; const COLLECTION_NAME_MAX_LEN = 70; const CHAIN_ID_SIZE = 8; const TYPE_1 = 1; const VERSION_1 = 1; const ADDRESS_LENGTH = 20; const ALGORITHM_ID_1 = 1; const ALGORITHM_ID_SIZE = 1; const TYPE_SIZE = 1; const VERSION_SIZE = 1; const SIGNATURE_LENGTH_SIZE = 1; const ED25519_PK_SIZE = 32; const INS = { GET_VERSION: 0, WALLET_ID: 1, GET_ADDR: 2, GET_EXTENDED_PUBLIC_KEY: 3, SIGN_HASH: 4, SIGN: 5, SIGN_MSG: 6, ETH_PROVIDE_NFT_INFO: 20 }; const PAYLOAD_TYPE = { INIT: 0, ADD: 1, LAST: 2 }; const P1_VALUES = { ONLY_RETRIEVE: 0, SHOW_ADDRESS_IN_DEVICE: 1 }; const P2_VALUES = { SECP256K1: 0, ED25519: 1 }; var LedgerError = /* @__PURE__ */ ((LedgerError2) => { LedgerError2[LedgerError2["U2FUnknown"] = 1] = "U2FUnknown"; LedgerError2[LedgerError2["U2FBadRequest"] = 2] = "U2FBadRequest"; LedgerError2[LedgerError2["U2FConfigurationUnsupported"] = 3] = "U2FConfigurationUnsupported"; LedgerError2[LedgerError2["U2FDeviceIneligible"] = 4] = "U2FDeviceIneligible"; LedgerError2[LedgerError2["U2FTimeout"] = 5] = "U2FTimeout"; LedgerError2[LedgerError2["Timeout"] = 14] = "Timeout"; LedgerError2[LedgerError2["NoErrors"] = 36864] = "NoErrors"; LedgerError2[LedgerError2["DeviceIsBusy"] = 36865] = "DeviceIsBusy"; LedgerError2[LedgerError2["ErrorDerivingKeys"] = 26626] = "ErrorDerivingKeys"; LedgerError2[LedgerError2["ExecutionError"] = 25600] = "ExecutionError"; LedgerError2[LedgerError2["WrongLength"] = 26368] = "WrongLength"; LedgerError2[LedgerError2["EmptyBuffer"] = 27010] = "EmptyBuffer"; LedgerError2[LedgerError2["OutputBufferTooSmall"] = 27011] = "OutputBufferTooSmall"; LedgerError2[LedgerError2["DataIsInvalid"] = 27264] = "DataIsInvalid"; LedgerError2[LedgerError2["ConditionsNotSatisfied"] = 27013] = "ConditionsNotSatisfied"; LedgerError2[LedgerError2["TransactionRejected"] = 27014] = "TransactionRejected"; LedgerError2[LedgerError2["BadKeyHandle"] = 27265] = "BadKeyHandle"; LedgerError2[LedgerError2["InvalidP1P2"] = 27392] = "InvalidP1P2"; LedgerError2[LedgerError2["InstructionNotSupported"] = 27904] = "InstructionNotSupported"; LedgerError2[LedgerError2["AppDoesNotSeemToBeOpen"] = 28161] = "AppDoesNotSeemToBeOpen"; LedgerError2[LedgerError2["UnknownError"] = 28416] = "UnknownError"; LedgerError2[LedgerError2["SignVerifyError"] = 28417] = "SignVerifyError"; return LedgerError2; })(LedgerError || {}); const ERROR_DESCRIPTION = { [1 /* U2FUnknown */]: "U2F: Unknown", [2 /* U2FBadRequest */]: "U2F: Bad request", [3 /* U2FConfigurationUnsupported */]: "U2F: Configuration unsupported", [4 /* U2FDeviceIneligible */]: "U2F: Device Ineligible", [5 /* U2FTimeout */]: "U2F: Timeout", [14 /* Timeout */]: "Timeout", [36864 /* NoErrors */]: "No errors", [36865 /* DeviceIsBusy */]: "Device is busy", [26626 /* ErrorDerivingKeys */]: "Error deriving keys", [25600 /* ExecutionError */]: "Execution Error", [26368 /* WrongLength */]: "Wrong Length", [27010 /* EmptyBuffer */]: "Empty Buffer", [27011 /* OutputBufferTooSmall */]: "Output buffer too small", [27264 /* DataIsInvalid */]: "Data is invalid", [27013 /* ConditionsNotSatisfied */]: "Conditions not satisfied", [27014 /* TransactionRejected */]: "Transaction rejected", [27265 /* BadKeyHandle */]: "Bad key handle", [27392 /* InvalidP1P2 */]: "Invalid P1/P2", [27904 /* InstructionNotSupported */]: "Instruction not supported", [28161 /* AppDoesNotSeemToBeOpen */]: "App does not seem to be open", [28416 /* UnknownError */]: "Unknown error", [28417 /* SignVerifyError */]: "Sign/verify error" }; function errorCodeToString(statusCode) { if (statusCode in ERROR_DESCRIPTION) return 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: 65535, errorMessage: response.toString() }; } return { returnCode: 65535, errorMessage: response.toString() }; } async function getVersion(transport) { return transport.send(CLA, 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) { targetId = (response[5] << 24) + (response[6] << 16) + (response[7] << 8) + (response[8] << 0); } 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); } export { ADDRESS_LENGTH, ALGORITHM_ID_1, ALGORITHM_ID_SIZE, CHAIN_ID_SIZE, CHUNK_SIZE, CLA, COLLECTION_NAME_MAX_LEN, ED25519_PK_SIZE, ERROR_DESCRIPTION, FIRST_MESSAGE, HASH_LEN, INS, LAST_MESSAGE, LedgerError, NEXT_MESSAGE, P1_VALUES, P2_VALUES, PAYLOAD_TYPE, SIGNATURE_LENGTH_SIZE, TYPE_1, TYPE_SIZE, VERSION_1, VERSION_SIZE, errorCodeToString, getVersion, processErrorResponse };