ccxt-look 1.81.50

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 (264) hide show
  1. package/.cache/eslintcache +1 -0
  2. package/.dockerignore +6 -0
  3. package/.eslintignore +1 -0
  4. package/.gitattributes +5 -0
  5. package/.readthedocs.yaml +16 -0
  6. package/CONTRIBUTING.md +1049 -0
  7. package/LICENSE.txt +21 -0
  8. package/README.md +537 -0
  9. package/SECURITY.md +5 -0
  10. package/build/cleanup-old-tags.js +94 -0
  11. package/build/countries.js +256 -0
  12. package/build/export-exchanges.js +520 -0
  13. package/build/fs.js +51 -0
  14. package/build/transpile.js +1772 -0
  15. package/build/vss.js +78 -0
  16. package/ccxt.browser.js +7 -0
  17. package/ccxt.d.ts +692 -0
  18. package/ccxt.js +171 -0
  19. package/cleanup.sh +2 -0
  20. package/composer-install.sh +20 -0
  21. package/dist/ccxt.browser.js +208383 -0
  22. package/gource.sh +3 -0
  23. package/index.html +7 -0
  24. package/js/.eslintrc +87 -0
  25. package/js/aax.js +2686 -0
  26. package/js/ascendex.js +2584 -0
  27. package/js/base/.eslintrc.js +43 -0
  28. package/js/base/Exchange.js +2371 -0
  29. package/js/base/Precise.js +283 -0
  30. package/js/base/errorHierarchy.js +47 -0
  31. package/js/base/errors.js +55 -0
  32. package/js/base/functions/crypto.js +158 -0
  33. package/js/base/functions/encode.js +118 -0
  34. package/js/base/functions/generic.js +270 -0
  35. package/js/base/functions/misc.js +138 -0
  36. package/js/base/functions/number.js +329 -0
  37. package/js/base/functions/platform.js +38 -0
  38. package/js/base/functions/string.js +21 -0
  39. package/js/base/functions/throttle.js +79 -0
  40. package/js/base/functions/time.js +210 -0
  41. package/js/base/functions/type.js +66 -0
  42. package/js/base/functions.js +28 -0
  43. package/js/bequant.js +32 -0
  44. package/js/bibox.js +1407 -0
  45. package/js/bigone.js +1366 -0
  46. package/js/binance.js +5652 -0
  47. package/js/binancecoinm.js +46 -0
  48. package/js/binanceus.js +46 -0
  49. package/js/binanceusdm.js +49 -0
  50. package/js/bit2c.js +535 -0
  51. package/js/bitbank.js +842 -0
  52. package/js/bitbay.js +16 -0
  53. package/js/bitbns.js +1073 -0
  54. package/js/bitcoincom.js +15 -0
  55. package/js/bitfinex.js +1433 -0
  56. package/js/bitfinex2.js +2025 -0
  57. package/js/bitflyer.js +840 -0
  58. package/js/bitforex.js +614 -0
  59. package/js/bitget.js +2397 -0
  60. package/js/bithumb.js +980 -0
  61. package/js/bitmart.js +2516 -0
  62. package/js/bitmex.js +1809 -0
  63. package/js/bitopro.js +1443 -0
  64. package/js/bitpanda.js +1782 -0
  65. package/js/bitrue.js +1747 -0
  66. package/js/bitso.js +1062 -0
  67. package/js/bitstamp.js +1757 -0
  68. package/js/bitstamp1.js +343 -0
  69. package/js/bittrex.js +1876 -0
  70. package/js/bitvavo.js +1579 -0
  71. package/js/bkex.js +1233 -0
  72. package/js/bl3p.js +346 -0
  73. package/js/blockchaincom.js +969 -0
  74. package/js/btcalpha.js +680 -0
  75. package/js/btcbox.js +477 -0
  76. package/js/btcmarkets.js +1022 -0
  77. package/js/btctradeua.js +466 -0
  78. package/js/btcturk.js +734 -0
  79. package/js/buda.js +946 -0
  80. package/js/bw.js +1265 -0
  81. package/js/bybit.js +3372 -0
  82. package/js/bytetrade.js +1336 -0
  83. package/js/cdax.js +1646 -0
  84. package/js/cex.js +1410 -0
  85. package/js/coinbase.js +1342 -0
  86. package/js/coinbaseprime.js +31 -0
  87. package/js/coinbasepro.js +1466 -0
  88. package/js/coincheck.js +755 -0
  89. package/js/coinex.js +3400 -0
  90. package/js/coinfalcon.js +880 -0
  91. package/js/coinmate.js +794 -0
  92. package/js/coinone.js +816 -0
  93. package/js/coinspot.js +345 -0
  94. package/js/crex24.js +1636 -0
  95. package/js/cryptocom.js +1832 -0
  96. package/js/currencycom.js +1748 -0
  97. package/js/delta.js +1547 -0
  98. package/js/deribit.js +2148 -0
  99. package/js/digifinex.js +1585 -0
  100. package/js/eqonex.js +1660 -0
  101. package/js/exmo.js +1670 -0
  102. package/js/fairdesk.js +1231 -0
  103. package/js/flowbtc.js +35 -0
  104. package/js/fmfwio.js +34 -0
  105. package/js/ftx.js +2751 -0
  106. package/js/ftxus.js +38 -0
  107. package/js/gateio.js +4174 -0
  108. package/js/gemini.js +1397 -0
  109. package/js/hitbtc.js +1343 -0
  110. package/js/hitbtc3.js +2329 -0
  111. package/js/hollaex.js +1486 -0
  112. package/js/huobi.js +5706 -0
  113. package/js/huobijp.js +1710 -0
  114. package/js/huobipro.js +18 -0
  115. package/js/idex.js +1439 -0
  116. package/js/independentreserve.js +649 -0
  117. package/js/indodax.js +742 -0
  118. package/js/itbit.js +722 -0
  119. package/js/kraken.js +2179 -0
  120. package/js/kucoin.js +2571 -0
  121. package/js/kucoinfutures.js +1771 -0
  122. package/js/kuna.js +809 -0
  123. package/js/latoken.js +1445 -0
  124. package/js/lbank.js +760 -0
  125. package/js/liquid.js +1432 -0
  126. package/js/luno.js +873 -0
  127. package/js/lykke.js +1147 -0
  128. package/js/mercado.js +771 -0
  129. package/js/mexc.js +3151 -0
  130. package/js/ndax.js +2233 -0
  131. package/js/novadax.js +1318 -0
  132. package/js/oceanex.js +816 -0
  133. package/js/okcoin.js +3841 -0
  134. package/js/okex.js +16 -0
  135. package/js/okex5.js +16 -0
  136. package/js/okx.js +4795 -0
  137. package/js/paymium.js +498 -0
  138. package/js/phemex.js +2957 -0
  139. package/js/poloniex.js +1674 -0
  140. package/js/probit.js +1346 -0
  141. package/js/qtrade.js +1588 -0
  142. package/js/ripio.js +1061 -0
  143. package/js/static_dependencies/BN/bn.js +3526 -0
  144. package/js/static_dependencies/README.md +1 -0
  145. package/js/static_dependencies/crypto-js/crypto-js.js +5988 -0
  146. package/js/static_dependencies/elliptic/lib/elliptic/curve/base.js +375 -0
  147. package/js/static_dependencies/elliptic/lib/elliptic/curve/edwards.js +433 -0
  148. package/js/static_dependencies/elliptic/lib/elliptic/curve/index.js +8 -0
  149. package/js/static_dependencies/elliptic/lib/elliptic/curve/mont.js +180 -0
  150. package/js/static_dependencies/elliptic/lib/elliptic/curve/short.js +938 -0
  151. package/js/static_dependencies/elliptic/lib/elliptic/curves.js +204 -0
  152. package/js/static_dependencies/elliptic/lib/elliptic/ec/index.js +240 -0
  153. package/js/static_dependencies/elliptic/lib/elliptic/ec/key.js +119 -0
  154. package/js/static_dependencies/elliptic/lib/elliptic/ec/signature.js +24 -0
  155. package/js/static_dependencies/elliptic/lib/elliptic/eddsa/index.js +145 -0
  156. package/js/static_dependencies/elliptic/lib/elliptic/eddsa/key.js +100 -0
  157. package/js/static_dependencies/elliptic/lib/elliptic/eddsa/signature.js +65 -0
  158. package/js/static_dependencies/elliptic/lib/elliptic/precomputed/secp256k1.js +780 -0
  159. package/js/static_dependencies/elliptic/lib/elliptic/utils.js +214 -0
  160. package/js/static_dependencies/elliptic/lib/elliptic.js +22 -0
  161. package/js/static_dependencies/elliptic/lib/hmac-drbg/hmac-drbg.js +114 -0
  162. package/js/static_dependencies/fetch-ponyfill/fetch-node.js +39 -0
  163. package/js/static_dependencies/node-fetch/index.js +1564 -0
  164. package/js/static_dependencies/node-rsa/NodeRSA.js +223 -0
  165. package/js/static_dependencies/node-rsa/asn1/ber/errors.js +13 -0
  166. package/js/static_dependencies/node-rsa/asn1/ber/index.js +21 -0
  167. package/js/static_dependencies/node-rsa/asn1/ber/reader.js +262 -0
  168. package/js/static_dependencies/node-rsa/asn1/ber/types.js +36 -0
  169. package/js/static_dependencies/node-rsa/asn1/index.js +17 -0
  170. package/js/static_dependencies/node-rsa/encryptEngines/js.js +34 -0
  171. package/js/static_dependencies/node-rsa/formats/components.js +71 -0
  172. package/js/static_dependencies/node-rsa/formats/formats.js +31 -0
  173. package/js/static_dependencies/node-rsa/formats/pkcs1.js +148 -0
  174. package/js/static_dependencies/node-rsa/formats/pkcs8.js +187 -0
  175. package/js/static_dependencies/node-rsa/libs/jsbn.js +1252 -0
  176. package/js/static_dependencies/node-rsa/libs/rsa.js +147 -0
  177. package/js/static_dependencies/node-rsa/schemes/pkcs1.js +176 -0
  178. package/js/static_dependencies/node-rsa/schemes/schemes.js +21 -0
  179. package/js/static_dependencies/node-rsa/utils.js +98 -0
  180. package/js/static_dependencies/qs/formats.js +18 -0
  181. package/js/static_dependencies/qs/index.js +11 -0
  182. package/js/static_dependencies/qs/parse.js +242 -0
  183. package/js/static_dependencies/qs/stringify.js +269 -0
  184. package/js/static_dependencies/qs/utils.js +230 -0
  185. package/js/stex.js +1925 -0
  186. package/js/test/.eslintrc.js +42 -0
  187. package/js/test/Exchange/test.balance.js +61 -0
  188. package/js/test/Exchange/test.borrowRate.js +32 -0
  189. package/js/test/Exchange/test.currency.js +52 -0
  190. package/js/test/Exchange/test.fetchBalance.js +23 -0
  191. package/js/test/Exchange/test.fetchBorrowInterest.js +59 -0
  192. package/js/test/Exchange/test.fetchBorrowRate.js +32 -0
  193. package/js/test/Exchange/test.fetchBorrowRates.js +28 -0
  194. package/js/test/Exchange/test.fetchClosedOrders.js +32 -0
  195. package/js/test/Exchange/test.fetchCurrencies.js +35 -0
  196. package/js/test/Exchange/test.fetchDeposits.js +31 -0
  197. package/js/test/Exchange/test.fetchFundingFees.js +19 -0
  198. package/js/test/Exchange/test.fetchFundingRateHistory.js +40 -0
  199. package/js/test/Exchange/test.fetchL2OrderBook.js +23 -0
  200. package/js/test/Exchange/test.fetchLedger.js +42 -0
  201. package/js/test/Exchange/test.fetchLeverageTiers.js +33 -0
  202. package/js/test/Exchange/test.fetchMarketLeverageTiers.js +22 -0
  203. package/js/test/Exchange/test.fetchMarkets.js +33 -0
  204. package/js/test/Exchange/test.fetchMyTrades.js +42 -0
  205. package/js/test/Exchange/test.fetchOHLCV.js +46 -0
  206. package/js/test/Exchange/test.fetchOpenOrders.js +36 -0
  207. package/js/test/Exchange/test.fetchOrderBook.js +25 -0
  208. package/js/test/Exchange/test.fetchOrderBooks.js +35 -0
  209. package/js/test/Exchange/test.fetchOrders.js +41 -0
  210. package/js/test/Exchange/test.fetchPositions.js +47 -0
  211. package/js/test/Exchange/test.fetchStatus.js +35 -0
  212. package/js/test/Exchange/test.fetchTicker.js +38 -0
  213. package/js/test/Exchange/test.fetchTickers.js +49 -0
  214. package/js/test/Exchange/test.fetchTrades.js +39 -0
  215. package/js/test/Exchange/test.fetchTradingFee.js +18 -0
  216. package/js/test/Exchange/test.fetchTradingFees.js +22 -0
  217. package/js/test/Exchange/test.fetchTransactions.js +31 -0
  218. package/js/test/Exchange/test.fetchWithdrawals.js +31 -0
  219. package/js/test/Exchange/test.ledgerItem.js +46 -0
  220. package/js/test/Exchange/test.leverageTier.js +33 -0
  221. package/js/test/Exchange/test.loadMarkets.js +35 -0
  222. package/js/test/Exchange/test.market.js +129 -0
  223. package/js/test/Exchange/test.ohlcv.js +33 -0
  224. package/js/test/Exchange/test.order.js +62 -0
  225. package/js/test/Exchange/test.orderbook.js +61 -0
  226. package/js/test/Exchange/test.position.js +21 -0
  227. package/js/test/Exchange/test.throttle.js +94 -0
  228. package/js/test/Exchange/test.ticker.js +95 -0
  229. package/js/test/Exchange/test.trade.js +68 -0
  230. package/js/test/Exchange/test.tradingFee.js +34 -0
  231. package/js/test/Exchange/test.transaction.js +35 -0
  232. package/js/test/base/.eslintrc +38 -0
  233. package/js/test/base/functions/test.crypto.js +110 -0
  234. package/js/test/base/functions/test.datetime.js +62 -0
  235. package/js/test/base/functions/test.generic.js +152 -0
  236. package/js/test/base/functions/test.number.js +362 -0
  237. package/js/test/base/functions/test.time.js +56 -0
  238. package/js/test/base/functions/test.type.js +53 -0
  239. package/js/test/base/test.base.js +193 -0
  240. package/js/test/errors/test.InsufficientFunds.js +86 -0
  241. package/js/test/errors/test.InvalidNonce.js +64 -0
  242. package/js/test/errors/test.InvalidOrder.js +35 -0
  243. package/js/test/errors/test.OrderNotFound.js +39 -0
  244. package/js/test/test.js +426 -0
  245. package/js/test/test.timeout_hang.js +12 -0
  246. package/js/therock.js +1431 -0
  247. package/js/tidebit.js +632 -0
  248. package/js/tidex.js +939 -0
  249. package/js/timex.js +1283 -0
  250. package/js/upbit.js +1622 -0
  251. package/js/vcc.js +1353 -0
  252. package/js/wavesexchange.js +2185 -0
  253. package/js/wazirx.js +732 -0
  254. package/js/whitebit.js +1352 -0
  255. package/js/woo.js +1577 -0
  256. package/js/xena.js +1948 -0
  257. package/js/yobit.js +1129 -0
  258. package/js/zaif.js +647 -0
  259. package/js/zb.js +4088 -0
  260. package/js/zipmex.js +40 -0
  261. package/js/zonda.js +1497 -0
  262. package/multilang.sh +159 -0
  263. package/package.json +591 -0
  264. package/postinstall.js +103 -0
