@zanii/kyb 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 ADDED
@@ -0,0 +1,159 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work; and
103
+
104
+ (d) If the Work includes a "NOTICE" text file as part of its
105
+ distribution, then any Derivative Works that You distribute must
106
+ include a readable copy of the attribution notices contained
107
+ within such NOTICE file.
108
+
109
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
110
+ any Contribution intentionally submitted for inclusion in the Work
111
+ by You to the Licensor shall be under the terms and conditions of
112
+ this License, without any additional terms or conditions.
113
+
114
+ 6. Trademarks. This License does not grant permission to use the trade
115
+ names, trademarks, service marks, or product names of the Licensor,
116
+ except as required for reasonable and customary use in describing the
117
+ origin of the Work and reproducing the content of the NOTICE file.
118
+
119
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed
120
+ to in writing, Licensor provides the Work (and each Contributor
121
+ provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
122
+ OR CONDITIONS OF ANY KIND, either express or implied, including,
123
+ without limitation, any warranties or conditions of TITLE,
124
+ NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
125
+
126
+ 8. Limitation of Liability. In no event and under no legal theory,
127
+ whether in tort (including negligence), contract, or otherwise,
128
+ unless required by applicable law (such as deliberate and grossly
129
+ negligent acts) or agreed to in writing, shall any Contributor be
130
+ liable to You for damages, including any direct, indirect, special,
131
+ incidental, or consequential damages of any character arising as a
132
+ result of this License or out of the use or inability to use the
133
+ Work.
134
+
135
+ 9. Accepting Warranty or Additional Liability. While redistributing
136
+ the Work or Derivative Works thereof, You may choose to offer,
137
+ and charge a fee for, acceptance of support, warranty, indemnity,
138
+ or other liability obligations and/or rights consistent with this
139
+ License. However, in accepting such obligations, You may act only
140
+ on Your own behalf and on Your sole responsibility, not on behalf
141
+ of any other Contributor, and only if You agree to indemnify,
142
+ defend, and hold each Contributor harmless for any liability
143
+ incurred by, or claims asserted against, such Contributor.
144
+
145
+ END OF TERMS AND CONDITIONS
146
+
147
+ Copyright 2026 Zanii
148
+
149
+ Licensed under the Apache License, Version 2.0 (the "License");
150
+ you may not use this file except in compliance with the License.
151
+ You may obtain a copy of the License at
152
+
153
+ http://www.apache.org/licenses/LICENSE-2.0
154
+
155
+ Unless required by applicable law or agreed to in writing, software
156
+ distributed under the License is distributed on an "AS IS" BASIS,
157
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
158
+ See the License for the specific language governing permissions and
159
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # @zanii/kyb
2
+
3
+ Owner-to-legal-entity binding: **who is legally on the hook?** `@zanii/kya` screens
4
+ *agents*; `@zanii/credentials` binds *institutions* to domains. This closes the gap
5
+ between them — binding an **owner did:key** to a legal entity, so `escrow`,
6
+ `insurance`, and `sla` parties know who they are contracting with in the legal sense.
7
+
8
+ ```sh
9
+ npm install @zanii/kyb @zanii/core
10
+ ```
11
+
12
+ The binding is deliberately **two-way** — one-sided claims are refused:
13
+
14
+ ```jsonc
15
+ // 1. the company serves https://acme.com/.well-known/zanii-owner.json
16
+ { "v": 1,
17
+ "entity": { "name": "Acme FZ-LLC", "jurisdiction": "AE-DU", "registration": "DED-12345" },
18
+ "domain": "acme.com",
19
+ "owner_dids": ["did:key:z6Mk…"] }
20
+ ```
21
+
22
+ ```ts
23
+ import { buildOwnerAttestation, fetchAndVerifyOwner } from '@zanii/kyb';
24
+
25
+ // 2. the owner signs the other half — this key claims this domain and entity
26
+ const attestation = buildOwnerAttestation(
27
+ { did: owner.did, domain: 'acme.com', entity: { name: 'Acme FZ-LLC' }, ts: now },
28
+ ownerPrivateKey,
29
+ );
30
+
31
+ // 3. anyone verifies both halves agree
32
+ const r = await fetchAndVerifyOwner(attestation);
33
+ r.ok; // signed by the DID + listed by the domain + same entity on both sides
34
+ r.entity; // the entity BOTH sides agree on
35
+ ```
36
+
37
+ Python: `from zanii.kyb import build_owner_attestation, fetch_and_verify_owner, ...`
38
+ — byte-identical JCS objects and hashes.
39
+
40
+ ## Why two-way
41
+
42
+ A domain listing a DID proves nothing alone — anyone can list any DID. A key naming a
43
+ domain proves nothing alone — anyone can name any domain. Both together bind: whoever
44
+ controls the domain and whoever controls the key agree they are the same entity. The
45
+ doc must be served from the domain it claims (no cross-domain vouching), entity names
46
+ must match exactly, and `jurisdiction`/`registration` must agree wherever both sides
47
+ state them (silence on a field is not a mismatch).
48
+
49
+ ## The limit, stated up front
50
+
51
+ **Domain control is not corporate registration.** Zanii verifies that both sides claim
52
+ the same entity — it does NOT verify the entity exists in a corporate registry, is
53
+ solvent, or is lawfully named. Real KYB (registry checks, sanctions, UBO) is an
54
+ off-protocol process this package makes *auditable*: the attestation is signed and
55
+ permanent, so a false claim is attributable forever. We verify domain control; we
56
+ never certify companies — the `@zanii/credentials` posture, unchanged.
@@ -0,0 +1,62 @@
1
+ export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
2
+ /** The legal-entity details both sides must agree on. */
3
+ export interface LegalEntity {
4
+ /** Registered name, e.g. `Acme FZ-LLC`. */
5
+ name: string;
6
+ /** Jurisdiction, e.g. `AE-DU`, `US-DE`. */
7
+ jurisdiction?: string;
8
+ /** Registry number / licence id — auditable off-protocol. */
9
+ registration?: string;
10
+ }
11
+ /** Published at `https://<domain>/.well-known/zanii-owner.json`. Domain control = half the binding. */
12
+ export interface OwnerDoc {
13
+ v: 1;
14
+ entity: LegalEntity;
15
+ /** MUST equal the domain it is served from — no cross-domain vouching. */
16
+ domain: string;
17
+ /** The owner `did:key`(s) this entity operates. Rotation: overlap old + new. */
18
+ owner_dids: string[];
19
+ }
20
+ /** The owner-signed half: this key claims this domain and entity. */
21
+ export interface OwnerAttestation {
22
+ v: 1;
23
+ type: 'kyb.attestation';
24
+ did: string;
25
+ domain: string;
26
+ entity: LegalEntity;
27
+ ts: string;
28
+ /** `ed25519:<hex>` by `did`. */
29
+ sig?: string;
30
+ }
31
+ /** Build + sign the owner's half of the binding. */
32
+ export declare function buildOwnerAttestation(fields: {
33
+ did: string;
34
+ domain: string;
35
+ entity: LegalEntity;
36
+ ts: string;
37
+ }, ownerPrivateKey: Uint8Array): OwnerAttestation;
38
+ /** Verify the attestation is signed by the DID it names. */
39
+ export declare function verifyOwnerAttestation(a: OwnerAttestation | undefined): boolean;
40
+ export declare function attestationHash(a: OwnerAttestation): string;
41
+ /** Fetch a domain's owner doc. Injected fetch keeps it offline-testable. Null = domain publishes nothing usable. */
42
+ export declare function resolveOwnerDoc(domain: string, opts?: {
43
+ fetchImpl?: FetchLike;
44
+ }): Promise<OwnerDoc | null>;
45
+ export interface KybCheck {
46
+ ok: boolean;
47
+ reasons: string[];
48
+ /** Set when ok — the entity BOTH sides agree on. */
49
+ entity: LegalEntity | null;
50
+ }
51
+ /**
52
+ * The two-way check — pure and offline (pass the doc you resolved). `ok` only when:
53
+ * the attestation is validly signed by its DID, the domain doc lists that DID, and
54
+ * both sides name the SAME entity (exact `name` match; `jurisdiction`/`registration`
55
+ * must match when both sides state them).
56
+ */
57
+ export declare function verifyOwnerBinding(attestation: OwnerAttestation, doc: OwnerDoc | null): KybCheck;
58
+ /** Convenience: resolve the domain doc and run the two-way check in one call. */
59
+ export declare function fetchAndVerifyOwner(attestation: OwnerAttestation, opts?: {
60
+ fetchImpl?: FetchLike;
61
+ }): Promise<KybCheck>;
62
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkCA,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEjF,yDAAyD;AACzD,MAAM,WAAW,WAAW;IAC1B,2CAA2C;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,YAAY,CAAC,EAA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G,IAAI,CAAC;CAC5B;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,gBAAgB,EAAE,GAAG,EAAE,QAAQ,GAAG,IAAI,GAAG,QAAQ,CAyBhG;AAED,iFAAiF;AACjF,wBAAsB,mBAAmB,CACvC,WAAW,EAAE,gBAAgB,EAC7B,IAAI,GAAE;IAAE,SAAS,CAAC,EAAE,SAAS,CAAA;CAAO,GACnC,OAAO,CAAC,QAAQ,CAAC,CAMnB"}
package/dist/index.js ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * @zanii/kyb — owner-to-legal-entity binding: who is legally on the hook?
3
+ *
4
+ * `@zanii/kya` screens *agents*; `@zanii/credentials` binds *institutions* to domains.
5
+ * The gap between them: nothing binds an **owner did:key** to a legal entity — and for
6
+ * `escrow`, `insurance`, and `sla` to work commercially, the parties need to know who
7
+ * they are contracting with in the legal sense.
8
+ *
9
+ * The binding is deliberately **two-way** — one-sided claims are refused:
10
+ *
11
+ * 1. The company serves `/.well-known/zanii-owner.json` on its **own domain**, listing
12
+ * its owner DIDs and legal-entity details (domain control = the web's existing
13
+ * trust anchor, the same mechanism `@zanii/credentials` uses).
14
+ * 2. The owner signs an **attestation** naming that exact domain and entity.
15
+ *
16
+ * A domain listing a DID it doesn't control proves nothing (anyone can list any DID);
17
+ * a key claiming a domain it doesn't control proves nothing (anyone can name any
18
+ * domain). Both together bind: whoever controls the domain and whoever controls the
19
+ * key agree they are the same entity.
20
+ *
21
+ * ## The limit, stated up front
22
+ *
23
+ * **Domain control is not corporate registration.** Zanii verifies that whoever
24
+ * controls `acme.com` and whoever controls the owner key both claim "Acme FZ-LLC" —
25
+ * it does NOT verify Acme FZ-LLC exists in a corporate registry, is solvent, or is
26
+ * lawfully named. Real KYB (registry checks, sanctions, UBO) is an off-protocol
27
+ * process this package makes *auditable*: the attestation is signed and permanent, so
28
+ * a false claim is attributable forever. We verify domain control; we never certify
29
+ * companies — the `@zanii/credentials` posture, unchanged.
30
+ */
31
+ import { ed25519 } from '@noble/curves/ed25519';
32
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
33
+ import { canonicalBytes, jcsHash, publicKeyFromDid } from '@zanii/core';
34
+ /** Build + sign the owner's half of the binding. */
35
+ export function buildOwnerAttestation(fields, ownerPrivateKey) {
36
+ if (!fields.did)
37
+ throw new Error('did is required');
38
+ if (!fields.domain)
39
+ throw new Error('domain is required');
40
+ if (!fields.entity?.name)
41
+ throw new Error('entity.name is required');
42
+ if (Number.isNaN(Date.parse(fields.ts)))
43
+ throw new Error('ts must be an ISO timestamp');
44
+ const unsigned = {
45
+ v: 1,
46
+ type: 'kyb.attestation',
47
+ did: fields.did,
48
+ domain: fields.domain.toLowerCase(),
49
+ entity: fields.entity,
50
+ ts: fields.ts,
51
+ };
52
+ const sig = ed25519.sign(canonicalBytes(unsigned), ownerPrivateKey);
53
+ return { ...unsigned, sig: `ed25519:${bytesToHex(sig)}` };
54
+ }
55
+ /** Verify the attestation is signed by the DID it names. */
56
+ export function verifyOwnerAttestation(a) {
57
+ const p = a ?? {};
58
+ if (p.v !== 1 || p.type !== 'kyb.attestation' || !p.did || !p.domain || !p.entity?.name)
59
+ return false;
60
+ if (typeof p.ts !== 'string' || Number.isNaN(Date.parse(p.ts)))
61
+ return false;
62
+ const pub = publicKeyFromDid(p.did);
63
+ const hex = typeof p.sig === 'string' && p.sig.startsWith('ed25519:') ? p.sig.slice(8) : '';
64
+ if (!pub || !/^[0-9a-f]{128}$/.test(hex))
65
+ return false;
66
+ const { sig: _omit, ...unsigned } = p;
67
+ try {
68
+ return ed25519.verify(hexToBytes(hex), canonicalBytes(unsigned), pub);
69
+ }
70
+ catch {
71
+ return false;
72
+ }
73
+ }
74
+ export function attestationHash(a) {
75
+ return jcsHash(a);
76
+ }
77
+ /** Fetch a domain's owner doc. Injected fetch keeps it offline-testable. Null = domain publishes nothing usable. */
78
+ export async function resolveOwnerDoc(domain, opts = {}) {
79
+ const fetchImpl = opts.fetchImpl ?? ((u, i) => fetch(u, i));
80
+ const host = domain.toLowerCase();
81
+ let doc;
82
+ try {
83
+ const res = await fetchImpl(`https://${host}/.well-known/zanii-owner.json`);
84
+ if (!res.ok)
85
+ return null;
86
+ doc = (await res.json());
87
+ }
88
+ catch {
89
+ return null;
90
+ }
91
+ if (doc?.v !== 1 || !doc.entity?.name || !Array.isArray(doc.owner_dids))
92
+ return null;
93
+ // Served-from domain must match the claimed domain — no cross-domain vouching.
94
+ if (typeof doc.domain !== 'string' || doc.domain.toLowerCase() !== host)
95
+ return null;
96
+ return doc;
97
+ }
98
+ /**
99
+ * The two-way check — pure and offline (pass the doc you resolved). `ok` only when:
100
+ * the attestation is validly signed by its DID, the domain doc lists that DID, and
101
+ * both sides name the SAME entity (exact `name` match; `jurisdiction`/`registration`
102
+ * must match when both sides state them).
103
+ */
104
+ export function verifyOwnerBinding(attestation, doc) {
105
+ const reasons = [];
106
+ if (!verifyOwnerAttestation(attestation)) {
107
+ return { ok: false, reasons: ['attestation is not validly signed by its DID'], entity: null };
108
+ }
109
+ if (!doc || doc.v !== 1 || typeof doc.domain !== 'string' || !doc.entity?.name || !Array.isArray(doc.owner_dids)) {
110
+ return { ok: false, reasons: [`${attestation.domain} publishes no valid owner doc — the domain side of the binding is missing`], entity: null };
111
+ }
112
+ if (doc.domain.toLowerCase() !== attestation.domain.toLowerCase()) {
113
+ reasons.push(`attestation names ${attestation.domain} but the doc is for ${doc.domain}`);
114
+ }
115
+ if (!doc.owner_dids.includes(attestation.did)) {
116
+ reasons.push(`the domain does not list ${attestation.did} — anyone can sign a claim; only the domain's list makes it mutual`);
117
+ }
118
+ if (doc.entity.name !== attestation.entity.name) {
119
+ reasons.push(`entity name mismatch: domain says '${doc.entity.name}', owner claims '${attestation.entity.name}'`);
120
+ }
121
+ for (const field of ['jurisdiction', 'registration']) {
122
+ const a = attestation.entity[field];
123
+ const d = doc.entity[field];
124
+ if (a !== undefined && d !== undefined && a !== d) {
125
+ reasons.push(`entity ${field} mismatch: domain says '${d}', owner claims '${a}'`);
126
+ }
127
+ }
128
+ return { ok: reasons.length === 0, reasons, entity: reasons.length === 0 ? doc.entity : null };
129
+ }
130
+ /** Convenience: resolve the domain doc and run the two-way check in one call. */
131
+ export async function fetchAndVerifyOwner(attestation, opts = {}) {
132
+ if (!verifyOwnerAttestation(attestation)) {
133
+ return { ok: false, reasons: ['attestation is not validly signed by its DID'], entity: null };
134
+ }
135
+ const doc = await resolveOwnerDoc(attestation.domain, opts);
136
+ return verifyOwnerBinding(attestation, doc);
137
+ }
138
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAoCxE,oDAAoD;AACpD,MAAM,UAAU,qBAAqB,CACnC,MAAwE,EACxE,eAA2B;IAE3B,IAAI,CAAC,MAAM,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACpD,IAAI,CAAC,MAAM,CAAC,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;IAC1D,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IACrE,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACxF,MAAM,QAAQ,GAAG;QACf,CAAC,EAAE,CAAU;QACb,IAAI,EAAE,iB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4
+ "description": "Owner-to-legal-entity binding - the missing link between an owner did:key and who is legally on the hook. Two-way: the company serves .well-known/zanii-owner.json on its own domain AND the owner signs an attestation naming that domain and entity; verification requires both. Zanii verifies domain control, never corporate registration - the claim becomes verifiable and attributable, not officially certified.",
5
+ "license": "Apache-2.0",
6
+ "homepage": "https://ledger.zanii.agency",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "src",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "engines": {
26
+ "node": ">=18"
27
+ },
28
+ "devDependencies": {
29
+ "@types/node": "^26.1.0",
30
+ "typescript": "^5.8.2",
31
+ "vitest": "^3.0.9"
32
+ },
33
+ "dependencies": {
34
+ "@noble/curves": "^1.8.1",
35
+ "@noble/hashes": "^1.7.1",
36
+ "@zanii/core": "0.4.0"
37
+ },
38
+ "scripts": {
39
+ "build": "tsc -p tsconfig.build.json",
40
+ "test": "vitest run",
41
+ "typecheck": "tsc --noEmit"
42
+ }
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,178 @@
1
+ /**
2
+ * @zanii/kyb — owner-to-legal-entity binding: who is legally on the hook?
3
+ *
4
+ * `@zanii/kya` screens *agents*; `@zanii/credentials` binds *institutions* to domains.
5
+ * The gap between them: nothing binds an **owner did:key** to a legal entity — and for
6
+ * `escrow`, `insurance`, and `sla` to work commercially, the parties need to know who
7
+ * they are contracting with in the legal sense.
8
+ *
9
+ * The binding is deliberately **two-way** — one-sided claims are refused:
10
+ *
11
+ * 1. The company serves `/.well-known/zanii-owner.json` on its **own domain**, listing
12
+ * its owner DIDs and legal-entity details (domain control = the web's existing
13
+ * trust anchor, the same mechanism `@zanii/credentials` uses).
14
+ * 2. The owner signs an **attestation** naming that exact domain and entity.
15
+ *
16
+ * A domain listing a DID it doesn't control proves nothing (anyone can list any DID);
17
+ * a key claiming a domain it doesn't control proves nothing (anyone can name any
18
+ * domain). Both together bind: whoever controls the domain and whoever controls the
19
+ * key agree they are the same entity.
20
+ *
21
+ * ## The limit, stated up front
22
+ *
23
+ * **Domain control is not corporate registration.** Zanii verifies that whoever
24
+ * controls `acme.com` and whoever controls the owner key both claim "Acme FZ-LLC" —
25
+ * it does NOT verify Acme FZ-LLC exists in a corporate registry, is solvent, or is
26
+ * lawfully named. Real KYB (registry checks, sanctions, UBO) is an off-protocol
27
+ * process this package makes *auditable*: the attestation is signed and permanent, so
28
+ * a false claim is attributable forever. We verify domain control; we never certify
29
+ * companies — the `@zanii/credentials` posture, unchanged.
30
+ */
31
+ import { ed25519 } from '@noble/curves/ed25519';
32
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
33
+ import { canonicalBytes, jcsHash, publicKeyFromDid } from '@zanii/core';
34
+
35
+ export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
36
+
37
+ /** The legal-entity details both sides must agree on. */
38
+ export interface LegalEntity {
39
+ /** Registered name, e.g. `Acme FZ-LLC`. */
40
+ name: string;
41
+ /** Jurisdiction, e.g. `AE-DU`, `US-DE`. */
42
+ jurisdiction?: string;
43
+ /** Registry number / licence id — auditable off-protocol. */
44
+ registration?: string;
45
+ }
46
+
47
+ /** Published at `https://<domain>/.well-known/zanii-owner.json`. Domain control = half the binding. */
48
+ export interface OwnerDoc {
49
+ v: 1;
50
+ entity: LegalEntity;
51
+ /** MUST equal the domain it is served from — no cross-domain vouching. */
52
+ domain: string;
53
+ /** The owner `did:key`(s) this entity operates. Rotation: overlap old + new. */
54
+ owner_dids: string[];
55
+ }
56
+
57
+ /** The owner-signed half: this key claims this domain and entity. */
58
+ export interface OwnerAttestation {
59
+ v: 1;
60
+ type: 'kyb.attestation';
61
+ did: string;
62
+ domain: string;
63
+ entity: LegalEntity;
64
+ ts: string;
65
+ /** `ed25519:<hex>` by `did`. */
66
+ sig?: string;
67
+ }
68
+
69
+ /** Build + sign the owner's half of the binding. */
70
+ export function buildOwnerAttestation(
71
+ fields: { did: string; domain: string; entity: LegalEntity; ts: string },
72
+ ownerPrivateKey: Uint8Array,
73
+ ): OwnerAttestation {
74
+ if (!fields.did) throw new Error('did is required');
75
+ if (!fields.domain) throw new Error('domain is required');
76
+ if (!fields.entity?.name) throw new Error('entity.name is required');
77
+ if (Number.isNaN(Date.parse(fields.ts))) throw new Error('ts must be an ISO timestamp');
78
+ const unsigned = {
79
+ v: 1 as const,
80
+ type: 'kyb.attestation' as const,
81
+ did: fields.did,
82
+ domain: fields.domain.toLowerCase(),
83
+ entity: fields.entity,
84
+ ts: fields.ts,
85
+ };
86
+ const sig = ed25519.sign(canonicalBytes(unsigned), ownerPrivateKey);
87
+ return { ...unsigned, sig: `ed25519:${bytesToHex(sig)}` };
88
+ }
89
+
90
+ /** Verify the attestation is signed by the DID it names. */
91
+ export function verifyOwnerAttestation(a: OwnerAttestation | undefined): boolean {
92
+ const p = a ?? ({} as OwnerAttestation);
93
+ if (p.v !== 1 || p.type !== 'kyb.attestation' || !p.did || !p.domain || !p.entity?.name) return false;
94
+ if (typeof p.ts !== 'string' || Number.isNaN(Date.parse(p.ts))) return false;
95
+ const pub = publicKeyFromDid(p.did);
96
+ const hex = typeof p.sig === 'string' && p.sig.startsWith('ed25519:') ? p.sig.slice(8) : '';
97
+ if (!pub || !/^[0-9a-f]{128}$/.test(hex)) return false;
98
+ const { sig: _omit, ...unsigned } = p;
99
+ try {
100
+ return ed25519.verify(hexToBytes(hex), canonicalBytes(unsigned as unknown as Record<string, unknown>), pub);
101
+ } catch {
102
+ return false;
103
+ }
104
+ }
105
+
106
+ export function attestationHash(a: OwnerAttestation): string {
107
+ return jcsHash(a as unknown as Record<string, unknown>);
108
+ }
109
+
110
+ /** Fetch a domain's owner doc. Injected fetch keeps it offline-testable. Null = domain publishes nothing usable. */
111
+ export async function resolveOwnerDoc(domain: string, opts: { fetchImpl?: FetchLike } = {}): Promise<OwnerDoc | null> {
112
+ const fetchImpl = opts.fetchImpl ?? ((u: string, i?: RequestInit) => fetch(u, i));
113
+ const host = domain.toLowerCase();
114
+ let doc: OwnerDoc;
115
+ try {
116
+ const res = await fetchImpl(`https://${host}/.well-known/zanii-owner.json`);
117
+ if (!res.ok) return null;
118
+ doc = (await res.json()) as OwnerDoc;
119
+ } catch {
120
+ return null;
121
+ }
122
+ if (doc?.v !== 1 || !doc.entity?.name || !Array.isArray(doc.owner_dids)) return null;
123
+ // Served-from domain must match the claimed domain — no cross-domain vouching.
124
+ if (typeof doc.domain !== 'string' || doc.domain.toLowerCase() !== host) return null;
125
+ return doc;
126
+ }
127
+
128
+ export interface KybCheck {
129
+ ok: boolean;
130
+ reasons: string[];
131
+ /** Set when ok — the entity BOTH sides agree on. */
132
+ entity: LegalEntity | null;
133
+ }
134
+
135
+ /**
136
+ * The two-way check — pure and offline (pass the doc you resolved). `ok` only when:
137
+ * the attestation is validly signed by its DID, the domain doc lists that DID, and
138
+ * both sides name the SAME entity (exact `name` match; `jurisdiction`/`registration`
139
+ * must match when both sides state them).
140
+ */
141
+ export function verifyOwnerBinding(attestation: OwnerAttestation, doc: OwnerDoc | null): KybCheck {
142
+ const reasons: string[] = [];
143
+ if (!verifyOwnerAttestation(attestation)) {
144
+ return { ok: false, reasons: ['attestation is not validly signed by its DID'], entity: null };
145
+ }
146
+ if (!doc || doc.v !== 1 || typeof doc.domain !== 'string' || !doc.entity?.name || !Array.isArray(doc.owner_dids)) {
147
+ return { ok: false, reasons: [`${attestation.domain} publishes no valid owner doc — the domain side of the binding is missing`], entity: null };
148
+ }
149
+ if (doc.domain.toLowerCase() !== attestation.domain.toLowerCase()) {
150
+ reasons.push(`attestation names ${attestation.domain} but the doc is for ${doc.domain}`);
151
+ }
152
+ if (!doc.owner_dids.includes(attestation.did)) {
153
+ reasons.push(`the domain does not list ${attestation.did} — anyone can sign a claim; only the domain's list makes it mutual`);
154
+ }
155
+ if (doc.entity.name !== attestation.entity.name) {
156
+ reasons.push(`entity name mismatch: domain says '${doc.entity.name}', owner claims '${attestation.entity.name}'`);
157
+ }
158
+ for (const field of ['jurisdiction', 'registration'] as const) {
159
+ const a = attestation.entity[field];
160
+ const d = doc.entity[field];
161
+ if (a !== undefined && d !== undefined && a !== d) {
162
+ reasons.push(`entity ${field} mismatch: domain says '${d}', owner claims '${a}'`);
163
+ }
164
+ }
165
+ return { ok: reasons.length === 0, reasons, entity: reasons.length === 0 ? doc.entity : null };
166
+ }
167
+
168
+ /** Convenience: resolve the domain doc and run the two-way check in one call. */
169
+ export async function fetchAndVerifyOwner(
170
+ attestation: OwnerAttestation,
171
+ opts: { fetchImpl?: FetchLike } = {},
172
+ ): Promise<KybCheck> {
173
+ if (!verifyOwnerAttestation(attestation)) {
174
+ return { ok: false, reasons: ['attestation is not validly signed by its DID'], entity: null };
175
+ }
176
+ const doc = await resolveOwnerDoc(attestation.domain, opts);
177
+ return verifyOwnerBinding(attestation, doc);
178
+ }