@zanii/redact 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,48 @@
1
+ # @zanii/redact
2
+
3
+ **Selective disclosure** for Zanii receipts — prove an agent acted on specific
4
+ data *without revealing the rest*. Each payload field is salted and committed; the
5
+ commitments form a Merkle tree whose **root becomes the receipt payload**. Later
6
+ you disclose individual fields with an inclusion proof that anyone verifies against
7
+ the immutable, anchored ledger entry — while the undisclosed fields stay hidden.
8
+
9
+ For privacy-preserving audits and cross-org sharing. **No server change** — the
10
+ log only ever sees the root.
11
+
12
+ ```sh
13
+ npm install @zanii/redact @zanii/sdk
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import { commit, disclose, verifyDisclosure } from '@zanii/redact';
20
+ import { ZaniiAgent } from '@zanii/sdk';
21
+
22
+ const agent = new ZaniiAgent({ /* … */ });
23
+
24
+ // 1. Commit the sensitive payload; record the envelope (root only):
25
+ const c = commit({ customer: 'acme-corp', amount: 4999, ssn: '123-45-6789' });
26
+ await agent.record({ target: 'billing.charge', payload: c.envelope });
27
+ // keep c.secrets private
28
+
29
+ // 2. Later, disclose just one field to an auditor:
30
+ const d = disclose(c, 'amount');
31
+
32
+ // 3. Anyone verifies it against the on-ledger envelope — the other fields stay hidden:
33
+ verifyDisclosure(d, c.envelope); // → true
34
+ ```
35
+
36
+ The recorded envelope contains only the Merkle root and the field *names* — never
37
+ the values. A disclosure reveals exactly one field; a tampered value, index, or
38
+ root fails verification. Reuses `@zanii/core`'s audited Merkle + JCS, so TS and
39
+ Python produce identical roots and proofs (format: "Zanii Selective Disclosure v1",
40
+ SPEC.md §12).
41
+
42
+ ## Changelog
43
+
44
+ - **0.1.0** — initial release: `commit`, `disclose`, `verifyDisclosure`.
45
+
46
+ ## License
47
+
48
+ Apache-2.0.
@@ -0,0 +1,47 @@
1
+ /** The public envelope — this becomes the receipt payload. Reveals field *names*, not values. */
2
+ export interface RedactEnvelope {
3
+ _zr: 'sd-v1';
4
+ /** `sha256:<hex>` Merkle root over the sorted field commitments. */
5
+ root: string;
6
+ /** Field names in sorted order (their index in the tree). */
7
+ keys: string[];
8
+ }
9
+ export interface CommitSecrets {
10
+ fields: Record<string, {
11
+ value: unknown;
12
+ salt: string;
13
+ index: number;
14
+ }>;
15
+ size: number;
16
+ keys: string[];
17
+ /** Hex leaf hashes, sorted by key — kept privately to produce disclosures. */
18
+ leaves: string[];
19
+ }
20
+ export interface CommitResult {
21
+ envelope: RedactEnvelope;
22
+ secrets: CommitSecrets;
23
+ }
24
+ /** A single-field disclosure — reveals one field + a proof it's in the committed set. */
25
+ export interface Disclosure {
26
+ _zr: 'sd-disclosure-v1';
27
+ key: string;
28
+ value: unknown;
29
+ salt: string;
30
+ index: number;
31
+ size: number;
32
+ proof: string[];
33
+ root: string;
34
+ }
35
+ /**
36
+ * Commit to a set of fields. Record `result.envelope` as the receipt payload; keep
37
+ * `result.secrets` private to disclose fields later.
38
+ */
39
+ export declare function commit(fields: Record<string, unknown>): CommitResult;
40
+ /** Produce a disclosure proof for one field. */
41
+ export declare function disclose(result: CommitResult, key: string): Disclosure;
42
+ /**
43
+ * Verify a disclosure against the envelope that was recorded on the ledger. Returns
44
+ * true only if the revealed `(key, value)` is provably one of the committed fields.
45
+ */
46
+ export declare function verifyDisclosure(d: Disclosure, envelope: RedactEnvelope): boolean;
47
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources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 * @zanii/redact — selective disclosure for Zanii receipts.
3
+ *
4
+ * Prove an agent acted on specific data *without revealing the rest*. Each payload
5
+ * field is salted and committed; the commitments form a Merkle tree whose **root
6
+ * becomes the receipt payload** (so the ledger sees only the root — no server
7
+ * change). Later you disclose individual fields with an inclusion proof that anyone
8
+ * verifies against the immutable, anchored entry, while the undisclosed fields stay
9
+ * hidden. Privacy-preserving audits and cross-org sharing.
10
+ *
11
+ * Reuses `@zanii/core`'s audited Merkle + JCS, so TS and Python produce identical
12
+ * roots and proofs. Format: "Zanii Selective Disclosure v1" (see SPEC.md §12).
13
+ */
14
+ import { canonicalBytes, inclusionProof, leafHash, merkleRoot, verifyInclusion } from '@zanii/core';
15
+ import { randomBytes } from 'node:crypto';
16
+ const hex = (b) => Buffer.from(b).toString('hex');
17
+ const unhex = (h) => new Uint8Array(Buffer.from(h, 'hex'));
18
+ /** Domain-separated field commitment leaf: leafHash(JCS(["zr-field-v1", key, value, salt])). */
19
+ function fieldLeaf(key, value, saltHex) {
20
+ return leafHash(canonicalBytes(['zr-field-v1', key, value, saltHex]));
21
+ }
22
+ /**
23
+ * Commit to a set of fields. Record `result.envelope` as the receipt payload; keep
24
+ * `result.secrets` private to disclose fields later.
25
+ */
26
+ export function commit(fields) {
27
+ const keys = Object.keys(fields).sort();
28
+ if (keys.length === 0)
29
+ throw new Error('commit requires at least one field');
30
+ const salts = keys.map(() => randomBytes(16).toString('hex'));
31
+ const leaves = keys.map((k, i) => fieldLeaf(k, fields[k], salts[i]));
32
+ const root = merkleRoot(leaves);
33
+ const secretFields = {};
34
+ keys.forEach((k, i) => { secretFields[k] = { value: fields[k], salt: salts[i], index: i }; });
35
+ return {
36
+ envelope: { _zr: 'sd-v1', root: `sha256:${hex(root)}`, keys },
37
+ secrets: { fields: secretFields, size: keys.length, keys, leaves: leaves.map(hex) },
38
+ };
39
+ }
40
+ /** Produce a disclosure proof for one field. */
41
+ export function disclose(result, key) {
42
+ const f = result.secrets.fields[key];
43
+ if (!f)
44
+ throw new Error(`unknown field '${key}'`);
45
+ const leaves = result.secrets.leaves.map(unhex);
46
+ const proof = inclusionProof(f.index, leaves).map(hex);
47
+ return { _zr: 'sd-disclosure-v1', key, value: f.value, salt: f.salt, index: f.index, size: result.secrets.size, proof, root: result.envelope.root };
48
+ }
49
+ /**
50
+ * Verify a disclosure against the envelope that was recorded on the ledger. Returns
51
+ * true only if the revealed `(key, value)` is provably one of the committed fields.
52
+ */
53
+ export function verifyDisclosure(d, envelope) {
54
+ if (d.root !== envelope.root)
55
+ return false;
56
+ if (d.size !== envelope.keys.length)
57
+ return false;
58
+ if (envelope.keys[d.index] !== d.key)
59
+ return false;
60
+ const leaf = fieldLeaf(d.key, d.value, d.salt);
61
+ const rootBytes = unhex(envelope.root.replace('sha256:', ''));
62
+ try {
63
+ return verifyInclusion(leaf, d.index, d.size, d.proof.map(unhex), rootBytes);
64
+ }
65
+ catch {
66
+ return false;
67
+ }
68
+ }
69
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACpG,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,GAAG,GAAG,CAAC,CAAa,EAAU,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACtE,MAAM,KAAK,GAAG,CAAC,CAAS,EAAc,EAAE,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;AAE/E,gGAAgG;AAChG,SAAS,SAAS,CAAC,GAAW,EAAE,KAAc,EAAE,OAAe;IAC7D,OAAO,QAAQ,CAAC,cAAc,CAAC,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MAAM,SAAS,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC;IAC9D,IAAI,CAAC;QACH,OAAO,eAAe,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,CAAC;IAC/E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@zanii/redact",
3
+ "version": "0.1.0",
4
+ "description": "Selective disclosure for Zanii receipts - prove an agent acted on specific data without revealing the rest, via salted field commitments and Merkle inclusion proofs.",
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
+ "@zanii/core": "0.2.0"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc -p tsconfig.build.json",
38
+ "test": "vitest run",
39
+ "typecheck": "tsc --noEmit"
40
+ }
41
+ }
package/src/index.ts ADDED
@@ -0,0 +1,101 @@
1
+ /**
2
+ * @zanii/redact — selective disclosure for Zanii receipts.
3
+ *
4
+ * Prove an agent acted on specific data *without revealing the rest*. Each payload
5
+ * field is salted and committed; the commitments form a Merkle tree whose **root
6
+ * becomes the receipt payload** (so the ledger sees only the root — no server
7
+ * change). Later you disclose individual fields with an inclusion proof that anyone
8
+ * verifies against the immutable, anchored entry, while the undisclosed fields stay
9
+ * hidden. Privacy-preserving audits and cross-org sharing.
10
+ *
11
+ * Reuses `@zanii/core`'s audited Merkle + JCS, so TS and Python produce identical
12
+ * roots and proofs. Format: "Zanii Selective Disclosure v1" (see SPEC.md §12).
13
+ */
14
+ import { canonicalBytes, inclusionProof, leafHash, merkleRoot, verifyInclusion } from '@zanii/core';
15
+ import { randomBytes } from 'node:crypto';
16
+
17
+ const hex = (b: Uint8Array): string => Buffer.from(b).toString('hex');
18
+ const unhex = (h: string): Uint8Array => new Uint8Array(Buffer.from(h, 'hex'));
19
+
20
+ /** Domain-separated field commitment leaf: leafHash(JCS(["zr-field-v1", key, value, salt])). */
21
+ function fieldLeaf(key: string, value: unknown, saltHex: string): Uint8Array {
22
+ return leafHash(canonicalBytes(['zr-field-v1', key, value, saltHex]));
23
+ }
24
+
25
+ /** The public envelope — this becomes the receipt payload. Reveals field *names*, not values. */
26
+ export interface RedactEnvelope {
27
+ _zr: 'sd-v1';
28
+ /** `sha256:<hex>` Merkle root over the sorted field commitments. */
29
+ root: string;
30
+ /** Field names in sorted order (their index in the tree). */
31
+ keys: string[];
32
+ }
33
+
34
+ export interface CommitSecrets {
35
+ fields: Record<string, { value: unknown; salt: string; index: number }>;
36
+ size: number;
37
+ keys: string[];
38
+ /** Hex leaf hashes, sorted by key — kept privately to produce disclosures. */
39
+ leaves: string[];
40
+ }
41
+
42
+ export interface CommitResult {
43
+ envelope: RedactEnvelope;
44
+ secrets: CommitSecrets;
45
+ }
46
+
47
+ /** A single-field disclosure — reveals one field + a proof it's in the committed set. */
48
+ export interface Disclosure {
49
+ _zr: 'sd-disclosure-v1';
50
+ key: string;
51
+ value: unknown;
52
+ salt: string;
53
+ index: number;
54
+ size: number;
55
+ proof: string[];
56
+ root: string;
57
+ }
58
+
59
+ /**
60
+ * Commit to a set of fields. Record `result.envelope` as the receipt payload; keep
61
+ * `result.secrets` private to disclose fields later.
62
+ */
63
+ export function commit(fields: Record<string, unknown>): CommitResult {
64
+ const keys = Object.keys(fields).sort();
65
+ if (keys.length === 0) throw new Error('commit requires at least one field');
66
+ const salts = keys.map(() => randomBytes(16).toString('hex'));
67
+ const leaves = keys.map((k, i) => fieldLeaf(k, fields[k], salts[i]!));
68
+ const root = merkleRoot(leaves);
69
+ const secretFields: CommitSecrets['fields'] = {};
70
+ keys.forEach((k, i) => { secretFields[k] = { value: fields[k], salt: salts[i]!, index: i }; });
71
+ return {
72
+ envelope: { _zr: 'sd-v1', root: `sha256:${hex(root)}`, keys },
73
+ secrets: { fields: secretFields, size: keys.length, keys, leaves: leaves.map(hex) },
74
+ };
75
+ }
76
+
77
+ /** Produce a disclosure proof for one field. */
78
+ export function disclose(result: CommitResult, key: string): Disclosure {
79
+ const f = result.secrets.fields[key];
80
+ if (!f) throw new Error(`unknown field '${key}'`);
81
+ const leaves = result.secrets.leaves.map(unhex);
82
+ const proof = inclusionProof(f.index, leaves).map(hex);
83
+ return { _zr: 'sd-disclosure-v1', key, value: f.value, salt: f.salt, index: f.index, size: result.secrets.size, proof, root: result.envelope.root };
84
+ }
85
+
86
+ /**
87
+ * Verify a disclosure against the envelope that was recorded on the ledger. Returns
88
+ * true only if the revealed `(key, value)` is provably one of the committed fields.
89
+ */
90
+ export function verifyDisclosure(d: Disclosure, envelope: RedactEnvelope): boolean {
91
+ if (d.root !== envelope.root) return false;
92
+ if (d.size !== envelope.keys.length) return false;
93
+ if (envelope.keys[d.index] !== d.key) return false;
94
+ const leaf = fieldLeaf(d.key, d.value, d.salt);
95
+ const rootBytes = unhex(envelope.root.replace('sha256:', ''));
96
+ try {
97
+ return verifyInclusion(leaf, d.index, d.size, d.proof.map(unhex), rootBytes);
98
+ } catch {
99
+ return false;
100
+ }
101
+ }