@dydxprotocol/v4-client-js 2.7.0 → 3.0.0

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.
Files changed (76) hide show
  1. package/CHANGELOG.md +7 -2
  2. package/__tests__/modules/client/Transfers.test.ts +1 -1
  3. package/__tests__/modules/client/ValidatorPostEndpoints.test.ts +1 -1
  4. package/build/cjs/__tests__/modules/client/Transfers.test.js +2 -2
  5. package/build/cjs/__tests__/modules/client/ValidatorPostEndpoints.test.js +2 -2
  6. package/build/cjs/examples/batch_cancel_orders_example.js +2 -2
  7. package/build/cjs/examples/composite_example.js +2 -2
  8. package/build/cjs/examples/long_term_order_cancel_example.js +2 -2
  9. package/build/cjs/examples/noble_example.js +3 -2
  10. package/build/cjs/examples/permissioned_keys_example.js +5 -8
  11. package/build/cjs/examples/short_term_order_cancel_example.js +2 -2
  12. package/build/cjs/examples/short_term_order_composite_example.js +2 -2
  13. package/build/cjs/examples/test.js +2 -2
  14. package/build/cjs/examples/transfer_example_deposit.js +2 -2
  15. package/build/cjs/examples/transfer_example_send.js +3 -3
  16. package/build/cjs/examples/transfer_example_subaccount_transfer.js +2 -2
  17. package/build/cjs/examples/transfer_example_withdraw.js +2 -2
  18. package/build/cjs/examples/transfer_example_withdraw_other.js +3 -3
  19. package/build/cjs/examples/validator_post_example.js +2 -2
  20. package/build/cjs/src/clients/composite-client.js +22 -27
  21. package/build/cjs/src/clients/modules/local-wallet.js +12 -1
  22. package/build/cjs/src/clients/modules/post.js +32 -32
  23. package/build/cjs/src/clients/native.js +23 -22
  24. package/build/cjs/src/clients/subaccount.js +19 -10
  25. package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  26. package/build/esm/__tests__/modules/client/Transfers.test.js +2 -2
  27. package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.js +2 -2
  28. package/build/esm/examples/batch_cancel_orders_example.js +2 -2
  29. package/build/esm/examples/composite_example.js +2 -2
  30. package/build/esm/examples/long_term_order_cancel_example.js +2 -2
  31. package/build/esm/examples/noble_example.js +3 -2
  32. package/build/esm/examples/permissioned_keys_example.js +5 -8
  33. package/build/esm/examples/short_term_order_cancel_example.js +2 -2
  34. package/build/esm/examples/short_term_order_composite_example.js +2 -2
  35. package/build/esm/examples/test.js +2 -2
  36. package/build/esm/examples/transfer_example_deposit.js +2 -2
  37. package/build/esm/examples/transfer_example_send.js +3 -3
  38. package/build/esm/examples/transfer_example_subaccount_transfer.js +2 -2
  39. package/build/esm/examples/transfer_example_withdraw.js +2 -2
  40. package/build/esm/examples/transfer_example_withdraw_other.js +3 -3
  41. package/build/esm/examples/validator_post_example.js +2 -2
  42. package/build/esm/src/clients/composite-client.d.ts +4 -8
  43. package/build/esm/src/clients/composite-client.d.ts.map +1 -1
  44. package/build/esm/src/clients/composite-client.js +22 -27
  45. package/build/esm/src/clients/modules/local-wallet.d.ts +2 -0
  46. package/build/esm/src/clients/modules/local-wallet.d.ts.map +1 -1
  47. package/build/esm/src/clients/modules/local-wallet.js +13 -2
  48. package/build/esm/src/clients/modules/post.d.ts +3 -4
  49. package/build/esm/src/clients/modules/post.d.ts.map +1 -1
  50. package/build/esm/src/clients/modules/post.js +32 -32
  51. package/build/esm/src/clients/native.d.ts.map +1 -1
  52. package/build/esm/src/clients/native.js +23 -22
  53. package/build/esm/src/clients/subaccount.d.ts +9 -3
  54. package/build/esm/src/clients/subaccount.d.ts.map +1 -1
  55. package/build/esm/src/clients/subaccount.js +19 -10
  56. package/build/esm/tsconfig.esm.tsbuildinfo +1 -1
  57. package/examples/batch_cancel_orders_example.ts +3 -3
  58. package/examples/composite_example.ts +1 -1
  59. package/examples/long_term_order_cancel_example.ts +1 -1
  60. package/examples/noble_example.ts +2 -1
  61. package/examples/permissioned_keys_example.ts +9 -8
  62. package/examples/short_term_order_cancel_example.ts +1 -1
  63. package/examples/short_term_order_composite_example.ts +1 -1
  64. package/examples/test.ts +1 -1
  65. package/examples/transfer_example_deposit.ts +1 -1
  66. package/examples/transfer_example_send.ts +2 -2
  67. package/examples/transfer_example_subaccount_transfer.ts +1 -1
  68. package/examples/transfer_example_withdraw.ts +1 -1
  69. package/examples/transfer_example_withdraw_other.ts +2 -2
  70. package/examples/validator_post_example.ts +1 -1
  71. package/package.json +1 -1
  72. package/src/clients/composite-client.ts +21 -42
  73. package/src/clients/modules/local-wallet.ts +14 -0
  74. package/src/clients/modules/post.ts +37 -37
  75. package/src/clients/native.ts +39 -39
  76. package/src/clients/subaccount.ts +42 -10
