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.
- package/.cache/eslintcache +1 -0
- package/.dockerignore +6 -0
- package/.eslintignore +1 -0
- package/.gitattributes +5 -0
- package/.readthedocs.yaml +16 -0
- package/CONTRIBUTING.md +1049 -0
- package/LICENSE.txt +21 -0
- package/README.md +537 -0
- package/SECURITY.md +5 -0
- package/build/cleanup-old-tags.js +94 -0
- package/build/countries.js +256 -0
- package/build/export-exchanges.js +520 -0
- package/build/fs.js +51 -0
- package/build/transpile.js +1772 -0
- package/build/vss.js +78 -0
- package/ccxt.browser.js +7 -0
- package/ccxt.d.ts +692 -0
- package/ccxt.js +171 -0
- package/cleanup.sh +2 -0
- package/composer-install.sh +20 -0
- package/dist/ccxt.browser.js +208383 -0
- package/gource.sh +3 -0
- package/index.html +7 -0
- package/js/.eslintrc +87 -0
- package/js/aax.js +2686 -0
- package/js/ascendex.js +2584 -0
- package/js/base/.eslintrc.js +43 -0
- package/js/base/Exchange.js +2371 -0
- package/js/base/Precise.js +283 -0
- package/js/base/errorHierarchy.js +47 -0
- package/js/base/errors.js +55 -0
- package/js/base/functions/crypto.js +158 -0
- package/js/base/functions/encode.js +118 -0
- package/js/base/functions/generic.js +270 -0
- package/js/base/functions/misc.js +138 -0
- package/js/base/functions/number.js +329 -0
- package/js/base/functions/platform.js +38 -0
- package/js/base/functions/string.js +21 -0
- package/js/base/functions/throttle.js +79 -0
- package/js/base/functions/time.js +210 -0
- package/js/base/functions/type.js +66 -0
- package/js/base/functions.js +28 -0
- package/js/bequant.js +32 -0
- package/js/bibox.js +1407 -0
- package/js/bigone.js +1366 -0
- package/js/binance.js +5652 -0
- package/js/binancecoinm.js +46 -0
- package/js/binanceus.js +46 -0
- package/js/binanceusdm.js +49 -0
- package/js/bit2c.js +535 -0
- package/js/bitbank.js +842 -0
- package/js/bitbay.js +16 -0
- package/js/bitbns.js +1073 -0
- package/js/bitcoincom.js +15 -0
- package/js/bitfinex.js +1433 -0
- package/js/bitfinex2.js +2025 -0
- package/js/bitflyer.js +840 -0
- package/js/bitforex.js +614 -0
- package/js/bitget.js +2397 -0
- package/js/bithumb.js +980 -0
- package/js/bitmart.js +2516 -0
- package/js/bitmex.js +1809 -0
- package/js/bitopro.js +1443 -0
- package/js/bitpanda.js +1782 -0
- package/js/bitrue.js +1747 -0
- package/js/bitso.js +1062 -0
- package/js/bitstamp.js +1757 -0
- package/js/bitstamp1.js +343 -0
- package/js/bittrex.js +1876 -0
- package/js/bitvavo.js +1579 -0
- package/js/bkex.js +1233 -0
- package/js/bl3p.js +346 -0
- package/js/blockchaincom.js +969 -0
- package/js/btcalpha.js +680 -0
- package/js/btcbox.js +477 -0
- package/js/btcmarkets.js +1022 -0
- package/js/btctradeua.js +466 -0
- package/js/btcturk.js +734 -0
- package/js/buda.js +946 -0
- package/js/bw.js +1265 -0
- package/js/bybit.js +3372 -0
- package/js/bytetrade.js +1336 -0
- package/js/cdax.js +1646 -0
- package/js/cex.js +1410 -0
- package/js/coinbase.js +1342 -0
- package/js/coinbaseprime.js +31 -0
- package/js/coinbasepro.js +1466 -0
- package/js/coincheck.js +755 -0
- package/js/coinex.js +3400 -0
- package/js/coinfalcon.js +880 -0
- package/js/coinmate.js +794 -0
- package/js/coinone.js +816 -0
- package/js/coinspot.js +345 -0
- package/js/crex24.js +1636 -0
- package/js/cryptocom.js +1832 -0
- package/js/currencycom.js +1748 -0
- package/js/delta.js +1547 -0
- package/js/deribit.js +2148 -0
- package/js/digifinex.js +1585 -0
- package/js/eqonex.js +1660 -0
- package/js/exmo.js +1670 -0
- package/js/fairdesk.js +1231 -0
- package/js/flowbtc.js +35 -0
- package/js/fmfwio.js +34 -0
- package/js/ftx.js +2751 -0
- package/js/ftxus.js +38 -0
- package/js/gateio.js +4174 -0
- package/js/gemini.js +1397 -0
- package/js/hitbtc.js +1343 -0
- package/js/hitbtc3.js +2329 -0
- package/js/hollaex.js +1486 -0
- package/js/huobi.js +5706 -0
- package/js/huobijp.js +1710 -0
- package/js/huobipro.js +18 -0
- package/js/idex.js +1439 -0
- package/js/independentreserve.js +649 -0
- package/js/indodax.js +742 -0
- package/js/itbit.js +722 -0
- package/js/kraken.js +2179 -0
- package/js/kucoin.js +2571 -0
- package/js/kucoinfutures.js +1771 -0
- package/js/kuna.js +809 -0
- package/js/latoken.js +1445 -0
- package/js/lbank.js +760 -0
- package/js/liquid.js +1432 -0
- package/js/luno.js +873 -0
- package/js/lykke.js +1147 -0
- package/js/mercado.js +771 -0
- package/js/mexc.js +3151 -0
- package/js/ndax.js +2233 -0
- package/js/novadax.js +1318 -0
- package/js/oceanex.js +816 -0
- package/js/okcoin.js +3841 -0
- package/js/okex.js +16 -0
- package/js/okex5.js +16 -0
- package/js/okx.js +4795 -0
- package/js/paymium.js +498 -0
- package/js/phemex.js +2957 -0
- package/js/poloniex.js +1674 -0
- package/js/probit.js +1346 -0
- package/js/qtrade.js +1588 -0
- package/js/ripio.js +1061 -0
- package/js/static_dependencies/BN/bn.js +3526 -0
- package/js/static_dependencies/README.md +1 -0
- package/js/static_dependencies/crypto-js/crypto-js.js +5988 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/base.js +375 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/edwards.js +433 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/index.js +8 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/mont.js +180 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/short.js +938 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curves.js +204 -0
- package/js/static_dependencies/elliptic/lib/elliptic/ec/index.js +240 -0
- package/js/static_dependencies/elliptic/lib/elliptic/ec/key.js +119 -0
- package/js/static_dependencies/elliptic/lib/elliptic/ec/signature.js +24 -0
- package/js/static_dependencies/elliptic/lib/elliptic/eddsa/index.js +145 -0
- package/js/static_dependencies/elliptic/lib/elliptic/eddsa/key.js +100 -0
- package/js/static_dependencies/elliptic/lib/elliptic/eddsa/signature.js +65 -0
- package/js/static_dependencies/elliptic/lib/elliptic/precomputed/secp256k1.js +780 -0
- package/js/static_dependencies/elliptic/lib/elliptic/utils.js +214 -0
- package/js/static_dependencies/elliptic/lib/elliptic.js +22 -0
- package/js/static_dependencies/elliptic/lib/hmac-drbg/hmac-drbg.js +114 -0
- package/js/static_dependencies/fetch-ponyfill/fetch-node.js +39 -0
- package/js/static_dependencies/node-fetch/index.js +1564 -0
- package/js/static_dependencies/node-rsa/NodeRSA.js +223 -0
- package/js/static_dependencies/node-rsa/asn1/ber/errors.js +13 -0
- package/js/static_dependencies/node-rsa/asn1/ber/index.js +21 -0
- package/js/static_dependencies/node-rsa/asn1/ber/reader.js +262 -0
- package/js/static_dependencies/node-rsa/asn1/ber/types.js +36 -0
- package/js/static_dependencies/node-rsa/asn1/index.js +17 -0
- package/js/static_dependencies/node-rsa/encryptEngines/js.js +34 -0
- package/js/static_dependencies/node-rsa/formats/components.js +71 -0
- package/js/static_dependencies/node-rsa/formats/formats.js +31 -0
- package/js/static_dependencies/node-rsa/formats/pkcs1.js +148 -0
- package/js/static_dependencies/node-rsa/formats/pkcs8.js +187 -0
- package/js/static_dependencies/node-rsa/libs/jsbn.js +1252 -0
- package/js/static_dependencies/node-rsa/libs/rsa.js +147 -0
- package/js/static_dependencies/node-rsa/schemes/pkcs1.js +176 -0
- package/js/static_dependencies/node-rsa/schemes/schemes.js +21 -0
- package/js/static_dependencies/node-rsa/utils.js +98 -0
- package/js/static_dependencies/qs/formats.js +18 -0
- package/js/static_dependencies/qs/index.js +11 -0
- package/js/static_dependencies/qs/parse.js +242 -0
- package/js/static_dependencies/qs/stringify.js +269 -0
- package/js/static_dependencies/qs/utils.js +230 -0
- package/js/stex.js +1925 -0
- package/js/test/.eslintrc.js +42 -0
- package/js/test/Exchange/test.balance.js +61 -0
- package/js/test/Exchange/test.borrowRate.js +32 -0
- package/js/test/Exchange/test.currency.js +52 -0
- package/js/test/Exchange/test.fetchBalance.js +23 -0
- package/js/test/Exchange/test.fetchBorrowInterest.js +59 -0
- package/js/test/Exchange/test.fetchBorrowRate.js +32 -0
- package/js/test/Exchange/test.fetchBorrowRates.js +28 -0
- package/js/test/Exchange/test.fetchClosedOrders.js +32 -0
- package/js/test/Exchange/test.fetchCurrencies.js +35 -0
- package/js/test/Exchange/test.fetchDeposits.js +31 -0
- package/js/test/Exchange/test.fetchFundingFees.js +19 -0
- package/js/test/Exchange/test.fetchFundingRateHistory.js +40 -0
- package/js/test/Exchange/test.fetchL2OrderBook.js +23 -0
- package/js/test/Exchange/test.fetchLedger.js +42 -0
- package/js/test/Exchange/test.fetchLeverageTiers.js +33 -0
- package/js/test/Exchange/test.fetchMarketLeverageTiers.js +22 -0
- package/js/test/Exchange/test.fetchMarkets.js +33 -0
- package/js/test/Exchange/test.fetchMyTrades.js +42 -0
- package/js/test/Exchange/test.fetchOHLCV.js +46 -0
- package/js/test/Exchange/test.fetchOpenOrders.js +36 -0
- package/js/test/Exchange/test.fetchOrderBook.js +25 -0
- package/js/test/Exchange/test.fetchOrderBooks.js +35 -0
- package/js/test/Exchange/test.fetchOrders.js +41 -0
- package/js/test/Exchange/test.fetchPositions.js +47 -0
- package/js/test/Exchange/test.fetchStatus.js +35 -0
- package/js/test/Exchange/test.fetchTicker.js +38 -0
- package/js/test/Exchange/test.fetchTickers.js +49 -0
- package/js/test/Exchange/test.fetchTrades.js +39 -0
- package/js/test/Exchange/test.fetchTradingFee.js +18 -0
- package/js/test/Exchange/test.fetchTradingFees.js +22 -0
- package/js/test/Exchange/test.fetchTransactions.js +31 -0
- package/js/test/Exchange/test.fetchWithdrawals.js +31 -0
- package/js/test/Exchange/test.ledgerItem.js +46 -0
- package/js/test/Exchange/test.leverageTier.js +33 -0
- package/js/test/Exchange/test.loadMarkets.js +35 -0
- package/js/test/Exchange/test.market.js +129 -0
- package/js/test/Exchange/test.ohlcv.js +33 -0
- package/js/test/Exchange/test.order.js +62 -0
- package/js/test/Exchange/test.orderbook.js +61 -0
- package/js/test/Exchange/test.position.js +21 -0
- package/js/test/Exchange/test.throttle.js +94 -0
- package/js/test/Exchange/test.ticker.js +95 -0
- package/js/test/Exchange/test.trade.js +68 -0
- package/js/test/Exchange/test.tradingFee.js +34 -0
- package/js/test/Exchange/test.transaction.js +35 -0
- package/js/test/base/.eslintrc +38 -0
- package/js/test/base/functions/test.crypto.js +110 -0
- package/js/test/base/functions/test.datetime.js +62 -0
- package/js/test/base/functions/test.generic.js +152 -0
- package/js/test/base/functions/test.number.js +362 -0
- package/js/test/base/functions/test.time.js +56 -0
- package/js/test/base/functions/test.type.js +53 -0
- package/js/test/base/test.base.js +193 -0
- package/js/test/errors/test.InsufficientFunds.js +86 -0
- package/js/test/errors/test.InvalidNonce.js +64 -0
- package/js/test/errors/test.InvalidOrder.js +35 -0
- package/js/test/errors/test.OrderNotFound.js +39 -0
- package/js/test/test.js +426 -0
- package/js/test/test.timeout_hang.js +12 -0
- package/js/therock.js +1431 -0
- package/js/tidebit.js +632 -0
- package/js/tidex.js +939 -0
- package/js/timex.js +1283 -0
- package/js/upbit.js +1622 -0
- package/js/vcc.js +1353 -0
- package/js/wavesexchange.js +2185 -0
- package/js/wazirx.js +732 -0
- package/js/whitebit.js +1352 -0
- package/js/woo.js +1577 -0
- package/js/xena.js +1948 -0
- package/js/yobit.js +1129 -0
- package/js/zaif.js +647 -0
- package/js/zb.js +4088 -0
- package/js/zipmex.js +40 -0
- package/js/zonda.js +1497 -0
- package/multilang.sh +159 -0
- package/package.json +591 -0
- package/postinstall.js +103 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
var _ = require('../utils')._;
|
2
|
+
|
3
|
+
module.exports = {
|
4
|
+
pkcs1: require('./pkcs1'),
|
5
|
+
pkcs8: require('./pkcs8'),
|
6
|
+
components: require('./components'),
|
7
|
+
|
8
|
+
detectAndImport: function (key, data, format) {
|
9
|
+
if (format === undefined) {
|
10
|
+
for (var scheme in module.exports) {
|
11
|
+
if (typeof module.exports[scheme].autoImport === 'function' && module.exports[scheme].autoImport(key, data)) {
|
12
|
+
return true;
|
13
|
+
}
|
14
|
+
}
|
15
|
+
} else if (format) {
|
16
|
+
var fmt = formatParse(format);
|
17
|
+
|
18
|
+
if (module.exports[fmt.scheme]) {
|
19
|
+
if (fmt.keyType === 'private') {
|
20
|
+
module.exports[fmt.scheme].privateImport(key, data, fmt.keyOpt);
|
21
|
+
} else {
|
22
|
+
module.exports[fmt.scheme].publicImport(key, data, fmt.keyOpt);
|
23
|
+
}
|
24
|
+
} else {
|
25
|
+
throw Error('Unsupported key format');
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
return false;
|
30
|
+
},
|
31
|
+
};
|
@@ -0,0 +1,148 @@
|
|
1
|
+
var ber = require('../asn1/index').Ber;
|
2
|
+
var _ = require('../utils')._;
|
3
|
+
var utils = require('../utils');
|
4
|
+
|
5
|
+
const PRIVATE_OPENING_BOUNDARY = '-----BEGIN RSA PRIVATE KEY-----';
|
6
|
+
const PRIVATE_CLOSING_BOUNDARY = '-----END RSA PRIVATE KEY-----';
|
7
|
+
|
8
|
+
const PUBLIC_OPENING_BOUNDARY = '-----BEGIN RSA PUBLIC KEY-----';
|
9
|
+
const PUBLIC_CLOSING_BOUNDARY = '-----END RSA PUBLIC KEY-----';
|
10
|
+
|
11
|
+
module.exports = {
|
12
|
+
privateExport: function (key, options) {
|
13
|
+
options = options || {};
|
14
|
+
|
15
|
+
var n = key.n.toBuffer();
|
16
|
+
var d = key.d.toBuffer();
|
17
|
+
var p = key.p.toBuffer();
|
18
|
+
var q = key.q.toBuffer();
|
19
|
+
var dmp1 = key.dmp1.toBuffer();
|
20
|
+
var dmq1 = key.dmq1.toBuffer();
|
21
|
+
var coeff = key.coeff.toBuffer();
|
22
|
+
|
23
|
+
var length = n.length + d.length + p.length + q.length + dmp1.length + dmq1.length + coeff.length + 512; // magic
|
24
|
+
var writer = new ber.Writer({size: length});
|
25
|
+
|
26
|
+
writer.startSequence();
|
27
|
+
writer.writeInt(0);
|
28
|
+
writer.writeBuffer(n, 2);
|
29
|
+
writer.writeInt(key.e);
|
30
|
+
writer.writeBuffer(d, 2);
|
31
|
+
writer.writeBuffer(p, 2);
|
32
|
+
writer.writeBuffer(q, 2);
|
33
|
+
writer.writeBuffer(dmp1, 2);
|
34
|
+
writer.writeBuffer(dmq1, 2);
|
35
|
+
writer.writeBuffer(coeff, 2);
|
36
|
+
writer.endSequence();
|
37
|
+
|
38
|
+
if (options.type === 'der') {
|
39
|
+
return writer.buffer;
|
40
|
+
} else {
|
41
|
+
return PRIVATE_OPENING_BOUNDARY + '\n' + utils.linebrk(writer.buffer.toString('base64'), 64) + '\n' + PRIVATE_CLOSING_BOUNDARY;
|
42
|
+
}
|
43
|
+
},
|
44
|
+
|
45
|
+
privateImport: function (key, data, options) {
|
46
|
+
options = options || {};
|
47
|
+
var buffer;
|
48
|
+
|
49
|
+
if (options.type !== 'der') {
|
50
|
+
if (Buffer.isBuffer(data)) {
|
51
|
+
data = data.toString('utf8');
|
52
|
+
}
|
53
|
+
|
54
|
+
if (_.isString(data)) {
|
55
|
+
var pem = utils.trimSurroundingText(data, PRIVATE_OPENING_BOUNDARY, PRIVATE_CLOSING_BOUNDARY)
|
56
|
+
.replace(/\s+|\n\r|\n|\r$/gm, '');
|
57
|
+
buffer = Buffer.from(pem, 'base64');
|
58
|
+
} else {
|
59
|
+
throw Error('Unsupported key format');
|
60
|
+
}
|
61
|
+
} else if (Buffer.isBuffer(data)) {
|
62
|
+
buffer = data;
|
63
|
+
} else {
|
64
|
+
throw Error('Unsupported key format');
|
65
|
+
}
|
66
|
+
|
67
|
+
var reader = new ber.Reader(buffer);
|
68
|
+
reader.readSequence();
|
69
|
+
reader.readString(2, true); // just zero
|
70
|
+
key.setPrivate(
|
71
|
+
reader.readString(2, true), // modulus
|
72
|
+
reader.readString(2, true), // publicExponent
|
73
|
+
reader.readString(2, true), // privateExponent
|
74
|
+
reader.readString(2, true), // prime1
|
75
|
+
reader.readString(2, true), // prime2
|
76
|
+
reader.readString(2, true), // exponent1 -- d mod (p1)
|
77
|
+
reader.readString(2, true), // exponent2 -- d mod (q-1)
|
78
|
+
reader.readString(2, true) // coefficient -- (inverse of q) mod p
|
79
|
+
);
|
80
|
+
},
|
81
|
+
|
82
|
+
publicExport: function (key, options) {
|
83
|
+
options = options || {};
|
84
|
+
|
85
|
+
var n = key.n.toBuffer();
|
86
|
+
var length = n.length + 512; // magic
|
87
|
+
|
88
|
+
var bodyWriter = new ber.Writer({size: length});
|
89
|
+
bodyWriter.startSequence();
|
90
|
+
bodyWriter.writeBuffer(n, 2);
|
91
|
+
bodyWriter.writeInt(key.e);
|
92
|
+
bodyWriter.endSequence();
|
93
|
+
|
94
|
+
if (options.type === 'der') {
|
95
|
+
return bodyWriter.buffer;
|
96
|
+
} else {
|
97
|
+
return PUBLIC_OPENING_BOUNDARY + '\n' + utils.linebrk(bodyWriter.buffer.toString('base64'), 64) + '\n' + PUBLIC_CLOSING_BOUNDARY;
|
98
|
+
}
|
99
|
+
},
|
100
|
+
|
101
|
+
publicImport: function (key, data, options) {
|
102
|
+
options = options || {};
|
103
|
+
var buffer;
|
104
|
+
|
105
|
+
if (options.type !== 'der') {
|
106
|
+
if (Buffer.isBuffer(data)) {
|
107
|
+
data = data.toString('utf8');
|
108
|
+
}
|
109
|
+
|
110
|
+
if (_.isString(data)) {
|
111
|
+
var pem = utils.trimSurroundingText(data, PUBLIC_OPENING_BOUNDARY, PUBLIC_CLOSING_BOUNDARY)
|
112
|
+
.replace(/\s+|\n\r|\n|\r$/gm, '');
|
113
|
+
buffer = Buffer.from(pem, 'base64');
|
114
|
+
}
|
115
|
+
} else if (Buffer.isBuffer(data)) {
|
116
|
+
buffer = data;
|
117
|
+
} else {
|
118
|
+
throw Error('Unsupported key format');
|
119
|
+
}
|
120
|
+
|
121
|
+
var body = new ber.Reader(buffer);
|
122
|
+
body.readSequence();
|
123
|
+
key.setPublic(
|
124
|
+
body.readString(0x02, true), // modulus
|
125
|
+
body.readString(0x02, true) // publicExponent
|
126
|
+
);
|
127
|
+
},
|
128
|
+
|
129
|
+
/**
|
130
|
+
* Trying autodetect and import key
|
131
|
+
* @param key
|
132
|
+
* @param data
|
133
|
+
*/
|
134
|
+
autoImport: function (key, data) {
|
135
|
+
// [\S\s]* matches zero or more of any character
|
136
|
+
if (/^[\S\s]*-----BEGIN RSA PRIVATE KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END RSA PRIVATE KEY-----[\S\s]*$/g.test(data)) {
|
137
|
+
module.exports.privateImport(key, data);
|
138
|
+
return true;
|
139
|
+
}
|
140
|
+
|
141
|
+
if (/^[\S\s]*-----BEGIN RSA PUBLIC KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END RSA PUBLIC KEY-----[\S\s]*$/g.test(data)) {
|
142
|
+
module.exports.publicImport(key, data);
|
143
|
+
return true;
|
144
|
+
}
|
145
|
+
|
146
|
+
return false;
|
147
|
+
}
|
148
|
+
};
|
@@ -0,0 +1,187 @@
|
|
1
|
+
var ber = require('../asn1/index').Ber;
|
2
|
+
var _ = require('../utils')._;
|
3
|
+
var PUBLIC_RSA_OID = '1.2.840.113549.1.1.1';
|
4
|
+
var utils = require('../utils');
|
5
|
+
|
6
|
+
const PRIVATE_OPENING_BOUNDARY = '-----BEGIN PRIVATE KEY-----';
|
7
|
+
const PRIVATE_CLOSING_BOUNDARY = '-----END PRIVATE KEY-----';
|
8
|
+
|
9
|
+
const PUBLIC_OPENING_BOUNDARY = '-----BEGIN PUBLIC KEY-----';
|
10
|
+
const PUBLIC_CLOSING_BOUNDARY = '-----END PUBLIC KEY-----';
|
11
|
+
|
12
|
+
module.exports = {
|
13
|
+
privateExport: function (key, options) {
|
14
|
+
options = options || {};
|
15
|
+
|
16
|
+
var n = key.n.toBuffer();
|
17
|
+
var d = key.d.toBuffer();
|
18
|
+
var p = key.p.toBuffer();
|
19
|
+
var q = key.q.toBuffer();
|
20
|
+
var dmp1 = key.dmp1.toBuffer();
|
21
|
+
var dmq1 = key.dmq1.toBuffer();
|
22
|
+
var coeff = key.coeff.toBuffer();
|
23
|
+
|
24
|
+
var length = n.length + d.length + p.length + q.length + dmp1.length + dmq1.length + coeff.length + 512; // magic
|
25
|
+
var bodyWriter = new ber.Writer({size: length});
|
26
|
+
|
27
|
+
bodyWriter.startSequence();
|
28
|
+
bodyWriter.writeInt(0);
|
29
|
+
bodyWriter.writeBuffer(n, 2);
|
30
|
+
bodyWriter.writeInt(key.e);
|
31
|
+
bodyWriter.writeBuffer(d, 2);
|
32
|
+
bodyWriter.writeBuffer(p, 2);
|
33
|
+
bodyWriter.writeBuffer(q, 2);
|
34
|
+
bodyWriter.writeBuffer(dmp1, 2);
|
35
|
+
bodyWriter.writeBuffer(dmq1, 2);
|
36
|
+
bodyWriter.writeBuffer(coeff, 2);
|
37
|
+
bodyWriter.endSequence();
|
38
|
+
|
39
|
+
var writer = new ber.Writer({size: length});
|
40
|
+
writer.startSequence();
|
41
|
+
writer.writeInt(0);
|
42
|
+
writer.startSequence();
|
43
|
+
writer.writeOID(PUBLIC_RSA_OID);
|
44
|
+
writer.writeNull();
|
45
|
+
writer.endSequence();
|
46
|
+
writer.writeBuffer(bodyWriter.buffer, 4);
|
47
|
+
writer.endSequence();
|
48
|
+
|
49
|
+
if (options.type === 'der') {
|
50
|
+
return writer.buffer;
|
51
|
+
} else {
|
52
|
+
return PRIVATE_OPENING_BOUNDARY + '\n' + utils.linebrk(writer.buffer.toString('base64'), 64) + '\n' + PRIVATE_CLOSING_BOUNDARY;
|
53
|
+
}
|
54
|
+
},
|
55
|
+
|
56
|
+
privateImport: function (key, data, options) {
|
57
|
+
options = options || {};
|
58
|
+
var buffer;
|
59
|
+
|
60
|
+
if (options.type !== 'der') {
|
61
|
+
if (Buffer.isBuffer(data)) {
|
62
|
+
data = data.toString('utf8');
|
63
|
+
}
|
64
|
+
|
65
|
+
if (_.isString(data)) {
|
66
|
+
var pem = utils.trimSurroundingText(data, PRIVATE_OPENING_BOUNDARY, PRIVATE_CLOSING_BOUNDARY)
|
67
|
+
.replace('-----END PRIVATE KEY-----', '')
|
68
|
+
.replace(/\s+|\n\r|\n|\r$/gm, '');
|
69
|
+
buffer = Buffer.from(pem, 'base64');
|
70
|
+
} else {
|
71
|
+
throw Error('Unsupported key format');
|
72
|
+
}
|
73
|
+
} else if (Buffer.isBuffer(data)) {
|
74
|
+
buffer = data;
|
75
|
+
} else {
|
76
|
+
throw Error('Unsupported key format');
|
77
|
+
}
|
78
|
+
|
79
|
+
var reader = new ber.Reader(buffer);
|
80
|
+
reader.readSequence();
|
81
|
+
reader.readInt(0);
|
82
|
+
var header = new ber.Reader(reader.readString(0x30, true));
|
83
|
+
|
84
|
+
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
|
85
|
+
throw Error('Invalid Public key format');
|
86
|
+
}
|
87
|
+
|
88
|
+
var body = new ber.Reader(reader.readString(0x04, true));
|
89
|
+
body.readSequence();
|
90
|
+
body.readString(2, true); // just zero
|
91
|
+
key.setPrivate(
|
92
|
+
body.readString(2, true), // modulus
|
93
|
+
body.readString(2, true), // publicExponent
|
94
|
+
body.readString(2, true), // privateExponent
|
95
|
+
body.readString(2, true), // prime1
|
96
|
+
body.readString(2, true), // prime2
|
97
|
+
body.readString(2, true), // exponent1 -- d mod (p1)
|
98
|
+
body.readString(2, true), // exponent2 -- d mod (q-1)
|
99
|
+
body.readString(2, true) // coefficient -- (inverse of q) mod p
|
100
|
+
);
|
101
|
+
},
|
102
|
+
|
103
|
+
publicExport: function (key, options) {
|
104
|
+
options = options || {};
|
105
|
+
|
106
|
+
var n = key.n.toBuffer();
|
107
|
+
var length = n.length + 512; // magic
|
108
|
+
|
109
|
+
var bodyWriter = new ber.Writer({size: length});
|
110
|
+
bodyWriter.writeByte(0);
|
111
|
+
bodyWriter.startSequence();
|
112
|
+
bodyWriter.writeBuffer(n, 2);
|
113
|
+
bodyWriter.writeInt(key.e);
|
114
|
+
bodyWriter.endSequence();
|
115
|
+
|
116
|
+
var writer = new ber.Writer({size: length});
|
117
|
+
writer.startSequence();
|
118
|
+
writer.startSequence();
|
119
|
+
writer.writeOID(PUBLIC_RSA_OID);
|
120
|
+
writer.writeNull();
|
121
|
+
writer.endSequence();
|
122
|
+
writer.writeBuffer(bodyWriter.buffer, 3);
|
123
|
+
writer.endSequence();
|
124
|
+
|
125
|
+
if (options.type === 'der') {
|
126
|
+
return writer.buffer;
|
127
|
+
} else {
|
128
|
+
return PUBLIC_OPENING_BOUNDARY + '\n' + utils.linebrk(writer.buffer.toString('base64'), 64) + '\n' + PUBLIC_CLOSING_BOUNDARY;
|
129
|
+
}
|
130
|
+
},
|
131
|
+
|
132
|
+
publicImport: function (key, data, options) {
|
133
|
+
options = options || {};
|
134
|
+
var buffer;
|
135
|
+
|
136
|
+
if (options.type !== 'der') {
|
137
|
+
if (Buffer.isBuffer(data)) {
|
138
|
+
data = data.toString('utf8');
|
139
|
+
}
|
140
|
+
|
141
|
+
if (_.isString(data)) {
|
142
|
+
var pem = utils.trimSurroundingText(data, PUBLIC_OPENING_BOUNDARY, PUBLIC_CLOSING_BOUNDARY)
|
143
|
+
.replace(/\s+|\n\r|\n|\r$/gm, '');
|
144
|
+
buffer = Buffer.from(pem, 'base64');
|
145
|
+
}
|
146
|
+
} else if (Buffer.isBuffer(data)) {
|
147
|
+
buffer = data;
|
148
|
+
} else {
|
149
|
+
throw Error('Unsupported key format');
|
150
|
+
}
|
151
|
+
|
152
|
+
var reader = new ber.Reader(buffer);
|
153
|
+
reader.readSequence();
|
154
|
+
var header = new ber.Reader(reader.readString(0x30, true));
|
155
|
+
|
156
|
+
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
|
157
|
+
throw Error('Invalid Public key format');
|
158
|
+
}
|
159
|
+
|
160
|
+
var body = new ber.Reader(reader.readString(0x03, true));
|
161
|
+
body.readByte();
|
162
|
+
body.readSequence();
|
163
|
+
key.setPublic(
|
164
|
+
body.readString(0x02, true), // modulus
|
165
|
+
body.readString(0x02, true) // publicExponent
|
166
|
+
);
|
167
|
+
},
|
168
|
+
|
169
|
+
/**
|
170
|
+
* Trying autodetect and import key
|
171
|
+
* @param key
|
172
|
+
* @param data
|
173
|
+
*/
|
174
|
+
autoImport: function (key, data) {
|
175
|
+
if (/^[\S\s]*-----BEGIN PRIVATE KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PRIVATE KEY-----[\S\s]*$/g.test(data)) {
|
176
|
+
module.exports.privateImport(key, data);
|
177
|
+
return true;
|
178
|
+
}
|
179
|
+
|
180
|
+
if (/^[\S\s]*-----BEGIN PUBLIC KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PUBLIC KEY-----[\S\s]*$/g.test(data)) {
|
181
|
+
module.exports.publicImport(key, data);
|
182
|
+
return true;
|
183
|
+
}
|
184
|
+
|
185
|
+
return false;
|
186
|
+
}
|
187
|
+
};
|