@intentlayer/sdk 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.
@@ -0,0 +1,205 @@
1
+ // src/policyBootstrapBuilder.ts
2
+ import { keccak256, encodePacked } from "viem";
3
+ var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
4
+ var ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
5
+ var GLOBAL_FLAG_NO_BRIDGE = BigInt(1);
6
+ var GLOBAL_FLAG_POST_CONDITIONS = BigInt(2);
7
+ var GLOBAL_FLAG_FX_SAFETY = BigInt(4);
8
+ function generatePolicyId(wallet, prefix) {
9
+ const timestamp = BigInt(Math.floor(Date.now() / 1e3));
10
+ if (prefix) {
11
+ return keccak256(
12
+ encodePacked(
13
+ ["string", "address", "uint256"],
14
+ [prefix, wallet, timestamp]
15
+ )
16
+ );
17
+ }
18
+ return keccak256(
19
+ encodePacked(
20
+ ["address", "uint256"],
21
+ [wallet, timestamp]
22
+ )
23
+ );
24
+ }
25
+ function buildMinimalPolicyBootstrapIntent(params) {
26
+ const now = params.currentTimestamp || BigInt(Math.floor(Date.now() / 1e3));
27
+ const validFor = params.validFor || 3600;
28
+ const validUntil = now + BigInt(validFor);
29
+ const nowSeconds = Number(now);
30
+ return {
31
+ // Intent metadata
32
+ wallet: params.wallet,
33
+ nonce: params.nonce || BigInt(0),
34
+ validUntil,
35
+ chainId: params.chainId,
36
+ // Core policy fields
37
+ policyId: params.policyId || generatePolicyId(params.wallet),
38
+ root: params.root,
39
+ expiresAt: params.expiresAt || nowSeconds + 30 * 24 * 3600,
40
+ // 30 days default
41
+ dailyCap: params.dailyCap || BigInt(0),
42
+ // No limit by default
43
+ delegate: params.delegate,
44
+ globalFlags: params.globalFlags || BigInt(0),
45
+ // All registries disabled
46
+ venueRegistry: ZERO_ADDRESS,
47
+ venueProfileId: ZERO_BYTES32,
48
+ venueVersion: 0,
49
+ requireVenue: false,
50
+ vendorRegistry: ZERO_ADDRESS,
51
+ vendorProfileId: ZERO_BYTES32,
52
+ vendorVersion: 0,
53
+ vendorRequired: false,
54
+ oracleRegistry: ZERO_ADDRESS,
55
+ oracleProfileId: ZERO_BYTES32,
56
+ oracleVersion: 0,
57
+ approvalRegistry: ZERO_ADDRESS,
58
+ approvalProfileId: ZERO_BYTES32,
59
+ approvalVersion: 0
60
+ };
61
+ }
62
+ function buildPolicyBootstrapIntent(params) {
63
+ const now = params.currentTimestamp || BigInt(Math.floor(Date.now() / 1e3));
64
+ const validFor = params.validFor || 3600;
65
+ const validUntil = now + BigInt(validFor);
66
+ const nowSeconds = Number(now);
67
+ return {
68
+ // Intent metadata
69
+ wallet: params.wallet,
70
+ nonce: params.nonce || BigInt(0),
71
+ validUntil,
72
+ chainId: params.chainId,
73
+ // Core policy fields
74
+ policyId: params.policyId || generatePolicyId(params.wallet),
75
+ root: params.root,
76
+ expiresAt: params.expiresAt || nowSeconds + 30 * 24 * 3600,
77
+ // 30 days default
78
+ dailyCap: params.dailyCap || BigInt(0),
79
+ // No limit by default
80
+ delegate: params.delegate,
81
+ globalFlags: params.globalFlags || BigInt(0),
82
+ // Venue header (optional)
83
+ venueRegistry: params.venueRegistry || ZERO_ADDRESS,
84
+ venueProfileId: params.venueProfileId || ZERO_BYTES32,
85
+ venueVersion: params.venueVersion || 0,
86
+ requireVenue: params.requireVenue || false,
87
+ // Vendor header (optional)
88
+ vendorRegistry: params.vendorRegistry || ZERO_ADDRESS,
89
+ vendorProfileId: params.vendorProfileId || ZERO_BYTES32,
90
+ vendorVersion: params.vendorVersion || 0,
91
+ vendorRequired: params.vendorRequired || false,
92
+ // Oracle header (optional)
93
+ oracleRegistry: params.oracleRegistry || ZERO_ADDRESS,
94
+ oracleProfileId: params.oracleProfileId || ZERO_BYTES32,
95
+ oracleVersion: params.oracleVersion || 0,
96
+ // Approval header (optional)
97
+ approvalRegistry: params.approvalRegistry || ZERO_ADDRESS,
98
+ approvalProfileId: params.approvalProfileId || ZERO_BYTES32,
99
+ approvalVersion: params.approvalVersion || 0
100
+ };
101
+ }
102
+ function validatePolicyBootstrapIntent(intent) {
103
+ const now = BigInt(Math.floor(Date.now() / 1e3));
104
+ if (intent.validUntil <= now) {
105
+ throw new Error("PolicyBootstrapIntent has expired");
106
+ }
107
+ if (intent.delegate === ZERO_ADDRESS) {
108
+ throw new Error("Delegate cannot be zero address");
109
+ }
110
+ if (intent.venueRegistry !== ZERO_ADDRESS) {
111
+ if (intent.venueProfileId === ZERO_BYTES32) {
112
+ throw new Error("Venue registry set but venueProfileId is zero");
113
+ }
114
+ if (intent.venueVersion === 0) {
115
+ throw new Error("Venue registry set but venueVersion is zero");
116
+ }
117
+ }
118
+ if (intent.vendorRegistry !== ZERO_ADDRESS) {
119
+ if (intent.vendorProfileId === ZERO_BYTES32) {
120
+ throw new Error("Vendor registry set but vendorProfileId is zero");
121
+ }
122
+ if (intent.vendorVersion === 0) {
123
+ throw new Error("Vendor registry set but vendorVersion is zero");
124
+ }
125
+ }
126
+ if (intent.oracleRegistry !== ZERO_ADDRESS) {
127
+ if (intent.oracleProfileId === ZERO_BYTES32) {
128
+ throw new Error("Oracle registry set but oracleProfileId is zero");
129
+ }
130
+ if (intent.oracleVersion === 0) {
131
+ throw new Error("Oracle registry set but oracleVersion is zero");
132
+ }
133
+ }
134
+ if (intent.approvalRegistry !== ZERO_ADDRESS) {
135
+ if (intent.approvalProfileId === ZERO_BYTES32) {
136
+ throw new Error("Approval registry set but approvalProfileId is zero");
137
+ }
138
+ if (intent.approvalVersion === 0) {
139
+ throw new Error("Approval registry set but approvalVersion is zero");
140
+ }
141
+ }
142
+ }
143
+ function hasRegistries(intent) {
144
+ return intent.venueRegistry !== ZERO_ADDRESS || intent.vendorRegistry !== ZERO_ADDRESS || intent.oracleRegistry !== ZERO_ADDRESS || intent.approvalRegistry !== ZERO_ADDRESS;
145
+ }
146
+ function summarizePolicyFeatures(intent) {
147
+ const flags = [];
148
+ if ((intent.globalFlags & GLOBAL_FLAG_NO_BRIDGE) !== BigInt(0)) {
149
+ flags.push("NO_BRIDGE");
150
+ }
151
+ if ((intent.globalFlags & GLOBAL_FLAG_POST_CONDITIONS) !== BigInt(0)) {
152
+ flags.push("POST_CONDITIONS");
153
+ }
154
+ if ((intent.globalFlags & GLOBAL_FLAG_FX_SAFETY) !== BigInt(0)) {
155
+ flags.push("FX_SAFETY");
156
+ }
157
+ return {
158
+ venue: intent.venueRegistry !== ZERO_ADDRESS,
159
+ vendor: intent.vendorRegistry !== ZERO_ADDRESS,
160
+ oracle: intent.oracleRegistry !== ZERO_ADDRESS,
161
+ approval: intent.approvalRegistry !== ZERO_ADDRESS,
162
+ flags
163
+ };
164
+ }
165
+ function serializePolicyIntent(intent) {
166
+ return {
167
+ wallet: intent.wallet,
168
+ nonce: intent.nonce.toString(),
169
+ validUntil: intent.validUntil.toString(),
170
+ chainId: intent.chainId.toString(),
171
+ policyId: intent.policyId,
172
+ root: intent.root,
173
+ expiresAt: intent.expiresAt,
174
+ dailyCap: intent.dailyCap.toString(),
175
+ delegate: intent.delegate,
176
+ globalFlags: intent.globalFlags.toString(),
177
+ venueRegistry: intent.venueRegistry,
178
+ venueProfileId: intent.venueProfileId,
179
+ venueVersion: intent.venueVersion,
180
+ requireVenue: intent.requireVenue,
181
+ vendorRegistry: intent.vendorRegistry,
182
+ vendorProfileId: intent.vendorProfileId,
183
+ vendorVersion: intent.vendorVersion,
184
+ vendorRequired: intent.vendorRequired,
185
+ oracleRegistry: intent.oracleRegistry,
186
+ oracleProfileId: intent.oracleProfileId,
187
+ oracleVersion: intent.oracleVersion,
188
+ approvalRegistry: intent.approvalRegistry,
189
+ approvalProfileId: intent.approvalProfileId,
190
+ approvalVersion: intent.approvalVersion
191
+ };
192
+ }
193
+
194
+ export {
195
+ GLOBAL_FLAG_NO_BRIDGE,
196
+ GLOBAL_FLAG_POST_CONDITIONS,
197
+ GLOBAL_FLAG_FX_SAFETY,
198
+ generatePolicyId,
199
+ buildMinimalPolicyBootstrapIntent,
200
+ buildPolicyBootstrapIntent,
201
+ validatePolicyBootstrapIntent,
202
+ hasRegistries,
203
+ summarizePolicyFeatures,
204
+ serializePolicyIntent
205
+ };