@injectivelabs/wallet-ledger 1.17.2-alpha.0 → 1.17.2-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,179 @@
1
+ const require_lib_es = require('./lib-es-_HPkCZoI.cjs');
2
+ let buffer = require("buffer");
3
+
4
+ //#region ../../../node_modules/.pnpm/bip32-path@0.4.2/node_modules/bip32-path/index.js
5
+ var require_bip32_path = /* @__PURE__ */ require_lib_es.__commonJS({ "../../../node_modules/.pnpm/bip32-path@0.4.2/node_modules/bip32-path/index.js": ((exports, module) => {
6
+ const HARDENED = 2147483648;
7
+ var BIPPath$1 = function(path) {
8
+ if (!Array.isArray(path)) throw new Error("Input must be an Array");
9
+ if (path.length === 0) throw new Error("Path must contain at least one level");
10
+ for (var i = 0; i < path.length; i++) if (typeof path[i] !== "number") throw new Error("Path element is not a number");
11
+ this.path = path;
12
+ };
13
+ BIPPath$1.validatePathArray = function(path) {
14
+ try {
15
+ BIPPath$1.fromPathArray(path);
16
+ return true;
17
+ } catch (e) {
18
+ return false;
19
+ }
20
+ };
21
+ BIPPath$1.validateString = function(text, reqRoot) {
22
+ try {
23
+ BIPPath$1.fromString(text, reqRoot);
24
+ return true;
25
+ } catch (e) {
26
+ return false;
27
+ }
28
+ };
29
+ BIPPath$1.fromPathArray = function(path) {
30
+ return new BIPPath$1(path);
31
+ };
32
+ BIPPath$1.fromString = function(text, reqRoot) {
33
+ if (/^m\//i.test(text)) text = text.slice(2);
34
+ else if (reqRoot) throw new Error("Root element is required");
35
+ var path = text.split("/");
36
+ var ret = new Array(path.length);
37
+ for (var i = 0; i < path.length; i++) {
38
+ var tmp = /(\d+)([hH\']?)/.exec(path[i]);
39
+ if (tmp === null) throw new Error("Invalid input");
40
+ ret[i] = parseInt(tmp[1], 10);
41
+ if (ret[i] >= HARDENED) throw new Error("Invalid child index");
42
+ if (tmp[2] === "h" || tmp[2] === "H" || tmp[2] === "'") ret[i] += HARDENED;
43
+ else if (tmp[2].length != 0) throw new Error("Invalid modifier");
44
+ }
45
+ return new BIPPath$1(ret);
46
+ };
47
+ BIPPath$1.prototype.toPathArray = function() {
48
+ return this.path;
49
+ };
50
+ BIPPath$1.prototype.toString = function(noRoot, oldStyle) {
51
+ var ret = new Array(this.path.length);
52
+ for (var i = 0; i < this.path.length; i++) {
53
+ var tmp = this.path[i];
54
+ if (tmp & HARDENED) ret[i] = (tmp & ~HARDENED) + (oldStyle ? "h" : "'");
55
+ else ret[i] = tmp;
56
+ }
57
+ return (noRoot ? "" : "m/") + ret.join("/");
58
+ };
59
+ BIPPath$1.prototype.inspect = function() {
60
+ return "BIPPath <" + this.toString() + ">";
61
+ };
62
+ module.exports = BIPPath$1;
63
+ }) });
64
+
65
+ //#endregion
66
+ //#region ../../../node_modules/.pnpm/@ledgerhq+hw-app-cosmos@6.32.9/node_modules/@ledgerhq/hw-app-cosmos/lib-es/Cosmos.js
67
+ var import_bip32_path = /* @__PURE__ */ require_lib_es.__toESM(require_bip32_path());
68
+ const CHUNK_SIZE = 250;
69
+ const CLA = 85;
70
+ const APP_KEY = "CSM";
71
+ const INS_GET_VERSION = 0;
72
+ const INS_SIGN_SECP256K1 = 2;
73
+ const INS_GET_ADDR_SECP256K1 = 4;
74
+ const PAYLOAD_TYPE_INIT = 0;
75
+ const PAYLOAD_TYPE_ADD = 1;
76
+ const PAYLOAD_TYPE_LAST = 2;
77
+ const SW_OK = 36864;
78
+ const SW_CANCEL = 27014;
79
+ /**
80
+ * Cosmos API
81
+ *
82
+ * @example
83
+ * import Cosmos from "@ledgerhq/hw-app-cosmos";
84
+ * const cosmos = new Cosmos(transport)
85
+ */
86
+ var Cosmos = class {
87
+ transport;
88
+ constructor(transport, scrambleKey = APP_KEY) {
89
+ this.transport = transport;
90
+ transport.decorateAppAPIMethods(this, [
91
+ "getAddress",
92
+ "sign",
93
+ "getAppConfiguration"
94
+ ], scrambleKey);
95
+ }
96
+ getAppConfiguration() {
97
+ return this.transport.send(CLA, INS_GET_VERSION, 0, 0).then((response) => {
98
+ return {
99
+ test_mode: response[0] !== 0,
100
+ version: "" + response[1] + "." + response[2] + "." + response[3],
101
+ device_locked: response[4] === 1,
102
+ major: response[1]
103
+ };
104
+ });
105
+ }
106
+ serializePath(path) {
107
+ const buf = buffer.Buffer.alloc(20);
108
+ buf.writeUInt32LE((2147483648 | path[0]) >>> 0, 0);
109
+ buf.writeUInt32LE((2147483648 | path[1]) >>> 0, 4);
110
+ buf.writeUInt32LE((2147483648 | path[2]) >>> 0, 8);
111
+ buf.writeUInt32LE(path[3], 12);
112
+ buf.writeUInt32LE(path[4], 16);
113
+ return buf;
114
+ }
115
+ serializeHRP(hrp) {
116
+ if (hrp == null || hrp.length === 0 || hrp.length > 83) throw new Error("Invalid HRP");
117
+ const buf = buffer.Buffer.alloc(1 + hrp.length);
118
+ buf.writeUInt8(hrp.length, 0);
119
+ buf.write(hrp, 1);
120
+ return buf;
121
+ }
122
+ /**
123
+ * get Cosmos address for a given BIP 32 path.
124
+ * @param path a path in BIP 32 format
125
+ * @param hrp usually cosmos
126
+ * @option boolDisplay optionally enable or not the display
127
+ * @return an object with a publicKey, address and (optionally) chainCode
128
+ * @example
129
+ * cosmos.getAddress("44'/60'/0'/0/0", "cosmos").then(o => o.address)
130
+ */
131
+ getAddress(path, hrp, boolDisplay) {
132
+ const bipPath = import_bip32_path.default.fromString(path).toPathArray();
133
+ const serializedPath = this.serializePath(bipPath);
134
+ const data = buffer.Buffer.concat([this.serializeHRP(hrp), serializedPath]);
135
+ return this.transport.send(CLA, INS_GET_ADDR_SECP256K1, boolDisplay ? 1 : 0, 0, data, [SW_OK]).then((response) => {
136
+ return {
137
+ address: buffer.Buffer.from(response.slice(33, -2)).toString(),
138
+ publicKey: buffer.Buffer.from(response.slice(0, 33)).toString("hex")
139
+ };
140
+ });
141
+ }
142
+ foreach(arr, callback) {
143
+ function iterate(index, array, result) {
144
+ if (index >= array.length) return result;
145
+ else return callback(array[index], index).then(function(res) {
146
+ result.push(res);
147
+ return iterate(index + 1, array, result);
148
+ });
149
+ }
150
+ return Promise.resolve().then(() => iterate(0, arr, []));
151
+ }
152
+ async sign(path, message) {
153
+ const bipPath = import_bip32_path.default.fromString(path).toPathArray();
154
+ const serializedPath = this.serializePath(bipPath);
155
+ const chunks = [];
156
+ chunks.push(serializedPath);
157
+ const buffer$1 = buffer.Buffer.from(message);
158
+ for (let i = 0; i < buffer$1.length; i += CHUNK_SIZE) {
159
+ let end = i + CHUNK_SIZE;
160
+ if (i > buffer$1.length) end = buffer$1.length;
161
+ chunks.push(buffer$1.slice(i, end));
162
+ }
163
+ let response = {};
164
+ return this.foreach(chunks, (data, j) => this.transport.send(CLA, INS_SIGN_SECP256K1, j === 0 ? PAYLOAD_TYPE_INIT : j + 1 === chunks.length ? PAYLOAD_TYPE_LAST : PAYLOAD_TYPE_ADD, 0, data, [SW_OK, SW_CANCEL]).then((apduResponse) => response = apduResponse)).then(() => {
165
+ const errorCodeData = response.slice(-2);
166
+ const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
167
+ let signature = null;
168
+ if (response.length > 2) signature = response.slice(0, response.length - 2);
169
+ if (returnCode === 27014) throw new require_lib_es.UserRefusedOnDevice();
170
+ return {
171
+ signature,
172
+ return_code: returnCode
173
+ };
174
+ });
175
+ }
176
+ };
177
+
178
+ //#endregion
179
+ exports.default = Cosmos;