@@ -0,0 +1,204 @@
1
+ 'use strict';
2
+
3
+ var curves = exports;
4
+
5
+ var elliptic = require('../elliptic');
6
+
7
+ var assert = elliptic.utils.assert;
8
+
9
+ function PresetCurve(options) {
10
+ if (options.type === 'short')
11
+ this.curve = new elliptic.curve.short(options);
12
+ else if (options.type === 'edwards')
13
+ this.curve = new elliptic.curve.edwards(options);
14
+ else
15
+ this.curve = new elliptic.curve.mont(options);
16
+ this.g = this.curve.g;
17
+ this.n = this.curve.n;
18
+ this.hash = options.hash;
19
+
20
+ assert(this.g.validate(), 'Invalid curve');
21
+ assert(this.g.mul(this.n).isInfinity(), 'Invalid curve, G*N != O');
22
+ }
23
+ curves.PresetCurve = PresetCurve;
24
+
25
+ function defineCurve(name, options) {
26
+ Object.defineProperty(curves, name, {
27
+ configurable: true,
28
+ enumerable: true,
29
+ get: function() {
30
+ var curve = new PresetCurve(options);
31
+ Object.defineProperty(curves, name, {
32
+ configurable: true,
33
+ enumerable: true,
34
+ value: curve
35
+ });
36
+ return curve;
37
+ }
38
+ });
39
+ }
40
+
41
+ defineCurve('p192', {
42
+ type: 'short',
43
+ prime: 'p192',
44
+ p: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff',
45
+ a: 'ffffffff ffffffff ffffffff fffffffe ffffffff fffffffc',
46
+ b: '64210519 e59c80e7 0fa7e9ab 72243049 feb8deec c146b9b1',
47
+ n: 'ffffffff ffffffff ffffffff 99def836 146bc9b1 b4d22831',
48
+ hash: 'SHA256',
49
+ gRed: false,
50
+ g: [
51
+ '188da80e b03090f6 7cbf20eb 43a18800 f4ff0afd 82ff1012',
52
+ '07192b95 ffc8da78 631011ed 6b24cdd5 73f977a1 1e794811'
53
+ ]
54
+ });
55
+
56
+ defineCurve('p224', {
57
+ type: 'short',
58
+ prime: 'p224',
59
+ p: 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001',
60
+ a: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe',
61
+ b: 'b4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4',
62
+ n: 'ffffffff ffffffff ffffffff ffff16a2 e0b8f03e 13dd2945 5c5c2a3d',
63
+ hash: 'SHA256',
64
+ gRed: false,
65
+ g: [
66
+ 'b70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21',
67
+ 'bd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34'
68
+ ]
69
+ });
70
+
71
+ defineCurve('p256', {
72
+ type: 'short',
73
+ prime: null,
74
+ p: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff',
75
+ a: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff fffffffc',
76
+ b: '5ac635d8 aa3a93e7 b3ebbd55 769886bc 651d06b0 cc53b0f6 3bce3c3e 27d2604b',
77
+ n: 'ffffffff 00000000 ffffffff ffffffff bce6faad a7179e84 f3b9cac2 fc632551',
78
+ hash: 'SHA256',
79
+ gRed: false,
80
+ g: [
81
+ '6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296',
82
+ '4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5'
83
+ ]
84
+ });
85
+
86
+ defineCurve('p384', {
87
+ type: 'short',
88
+ prime: null,
89
+ p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
90
+ 'fffffffe ffffffff 00000000 00000000 ffffffff',
91
+ a: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
92
+ 'fffffffe ffffffff 00000000 00000000 fffffffc',
93
+ b: 'b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112 0314088f ' +
94
+ '5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef',
95
+ n: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff c7634d81 ' +
96
+ 'f4372ddf 581a0db2 48b0a77a ecec196a ccc52973',
97
+ hash: 'SHA384',
98
+ gRed: false,
99
+ g: [
100
+ 'aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98 59f741e0 82542a38 ' +
101
+ '5502f25d bf55296c 3a545e38 72760ab7',
102
+ '3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c e9da3113 b5f0b8c0 ' +
103
+ '0a60b1ce 1d7e819d 7a431d7c 90ea0e5f'
104
+ ]
105
+ });
106
+
107
+ defineCurve('p521', {
108
+ type: 'short',
109
+ prime: null,
110
+ p: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
111
+ 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
112
+ 'ffffffff ffffffff ffffffff ffffffff ffffffff',
113
+ a: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
114
+ 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
115
+ 'ffffffff ffffffff ffffffff ffffffff fffffffc',
116
+ b: '00000051 953eb961 8e1c9a1f 929a21a0 b68540ee a2da725b ' +
117
+ '99b315f3 b8b48991 8ef109e1 56193951 ec7e937b 1652c0bd ' +
118
+ '3bb1bf07 3573df88 3d2c34f1 ef451fd4 6b503f00',
119
+ n: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
120
+ 'ffffffff ffffffff fffffffa 51868783 bf2f966b 7fcc0148 ' +
121
+ 'f709a5d0 3bb5c9b8 899c47ae bb6fb71e 91386409',
122
+ hash: 'SHA512',
123
+ gRed: false,
124
+ g: [
125
+ '000000c6 858e06b7 0404e9cd 9e3ecb66 2395b442 9c648139 ' +
126
+ '053fb521 f828af60 6b4d3dba a14b5e77 efe75928 fe1dc127 ' +
127
+ 'a2ffa8de 3348b3c1 856a429b f97e7e31 c2e5bd66',
128
+ '00000118 39296a78 9a3bc004 5c8a5fb4 2c7d1bd9 98f54449 ' +
129
+ '579b4468 17afbd17 273e662c 97ee7299 5ef42640 c550b901 ' +
130
+ '3fad0761 353c7086 a272c240 88be9476 9fd16650'
131
+ ]
132
+ });
133
+
134
+ defineCurve('curve25519', {
135
+ type: 'mont',
136
+ prime: 'p25519',
137
+ p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
138
+ a: '76d06',
139
+ b: '1',
140
+ n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
141
+ hash: 'SHA256',
142
+ gRed: false,
143
+ g: [
144
+ '9'
145
+ ]
146
+ });
147
+
148
+ defineCurve('ed25519', {
149
+ type: 'edwards',
150
+ prime: 'p25519',
151
+ p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
152
+ a: '-1',
153
+ c: '1',
154
+ // -121665 * (121666^(-1)) (mod P)
155
+ d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
156
+ n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
157
+ hash: 'SHA256',
158
+ gRed: false,
159
+ g: [
160
+ '216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a',
161
+
162
+ // 4/5
163
+ '6666666666666666666666666666666666666666666666666666666666666658'
164
+ ]
165
+ });
166
+
167
+ var pre;
168
+ try {
169
+ pre = require('./precomputed/secp256k1');
170
+ } catch (e) {
171
+ pre = undefined;
172
+ }
173
+
174
+ defineCurve('secp256k1', {
175
+ type: 'short',
176
+ prime: 'k256',
177
+ p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f',
178
+ a: '0',
179
+ b: '7',
180
+ n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141',
181
+ h: '1',
182
+ hash: 'SHA256',
183
+
184
+ // Precomputed endomorphism
185
+ beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee',
186
+ lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72',
187
+ basis: [
188
+ {
189
+ a: '3086d221a7d46bcde86c90e49284eb15',
190
+ b: '-e4437ed6010e88286f547fa90abfe4c3'
191
+ },
192
+ {
193
+ a: '114ca50f7a8e2f3f657c1108d9d44cfd8',
194
+ b: '3086d221a7d46bcde86c90e49284eb15'
195
+ }
196
+ ],
197
+
198
+ gRed: false,
199
+ g: [
200
+ '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
201
+ '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
202
+ pre
203
+ ]
204
+ });
@@ -0,0 +1,240 @@
1
+ 'use strict';
2
+
3
+ var BN = require('../../../../BN/bn');
4
+ var HmacDRBG = require('../../hmac-drbg/hmac-drbg');
5
+ var elliptic = require('../../elliptic');
6
+ var utils = elliptic.utils;
7
+ var assert = utils.assert;
8
+
9
+ var KeyPair = require('./key');
10
+ var Signature = require('./signature');
11
+
12
+ function EC(options) {
13
+ if (!(this instanceof EC))
14
+ return new EC(options);
15
+
16
+ // Shortcut `elliptic.ec(curve-name)`
17
+ if (typeof options === 'string') {
18
+ assert(elliptic.curves.hasOwnProperty(options), 'Unknown curve ' + options);
19
+
20
+ options = elliptic.curves[options];
21
+ }
22
+
23
+ // Shortcut for `elliptic.ec(elliptic.curves.curveName)`
24
+ if (options instanceof elliptic.curves.PresetCurve)
25
+ options = { curve: options };
26
+
27
+ this.curve = options.curve.curve;
28
+ this.n = this.curve.n;
29
+ this.nh = this.n.ushrn(1);
30
+ this.g = this.curve.g;
31
+
32
+ // Point on curve
33
+ this.g = options.curve.g;
34
+ this.g.precompute(options.curve.n.bitLength() + 1);
35
+
36
+ // Hash for function for DRBG
37
+ this.hash = options.hash || options.curve.hash;
38
+ }
39
+ module.exports = EC;
40
+
41
+ EC.prototype.keyPair = function keyPair(options) {
42
+ return new KeyPair(this, options);
43
+ };
44
+
45
+ EC.prototype.keyFromPrivate = function keyFromPrivate(priv, enc) {
46
+ return KeyPair.fromPrivate(this, priv, enc);
47
+ };
48
+
49
+ EC.prototype.keyFromPublic = function keyFromPublic(pub, enc) {
50
+ return KeyPair.fromPublic(this, pub, enc);
51
+ };
52
+
53
+ EC.prototype.genKeyPair = function genKeyPair(options) {
54
+ if (!options)
55
+ options = {};
56
+
57
+ // Instantiate Hmac_DRBG
58
+ var drbg = new HmacDRBG({
59
+ hash: this.hash,
60
+ pers: options.pers,
61
+ persEnc: options.persEnc || 'utf8',
62
+ entropy: options.entropy || null, //elliptic.rand(this.hash.hmacStrength),
63
+ entropyEnc: options.entropy && options.entropyEnc || 'utf8',
64
+ nonce: this.n.toArray()
65
+ });
66
+
67
+ var bytes = this.n.byteLength();
68
+ var ns2 = this.n.sub(new BN(2));
69
+ do {
70
+ var priv = new BN(drbg.generate(bytes));
71
+ if (priv.cmp(ns2) > 0)
72
+ continue;
73
+
74
+ priv.iaddn(1);
75
+ return this.keyFromPrivate(priv);
76
+ } while (true);
77
+ };
78
+
79
+ EC.prototype._truncateToN = function truncateToN(msg, truncOnly) {
80
+ var delta = msg.byteLength() * 8 - this.n.bitLength();
81
+ if (delta > 0)
82
+ msg = msg.ushrn(delta);
83
+ if (!truncOnly && msg.cmp(this.n) >= 0)
84
+ return msg.sub(this.n);
85
+ else
86
+ return msg;
87
+ };
88
+
89
+ EC.prototype.sign = function sign(msg, key, enc, options) {
90
+ if (typeof enc === 'object') {
91
+ options = enc;
92
+ enc = null;
93
+ }
94
+ if (!options)
95
+ options = {};
96
+
97
+ key = this.keyFromPrivate(key, enc);
98
+ msg = this._truncateToN(new BN(msg, 16));
99
+
100
+ // Zero-extend key to provide enough entropy
101
+ var bytes = this.n.byteLength();
102
+ var bkey = key.getPrivate().toArray('be', bytes);
103
+
104
+ // Zero-extend nonce to have the same byte size as N
105
+ var nonce = msg.toArray('be', bytes);
106
+
107
+ // Instantiate Hmac_DRBG
108
+ var drbg = new HmacDRBG({
109
+ hash: this.hash,
110
+ entropy: options['extraEntropy'] ? bkey.concat (options['extraEntropy']) : bkey,
111
+ nonce: nonce,
112
+ pers: options.pers,
113
+ persEnc: options.persEnc || 'utf8'
114
+ });
115
+
116
+ // Number of bytes to generate
117
+ var ns1 = this.n.sub(new BN(1));
118
+
119
+ for (var iter = 0; true; iter++) {
120
+ var k = options.k ?
121
+ options.k(iter) :
122
+ new BN(drbg.generate(this.n.byteLength()));
123
+ k = this._truncateToN(k, true);
124
+ if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0)
125
+ continue;
126
+
127
+ var kp = this.g.mul(k);
128
+ if (kp.isInfinity())
129
+ continue;
130
+
131
+ var kpX = kp.getX();
132
+ var r = kpX.umod(this.n);
133
+ if (r.cmpn(0) === 0)
134
+ continue;
135
+
136
+ var s = k.invm(this.n).mul(r.mul(key.getPrivate()).iadd(msg));
137
+ s = s.umod(this.n);
138
+ if (s.cmpn(0) === 0)
139
+ continue;
140
+
141
+ var recoveryParam = (kp.getY().isOdd() ? 1 : 0) |
142
+ (kpX.cmp(r) !== 0 ? 2 : 0);
143
+
144
+ // Use complement of `s`, if it is > `n / 2`
145
+ if (options.canonical && s.cmp(this.nh) > 0) {
146
+ s = this.n.sub(s);
147
+ recoveryParam ^= 1;
148
+ }
149
+
150
+ return new Signature({ r: r, s: s, recoveryParam: recoveryParam });
151
+ }
152
+ };
153
+
154
+ EC.prototype.verify = function verify(msg, signature, key, enc) {
155
+ msg = this._truncateToN(new BN(msg, 16));
156
+ key = this.keyFromPublic(key, enc);
157
+ signature = new Signature(signature, 'hex');
158
+
159
+ // Perform primitive values validation
160
+ var r = signature.r;
161
+ var s = signature.s;
162
+ if (r.cmpn(1) < 0 || r.cmp(this.n) >= 0)
163
+ return false;
164
+ if (s.cmpn(1) < 0 || s.cmp(this.n) >= 0)
165
+ return false;
166
+
167
+ // Validate signature
168
+ var sinv = s.invm(this.n);
169
+ var u1 = sinv.mul(msg).umod(this.n);
170
+ var u2 = sinv.mul(r).umod(this.n);
171
+
172
+ if (!this.curve._maxwellTrick) {
173
+ var p = this.g.mulAdd(u1, key.getPublic(), u2);
174
+ if (p.isInfinity())
175
+ return false;
176
+
177
+ return p.getX().umod(this.n).cmp(r) === 0;
178
+ }
179
+
180
+ // NOTE: Greg Maxwell's trick, inspired by:
181
+ // https://git.io/vad3K
182
+
183
+ var p = this.g.jmulAdd(u1, key.getPublic(), u2);
184
+ if (p.isInfinity())
185
+ return false;
186
+
187
+ // Compare `p.x` of Jacobian point with `r`,
188
+ // this will do `p.x == r * p.z^2` instead of multiplying `p.x` by the
189
+ // inverse of `p.z^2`
190
+ return p.eqXToP(r);
191
+ };
192
+
193
+ EC.prototype.recoverPubKey = function(msg, signature, j, enc) {
194
+ assert((3 & j) === j, 'The recovery param is more than two bits');
195
+ signature = new Signature(signature, enc);
196
+
197
+ var n = this.n;
198
+ var e = new BN(msg);
199
+ var r = signature.r;
200
+ var s = signature.s;
201
+
202
+ // A set LSB signifies that the y-coordinate is odd
203
+ var isYOdd = j & 1;
204
+ var isSecondKey = j >> 1;
205
+ if (r.cmp(this.curve.p.umod(this.curve.n)) >= 0 && isSecondKey)
206
+ throw new Error('Unable to find sencond key candinate');
207
+
208
+ // 1.1. Let x = r + jn.
209
+ if (isSecondKey)
210
+ r = this.curve.pointFromX(r.add(this.curve.n), isYOdd);
211
+ else
212
+ r = this.curve.pointFromX(r, isYOdd);
213
+
214
+ var rInv = signature.r.invm(n);
215
+ var s1 = n.sub(e).mul(rInv).umod(n);
216
+ var s2 = s.mul(rInv).umod(n);
217
+
218
+ // 1.6.1 Compute Q = r^-1 (sR - eG)
219
+ // Q = r^-1 (sR + -eG)
220
+ return this.g.mulAdd(s1, r, s2);
221
+ };
222
+
223
+ EC.prototype.getKeyRecoveryParam = function(e, signature, Q, enc) {
224
+ signature = new Signature(signature, enc);
225
+ if (signature.recoveryParam !== null)
226
+ return signature.recoveryParam;
227
+
228
+ for (var i = 0; i < 4; i++) {
229
+ var Qprime;
230
+ try {
231
+ Qprime = this.recoverPubKey(e, signature, i);
232
+ } catch (e) {
233
+ continue;
234
+ }
235
+
236
+ if (Qprime.eq(Q))
237
+ return i;
238
+ }
239
+ throw new Error('Unable to find valid recovery factor');
240
+ };
@@ -0,0 +1,119 @@
1
+ 'use strict';
2
+
3
+ var BN = require('../../../../BN/bn');
4
+ var elliptic = require('../../elliptic');
5
+ var utils = elliptic.utils;
6
+ var assert = utils.assert;
7
+
8
+ function KeyPair(ec, options) {
9
+ this.ec = ec;
10
+ this.priv = null;
11
+ this.pub = null;
12
+
13
+ // KeyPair(ec, { priv: ..., pub: ... })
14
+ if (options.priv)
15
+ this._importPrivate(options.priv, options.privEnc);
16
+ if (options.pub)
17
+ this._importPublic(options.pub, options.pubEnc);
18
+ }
19
+ module.exports = KeyPair;
20
+
21
+ KeyPair.fromPublic = function fromPublic(ec, pub, enc) {
22
+ if (pub instanceof KeyPair)
23
+ return pub;
24
+
25
+ return new KeyPair(ec, {
26
+ pub: pub,
27
+ pubEnc: enc
28
+ });
29
+ };
30
+
31
+ KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) {
32
+ if (priv instanceof KeyPair)
33
+ return priv;
34
+
35
+ return new KeyPair(ec, {
36
+ priv: priv,
37
+ privEnc: enc
38
+ });
39
+ };
40
+
41
+ KeyPair.prototype.validate = function validate() {
42
+ var pub = this.getPublic();
43
+
44
+ if (pub.isInfinity())
45
+ return { result: false, reason: 'Invalid public key' };
46
+ if (!pub.validate())
47
+ return { result: false, reason: 'Public key is not a point' };
48
+ if (!pub.mul(this.ec.curve.n).isInfinity())
49
+ return { result: false, reason: 'Public key * N != O' };
50
+
51
+ return { result: true, reason: null };
52
+ };
53
+
54
+ KeyPair.prototype.getPublic = function getPublic(compact, enc) {
55
+ // compact is optional argument
56
+ if (typeof compact === 'string') {
57
+ enc = compact;
58
+ compact = null;
59
+ }
60
+
61
+ if (!this.pub)
62
+ this.pub = this.ec.g.mul(this.priv);
63
+
64
+ if (!enc)
65
+ return this.pub;
66
+
67
+ return this.pub.encode(enc, compact);
68
+ };
69
+
70
+ KeyPair.prototype.getPrivate = function getPrivate(enc) {
71
+ if (enc === 'hex')
72
+ return this.priv.toString(16, 2);
73
+ else
74
+ return this.priv;
75
+ };
76
+
77
+ KeyPair.prototype._importPrivate = function _importPrivate(key, enc) {
78
+ this.priv = new BN(key, enc || 16);
79
+
80
+ // Ensure that the priv won't be bigger than n, otherwise we may fail
81
+ // in fixed multiplication method
82
+ this.priv = this.priv.umod(this.ec.curve.n);
83
+ };
84
+
85
+ KeyPair.prototype._importPublic = function _importPublic(key, enc) {
86
+ if (key.x || key.y) {
87
+ // Montgomery points only have an `x` coordinate.
88
+ // Weierstrass/Edwards points on the other hand have both `x` and
89
+ // `y` coordinates.
90
+ if (this.ec.curve.type === 'mont') {
91
+ assert(key.x, 'Need x coordinate');
92
+ } else if (this.ec.curve.type === 'short' ||
93
+ this.ec.curve.type === 'edwards') {
94
+ assert(key.x && key.y, 'Need both x and y coordinate');
95
+ }
96
+ this.pub = this.ec.curve.point(key.x, key.y);
97
+ return;
98
+ }
99
+ this.pub = this.ec.curve.decodePoint(key, enc);
100
+ };
101
+
102
+ // ECDH
103
+ KeyPair.prototype.derive = function derive(pub) {
104
+ return pub.mul(this.priv).getX();
105
+ };
106
+
107
+ // ECDSA
108
+ KeyPair.prototype.sign = function sign(msg, enc, options) {
109
+ return this.ec.sign(msg, this, enc, options);
110
+ };
111
+
112
+ KeyPair.prototype.verify = function verify(msg, signature) {
113
+ return this.ec.verify(msg, signature, this);
114
+ };
115
+
116
+ KeyPair.prototype.inspect = function inspect() {
117
+ return '<Key priv: ' + (this.priv && this.priv.toString(16, 2)) +
118
+ ' pub: ' + (this.pub && this.pub.inspect()) + ' >';
119
+ };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ var BN = require('../../../../BN/bn');
4
+
5
+ var elliptic = require('../../elliptic');
6
+ var utils = elliptic.utils;
7
+ var assert = utils.assert;
8
+
9
+ function Signature(options, enc) {
10
+ if (options instanceof Signature)
11
+ return options;
12
+
13
+ //if (this._importDER(options, enc))
14
+ // return;
15
+
16
+ assert(options.r && options.s, 'Signature without r or s');
17
+ this.r = new BN(options.r, 16);
18
+ this.s = new BN(options.s, 16);
19
+ if (options.recoveryParam === undefined)
20
+ this.recoveryParam = null;
21
+ else
22
+ this.recoveryParam = options.recoveryParam;
23
+ }
24
+ module.exports = Signature;