UNPKG

@zondax/ledger-stacks

Version:

Node API for Stacks App (Ledger Nano S+, X, Stax, Flex and Apex)

142 lines 6.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ERROR_DESCRIPTION = exports.LedgerError = exports.PKLEN = exports.P1_VALUES = exports.PAYLOAD_TYPE = exports.MULTISIG_MAX_PUBKEYS = exports.MULTISIG_HASH_MODE = exports.INS = exports.APP_KEY = exports.CHUNK_SIZE = exports.CLA = void 0; exports.errorCodeToString = errorCodeToString; exports.processErrorResponse = processErrorResponse; exports.getVersion = getVersion; exports.CLA = 0x09; exports.CHUNK_SIZE = 250; exports.APP_KEY = 'STX'; exports.INS = { GET_VERSION: 0x00, GET_ADDR_SECP256K1: 0x01, SIGN_SECP256K1: 0x02, GET_AUTH_PUBKEY: 0x03, SIGN_JWT_SECP256K1: 0x04, SIGN_STRUCTURED_MSG: 0x05, GET_MASTER_FINGERPRINT: 0x06, GET_ADDR_MULTISIG: 0x07, }; // Multisig hash modes accepted by the device for address verification. // Sequential and non-sequential P2SH derive the *same* address (identical // redeem script / Hash160); they differ only in how the transaction is later // signed. SegWit modes (P2WSH / P2WSH non-sequential) are not supported. exports.MULTISIG_HASH_MODE = { P2SH: 0x01, P2SH_NONSEQUENTIAL: 0x05, }; // The 520-byte P2SH redeem-script limit caps a Stacks multisig at 15 compressed // keys (matches stacks.js). The header + cosigner keys are sent chunked. exports.MULTISIG_MAX_PUBKEYS = 15; exports.PAYLOAD_TYPE = { INIT: 0x00, ADD: 0x01, LAST: 0x02, }; exports.P1_VALUES = { ONLY_RETRIEVE: 0x00, SHOW_ADDRESS_IN_DEVICE: 0x01, }; exports.PKLEN = 33; 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"] = 27012] = "DataIsInvalid"; LedgerError[LedgerError["ConditionsNotSatisfied"] = 27013] = "ConditionsNotSatisfied"; LedgerError[LedgerError["TransactionRejected"] = 27014] = "TransactionRejected"; LedgerError[LedgerError["BadKeyHandle"] = 27264] = "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(), }; } 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] ?? 0) * 256 + (errorCodeData[1] ?? 0)); let targetId = 0; if (response.length >= 9) { targetId = ((response[5] ?? 0) << 24) + ((response[6] ?? 0) << 16) + ((response[7] ?? 0) << 8) + ((response[8] ?? 0) << 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); } //# sourceMappingURL=common.js.map