@sachfolio/cas-reader 0.0.0-stage → 0.2.0-beta.1
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/CHANGELOG.md +19 -0
- package/LICENSE +71 -0
- package/NOTICE +41 -0
- package/README.md +198 -2
- package/dist/analytics/contract.d.ts +110 -0
- package/dist/analytics/contract.js +79 -0
- package/dist/analytics/eligibility.d.ts +66 -0
- package/dist/analytics/eligibility.js +93 -0
- package/dist/analytics/gains-schema.d.ts +898 -0
- package/dist/analytics/gains-schema.js +170 -0
- package/dist/analytics/gains.d.ts +319 -0
- package/dist/analytics/gains.js +649 -0
- package/dist/analytics/modules.d.ts +33 -0
- package/dist/analytics/modules.js +74 -0
- package/dist/analytics/returns-schema.d.ts +931 -0
- package/dist/analytics/returns-schema.js +152 -0
- package/dist/analytics/returns.d.ts +126 -0
- package/dist/analytics/returns.js +453 -0
- package/dist/analytics/schedule112a.d.ts +77 -0
- package/dist/analytics/schedule112a.js +134 -0
- package/dist/analytics/xirr.d.ts +75 -0
- package/dist/analytics/xirr.js +314 -0
- package/dist/bin/cas.d.ts +2 -0
- package/dist/bin/cas.js +137 -0
- package/dist/checks.d.ts +49 -0
- package/dist/checks.js +227 -0
- package/dist/compat/casparser.d.ts +121 -0
- package/dist/compat/casparser.js +193 -0
- package/dist/detect.d.ts +10 -0
- package/dist/detect.js +69 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +95 -0
- package/dist/export/csv.d.ts +21 -0
- package/dist/export/csv.js +49 -0
- package/dist/extract/browser.d.ts +17 -0
- package/dist/extract/browser.js +34 -0
- package/dist/extract/budget.d.ts +46 -0
- package/dist/extract/budget.js +88 -0
- package/dist/extract/node.d.ts +11 -0
- package/dist/extract/node.js +31 -0
- package/dist/extract/pdfjs.d.ts +71 -0
- package/dist/extract/pdfjs.js +244 -0
- package/dist/extract/types.d.ts +106 -0
- package/dist/extract/types.js +11 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +26 -0
- package/dist/layout/lines.d.ts +51 -0
- package/dist/layout/lines.js +115 -0
- package/dist/licence/app-licence.d.ts +53 -0
- package/dist/licence/app-licence.js +139 -0
- package/dist/licence/ed25519.d.ts +2 -0
- package/dist/licence/ed25519.js +103 -0
- package/dist/model.d.ts +148 -0
- package/dist/model.js +1 -0
- package/dist/num.d.ts +19 -0
- package/dist/num.js +27 -0
- package/dist/parse.d.ts +4 -0
- package/dist/parse.js +13 -0
- package/dist/parsers/cams/classify.d.ts +25 -0
- package/dist/parsers/cams/classify.js +103 -0
- package/dist/parsers/cams/detailed.d.ts +15 -0
- package/dist/parsers/cams/detailed.js +343 -0
- package/dist/parsers/cams/header.d.ts +47 -0
- package/dist/parsers/cams/header.js +170 -0
- package/dist/parsers/cams/investor.d.ts +28 -0
- package/dist/parsers/cams/investor.js +139 -0
- package/dist/parsers/cams/summary.d.ts +29 -0
- package/dist/parsers/cams/summary.js +413 -0
- package/dist/parsers/cams/table.d.ts +55 -0
- package/dist/parsers/cams/table.js +144 -0
- package/dist/parsers/cams/total.d.ts +17 -0
- package/dist/parsers/cams/total.js +105 -0
- package/dist/parsers/depository/mf.d.ts +22 -0
- package/dist/parsers/depository/mf.js +438 -0
- package/dist/privacy.d.ts +243 -0
- package/dist/privacy.js +256 -0
- package/dist/schema.d.ts +779 -0
- package/dist/schema.js +214 -0
- package/examples/nextjs/app/page.jsx +34 -0
- package/examples/node/server.mjs +45 -0
- package/examples/python/read_cas.py +67 -0
- package/examples/vite/index.html +14 -0
- package/examples/vite/main.js +22 -0
- package/package.json +54 -4
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licence codes for the developer package, one per app (PLAN.md stage E;
|
|
3
|
+
* DECISIONS 11-Nov: a flat yearly licence per app with unlimited reads).
|
|
4
|
+
*
|
|
5
|
+
* SF1.<payload>.<signature>
|
|
6
|
+
*
|
|
7
|
+
* The same format and signing key as the distributor codes
|
|
8
|
+
* (docs/ops/LICENCE-CODES.md): `payload` is base64url of compact JSON,
|
|
9
|
+
* `signature` is Ed25519 over the ASCII text "SF1.<payload>". The product
|
|
10
|
+
* field `p` is "cas-reader", so a distributor code is never an app licence
|
|
11
|
+
* and the reverse.
|
|
12
|
+
*
|
|
13
|
+
* Checking is offline: our public key is in this file, and nothing is sent
|
|
14
|
+
* anywhere. Reading statements does NOT depend on the result. The package is
|
|
15
|
+
* licensed under BUSL-1.1 and enforcement is the licence plus honour; what a
|
|
16
|
+
* missing or lapsed code should do in code is the founder's decision
|
|
17
|
+
* (PLAN.md stage E), so today `checkAppLicence` only reports.
|
|
18
|
+
*/
|
|
19
|
+
import { ed25519Verify } from "./ed25519.js";
|
|
20
|
+
export const APP_CODE_PREFIX = "SF1";
|
|
21
|
+
/** Public keys that sign Sachfolio licence codes: base64url of the raw 32-byte Ed25519 key. */
|
|
22
|
+
export const SACHFOLIO_PUBLIC_KEYS = {
|
|
23
|
+
k1: "LgFzGCz-VZpJZw9YS0KM_V6mYOMtha1G-A6wOhfcPGU",
|
|
24
|
+
};
|
|
25
|
+
const ISO = /^\d{4}-\d{2}-\d{2}$/;
|
|
26
|
+
const ID = /^L[0-9a-f]{10}$/;
|
|
27
|
+
const B64URL = /^[A-Za-z0-9_-]+$/;
|
|
28
|
+
// A domain, or a reverse-DNS app id: lower-case labels joined by dots.
|
|
29
|
+
const APP = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-_]{0,61}[a-z0-9])?)+$/;
|
|
30
|
+
function toBase64Url(bytes) {
|
|
31
|
+
let s = "";
|
|
32
|
+
for (const b of bytes)
|
|
33
|
+
s += String.fromCharCode(b);
|
|
34
|
+
return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
35
|
+
}
|
|
36
|
+
function fromBase64Url(text) {
|
|
37
|
+
if (!B64URL.test(text))
|
|
38
|
+
throw new Error("not base64url");
|
|
39
|
+
const s = atob(text.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - (text.length % 4)) % 4));
|
|
40
|
+
const out = new Uint8Array(s.length);
|
|
41
|
+
for (let i = 0; i < s.length; i++)
|
|
42
|
+
out[i] = s.charCodeAt(i);
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
const utf8 = new TextEncoder();
|
|
46
|
+
function encodePayload(p) {
|
|
47
|
+
const ordered = { v: p.v, k: p.k, id: p.id, p: p.p, plan: p.plan, org: p.org, app: p.app, from: p.from, to: p.to };
|
|
48
|
+
return toBase64Url(utf8.encode(JSON.stringify(ordered)));
|
|
49
|
+
}
|
|
50
|
+
/** Today's date in India, as an ISO date. */
|
|
51
|
+
export function todayInIndia(now = new Date()) {
|
|
52
|
+
return new Date(now.getTime() + 330 * 60_000).toISOString().slice(0, 10);
|
|
53
|
+
}
|
|
54
|
+
export function checkAppPayload(x) {
|
|
55
|
+
if (!x || typeof x !== "object")
|
|
56
|
+
return null;
|
|
57
|
+
const p = x;
|
|
58
|
+
if (Object.keys(p).sort().join(",") !== "app,from,id,k,org,p,plan,to,v")
|
|
59
|
+
return null;
|
|
60
|
+
if (p.v !== 1 || typeof p.k !== "string" || !/^[a-z0-9]{1,8}$/.test(p.k))
|
|
61
|
+
return null;
|
|
62
|
+
if (typeof p.id !== "string" || !ID.test(p.id) || p.p !== "cas-reader")
|
|
63
|
+
return null;
|
|
64
|
+
if (p.plan !== "app-year" && p.plan !== "evaluation")
|
|
65
|
+
return null;
|
|
66
|
+
if (typeof p.org !== "string" || p.org.trim() !== p.org || p.org.length < 2 || p.org.length > 120 || /[\u0000-\u001f]/.test(p.org))
|
|
67
|
+
return null;
|
|
68
|
+
if (typeof p.app !== "string" || p.app.length > 200 || !APP.test(p.app))
|
|
69
|
+
return null;
|
|
70
|
+
if (typeof p.from !== "string" || !ISO.test(p.from) || typeof p.to !== "string" || !ISO.test(p.to))
|
|
71
|
+
return null;
|
|
72
|
+
if (Number.isNaN(Date.parse(p.from)) || Number.isNaN(Date.parse(p.to)) || p.to < p.from)
|
|
73
|
+
return null;
|
|
74
|
+
return p;
|
|
75
|
+
}
|
|
76
|
+
async function signatureOk(publicKey, signature, message) {
|
|
77
|
+
if (!globalThis.crypto?.subtle)
|
|
78
|
+
return null;
|
|
79
|
+
try {
|
|
80
|
+
const key = await crypto.subtle.importKey("raw", publicKey, { name: "Ed25519" }, false, ["verify"]);
|
|
81
|
+
return await crypto.subtle.verify({ name: "Ed25519" }, key, signature, message);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
try {
|
|
85
|
+
return await ed25519Verify(publicKey, signature, message);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Checks an app licence code offline. Never throws for a bad code, and never
|
|
94
|
+
* affects reading: show the result in your admin screen or logs if you like.
|
|
95
|
+
*/
|
|
96
|
+
export async function checkAppLicence(code, options = {}) {
|
|
97
|
+
const text = code.replace(/\s+/g, "");
|
|
98
|
+
const parts = text.split(".");
|
|
99
|
+
if (parts.length !== 3 || parts[0] !== APP_CODE_PREFIX || !parts[1] || !parts[2])
|
|
100
|
+
return { status: "invalid" };
|
|
101
|
+
let payload;
|
|
102
|
+
let signature;
|
|
103
|
+
try {
|
|
104
|
+
payload = checkAppPayload(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(fromBase64Url(parts[1]))));
|
|
105
|
+
signature = fromBase64Url(parts[2]);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return { status: "invalid" };
|
|
109
|
+
}
|
|
110
|
+
// A distributor code, or a field a later version added.
|
|
111
|
+
if (!payload)
|
|
112
|
+
return { status: "unsupported" };
|
|
113
|
+
if (signature.length !== 64 || toBase64Url(signature) !== parts[2] || encodePayload(payload) !== parts[1])
|
|
114
|
+
return { status: "invalid" };
|
|
115
|
+
const key = (options.keys ?? SACHFOLIO_PUBLIC_KEYS)[payload.k];
|
|
116
|
+
if (!key)
|
|
117
|
+
return { status: "unknown-key" };
|
|
118
|
+
const ok = await signatureOk(fromBase64Url(key), signature, utf8.encode(`${APP_CODE_PREFIX}.${parts[1]}`));
|
|
119
|
+
if (ok === null)
|
|
120
|
+
return { status: "unchecked" };
|
|
121
|
+
if (!ok)
|
|
122
|
+
return { status: "invalid" };
|
|
123
|
+
const today = options.today ?? todayInIndia();
|
|
124
|
+
if (today > payload.to)
|
|
125
|
+
return { status: "expired", licence: payload };
|
|
126
|
+
if (today < payload.from)
|
|
127
|
+
return { status: "not-started", licence: payload };
|
|
128
|
+
return { status: "valid", licence: payload };
|
|
129
|
+
}
|
|
130
|
+
/** Makes a code. Only the minting tool calls this, with the private key. */
|
|
131
|
+
export async function mintAppLicence(input, privateKey) {
|
|
132
|
+
const id = input.id ?? `L${[...crypto.getRandomValues(new Uint8Array(5))].map((x) => x.toString(16).padStart(2, "0")).join("")}`;
|
|
133
|
+
const payload = { v: 1, k: input.k, id, p: "cas-reader", plan: input.plan, org: input.org.trim(), app: input.app.trim().toLowerCase(), from: input.from, to: input.to };
|
|
134
|
+
if (!checkAppPayload(payload))
|
|
135
|
+
throw new Error("licence: these details don't make a valid app code (check the organisation, the app as a domain or app id, the dates and the key id)");
|
|
136
|
+
const body = encodePayload(payload);
|
|
137
|
+
const sig = new Uint8Array(await crypto.subtle.sign({ name: "Ed25519" }, privateKey, utf8.encode(`${APP_CODE_PREFIX}.${body}`)));
|
|
138
|
+
return { code: `${APP_CODE_PREFIX}.${body}.${toBase64Url(sig)}`, payload };
|
|
139
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 signature check (RFC 8032 §5.1.7), verify only, for browsers whose
|
|
3
|
+
* Web Crypto has no Ed25519 (older Safari; WebKit on Windows). The licence
|
|
4
|
+
* checks (apps/web/src/licence/code.ts, licence/app-licence.ts) try the
|
|
5
|
+
* browser's own Ed25519 first and come here only when that is missing.
|
|
6
|
+
* SHA-512 is the browser's. Everything checked is public (the code and our
|
|
7
|
+
* public key), so this needn't run in constant time.
|
|
8
|
+
*
|
|
9
|
+
* Tested against RFC 8032's vectors and against Node's own Ed25519 on random
|
|
10
|
+
* keys and messages (apps/web/test/licence.test.ts).
|
|
11
|
+
*/
|
|
12
|
+
const P = 2n ** 255n - 19n;
|
|
13
|
+
const L = 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
14
|
+
const mod = (a, m = P) => {
|
|
15
|
+
const r = a % m;
|
|
16
|
+
return r >= 0n ? r : r + m;
|
|
17
|
+
};
|
|
18
|
+
function pow(base, exp, m = P) {
|
|
19
|
+
let r = 1n;
|
|
20
|
+
let b = mod(base, m);
|
|
21
|
+
let e = exp;
|
|
22
|
+
while (e > 0n) {
|
|
23
|
+
if (e & 1n)
|
|
24
|
+
r = (r * b) % m;
|
|
25
|
+
b = (b * b) % m;
|
|
26
|
+
e >>= 1n;
|
|
27
|
+
}
|
|
28
|
+
return r;
|
|
29
|
+
}
|
|
30
|
+
const inv = (x) => pow(x, P - 2n);
|
|
31
|
+
const D = mod(-121665n * inv(121666n));
|
|
32
|
+
const SQRT_M1 = pow(2n, (P - 1n) / 4n);
|
|
33
|
+
function add([x1, y1, z1, t1], [x2, y2, z2, t2]) {
|
|
34
|
+
const a = mod((y1 - x1) * (y2 - x2));
|
|
35
|
+
const b = mod((y1 + x1) * (y2 + x2));
|
|
36
|
+
const c = mod(2n * t1 * t2 * D);
|
|
37
|
+
const d = mod(2n * z1 * z2);
|
|
38
|
+
const e = b - a;
|
|
39
|
+
const f = d - c;
|
|
40
|
+
const g = d + c;
|
|
41
|
+
const h = b + a;
|
|
42
|
+
return [mod(e * f), mod(g * h), mod(f * g), mod(e * h)];
|
|
43
|
+
}
|
|
44
|
+
const ZERO = [0n, 1n, 1n, 0n];
|
|
45
|
+
function mul(s, p) {
|
|
46
|
+
let q = ZERO;
|
|
47
|
+
let a = p;
|
|
48
|
+
for (let k = s; k > 0n; k >>= 1n) {
|
|
49
|
+
if (k & 1n)
|
|
50
|
+
q = add(q, a);
|
|
51
|
+
a = add(a, a);
|
|
52
|
+
}
|
|
53
|
+
return q;
|
|
54
|
+
}
|
|
55
|
+
const same = ([x1, y1, z1], [x2, y2, z2]) => mod(x1 * z2 - x2 * z1) === 0n && mod(y1 * z2 - y2 * z1) === 0n;
|
|
56
|
+
function recoverX(y, sign) {
|
|
57
|
+
if (y >= P)
|
|
58
|
+
return null;
|
|
59
|
+
const x2 = mod((y * y - 1n) * inv(D * y * y + 1n));
|
|
60
|
+
if (x2 === 0n)
|
|
61
|
+
return sign ? null : 0n;
|
|
62
|
+
let x = pow(x2, (P + 3n) / 8n);
|
|
63
|
+
if (mod(x * x - x2) !== 0n)
|
|
64
|
+
x = mod(x * SQRT_M1);
|
|
65
|
+
if (mod(x * x - x2) !== 0n)
|
|
66
|
+
return null;
|
|
67
|
+
if ((x & 1n) !== sign)
|
|
68
|
+
x = P - x;
|
|
69
|
+
return x;
|
|
70
|
+
}
|
|
71
|
+
const littleEndian = (b) => {
|
|
72
|
+
let n = 0n;
|
|
73
|
+
for (let i = b.length - 1; i >= 0; i--)
|
|
74
|
+
n = (n << 8n) | BigInt(b[i]);
|
|
75
|
+
return n;
|
|
76
|
+
};
|
|
77
|
+
function decodePoint(b) {
|
|
78
|
+
if (b.length !== 32)
|
|
79
|
+
return null;
|
|
80
|
+
const n = littleEndian(b);
|
|
81
|
+
const y = n & ((1n << 255n) - 1n);
|
|
82
|
+
const x = recoverX(y, n >> 255n);
|
|
83
|
+
return x === null ? null : [x, y, 1n, mod(x * y)];
|
|
84
|
+
}
|
|
85
|
+
const GY = mod(4n * inv(5n));
|
|
86
|
+
const G = [recoverX(GY, 0n), GY, 1n, mod(recoverX(GY, 0n) * GY)];
|
|
87
|
+
/** True when `signature` is `publicKey`'s Ed25519 signature of `message`. */
|
|
88
|
+
export async function ed25519Verify(publicKey, signature, message) {
|
|
89
|
+
if (publicKey.length !== 32 || signature.length !== 64)
|
|
90
|
+
return false;
|
|
91
|
+
const a = decodePoint(publicKey);
|
|
92
|
+
const rBytes = signature.subarray(0, 32);
|
|
93
|
+
const r = decodePoint(rBytes);
|
|
94
|
+
const s = littleEndian(signature.subarray(32));
|
|
95
|
+
if (!a || !r || s >= L)
|
|
96
|
+
return false;
|
|
97
|
+
const joined = new Uint8Array(64 + message.length);
|
|
98
|
+
joined.set(rBytes, 0);
|
|
99
|
+
joined.set(publicKey, 32);
|
|
100
|
+
joined.set(message, 64);
|
|
101
|
+
const h = mod(littleEndian(new Uint8Array(await crypto.subtle.digest("SHA-512", joined))), L);
|
|
102
|
+
return same(mul(s, G), add(r, mul(h, a)));
|
|
103
|
+
}
|
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CasDocument v1: parsed evidence only (PLAN.md §5.5). Analytics live in a
|
|
3
|
+
* separate, versioned object. Every value can point back to where it was read.
|
|
4
|
+
* Every field has a privacy class (P0–P3, PX) in privacy.ts; a new field needs one.
|
|
5
|
+
*/
|
|
6
|
+
import type { DocumentMeta } from "./extract/types.js";
|
|
7
|
+
import type { Issuer, Variant } from "./detect.js";
|
|
8
|
+
import type { Num } from "./num.js";
|
|
9
|
+
export interface Source {
|
|
10
|
+
page: number;
|
|
11
|
+
/** Baseline y of the line (top-down points). */
|
|
12
|
+
y: number;
|
|
13
|
+
}
|
|
14
|
+
export type TxnType = "PURCHASE" | "PURCHASE_SIP" | "REDEMPTION"
|
|
15
|
+
/** Systematic withdrawal plan instalment: a redemption on a schedule. */
|
|
16
|
+
| "SWP" | "SWITCH_IN" | "SWITCH_IN_MERGER" | "SWITCH_OUT" | "SWITCH_OUT_MERGER" | "DIVIDEND_PAYOUT" | "DIVIDEND_REINVEST" | "SEGREGATION" | "GIFT_IN" | "GIFT_OUT" | "REVERSAL"
|
|
17
|
+
/** Units moved between folios or holders without cash: folio transfer, transmission, consolidation. */
|
|
18
|
+
| "TRANSFER_IN" | "TRANSFER_OUT" | "BONUS"
|
|
19
|
+
/** Split, consolidation of units or other RTA adjustment. */
|
|
20
|
+
| "UNIT_ADJUSTMENT"
|
|
21
|
+
/** Units pledged / lien marked for a loan against mutual funds (still owned). */
|
|
22
|
+
| "PLEDGE" | "PLEDGE_RELEASE"
|
|
23
|
+
/** The lender invoked the pledge and the units were redeemed for it. */
|
|
24
|
+
| "PLEDGE_INVOKED" | "STAMP_DUTY_TAX" | "STT_TAX" | "TDS_TAX" | "MISC" | "UNKNOWN";
|
|
25
|
+
export interface Transaction {
|
|
26
|
+
/** Position in the statement, preserving order for same-date rows. */
|
|
27
|
+
order: number;
|
|
28
|
+
date: string;
|
|
29
|
+
description: string;
|
|
30
|
+
amount: Num | null;
|
|
31
|
+
units: Num | null;
|
|
32
|
+
price: Num | null;
|
|
33
|
+
balance: Num | null;
|
|
34
|
+
type: TxnType;
|
|
35
|
+
dividendRate: string | null;
|
|
36
|
+
giftFolio: string | null;
|
|
37
|
+
/** Set when a printed sign was corrected from the running balance; the raw values stay in `raw`. */
|
|
38
|
+
signCorrected: boolean;
|
|
39
|
+
source: Source;
|
|
40
|
+
}
|
|
41
|
+
export type CheckStatus = "pass" | "fail" | "unavailable" | "not_applicable";
|
|
42
|
+
export interface Check {
|
|
43
|
+
id: string;
|
|
44
|
+
status: CheckStatus;
|
|
45
|
+
expected?: string;
|
|
46
|
+
observed?: string;
|
|
47
|
+
tolerance?: string;
|
|
48
|
+
detail?: string;
|
|
49
|
+
source?: Source;
|
|
50
|
+
}
|
|
51
|
+
export type Plan = "DIRECT" | "REGULAR" | "UNKNOWN";
|
|
52
|
+
export type PlanOption = "GROWTH" | "IDCW_PAYOUT" | "IDCW_REINVEST" | "UNKNOWN";
|
|
53
|
+
export interface Scheme {
|
|
54
|
+
name: string;
|
|
55
|
+
rtaCode: string;
|
|
56
|
+
rta: string;
|
|
57
|
+
isin: string | null;
|
|
58
|
+
/** As labelled on the CAS: "(Advisor: ARN-…/INA…/DIRECT)". Not an advisory relationship. */
|
|
59
|
+
advisor: string | null;
|
|
60
|
+
plan: Plan;
|
|
61
|
+
option: PlanOption;
|
|
62
|
+
nominees: string[];
|
|
63
|
+
open: Num | null;
|
|
64
|
+
close: Num | null;
|
|
65
|
+
closeCalculated: string | null;
|
|
66
|
+
nav: {
|
|
67
|
+
date: string;
|
|
68
|
+
value: Num;
|
|
69
|
+
} | null;
|
|
70
|
+
valuation: {
|
|
71
|
+
date: string;
|
|
72
|
+
value: Num;
|
|
73
|
+
} | null;
|
|
74
|
+
cost: Num | null;
|
|
75
|
+
transactions: Transaction[];
|
|
76
|
+
checks: Check[];
|
|
77
|
+
source: Source;
|
|
78
|
+
/** Where the closing balance / NAV / value footer was read (the valuation line when printed). */
|
|
79
|
+
footerSource: Source | null;
|
|
80
|
+
}
|
|
81
|
+
export interface Folio {
|
|
82
|
+
folio: string;
|
|
83
|
+
amc: string;
|
|
84
|
+
pan: string | null;
|
|
85
|
+
kyc: string | null;
|
|
86
|
+
panKyc: string | null;
|
|
87
|
+
holderName: string | null;
|
|
88
|
+
schemes: Scheme[];
|
|
89
|
+
}
|
|
90
|
+
export interface CasDocument {
|
|
91
|
+
schemaVersion: "1";
|
|
92
|
+
source: {
|
|
93
|
+
issuer: Issuer;
|
|
94
|
+
variant: Variant;
|
|
95
|
+
engineVersion: string;
|
|
96
|
+
};
|
|
97
|
+
provenance: Omit<DocumentMeta, "byteLength">;
|
|
98
|
+
statementPeriod: {
|
|
99
|
+
from: string;
|
|
100
|
+
to: string;
|
|
101
|
+
} | null;
|
|
102
|
+
/** The person the statement is addressed to (page 1). Not necessarily every holder. */
|
|
103
|
+
addressee: {
|
|
104
|
+
name: string | null;
|
|
105
|
+
email: string | null;
|
|
106
|
+
mobile: string | null;
|
|
107
|
+
address: string | null;
|
|
108
|
+
};
|
|
109
|
+
folios: Folio[];
|
|
110
|
+
/** The statement's own portfolio total row, as printed; null when none was found. Never computed. */
|
|
111
|
+
printedTotal: PrintedTotal | null;
|
|
112
|
+
/** Statement-level checks (per-scheme checks are on each scheme), e.g. total.market_value. */
|
|
113
|
+
checks: Check[];
|
|
114
|
+
coverage: Coverage;
|
|
115
|
+
warnings: string[];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The portfolio total a statement prints: the "Total" row of the PORTFOLIO
|
|
119
|
+
* SUMMARY table (detailed CAS) or of the holdings table (summary CAS).
|
|
120
|
+
*/
|
|
121
|
+
export interface PrintedTotal {
|
|
122
|
+
/** The row's label as printed, e.g. "Total" or "Grand Total". */
|
|
123
|
+
label: string;
|
|
124
|
+
/** Total cost value as printed on the row; null when the row prints none we could place. */
|
|
125
|
+
cost: Num | null;
|
|
126
|
+
/** Total market value as printed on the row; null when the row prints none we could place. */
|
|
127
|
+
marketValue: Num | null;
|
|
128
|
+
/** The total row's line. */
|
|
129
|
+
source: Source;
|
|
130
|
+
}
|
|
131
|
+
export interface Coverage {
|
|
132
|
+
pages: number;
|
|
133
|
+
datedRowsSeen: number;
|
|
134
|
+
transactionsParsed: number;
|
|
135
|
+
datedRowsSkipped: Array<{
|
|
136
|
+
source: Source;
|
|
137
|
+
reason: string;
|
|
138
|
+
}>;
|
|
139
|
+
ambiguousCells: Array<{
|
|
140
|
+
source: Source;
|
|
141
|
+
text: string;
|
|
142
|
+
reason: string;
|
|
143
|
+
}>;
|
|
144
|
+
schemeRegionsUnbuilt: Array<{
|
|
145
|
+
source: Source;
|
|
146
|
+
reason: string;
|
|
147
|
+
}>;
|
|
148
|
+
}
|
package/dist/model.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/num.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Printed numbers, kept exact (PLAN.md §5.6): parsed straight from the string
|
|
3
|
+
* into big.js, never via JS `Number`. The printed text and its decimal scale
|
|
4
|
+
* are retained because tolerances derive from printed precision.
|
|
5
|
+
*/
|
|
6
|
+
import Big from "big.js";
|
|
7
|
+
export interface Num {
|
|
8
|
+
/** Exactly as printed, e.g. "(1,234.50)". */
|
|
9
|
+
raw: string;
|
|
10
|
+
/** Canonical decimal string with sign applied, e.g. "-1234.50". */
|
|
11
|
+
value: string;
|
|
12
|
+
/** Digits after the decimal point as printed. */
|
|
13
|
+
scale: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function parseNum(raw: string | undefined | null): Num | null;
|
|
16
|
+
export declare const big: (n: Num | string | number) => Big;
|
|
17
|
+
/** Half a unit in the last printed place: the rounding uncertainty of a printed value. */
|
|
18
|
+
export declare function halfUlp(n: Num): Big;
|
|
19
|
+
export declare function negate(n: Num): Num;
|
package/dist/num.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Printed numbers, kept exact (PLAN.md §5.6): parsed straight from the string
|
|
3
|
+
* into big.js, never via JS `Number`. The printed text and its decimal scale
|
|
4
|
+
* are retained because tolerances derive from printed precision.
|
|
5
|
+
*/
|
|
6
|
+
import Big from "big.js";
|
|
7
|
+
const NUM_RE = /^\(?\s*-?\s*[\d,]*\.?\d+\s*\)?$/;
|
|
8
|
+
export function parseNum(raw) {
|
|
9
|
+
if (raw == null)
|
|
10
|
+
return null;
|
|
11
|
+
const t = raw.trim();
|
|
12
|
+
if (!t || !/\d/.test(t) || !NUM_RE.test(t))
|
|
13
|
+
return null;
|
|
14
|
+
const negative = t.startsWith("(") || t.replace(/^\(/, "").trim().startsWith("-");
|
|
15
|
+
const digits = t.replace(/[()\s,-]/g, "");
|
|
16
|
+
const scale = digits.includes(".") ? digits.length - digits.indexOf(".") - 1 : 0;
|
|
17
|
+
const value = new Big(digits.startsWith(".") ? `0${digits}` : digits);
|
|
18
|
+
return { raw: t, value: (negative ? value.neg() : value).toFixed(scale), scale };
|
|
19
|
+
}
|
|
20
|
+
export const big = (n) => new Big(typeof n === "object" ? n.value : n);
|
|
21
|
+
/** Half a unit in the last printed place: the rounding uncertainty of a printed value. */
|
|
22
|
+
export function halfUlp(n) {
|
|
23
|
+
return new Big(5).times(new Big(10).pow(-(n.scale + 1)));
|
|
24
|
+
}
|
|
25
|
+
export function negate(n) {
|
|
26
|
+
return { ...n, value: big(n).neg().toFixed(n.scale) };
|
|
27
|
+
}
|
package/dist/parse.d.ts
ADDED
package/dist/parse.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { detect } from "./detect.js";
|
|
2
|
+
import { parseDetailed } from "./parsers/cams/detailed.js";
|
|
3
|
+
import { parseSummary } from "./parsers/cams/summary.js";
|
|
4
|
+
import { parseDepository } from "./parsers/depository/mf.js";
|
|
5
|
+
/** Extracted document → CasDocument. Throws CasError for anything unsupported. */
|
|
6
|
+
export function parseCas(doc) {
|
|
7
|
+
const detection = detect(doc);
|
|
8
|
+
if (detection.variant === "DETAILED")
|
|
9
|
+
return parseDetailed(doc, detection);
|
|
10
|
+
if (detection.variant === "SUMMARY")
|
|
11
|
+
return parseSummary(doc, detection);
|
|
12
|
+
return parseDepository(doc, detection);
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transaction-type classification. Ported from casparser `_classify.py`
|
|
3
|
+
* (MIT) as a starting table, with one deliberate change (PLAN.md §5.3): rows
|
|
4
|
+
* whose wording isn't recognised are NOT inferred from the units sign alone.
|
|
5
|
+
* Plain "purchase"/"redemption"-style wording is required; otherwise UNKNOWN,
|
|
6
|
+
* which blocks analytics that depend on the row.
|
|
7
|
+
*
|
|
8
|
+
* Also not in casparser (M2 review): units that move without cash are never
|
|
9
|
+
* an ordinary flow. Merger wording gives SWITCH_*_MERGER with or without
|
|
10
|
+
* "switch"; transfers between folios (incl. transmission and consolidation),
|
|
11
|
+
* bonus, unit adjustments and loan pledges each get their own type. Pledge
|
|
12
|
+
* wording ("Pledged <lender>", "Units Lien Marked <lender>") was measured on
|
|
13
|
+
* a real KFintech statement.
|
|
14
|
+
* Reversal wording is tested first in both directions, so a reversed
|
|
15
|
+
* switch-out that restores units is not a switch-in.
|
|
16
|
+
*/
|
|
17
|
+
import type { TxnType } from "../../model.js";
|
|
18
|
+
import { type Num } from "../../num.js";
|
|
19
|
+
export declare function classify(description: string, units: Num | null): {
|
|
20
|
+
type: TxnType;
|
|
21
|
+
dividendRate: string | null;
|
|
22
|
+
};
|
|
23
|
+
export declare function giftFolio(description: string): string | null;
|
|
24
|
+
/** casparser `get_parsed_scheme_name`: drop (formerly …)/(erstwhile …)/(Demat …) trailers. */
|
|
25
|
+
export declare function cleanSchemeName(raw: string): string;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { big } from "../../num.js";
|
|
2
|
+
const DIVIDEND_RE = /(?:div\.|dividend|idcw).*?@\s*rs\.\s*([\d.]+)(?:\s+per\s+unit)?/is;
|
|
3
|
+
const REINVEST_RE = /reinvest/i;
|
|
4
|
+
const STP_RE = /\bs\s*t\s*p\b|systematic\s+transfer/i;
|
|
5
|
+
const GIFT_FOLIO_RE = /Folio\s+No\s*[:.]\s*(\d+)/i;
|
|
6
|
+
const REVERSAL_RE = /reversal|reversed|reject(?:ed|ion)?|cancel+ed|bounced|dishonou?red|mismatch|insufficient\s+balance|payment\s+not\s+received/i;
|
|
7
|
+
const MERGER_RE = /merger|amalgamat/i;
|
|
8
|
+
// Units received or given up without a purchase or sale in this folio. STP
|
|
9
|
+
// wording ("Systematic Transfer In") is a switch and is tested before this.
|
|
10
|
+
const TRANSFER_RE = /transmission|consolidat|transfer(?:red)?\s*-?\s*(?:in|out)\b/i;
|
|
11
|
+
const BONUS_RE = /\bbonus\b/i;
|
|
12
|
+
const ADJUST_RE = /unit\s+adjust|\bsplit\b/i;
|
|
13
|
+
const PLEDGE_RE = /pledg|lien/i;
|
|
14
|
+
const PLEDGE_RELEASE_RE = /releas|revok|revoc|unpledg|remov|clos/i;
|
|
15
|
+
const PLEDGE_INVOKE_RE = /invo(?:ked|cation)/i;
|
|
16
|
+
const SWP_RE = /\bswp\b|systematic\s+withdrawal/i;
|
|
17
|
+
const SIP_RE = /\bsip\b|systematic|instal+ment|sys.+?invest/is;
|
|
18
|
+
// Wording that positively identifies an ordinary inflow or outflow. Bare
|
|
19
|
+
// "allotment", "additional" and "transfer in/out" are not enough: bonus,
|
|
20
|
+
// merger and folio-transfer rows use them too.
|
|
21
|
+
const PURCHASE_WORDS_RE = /purchase|subscription|investment|new\s+fund\s+offer|\bnfo\b|lateral\s+shift\s+in|switch(?:ed)?\s*in/i;
|
|
22
|
+
const REDEMPTION_WORDS_RE = /redemption|redeem|repurchase|\bswp\b|systematic\s+withdrawal|withdrawal|lateral\s+shift\s+out|switch(?:ed)?\s*out|payout|maturity|extinguish/i;
|
|
23
|
+
// "Lateral shift" is CAMS wording for a switch between schemes of one AMC.
|
|
24
|
+
const SWITCH_RE = /switch|lateral\s+shift/i;
|
|
25
|
+
export function classify(description, units) {
|
|
26
|
+
const d = description.toLowerCase();
|
|
27
|
+
const div = DIVIDEND_RE.exec(d);
|
|
28
|
+
if (div)
|
|
29
|
+
return { type: REINVEST_RE.test(d) ? "DIVIDEND_REINVEST" : "DIVIDEND_PAYOUT", dividendRate: div[1] };
|
|
30
|
+
if (units === null) {
|
|
31
|
+
if (/\bstt\b/.test(d))
|
|
32
|
+
return { type: "STT_TAX", dividendRate: null };
|
|
33
|
+
if (d.includes("stamp"))
|
|
34
|
+
return { type: "STAMP_DUTY_TAX", dividendRate: null };
|
|
35
|
+
if (/\btds\b/.test(d))
|
|
36
|
+
return { type: "TDS_TAX", dividendRate: null };
|
|
37
|
+
return { type: "MISC", dividendRate: null };
|
|
38
|
+
}
|
|
39
|
+
const u = big(units);
|
|
40
|
+
if (!u.eq(0) && REVERSAL_RE.test(d))
|
|
41
|
+
return { type: "REVERSAL", dividendRate: null };
|
|
42
|
+
// KFintech prints pledge and lien rows with zero units: the units stay in
|
|
43
|
+
// the folio and only a lien is recorded (measured on a real statement).
|
|
44
|
+
if (u.eq(0) && PLEDGE_RE.test(d)) {
|
|
45
|
+
return { type: PLEDGE_INVOKE_RE.test(d) ? "PLEDGE_INVOKED" : PLEDGE_RELEASE_RE.test(d) ? "PLEDGE_RELEASE" : "PLEDGE", dividendRate: null };
|
|
46
|
+
}
|
|
47
|
+
if (u.gt(0)) {
|
|
48
|
+
if (d.includes("gift"))
|
|
49
|
+
return { type: "GIFT_IN", dividendRate: null };
|
|
50
|
+
if (MERGER_RE.test(d))
|
|
51
|
+
return { type: "SWITCH_IN_MERGER", dividendRate: null };
|
|
52
|
+
if (SWITCH_RE.test(d) || STP_RE.test(d))
|
|
53
|
+
return { type: "SWITCH_IN", dividendRate: null };
|
|
54
|
+
// Units coming back from a pledge; an unexplained "lien" credit stays UNKNOWN.
|
|
55
|
+
if (PLEDGE_RE.test(d))
|
|
56
|
+
return { type: PLEDGE_RELEASE_RE.test(d) ? "PLEDGE_RELEASE" : "UNKNOWN", dividendRate: null };
|
|
57
|
+
if (TRANSFER_RE.test(d))
|
|
58
|
+
return { type: "TRANSFER_IN", dividendRate: null };
|
|
59
|
+
if (BONUS_RE.test(d))
|
|
60
|
+
return { type: "BONUS", dividendRate: null };
|
|
61
|
+
if (ADJUST_RE.test(d))
|
|
62
|
+
return { type: "UNIT_ADJUSTMENT", dividendRate: null };
|
|
63
|
+
if (d.includes("segregat"))
|
|
64
|
+
return { type: "SEGREGATION", dividendRate: null };
|
|
65
|
+
if (SIP_RE.test(d))
|
|
66
|
+
return { type: "PURCHASE_SIP", dividendRate: null };
|
|
67
|
+
if (PURCHASE_WORDS_RE.test(d))
|
|
68
|
+
return { type: "PURCHASE", dividendRate: null };
|
|
69
|
+
return { type: "UNKNOWN", dividendRate: null };
|
|
70
|
+
}
|
|
71
|
+
if (u.lt(0)) {
|
|
72
|
+
if (d.includes("gift"))
|
|
73
|
+
return { type: "GIFT_OUT", dividendRate: null };
|
|
74
|
+
if (MERGER_RE.test(d))
|
|
75
|
+
return { type: "SWITCH_OUT_MERGER", dividendRate: null };
|
|
76
|
+
if (SWITCH_RE.test(d) || STP_RE.test(d))
|
|
77
|
+
return { type: "SWITCH_OUT", dividendRate: null };
|
|
78
|
+
// A release wording on a debit makes no sense; leave it UNKNOWN rather than guess.
|
|
79
|
+
if (PLEDGE_RE.test(d))
|
|
80
|
+
return { type: PLEDGE_INVOKE_RE.test(d) ? "PLEDGE_INVOKED" : PLEDGE_RELEASE_RE.test(d) ? "UNKNOWN" : "PLEDGE", dividendRate: null };
|
|
81
|
+
if (TRANSFER_RE.test(d))
|
|
82
|
+
return { type: "TRANSFER_OUT", dividendRate: null };
|
|
83
|
+
if (ADJUST_RE.test(d))
|
|
84
|
+
return { type: "UNIT_ADJUSTMENT", dividendRate: null };
|
|
85
|
+
if (SWP_RE.test(d))
|
|
86
|
+
return { type: "SWP", dividendRate: null };
|
|
87
|
+
if (REDEMPTION_WORDS_RE.test(d))
|
|
88
|
+
return { type: "REDEMPTION", dividendRate: null };
|
|
89
|
+
return { type: "UNKNOWN", dividendRate: null };
|
|
90
|
+
}
|
|
91
|
+
return { type: "UNKNOWN", dividendRate: null };
|
|
92
|
+
}
|
|
93
|
+
export function giftFolio(description) {
|
|
94
|
+
return GIFT_FOLIO_RE.exec(description)?.[1] ?? null;
|
|
95
|
+
}
|
|
96
|
+
/** casparser `get_parsed_scheme_name`: drop (formerly …)/(erstwhile …)/(Demat …) trailers. */
|
|
97
|
+
export function cleanSchemeName(raw) {
|
|
98
|
+
let s = raw.replace(/\((formerly|erstwhile).+?\)/gis, "").trim();
|
|
99
|
+
// casparser only strips "(Non-Demat"; real CAMS files also print "(Non Demat)".
|
|
100
|
+
s = s.replace(/\((Demat|Non[- ]?Demat).*/is, "").trim();
|
|
101
|
+
s = s.replace(/\s+/g, " ").trim();
|
|
102
|
+
return s.replace(/[^a-zA-Z0-9_)]+$/, "").trim();
|
|
103
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CAMS / KFintech DETAILED parser. The state machine follows casparser
|
|
3
|
+
* `cams_detailed.py` (MIT): AMC → folio → scheme-header region → opening
|
|
4
|
+
* balance → transaction rows (+ wrapped continuations) → closing balance /
|
|
5
|
+
* NAV / value / cost footer. Differences: printed balances and document order
|
|
6
|
+
* are kept, nothing is inferred from sign alone, and every skipped dated row,
|
|
7
|
+
* ambiguous cell and unbuilt header region is recorded in `coverage`.
|
|
8
|
+
*/
|
|
9
|
+
import type { Detection } from "../../detect.js";
|
|
10
|
+
import type { ExtractedDocument } from "../../extract/types.js";
|
|
11
|
+
import type { CasDocument } from "../../model.js";
|
|
12
|
+
export declare const ENGINE_VERSION = "0.1.0-m2";
|
|
13
|
+
/** "dd-Mmm-yyyy" → ISO date, or null unless it is a real calendar date (PLAN.md §5.4 group 3). */
|
|
14
|
+
export declare function isoDate(s: string): string | null;
|
|
15
|
+
export declare function parseDetailed(doc: ExtractedDocument, detection: Detection): CasDocument;
|