@dydxprotocol/v4-client-js 1.3.1 → 1.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dydxprotocol/v4-client-js",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "General client library for the new dYdX system (v4 decentralized)",
5
5
  "main": "build/src/index.js",
6
6
  "scripts": {
@@ -36,7 +36,6 @@
36
36
  "@cosmjs/stargate": "^0.32.1",
37
37
  "@cosmjs/tendermint-rpc": "^0.32.1",
38
38
  "@cosmjs/utils": "^0.32.1",
39
- "@dydxprotocol/v4-proto": "6.0.1",
40
39
  "@osmonauts/lcd": "^0.6.0",
41
40
  "@scure/bip32": "^1.1.5",
42
41
  "@scure/bip39": "^1.1.1",
@@ -78,5 +77,8 @@
78
77
  "util": "^0.12.5",
79
78
  "webpack": "^5.77.0",
80
79
  "webpack-cli": "^5.0.1"
80
+ },
81
+ "peerDependencies": {
82
+ "@dydxprotocol/v4-proto": "6.0.1"
81
83
  }
82
84
  }
@@ -16,7 +16,7 @@ import Long from 'long';
16
16
  import { BECH32_PREFIX, GAS_MULTIPLIER, NOBLE_BECH32_PREFIX } from '../lib/constants';
17
17
  import { UserError } from '../lib/errors';
18
18
  import { ByteArrayEncoding, encodeJson } from '../lib/helpers';
19
- import { deriveHDKeyFromEthereumSignature } from '../lib/onboarding';
19
+ import { deriveHDKeyFromEthereumSignature, deriveHDKeyFromMnemonic } from '../lib/onboarding';
20
20
  import { NetworkOptimizer } from '../network_optimizer';
21
21
  import { CompositeClient, MarketInfo } from './composite-client';
22
22
  import {
@@ -147,6 +147,12 @@ export async function connectWallet(mnemonic: string): Promise<string> {
147
147
  globalThis.wallet = await LocalWallet.fromMnemonic(mnemonic, BECH32_PREFIX);
148
148
  globalThis.nobleWallet = await LocalWallet.fromMnemonic(mnemonic, NOBLE_BECH32_PREFIX);
149
149
 
150
+ const { privateKey, publicKey } = deriveHDKeyFromMnemonic(mnemonic);
151
+ globalThis.hdKey = {
152
+ privateKey,
153
+ publicKey,
154
+ };
155
+
150
156
  try {
151
157
  await globalThis.nobleClient?.connect(globalThis.nobleWallet);
152
158
  } catch (e) {
@@ -1306,6 +1312,34 @@ export async function signCompliancePayload(payload: string): Promise<string> {
1306
1312
  }
1307
1313
  }
1308
1314
 
1315
+ export async function signPushNotificationTokenRegistrationPayload(payload: string): Promise<string> {
1316
+ try {
1317
+ const json = JSON.parse(payload);
1318
+ const message = json.message;
1319
+ if (message === undefined) {
1320
+ throw new UserError('message is not set');
1321
+ }
1322
+ if (!globalThis.hdKey?.privateKey || !globalThis.hdKey?.publicKey) {
1323
+ throw new Error('Missing hdKey');
1324
+ }
1325
+
1326
+ const timestampInSeconds = Math.floor(Date.now() / 1000);
1327
+ const messageToSign: string = `${message}:REGISTER_TOKEN"${''}:${timestampInSeconds}`;
1328
+ const messageHash = sha256(Buffer.from(messageToSign));
1329
+
1330
+ const signed = await Secp256k1.createSignature(messageHash, globalThis.hdKey.privateKey);
1331
+ const signedMessage = signed.toFixedLength();
1332
+
1333
+ return encodeJson({
1334
+ signedMessage: Buffer.from(signedMessage).toString('base64'),
1335
+ publicKey: Buffer.from(globalThis.hdKey.publicKey).toString('base64'),
1336
+ timestamp: timestampInSeconds,
1337
+ });
1338
+ } catch (error) {
1339
+ return wrappedError(error);
1340
+ }
1341
+ }
1342
+
1309
1343
  export async function setSelectedGasDenom(gasDenom: string): Promise<string> {
1310
1344
  try {
1311
1345
  const client = globalThis.client;
@@ -27,19 +27,11 @@ export const exportMnemonicAndPrivateKey = (
27
27
  publicKey: Uint8Array | null;
28
28
  } => {
29
29
  const mnemonic = entropyToMnemonic(entropy, wordlist);
30
- const seed = mnemonicToSeedSync(mnemonic);
31
-
32
- const hdkey = HDKey.fromMasterSeed(seed);
33
- const derivedHdkey = hdkey.derive(path);
34
-
35
- if (!hdkey.privateKey) {
36
- throw new Error('null hd key');
37
- }
38
-
30
+ const { privateKey, publicKey } = deriveHDKeyFromMnemonic(mnemonic, path);
39
31
  return {
40
32
  mnemonic,
41
- privateKey: derivedHdkey.privateKey,
42
- publicKey: derivedHdkey.publicKey,
33
+ privateKey,
34
+ publicKey,
43
35
  };
44
36
  };
45
37
 
@@ -67,3 +59,38 @@ export const deriveHDKeyFromEthereumSignature = (
67
59
  const entropy = keccak256(rsValues);
68
60
  return exportMnemonicAndPrivateKey(entropy);
69
61
  };
62
+
63
+ /**
64
+ * @description Derive priv/pub keys from mnemonic and BIP44 HD path
65
+ *
66
+ * @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
67
+ *
68
+ * @param mnemonic used to generate seed
69
+ *
70
+ * @param path BIP44 HD Path. Default is The Cosmos Hub path
71
+ *
72
+ * @throws Error if the hdkey does not exist
73
+ *
74
+ * @returns Priv/pub keys
75
+ */
76
+ export const deriveHDKeyFromMnemonic = (
77
+ mnemonic: string,
78
+ path: string = "m/44'/118'/0'/0/0",
79
+ ): {
80
+ privateKey: Uint8Array | null;
81
+ publicKey: Uint8Array | null;
82
+ } => {
83
+ const seed = mnemonicToSeedSync(mnemonic);
84
+
85
+ const hdkey = HDKey.fromMasterSeed(seed);
86
+ const derivedHdkey = hdkey.derive(path);
87
+
88
+ if (!hdkey.privateKey) {
89
+ throw new Error('null hd key');
90
+ }
91
+
92
+ return {
93
+ privateKey: derivedHdkey.privateKey,
94
+ publicKey: derivedHdkey.publicKey,
95
+ };
96
+ };