@hazbase/simplicity 0.0.3 → 0.0.5
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/README.md +229 -0
- package/dist/cli.js +388 -1
- package/dist/client/ContractFactory.d.ts +2 -1
- package/dist/client/ContractFactory.js +3 -0
- package/dist/client/DeployedContract.d.ts +15 -1
- package/dist/client/DeployedContract.js +22 -0
- package/dist/client/SimplicityClient.d.ts +304 -1
- package/dist/client/SimplicityClient.js +49 -0
- package/dist/core/artifact.js +8 -0
- package/dist/core/compiler.js +60 -5
- package/dist/core/executor.js +24 -0
- package/dist/core/state.d.ts +18 -0
- package/dist/core/state.js +278 -0
- package/dist/core/types.d.ts +106 -0
- package/dist/domain/bond.d.ts +2051 -0
- package/dist/domain/bond.js +1042 -0
- package/dist/domain/bondSettlementValidation.d.ts +18 -0
- package/dist/domain/bondSettlementValidation.js +122 -0
- package/dist/domain/bondValidation.d.ts +29 -0
- package/dist/domain/bondValidation.js +303 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +41 -1
- package/package.json +1 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.loadStateInput = loadStateInput;
|
|
7
|
+
exports.detectOnChainStateAnchor = detectOnChainStateAnchor;
|
|
8
|
+
exports.buildArtifactStateMetadata = buildArtifactStateMetadata;
|
|
9
|
+
exports.verifyStateDescriptorAgainstArtifact = verifyStateDescriptorAgainstArtifact;
|
|
10
|
+
exports.verifyStateAgainstArtifact = verifyStateAgainstArtifact;
|
|
11
|
+
const promises_1 = require("node:fs/promises");
|
|
12
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
13
|
+
const errors_1 = require("./errors");
|
|
14
|
+
const summary_1 = require("./summary");
|
|
15
|
+
const DEFAULT_SCHEMA_VERSION = "1";
|
|
16
|
+
const DEFAULT_ANCHOR_MODE = "artifact-hash-anchor";
|
|
17
|
+
const ZERO_HASH_256 = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
18
|
+
function stripComments(source) {
|
|
19
|
+
return source
|
|
20
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
21
|
+
.replace(/(^|[^:])\/\/.*$/gm, "$1");
|
|
22
|
+
}
|
|
23
|
+
function extractFunctionBody(source, functionName) {
|
|
24
|
+
const marker = `fn ${functionName}()`;
|
|
25
|
+
const start = source.indexOf(marker);
|
|
26
|
+
if (start === -1)
|
|
27
|
+
return null;
|
|
28
|
+
const braceStart = source.indexOf("{", start);
|
|
29
|
+
if (braceStart === -1)
|
|
30
|
+
return null;
|
|
31
|
+
let depth = 0;
|
|
32
|
+
for (let i = braceStart; i < source.length; i += 1) {
|
|
33
|
+
const char = source[i];
|
|
34
|
+
if (char === "{")
|
|
35
|
+
depth += 1;
|
|
36
|
+
if (char === "}") {
|
|
37
|
+
depth -= 1;
|
|
38
|
+
if (depth === 0) {
|
|
39
|
+
return source.slice(braceStart + 1, i);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
function assertNonEmpty(value, fieldName) {
|
|
46
|
+
if (!value || value.trim().length === 0) {
|
|
47
|
+
throw new errors_1.DefinitionError(`${fieldName} must not be empty`);
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
function ensureSerializable(value, seen = new WeakSet()) {
|
|
52
|
+
if (value === undefined) {
|
|
53
|
+
throw new errors_1.DefinitionError("State JSON must not contain undefined values");
|
|
54
|
+
}
|
|
55
|
+
if (value === null)
|
|
56
|
+
return;
|
|
57
|
+
if (typeof value === "bigint") {
|
|
58
|
+
throw new errors_1.DefinitionError("State JSON must not contain bigint values");
|
|
59
|
+
}
|
|
60
|
+
if (value instanceof Date) {
|
|
61
|
+
throw new errors_1.DefinitionError("State JSON must not contain Date objects; normalize them first");
|
|
62
|
+
}
|
|
63
|
+
if (Array.isArray(value)) {
|
|
64
|
+
for (const entry of value)
|
|
65
|
+
ensureSerializable(entry, seen);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (typeof value === "object") {
|
|
69
|
+
const objectValue = value;
|
|
70
|
+
if (seen.has(objectValue)) {
|
|
71
|
+
throw new errors_1.DefinitionError("State JSON must not contain circular references");
|
|
72
|
+
}
|
|
73
|
+
seen.add(objectValue);
|
|
74
|
+
for (const entry of Object.values(objectValue))
|
|
75
|
+
ensureSerializable(entry, seen);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function resolveStateValue(input) {
|
|
79
|
+
if ((input.jsonPath ? 1 : 0) + (input.value !== undefined ? 1 : 0) !== 1) {
|
|
80
|
+
throw new errors_1.DefinitionError("Exactly one of jsonPath or value must be provided");
|
|
81
|
+
}
|
|
82
|
+
if (input.jsonPath) {
|
|
83
|
+
const sourcePath = node_path_1.default.resolve(input.jsonPath);
|
|
84
|
+
const raw = await (0, promises_1.readFile)(sourcePath, "utf8");
|
|
85
|
+
try {
|
|
86
|
+
return { value: JSON.parse(raw), sourcePath };
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw new errors_1.DefinitionError(`Failed to parse state JSON at ${sourcePath}`, error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return { value: input.value };
|
|
93
|
+
}
|
|
94
|
+
async function loadStateInput(input) {
|
|
95
|
+
const stateType = assertNonEmpty(input.type, "state.type");
|
|
96
|
+
const stateId = assertNonEmpty(input.id, "state.id");
|
|
97
|
+
const schemaVersion = assertNonEmpty(input.schemaVersion ?? DEFAULT_SCHEMA_VERSION, "state.schemaVersion");
|
|
98
|
+
const { value, sourcePath } = await resolveStateValue(input);
|
|
99
|
+
ensureSerializable(value);
|
|
100
|
+
const canonicalJson = (0, summary_1.stableStringify)(value);
|
|
101
|
+
return {
|
|
102
|
+
stateType,
|
|
103
|
+
stateId,
|
|
104
|
+
schemaVersion,
|
|
105
|
+
canonicalJson,
|
|
106
|
+
hash: (0, summary_1.sha256HexUtf8)(canonicalJson),
|
|
107
|
+
sourcePath,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function detectOnChainStateAnchor(simfSource) {
|
|
111
|
+
const source = stripComments(simfSource.replace(/\r\n/g, "\n"));
|
|
112
|
+
if (!source.includes("{{STATE_HASH}}")) {
|
|
113
|
+
return { sourceVerified: false, reason: "STATE_HASH placeholder is missing" };
|
|
114
|
+
}
|
|
115
|
+
const helperBody = extractFunctionBody(source, "require_state_anchor");
|
|
116
|
+
if (!helperBody) {
|
|
117
|
+
return { sourceVerified: false, reason: "Required state anchor helper function is missing" };
|
|
118
|
+
}
|
|
119
|
+
if (!helperBody.includes("let anchored_state_hash: u256 = 0x{{STATE_HASH}};")) {
|
|
120
|
+
return { sourceVerified: false, reason: "Required anchored_state_hash assignment is missing" };
|
|
121
|
+
}
|
|
122
|
+
if (!helperBody.includes(`let zero_hash: u256 = ${ZERO_HASH_256};`)) {
|
|
123
|
+
return { sourceVerified: false, reason: "Required zero_hash assignment is missing" };
|
|
124
|
+
}
|
|
125
|
+
if (!helperBody.includes("assert!(not(jet::eq_256(anchored_state_hash, zero_hash)));")) {
|
|
126
|
+
return { sourceVerified: false, reason: "Required eq_256 assertion is missing" };
|
|
127
|
+
}
|
|
128
|
+
const mainBody = extractFunctionBody(source, "main");
|
|
129
|
+
if (!mainBody) {
|
|
130
|
+
return { sourceVerified: false, reason: "main function is missing" };
|
|
131
|
+
}
|
|
132
|
+
if (!mainBody.includes("require_state_anchor();")) {
|
|
133
|
+
return { sourceVerified: false, reason: "require_state_anchor() is not called from main" };
|
|
134
|
+
}
|
|
135
|
+
return { sourceVerified: true, helper: "nonzero-eq_256" };
|
|
136
|
+
}
|
|
137
|
+
function buildArtifactStateMetadata(state, options) {
|
|
138
|
+
return {
|
|
139
|
+
stateType: state.stateType,
|
|
140
|
+
stateId: state.stateId,
|
|
141
|
+
schemaVersion: state.schemaVersion,
|
|
142
|
+
hash: state.hash,
|
|
143
|
+
trustMode: "hash-anchor",
|
|
144
|
+
anchorMode: options?.anchorMode ?? DEFAULT_ANCHOR_MODE,
|
|
145
|
+
onChainAnchor: options?.onChainAnchor,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function verifyStateDescriptorAgainstArtifact(state, artifactState, expectedType, expectedId) {
|
|
149
|
+
const noStateTrust = {
|
|
150
|
+
artifactMatch: false,
|
|
151
|
+
onChainAnchorPresent: false,
|
|
152
|
+
onChainAnchorVerified: false,
|
|
153
|
+
effectiveMode: "none",
|
|
154
|
+
};
|
|
155
|
+
if (expectedType && expectedType !== state.stateType) {
|
|
156
|
+
return {
|
|
157
|
+
ok: false,
|
|
158
|
+
reason: `State type mismatch: expected=${expectedType} actual=${state.stateType}`,
|
|
159
|
+
state,
|
|
160
|
+
artifactState,
|
|
161
|
+
trust: noStateTrust,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (expectedId && expectedId !== state.stateId) {
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
reason: `State id mismatch: expected=${expectedId} actual=${state.stateId}`,
|
|
168
|
+
state,
|
|
169
|
+
artifactState,
|
|
170
|
+
trust: noStateTrust,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
if (!artifactState) {
|
|
174
|
+
return {
|
|
175
|
+
ok: false,
|
|
176
|
+
reason: "Artifact does not contain state metadata",
|
|
177
|
+
state,
|
|
178
|
+
trust: noStateTrust,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
const trust = {
|
|
182
|
+
artifactMatch: false,
|
|
183
|
+
onChainAnchorPresent: artifactState.anchorMode === "on-chain-constant-committed",
|
|
184
|
+
onChainAnchorVerified: false,
|
|
185
|
+
effectiveMode: artifactState.anchorMode,
|
|
186
|
+
};
|
|
187
|
+
if (artifactState.stateType !== state.stateType) {
|
|
188
|
+
return {
|
|
189
|
+
ok: false,
|
|
190
|
+
reason: `State type mismatch: artifact=${artifactState.stateType} actual=${state.stateType}`,
|
|
191
|
+
state,
|
|
192
|
+
artifactState,
|
|
193
|
+
trust,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
if (artifactState.stateId !== state.stateId) {
|
|
197
|
+
return {
|
|
198
|
+
ok: false,
|
|
199
|
+
reason: `State id mismatch: artifact=${artifactState.stateId} actual=${state.stateId}`,
|
|
200
|
+
state,
|
|
201
|
+
artifactState,
|
|
202
|
+
trust,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
if (artifactState.schemaVersion !== state.schemaVersion) {
|
|
206
|
+
return {
|
|
207
|
+
ok: false,
|
|
208
|
+
reason: `State schemaVersion mismatch: artifact=${artifactState.schemaVersion} actual=${state.schemaVersion}`,
|
|
209
|
+
state,
|
|
210
|
+
artifactState,
|
|
211
|
+
trust,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
if (artifactState.hash !== state.hash) {
|
|
215
|
+
return {
|
|
216
|
+
ok: false,
|
|
217
|
+
reason: `State hash mismatch: artifact=${artifactState.hash} actual=${state.hash}`,
|
|
218
|
+
state,
|
|
219
|
+
artifactState,
|
|
220
|
+
trust,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
ok: true,
|
|
225
|
+
state,
|
|
226
|
+
artifactState,
|
|
227
|
+
trust: {
|
|
228
|
+
...trust,
|
|
229
|
+
artifactMatch: true,
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async function resolveStateTrust(artifact, baseTrust) {
|
|
234
|
+
if (!artifact.state) {
|
|
235
|
+
return {
|
|
236
|
+
artifactMatch: false,
|
|
237
|
+
onChainAnchorPresent: false,
|
|
238
|
+
onChainAnchorVerified: false,
|
|
239
|
+
effectiveMode: "none",
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (artifact.state.anchorMode !== "on-chain-constant-committed") {
|
|
243
|
+
return baseTrust;
|
|
244
|
+
}
|
|
245
|
+
const sourcePath = artifact.source.mode === "file" ? artifact.source.simfPath : undefined;
|
|
246
|
+
if (!sourcePath) {
|
|
247
|
+
return {
|
|
248
|
+
...baseTrust,
|
|
249
|
+
onChainAnchorPresent: true,
|
|
250
|
+
onChainAnchorVerified: false,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
const source = await (0, promises_1.readFile)(sourcePath, "utf8");
|
|
255
|
+
const detection = detectOnChainStateAnchor(source);
|
|
256
|
+
return {
|
|
257
|
+
...baseTrust,
|
|
258
|
+
onChainAnchorPresent: true,
|
|
259
|
+
onChainAnchorVerified: detection.sourceVerified,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return {
|
|
264
|
+
...baseTrust,
|
|
265
|
+
onChainAnchorPresent: true,
|
|
266
|
+
onChainAnchorVerified: false,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async function verifyStateAgainstArtifact(input) {
|
|
271
|
+
const state = await loadStateInput(input.state);
|
|
272
|
+
const base = verifyStateDescriptorAgainstArtifact(state, input.artifact.state, input.expectedType, input.expectedId);
|
|
273
|
+
const trust = await resolveStateTrust(input.artifact, base.trust);
|
|
274
|
+
return {
|
|
275
|
+
...base,
|
|
276
|
+
trust,
|
|
277
|
+
};
|
|
278
|
+
}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -71,6 +71,7 @@ export interface SimplicityArtifact {
|
|
|
71
71
|
notes: string | null;
|
|
72
72
|
};
|
|
73
73
|
definition?: ArtifactDefinitionMetadata;
|
|
74
|
+
state?: ArtifactStateMetadata;
|
|
74
75
|
legacy?: {
|
|
75
76
|
simfTemplatePath?: string;
|
|
76
77
|
params?: {
|
|
@@ -85,12 +86,14 @@ export interface CompileFromFileInput {
|
|
|
85
86
|
templateVars?: Record<string, string | number>;
|
|
86
87
|
artifactPath?: string;
|
|
87
88
|
definition?: DefinitionInput;
|
|
89
|
+
state?: StateDocumentInput;
|
|
88
90
|
}
|
|
89
91
|
export interface CompileFromPresetInput {
|
|
90
92
|
preset: string;
|
|
91
93
|
params: Record<string, string | number>;
|
|
92
94
|
artifactPath?: string;
|
|
93
95
|
definition?: DefinitionInput;
|
|
96
|
+
state?: StateDocumentInput;
|
|
94
97
|
}
|
|
95
98
|
export interface DefinitionInput {
|
|
96
99
|
type: string;
|
|
@@ -108,6 +111,22 @@ export interface DefinitionDescriptor {
|
|
|
108
111
|
hash: string;
|
|
109
112
|
sourcePath?: string;
|
|
110
113
|
}
|
|
114
|
+
export interface StateDocumentInput {
|
|
115
|
+
type: string;
|
|
116
|
+
id: string;
|
|
117
|
+
schemaVersion?: string;
|
|
118
|
+
jsonPath?: string;
|
|
119
|
+
value?: unknown;
|
|
120
|
+
anchorMode?: DefinitionAnchorMode;
|
|
121
|
+
}
|
|
122
|
+
export interface StateDocumentDescriptor {
|
|
123
|
+
stateType: string;
|
|
124
|
+
stateId: string;
|
|
125
|
+
schemaVersion: string;
|
|
126
|
+
canonicalJson: string;
|
|
127
|
+
hash: string;
|
|
128
|
+
sourcePath?: string;
|
|
129
|
+
}
|
|
111
130
|
export interface ArtifactDefinitionMetadata {
|
|
112
131
|
definitionType: string;
|
|
113
132
|
definitionId: string;
|
|
@@ -121,6 +140,26 @@ export interface ArtifactDefinitionMetadata {
|
|
|
121
140
|
sourceVerified: boolean;
|
|
122
141
|
};
|
|
123
142
|
}
|
|
143
|
+
export interface ArtifactStateMetadata {
|
|
144
|
+
stateType: string;
|
|
145
|
+
stateId: string;
|
|
146
|
+
schemaVersion: string;
|
|
147
|
+
hash: string;
|
|
148
|
+
trustMode: DefinitionTrustMode;
|
|
149
|
+
anchorMode: DefinitionAnchorMode;
|
|
150
|
+
onChainAnchor?: {
|
|
151
|
+
helper: "nonzero-eq_256";
|
|
152
|
+
templateVar: "STATE_HASH";
|
|
153
|
+
sourceVerified: boolean;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
export type BondIssuanceStatus = "ISSUED" | "PARTIALLY_REDEEMED" | "REDEEMED";
|
|
157
|
+
export type BondTransitionType = "ISSUE" | "REDEEM";
|
|
158
|
+
export interface BondStateTransition {
|
|
159
|
+
type: BondTransitionType;
|
|
160
|
+
amount: number;
|
|
161
|
+
at: string;
|
|
162
|
+
}
|
|
124
163
|
export interface DeploymentInfo {
|
|
125
164
|
contractAddress: string;
|
|
126
165
|
internalKey: string;
|
|
@@ -192,6 +231,13 @@ export interface PsetSummary {
|
|
|
192
231
|
trustMode: DefinitionTrustMode | null;
|
|
193
232
|
anchorMode: DefinitionAnchorMode | null;
|
|
194
233
|
};
|
|
234
|
+
state?: {
|
|
235
|
+
type: string | null;
|
|
236
|
+
id: string | null;
|
|
237
|
+
hash: string | null;
|
|
238
|
+
trustMode: DefinitionTrustMode | null;
|
|
239
|
+
anchorMode: DefinitionAnchorMode | null;
|
|
240
|
+
};
|
|
195
241
|
contract: {
|
|
196
242
|
address: string;
|
|
197
243
|
cmr: string;
|
|
@@ -271,6 +317,66 @@ export interface DefinitionVerificationResult {
|
|
|
271
317
|
effectiveMode: "none" | DefinitionAnchorMode;
|
|
272
318
|
};
|
|
273
319
|
}
|
|
320
|
+
export interface StateVerificationResult {
|
|
321
|
+
ok: boolean;
|
|
322
|
+
reason?: string;
|
|
323
|
+
state: StateDocumentDescriptor;
|
|
324
|
+
artifactState?: ArtifactStateMetadata;
|
|
325
|
+
trust: {
|
|
326
|
+
artifactMatch: boolean;
|
|
327
|
+
onChainAnchorPresent: boolean;
|
|
328
|
+
onChainAnchorVerified: boolean;
|
|
329
|
+
effectiveMode: "none" | DefinitionAnchorMode;
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
export interface BondDefinition {
|
|
333
|
+
bondId: string;
|
|
334
|
+
issuer: string;
|
|
335
|
+
faceValue: number;
|
|
336
|
+
couponBps: number;
|
|
337
|
+
issueDate: string;
|
|
338
|
+
maturityDate: number;
|
|
339
|
+
currencyAssetId: string;
|
|
340
|
+
controllerXonly: string;
|
|
341
|
+
}
|
|
342
|
+
export interface BondIssuanceState {
|
|
343
|
+
issuanceId: string;
|
|
344
|
+
bondId: string;
|
|
345
|
+
issuerEntityId: string;
|
|
346
|
+
issuedPrincipal: number;
|
|
347
|
+
outstandingPrincipal: number;
|
|
348
|
+
redeemedPrincipal: number;
|
|
349
|
+
currencyAssetId: string;
|
|
350
|
+
controllerXonly: string;
|
|
351
|
+
issuedAt: string;
|
|
352
|
+
status: BondIssuanceStatus;
|
|
353
|
+
previousStateHash?: string | null;
|
|
354
|
+
lastTransition?: BondStateTransition;
|
|
355
|
+
}
|
|
356
|
+
export interface BondSettlementDescriptor {
|
|
357
|
+
settlementId: string;
|
|
358
|
+
bondId: string;
|
|
359
|
+
issuanceId: string;
|
|
360
|
+
definitionHash: string;
|
|
361
|
+
previousStateHash: string;
|
|
362
|
+
nextStateHash: string;
|
|
363
|
+
previousStatus: BondIssuanceStatus;
|
|
364
|
+
nextStatus: BondIssuanceStatus;
|
|
365
|
+
transitionKind: BondTransitionType;
|
|
366
|
+
redeemAmount: number;
|
|
367
|
+
transitionAt: string;
|
|
368
|
+
assetId: string;
|
|
369
|
+
nextContractAddress: string;
|
|
370
|
+
nextAmountSat: number;
|
|
371
|
+
maxFeeSat: number;
|
|
372
|
+
principal: {
|
|
373
|
+
issued: number;
|
|
374
|
+
previousOutstanding: number;
|
|
375
|
+
nextOutstanding: number;
|
|
376
|
+
previousRedeemed: number;
|
|
377
|
+
nextRedeemed: number;
|
|
378
|
+
};
|
|
379
|
+
}
|
|
274
380
|
export interface WaitForFundingInput {
|
|
275
381
|
minAmountSat?: number;
|
|
276
382
|
pollIntervalMs?: number;
|