@@ -57,7 +57,7 @@ async function test(): Promise<void> {
57
57
  const client = await CompositeClient.connect(network);
58
58
  console.log('**Client**', client);
59
59
 
60
- const subaccount = new SubaccountInfo(wallet, 0);
60
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
61
61
  const currentBlock = await client.validatorClient.get.latestBlockHeight();
62
62
  const goodTilBlock = currentBlock + 10;
63
63
 
@@ -100,7 +100,7 @@ const placeShortTermOrders = async (
100
100
 
101
101
  // Wait for all order placements to complete
102
102
  await Promise.all(orderPromises);
103
- }
103
+ };
104
104
 
105
105
  const batchCancelOrders = async (
106
106
  client: CompositeClient,
@@ -119,7 +119,7 @@ const batchCancelOrders = async (
119
119
  } catch (error) {
120
120
  console.error('**Batch Cancel Short Term Orders Failed**', error.message);
121
121
  }
122
- }
122
+ };
123
123
 
124
124
  test()
125
125
  .then(() => {
@@ -24,7 +24,7 @@ async function test(): Promise<void> {
24
24
  const client = await CompositeClient.connect(network);
25
25
  console.log('**Client**');
26
26
  console.log(client);
27
- const subaccount = new SubaccountInfo(wallet, 0);
27
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
28
28
  for (const orderParams of ordersParams) {
29
29
  try {
30
30
  const type = OrderType[orderParams.type as keyof typeof OrderType];
@@ -23,7 +23,7 @@ async function test(): Promise<void> {
23
23
  const client = await CompositeClient.connect(network);
24
24
  console.log('**Client**');
25
25
  console.log(client);
26
- const subaccount = new SubaccountInfo(wallet, 0);
26
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
27
27
 
28
28
  /*
29
29
  Note this example places a stateful order.
@@ -1,6 +1,7 @@
1
1
  import { EncodeObject } from '@cosmjs/proto-signing';
2
2
  import Long from 'long';
3
3
 
4
+ import { SubaccountInfo } from '../src';
4
5
  import { Network } from '../src/clients/constants';
5
6
  import LocalWallet from '../src/clients/modules/local-wallet';
6
7
  import { NobleClient } from '../src/clients/noble-client';
@@ -43,7 +44,7 @@ async function test(): Promise<void> {
43
44
  const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
44
45
 
45
46
  await dydxClient.post.send(
46
- dydxWallet,
47
+ SubaccountInfo.forLocalWallet(dydxWallet),
47
48
  () => {
48
49
  return encodeObjects;
49
50
  },
@@ -20,9 +20,9 @@ async function test(): Promise<void> {
20
20
  console.log('**Client**');
21
21
  console.log(client);
22
22
 
23
- const subaccount1 = new SubaccountInfo(wallet1, 0);
24
- const subaccount2 = new SubaccountInfo(wallet2, 0);
25
- const subaccount3 = new SubaccountInfo(wallet3, 0);
23
+ const subaccount1 = SubaccountInfo.forLocalWallet(wallet1, 0);
24
+ const subaccount2 = SubaccountInfo.forLocalWallet(wallet2, 0);
25
+ const subaccount3 = SubaccountInfo.forLocalWallet(wallet3, 0);
26
26
 
27
27
  // Change second wallet pubkey
28
28
  // Add an authenticator to allow wallet2 to place orders
@@ -136,7 +136,12 @@ async function placeOrder(
136
136
  const clientId = Math.floor(Math.random() * 10000);
137
137
 
138
138
  const tx = await client.placeShortTermOrder(
139
- fromAccount,
139
+ SubaccountInfo.forPermissionedWallet(
140
+ fromAccount.signingWallet,
141
+ forAccount.address,
142
+ forAccount.subaccountNumber,
143
+ [authenticatorId],
144
+ ),
140
145
  'ETH-USD',
141
146
  side,
142
147
  price,
@@ -146,10 +151,6 @@ async function placeOrder(
146
151
  timeInForce,
147
152
  false,
148
153
  undefined,
149
- {
150
- authenticators: [authenticatorId],
151
- accountForOrder: forAccount,
152
- },
153
154
  );
154
155
  console.log('**Order Tx**');
155
156
  console.log(Buffer.from(tx.hash).toString('hex'));
@@ -13,7 +13,7 @@ async function test(): Promise<void> {
13
13
  const client = await CompositeClient.connect(network);
14
14
  console.log('**Client**');
15
15
  console.log(client);
16
- const subaccount = new SubaccountInfo(wallet, 0);
16
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
17
17
 
18
18
  const currentBlock = await client.validatorClient.get.latestBlockHeight();
19
19
  const nextValidBlockHeight = currentBlock + 1;
@@ -20,7 +20,7 @@ async function test(): Promise<void> {
20
20
  const client = await CompositeClient.connect(network);
21
21
  console.log('**Client**');
22
22
  console.log(client);
23
- const subaccount = new SubaccountInfo(wallet, 0);
23
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
24
24
  for (const orderParams of ordersParams) {
25
25
  try {
26
26
  const side = OrderSide[orderParams.side as keyof typeof OrderSide];
package/examples/test.ts CHANGED
@@ -24,7 +24,7 @@ async function test(): Promise<void> {
24
24
  const client = await CompositeClient.connect(network);
25
25
  console.log('**Client**');
26
26
  console.log(client);
27
- const subaccount = new SubaccountInfo(wallet, 0);
27
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
28
28
  for (const orderParams of ordersParams) {
29
29
  try {
30
30
  const type = OrderType[orderParams.type as keyof typeof OrderType];
@@ -15,7 +15,7 @@ async function test(): Promise<void> {
15
15
  console.log('**Client**');
16
16
  console.log(client);
17
17
 
18
- const subaccount = new SubaccountInfo(wallet, 0);
18
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
19
19
  const tx = await client.post.deposit(subaccount, 0, new Long(10_000_000));
20
20
  console.log('**Deposit Tx**');
21
21
  console.log(tx);
@@ -18,7 +18,7 @@ async function test(): Promise<void> {
18
18
  console.log('**Client**');
19
19
  console.log(client);
20
20
 
21
- const subaccount = new SubaccountInfo(wallet, 0);
21
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
22
22
 
23
23
  const amount = new Long(100_000_000);
24
24
 
@@ -33,7 +33,7 @@ async function test(): Promise<void> {
33
33
  resolve([msg]);
34
34
  });
35
35
 
36
- const totalFee = await client.post.simulate(subaccount.wallet, () => msgs, undefined, undefined);
36
+ const totalFee = await client.post.simulate(subaccount, () => msgs, undefined, undefined);
37
37
  console.log('**Total Fee**');
38
38
  console.log(totalFee);
39
39
 
@@ -15,7 +15,7 @@ async function test(): Promise<void> {
15
15
  console.log('**Client**');
16
16
  console.log(client);
17
17
 
18
- const subaccount = new SubaccountInfo(wallet, 0);
18
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
19
19
 
20
20
  const tx = await client.post.transfer(subaccount, subaccount.address, 1, 0, new Long(10_000_000));
21
21
  console.log('**Transfer Tx**');
@@ -17,7 +17,7 @@ async function test(): Promise<void> {
17
17
  console.log('**Client**');
18
18
  console.log(client);
19
19
 
20
- const subaccount = new SubaccountInfo(wallet, 0);
20
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
21
21
 
22
22
  const tx = await client.post.withdraw(subaccount, 0, new Long(1_00_000_000));
23
23
  console.log('**Withdraw Tx**');
@@ -18,7 +18,7 @@ async function test(): Promise<void> {
18
18
  console.log('**Client**');
19
19
  console.log(client);
20
20
 
21
- const subaccount = new SubaccountInfo(wallet, 0);
21
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
22
22
 
23
23
  const amount = new Long(100_000_000);
24
24
 
@@ -34,7 +34,7 @@ async function test(): Promise<void> {
34
34
  resolve([msg]);
35
35
  });
36
36
 
37
- const totalFee = await client.post.simulate(subaccount.wallet, () => msgs, undefined);
37
+ const totalFee = await client.post.simulate(subaccount, () => msgs, undefined);
38
38
  console.log('**Total Fee**');
39
39
  console.log(totalFee);
40
40
 
@@ -40,7 +40,7 @@ async function test(): Promise<void> {
40
40
  const value1 = Long.fromNumber(400000000000);
41
41
  console.log(value1.toString());
42
42
 
43
- const subaccount = new SubaccountInfo(wallet, 0);
43
+ const subaccount = SubaccountInfo.forLocalWallet(wallet, 0);
44
44
  for (const orderParams of ordersParams) {
45
45
  const height = await client.get.latestBlockHeight();
46
46
  const placeOrder = dummyOrder(height);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dydxprotocol/v4-client-js",
3
- "version": "2.7.0",
3
+ "version": "3.0.0",
4
4
  "description": "General client library for the new dYdX system (v4 decentralized)",
5
5
  "main": "build/cjs/src/index.js",
6
6
  "module": "build/esm/src/index.js",
@@ -69,11 +69,6 @@ export interface OrderBatchWithMarketId {
69
69
  clientIds: number[];
70
70
  }
71
71
 
72
- export interface PermissionedKeysAccountAuth {
73
- authenticators: Long[];
74
- accountForOrder: SubaccountInfo;
75
- }
76
-
77
72
  export type PlaceOrderPayload = {
78
73
  subaccountNumber: number;
79
74
  marketId: string;
@@ -167,14 +162,14 @@ export class CompositeClient {
167
162
  * @returns The Signature.
168
163
  */
169
164
  async sign(
170
- wallet: LocalWallet,
165
+ subaccount: SubaccountInfo,
171
166
  messaging: () => Promise<EncodeObject[]>,
172
167
  zeroFee: boolean,
173
168
  gasPrice?: GasPrice,
174
169
  memo?: string,
175
170
  account?: () => Promise<Account>,
176
171
  ): Promise<Uint8Array> {
177
- return this.validatorClient.post.sign(wallet, messaging, zeroFee, gasPrice, memo, account);
172
+ return this.validatorClient.post.sign(subaccount, messaging, zeroFee, gasPrice, memo, account);
178
173
  }
179
174
 
180
175
  /**
@@ -186,17 +181,16 @@ export class CompositeClient {
186
181
  * @returns The Transaction Hash.
187
182
  */
188
183
  async send(
189
- wallet: LocalWallet,
184
+ subaccount: SubaccountInfo,
190
185
  messaging: () => Promise<EncodeObject[]>,
191
186
  zeroFee: boolean,
192
187
  gasPrice?: GasPrice,
193
188
  memo?: string,
194
189
  broadcastMode?: BroadcastMode,
195
190
  account?: () => Promise<Account>,
196
- authenticators?: Long[],
197
191
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
198
192
  return this.validatorClient.post.send(
199
- wallet,
193
+ subaccount,
200
194
  messaging,
201
195
  zeroFee,
202
196
  gasPrice,
@@ -204,7 +198,6 @@ export class CompositeClient {
204
198
  broadcastMode,
205
199
  account,
206
200
  undefined,
207
- authenticators,
208
201
  );
209
202
  }
210
203
 
@@ -237,13 +230,13 @@ export class CompositeClient {
237
230
  * @returns The gas estimate.
238
231
  */
239
232
  async simulate(
240
- wallet: LocalWallet,
233
+ subaccount: SubaccountInfo,
241
234
  messaging: () => Promise<EncodeObject[]>,
242
235
  gasPrice?: GasPrice,
243
236
  memo?: string,
244
237
  account?: () => Promise<Account>,
245
238
  ): Promise<StdFee> {
246
- return this.validatorClient.post.simulate(wallet, messaging, gasPrice, memo, account);
239
+ return this.validatorClient.post.simulate(subaccount, messaging, gasPrice, memo, account);
247
240
  }
248
241
 
249
242
  /**
@@ -342,16 +335,10 @@ export class CompositeClient {
342
335
  timeInForce: Order_TimeInForce,
343
336
  reduceOnly: boolean,
344
337
  memo?: string,
345
- permissionedKeysAccountAuth?: PermissionedKeysAccountAuth,
346
338
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
347
- // For permissioned orders, use the permissioning account details instead of the subaccount
348
- // This allows placing orders on behalf of another account when using permissioned keys
349
- const accountForOrder = permissionedKeysAccountAuth
350
- ? permissionedKeysAccountAuth.accountForOrder
351
- : subaccount;
352
339
  const msgs: Promise<EncodeObject[]> = new Promise((resolve, reject) => {
353
340
  const msg = this.placeShortTermOrderMessage(
354
- accountForOrder,
341
+ subaccount,
355
342
  marketId,
356
343
  side,
357
344
  price,
@@ -371,18 +358,17 @@ export class CompositeClient {
371
358
  });
372
359
  });
373
360
  const account: Promise<Account> = this.validatorClient.post.account(
374
- accountForOrder.address,
361
+ subaccount.address,
375
362
  undefined,
376
363
  );
377
364
  return this.send(
378
- subaccount.wallet,
365
+ subaccount,
379
366
  () => msgs,
380
367
  true,
381
368
  undefined,
382
369
  memo,
383
370
  undefined,
384
371
  () => account,
385
- permissionedKeysAccountAuth?.authenticators,
386
372
  );
387
373
  }
388
374
 
@@ -467,7 +453,7 @@ export class CompositeClient {
467
453
  orderFlags,
468
454
  );
469
455
  return this.send(
470
- subaccount.wallet,
456
+ subaccount,
471
457
  () => msgs,
472
458
  true,
473
459
  undefined,
@@ -846,7 +832,7 @@ export class CompositeClient {
846
832
  resolve([msg]);
847
833
  });
848
834
  return this.send(
849
- subaccount.wallet,
835
+ subaccount,
850
836
  () => msgs,
851
837
  false,
852
838
  undefined,
@@ -915,7 +901,7 @@ export class CompositeClient {
915
901
  const msg = this.depositToSubaccountMessage(subaccount, amount);
916
902
  resolve([msg]);
917
903
  });
918
- return this.validatorClient.post.send(subaccount.wallet, () => msgs, false, undefined, memo);
904
+ return this.validatorClient.post.send(subaccount, () => msgs, false, undefined, memo);
919
905
  }
920
906
 
921
907
  /**
@@ -970,7 +956,7 @@ export class CompositeClient {
970
956
  const msg = this.withdrawFromSubaccountMessage(subaccount, amount, recipient);
971
957
  resolve([msg]);
972
958
  });
973
- return this.send(subaccount.wallet, () => msgs, false, undefined, memo);
959
+ return this.send(subaccount, () => msgs, false, undefined, memo);
974
960
  }
975
961
 
976
962
  /**
@@ -1082,7 +1068,7 @@ export class CompositeClient {
1082
1068
  console.log(err);
1083
1069
  });
1084
1070
  });
1085
- const signature = await this.sign(subaccount.wallet, () => msgs, true);
1071
+ const signature = await this.sign(subaccount, () => msgs, true);
1086
1072
 
1087
1073
  return Buffer.from(signature).toString('base64');
1088
1074
  }
@@ -1107,7 +1093,7 @@ export class CompositeClient {
1107
1093
  );
1108
1094
  resolve([msg]);
1109
1095
  });
1110
- const signature = await this.sign(subaccount.wallet, () => msgs, true);
1096
+ const signature = await this.sign(subaccount, () => msgs, true);
1111
1097
 
1112
1098
  return Buffer.from(signature).toString('base64');
1113
1099
  }
@@ -1142,10 +1128,7 @@ export class CompositeClient {
1142
1128
 
1143
1129
  const msgs: Promise<EncodeObject[]> = (async () => {
1144
1130
  const cancelMsgPromises = cancelOrderPayloads.map(async (cancelPayload) => {
1145
- const cancelSubaccount = new SubaccountInfo(
1146
- subaccount.wallet,
1147
- cancelPayload.subaccountNumber,
1148
- );
1131
+ const cancelSubaccount = subaccount.cloneWithSubaccount(cancelPayload.subaccountNumber);
1149
1132
  return this.validatorClient.post.cancelOrderMsg(
1150
1133
  cancelSubaccount.address,
1151
1134
  cancelSubaccount.subaccountNumber,
@@ -1161,9 +1144,8 @@ export class CompositeClient {
1161
1144
  if (transferToSubaccountPayload == null) {
1162
1145
  return undefined;
1163
1146
  }
1164
- const transferSubaccount = new SubaccountInfo(
1165
- subaccount.wallet,
1166
- transferToSubaccountPayload?.sourceSubaccountNumber,
1147
+ const transferSubaccount = subaccount.cloneWithSubaccount(
1148
+ transferToSubaccountPayload.sourceSubaccountNumber,
1167
1149
  );
1168
1150
  return this.transferToSubaccountMessage(
1169
1151
  transferSubaccount,
@@ -1174,10 +1156,7 @@ export class CompositeClient {
1174
1156
  })();
1175
1157
 
1176
1158
  const placeOrderMsgPromises = placeOrderPayloads.map((placePayload) => {
1177
- const placeSubaccount = new SubaccountInfo(
1178
- subaccount.wallet,
1179
- placePayload.subaccountNumber,
1180
- );
1159
+ const placeSubaccount = subaccount.cloneWithSubaccount(placePayload.subaccountNumber);
1181
1160
  return this.placeOrderMessage(
1182
1161
  placeSubaccount,
1183
1162
  placePayload.marketId,
@@ -1206,7 +1185,7 @@ export class CompositeClient {
1206
1185
  })();
1207
1186
 
1208
1187
  return this.send(
1209
- subaccount.wallet,
1188
+ subaccount,
1210
1189
  () => msgs,
1211
1190
  true,
1212
1191
  undefined,
@@ -1362,7 +1341,7 @@ export class CompositeClient {
1362
1341
  resolve([submitProposal]);
1363
1342
  });
1364
1343
 
1365
- return this.send(wallet, () => msg, false, undefined, memo);
1344
+ return this.send(SubaccountInfo.forLocalWallet(wallet), () => msg, false, undefined, memo);
1366
1345
  }
1367
1346
 
1368
1347
  async createMarketPermissionless(
@@ -1,7 +1,9 @@
1
1
  import { Secp256k1Pubkey, StdFee, encodeSecp256k1Pubkey } from '@cosmjs/amino';
2
+ import { fromHex } from '@cosmjs/encoding';
2
3
  import {
3
4
  AccountData,
4
5
  DirectSecp256k1HdWallet,
6
+ DirectSecp256k1Wallet,
5
7
  EncodeObject,
6
8
  OfflineSigner,
7
9
  } from '@cosmjs/proto-signing';
@@ -9,6 +11,7 @@ import { SigningStargateClient } from '@cosmjs/stargate';
9
11
  import Long from 'long';
10
12
  import protobuf from 'protobufjs';
11
13
 
14
+ import { stripHexPrefix } from '../../lib/helpers';
12
15
  import { generateRegistry } from '../lib/registry';
13
16
  import { TransactionOptions } from '../types';
14
17
  import { TransactionSigner } from './signer';
@@ -36,6 +39,12 @@ export default class LocalWallet {
36
39
  return wallet;
37
40
  }
38
41
 
42
+ static async fromPrivateKey(pkHex: string, prefix?: string): Promise<LocalWallet> {
43
+ const wallet = new LocalWallet();
44
+ await wallet.setPrivateKey(pkHex, prefix);
45
+ return wallet;
46
+ }
47
+
39
48
  async setSigner(signer: OfflineSigner): Promise<void> {
40
49
  this.offlineSigner = signer;
41
50
  const stargateClient = await SigningStargateClient.offline(signer, {
@@ -54,6 +63,11 @@ export default class LocalWallet {
54
63
  return this.setSigner(signer);
55
64
  }
56
65
 
66
+ async setPrivateKey(pkHex: string, prefix?: string): Promise<void> {
67
+ const signer = await DirectSecp256k1Wallet.fromKey(fromHex(stripHexPrefix(pkHex)), prefix);
68
+ return this.setSigner(signer);
69
+ }
70
+
57
71
  public async signTransaction(
58
72
  messages: EncodeObject[],
59
73
  transactionOptions: TransactionOptions,