carbon-js-sdk 0.3.27-beta.1 → 0.3.28
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.
|
@@ -16,6 +16,8 @@ declare class CosmosLedger {
|
|
|
16
16
|
getOpenApp(): Promise<any>;
|
|
17
17
|
getPubKey(): Promise<any>;
|
|
18
18
|
getCosmosAddress(): Promise<string>;
|
|
19
|
+
generateCosmosAddress(hdPath: Array<number>): Promise<string>;
|
|
20
|
+
changeBIP44Path(hdPath: Array<number>, hrp: string): Promise<void>;
|
|
19
21
|
confirmLedgerAddress(): Promise<void>;
|
|
20
22
|
sign(signMessage: string): Promise<Uint8Array>;
|
|
21
23
|
private checkLedgerErrors;
|
|
@@ -38,12 +38,12 @@ exports.checkAppMode = void 0;
|
|
|
38
38
|
// original implementation: https://github.com/luniehq/cosmos-ledger
|
|
39
39
|
const ledger_cosmos_js_1 = __importDefault(require("ledger-cosmos-js"));
|
|
40
40
|
const secp256k1_1 = require("secp256k1");
|
|
41
|
-
const semver = require(
|
|
41
|
+
const semver = require("semver");
|
|
42
42
|
const crypto = __importStar(require("crypto"));
|
|
43
43
|
const ripemd160_1 = __importDefault(require("ripemd160"));
|
|
44
44
|
const bech32 = __importStar(require("bech32"));
|
|
45
45
|
const INTERACTION_TIMEOUT = 120; // seconds to wait for user action on Ledger, currently is always limited to 60
|
|
46
|
-
const REQUIRED_COSMOS_APP_VERSION =
|
|
46
|
+
const REQUIRED_COSMOS_APP_VERSION = "1.5.3";
|
|
47
47
|
/*
|
|
48
48
|
HD wallet derivation path (BIP44)
|
|
49
49
|
DerivationPath{44, 118, account, 0, index}
|
|
@@ -98,35 +98,33 @@ class CosmosLedger {
|
|
|
98
98
|
if (!navigator.hid) {
|
|
99
99
|
throw new Error(`Your browser doesn't have HID enabled. Please enable this feature by visiting: chrome://flags/#enable-experimental-web-platform-features`);
|
|
100
100
|
}
|
|
101
|
-
const { default: TransportWebHID } = yield Promise.resolve().then(() => __importStar(require(
|
|
102
|
-
/* webpackChunkName: "webhid" */ '@ledgerhq/hw-transport-webhid')));
|
|
101
|
+
const { default: TransportWebHID } = yield Promise.resolve().then(() => __importStar(require(/* webpackChunkName: "webhid" */ "@ledgerhq/hw-transport-webhid")));
|
|
103
102
|
transport = yield TransportWebHID.create(timeout * 1000);
|
|
104
103
|
}
|
|
105
104
|
// OSX / Linux
|
|
106
105
|
else {
|
|
107
106
|
try {
|
|
108
|
-
const { default: TransportWebUSB } = yield Promise.resolve().then(() => __importStar(require(
|
|
109
|
-
/* webpackChunkName: "webusb" */ '@ledgerhq/hw-transport-webusb')));
|
|
107
|
+
const { default: TransportWebUSB } = yield Promise.resolve().then(() => __importStar(require(/* webpackChunkName: "webusb" */ "@ledgerhq/hw-transport-webusb")));
|
|
110
108
|
transport = yield TransportWebUSB.create(timeout * 1000);
|
|
111
109
|
}
|
|
112
110
|
catch (err) {
|
|
113
111
|
if (err instanceof Error) {
|
|
114
112
|
/* istanbul ignore next: specific error rewrite */
|
|
115
|
-
if (err.message.trim().startsWith(
|
|
113
|
+
if (err.message.trim().startsWith("No WebUSB interface found for your Ledger device")) {
|
|
116
114
|
throw new Error("Couldn't connect to a Ledger device. Please use Ledger Live to upgrade the Ledger firmware to version 1.5.5 or later.");
|
|
117
115
|
}
|
|
118
116
|
/* istanbul ignore next: specific error rewrite */
|
|
119
|
-
if (err.message.trim().startsWith(
|
|
117
|
+
if (err.message.trim().startsWith("Unable to claim interface")) {
|
|
120
118
|
// apparently can't use it in several tabs in parallel
|
|
121
|
-
throw new Error(
|
|
119
|
+
throw new Error("Could not access Ledger device. Is it being used in another tab?");
|
|
122
120
|
}
|
|
123
121
|
/* istanbul ignore next: specific error rewrite */
|
|
124
|
-
if (err.message.trim().startsWith(
|
|
122
|
+
if (err.message.trim().startsWith("Not supported")) {
|
|
125
123
|
// apparently can't use it in several tabs in parallel
|
|
126
124
|
throw new Error("Your browser doesn't seem to support WebUSB yet. Try updating it to the latest version.");
|
|
127
125
|
}
|
|
128
126
|
/* istanbul ignore next: specific error rewrite */
|
|
129
|
-
if (err.message.trim().startsWith(
|
|
127
|
+
if (err.message.trim().startsWith("No device selected")) {
|
|
130
128
|
// apparently can't use it in several tabs in parallel
|
|
131
129
|
throw new Error("You did not select a Ledger device. If you didn't see your Ledger, check if the Ledger is plugged in and unlocked.");
|
|
132
130
|
}
|
|
@@ -194,6 +192,28 @@ class CosmosLedger {
|
|
|
194
192
|
return res;
|
|
195
193
|
});
|
|
196
194
|
}
|
|
195
|
+
generateCosmosAddress(hdPath) {
|
|
196
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
197
|
+
yield this.connect();
|
|
198
|
+
const pubKey = yield this.cosmosApp.publicKey(hdPath);
|
|
199
|
+
this.checkLedgerErrors(pubKey);
|
|
200
|
+
const compressedPk = pubKey.compressed_pk;
|
|
201
|
+
const response = yield getBech32FromPK(this.hrp, compressedPk);
|
|
202
|
+
return response;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
changeBIP44Path(hdPath, hrp) {
|
|
206
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
207
|
+
try {
|
|
208
|
+
this.hdPath = hdPath;
|
|
209
|
+
this.hrp = hrp;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
throw new Error(`error`);
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
});
|
|
216
|
+
}
|
|
197
217
|
// triggers a confirmation request of the cosmos address on the Ledger device
|
|
198
218
|
confirmLedgerAddress() {
|
|
199
219
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -205,7 +225,7 @@ class CosmosLedger {
|
|
|
205
225
|
}
|
|
206
226
|
const response = yield this.cosmosApp.showAddressAndPubKey(this.hdPath, this.hrp);
|
|
207
227
|
this.checkLedgerErrors(response, {
|
|
208
|
-
rejectionMessage:
|
|
228
|
+
rejectionMessage: "Displayed address was rejected",
|
|
209
229
|
});
|
|
210
230
|
});
|
|
211
231
|
}
|
|
@@ -224,7 +244,7 @@ class CosmosLedger {
|
|
|
224
244
|
}
|
|
225
245
|
// parse Ledger errors in a more user friendly format
|
|
226
246
|
/* istanbul ignore next: maps a bunch of errors */
|
|
227
|
-
checkLedgerErrors({ error_message, device_locked }, { timeoutMessag =
|
|
247
|
+
checkLedgerErrors({ error_message, device_locked }, { timeoutMessag = "Connection timed out. Please try again.", rejectionMessage = "User rejected the transaction" } = {}) {
|
|
228
248
|
if (device_locked) {
|
|
229
249
|
throw new Error(`Ledger's screensaver mode is on`);
|
|
230
250
|
}
|
|
@@ -240,8 +260,7 @@ class CosmosLedger {
|
|
|
240
260
|
case `Unknown Status Code: 26628`:
|
|
241
261
|
throw new Error(`Ledger's screensaver mode is on`);
|
|
242
262
|
case `Instruction not supported`:
|
|
243
|
-
throw new Error(`Your Cosmos Ledger App is not up to date.
|
|
244
|
-
`Please update to version ${REQUIRED_COSMOS_APP_VERSION}.`);
|
|
263
|
+
throw new Error(`Your Cosmos Ledger App is not up to date. Please update to version ${REQUIRED_COSMOS_APP_VERSION}.`);
|
|
245
264
|
case `No errors`:
|
|
246
265
|
// do nothing
|
|
247
266
|
break;
|
|
@@ -264,17 +283,14 @@ exports.checkAppMode = checkAppMode;
|
|
|
264
283
|
// doesn't properly work in ledger-cosmos-js
|
|
265
284
|
function getBech32FromPK(hrp, pk) {
|
|
266
285
|
if (pk.length !== 33) {
|
|
267
|
-
throw new Error(
|
|
286
|
+
throw new Error("expected compressed public key [31 bytes]");
|
|
268
287
|
}
|
|
269
|
-
const hashSha256 = crypto
|
|
270
|
-
.createHash('sha256')
|
|
271
|
-
.update(pk)
|
|
272
|
-
.digest();
|
|
288
|
+
const hashSha256 = crypto.createHash("sha256").update(pk).digest();
|
|
273
289
|
const hashRip = new ripemd160_1.default().update(hashSha256).digest();
|
|
274
290
|
return bech32.encode(hrp, bech32.toWords(hashRip));
|
|
275
291
|
}
|
|
276
292
|
function isWindows(platform) {
|
|
277
|
-
return platform.indexOf(
|
|
293
|
+
return platform.indexOf("Win") > -1;
|
|
278
294
|
}
|
|
279
295
|
function getBrowser(userAgent) {
|
|
280
296
|
var _a;
|
|
@@ -285,8 +301,8 @@ function getBrowser(userAgent) {
|
|
|
285
301
|
throw new Error("Your browser doesn't support Ledger devices.");
|
|
286
302
|
}
|
|
287
303
|
if (isBrave)
|
|
288
|
-
return
|
|
304
|
+
return "brave";
|
|
289
305
|
else
|
|
290
|
-
return
|
|
306
|
+
return "chrome";
|
|
291
307
|
}
|
|
292
308
|
exports.default = CosmosLedger;
|