@zondax/ledger-polkadot
Version:
TS / Node API for Polkadot apps running on Ledger Nano S/S+/X and Stax
86 lines (85 loc) • 3.51 kB
JavaScript
;
/** ******************************************************************************
* (c) 2019 - 2023 Zondax AG
* (c) 2016-2017 Ledger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************* */
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializePath = exports.processErrorResponse = exports.splitChunks = exports.errorCodeToString = void 0;
const types_1 = require("./types");
const CHUNK_SIZE = 250;
function errorCodeToString(statusCode) {
return types_1.ERROR_DESCRIPTION[statusCode] ?? `Unknown Status Code: ${statusCode}`;
}
exports.errorCodeToString = errorCodeToString;
function splitChunks(message) {
const chunks = [];
const buffer = Buffer.from(message);
for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {
let end = i + CHUNK_SIZE;
if (i > buffer.length) {
end = buffer.length;
}
chunks.push(buffer.subarray(i, end));
}
return chunks;
}
exports.splitChunks = splitChunks;
// response should be an object with statusCode entry
// we want to forward it and go ahead
function processErrorResponse(response) {
if (response == null) {
return { returnCode: 0xffff, errorMessage: "Empty response received" };
}
if (!Object.prototype.hasOwnProperty.call(response, "statusCode")) {
console.log(response);
return { returnCode: 0xffff, errorMessage: "Unknown error format found" };
}
return {
// @ts-expect-error statusCode exists here
returnCode: response.statusCode,
// @ts-expect-error statusCode exists here
errorMessage: errorCodeToString(response.statusCode),
};
}
exports.processErrorResponse = processErrorResponse;
function serializePath(path) {
const HARDENED = 0x80000000;
if (!path.startsWith("m/44'/354'"))
throw new Error(`Path should start with "m/44'/354'" (e.g "m/44'/354'/0'/0/1")`);
const pathArray = path.split("/");
pathArray.shift(); // remove "m"
if (pathArray.length !== 5)
throw new Error(`Invalid path length. It needs to have 5 entries. (e.g "m/44'/354'/0'/0/1")`);
const buf = Buffer.alloc(4 * pathArray.length);
for (let i = 0; i < pathArray.length; i++) {
let child = pathArray[i];
let value = 0;
if (child.endsWith("'")) {
value += HARDENED;
child = child.slice(0, -1);
}
const numChild = Number(child);
if (Number.isNaN(numChild))
throw new Error(`Invalid path : ${child} is not a number `);
if (!Number.isInteger(numChild))
throw new Error(`Invalid entry value. ${child} is not an integer`);
if (numChild >= HARDENED)
throw new Error(`Invalid entry value. ${child} needs to be up to 2^31`);
value += numChild;
buf.writeUInt32LE(value, 4 * i);
}
return buf;
}
exports.serializePath = serializePath;