ccxt 4.5.18 → 4.5.20

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 (152) hide show
  1. package/README.md +7 -6
  2. package/dist/ccxt.browser.min.js +40 -16
  3. package/dist/cjs/_virtual/index.cjs.js +1459 -9
  4. package/dist/cjs/ccxt.js +8 -2
  5. package/dist/cjs/src/abstract/dydx.js +11 -0
  6. package/dist/cjs/src/base/Exchange.js +164 -10
  7. package/dist/cjs/src/base/ws/Client.js +3 -1
  8. package/dist/cjs/src/binance.js +8 -1
  9. package/dist/cjs/src/bingx.js +20 -0
  10. package/dist/cjs/src/bitget.js +43 -41
  11. package/dist/cjs/src/bybit.js +21 -23
  12. package/dist/cjs/src/deribit.js +6 -0
  13. package/dist/cjs/src/dydx.js +2454 -0
  14. package/dist/cjs/src/gate.js +4 -4
  15. package/dist/cjs/src/hibachi.js +1 -1
  16. package/dist/cjs/src/hyperliquid.js +207 -9
  17. package/dist/cjs/src/kucoin.js +711 -109
  18. package/dist/cjs/src/mexc.js +2 -3
  19. package/dist/cjs/src/pro/binance.js +59 -144
  20. package/dist/cjs/src/pro/dydx.js +418 -0
  21. package/dist/cjs/src/pro/kraken.js +4 -3
  22. package/dist/cjs/src/pro/xt.js +218 -4
  23. package/dist/cjs/src/protobuf/mexc/compiled.cjs.js +1 -0
  24. package/dist/cjs/src/static_dependencies/dydx-v4-client/cosmos/base/v1beta1/coin.js +56 -0
  25. package/dist/cjs/src/static_dependencies/dydx-v4-client/cosmos/crypto/multisig/v1beta1/multisig.js +56 -0
  26. package/dist/cjs/src/static_dependencies/dydx-v4-client/cosmos/crypto/secp256k1/keys.js +48 -0
  27. package/dist/cjs/src/static_dependencies/dydx-v4-client/cosmos/tx/signing/v1beta1/signing.js +343 -0
  28. package/dist/cjs/src/static_dependencies/dydx-v4-client/cosmos/tx/v1beta1/tx.js +717 -0
  29. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/accountplus/tx.js +60 -0
  30. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/clob_pair.js +45 -0
  31. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/order.js +380 -0
  32. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/order_removals.js +72 -0
  33. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/tx.js +211 -0
  34. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/sending/transfer.js +195 -0
  35. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/sending/tx.js +49 -0
  36. package/dist/cjs/src/static_dependencies/dydx-v4-client/dydxprotocol/subaccounts/subaccount.js +57 -0
  37. package/dist/cjs/src/static_dependencies/dydx-v4-client/google/protobuf/any.js +56 -0
  38. package/dist/cjs/src/static_dependencies/dydx-v4-client/helpers.js +79 -0
  39. package/dist/cjs/src/static_dependencies/dydx-v4-client/long/index.cjs.js +9 -0
  40. package/dist/cjs/src/static_dependencies/dydx-v4-client/onboarding.js +59 -0
  41. package/dist/cjs/src/static_dependencies/dydx-v4-client/registry.js +39 -0
  42. package/dist/cjs/src/static_dependencies/noble-hashes/pbkdf2.js +69 -0
  43. package/dist/cjs/src/static_dependencies/noble-hashes/ripemd160.js +108 -0
  44. package/dist/cjs/src/static_dependencies/noble-hashes/utils.js +50 -1
  45. package/dist/cjs/src/static_dependencies/scure-base/index.js +29 -0
  46. package/dist/cjs/src/static_dependencies/scure-bip32/index.js +278 -0
  47. package/dist/cjs/src/static_dependencies/scure-bip39/index.js +97 -0
  48. package/dist/cjs/src/static_dependencies/scure-bip39/wordlists/english.js +2060 -0
  49. package/dist/cjs/src/static_dependencies/zklink/zklink-sdk-web.js +2 -0
  50. package/dist/cjs/src/toobit.js +2 -1
  51. package/js/ccxt.d.ts +8 -2
  52. package/js/ccxt.js +6 -2
  53. package/js/src/abstract/binance.d.ts +7 -0
  54. package/js/src/abstract/binancecoinm.d.ts +7 -0
  55. package/js/src/abstract/binanceus.d.ts +7 -0
  56. package/js/src/abstract/binanceusdm.d.ts +7 -0
  57. package/js/src/abstract/bitget.d.ts +1 -0
  58. package/js/src/abstract/dydx.d.ts +61 -0
  59. package/js/src/abstract/dydx.js +11 -0
  60. package/js/src/abstract/kucoin.d.ts +1 -1
  61. package/js/src/abstract/kucoinfutures.d.ts +1 -1
  62. package/js/src/base/Exchange.d.ts +7 -0
  63. package/js/src/base/Exchange.js +163 -10
  64. package/js/src/base/ws/Client.js +3 -1
  65. package/js/src/binance.js +8 -1
  66. package/js/src/bingx.js +20 -0
  67. package/js/src/bitget.d.ts +3 -3
  68. package/js/src/bitget.js +43 -41
  69. package/js/src/bybit.d.ts +3 -3
  70. package/js/src/bybit.js +21 -23
  71. package/js/src/deribit.js +6 -0
  72. package/js/src/dydx.d.ts +364 -0
  73. package/js/src/dydx.js +2453 -0
  74. package/js/src/gate.d.ts +2 -2
  75. package/js/src/gate.js +4 -4
  76. package/js/src/hibachi.js +1 -1
  77. package/js/src/hyperliquid.d.ts +17 -0
  78. package/js/src/hyperliquid.js +207 -9
  79. package/js/src/kucoin.d.ts +48 -1
  80. package/js/src/kucoin.js +711 -109
  81. package/js/src/mexc.js +2 -3
  82. package/js/src/pro/binance.d.ts +2 -2
  83. package/js/src/pro/binance.js +59 -144
  84. package/js/src/pro/dydx.d.ts +81 -0
  85. package/js/src/pro/dydx.js +417 -0
  86. package/js/src/pro/kraken.js +4 -3
  87. package/js/src/pro/xt.d.ts +85 -2
  88. package/js/src/pro/xt.js +218 -4
  89. package/js/src/static_dependencies/dydx-v4-client/cosmos/base/v1beta1/coin.d.ts +90 -0
  90. package/js/src/static_dependencies/dydx-v4-client/cosmos/base/v1beta1/coin.js +163 -0
  91. package/js/src/static_dependencies/dydx-v4-client/cosmos/crypto/multisig/keys.d.ts +26 -0
  92. package/js/src/static_dependencies/dydx-v4-client/cosmos/crypto/multisig/keys.js +51 -0
  93. package/js/src/static_dependencies/dydx-v4-client/cosmos/crypto/multisig/v1beta1/multisig.d.ts +48 -0
  94. package/js/src/static_dependencies/dydx-v4-client/cosmos/crypto/multisig/v1beta1/multisig.js +85 -0
  95. package/js/src/static_dependencies/dydx-v4-client/cosmos/crypto/secp256k1/keys.d.ts +40 -0
  96. package/js/src/static_dependencies/dydx-v4-client/cosmos/crypto/secp256k1/keys.js +77 -0
  97. package/js/src/static_dependencies/dydx-v4-client/cosmos/tx/signing/v1beta1/signing.d.ts +162 -0
  98. package/js/src/static_dependencies/dydx-v4-client/cosmos/tx/signing/v1beta1/signing.js +329 -0
  99. package/js/src/static_dependencies/dydx-v4-client/cosmos/tx/v1beta1/tx.d.ts +460 -0
  100. package/js/src/static_dependencies/dydx-v4-client/cosmos/tx/v1beta1/tx.js +698 -0
  101. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/accountplus/tx.d.ts +127 -0
  102. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/accountplus/tx.js +286 -0
  103. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/block_rate_limit_config.d.ts +66 -0
  104. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/block_rate_limit_config.js +109 -0
  105. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/clob_pair.d.ts +127 -0
  106. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/clob_pair.js +257 -0
  107. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/equity_tier_limit_config.d.ts +48 -0
  108. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/equity_tier_limit_config.js +93 -0
  109. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/finalize_block.d.ts +23 -0
  110. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/finalize_block.js +43 -0
  111. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/liquidations.d.ts +92 -0
  112. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/liquidations.js +164 -0
  113. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/liquidations_config.d.ts +124 -0
  114. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/liquidations_config.js +196 -0
  115. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/matches.d.ts +159 -0
  116. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/matches.js +324 -0
  117. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/order.d.ts +546 -0
  118. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/order.js +872 -0
  119. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/order_removals.d.ts +84 -0
  120. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/order_removals.js +181 -0
  121. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/tx.d.ts +397 -0
  122. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/clob/tx.js +757 -0
  123. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/sending/transfer.d.ts +120 -0
  124. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/sending/transfer.js +246 -0
  125. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/sending/tx.d.ts +79 -0
  126. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/sending/tx.js +147 -0
  127. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/subaccounts/asset_position.d.ts +32 -0
  128. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/subaccounts/asset_position.js +59 -0
  129. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/subaccounts/perpetual_position.d.ts +34 -0
  130. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/subaccounts/perpetual_position.js +66 -0
  131. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/subaccounts/subaccount.d.ts +62 -0
  132. package/js/src/static_dependencies/dydx-v4-client/dydxprotocol/subaccounts/subaccount.js +111 -0
  133. package/js/src/static_dependencies/dydx-v4-client/google/protobuf/any.d.ts +207 -0
  134. package/js/src/static_dependencies/dydx-v4-client/google/protobuf/any.js +50 -0
  135. package/js/src/static_dependencies/dydx-v4-client/helpers.d.ts +82 -0
  136. package/js/src/static_dependencies/dydx-v4-client/helpers.js +172 -0
  137. package/js/src/static_dependencies/dydx-v4-client/long/index.cjs +1473 -0
  138. package/js/src/static_dependencies/dydx-v4-client/long/index.d.cts +2 -0
  139. package/js/src/static_dependencies/dydx-v4-client/onboarding.d.ts +35 -0
  140. package/js/src/static_dependencies/dydx-v4-client/onboarding.js +56 -0
  141. package/js/src/static_dependencies/dydx-v4-client/registry.d.ts +7 -0
  142. package/js/src/static_dependencies/dydx-v4-client/registry.js +36 -0
  143. package/js/src/static_dependencies/noble-hashes/utils.d.ts +1 -0
  144. package/js/src/static_dependencies/noble-hashes/utils.js +4 -0
  145. package/js/src/static_dependencies/scure-bip32/index.d.ts +49 -0
  146. package/js/src/static_dependencies/scure-bip32/index.js +295 -0
  147. package/js/src/static_dependencies/scure-bip39/index.d.ts +54 -0
  148. package/js/src/static_dependencies/scure-bip39/index.js +140 -0
  149. package/js/src/static_dependencies/scure-bip39/wordlists/english.d.ts +1 -0
  150. package/js/src/static_dependencies/scure-bip39/wordlists/english.js +2054 -0
  151. package/js/src/toobit.js +2 -1
  152. package/package.json +9 -3
