@dydxprotocol/v4-client-js 3.5.0 → 3.6.1
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/.nvmrc +1 -1
- package/README.md +3 -3
- package/__native__/__ios__/v4-native-client.js +382 -29
- package/__tests__/clients/composite-client.test.ts +1 -1
- package/build/cjs/__tests__/clients/composite-client.test.js +1 -1
- package/build/cjs/src/clients/composite-client.js +4 -2
- package/build/cjs/src/clients/constants.js +2 -2
- package/build/cjs/src/clients/modules/eth-secp256k1.js +57 -0
- package/build/cjs/src/clients/modules/local-wallet.js +11 -1
- package/build/cjs/src/index.js +4 -2
- package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/build/esm/__tests__/clients/composite-client.test.js +1 -1
- package/build/esm/src/clients/composite-client.d.ts.map +1 -1
- package/build/esm/src/clients/composite-client.js +4 -2
- package/build/esm/src/clients/constants.d.ts +1 -1
- package/build/esm/src/clients/constants.d.ts.map +1 -1
- package/build/esm/src/clients/constants.js +2 -2
- package/build/esm/src/clients/modules/eth-secp256k1.d.ts +18 -0
- package/build/esm/src/clients/modules/eth-secp256k1.d.ts.map +1 -0
- package/build/esm/src/clients/modules/eth-secp256k1.js +53 -0
- package/build/esm/src/clients/modules/local-wallet.d.ts +5 -0
- package/build/esm/src/clients/modules/local-wallet.d.ts.map +1 -1
- package/build/esm/src/clients/modules/local-wallet.js +11 -1
- package/build/esm/src/index.d.ts +1 -0
- package/build/esm/src/index.d.ts.map +1 -1
- package/build/esm/src/index.js +2 -1
- package/build/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/src/clients/composite-client.ts +5 -1
- package/src/clients/constants.ts +1 -1
- package/src/clients/modules/eth-secp256k1.ts +83 -0
- package/src/clients/modules/local-wallet.ts +15 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dydxprotocol/v4-client-js",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.1",
|
|
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",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"homepage": "https://github.com/dydxprotocol/v4-clients#readme",
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@cosmjs/amino": "^0.32.1",
|
|
49
|
+
"@cosmjs/crypto": "^0.32.1",
|
|
49
50
|
"@cosmjs/encoding": "^0.32.1",
|
|
50
51
|
"@cosmjs/math": "^0.32.1",
|
|
51
52
|
"@cosmjs/proto-signing": "^0.32.1",
|
|
@@ -550,7 +550,11 @@ export class CompositeClient {
|
|
|
550
550
|
);
|
|
551
551
|
const orderTimeInForce = calculateTimeInForce(type, timeInForce, execution, postOnly);
|
|
552
552
|
let goodTilBlockTime = 0;
|
|
553
|
-
if (
|
|
553
|
+
if (
|
|
554
|
+
orderFlags === OrderFlags.LONG_TERM ||
|
|
555
|
+
orderFlags === OrderFlags.CONDITIONAL ||
|
|
556
|
+
orderFlags === OrderFlags.TWAP
|
|
557
|
+
) {
|
|
554
558
|
if (goodTilTimeInSeconds == null) {
|
|
555
559
|
throw new Error('goodTilTimeInSeconds must be set for LONG_TERM or CONDITIONAL order');
|
|
556
560
|
} else {
|
package/src/clients/constants.ts
CHANGED
|
@@ -43,7 +43,7 @@ export enum FaucetApiHost {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export enum ValidatorApiHost {
|
|
46
|
-
TESTNET = 'https://
|
|
46
|
+
TESTNET = 'https://dydx-testnet-rpc.polkachu.com',
|
|
47
47
|
STAGING = 'https://validator.v4staging.dydx.exchange',
|
|
48
48
|
LOCAL = 'http://localhost:26657',
|
|
49
49
|
// For the deployment by DYDX token holders
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Bip39,
|
|
3
|
+
EnglishMnemonic,
|
|
4
|
+
keccak256,
|
|
5
|
+
Secp256k1,
|
|
6
|
+
Slip10,
|
|
7
|
+
Slip10Curve,
|
|
8
|
+
stringToPath,
|
|
9
|
+
} from '@cosmjs/crypto';
|
|
10
|
+
import { toBase64, toBech32 } from '@cosmjs/encoding';
|
|
11
|
+
import {
|
|
12
|
+
AccountData,
|
|
13
|
+
DirectSignResponse,
|
|
14
|
+
makeSignBytes,
|
|
15
|
+
OfflineDirectSigner,
|
|
16
|
+
} from '@cosmjs/proto-signing';
|
|
17
|
+
import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
|
|
18
|
+
|
|
19
|
+
export const ETH_SECP256K1_HD_PATH = "m/44'/60'/0'/0/0";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* OfflineDirectSigner for `eth_secp256k1` keys (e.g. Injective).
|
|
23
|
+
*/
|
|
24
|
+
export class DirectEthSecp256k1Wallet implements OfflineDirectSigner {
|
|
25
|
+
private readonly privkey: Uint8Array;
|
|
26
|
+
private readonly pubkey: Uint8Array;
|
|
27
|
+
private readonly prefix: string;
|
|
28
|
+
|
|
29
|
+
private constructor(privkey: Uint8Array, compressedPubkey: Uint8Array, prefix: string) {
|
|
30
|
+
this.privkey = privkey;
|
|
31
|
+
this.pubkey = compressedPubkey;
|
|
32
|
+
this.prefix = prefix;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static async fromKey(privkey: Uint8Array, prefix: string): Promise<DirectEthSecp256k1Wallet> {
|
|
36
|
+
const { pubkey } = await Secp256k1.makeKeypair(privkey);
|
|
37
|
+
return new DirectEthSecp256k1Wallet(privkey, Secp256k1.compressPubkey(pubkey), prefix);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static async fromMnemonic(
|
|
41
|
+
mnemonic: string,
|
|
42
|
+
prefix: string,
|
|
43
|
+
hdPath: string = ETH_SECP256K1_HD_PATH,
|
|
44
|
+
): Promise<DirectEthSecp256k1Wallet> {
|
|
45
|
+
const seed = await Bip39.mnemonicToSeed(new EnglishMnemonic(mnemonic));
|
|
46
|
+
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, stringToPath(hdPath));
|
|
47
|
+
return DirectEthSecp256k1Wallet.fromKey(privkey, prefix);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private get address(): string {
|
|
51
|
+
const uncompressed = Secp256k1.uncompressPubkey(this.pubkey).slice(1);
|
|
52
|
+
return toBech32(this.prefix, keccak256(uncompressed).slice(-20));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getAccounts(): Promise<readonly AccountData[]> {
|
|
56
|
+
return Promise.resolve([
|
|
57
|
+
{
|
|
58
|
+
// Not part of cosmjs's `Algo` union
|
|
59
|
+
algo: 'eth_secp256k1' as AccountData['algo'],
|
|
60
|
+
address: this.address,
|
|
61
|
+
pubkey: this.pubkey,
|
|
62
|
+
},
|
|
63
|
+
]);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async signDirect(signerAddress: string, signDoc: SignDoc): Promise<DirectSignResponse> {
|
|
67
|
+
if (signerAddress !== this.address) {
|
|
68
|
+
throw new Error(`Address ${signerAddress} not found in wallet`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const hash = keccak256(makeSignBytes(signDoc));
|
|
72
|
+
const signature = await Secp256k1.createSignature(hash, this.privkey);
|
|
73
|
+
const signatureBytes = new Uint8Array([...signature.r(32), ...signature.s(32)]);
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
signed: signDoc,
|
|
77
|
+
signature: {
|
|
78
|
+
pub_key: { type: 'injective/PubKeyEthSecp256k1', value: toBase64(this.pubkey) },
|
|
79
|
+
signature: toBase64(signatureBytes),
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -14,6 +14,7 @@ import protobuf from 'protobufjs';
|
|
|
14
14
|
import { stripHexPrefix } from '../../lib/helpers';
|
|
15
15
|
import { generateRegistry } from '../lib/registry';
|
|
16
16
|
import { TransactionOptions } from '../types';
|
|
17
|
+
import { DirectEthSecp256k1Wallet } from './eth-secp256k1';
|
|
17
18
|
import { TransactionSigner } from './signer';
|
|
18
19
|
|
|
19
20
|
// Required for encoding and decoding queries that are of type Long.
|
|
@@ -39,6 +40,20 @@ export default class LocalWallet {
|
|
|
39
40
|
return wallet;
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Creates an Injective wallet for chains using Ethermint-style keys.
|
|
45
|
+
* Uses HD path (`m/44'/60'/0'/0/0`) by default.
|
|
46
|
+
*/
|
|
47
|
+
static async fromEthSecp256k1Mnemonic(
|
|
48
|
+
mnemonic: string,
|
|
49
|
+
prefix: string,
|
|
50
|
+
hdPath?: string,
|
|
51
|
+
): Promise<LocalWallet> {
|
|
52
|
+
const wallet = new LocalWallet();
|
|
53
|
+
await wallet.setSigner(await DirectEthSecp256k1Wallet.fromMnemonic(mnemonic, prefix, hdPath));
|
|
54
|
+
return wallet;
|
|
55
|
+
}
|
|
56
|
+
|
|
42
57
|
static async fromPrivateKey(pkHex: string, prefix?: string): Promise<LocalWallet> {
|
|
43
58
|
const wallet = new LocalWallet();
|
|
44
59
|
await wallet.setPrivateKey(pkHex, prefix);
|
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ export { FaucetClient } from './clients/faucet-client';
|
|
|
13
13
|
export { IndexerClient } from './clients/indexer-client';
|
|
14
14
|
export { default as LocalWallet } from './clients/modules/local-wallet';
|
|
15
15
|
export { NobleClient } from './clients/noble-client';
|
|
16
|
+
export { DirectEthSecp256k1Wallet } from './clients/modules/eth-secp256k1';
|
|
16
17
|
export { SocketClient } from './clients/socket-client';
|
|
17
18
|
export { SubaccountInfo as SubaccountClient, SubaccountInfo } from './clients/subaccount';
|
|
18
19
|
export { ValidatorClient } from './clients/validator-client';
|