@ledgerhq/hw-app-vet
Version:
Ledger Hardware Wallet VeChain Application API
91 lines • 3.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const buffer_1 = require("buffer");
const errors_1 = require("./errors");
const remapTransactionRelatedErrors = e => {
if (e && e.statusCode === 0x6a80) {
return new errors_1.VechainAppPleaseEnableContractDataAndMultiClause("Please enable contract data in Vechain app settings");
}
return e;
};
/**
* VeChain API
*
* @example
* import Vet from "@ledgerhq/hw-app-vet";
* const vet = new Vet(transport)
*/
class Vet {
transport;
constructor(transport, scrambleKey = "V3T") {
this.transport = transport;
transport.decorateAppAPIMethods(this, ["getAppConfiguration", "getAddress", "signTransaction"], scrambleKey);
}
async getAppConfiguration() {
const response = await this.transport.send(0xe0, 0x06, 0x00, 0x00, buffer_1.Buffer.alloc(0), [
constants_1.StatusCodes.OK,
]);
return response.slice(0, 4);
}
/**
* get VeChain address for a given BIP 32 path.
* @param path a path in BIP 32 format
* @option display
* @option chainCode
* @return an object with a publicKey and address
* @example
* vet.getAddress("m/44'/818'/0'/0").then(o => o.address)
*/
async getAddress(path, display, chainCode, statusCodes = [constants_1.StatusCodes.OK]) {
const paths = (0, utils_1.splitPath)(path);
const buffer = buffer_1.Buffer.alloc(1 + paths.length * 4);
buffer[0] = paths.length;
paths.forEach((element, index) => {
buffer.writeUInt32BE(element, 1 + 4 * index);
});
const response = await this.transport.send(0xe0, 0x02, display ? 0x01 : 0x00, chainCode ? 0x01 : 0x00, buffer, statusCodes);
const publicKeyLength = response[0];
const addressLength = response[1 + publicKeyLength];
const acc = {
publicKey: response.slice(1, 1 + publicKeyLength).toString("hex"),
address: "0x" +
response
.slice(1 + publicKeyLength + 1, 1 + publicKeyLength + 1 + addressLength)
.toString("ascii")
.toLowerCase(),
};
if (chainCode) {
acc.chainCode = response
.slice(1 + publicKeyLength + 1 + addressLength, 1 + publicKeyLength + 1 + addressLength + 32)
.toString("hex");
}
return acc;
}
/**
* You can sign a transaction and retrieve v, r, s given the raw transaction and the BIP 32 path of the account to sign.
*
* @param path: the BIP32 path to sign the transaction on
* @param rawTxHex: the raw vechain transaction in hexadecimal to sign
*/
async signTransaction(path, rawTxHex) {
const buffers = (0, utils_1.splitRaw)(path, rawTxHex, true);
const responses = [];
for (let i = 0; i < buffers.length; i++) {
const data = buffers[i];
responses.push(await this.transport
.send(0xe0, 0x04, i === 0 ? 0x00 : 0x80, 0x00, data, [constants_1.StatusCodes.OK])
.catch(e => {
throw remapTransactionRelatedErrors(e);
}));
}
const lastResponse = responses[responses.length - 1];
if (lastResponse.length < 65) {
throw new Error("invalid signature");
}
return lastResponse.slice(0, 65);
}
}
exports.default = Vet;
//# sourceMappingURL=Vet.js.map