hdfc-smart-statement 0.0.0-stage → 0.1.0
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/LICENSE +9 -0
- package/README.md +29 -2
- package/bin/cli.js +24 -0
- package/package.json +12 -4
- package/src/index.js +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ashik Nesin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# hdfc-smart-statement
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Extract the transactions table from an HDFC Bank **Smart Statement** email link as CSV — no browser, no dependencies.
|
|
4
|
+
|
|
5
|
+
## CLI
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
HDFC_PASSWORD=xxxx npx hdfc-smart-statement "<smart statement url>" transactions.csv
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Omit the output file to print CSV to stdout. The password is read from the `HDFC_PASSWORD` environment variable so it stays out of shell history.
|
|
12
|
+
|
|
13
|
+
## API
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { statementToCsv, fetchStatementHtml, parseTransactions } from 'hdfc-smart-statement';
|
|
17
|
+
|
|
18
|
+
const csv = await statementToCsv(url, password);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`parseTransactions(html)` returns `{ header, rows }` if you'd rather have arrays than CSV.
|
|
22
|
+
|
|
23
|
+
## How it works
|
|
24
|
+
|
|
25
|
+
The Smart Statement page encrypts your password in the browser with a script served by HDFC (run here in a `vm`) and returns the statement as an AES-128-ECB blob whose key is embedded in the response. This tool replays that flow and parses the "Account Statement" table. Values are kept exactly as HDFC shows them (e.g. `"2,00,000.00"`, `-` for empty).
|
|
26
|
+
|
|
27
|
+
## Caveats
|
|
28
|
+
|
|
29
|
+
- Relies on undocumented HDFC endpoints; it may break without notice.
|
|
30
|
+
- Unofficial and unaffiliated with HDFC Bank. Your credentials only go to the HDFC host in the link you provide.
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { statementToCsv } from '../src/index.js';
|
|
4
|
+
|
|
5
|
+
const [url, outFile] = process.argv.slice(2);
|
|
6
|
+
const password = process.env.HDFC_PASSWORD;
|
|
7
|
+
if (!url || !password) {
|
|
8
|
+
console.error('Usage: HDFC_PASSWORD=xxxx hdfc-smart-statement "<smart statement url>" [out.csv]');
|
|
9
|
+
console.error('Writes CSV to stdout when no output file is given.');
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const csv = await statementToCsv(url, password);
|
|
15
|
+
if (outFile) {
|
|
16
|
+
fs.writeFileSync(outFile, csv);
|
|
17
|
+
console.error(`Wrote ${csv.trimEnd().split('\n').length - 1} transactions to ${outFile}`);
|
|
18
|
+
} else {
|
|
19
|
+
process.stdout.write(csv);
|
|
20
|
+
}
|
|
21
|
+
} catch (e) {
|
|
22
|
+
console.error(`Error: ${e.message}`);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hdfc-smart-statement",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Extract transactions from an HDFC Bank Smart Statement link as CSV, without a browser",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": "./src/index.js",
|
|
8
|
+
"bin": { "hdfc-smart-statement": "./bin/cli.js" },
|
|
9
|
+
"files": ["src", "bin"],
|
|
10
|
+
"engines": { "node": ">=18" },
|
|
11
|
+
"keywords": ["hdfc", "bank", "smart-statement", "csv", "india", "statement"],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Ashik Nesin"
|
|
14
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import vm from 'node:vm';
|
|
3
|
+
|
|
4
|
+
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/120 Safari/537.36';
|
|
5
|
+
|
|
6
|
+
const decodeEntities = (s) =>
|
|
7
|
+
s
|
|
8
|
+
.replace(/<[^>]*>/g, '')
|
|
9
|
+
.replace(/ /g, ' ')
|
|
10
|
+
.replace(/&/g, '&')
|
|
11
|
+
.replace(/</g, '<')
|
|
12
|
+
.replace(/>/g, '>')
|
|
13
|
+
.replace(/"/g, '"')
|
|
14
|
+
.replace(/&#(\d+);/g, (_, n) => String.fromCharCode(n))
|
|
15
|
+
.replace(/\s+/g, ' ')
|
|
16
|
+
.trim();
|
|
17
|
+
|
|
18
|
+
const csvCell = (v) => (/[",\n]/.test(v) ? `"${v.replace(/"/g, '""')}"` : v);
|
|
19
|
+
|
|
20
|
+
/** Logs in to a Smart Statement link and returns the decrypted statement HTML. */
|
|
21
|
+
export async function fetchStatementHtml(url, password) {
|
|
22
|
+
const base = new URL('.', url).href;
|
|
23
|
+
const cookies = new Map();
|
|
24
|
+
|
|
25
|
+
async function http(target, init = {}) {
|
|
26
|
+
const cookie = [...cookies].map(([k, v]) => `${k}=${v}`).join('; ');
|
|
27
|
+
const res = await fetch(target, {
|
|
28
|
+
...init,
|
|
29
|
+
headers: { 'User-Agent': UA, ...(cookie && { Cookie: cookie }), ...init.headers },
|
|
30
|
+
});
|
|
31
|
+
for (const c of res.headers.getSetCookie()) {
|
|
32
|
+
const [kv] = c.split(';');
|
|
33
|
+
const i = kv.indexOf('=');
|
|
34
|
+
cookies.set(kv.slice(0, i), kv.slice(i + 1));
|
|
35
|
+
}
|
|
36
|
+
if (!res.ok) throw new Error(`${res.status} ${res.statusText} for ${target}`);
|
|
37
|
+
return res.text();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const loginHtml = await http(url);
|
|
41
|
+
const field = (id) => loginHtml.match(new RegExp(`id=["']${id}["'][^>]*value=['"]([^'"]*)['"]`))?.[1];
|
|
42
|
+
const jobkey = field('ke');
|
|
43
|
+
const seqence = field('seqence');
|
|
44
|
+
if (!jobkey) throw new Error('Could not find jobkey on login page (link expired?)');
|
|
45
|
+
|
|
46
|
+
const token = (await http(`${base}CRSGetToken?jobkey=${jobkey}`)).trim();
|
|
47
|
+
const ctx = vm.createContext({ Math });
|
|
48
|
+
vm.runInContext(await http(`${base}js/encrypt.js`), ctx);
|
|
49
|
+
|
|
50
|
+
const stmtHtml = await http(`${base}webresources/app/htmlformat`, {
|
|
51
|
+
method: 'POST',
|
|
52
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded', Referer: url },
|
|
53
|
+
body: new URLSearchParams({ ke: jobkey, seqence, pwd: ctx.encrypt(token + password) }),
|
|
54
|
+
});
|
|
55
|
+
const m = stmtHtml.match(/Data\("([^"]+)","([^"]+)"\)/);
|
|
56
|
+
if (!m) throw new Error('Statement not returned - wrong password or expired link?');
|
|
57
|
+
|
|
58
|
+
const decipher = crypto.createDecipheriv('aes-128-ecb', Buffer.from(m[1], 'base64'), null);
|
|
59
|
+
return Buffer.concat([decipher.update(Buffer.from(m[2], 'base64')), decipher.final()]).toString('utf8');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Parses the Account Statement transactions table into `{ header, rows }` (7 visible columns). */
|
|
63
|
+
export function parseTransactions(html) {
|
|
64
|
+
const table = html.match(/<table[^>]*id="Table76"[\s\S]*?<\/table>/)?.[0];
|
|
65
|
+
if (!table) throw new Error('Transactions table not found in decrypted statement');
|
|
66
|
+
const [header, ...rows] = [...table.matchAll(/<tr[^>]*>([\s\S]*?)<\/tr>/g)].map((r) =>
|
|
67
|
+
[...r[1].matchAll(/<t[hd][^>]*>([\s\S]*?)<\/t[hd]>/g)].map((c) => decodeEntities(c[1])).slice(0, 7),
|
|
68
|
+
);
|
|
69
|
+
return { header: header.map((h, i) => (i >= 4 ? `${h} (INR)` : h)), rows };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function toCsv({ header, rows }) {
|
|
73
|
+
return [header, ...rows].map((r) => r.map(csvCell).join(',')).join('\n') + '\n';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Convenience: link + password in, CSV string out. */
|
|
77
|
+
export async function statementToCsv(url, password) {
|
|
78
|
+
return toCsv(parseTransactions(await fetchStatementHtml(url, password)));
|
|
79
|
+
}
|