@@ -0,0 +1,2 @@
1
+ declare const _exports: any;
2
+ export = _exports;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @description Get Mnemonic and priv/pub keys from privateKeyBytes and BIP44 HD path
3
+ *
4
+ * @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
5
+ *
6
+ * @param entropy used to generate mnemonic
7
+ *
8
+ * @param path BIP44 HD Path. Default is The Cosmos Hub path
9
+ *
10
+ * @throws Error if the hdkey does not exist
11
+ *
12
+ * @returns Mnemonic and priv/pub keys
13
+ */
14
+ export declare const exportMnemonicAndPrivateKey: (entropy: Uint8Array, path?: string) => {
15
+ mnemonic: string;
16
+ privateKey: Uint8Array | null;
17
+ publicKey: Uint8Array | null;
18
+ };
19
+ /**
20
+ * @description Derive priv/pub keys from mnemonic and BIP44 HD path
21
+ *
22
+ * @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
23
+ *
24
+ * @param mnemonic used to generate seed
25
+ *
26
+ * @param path BIP44 HD Path. Default is The Cosmos Hub path
27
+ *
28
+ * @throws Error if the hdkey does not exist
29
+ *
30
+ * @returns Priv/pub keys
31
+ */
32
+ export declare const deriveHDKeyFromMnemonic: (mnemonic: string, path?: string) => {
33
+ privateKey: Uint8Array | null;
34
+ publicKey: Uint8Array | null;
35
+ };
@@ -0,0 +1,56 @@
1
+ // ----------------------------------------------------------------------------
2
+
3
+ // PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
4
+ // https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
5
+ // EDIT THE CORRESPONDENT .ts FILE INSTEAD
6
+
7
+ import { HDKey } from '../scure-bip32/index.js';
8
+ import { entropyToMnemonic, mnemonicToSeedSync } from '../scure-bip39/index.js';
9
+ import { wordlist } from '../scure-bip39/wordlists/english.js';
10
+ /**
11
+ * @description Get Mnemonic and priv/pub keys from privateKeyBytes and BIP44 HD path
12
+ *
13
+ * @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
14
+ *
15
+ * @param entropy used to generate mnemonic
16
+ *
17
+ * @param path BIP44 HD Path. Default is The Cosmos Hub path
18
+ *
19
+ * @throws Error if the hdkey does not exist
20
+ *
21
+ * @returns Mnemonic and priv/pub keys
22
+ */
23
+ export const exportMnemonicAndPrivateKey = (entropy, path = "m/44'/118'/0'/0/0") => {
24
+ const mnemonic = entropyToMnemonic(entropy, wordlist);
25
+ const { privateKey, publicKey } = deriveHDKeyFromMnemonic(mnemonic, path);
26
+ return {
27
+ mnemonic,
28
+ privateKey,
29
+ publicKey,
30
+ };
31
+ };
32
+ /**
33
+ * @description Derive priv/pub keys from mnemonic and BIP44 HD path
34
+ *
35
+ * @url https://github.com/confio/cosmos-hd-key-derivation-spec#bip44
36
+ *
37
+ * @param mnemonic used to generate seed
38
+ *
39
+ * @param path BIP44 HD Path. Default is The Cosmos Hub path
40
+ *
41
+ * @throws Error if the hdkey does not exist
42
+ *
43
+ * @returns Priv/pub keys
44
+ */
45
+ export const deriveHDKeyFromMnemonic = (mnemonic, path = "m/44'/118'/0'/0/0") => {
46
+ const seed = mnemonicToSeedSync(mnemonic);
47
+ const hdkey = HDKey.fromMasterSeed(seed);
48
+ const derivedHdkey = hdkey.derive(path);
49
+ if (!hdkey.privateKey) {
50
+ throw new Error('null hd key');
51
+ }
52
+ return {
53
+ privateKey: derivedHdkey.privateKey,
54
+ publicKey: derivedHdkey.publicKey,
55
+ };
56
+ };
@@ -0,0 +1,7 @@
1
+ import { Any } from "./google/protobuf/any.js";
2
+ export declare const registry: Record<string, any>;
3
+ export interface EncodeObject {
4
+ readonly typeUrl: string;
5
+ readonly value: any;
6
+ }
7
+ export declare function encodeAsAny(encodeObject: EncodeObject): Any;
@@ -0,0 +1,36 @@
1
+ // ----------------------------------------------------------------------------
2
+
3
+ // PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
4
+ // https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
5
+ // EDIT THE CORRESPONDENT .ts FILE INSTEAD
6
+
7
+ import { PubKey } from './cosmos/crypto/secp256k1/keys.js';
8
+ import { TxExtension } from './dydxprotocol/accountplus/tx.js';
9
+ import { MsgPlaceOrder, MsgCancelOrder, MsgBatchCancel, } from './dydxprotocol/clob/tx.js';
10
+ import { MsgWithdrawFromSubaccount, MsgDepositToSubaccount, } from './dydxprotocol/sending/transfer.js';
11
+ import { MsgCreateTransfer } from './dydxprotocol/sending/tx.js';
12
+ import { Any } from "./google/protobuf/any.js";
13
+ export const registry = {
14
+ // clob
15
+ '/dydxprotocol.clob.MsgPlaceOrder': MsgPlaceOrder,
16
+ '/dydxprotocol.clob.MsgCancelOrder': MsgCancelOrder,
17
+ '/dydxprotocol.clob.MsgBatchCancel': MsgBatchCancel,
18
+ // sending
19
+ '/dydxprotocol.sending.MsgCreateTransfer': MsgCreateTransfer,
20
+ '/dydxprotocol.sending.MsgWithdrawFromSubaccount': MsgWithdrawFromSubaccount,
21
+ '/dydxprotocol.sending.MsgDepositToSubaccount': MsgDepositToSubaccount,
22
+ '/dydxprotocol.accountplus.TxExtension': TxExtension,
23
+ '/cosmos.crypto.secp256k1.PubKey': PubKey,
24
+ };
25
+ export function encodeAsAny(encodeObject) {
26
+ const { typeUrl, value } = encodeObject;
27
+ const type = registry[typeUrl];
28
+ if (!type) {
29
+ throw new Error(`Unsupport type url: ${typeUrl}`);
30
+ }
31
+ const encodedMsg = type.encode(type.fromPartial(value)).finish();
32
+ return Any.fromPartial({
33
+ typeUrl: typeUrl,
34
+ value: encodedMsg,
35
+ });
36
+ }
@@ -74,4 +74,5 @@ export declare function wrapConstructorWithOpts<H extends Hash<H>, T extends Obj
74
74
  * Secure PRNG. Uses `globalThis.crypto` or node.js crypto module.
