@zondax/ledger-filecoin
Version:
Node API for the Filecoin App (Ledger Nano S+, X, Stax and Flex)
157 lines • 6.54 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilecoinApp = void 0;
/** ******************************************************************************
* (c) 2019-2025 Zondax AG
*
* 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.
******************************************************************************* */
const hw_app_eth_1 = __importDefault(require("@ledgerhq/hw-app-eth"));
const ledger_js_1 = __importStar(require("@zondax/ledger-js"));
const varint = __importStar(require("varint"));
const consts_1 = require("./consts");
class FilecoinApp extends ledger_js_1.default {
constructor(transport) {
super(transport, FilecoinApp._params);
if (!this.transport) {
throw new Error('Transport has not been defined');
}
this.eth = new hw_app_eth_1.default(transport);
}
parseAddressResponse(response) {
const compressed_pk = response.readBytes(consts_1.PUBKEYLEN);
const addrByteLength = response.readBytes(1)[0];
const addrByte = response.readBytes(addrByteLength);
const addrStringLength = response.readBytes(1)[0];
const addrString = response.readBytes(addrStringLength).toString();
return {
compressed_pk,
addrByte,
addrString,
};
}
async getAddressAndPubKey(path) {
const bip44PathBuffer = this.serializePath(path);
try {
const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, consts_1.P1_VALUES.ONLY_RETRIEVE, 0, bip44PathBuffer);
const response = (0, ledger_js_1.processResponse)(responseBuffer);
return this.parseAddressResponse(response);
}
catch (e) {
throw (0, ledger_js_1.processErrorResponse)(e);
}
}
async showAddressAndPubKey(path) {
const bip44PathBuffer = this.serializePath(path);
try {
const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, consts_1.P1_VALUES.SHOW_ADDRESS_IN_DEVICE, 0, bip44PathBuffer);
const response = (0, ledger_js_1.processResponse)(responseBuffer);
return this.parseAddressResponse(response);
}
catch (e) {
throw (0, ledger_js_1.processErrorResponse)(e);
}
}
async _sign(instruction, path, data) {
const chunks = this.prepareChunks(path, data);
try {
// First chunk
let signatureResponse = await this.sendGenericChunk(instruction, 0, 1, chunks.length, chunks[0]);
for (let i = 1; i < chunks.length; i += 1) {
signatureResponse = await this.sendGenericChunk(instruction, 0, 1 + i, chunks.length, chunks[i]);
}
return {
signature_compact: signatureResponse.readBytes(65),
signature_der: signatureResponse.getAvailableBuffer(),
};
}
catch (e) {
throw (0, ledger_js_1.processErrorResponse)(e);
}
}
async sign(path, blob) {
return this._sign(this.INS.SIGN_SECP256K1, path, blob);
}
async signRawBytes(path, message) {
const len = Buffer.from(varint.encode(message.length));
const data = Buffer.concat([len, message]);
return this._sign(this.INS.SIGN_RAW_BYTES, path, data);
}
async signPersonalMessageFVM(path, messageHex) {
const len = Buffer.alloc(4);
len.writeUInt32BE(messageHex.length, 0);
const data = Buffer.concat([len, messageHex]);
return this._sign(this.INS.SIGN_PERSONAL_MESSAGE, path, data);
}
async signETHTransaction(path, rawTxHex, resolution = null) {
return await this.eth.signTransaction(path, rawTxHex, resolution);
}
async getETHAddress(path, boolDisplay = false, boolChaincode = false) {
return await this.eth.getAddress(path, boolDisplay, boolChaincode);
}
async signPersonalMessageEVM(path, messageHex) {
return await this.eth.signPersonalMessage(path, messageHex);
}
}
exports.FilecoinApp = FilecoinApp;
FilecoinApp._INS = {
GET_VERSION: 0x00,
GET_ADDR_SECP256K1: 0x01,
SIGN_SECP256K1: 0x02,
SIGN_RAW_BYTES: 0x07,
SIGN_PERSONAL_MESSAGE: 0x08,
};
FilecoinApp._params = {
cla: 0x06,
ins: { ...FilecoinApp._INS },
p1Values: { ONLY_RETRIEVE: 0x00, SHOW_ADDRESS_IN_DEVICE: 0x01 },
chunkSize: 250,
requiredPathLengths: [5],
};
//# sourceMappingURL=app.js.map