@thorchain/ledger-thorchain
Version:
Node API for THORChain App (Ledger Nano S/X)
360 lines • 19.2 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.THORChainApp = void 0;
var Helpers = __importStar(require("./helper"));
var common_1 = require("./common");
var THORChainApp = /** @class */ (function () {
function THORChainApp(transport, scrambleKey) {
if (scrambleKey === void 0) { scrambleKey = common_1.APP_KEY; }
if (!transport) {
throw new Error("Transport has not been defined");
}
this.transport = transport;
transport.decorateAppAPIMethods(this, ["getVersion", "sign", "getAddressAndPubKey", "appInfo", "deviceInfo", "getBech32FromPK"], scrambleKey);
}
THORChainApp.prototype.serializePath = function (path) {
return __awaiter(this, void 0, void 0, function () {
var version;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Helpers.getVersion(this.transport)];
case 1:
version = _a.sent();
if (version.returnCode !== common_1.LedgerErrorType.NoErrors) {
throw new common_1.LedgerError(version.returnCode, version.errorMessage);
}
switch (version.major) {
case 2:
return [2 /*return*/, Helpers.serializePath(path)];
default:
throw new common_1.LedgerError(common_1.LedgerErrorType.ExecutionError, "App Version is not supported");
}
return [2 /*return*/];
}
});
});
};
THORChainApp.prototype.signGetChunks = function (path, message) {
return __awaiter(this, void 0, void 0, function () {
var serializedPath, chunks, buffer, i, end;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.serializePath(path)];
case 1:
serializedPath = _a.sent();
chunks = [];
chunks.push(serializedPath);
buffer = Buffer.from(message);
for (i = 0; i < buffer.length; i += common_1.CHUNK_SIZE) {
end = i + common_1.CHUNK_SIZE;
if (i > buffer.length) {
end = buffer.length;
}
chunks.push(buffer.slice(i, end));
}
return [2 /*return*/, chunks];
}
});
});
};
THORChainApp.prototype.getVersion = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, Helpers.getVersion(this.transport)];
});
});
};
THORChainApp.prototype.getAppInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var response, errorCodeData, returnCode, appNameLen, appName, idx, appVersionLen, appVersion, appFlagsLen, flagLen, flagsValue, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.transport.send(0xb0, 0x01, 0, 0)];
case 1:
response = _a.sent();
errorCodeData = response.slice(-2);
returnCode = errorCodeData[0] * 256 + errorCodeData[1];
if (response[0] !== 1) {
// Ledger responds with format ID 1. There is no spec for any format != 1
throw new common_1.LedgerError(common_1.LedgerErrorType.DeviceIsBusy, "response format ID not recognized");
}
appNameLen = response[1];
appName = response.slice(2, 2 + appNameLen).toString("ascii");
idx = 2 + appNameLen;
appVersionLen = response[idx];
idx += 1;
appVersion = response.slice(idx, idx + appVersionLen).toString("ascii");
idx += appVersionLen;
appFlagsLen = response[idx];
idx += 1;
flagLen = appFlagsLen;
flagsValue = response[idx];
return [2 /*return*/, {
returnCode: returnCode,
errorMessage: common_1.errorCodeToString(returnCode),
appName: appName,
appVersion: appVersion,
flagLen: flagLen,
flagsValue: flagsValue,
// eslint-disable-next-line no-bitwise
flagRecovery: (flagsValue & 1) !== 0,
// eslint-disable-next-line no-bitwise
flagSignedMcuCode: (flagsValue & 2) !== 0,
// eslint-disable-next-line no-bitwise
flagOnboarded: (flagsValue & 4) !== 0,
// eslint-disable-next-line no-bitwise
flagPinValidated: (flagsValue & 128) !== 0,
}];
case 2:
error_1 = _a.sent();
throw common_1.ledgerErrorFromResponse(error_1);
case 3: return [2 /*return*/];
}
});
});
};
THORChainApp.prototype.getDeviceInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var response, errorCodeData, returnCode, targetId, pos, secureElementVersionLen, seVersion, flagsLen, flag, mcuVersionLen, tmp, mcuVersion, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.transport.send(0xe0, 0x01, 0, 0, Buffer.from([]), [
common_1.LedgerErrorType.NoErrors,
common_1.LedgerErrorType.AppDoesNotSeemToBeOpen,
])];
case 1:
response = _a.sent();
errorCodeData = response.slice(-2);
returnCode = errorCodeData[0] * 256 + errorCodeData[1];
if (returnCode === common_1.LedgerErrorType.AppDoesNotSeemToBeOpen) {
throw new common_1.LedgerError(returnCode, "This command is only available in the Dashboard");
}
targetId = response.slice(0, 4).toString("hex");
pos = 4;
secureElementVersionLen = response[pos];
pos += 1;
seVersion = response.slice(pos, pos + secureElementVersionLen).toString();
pos += secureElementVersionLen;
flagsLen = response[pos];
pos += 1;
flag = response.slice(pos, pos + flagsLen).toString("hex");
pos += flagsLen;
mcuVersionLen = response[pos];
pos += 1;
tmp = response.slice(pos, pos + mcuVersionLen);
if (tmp[mcuVersionLen - 1] === 0) {
tmp = response.slice(pos, pos + mcuVersionLen - 1);
}
mcuVersion = tmp.toString();
return [2 /*return*/, {
returnCode: returnCode,
errorMessage: common_1.errorCodeToString(returnCode),
targetId: targetId,
seVersion: seVersion,
flag: flag,
mcuVersion: mcuVersion,
}];
case 2:
error_2 = _a.sent();
throw common_1.ledgerErrorFromResponse(error_2);
case 3: return [2 /*return*/];
}
});
});
};
THORChainApp.prototype.getPublicKey = function (path) {
return __awaiter(this, void 0, void 0, function () {
var serializedPath, version, data, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
return [4 /*yield*/, this.serializePath(path)];
case 1:
serializedPath = _a.sent();
return [4 /*yield*/, this.getVersion()];
case 2:
version = _a.sent();
switch (version.major) {
case 2: {
data = Buffer.concat([Helpers.serializeHRP("thor"), serializedPath]);
return [2 /*return*/, Helpers.getPublicKey(this.transport, data)];
}
default:
throw new common_1.LedgerError(common_1.LedgerErrorType.ExecutionError, "App Version is not supported");
}
return [3 /*break*/, 4];
case 3:
e_1 = _a.sent();
throw common_1.ledgerErrorFromResponse(e_1);
case 4: return [2 /*return*/];
}
});
});
};
THORChainApp.prototype.getAddressAndPubKey = function (path, hrp) {
return __awaiter(this, void 0, void 0, function () {
var serializedPath, serializedHRP, data, response, errorCodeData, returnCode, compressedPk, bech32Address;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.serializePath(path)];
case 1:
serializedPath = _a.sent();
serializedHRP = Helpers.serializeHRP(hrp);
data = Buffer.concat([serializedHRP, serializedPath]);
return [4 /*yield*/, this.transport.send(common_1.CLA, common_1.INS.GET_ADDR_SECP256K1, common_1.P1_VALUES.ONLY_RETRIEVE, 0, data, [common_1.LedgerErrorType.NoErrors])];
case 2:
response = _a.sent();
errorCodeData = response.slice(-2);
returnCode = errorCodeData[0] * 256 + errorCodeData[1];
compressedPk = Buffer.from(response.slice(0, 33));
bech32Address = Buffer.from(response.slice(33, -2)).toString();
return [2 /*return*/, {
bech32Address: bech32Address,
compressedPk: compressedPk,
returnCode: returnCode,
errorMessage: common_1.errorCodeToString(returnCode),
}];
}
});
});
};
THORChainApp.prototype.showAddressAndPubKey = function (path, hrp) {
return __awaiter(this, void 0, void 0, function () {
var serializedPath, data, response, errorCodeData, returnCode, compressedPk, bech32Address;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.serializePath(path)];
case 1:
serializedPath = _a.sent();
data = Buffer.concat([Helpers.serializeHRP(hrp), serializedPath]);
return [4 /*yield*/, this.transport.send(common_1.CLA, common_1.INS.GET_ADDR_SECP256K1, common_1.P1_VALUES.SHOW_ADDRESS_IN_DEVICE, 0, data, [common_1.LedgerErrorType.NoErrors])];
case 2:
response = _a.sent();
errorCodeData = response.slice(-2);
returnCode = errorCodeData[0] * 256 + errorCodeData[1];
compressedPk = Buffer.from(response.slice(0, 33));
bech32Address = Buffer.from(response.slice(33, -2)).toString();
return [2 /*return*/, {
bech32Address: bech32Address,
compressedPk: compressedPk,
returnCode: returnCode,
errorMessage: common_1.errorCodeToString(returnCode),
}];
}
});
});
};
THORChainApp.prototype.signSendChunk = function (chunkIdx, chunkNum, chunk) {
return __awaiter(this, void 0, void 0, function () {
var version;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Helpers.getVersion(this.transport)];
case 1:
version = _a.sent();
switch (version.major) {
case 2:
return [2 /*return*/, Helpers.signSendChunk(this.transport, chunkIdx, chunkNum, chunk)];
default:
throw new common_1.LedgerError(common_1.LedgerErrorType.ExecutionError, "App Version is not supported");
}
return [2 /*return*/];
}
});
});
};
THORChainApp.prototype.sign = function (path, message) {
return __awaiter(this, void 0, void 0, function () {
var chunks, response, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.signGetChunks(path, message)];
case 1:
chunks = _a.sent();
return [4 /*yield*/, this.signSendChunk(1, chunks.length, chunks[0])];
case 2:
response = _a.sent();
i = 1;
_a.label = 3;
case 3:
if (!(i < chunks.length)) return [3 /*break*/, 6];
return [4 /*yield*/, this.signSendChunk(1 + i, chunks.length, chunks[i])];
case 4:
// eslint-disable-next-line no-await-in-loop
response = _a.sent();
if (response.returnCode !== common_1.LedgerErrorType.NoErrors) {
return [3 /*break*/, 6];
}
_a.label = 5;
case 5:
i += 1;
return [3 /*break*/, 3];
case 6: return [2 /*return*/, response];
}
});
});
};
return THORChainApp;
}());
exports.THORChainApp = THORChainApp;
//# sourceMappingURL=app.js.map