75
75
  */
76
76
  export declare function randomBytes(bytesLength?: number): Uint8Array;
77
+ export declare const abytes: (b: unknown) => void;
77
78
  export {};
@@ -145,3 +145,7 @@ export function randomBytes(bytesLength = 32) {
145
145
  }
146
146
  throw new Error('crypto.getRandomValues must be defined');
147
147
  }
148
+ export const abytes = (b) => {
149
+ if (!(b instanceof Uint8Array || (ArrayBuffer.isView(b) && b.constructor.name === 'Uint8Array')))
150
+ throw new Error('Uint8Array expected');
151
+ };
@@ -0,0 +1,49 @@
1
+ export declare const HARDENED_OFFSET: number;
2
+ export interface Versions {
3
+ private: number;
4
+ public: number;
5
+ }
6
+ interface HDKeyOpt {
7
+ versions?: Versions;
8
+ depth?: number;
9
+ index?: number;
10
+ parentFingerprint?: number;
11
+ chainCode?: Uint8Array;
12
+ publicKey?: Uint8Array;
13
+ privateKey?: Uint8Array | bigint;
14
+ }
15
+ export declare class HDKey {
16
+ get fingerprint(): number;
17
+ get identifier(): Uint8Array | undefined;
18
+ get pubKeyHash(): Uint8Array | undefined;
19
+ get privateKey(): Uint8Array | null;
20
+ get publicKey(): Uint8Array | null;
21
+ get privateExtendedKey(): string;
22
+ get publicExtendedKey(): string;
23
+ static fromMasterSeed(seed: Uint8Array, versions?: Versions): HDKey;
24
+ static fromExtendedKey(base58key: string, versions?: Versions): HDKey;
25
+ static fromJSON(json: {
26
+ xpriv: string;
27
+ }): HDKey;
28
+ readonly versions: Versions;
29
+ readonly depth: number;
30
+ readonly index: number;
31
+ readonly chainCode: Uint8Array | null;
32
+ readonly parentFingerprint: number;
33
+ private privKey?;
34
+ private privKeyBytes?;
35
+ private pubKey?;
36
+ private pubHash;
37
+ constructor(opt: HDKeyOpt);
38
+ derive(path: string): HDKey;
39
+ deriveChild(index: number): HDKey;
40
+ sign(hash: Uint8Array): Uint8Array;
41
+ verify(hash: Uint8Array, signature: Uint8Array): boolean;
42
+ wipePrivateData(): this;
43
+ toJSON(): {
44
+ xpriv: string;
45
+ xpub: string;
46
+ };
47
+ private serialize;
48
+ }
49
+ export {};
@@ -0,0 +1,295 @@
1
+ // ----------------------------------------------------------------------------
2
+
3
+ // PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
4
+ // https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
5
+ // EDIT THE CORRESPONDENT .ts FILE INSTEAD
6
+
7
+ /**
8
+ * BIP32 hierarchical deterministic (HD) wallets over secp256k1.
9
+ * @module
10
+ * @example
11
+ * ```js
12
+ * import { HDKey } from "@scure/bip32";
13
+ * const hdkey1 = HDKey.fromMasterSeed(seed);
14
+ * const hdkey2 = HDKey.fromExtendedKey(base58key);
15
+ * const hdkey3 = HDKey.fromJSON({ xpriv: string });
16
+ *
17
+ * // props
18
+ * [hdkey1.depth, hdkey1.index, hdkey1.chainCode];
19
+ * console.log(hdkey2.privateKey, hdkey2.publicKey);
20
+ * console.log(hdkey3.derive("m/0/2147483647'/1"));
21
+ * const sig = hdkey3.sign(hash);
22
+ * hdkey3.verify(hash, sig);
23
+ * ```
24
+ * @version 1.7.0
25
+ */
26
+ /*! scure-bip32 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
27
+ import { mod } from '../noble-curves/abstract/modular.js';
28
+ import { secp256k1 as secp } from '../noble-curves/secp256k1.js';
29
+ import { hmac } from '../noble-hashes/hmac.js';
30
+ import { ripemd160 } from '../noble-hashes/ripemd160.js';
31
+ import { sha256 } from '../noble-hashes/sha256.js';
32
+ import { sha512 } from '../noble-hashes/sha512.js';
33
+ import { abytes, bytesToHex, concatBytes, createView, hexToBytes, utf8ToBytes, } from '../noble-hashes/utils.js';
34
+ import { base58 as base58check } from '../scure-base/index.js';
35
+ const Point = secp.ProjectivePoint;
36
+ function bytesToNumber(bytes) {
37
+ abytes(bytes);
38
+ const h = bytes.length === 0 ? '0' : bytesToHex(bytes);
39
+ return BigInt('0x' + h);
40
+ }
41
+ function numberToBytes(num) {
42
+ if (typeof num !== 'bigint')
43
+ throw new Error('bigint expected');
44
+ return hexToBytes(num.toString(16).padStart(64, '0'));
45
+ }
46
+ const MASTER_SECRET = utf8ToBytes('Bitcoin seed');
47
+ // Bitcoin hardcoded by default
48
+ const BITCOIN_VERSIONS = { private: 0x0488ade4, public: 0x0488b21e };
49
+ export const HARDENED_OFFSET = 0x80000000;
50
+ const hash160 = (data) => ripemd160(sha256(data));
51
+ const fromU32 = (data) => createView(data).getUint32(0, false);
52
+ const toU32 = (n) => {
53
+ if (!Number.isSafeInteger(n) || n < 0 || n > 2 ** 32 - 1) {
54
+ throw new Error('invalid number, should be from 0 to 2**32-1, got ' + n);
55
+ }
56
+ const buf = new Uint8Array(4);
57
+ createView(buf).setUint32(0, n, false);
58
+ return buf;
59
+ };
60
+ export class HDKey {
61
+ constructor(opt) {
62
+ this.depth = 0;
63
+ this.index = 0;
64
+ this.chainCode = null;
65
+ this.parentFingerprint = 0;
66
+ if (!opt || typeof opt !== 'object') {
67
+ throw new Error('HDKey.constructor must not be called directly');
68
+ }
69
+ this.versions = opt.versions || BITCOIN_VERSIONS;
70
+ this.depth = opt.depth || 0;
71
+ this.chainCode = opt.chainCode || null;
72
+ this.index = opt.index || 0;
73
+ this.parentFingerprint = opt.parentFingerprint || 0;
74
+ if (!this.depth) {
75
+ if (this.parentFingerprint || this.index) {
76
+ throw new Error('HDKey: zero depth with non-zero index/parent fingerprint');
77
+ }
78
+ }
79
+ if (opt.publicKey && opt.privateKey) {
80
+ throw new Error('HDKey: publicKey and privateKey at same time.');
81
+ }
82
+ if (opt.privateKey) {
83
+ if (!secp.utils.isValidPrivateKey(opt.privateKey)) {
84
+ throw new Error('Invalid private key');
85
+ }
86
+ this.privKey =
87
+ typeof opt.privateKey === 'bigint' ? opt.privateKey : bytesToNumber(opt.privateKey);
88
+ this.privKeyBytes = numberToBytes(this.privKey);
89
+ this.pubKey = secp.getPublicKey(opt.privateKey, true);
90
+ }
91
+ else if (opt.publicKey) {
92
+ this.pubKey = Point.fromHex(opt.publicKey).toRawBytes(true); // force compressed point
93
+ }
94
+ else {
95
+ throw new Error('HDKey: no public or private key provided');
96
+ }
97
+ this.pubHash = hash160(this.pubKey);
98
+ }
99
+ get fingerprint() {
100
+ if (!this.pubHash) {
101
+ throw new Error('No publicKey set!');
102
+ }
103
+ return fromU32(this.pubHash);
104
+ }
105
+ get identifier() {
106
+ return this.pubHash;
107
+ }
108
+ get pubKeyHash() {
109
+ return this.pubHash;
110
+ }
111
+ get privateKey() {
112
+ return this.privKeyBytes || null;
113
+ }
114
+ get publicKey() {
115
+ return this.pubKey || null;
116
+ }
117
+ get privateExtendedKey() {
118
+ const priv = this.privateKey;
119
+ if (!priv) {
120
+ throw new Error('No private key');
121
+ }
122
+ return base58check.encode(this.serialize(this.versions.private, concatBytes(new Uint8Array([0]), priv)));
123
+ }
124
+ get publicExtendedKey() {
125
+ if (!this.pubKey) {
126
+ throw new Error('No public key');
127
+ }
128
+ return base58check.encode(this.serialize(this.versions.public, this.pubKey));
129
+ }
130
+ static fromMasterSeed(seed, versions = BITCOIN_VERSIONS) {
131
+ abytes(seed);
132
+ if (8 * seed.length < 128 || 8 * seed.length > 512) {
133
+ throw new Error('HDKey: seed length must be between 128 and 512 bits; 256 bits is advised, got ' +
134
+ seed.length);
135
+ }
136
+ const I = hmac(sha512, MASTER_SECRET, seed);
137
+ return new HDKey({
138
+ versions,
139
+ chainCode: I.slice(32),
140
+ privateKey: I.slice(0, 32),
141
+ });
142
+ }
143
+ static fromExtendedKey(base58key, versions = BITCOIN_VERSIONS) {
144
+ // => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
145
+ const keyBuffer = base58check.decode(base58key);
146
+ const keyView = createView(keyBuffer);
147
+ const version = keyView.getUint32(0, false);
148
+ const opt = {
149
+ versions,
150
+ depth: keyBuffer[4],
151
+ parentFingerprint: keyView.getUint32(5, false),
152
+ index: keyView.getUint32(9, false),
153
+ chainCode: keyBuffer.slice(13, 45),
154
+ };
155
+ const key = keyBuffer.slice(45);
156
+ const isPriv = key[0] === 0;
157
+ if (version !== versions[isPriv ? 'private' : 'public']) {
158
+ throw new Error('Version mismatch');
159
+ }
160
+ if (isPriv) {
161
+ return new HDKey({ ...opt, privateKey: key.slice(1) });
162
+ }
163
+ else {
164
+ return new HDKey({ ...opt, publicKey: key });
165
+ }
166
+ }
167
+ static fromJSON(json) {
168
+ return HDKey.fromExtendedKey(json.xpriv);
169
+ }
170
+ derive(path) {
171
+ if (!/^[mM]'?/.test(path)) {
172
+ throw new Error('Path must start with "m" or "M"');
173
+ }
174
+ if (/^[mM]'?$/.test(path)) {
175
+ return this;
176
+ }
177
+ const parts = path.replace(/^[mM]'?\//, '').split('/');
178
+ // tslint:disable-next-line
179
+ let child = this;
180
+ for (const c of parts) {
181
+ const m = /^(\d+)('?)$/.exec(c);
182
+ const m1 = m && m[1];
183
+ if (!m || m.length !== 3 || typeof m1 !== 'string')
184
+ throw new Error('invalid child index: ' + c);
185
+ let idx = +m1;
186
+ if (!Number.isSafeInteger(idx) || idx >= HARDENED_OFFSET) {
187
+ throw new Error('Invalid index');
188
+ }
189
+ // hardened key
190
+ if (m[2] === "'") {
191
+ idx += HARDENED_OFFSET;
192
+ }
193
+ child = child.deriveChild(idx);
194
+ }
195
+ return child;
196
+ }
197
+ deriveChild(index) {
198
+ if (!this.pubKey || !this.chainCode) {
199
+ throw new Error('No publicKey or chainCode set');
200
+ }
201
+ let data = toU32(index);
202
+ if (index >= HARDENED_OFFSET) {
203
+ // Hardened
204
+ const priv = this.privateKey;
205
+ if (!priv) {
206
+ throw new Error('Could not derive hardened child key');
207
+ }
208
+ // Hardened child: 0x00 || ser256(kpar) || ser32(index)
209
+ data = concatBytes(new Uint8Array([0]), priv, data);
210
+ }
211
+ else {
212
+ // Normal child: serP(point(kpar)) || ser32(index)
213
+ data = concatBytes(this.pubKey, data);
214
+ }
215
+ const I = hmac(sha512, this.chainCode, data);
216
+ const childTweak = bytesToNumber(I.slice(0, 32));
217
+ const chainCode = I.slice(32);
218
+ if (!secp.utils.isValidPrivateKey(childTweak)) {
219
+ throw new Error('Tweak bigger than curve order');
220
+ }
221
+ const opt = {
222
+ versions: this.versions,
223
+ chainCode,
224
+ depth: this.depth + 1,
225
+ parentFingerprint: this.fingerprint,
226
+ index,
227
+ };
228
+ try {
229
+ // Private parent key -> private child key
230
+ if (this.privateKey) {
231
+ const added = mod(this.privKey + childTweak, secp.CURVE.n);
232
+ if (!secp.utils.isValidPrivateKey(added)) {
233
+ throw new Error('The tweak was out of range or the resulted private key is invalid');
234
+ }
235
+ opt.privateKey = added;
236
+ }
237
+ else {
238
+ const added = Point.fromHex(this.pubKey).add(Point.fromPrivateKey(childTweak));
239
+ // Cryptographically impossible: hmac-sha512 preimage would need to be found
240
+ if (added.equals(Point.ZERO)) {
241
+ throw new Error('The tweak was equal to negative P, which made the result key invalid');
242
+ }
243
+ opt.publicKey = added.toRawBytes(true);
244
+ }
245
+ return new HDKey(opt);
246
+ }
247
+ catch (err) {
248
+ return this.deriveChild(index + 1);
249
+ }
250
+ }
251
+ sign(hash) {
252
+ if (!this.privateKey) {
253
+ throw new Error('No privateKey set!');
254
+ }
255
+ abytes(hash);
256
+ return secp.sign(hash, this.privKey).toCompactRawBytes();
257
+ }
258
+ verify(hash, signature) {
259
+ abytes(hash);
260
+ abytes(signature);
261
+ if (!this.publicKey) {
262
+ throw new Error('No publicKey set!');
263
+ }
264
+ let sig;
265
+ try {
266
+ sig = secp.Signature.fromCompact(signature);
267
+ }
268
+ catch (error) {
269
+ return false;
270
+ }
271
+ return secp.verify(sig, hash, this.publicKey);
272
+ }
273
+ wipePrivateData() {
274
+ this.privKey = undefined;
275
+ if (this.privKeyBytes) {
276
+ this.privKeyBytes.fill(0);
277
+ this.privKeyBytes = undefined;
278
+ }
279
+ return this;
280
+ }
281
+ toJSON() {
282
+ return {
283
+ xpriv: this.privateExtendedKey,
284
+ xpub: this.publicExtendedKey,
285
+ };
286
+ }
287
+ serialize(version, key) {
288
+ if (!this.chainCode) {
289
+ throw new Error('No chainCode set');
290
+ }
291
+ abytes(key);
292
+ // version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
293
+ return concatBytes(toU32(version), new Uint8Array([this.depth]), toU32(this.parentFingerprint), toU32(this.index), this.chainCode, key);
294
+ }
295
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Reversible: Converts mnemonic string to raw entropy in form of byte array.
3
+ * @param mnemonic 12-24 words
4
+ * @param wordlist imported wordlist for specific language
5
+ * @example
6
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
7
+ * mnemonicToEntropy(mnem, wordlist)
8
+ * // Produces
9
+ * new Uint8Array([
10
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
11
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
12
+ * ])
13
+ */
14
+ export declare function mnemonicToEntropy(mnemonic: string, wordlist: string[]): Uint8Array;
15
+ /**
16
+ * Reversible: Converts raw entropy in form of byte array to mnemonic string.
17
+ * @param entropy byte array
18
+ * @param wordlist imported wordlist for specific language
19
+ * @returns 12-24 words
20
+ * @example
21
+ * const ent = new Uint8Array([
22
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
23
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
24
+ * ]);
25
+ * entropyToMnemonic(ent, wordlist);
26
+ * // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
27
+ */
28
+ export declare function entropyToMnemonic(entropy: Uint8Array, wordlist: string[]): string;
29
+ /**
30
+ * Validates mnemonic for being 12-24 words contained in `wordlist`.
31
+ */
32
+ export declare function validateMnemonic(mnemonic: string, wordlist: string[]): boolean;
33
+ /**
34
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
35
+ * @param mnemonic 12-24 words
36
+ * @param passphrase string that will additionally protect the key
37
+ * @returns 64 bytes of key data
38
+ * @example
39
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
40
+ * await mnemonicToSeed(mnem, 'password');
41
+ * // new Uint8Array([...64 bytes])
42
+ */
43
+ export declare function mnemonicToSeed(mnemonic: string, passphrase?: string): Promise<Uint8Array>;
44
+ /**
45
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
46
+ * @param mnemonic 12-24 words
47
+ * @param passphrase string that will additionally protect the key
48
+ * @returns 64 bytes of key data
49
+ * @example
50
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
51
+ * mnemonicToSeedSync(mnem, 'password');
52
+ * // new Uint8Array([...64 bytes])
53
+ */
54
+ export declare function mnemonicToSeedSync(mnemonic: string, passphrase?: string): Uint8Array;