@certen.io/sdk 0.14.1 → 0.15.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/CHANGELOG.md +9 -0
- package/dist/agent/core.d.ts +7 -0
- package/dist/agent/core.d.ts.map +1 -0
- package/dist/agent/core.js +7 -0
- package/dist/agent/core.js.map +1 -0
- package/dist/agent/mandate.d.ts +141 -0
- package/dist/agent/mandate.d.ts.map +1 -0
- package/dist/agent/mandate.js +158 -0
- package/dist/agent/mandate.js.map +1 -0
- package/dist/agent-enrollment.d.ts +83 -0
- package/dist/agent-enrollment.d.ts.map +1 -0
- package/dist/agent-enrollment.js +102 -0
- package/dist/agent-enrollment.js.map +1 -0
- package/dist/core/bytes.d.ts +38 -0
- package/dist/core/bytes.d.ts.map +1 -0
- package/dist/core/bytes.js +161 -0
- package/dist/core/bytes.js.map +1 -0
- package/dist/core/grant-profile.d.ts +133 -0
- package/dist/core/grant-profile.d.ts.map +1 -0
- package/dist/core/grant-profile.js +323 -0
- package/dist/core/grant-profile.js.map +1 -0
- package/dist/core/hashes.d.ts +59 -0
- package/dist/core/hashes.d.ts.map +1 -0
- package/dist/core/hashes.js +128 -0
- package/dist/core/hashes.js.map +1 -0
- package/dist/core/jcs.d.ts +29 -0
- package/dist/core/jcs.d.ts.map +1 -0
- package/dist/core/jcs.js +371 -0
- package/dist/core/jcs.js.map +1 -0
- package/dist/core/registry-profile.d.ts +47 -0
- package/dist/core/registry-profile.d.ts.map +1 -0
- package/dist/core/registry-profile.js +119 -0
- package/dist/core/registry-profile.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/principal-entries.d.ts +109 -0
- package/dist/principal-entries.d.ts.map +1 -0
- package/dist/principal-entries.js +237 -0
- package/dist/principal-entries.js.map +1 -0
- package/dist/principal-intervention.d.ts +97 -0
- package/dist/principal-intervention.d.ts.map +1 -0
- package/dist/principal-intervention.js +139 -0
- package/dist/principal-intervention.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte and framing primitives of the core: the proof v2 length-prefix convention, hex, big-endian integers, SHA-256.
|
|
3
|
+
* Pure functions; the only platform dependency of the whole package is node:crypto (createHash here).
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
/** Every refusal that a pure helper raises is a CoreError with a stable machine-readable code (never prose). */
|
|
7
|
+
export class CoreError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
detail;
|
|
10
|
+
constructor(code, detail) {
|
|
11
|
+
super(detail === undefined ? code : code + ': ' + detail);
|
|
12
|
+
this.name = 'CoreError';
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.detail = detail;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export const HASH_RE = /^0x[0-9a-f]{64}$/;
|
|
18
|
+
export const ADDRESS_RE = /^0x[0-9a-f]{40}$/;
|
|
19
|
+
export const AMOUNT_RE = /^(0|[1-9][0-9]*)$/;
|
|
20
|
+
export const MAX_UINT256 = (1n << 256n) - 1n;
|
|
21
|
+
const HEX_RE = /^([0-9a-f]{2})*$/;
|
|
22
|
+
const ENCODER = new TextEncoder();
|
|
23
|
+
const STRICT_DECODER = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
24
|
+
export function hasLoneSurrogate(s) {
|
|
25
|
+
for (let i = 0; i < s.length; i++) {
|
|
26
|
+
const c = s.charCodeAt(i);
|
|
27
|
+
if (c >= 0xd800 && c <= 0xdbff) {
|
|
28
|
+
const n = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
|
|
29
|
+
if (n >= 0xdc00 && n <= 0xdfff)
|
|
30
|
+
i++;
|
|
31
|
+
else
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
else if (c >= 0xdc00 && c <= 0xdfff)
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
/** UTF-8 of a string; a lone surrogate is refused instead of being silently replaced. */
|
|
40
|
+
export function utf8(s) {
|
|
41
|
+
if (hasLoneSurrogate(s))
|
|
42
|
+
throw new CoreError('lone_surrogate');
|
|
43
|
+
return ENCODER.encode(s);
|
|
44
|
+
}
|
|
45
|
+
/** Number of bytes a string takes in UTF-8 (a lone surrogate counts as the three bytes of its replacement). */
|
|
46
|
+
export function utf8Length(s) {
|
|
47
|
+
let n = 0;
|
|
48
|
+
for (let i = 0; i < s.length; i++) {
|
|
49
|
+
const c = s.charCodeAt(i);
|
|
50
|
+
if (c < 0x80)
|
|
51
|
+
n += 1;
|
|
52
|
+
else if (c < 0x800)
|
|
53
|
+
n += 2;
|
|
54
|
+
else if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length && s.charCodeAt(i + 1) >= 0xdc00 && s.charCodeAt(i + 1) <= 0xdfff) {
|
|
55
|
+
n += 4;
|
|
56
|
+
i++;
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
n += 3;
|
|
60
|
+
}
|
|
61
|
+
return n;
|
|
62
|
+
}
|
|
63
|
+
/** Strict UTF-8 decode: invalid sequences are refused, a byte order mark is kept (and so refused by the JSON parser). */
|
|
64
|
+
export function utf8Decode(bytes) {
|
|
65
|
+
try {
|
|
66
|
+
return STRICT_DECODER.decode(bytes);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
throw new CoreError('not_utf8');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function toHex(bytes) {
|
|
73
|
+
let out = '';
|
|
74
|
+
for (let i = 0; i < bytes.length; i++)
|
|
75
|
+
out += (bytes[i] >> 4).toString(16) + (bytes[i] & 15).toString(16);
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
/** Lowercase hex only, even length; anything else is refused. */
|
|
79
|
+
export function fromHex(s) {
|
|
80
|
+
if (typeof s !== 'string' || !HEX_RE.test(s))
|
|
81
|
+
throw new CoreError('hex_invalid');
|
|
82
|
+
const out = new Uint8Array(s.length / 2);
|
|
83
|
+
for (let i = 0; i < out.length; i++)
|
|
84
|
+
out[i] = parseInt(s.slice(2 * i, 2 * i + 2), 16);
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
export function sha256(data) {
|
|
88
|
+
return new Uint8Array(createHash('sha256').update(data).digest());
|
|
89
|
+
}
|
|
90
|
+
export function concat(...parts) {
|
|
91
|
+
let n = 0;
|
|
92
|
+
for (const p of parts)
|
|
93
|
+
n += p.length;
|
|
94
|
+
const out = new Uint8Array(n);
|
|
95
|
+
let o = 0;
|
|
96
|
+
for (const p of parts) {
|
|
97
|
+
out.set(p, o);
|
|
98
|
+
o += p.length;
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
/** Big-endian unsigned 64-bit integer. */
|
|
103
|
+
export function be64(n) {
|
|
104
|
+
let v;
|
|
105
|
+
if (typeof n === 'bigint')
|
|
106
|
+
v = n;
|
|
107
|
+
else if (Number.isSafeInteger(n))
|
|
108
|
+
v = BigInt(n);
|
|
109
|
+
else
|
|
110
|
+
throw new CoreError('integer_invalid');
|
|
111
|
+
if (v < 0n || v > 0xffffffffffffffffn)
|
|
112
|
+
throw new CoreError('integer_invalid');
|
|
113
|
+
const out = new Uint8Array(8);
|
|
114
|
+
for (let i = 7; i >= 0; i--) {
|
|
115
|
+
out[i] = Number(v & 0xffn);
|
|
116
|
+
v >>= 8n;
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
/** lp(x): the four-byte big-endian length of x followed by x (proof v2 convention). */
|
|
121
|
+
export function lp(x) {
|
|
122
|
+
if (x.length > 0xffffffff)
|
|
123
|
+
throw new CoreError('field_too_long');
|
|
124
|
+
const head = new Uint8Array(4);
|
|
125
|
+
head[0] = (x.length >>> 24) & 255;
|
|
126
|
+
head[1] = (x.length >>> 16) & 255;
|
|
127
|
+
head[2] = (x.length >>> 8) & 255;
|
|
128
|
+
head[3] = x.length & 255;
|
|
129
|
+
return concat(head, x);
|
|
130
|
+
}
|
|
131
|
+
/** `0x` plus 64 lowercase hex digits to its 32 bytes. */
|
|
132
|
+
export function hash32(s, what) {
|
|
133
|
+
if (typeof s !== 'string' || !HASH_RE.test(s))
|
|
134
|
+
throw new CoreError('hash_invalid', what);
|
|
135
|
+
return fromHex(s.slice(2));
|
|
136
|
+
}
|
|
137
|
+
/** `0x` plus 40 lowercase hex digits to its 20 bytes. */
|
|
138
|
+
export function address20(s, what) {
|
|
139
|
+
if (typeof s !== 'string' || !ADDRESS_RE.test(s))
|
|
140
|
+
throw new CoreError('address_invalid', what);
|
|
141
|
+
return fromHex(s.slice(2));
|
|
142
|
+
}
|
|
143
|
+
/** Decimal unsigned integer text with no sign and no leading zero, at most 2^256-1. */
|
|
144
|
+
export function isAmount(s) {
|
|
145
|
+
return typeof s === 'string' && s.length <= 78 && AMOUNT_RE.test(s) && BigInt(s) <= MAX_UINT256;
|
|
146
|
+
}
|
|
147
|
+
export function isPlainObject(v) {
|
|
148
|
+
if (typeof v !== 'object' || v === null || Array.isArray(v))
|
|
149
|
+
return false;
|
|
150
|
+
const p = Object.getPrototypeOf(v);
|
|
151
|
+
return p === Object.prototype || p === null;
|
|
152
|
+
}
|
|
153
|
+
export function bytesEqual(a, b) {
|
|
154
|
+
if (a.length !== b.length)
|
|
155
|
+
return false;
|
|
156
|
+
let d = 0;
|
|
157
|
+
for (let i = 0; i < a.length; i++)
|
|
158
|
+
d |= a[i] ^ b[i];
|
|
159
|
+
return d === 0;
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../../src/core/bytes.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,U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AC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7B,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,QAAQ,CAAC,CAAU;IACjC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,IAAI,EAAE,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,IAAI,WAAW,CAAC;AAClG,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,CAAU;IACtC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IAC1E,MAAM,CAAC,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IACnC,OAAO,CAAC,KAAK,MAAM,CAAC,SAAS,IAAI,CAAC,KAAK,IAAI,CAAC;AAC9C,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,CAAa,EAAE,CAAa;IACrD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,CAAC;IACtD,OAAO,CAAC,KAAK,CAAC,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Grant profile (GRANT.md sections 2 and 3): the closed object certen.grant/v1, its strict validation and its
|
|
3
|
+
* canonical text. Every refusal is a named reason; nothing is partially evaluated.
|
|
4
|
+
*/
|
|
5
|
+
import { type Json } from './bytes.js';
|
|
6
|
+
export declare const GRANT_SCHEMA = "certen.grant/v1";
|
|
7
|
+
/** the closed action set (GR-84): three typed kinds and `raw`, the kind of a single call that no typed kind describes (GRANT.md section 6a); a decoder never answers raw */
|
|
8
|
+
export declare const ACTION_KINDS: readonly ["erc20.transfer", "erc20.approve", "native.transfer", "raw"];
|
|
9
|
+
export type ActionKind = (typeof ACTION_KINDS)[number];
|
|
10
|
+
export declare const SUBJECT_TYPES: readonly ["agent", "merchant", "client"];
|
|
11
|
+
export type SubjectType = (typeof SUBJECT_TYPES)[number];
|
|
12
|
+
export interface ChainAddress {
|
|
13
|
+
chainId: number;
|
|
14
|
+
address: string;
|
|
15
|
+
}
|
|
16
|
+
export interface AssetRef {
|
|
17
|
+
chainId: number;
|
|
18
|
+
token: string;
|
|
19
|
+
}
|
|
20
|
+
export interface RequiredBook {
|
|
21
|
+
book: string;
|
|
22
|
+
threshold: number;
|
|
23
|
+
}
|
|
24
|
+
export interface Tier {
|
|
25
|
+
upTo: string | null;
|
|
26
|
+
required: RequiredBook[];
|
|
27
|
+
}
|
|
28
|
+
export type Condition = {
|
|
29
|
+
all: Condition[];
|
|
30
|
+
} | {
|
|
31
|
+
any: Condition[];
|
|
32
|
+
} | {
|
|
33
|
+
not: Condition;
|
|
34
|
+
} | {
|
|
35
|
+
field: string;
|
|
36
|
+
op: string;
|
|
37
|
+
value: Json;
|
|
38
|
+
};
|
|
39
|
+
export interface Grant {
|
|
40
|
+
schema: 'certen.grant/v1';
|
|
41
|
+
grantId: string;
|
|
42
|
+
version: number;
|
|
43
|
+
priorHash: string | null;
|
|
44
|
+
issuedAt: number;
|
|
45
|
+
subject: {
|
|
46
|
+
type: SubjectType;
|
|
47
|
+
adi: string;
|
|
48
|
+
};
|
|
49
|
+
grantor: {
|
|
50
|
+
adi: string;
|
|
51
|
+
keyBook: string;
|
|
52
|
+
};
|
|
53
|
+
scope: {
|
|
54
|
+
actions: ActionKind[];
|
|
55
|
+
accounts: ChainAddress[];
|
|
56
|
+
beneficiaries: ChainAddress[];
|
|
57
|
+
assets: AssetRef[];
|
|
58
|
+
};
|
|
59
|
+
limits: Array<{
|
|
60
|
+
asset: AssetRef;
|
|
61
|
+
perTransaction: string;
|
|
62
|
+
cumulative: {
|
|
63
|
+
amount: string;
|
|
64
|
+
periodSeconds: number;
|
|
65
|
+
};
|
|
66
|
+
count: {
|
|
67
|
+
max: number;
|
|
68
|
+
periodSeconds: number;
|
|
69
|
+
};
|
|
70
|
+
}>;
|
|
71
|
+
validity: {
|
|
72
|
+
notBefore: number;
|
|
73
|
+
notAfter: number;
|
|
74
|
+
};
|
|
75
|
+
approvals: Array<{
|
|
76
|
+
asset: AssetRef;
|
|
77
|
+
tiers: Tier[];
|
|
78
|
+
}>;
|
|
79
|
+
conditions: Condition;
|
|
80
|
+
}
|
|
81
|
+
export type GrantValidation = {
|
|
82
|
+
ok: true;
|
|
83
|
+
grant: Grant;
|
|
84
|
+
} | {
|
|
85
|
+
ok: false;
|
|
86
|
+
reason: string;
|
|
87
|
+
detail?: string;
|
|
88
|
+
};
|
|
89
|
+
/** GR-46: the profile size limits are part of the specification. conditionDepth and conditionNodes are GR-21; inList is this library's bound on one in/nin list (inside the 64-node budget). */
|
|
90
|
+
export declare const LIMITS: {
|
|
91
|
+
textBytes: number;
|
|
92
|
+
scopeEntries: number;
|
|
93
|
+
tiers: number;
|
|
94
|
+
required: number;
|
|
95
|
+
conditionDepth: number;
|
|
96
|
+
conditionNodes: number;
|
|
97
|
+
inList: number;
|
|
98
|
+
};
|
|
99
|
+
/** acc:// URL: lowercase, non-empty segments of [a-z0-9._-], no query, no fragment, no trailing slash. */
|
|
100
|
+
export declare function isAccUrl(v: unknown): v is string;
|
|
101
|
+
/** an ADI URL has exactly one segment */
|
|
102
|
+
export declare function isAdiUrl(v: unknown): v is string;
|
|
103
|
+
export declare function hostOf(url: string): string;
|
|
104
|
+
/**
|
|
105
|
+
* True when `url` is the subject ADI, lies under it, or belongs to a sub-ADI of it (a sub-ADI host is
|
|
106
|
+
* `<name>.<subject host>`, a text prefix is NOT the relation: acc://agent-7.x.acme-extra is another ADI).
|
|
107
|
+
*/
|
|
108
|
+
export declare function isUnderAdi(url: string, adi: string): boolean;
|
|
109
|
+
export declare const CONDITION_FIELDS: string[];
|
|
110
|
+
/** Validates an already-parsed value as a Grant and returns a clean deep copy (later mutation of the input cannot affect it). */
|
|
111
|
+
export declare function validateGrant(value: unknown): GrantValidation;
|
|
112
|
+
export declare function canonicalGrant(grant: unknown): string;
|
|
113
|
+
export type ParseGrantResult = {
|
|
114
|
+
ok: true;
|
|
115
|
+
grant: Grant;
|
|
116
|
+
canonical: string;
|
|
117
|
+
grantHash: string;
|
|
118
|
+
} | {
|
|
119
|
+
ok: false;
|
|
120
|
+
reason: string;
|
|
121
|
+
detail?: string;
|
|
122
|
+
};
|
|
123
|
+
/** Strict parse of Grant text: duplicates, lone surrogates and non-integer numbers refuse; then the profile. */
|
|
124
|
+
export declare function parseGrantTextUnhashed(text: string): {
|
|
125
|
+
ok: true;
|
|
126
|
+
grant: Grant;
|
|
127
|
+
canonical: string;
|
|
128
|
+
} | {
|
|
129
|
+
ok: false;
|
|
130
|
+
reason: string;
|
|
131
|
+
detail?: string;
|
|
132
|
+
};
|
|
133
|
+
//# sourceMappingURL=grant-profile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grant-profile.d.ts","sourceRoot":"","sources":["../../src/core/grant-profile.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAoF,KAAK,IAAI,EAAE,MAAM,YAAY,CAAC;AAGzH,eAAO,MAAM,YAAY,oBAAoB,CAAC;AAC9C,4KAA4K;AAC5K,eAAO,MAAM,YAAY,wEAAyE,CAAC;AACnG,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AACvD,eAAO,MAAM,aAAa,0CAA2C,CAAC;AACtE,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,MAAM,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D;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAIrD;AAED,MAAM,MAAM,gBAAgB,GACxB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAChE;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnD,gHAAgH;AAChH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAYnJ"}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Grant profile (GRANT.md sections 2 and 3): the closed object certen.grant/v1, its strict validation and its
|
|
3
|
+
* canonical text. Every refusal is a named reason; nothing is partially evaluated.
|
|
4
|
+
*/
|
|
5
|
+
import { CoreError, ADDRESS_RE, HASH_RE, MAX_UINT256, isAmount, isPlainObject, utf8Length } from './bytes.js';
|
|
6
|
+
import { canonicalize, parseJson } from './jcs.js';
|
|
7
|
+
export const GRANT_SCHEMA = 'certen.grant/v1';
|
|
8
|
+
/** the closed action set (GR-84): three typed kinds and `raw`, the kind of a single call that no typed kind describes (GRANT.md section 6a); a decoder never answers raw */
|
|
9
|
+
export const ACTION_KINDS = ['erc20.transfer', 'erc20.approve', 'native.transfer', 'raw'];
|
|
10
|
+
export const SUBJECT_TYPES = ['agent', 'merchant', 'client'];
|
|
11
|
+
/** GR-46: the profile size limits are part of the specification. conditionDepth and conditionNodes are GR-21; inList is this library's bound on one in/nin list (inside the 64-node budget). */
|
|
12
|
+
export const LIMITS = { textBytes: 65536, scopeEntries: 64, tiers: 16, required: 16, conditionDepth: 8, conditionNodes: 64, inList: 64 };
|
|
13
|
+
const GRANT_ID_RE = /^[a-z0-9][a-z0-9._-]{0,62}$/;
|
|
14
|
+
const URL_SEGMENT_RE = /^[a-z0-9][a-z0-9._-]*$/;
|
|
15
|
+
class Refusal extends Error {
|
|
16
|
+
reason;
|
|
17
|
+
detail;
|
|
18
|
+
constructor(reason, detail) {
|
|
19
|
+
super(reason);
|
|
20
|
+
this.reason = reason;
|
|
21
|
+
this.detail = detail;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const fail = (reason, path) => {
|
|
25
|
+
throw new Refusal(reason, path);
|
|
26
|
+
};
|
|
27
|
+
/** acc:// URL: lowercase, non-empty segments of [a-z0-9._-], no query, no fragment, no trailing slash. */
|
|
28
|
+
export function isAccUrl(v) {
|
|
29
|
+
if (typeof v !== 'string' || v.length > 512 || !v.startsWith('acc://'))
|
|
30
|
+
return false;
|
|
31
|
+
const segs = v.slice(6).split('/');
|
|
32
|
+
return segs.length >= 1 && segs.every((s) => URL_SEGMENT_RE.test(s));
|
|
33
|
+
}
|
|
34
|
+
/** an ADI URL has exactly one segment */
|
|
35
|
+
export function isAdiUrl(v) {
|
|
36
|
+
return isAccUrl(v) && !v.slice(6).includes('/');
|
|
37
|
+
}
|
|
38
|
+
export function hostOf(url) {
|
|
39
|
+
return url.slice(6).split('/')[0];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* True when `url` is the subject ADI, lies under it, or belongs to a sub-ADI of it (a sub-ADI host is
|
|
43
|
+
* `<name>.<subject host>`, a text prefix is NOT the relation: acc://agent-7.x.acme-extra is another ADI).
|
|
44
|
+
*/
|
|
45
|
+
export function isUnderAdi(url, adi) {
|
|
46
|
+
const h = hostOf(url);
|
|
47
|
+
const s = hostOf(adi);
|
|
48
|
+
return h === s || h.endsWith('.' + s);
|
|
49
|
+
}
|
|
50
|
+
function obj(v, path, keys) {
|
|
51
|
+
if (!isPlainObject(v))
|
|
52
|
+
return fail('grant_field_invalid', path);
|
|
53
|
+
const have = Object.keys(v);
|
|
54
|
+
for (const k of have)
|
|
55
|
+
if (!keys.includes(k))
|
|
56
|
+
fail('grant_unknown_field', path + '.' + k);
|
|
57
|
+
for (const k of keys)
|
|
58
|
+
if (!have.includes(k))
|
|
59
|
+
fail('grant_missing_field', path + '.' + k);
|
|
60
|
+
return v;
|
|
61
|
+
}
|
|
62
|
+
function arr(v, path, max, min = 0) {
|
|
63
|
+
if (!Array.isArray(v) || v.length < min)
|
|
64
|
+
return fail('grant_field_invalid', path);
|
|
65
|
+
if (v.length > max)
|
|
66
|
+
return fail('grant_too_large', path);
|
|
67
|
+
return v;
|
|
68
|
+
}
|
|
69
|
+
function int(v, path, min) {
|
|
70
|
+
if (typeof v !== 'number' || !Number.isSafeInteger(v) || v < min || Object.is(v, -0))
|
|
71
|
+
return fail('grant_field_invalid', path);
|
|
72
|
+
return v;
|
|
73
|
+
}
|
|
74
|
+
function str(v, path, re) {
|
|
75
|
+
if (typeof v !== 'string' || !re.test(v))
|
|
76
|
+
return fail('grant_field_invalid', path);
|
|
77
|
+
return v;
|
|
78
|
+
}
|
|
79
|
+
function amount(v, path) {
|
|
80
|
+
if (!isAmount(v))
|
|
81
|
+
return fail('grant_field_invalid', path);
|
|
82
|
+
return v;
|
|
83
|
+
}
|
|
84
|
+
function adi(v, path) {
|
|
85
|
+
if (!isAdiUrl(v))
|
|
86
|
+
return fail('grant_field_invalid', path);
|
|
87
|
+
return v;
|
|
88
|
+
}
|
|
89
|
+
function bookUrl(v, path) {
|
|
90
|
+
if (!isAccUrl(v) || !v.slice(6).includes('/'))
|
|
91
|
+
return fail('grant_field_invalid', path);
|
|
92
|
+
return v;
|
|
93
|
+
}
|
|
94
|
+
function chainAddress(v, path) {
|
|
95
|
+
const o = obj(v, path, ['chainId', 'address']);
|
|
96
|
+
return { chainId: int(o['chainId'], path + '.chainId', 1), address: str(o['address'], path + '.address', ADDRESS_RE) };
|
|
97
|
+
}
|
|
98
|
+
function assetRef(v, path) {
|
|
99
|
+
const o = obj(v, path, ['chainId', 'token']);
|
|
100
|
+
const token = o['token'];
|
|
101
|
+
if (token !== 'native')
|
|
102
|
+
str(token, path + '.token', ADDRESS_RE);
|
|
103
|
+
return { chainId: int(o['chainId'], path + '.chainId', 1), token: token };
|
|
104
|
+
}
|
|
105
|
+
const assetKey = (a) => a.chainId + ':' + a.token;
|
|
106
|
+
const FIELD_KINDS = {
|
|
107
|
+
'action.kind': 'kind',
|
|
108
|
+
'action.chainId': 'int',
|
|
109
|
+
'action.amount': 'amount',
|
|
110
|
+
'action.beneficiary': 'address',
|
|
111
|
+
'action.source': 'address',
|
|
112
|
+
'action.asset': 'asset',
|
|
113
|
+
'time.hourUtc': 'hour',
|
|
114
|
+
'time.weekdayUtc': 'weekday',
|
|
115
|
+
};
|
|
116
|
+
const OPS_ALL = ['eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin'];
|
|
117
|
+
const OPS_EQ = ['eq', 'ne', 'in', 'nin'];
|
|
118
|
+
export const CONDITION_FIELDS = Object.keys(FIELD_KINDS);
|
|
119
|
+
const OPS_FOR = { kind: OPS_EQ, int: OPS_ALL, amount: OPS_ALL, address: OPS_EQ, asset: OPS_EQ, hour: OPS_ALL, weekday: OPS_ALL };
|
|
120
|
+
function conditionScalar(kind, v) {
|
|
121
|
+
switch (kind) {
|
|
122
|
+
case 'kind': return typeof v === 'string' && ACTION_KINDS.includes(v);
|
|
123
|
+
case 'int': return typeof v === 'number' && Number.isSafeInteger(v) && v >= 1;
|
|
124
|
+
case 'amount': return isAmount(v);
|
|
125
|
+
case 'address': return typeof v === 'string' && ADDRESS_RE.test(v);
|
|
126
|
+
case 'asset': return v === 'native' || (typeof v === 'string' && ADDRESS_RE.test(v));
|
|
127
|
+
case 'hour': return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 && v <= 23;
|
|
128
|
+
case 'weekday': return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 && v <= 6;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function validateCondition(node, depth, count, path) {
|
|
132
|
+
if (depth > LIMITS.conditionDepth)
|
|
133
|
+
return fail('condition_too_large', path);
|
|
134
|
+
count.n++;
|
|
135
|
+
if (count.n > LIMITS.conditionNodes)
|
|
136
|
+
return fail('condition_too_large', path);
|
|
137
|
+
if (!isPlainObject(node))
|
|
138
|
+
return fail('grant_conditions_invalid', path);
|
|
139
|
+
const keys = Object.keys(node);
|
|
140
|
+
const comb = keys.filter((k) => k === 'all' || k === 'any' || k === 'not');
|
|
141
|
+
if (comb.length > 0) {
|
|
142
|
+
if (keys.length !== 1)
|
|
143
|
+
return fail('grant_conditions_invalid', path);
|
|
144
|
+
const k = comb[0];
|
|
145
|
+
const child = node[k];
|
|
146
|
+
if (k === 'not')
|
|
147
|
+
return { not: validateCondition(child, depth + 1, count, path + '.not') };
|
|
148
|
+
if (!Array.isArray(child) || (k === 'any' && child.length === 0))
|
|
149
|
+
return fail('grant_conditions_invalid', path + '.' + k);
|
|
150
|
+
const kids = child.map((c, i) => validateCondition(c, depth + 1, count, path + '.' + k + '[' + i + ']'));
|
|
151
|
+
return k === 'all' ? { all: kids } : { any: kids };
|
|
152
|
+
}
|
|
153
|
+
if (keys.length !== 3 || !keys.includes('field') || !keys.includes('op') || !keys.includes('value'))
|
|
154
|
+
return fail('grant_conditions_invalid', path);
|
|
155
|
+
const field = node['field'];
|
|
156
|
+
if (typeof field !== 'string' || !(field in FIELD_KINDS) || !Object.prototype.hasOwnProperty.call(FIELD_KINDS, field))
|
|
157
|
+
return fail('condition_unknown_field', path + '.field');
|
|
158
|
+
const kind = FIELD_KINDS[field];
|
|
159
|
+
const op = node['op'];
|
|
160
|
+
if (typeof op !== 'string' || !OPS_ALL.includes(op) || !OPS_FOR[kind].includes(op))
|
|
161
|
+
return fail('condition_unknown_operator', path + '.op');
|
|
162
|
+
const value = node['value'];
|
|
163
|
+
if (op === 'in' || op === 'nin') {
|
|
164
|
+
if (!Array.isArray(value) || value.length === 0 || value.length > LIMITS.inList || !value.every((x) => conditionScalar(kind, x)))
|
|
165
|
+
return fail('condition_value_invalid', path + '.value');
|
|
166
|
+
if (new Set(value.map((x) => String(x))).size !== value.length)
|
|
167
|
+
return fail('condition_value_invalid', path + '.value');
|
|
168
|
+
}
|
|
169
|
+
else if (!conditionScalar(kind, value))
|
|
170
|
+
return fail('condition_value_invalid', path + '.value');
|
|
171
|
+
return { field, op, value: value };
|
|
172
|
+
}
|
|
173
|
+
// ---- the Grant -----------------------------------------------------------------------------------------------
|
|
174
|
+
function validate(v) {
|
|
175
|
+
const g = obj(v, '$', ['schema', 'grantId', 'version', 'priorHash', 'issuedAt', 'subject', 'grantor', 'scope', 'limits', 'validity', 'approvals', 'conditions']);
|
|
176
|
+
if (g['schema'] !== GRANT_SCHEMA)
|
|
177
|
+
fail('grant_field_invalid', '$.schema');
|
|
178
|
+
str(g['grantId'], '$.grantId', GRANT_ID_RE);
|
|
179
|
+
const version = int(g['version'], '$.version', 1);
|
|
180
|
+
const prior = g['priorHash'];
|
|
181
|
+
if (prior !== null)
|
|
182
|
+
str(prior, '$.priorHash', HASH_RE);
|
|
183
|
+
int(g['issuedAt'], '$.issuedAt', 0);
|
|
184
|
+
const subject = obj(g['subject'], '$.subject', ['type', 'adi']);
|
|
185
|
+
if (typeof subject['type'] !== 'string' || !SUBJECT_TYPES.includes(subject['type']))
|
|
186
|
+
fail('grant_field_invalid', '$.subject.type');
|
|
187
|
+
const subjectAdi = adi(subject['adi'], '$.subject.adi');
|
|
188
|
+
const grantor = obj(g['grantor'], '$.grantor', ['adi', 'keyBook']);
|
|
189
|
+
const grantorAdi = adi(grantor['adi'], '$.grantor.adi');
|
|
190
|
+
const keyBook = bookUrl(grantor['keyBook'], '$.grantor.keyBook');
|
|
191
|
+
if (!keyBook.startsWith(grantorAdi + '/'))
|
|
192
|
+
fail('grant_field_invalid', '$.grantor.keyBook');
|
|
193
|
+
if (isUnderAdi(grantorAdi, subjectAdi))
|
|
194
|
+
fail('grantor_is_subject', '$.grantor.adi');
|
|
195
|
+
if ((version === 1) !== (prior === null))
|
|
196
|
+
fail('grant_lineage_invalid', '$.priorHash');
|
|
197
|
+
const scope = obj(g['scope'], '$.scope', ['actions', 'accounts', 'beneficiaries', 'assets']);
|
|
198
|
+
const actions = arr(scope['actions'], '$.scope.actions', ACTION_KINDS.length);
|
|
199
|
+
for (const a of actions)
|
|
200
|
+
if (typeof a !== 'string' || !ACTION_KINDS.includes(a))
|
|
201
|
+
fail('grant_field_invalid', '$.scope.actions');
|
|
202
|
+
// GR-84: each kind at most once
|
|
203
|
+
if (new Set(actions).size !== actions.length)
|
|
204
|
+
fail('grant_field_invalid', '$.scope.actions');
|
|
205
|
+
arr(scope['accounts'], '$.scope.accounts', LIMITS.scopeEntries).forEach((a, i) => chainAddress(a, '$.scope.accounts[' + i + ']'));
|
|
206
|
+
arr(scope['beneficiaries'], '$.scope.beneficiaries', LIMITS.scopeEntries).forEach((a, i) => chainAddress(a, '$.scope.beneficiaries[' + i + ']'));
|
|
207
|
+
const assets = arr(scope['assets'], '$.scope.assets', LIMITS.scopeEntries, 1).map((a, i) => assetRef(a, '$.scope.assets[' + i + ']'));
|
|
208
|
+
const assetKeys = assets.map(assetKey);
|
|
209
|
+
if (new Set(assetKeys).size !== assetKeys.length)
|
|
210
|
+
fail('grant_field_invalid', '$.scope.assets');
|
|
211
|
+
const limits = arr(g['limits'], '$.limits', LIMITS.scopeEntries);
|
|
212
|
+
const limitKeys = [];
|
|
213
|
+
limits.forEach((l, i) => {
|
|
214
|
+
const p = '$.limits[' + i + ']';
|
|
215
|
+
const o = obj(l, p, ['asset', 'perTransaction', 'cumulative', 'count']);
|
|
216
|
+
limitKeys.push(assetKey(assetRef(o['asset'], p + '.asset')));
|
|
217
|
+
const per = BigInt(amount(o['perTransaction'], p + '.perTransaction'));
|
|
218
|
+
const cum = obj(o['cumulative'], p + '.cumulative', ['amount', 'periodSeconds']);
|
|
219
|
+
const cumAmount = BigInt(amount(cum['amount'], p + '.cumulative.amount'));
|
|
220
|
+
int(cum['periodSeconds'], p + '.cumulative.periodSeconds', 60);
|
|
221
|
+
const cnt = obj(o['count'], p + '.count', ['max', 'periodSeconds']);
|
|
222
|
+
int(cnt['max'], p + '.count.max', 1);
|
|
223
|
+
int(cnt['periodSeconds'], p + '.count.periodSeconds', 60);
|
|
224
|
+
if (cumAmount < per || cumAmount > MAX_UINT256)
|
|
225
|
+
fail('grant_limits_inconsistent', p);
|
|
226
|
+
});
|
|
227
|
+
if (limitKeys.length !== assetKeys.length || new Set(limitKeys).size !== limitKeys.length || !assetKeys.every((k) => limitKeys.includes(k)))
|
|
228
|
+
fail('grant_limits_coverage', '$.limits');
|
|
229
|
+
const validity = obj(g['validity'], '$.validity', ['notBefore', 'notAfter']);
|
|
230
|
+
const notBefore = int(validity['notBefore'], '$.validity.notBefore', 0);
|
|
231
|
+
const notAfter = int(validity['notAfter'], '$.validity.notAfter', 0);
|
|
232
|
+
if (notAfter < notBefore || (version === 1 && notAfter === notBefore))
|
|
233
|
+
fail('grant_validity_invalid', '$.validity');
|
|
234
|
+
const approvals = arr(g['approvals'], '$.approvals', LIMITS.scopeEntries);
|
|
235
|
+
const approvalKeys = [];
|
|
236
|
+
approvals.forEach((a, i) => {
|
|
237
|
+
const p = '$.approvals[' + i + ']';
|
|
238
|
+
const o = obj(a, p, ['asset', 'tiers']);
|
|
239
|
+
approvalKeys.push(assetKey(assetRef(o['asset'], p + '.asset')));
|
|
240
|
+
const tiers = arr(o['tiers'], p + '.tiers', LIMITS.tiers);
|
|
241
|
+
if (tiers.length === 0)
|
|
242
|
+
fail('grant_tiers_invalid', p + '.tiers');
|
|
243
|
+
let prev = null;
|
|
244
|
+
tiers.forEach((t, j) => {
|
|
245
|
+
const tp = p + '.tiers[' + j + ']';
|
|
246
|
+
const to = obj(t, tp, ['upTo', 'required']);
|
|
247
|
+
const last = j === tiers.length - 1;
|
|
248
|
+
if (last) {
|
|
249
|
+
if (to['upTo'] !== null)
|
|
250
|
+
fail('grant_tiers_invalid', tp + '.upTo');
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
if (to['upTo'] === null)
|
|
254
|
+
fail('grant_tiers_invalid', tp + '.upTo');
|
|
255
|
+
const up = BigInt(amount(to['upTo'], tp + '.upTo'));
|
|
256
|
+
if (prev !== null && up <= prev)
|
|
257
|
+
fail('grant_tiers_invalid', tp + '.upTo');
|
|
258
|
+
prev = up;
|
|
259
|
+
}
|
|
260
|
+
const req = arr(to['required'], tp + '.required', LIMITS.required);
|
|
261
|
+
if (req.length === 0)
|
|
262
|
+
fail('grant_tiers_invalid', tp + '.required');
|
|
263
|
+
const books = new Set();
|
|
264
|
+
req.forEach((r, k) => {
|
|
265
|
+
const rp = tp + '.required[' + k + ']';
|
|
266
|
+
const ro = obj(r, rp, ['book', 'threshold']);
|
|
267
|
+
const bookValue = ro['book'];
|
|
268
|
+
if (isAccUrl(bookValue) && isUnderAdi(bookValue, subjectAdi))
|
|
269
|
+
fail('grant_approver_is_subject', rp + '.book');
|
|
270
|
+
const book = bookUrl(bookValue, rp + '.book');
|
|
271
|
+
int(ro['threshold'], rp + '.threshold', 1);
|
|
272
|
+
if (books.has(book))
|
|
273
|
+
fail('grant_tiers_invalid', rp + '.book');
|
|
274
|
+
books.add(book);
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
if (approvalKeys.length !== assetKeys.length || new Set(approvalKeys).size !== approvalKeys.length || !assetKeys.every((k) => approvalKeys.includes(k)))
|
|
279
|
+
fail('grant_approvals_coverage', '$.approvals');
|
|
280
|
+
validateCondition(g['conditions'], 1, { n: 0 }, '$.conditions');
|
|
281
|
+
}
|
|
282
|
+
/** Validates an already-parsed value as a Grant and returns a clean deep copy (later mutation of the input cannot affect it). */
|
|
283
|
+
export function validateGrant(value) {
|
|
284
|
+
try {
|
|
285
|
+
validate(value);
|
|
286
|
+
const canonicalText = canonicalize(value);
|
|
287
|
+
if (utf8Length(canonicalText) > LIMITS.textBytes)
|
|
288
|
+
fail('grant_too_large', '$');
|
|
289
|
+
return { ok: true, grant: parseJson(canonicalText, { integersOnly: true }) };
|
|
290
|
+
}
|
|
291
|
+
catch (e) {
|
|
292
|
+
if (e instanceof Refusal)
|
|
293
|
+
return e.detail === undefined ? { ok: false, reason: e.reason } : { ok: false, reason: e.reason, detail: e.detail };
|
|
294
|
+
if (e instanceof CoreError)
|
|
295
|
+
return { ok: false, reason: 'grant_field_invalid', detail: e.code };
|
|
296
|
+
throw e;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
export function canonicalGrant(grant) {
|
|
300
|
+
const v = validateGrant(grant);
|
|
301
|
+
if (!v.ok)
|
|
302
|
+
throw new CoreError(v.reason, v.detail);
|
|
303
|
+
return canonicalize(v.grant);
|
|
304
|
+
}
|
|
305
|
+
/** Strict parse of Grant text: duplicates, lone surrogates and non-integer numbers refuse; then the profile. */
|
|
306
|
+
export function parseGrantTextUnhashed(text) {
|
|
307
|
+
if (utf8Length(text) > LIMITS.textBytes)
|
|
308
|
+
return { ok: false, reason: 'grant_too_large' };
|
|
309
|
+
let value;
|
|
310
|
+
try {
|
|
311
|
+
value = parseJson(text, { integersOnly: true });
|
|
312
|
+
}
|
|
313
|
+
catch (e) {
|
|
314
|
+
if (e instanceof CoreError)
|
|
315
|
+
return { ok: false, reason: 'grant_' + e.code };
|
|
316
|
+
throw e;
|
|
317
|
+
}
|
|
318
|
+
const v = validateGrant(value);
|
|
319
|
+
if (!v.ok)
|
|
320
|
+
return v;
|
|
321
|
+
return { ok: true, grant: v.grant, canonical: canonicalize(v.grant) };
|
|
322
|
+
}
|
|
323
|
+
//# sourceMappingURL=grant-profile.js.map
|