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,223 @@
1
+ /*!
2
+ * RSA library for Node.js
3
+ *
4
+ * Author: rzcoder
5
+ * License MIT
6
+ */
7
+
8
+ var rsa = require('./libs/rsa.js');
9
+ var _ = require('./utils')._;
10
+ var schemes = require('./schemes/schemes.js');
11
+ var formats = require('./formats/formats.js');
12
+
13
+ module.exports = (function () {
14
+ var SUPPORTED_HASH_ALGORITHMS = {
15
+ browser: ['md5', 'ripemd160', 'sha1', 'sha256', 'sha512']
16
+ };
17
+
18
+ var DEFAULT_ENCRYPTION_SCHEME = 'pkcs1';
19
+ var DEFAULT_SIGNING_SCHEME = 'pkcs1';
20
+
21
+ var EXPORT_FORMAT_ALIASES = {
22
+ 'private': 'pkcs1-private-pem',
23
+ 'private-der': 'pkcs1-private-der',
24
+ 'public': 'pkcs8-public-pem',
25
+ 'public-der': 'pkcs8-public-der',
26
+ };
27
+
28
+ /**
29
+ * @param key {string|buffer|object} Key in PEM format, or data for generate key {b: bits, e: exponent}
30
+ * @constructor
31
+ */
32
+ function NodeRSA(key, format, options) {
33
+ if (!(this instanceof NodeRSA)) {
34
+ return new NodeRSA(key, format, options);
35
+ }
36
+
37
+ if (_.isObject(format)) {
38
+ options = format;
39
+ format = undefined;
40
+ }
41
+
42
+ this.$options = {
43
+ signingScheme: DEFAULT_SIGNING_SCHEME,
44
+ signingSchemeOptions: {
45
+ hash: 'sha256',
46
+ saltLength: null
47
+ },
48
+ encryptionScheme: DEFAULT_ENCRYPTION_SCHEME,
49
+ encryptionSchemeOptions: {
50
+ hash: 'sha1',
51
+ label: null
52
+ },
53
+ environment: 'browser',
54
+ rsaUtils: this
55
+ };
56
+ this.keyPair = new rsa.Key();
57
+ this.$cache = {};
58
+
59
+ if (Buffer.isBuffer(key) || _.isString(key)) {
60
+ this.importKey(key, format);
61
+ } else if (_.isObject(key)) {
62
+ this.generateKeyPair(key.b, key.e);
63
+ }
64
+
65
+ this.setOptions(options);
66
+ }
67
+
68
+ /**
69
+ * Set and validate options for key instance
70
+ * @param options
71
+ */
72
+ NodeRSA.prototype.setOptions = function (options) {
73
+ options = options || {};
74
+ if (options.environment) {
75
+ this.$options.environment = options.environment;
76
+ }
77
+
78
+ if (options.signingScheme) {
79
+ if (_.isString(options.signingScheme)) {
80
+ var signingScheme = options.signingScheme.toLowerCase().split('-');
81
+ if (signingScheme.length == 1) {
82
+ if (SUPPORTED_HASH_ALGORITHMS.node.indexOf(signingScheme[0]) > -1) {
83
+ this.$options.signingSchemeOptions = {
84
+ hash: signingScheme[0]
85
+ };
86
+ this.$options.signingScheme = DEFAULT_SIGNING_SCHEME;
87
+ } else {
88
+ this.$options.signingScheme = signingScheme[0];
89
+ this.$options.signingSchemeOptions = {
90
+ hash: null
91
+ };
92
+ }
93
+ } else {
94
+ this.$options.signingSchemeOptions = {
95
+ hash: signingScheme[1]
96
+ };
97
+ this.$options.signingScheme = signingScheme[0];
98
+ }
99
+ } else if (_.isObject(options.signingScheme)) {
100
+ this.$options.signingScheme = options.signingScheme.scheme || DEFAULT_SIGNING_SCHEME;
101
+ this.$options.signingSchemeOptions = _.omit(options.signingScheme, 'scheme');
102
+ }
103
+
104
+ if (!schemes.isSignature(this.$options.signingScheme)) {
105
+ throw Error('Unsupported signing scheme');
106
+ }
107
+
108
+ if (this.$options.signingSchemeOptions.hash &&
109
+ SUPPORTED_HASH_ALGORITHMS[this.$options.environment].indexOf(this.$options.signingSchemeOptions.hash) === -1) {
110
+ throw Error('Unsupported hashing algorithm for ' + this.$options.environment + ' environment');
111
+ }
112
+ }
113
+
114
+ if (options.encryptionScheme) {
115
+ if (_.isString(options.encryptionScheme)) {
116
+ this.$options.encryptionScheme = options.encryptionScheme.toLowerCase();
117
+ this.$options.encryptionSchemeOptions = {};
118
+ } else if (_.isObject(options.encryptionScheme)) {
119
+ this.$options.encryptionScheme = options.encryptionScheme.scheme || DEFAULT_ENCRYPTION_SCHEME;
120
+ this.$options.encryptionSchemeOptions = _.omit(options.encryptionScheme, 'scheme');
121
+ }
122
+
123
+ if (!schemes.isEncryption(this.$options.encryptionScheme)) {
124
+ throw Error('Unsupported encryption scheme');
125
+ }
126
+
127
+ if (this.$options.encryptionSchemeOptions.hash &&
128
+ SUPPORTED_HASH_ALGORITHMS[this.$options.environment].indexOf(this.$options.encryptionSchemeOptions.hash) === -1) {
129
+ throw Error('Unsupported hashing algorithm for ' + this.$options.environment + ' environment');
130
+ }
131
+ }
132
+
133
+ this.keyPair.setOptions(this.$options);
134
+ };
135
+
136
+ /**
137
+ * Importing key
138
+ * @param keyData {string|buffer|Object}
139
+ * @param format {string}
140
+ */
141
+ NodeRSA.prototype.importKey = function (keyData, format) {
142
+ if (!keyData) {
143
+ throw Error("Empty key given");
144
+ }
145
+
146
+ if (format) {
147
+ format = EXPORT_FORMAT_ALIASES[format] || format;
148
+ }
149
+
150
+ if (!formats.detectAndImport(this.keyPair, keyData, format) && format === undefined) {
151
+ throw Error("Key format must be specified");
152
+ }
153
+
154
+ this.$cache = {};
155
+
156
+ return this;
157
+ };
158
+
159
+ /**
160
+ * Check if key pair contains private key
161
+ */
162
+ NodeRSA.prototype.isPrivate = function () {
163
+ return this.keyPair.isPrivate();
164
+ };
165
+
166
+ /**
167
+ * Check if key pair contains public key
168
+ * @param [strict] {boolean} - public key only, return false if have private exponent
169
+ */
170
+ NodeRSA.prototype.isPublic = function (strict) {
171
+ return this.keyPair.isPublic(strict);
172
+ };
173
+
174
+ /**
175
+ * Check if key pair doesn't contains any data
176
+ */
177
+ NodeRSA.prototype.isEmpty = function (strict) {
178
+ return !(this.keyPair.n || this.keyPair.e || this.keyPair.d);
179
+ };
180
+
181
+ /**
182
+ * Signing data
183
+ *
184
+ * @param buffer {string|number|object|array|Buffer} - data for signing. Object and array will convert to JSON string.
185
+ * @param encoding {string} - optional. Encoding for output result, may be 'buffer', 'binary', 'hex' or 'base64'. Default 'buffer'.
186
+ * @param source_encoding {string} - optional. Encoding for given string. Default utf8.
187
+ * @returns {string|Buffer}
188
+ */
189
+ NodeRSA.prototype.sign = function (buffer, encoding, source_encoding) {
190
+ if (!this.isPrivate()) {
191
+ throw Error("This is not private key");
192
+ }
193
+
194
+ var res = this.keyPair.sign(this.$getDataForEncrypt(buffer, source_encoding));
195
+
196
+ if (encoding && encoding != 'buffer') {
197
+ res = res.toString(encoding);
198
+ }
199
+
200
+ return res;
201
+ };
202
+
203
+ /**
204
+ * Preparing given data for encrypting/signing. Just make new/return Buffer object.
205
+ *
206
+ * @param buffer {string|number|object|array|Buffer} - data for encrypting. Object and array will convert to JSON string.
207
+ * @param encoding {string} - optional. Encoding for given string. Default utf8.
208
+ * @returns {Buffer}
209
+ */
210
+ NodeRSA.prototype.$getDataForEncrypt = function (buffer, encoding) {
211
+ if (_.isString(buffer) || _.isNumber(buffer)) {
212
+ return Buffer.from('' + buffer, encoding || 'utf8');
213
+ } else if (Buffer.isBuffer(buffer)) {
214
+ return buffer;
215
+ } else if (_.isObject(buffer)) {
216
+ return Buffer.from(JSON.stringify(buffer));
217
+ } else {
218
+ throw Error("Unexpected data type");
219
+ }
220
+ };
221
+
222
+ return NodeRSA;
223
+ })();
@@ -0,0 +1,13 @@
1
+ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
2
+
3
+
4
+ module.exports = {
5
+
6
+ newInvalidAsn1Error: function (msg) {
7
+ var e = new Error();
8
+ e.name = 'InvalidAsn1Error';
9
+ e.message = msg || '';
10
+ return e;
11
+ }
12
+
13
+ };
@@ -0,0 +1,21 @@
1
+ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
2
+
3
+ var errors = require('./errors');
4
+ var types = require('./types');
5
+
6
+ var Reader = require('./reader');
7
+
8
+ // --- Exports
9
+
10
+ module.exports = {
11
+ Reader: Reader,
12
+ };
13
+
14
+ for (var t in types) {
15
+ if (types.hasOwnProperty(t))
16
+ module.exports[t] = types[t];
17
+ }
18
+ for (var e in errors) {
19
+ if (errors.hasOwnProperty(e))
20
+ module.exports[e] = errors[e];
21
+ }
@@ -0,0 +1,262 @@
1
+ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
2
+
3
+ var assert = require('assert');
4
+ // var Buffer = require('safer-buffer').Buffer;
5
+
6
+ var ASN1 = require('./types');
7
+ var errors = require('./errors');
8
+
9
+
10
+ // --- Globals
11
+
12
+ var newInvalidAsn1Error = errors.newInvalidAsn1Error;
13
+
14
+
15
+
16
+ // --- API
17
+
18
+ function Reader(data) {
19
+ if (!data || !Buffer.isBuffer(data))
20
+ throw new TypeError('data must be a node Buffer');
21
+
22
+ this._buf = data;
23
+ this._size = data.length;
24
+
25
+ // These hold the "current" state
26
+ this._len = 0;
27
+ this._offset = 0;
28
+ }
29
+
30
+ Object.defineProperty(Reader.prototype, 'length', {
31
+ enumerable: true,
32
+ get: function () { return (this._len); }
33
+ });
34
+
35
+ Object.defineProperty(Reader.prototype, 'offset', {
36
+ enumerable: true,
37
+ get: function () { return (this._offset); }
38
+ });
39
+
40
+ Object.defineProperty(Reader.prototype, 'remain', {
41
+ get: function () { return (this._size - this._offset); }
42
+ });
43
+
44
+ Object.defineProperty(Reader.prototype, 'buffer', {
45
+ get: function () { return (this._buf.slice(this._offset)); }
46
+ });
47
+
48
+
49
+ /**
50
+ * Reads a single byte and advances offset; you can pass in `true` to make this
51
+ * a "peek" operation (i.e., get the byte, but don't advance the offset).
52
+ *
53
+ * @param {Boolean} peek true means don't move offset.
54
+ * @return {Number} the next byte, null if not enough data.
55
+ */
56
+ Reader.prototype.readByte = function (peek) {
57
+ if (this._size - this._offset < 1)
58
+ return null;
59
+
60
+ var b = this._buf[this._offset] & 0xff;
61
+
62
+ if (!peek)
63
+ this._offset += 1;
64
+
65
+ return b;
66
+ };
67
+
68
+
69
+ Reader.prototype.peek = function () {
70
+ return this.readByte(true);
71
+ };
72
+
73
+
74
+ /**
75
+ * Reads a (potentially) variable length off the BER buffer. This call is
76
+ * not really meant to be called directly, as callers have to manipulate
77
+ * the internal buffer afterwards.
78
+ *
79
+ * As a result of this call, you can call `Reader.length`, until the
80
+ * next thing called that does a readLength.
81
+ *
82
+ * @return {Number} the amount of offset to advance the buffer.
83
+ * @throws {InvalidAsn1Error} on bad ASN.1
84
+ */
85
+ Reader.prototype.readLength = function (offset) {
86
+ if (offset === undefined)
87
+ offset = this._offset;
88
+
89
+ if (offset >= this._size)
90
+ return null;
91
+
92
+ var lenB = this._buf[offset++] & 0xff;
93
+ if (lenB === null)
94
+ return null;
95
+
96
+ if ((lenB & 0x80) === 0x80) {
97
+ lenB &= 0x7f;
98
+
99
+ if (lenB === 0)
100
+ throw newInvalidAsn1Error('Indefinite length not supported');
101
+
102
+ if (lenB > 4)
103
+ throw newInvalidAsn1Error('encoding too long');
104
+
105
+ if (this._size - offset < lenB)
106
+ return null;
107
+
108
+ this._len = 0;
109
+ for (var i = 0; i < lenB; i++)
110
+ this._len = (this._len << 8) + (this._buf[offset++] & 0xff);
111
+
112
+ } else {
113
+ // Wasn't a variable length
114
+ this._len = lenB;
115
+ }
116
+
117
+ return offset;
118
+ };
119
+
120
+
121
+ /**
122
+ * Parses the next sequence in this BER buffer.
123
+ *
124
+ * To get the length of the sequence, call `Reader.length`.
125
+ *
126
+ * @return {Number} the sequence's tag.
127
+ */
128
+ Reader.prototype.readSequence = function (tag) {
129
+ var seq = this.peek();
130
+ if (seq === null)
131
+ return null;
132
+ if (tag !== undefined && tag !== seq)
133
+ throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
134
+ ': got 0x' + seq.toString(16));
135
+
136
+ var o = this.readLength(this._offset + 1); // stored in `length`
137
+ if (o === null)
138
+ return null;
139
+
140
+ this._offset = o;
141
+ return seq;
142
+ };
143
+
144
+
145
+ Reader.prototype.readInt = function () {
146
+ return this._readTag(ASN1.Integer);
147
+ };
148
+
149
+
150
+ Reader.prototype.readBoolean = function () {
151
+ return (this._readTag(ASN1.Boolean) === 0 ? false : true);
152
+ };
153
+
154
+
155
+ Reader.prototype.readEnumeration = function () {
156
+ return this._readTag(ASN1.Enumeration);
157
+ };
158
+
159
+
160
+ Reader.prototype.readString = function (tag, retbuf) {
161
+ if (!tag)
162
+ tag = ASN1.OctetString;
163
+
164
+ var b = this.peek();
165
+ if (b === null)
166
+ return null;
167
+
168
+ if (b !== tag)
169
+ throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
170
+ ': got 0x' + b.toString(16));
171
+
172
+ var o = this.readLength(this._offset + 1); // stored in `length`
173
+
174
+ if (o === null)
175
+ return null;
176
+
177
+ if (this.length > this._size - o)
178
+ return null;
179
+
180
+ this._offset = o;
181
+
182
+ if (this.length === 0)
183
+ return retbuf ? Buffer.alloc(0) : '';
184
+
185
+ var str = this._buf.slice(this._offset, this._offset + this.length);
186
+ this._offset += this.length;
187
+
188
+ return retbuf ? str : str.toString('utf8');
189
+ };
190
+
191
+ Reader.prototype.readOID = function (tag) {
192
+ if (!tag)
193
+ tag = ASN1.OID;
194
+
195
+ var b = this.readString(tag, true);
196
+ if (b === null)
197
+ return null;
198
+
199
+ var values = [];
200
+ var value = 0;
201
+
202
+ for (var i = 0; i < b.length; i++) {
203
+ var byte = b[i] & 0xff;
204
+
205
+ value <<= 7;
206
+ value += byte & 0x7f;
207
+ if ((byte & 0x80) === 0) {
208
+ values.push(value);
209
+ value = 0;
210
+ }
211
+ }
212
+
213
+ value = values.shift();
214
+ values.unshift(value % 40);
215
+ values.unshift((value / 40) >> 0);
216
+
217
+ return values.join('.');
218
+ };
219
+
220
+
221
+ Reader.prototype._readTag = function (tag) {
222
+ assert.ok(tag !== undefined);
223
+
224
+ var b = this.peek();
225
+
226
+ if (b === null)
227
+ return null;
228
+
229
+ if (b !== tag)
230
+ throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
231
+ ': got 0x' + b.toString(16));
232
+
233
+ var o = this.readLength(this._offset + 1); // stored in `length`
234
+ if (o === null)
235
+ return null;
236
+
237
+ if (this.length > 4)
238
+ throw newInvalidAsn1Error('Integer too long: ' + this.length);
239
+
240
+ if (this.length > this._size - o)
241
+ return null;
242
+ this._offset = o;
243
+
244
+ var fb = this._buf[this._offset];
245
+ var value = 0;
246
+
247
+ for (var i = 0; i < this.length; i++) {
248
+ value <<= 8;
249
+ value |= (this._buf[this._offset++] & 0xff);
250
+ }
251
+
252
+ if ((fb & 0x80) === 0x80 && i !== 4)
253
+ value -= (1 << (i * 8));
254
+
255
+ return value >> 0;
256
+ };
257
+
258
+
259
+
260
+ // --- Exported API
261
+
262
+ module.exports = Reader;
@@ -0,0 +1,36 @@
1
+ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
2
+
3
+
4
+ module.exports = {
5
+ EOC: 0,
6
+ Boolean: 1,
7
+ Integer: 2,
8
+ BitString: 3,
9
+ OctetString: 4,
10
+ Null: 5,
11
+ OID: 6,
12
+ ObjectDescriptor: 7,
13
+ External: 8,
14
+ Real: 9, // float
15
+ Enumeration: 10,
16
+ PDV: 11,
17
+ Utf8String: 12,
18
+ RelativeOID: 13,
19
+ Sequence: 16,
20
+ Set: 17,
21
+ NumericString: 18,
22
+ PrintableString: 19,
23
+ T61String: 20,
24
+ VideotexString: 21,
25
+ IA5String: 22,
26
+ UTCTime: 23,
27
+ GeneralizedTime: 24,
28
+ GraphicString: 25,
29
+ VisibleString: 26,
30
+ GeneralString: 28,
31
+ UniversalString: 29,
32
+ CharacterString: 30,
33
+ BMPString: 31,
34
+ Constructor: 32,
35
+ Context: 128
36
+ };
@@ -0,0 +1,17 @@
1
+ // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
2
+
3
+ // If you have no idea what ASN.1 or BER is, see this:
4
+ // ftp://ftp.rsa.com/pub/pkcs/ascii/layman.asc
5
+
6
+ var Ber = require('./ber/index');
7
+
8
+
9
+
10
+ // --- Exported API
11
+
12
+ module.exports = {
13
+
14
+ Ber: Ber,
15
+
16
+ BerReader: Ber.Reader,
17
+ };
@@ -0,0 +1,34 @@
1
+ var BigInteger = require('../libs/jsbn.js');
2
+ var schemes = require('../schemes/schemes.js');
3
+
4
+ module.exports = function (keyPair, options) {
5
+ var pkcs1Scheme = schemes.pkcs1.makeScheme(keyPair, options);
6
+
7
+ return {
8
+ encrypt: function (buffer, usePrivate) {
9
+ var m, c;
10
+ if (usePrivate) {
11
+ /* Type 1: zeros padding for private key encrypt */
12
+ m = new BigInteger(pkcs1Scheme.encPad(buffer, {type: 1}));
13
+ c = keyPair.$doPrivate(m);
14
+ } else {
15
+ m = new BigInteger(keyPair.encryptionScheme.encPad(buffer));
16
+ c = keyPair.$doPublic(m);
17
+ }
18
+ return c.toBuffer(keyPair.encryptedDataLength);
19
+ },
20
+
21
+ decrypt: function (buffer, usePublic) {
22
+ var m, c = new BigInteger(buffer);
23
+
24
+ if (usePublic) {
25
+ m = keyPair.$doPublic(c);
26
+ /* Type 1: zeros padding for private key decrypt */
27
+ return pkcs1Scheme.encUnPad(m.toBuffer(keyPair.encryptedDataLength), {type: 1});
28
+ } else {
29
+ m = keyPair.$doPrivate(c);
30
+ return keyPair.encryptionScheme.encUnPad(m.toBuffer(keyPair.encryptedDataLength));
31
+ }
32
+ }
33
+ };
34
+ };
@@ -0,0 +1,71 @@
1
+ var _ = require('../utils')._;
2
+ var utils = require('../utils');
3
+
4
+ module.exports = {
5
+ privateExport: function (key, options) {
6
+ return {
7
+ n: key.n.toBuffer(),
8
+ e: key.e,
9
+ d: key.d.toBuffer(),
10
+ p: key.p.toBuffer(),
11
+ q: key.q.toBuffer(),
12
+ dmp1: key.dmp1.toBuffer(),
13
+ dmq1: key.dmq1.toBuffer(),
14
+ coeff: key.coeff.toBuffer()
15
+ };
16
+ },
17
+
18
+ privateImport: function (key, data, options) {
19
+ if (data.n && data.e && data.d && data.p && data.q && data.dmp1 && data.dmq1 && data.coeff) {
20
+ key.setPrivate(
21
+ data.n,
22
+ data.e,
23
+ data.d,
24
+ data.p,
25
+ data.q,
26
+ data.dmp1,
27
+ data.dmq1,
28
+ data.coeff
29
+ );
30
+ } else {
31
+ throw Error("Invalid key data");
32
+ }
33
+ },
34
+
35
+ publicExport: function (key, options) {
36
+ return {
37
+ n: key.n.toBuffer(),
38
+ e: key.e
39
+ };
40
+ },
41
+
42
+ publicImport: function (key, data, options) {
43
+ if (data.n && data.e) {
44
+ key.setPublic(
45
+ data.n,
46
+ data.e
47
+ );
48
+ } else {
49
+ throw Error("Invalid key data");
50
+ }
51
+ },
52
+
53
+ /**
54
+ * Trying autodetect and import key
55
+ * @param key
56
+ * @param data
57
+ */
58
+ autoImport: function (key, data) {
59
+ if (data.n && data.e) {
60
+ if (data.d && data.p && data.q && data.dmp1 && data.dmq1 && data.coeff) {
61
+ module.exports.privateImport(key, data);
62
+ return true;
63
+ } else {
64
+ module.exports.publicImport(key, data);
65
+ return true;
66
+ }
67
+ }
68
+
69
+ return false;
70
+ }
71
+ };