UNPKG

5.2 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.SXP = void 0;
23const Apdu = __importStar(require("./apdu"));
24const Bip44 = __importStar(require("./bip44"));
25const TransportErrors = __importStar(require("./errors"));
26class SXP {
27 /**
28 * Create an instance using a 'LedgerTransport' object.
29 *
30 * 'decorateAppAPIMethods' basically "locks" execution of the current instruction,
31 * preventing race conditions where parallel calls are attempted.
32 *
33 * @param {LedgerTransport} transport generic transport interface for Ledger HW.
34 */
35 constructor(transport) {
36 Object.defineProperty(this, "transport", {
37 enumerable: true,
38 configurable: true,
39 writable: true,
40 value: void 0
41 });
42 this.transport = transport;
43 this.transport.decorateAppAPIMethods(this, ["getVersion", "getPublicKey", "getExtPublicKey", "signMessageWithSchnorr", "signTransactionWithSchnorr"], "w0w");
44 }
45 /**
46 * Get the installed Application version from a Ledger Device.
47 *
48 * @returns {Promise<string>} installed application version (e.g. '2.0.1')
49 */
50 async getVersion() {
51 const response = await new Apdu.Builder(Apdu.Flag.CLA, Apdu.Flag.INS_GET_VERSION, Apdu.Flag.P1_NON_CONFIRM, Apdu.Flag.P2_NO_CHAINCODE).send(this.transport);
52 return `${response[1]}.${response[2]}.${response[3]}`;
53 }
54 /**
55 * Get the PublicKey from a Ledger Device using a Bip44 path-string.
56 *
57 * @param {string} path bip44 path as a string
58 * @returns {Promise<string>} device compressed publicKey
59 */
60 async getPublicKey(path) {
61 const response = await new Apdu.Builder(Apdu.Flag.CLA, Apdu.Flag.INS_GET_PUBLIC_KEY, Apdu.Flag.P1_NON_CONFIRM, Apdu.Flag.P2_NO_CHAINCODE, Bip44.Path.fromString(path).toBytes()).send(this.transport);
62 return response.slice(1, response.length).toString("hex");
63 }
64 /**
65 * Get the Extended PublicKey from a Ledger Device using a Bip44 path-string.
66 *
67 * Used to derive a 'hardened' account key (eg 44'/111'/0'").
68 * Hex result is a 33-byte compressed publicKey prepending a 32-byte chainCode.
69 *
70 * @param {string} path bip44 path as a string
71 * @returns {Promise<string>} device extended publicKey & chaincode
72 */
73 async getExtPublicKey(path) {
74 const response = await new Apdu.Builder(Apdu.Flag.CLA, Apdu.Flag.INS_GET_PUBLIC_KEY, Apdu.Flag.P1_NON_CONFIRM, Apdu.Flag.P2_CHAINCODE, Bip44.Path.fromString(path).toBytes()).send(this.transport);
75 return response.slice(1, response.length).toString("hex");
76 }
77 /**
78 * Sign a Message using a Ledger Device with Schnorr Signatures.
79 *
80 * @param {string} path bip44 path as a string
81 * @param {Buffer} message message payload
82 * @returns {Promise<string>} payload schnorr signature
83 */
84 async signMessageWithSchnorr(path, message) {
85 this.checkMessageFormat(message);
86 const response = await new Apdu.Builder(Apdu.Flag.CLA, Apdu.Flag.INS_SIGN_MESSAGE, Apdu.Flag.P1_SINGLE, Apdu.Flag.P2_SCHNORR_LEG, Buffer.concat([Bip44.Path.fromString(path).toBytes(), message])).send(this.transport);
87 return response.toString("hex");
88 }
89 /**
90 * Sign a Transaction using a Ledger Device with Schnorr Signatures.
91 *
92 * @param {string} path bip44 path as a string
93 * @param {Buffer} payload transaction bytes
94 * @returns {Promise<string>} payload schnorr signature
95 */
96 async signTransactionWithSchnorr(path, payload) {
97 const response = await new Apdu.Builder(Apdu.Flag.CLA, Apdu.Flag.INS_SIGN_TRANSACTION, Apdu.Flag.P1_SINGLE, Apdu.Flag.P2_SCHNORR_LEG, Buffer.concat([Bip44.Path.fromString(path).toBytes(), payload])).send(this.transport);
98 return response.toString("hex");
99 }
100 /**
101 * Check the formatting of a message.
102 *
103 * @param {Buffer} message message payload
104 * @throws {MessageAsciiError} if the message contains non-ascii characters
105 */
106 checkMessageFormat(message) {
107 const REGEXP_INVALID_MESSAGE = "[^\x00-\x7F]";
108 if (message.toString().match(new RegExp(REGEXP_INVALID_MESSAGE, "g"))) {
109 throw new TransportErrors.MessageAsciiError();
110 }
111 }
112}
113exports.SXP = SXP;
114//# sourceMappingURL=sxp.js.map
\No newline at end of file