@thruputchain/client 0.25.0 → 0.26.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 +1 -1
- package/README.md +58 -40
- package/dist/{actions-DxJZ3yIv.cjs → actions-CXx6qzsv.cjs} +161 -5
- package/dist/actions-CXx6qzsv.cjs.map +1 -0
- package/dist/{actions-CdFSUTO2.js → actions-QHgiCqT6.js} +139 -7
- package/dist/actions-QHgiCqT6.js.map +1 -0
- package/dist/actions.cjs +6 -2
- package/dist/actions.d.cts +4 -4
- package/dist/actions.d.ts +4 -4
- package/dist/actions.js +3 -3
- package/dist/artifact-BM_MVzhL.d.cts +35 -0
- package/dist/artifact-BM_MVzhL.d.cts.map +1 -0
- package/dist/artifact-BM_MVzhL.d.ts +35 -0
- package/dist/artifact-BM_MVzhL.d.ts.map +1 -0
- package/dist/{client-BxB6ktQd.d.cts → client-JixxskeH.d.cts} +58 -2
- package/dist/client-JixxskeH.d.cts.map +1 -0
- package/dist/{client-BxB6ktQd.d.ts → client-JixxskeH.d.ts} +58 -2
- package/dist/client-JixxskeH.d.ts.map +1 -0
- package/dist/{codec-CX-19jac.js → codec-BHKoSaOP.js} +1426 -50
- package/dist/{codec-CX-19jac.js.map → codec-BHKoSaOP.js.map} +1 -1
- package/dist/{codec-B7HJjMzI.cjs → codec-C3OlYm98.cjs} +1426 -50
- package/dist/{codec-B7HJjMzI.cjs.map → codec-C3OlYm98.cjs.map} +1 -1
- package/dist/{contract-D0wCFcAN.cjs → contract-C7T2H2Oi.cjs} +742 -333
- package/dist/contract-C7T2H2Oi.cjs.map +1 -0
- package/dist/{contract-BpxjDMHw.js → contract-DzvyyFCI.js} +712 -345
- package/dist/contract-DzvyyFCI.js.map +1 -0
- package/dist/contract.cjs +6 -1
- package/dist/contract.d.cts +4 -3
- package/dist/contract.d.ts +4 -3
- package/dist/contract.js +2 -2
- package/dist/contracts.cjs +3 -3
- package/dist/contracts.d.cts +3 -3
- package/dist/contracts.d.ts +3 -3
- package/dist/contracts.js +3 -3
- package/dist/{dev_wallet-C9eMQROk.js → dev_wallet-CSMA1PCt.js} +2 -2
- package/dist/{dev_wallet-C9eMQROk.js.map → dev_wallet-CSMA1PCt.js.map} +1 -1
- package/dist/{dev_wallet-BXWK5KR6.cjs → dev_wallet-GX-NCAFk.cjs} +2 -2
- package/dist/{dev_wallet-BXWK5KR6.cjs.map → dev_wallet-GX-NCAFk.cjs.map} +1 -1
- package/dist/{execution-DyxaIG4v.d.cts → execution-DYqPDn4O.d.cts} +3 -3
- package/dist/{execution-DyxaIG4v.d.cts.map → execution-DYqPDn4O.d.cts.map} +1 -1
- package/dist/{execution-CRDyKu45.d.ts → execution-Dj6E4B_b.d.ts} +3 -3
- package/dist/{execution-CRDyKu45.d.ts.map → execution-Dj6E4B_b.d.ts.map} +1 -1
- package/dist/index-1-rFirYe.d.cts +90 -0
- package/dist/index-1-rFirYe.d.cts.map +1 -0
- package/dist/{index-BZ0O5jZG.d.cts → index-BraLWObY.d.cts} +3 -2
- package/dist/{index-BZ0O5jZG.d.cts.map → index-BraLWObY.d.cts.map} +1 -1
- package/dist/{index-sAzHHayu.d.ts → index-ByvV2UF6.d.ts} +3 -2
- package/dist/{index-sAzHHayu.d.ts.map → index-ByvV2UF6.d.ts.map} +1 -1
- package/dist/{index-DOEBWXY2.d.cts → index-CG2WRtGa.d.cts} +31 -4
- package/dist/index-CG2WRtGa.d.cts.map +1 -0
- package/dist/{index-C-M-4jMR.d.cts → index-CVrhB9Dr.d.cts} +4 -4
- package/dist/{index-C-M-4jMR.d.cts.map → index-CVrhB9Dr.d.cts.map} +1 -1
- package/dist/{index-C97zw2rk.d.ts → index-D89ur7mO.d.ts} +31 -4
- package/dist/index-D89ur7mO.d.ts.map +1 -0
- package/dist/index-SV-inhhO.d.ts +90 -0
- package/dist/index-SV-inhhO.d.ts.map +1 -0
- package/dist/{index-CXuO2Zjx.d.ts → index-dJ65V8se.d.ts} +4 -4
- package/dist/{index-CXuO2Zjx.d.ts.map → index-dJ65V8se.d.ts.map} +1 -1
- package/dist/index.cjs +20 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -9
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +10 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -53
- package/dist/index.js.map +1 -1
- package/dist/{readError-BVbfTMLv.cjs → readError-AoNjzW75.cjs} +2 -2
- package/dist/{readError-BVbfTMLv.cjs.map → readError-AoNjzW75.cjs.map} +1 -1
- package/dist/{readError-Dn_XN98x.js → readError-CZPYcW__.js} +2 -2
- package/dist/{readError-Dn_XN98x.js.map → readError-CZPYcW__.js.map} +1 -1
- package/dist/{readViews-BKJtE5J3.d.cts → readViews-DJyqc7im.d.cts} +14 -4
- package/dist/readViews-DJyqc7im.d.cts.map +1 -0
- package/dist/{readViews-C_FX8wby.d.ts → readViews-DQoaUORa.d.ts} +14 -4
- package/dist/readViews-DQoaUORa.d.ts.map +1 -0
- package/dist/{sendInvocation-C4ruTC-9.js → sendInvocation-BwabRa2N.js} +5 -5
- package/dist/{sendInvocation-C4ruTC-9.js.map → sendInvocation-BwabRa2N.js.map} +1 -1
- package/dist/{sendInvocation-2EZanZZr.cjs → sendInvocation-qO8AZcmU.cjs} +5 -5
- package/dist/{sendInvocation-2EZanZZr.cjs.map → sendInvocation-qO8AZcmU.cjs.map} +1 -1
- package/dist/signed-op.cjs +2 -2
- package/dist/signed-op.d.cts +1 -1
- package/dist/signed-op.d.ts +1 -1
- package/dist/signed-op.js +2 -2
- package/dist/{signedOp-x4fakrr4.js → signedOp-C52gOgpR.js} +3 -3
- package/dist/{signedOp-x4fakrr4.js.map → signedOp-C52gOgpR.js.map} +1 -1
- package/dist/{signedOp-Bluo0OAd.cjs → signedOp-oqAXEqmE.cjs} +3 -3
- package/dist/{signedOp-Bluo0OAd.cjs.map → signedOp-oqAXEqmE.cjs.map} +1 -1
- package/dist/{signed_op-BzVbBgv4.js → signed_op-BokEDWr-.js} +2 -2
- package/dist/{signed_op-BzVbBgv4.js.map → signed_op-BokEDWr-.js.map} +1 -1
- package/dist/{signed_op-DFgQ2yFj.d.ts → signed_op-C4lY_TCc.d.ts} +3 -3
- package/dist/{signed_op-DFgQ2yFj.d.ts.map → signed_op-C4lY_TCc.d.ts.map} +1 -1
- package/dist/{signed_op-LwxrDud8.d.cts → signed_op-DX5WuyEh.d.cts} +3 -3
- package/dist/{signed_op-LwxrDud8.d.cts.map → signed_op-DX5WuyEh.d.cts.map} +1 -1
- package/dist/{signed_op-z_8fqAi1.cjs → signed_op-q8KKSm5e.cjs} +2 -2
- package/dist/{signed_op-z_8fqAi1.cjs.map → signed_op-q8KKSm5e.cjs.map} +1 -1
- package/dist/{transport-DqyoNtUy.js → transport-CBFwAIIv.js} +31 -4
- package/dist/transport-CBFwAIIv.js.map +1 -0
- package/dist/{transport-B-P16rs6.cjs → transport-tWxKhXUt.cjs} +31 -4
- package/dist/transport-tWxKhXUt.cjs.map +1 -0
- package/dist/transport.cjs +3 -3
- package/dist/transport.d.cts +3 -3
- package/dist/transport.d.ts +3 -3
- package/dist/transport.js +3 -3
- package/dist/{wallet-CwWPYoHF.js → wallet-7RELuTkf.js} +3 -3
- package/dist/{wallet-CwWPYoHF.js.map → wallet-7RELuTkf.js.map} +1 -1
- package/dist/{wallet-Bthapk8i.cjs → wallet-T_JazXRc.cjs} +3 -3
- package/dist/{wallet-Bthapk8i.cjs.map → wallet-T_JazXRc.cjs.map} +1 -1
- package/dist/wallet.cjs +2 -2
- package/dist/wallet.d.cts +2 -2
- package/dist/wallet.d.ts +2 -2
- package/dist/wallet.js +2 -2
- package/package.json +4 -4
- package/dist/actions-CdFSUTO2.js.map +0 -1
- package/dist/actions-DxJZ3yIv.cjs.map +0 -1
- package/dist/client-BxB6ktQd.d.cts.map +0 -1
- package/dist/client-BxB6ktQd.d.ts.map +0 -1
- package/dist/contract-BpxjDMHw.js.map +0 -1
- package/dist/contract-D0wCFcAN.cjs.map +0 -1
- package/dist/index-BuPQcAK2.d.ts +0 -23
- package/dist/index-BuPQcAK2.d.ts.map +0 -1
- package/dist/index-C97zw2rk.d.ts.map +0 -1
- package/dist/index-DOEBWXY2.d.cts.map +0 -1
- package/dist/index-uQqRk9Ww.d.cts +0 -23
- package/dist/index-uQqRk9Ww.d.cts.map +0 -1
- package/dist/readViews-BKJtE5J3.d.cts.map +0 -1
- package/dist/readViews-C_FX8wby.d.ts.map +0 -1
- package/dist/transport-B-P16rs6.cjs.map +0 -1
- package/dist/transport-DqyoNtUy.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_codec = require("./codec-
|
|
1
|
+
const require_codec = require("./codec-C3OlYm98.cjs");
|
|
2
2
|
const require_hex = require("./hex-DkGpiZ5e.cjs");
|
|
3
3
|
let _thruputchain_protocol = require("@thruputchain/protocol");
|
|
4
4
|
let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
|
|
@@ -38,6 +38,21 @@ var ContractDecodeError = class extends Error {
|
|
|
38
38
|
this.name = "ContractDecodeError";
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
|
+
/** Retrieved bytes or response metadata failed identity/canonical validation. */
|
|
42
|
+
var ContractVerificationError = class extends Error {
|
|
43
|
+
constructor(message, options) {
|
|
44
|
+
super(message, options);
|
|
45
|
+
this.name = "ContractVerificationError";
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/contract/valueLimits.ts
|
|
50
|
+
function contractValueLimits(inputBytes) {
|
|
51
|
+
return {
|
|
52
|
+
maxDepth: 128,
|
|
53
|
+
maxWork: Math.min(1e5, 1024 + 16 * inputBytes)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
41
56
|
//#endregion
|
|
42
57
|
//#region src/contract/signedToken.ts
|
|
43
58
|
const SIGNED_TOKEN = Object.freeze({ kind: "signedToken" });
|
|
@@ -117,6 +132,7 @@ function assertWellFormedString(value) {
|
|
|
117
132
|
}
|
|
118
133
|
function requireByteArrayCodec(codec, label = "U8 container") {
|
|
119
134
|
return {
|
|
135
|
+
...codec,
|
|
120
136
|
encode(writer, value) {
|
|
121
137
|
if (!(value instanceof Uint8Array)) throw new TypeError(`Expected Uint8Array for ${label}`);
|
|
122
138
|
codec.encode(writer, value);
|
|
@@ -126,6 +142,7 @@ function requireByteArrayCodec(codec, label = "U8 container") {
|
|
|
126
142
|
}
|
|
127
143
|
function requireBigIntCodec(label, codec) {
|
|
128
144
|
return {
|
|
145
|
+
...codec,
|
|
129
146
|
encode(writer, value) {
|
|
130
147
|
if (typeof value !== "bigint") throw new TypeError(`${label} expected bigint`);
|
|
131
148
|
codec.encode(writer, value);
|
|
@@ -135,6 +152,7 @@ function requireBigIntCodec(label, codec) {
|
|
|
135
152
|
}
|
|
136
153
|
function requireArrayCodec(codec) {
|
|
137
154
|
return {
|
|
155
|
+
...codec,
|
|
138
156
|
encode(writer, value) {
|
|
139
157
|
if (!Array.isArray(value)) throw new TypeError("Expected array");
|
|
140
158
|
assertOwnArrayElements(value);
|
|
@@ -143,20 +161,6 @@ function requireArrayCodec(codec) {
|
|
|
143
161
|
decode: codec.decode
|
|
144
162
|
};
|
|
145
163
|
}
|
|
146
|
-
const MAX_ZERO_WIDTH_VECTOR_LENGTH = 1024;
|
|
147
|
-
function safeVarArrayCodec(inner, maxLength) {
|
|
148
|
-
return {
|
|
149
|
-
encode: (0, _thruputchain_protocol.VarArrayCodec)(inner, maxLength).encode,
|
|
150
|
-
decode(reader) {
|
|
151
|
-
const length = Number(reader.readUint32());
|
|
152
|
-
if (length > maxLength) throw new Error(`VarArray length ${length} exceeds max ${maxLength}`);
|
|
153
|
-
if (length > reader.remaining && length > MAX_ZERO_WIDTH_VECTOR_LENGTH) throw new Error(`VarArray length ${length} cannot be safely materialized from ${reader.remaining} remaining bytes`);
|
|
154
|
-
const values = [];
|
|
155
|
-
for (let index = 0; index < length; index++) values.push(inner.decode(reader));
|
|
156
|
-
return values;
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
164
|
/**
|
|
161
165
|
* Resolve a contract-spec type into the XDR/CXS codec used to encode values.
|
|
162
166
|
*
|
|
@@ -164,12 +168,13 @@ function safeVarArrayCodec(inner, maxLength) {
|
|
|
164
168
|
* contract spec. Build that map with {@link buildNamedCodecs}.
|
|
165
169
|
*/
|
|
166
170
|
function resolveCodec(ty, namedCodecs) {
|
|
167
|
-
if (
|
|
171
|
+
if (ty.v === _thruputchain_contract_spec.ContractTypeKind.Named) return resolveUncachedCodec(ty, namedCodecs);
|
|
172
|
+
if (!namedCodecs) return (0, _thruputchain_xdr.withCodecBudget)(resolveUncachedCodec(ty));
|
|
168
173
|
const resolvedCache = resolvedCodecCaches.get(namedCodecs);
|
|
169
|
-
if (!resolvedCache?.cacheableTypes.has(ty)) return resolveUncachedCodec(ty, namedCodecs);
|
|
174
|
+
if (!resolvedCache?.cacheableTypes.has(ty)) return (0, _thruputchain_xdr.withCodecBudget)(resolveUncachedCodec(ty, namedCodecs));
|
|
170
175
|
const cached = resolvedCache.codecs.get(ty);
|
|
171
176
|
if (cached) return cached;
|
|
172
|
-
const codec = resolveUncachedCodec(ty, namedCodecs);
|
|
177
|
+
const codec = (0, _thruputchain_xdr.withCodecBudget)(resolveUncachedCodec(ty, namedCodecs));
|
|
173
178
|
resolvedCache.codecs.set(ty, codec);
|
|
174
179
|
return codec;
|
|
175
180
|
}
|
|
@@ -192,13 +197,13 @@ function resolveUncachedCodec(ty, namedCodecs) {
|
|
|
192
197
|
case _thruputchain_contract_spec.ContractTypeKind.Option: return (0, _thruputchain_protocol.OptionCodec)(resolveCodec(ty.option_inner, namedCodecs));
|
|
193
198
|
case _thruputchain_contract_spec.ContractTypeKind.Vector:
|
|
194
199
|
if (ty.vector.element?.v === _thruputchain_contract_spec.ContractTypeKind.U8) return requireByteArrayCodec((0, _thruputchain_xdr.VarOpaqueCodec)(Number(ty.vector.max_len)));
|
|
195
|
-
return requireArrayCodec(
|
|
200
|
+
return requireArrayCodec((0, _thruputchain_protocol.VarArrayCodec)(resolveCodec(ty.vector.element, namedCodecs), Number(ty.vector.max_len)));
|
|
196
201
|
case _thruputchain_contract_spec.ContractTypeKind.Array:
|
|
197
202
|
if (ty.array.element?.v === _thruputchain_contract_spec.ContractTypeKind.U8) return requireByteArrayCodec((0, _thruputchain_xdr.FixedOpaqueCodec)(Number(ty.array.len)));
|
|
198
203
|
const arrayLength = Number(ty.array.len);
|
|
199
204
|
const arrayCodec = requireArrayCodec((0, _thruputchain_protocol.FixedArrayCodec)(resolveCodec(ty.array.element, namedCodecs), arrayLength));
|
|
200
205
|
return {
|
|
201
|
-
|
|
206
|
+
...arrayCodec,
|
|
202
207
|
decode(reader) {
|
|
203
208
|
return (0, _thruputchain_xdr.fixedArray)(arrayCodec.decode(reader), arrayLength);
|
|
204
209
|
}
|
|
@@ -269,7 +274,7 @@ function encodeCalldata(spec, interfaceName, methodName, args, namedCodecs) {
|
|
|
269
274
|
function decodeCalldata(spec, interfaceName, methodName, data, namedCodecs) {
|
|
270
275
|
const method = lookupMethod(spec, interfaceName, methodName);
|
|
271
276
|
try {
|
|
272
|
-
const r = new _thruputchain_protocol.CxsReader(data);
|
|
277
|
+
const r = new _thruputchain_protocol.CxsReader(data, contractValueLimits(data.length));
|
|
273
278
|
const result = {};
|
|
274
279
|
for (const arg of method.args) {
|
|
275
280
|
const argCodec = resolveCodec(arg.ty, namedCodecs);
|
|
@@ -293,6 +298,47 @@ function defineRecordValue$2(record, name, value) {
|
|
|
293
298
|
});
|
|
294
299
|
}
|
|
295
300
|
//#endregion
|
|
301
|
+
//#region src/derivation/entrypointId.ts
|
|
302
|
+
/**
|
|
303
|
+
* The major-version domain a semver string belongs to.
|
|
304
|
+
*
|
|
305
|
+
* Mirrors the Rust SDK's `major_version_from_string`: a zero major or minor is
|
|
306
|
+
* not a stable domain on its own, so the domain widens until it is.
|
|
307
|
+
*
|
|
308
|
+
* - `1.2.3` → `1`
|
|
309
|
+
* - `0.9.2` → `0.9`
|
|
310
|
+
* - `0.0.2` → `0.0.2`
|
|
311
|
+
*
|
|
312
|
+
* @throws If `versionStr` has fewer than three dot-separated components.
|
|
313
|
+
*/
|
|
314
|
+
function majorVersionFromSemver(versionStr) {
|
|
315
|
+
const [majorPart, minorPart, patchPart] = versionStr.split(".");
|
|
316
|
+
if (majorPart === void 0 || minorPart === void 0 || patchPart === void 0) throw new Error(`Invalid semver: ${versionStr}`);
|
|
317
|
+
const major = parseInt(majorPart, 10);
|
|
318
|
+
const minor = parseInt(minorPart, 10);
|
|
319
|
+
const patch = parseInt(patchPart, 10);
|
|
320
|
+
if (major > 0) return `${major}`;
|
|
321
|
+
if (minor > 0) return `${major}.${minor}`;
|
|
322
|
+
if (patch > 0) return `${major}.${minor}.${patch}`;
|
|
323
|
+
return versionStr;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The 8-byte entrypoint ID for a contract method:
|
|
327
|
+
* `sha256(interface :: majorVersion :: method)[0..8]`.
|
|
328
|
+
*
|
|
329
|
+
* A null `interfaceName` uses the `_unnamed_interface` domain. The literal `::`
|
|
330
|
+
* separators are what keep field boundaries unambiguous — without them
|
|
331
|
+
* interface `Foo1` with major `2` and interface `Foo` with major `12` would
|
|
332
|
+
* hash identically. Fields are raw UTF-8, not XDR-encoded.
|
|
333
|
+
*
|
|
334
|
+
* Matches the Rust SDK's `contract_entrypoint_id`.
|
|
335
|
+
*/
|
|
336
|
+
function deriveEntrypointID(interfaceName, versionStr, methodName) {
|
|
337
|
+
const name = interfaceName ?? "_unnamed_interface";
|
|
338
|
+
const majorVersion = majorVersionFromSemver(versionStr);
|
|
339
|
+
return (0, _thruputchain_protocol.contractEntrypointID)((0, _noble_hashes_sha2_js.sha256)(new TextEncoder().encode(`${name}::${majorVersion}::${methodName}`)).slice(0, 8));
|
|
340
|
+
}
|
|
341
|
+
//#endregion
|
|
296
342
|
//#region src/contract/codegen.ts
|
|
297
343
|
/**
|
|
298
344
|
* Build codecs for named contract-spec types.
|
|
@@ -309,7 +355,7 @@ function buildNamedCodecs(typeDefs) {
|
|
|
309
355
|
const define = (id, codec) => {
|
|
310
356
|
const key = specIdKey(id);
|
|
311
357
|
if (Object.hasOwn(codecs, key)) throw new Error(`Contract spec defines "${formatSpecId(id)}" twice`);
|
|
312
|
-
codecs[key] = codec;
|
|
358
|
+
codecs[key] = (0, _thruputchain_xdr.withCodecBudget)(codec);
|
|
313
359
|
};
|
|
314
360
|
for (const def of typeDefs) switch (def.v) {
|
|
315
361
|
case _thruputchain_contract_spec.ContractTypeDefKind.TypeDefEnum:
|
|
@@ -434,43 +480,6 @@ function defineRecordValue$1(record, name, value) {
|
|
|
434
480
|
});
|
|
435
481
|
}
|
|
436
482
|
//#endregion
|
|
437
|
-
//#region src/contract/hash.ts
|
|
438
|
-
/**
|
|
439
|
-
* Return the network identity of a protocol contract.
|
|
440
|
-
*
|
|
441
|
-
* The hash covers the canonical encoded `Contract`, including its type and
|
|
442
|
-
* payload framing. Hashing only the raw artifact bytes produces a different
|
|
443
|
-
* value and therefore a different derived address.
|
|
444
|
-
*
|
|
445
|
-
* @example
|
|
446
|
-
* ```ts
|
|
447
|
-
* import {
|
|
448
|
-
* deriveContractAddress,
|
|
449
|
-
* hashContract,
|
|
450
|
-
* } from "@thruputchain/client";
|
|
451
|
-
*
|
|
452
|
-
* const contractHash = hashContract(contract);
|
|
453
|
-
* const address = deriveContractAddress(wallet, deploymentNonce, contractHash);
|
|
454
|
-
* ```
|
|
455
|
-
*/
|
|
456
|
-
function hashContract(contract) {
|
|
457
|
-
return (0, _thruputchain_protocol.sha256Hash)((0, _noble_hashes_sha2_js.sha256)((0, _thruputchain_protocol.serialize)(contract, _thruputchain_protocol.codec.Contract)));
|
|
458
|
-
}
|
|
459
|
-
//#endregion
|
|
460
|
-
//#region src/derivation/contractAddress.ts
|
|
461
|
-
/**
|
|
462
|
-
* The on-chain address a deployment produces:
|
|
463
|
-
* `sha256(txs(ContractDeploymentHashPreimage))` over the deployer, nonce, and
|
|
464
|
-
* contract hash.
|
|
465
|
-
*/
|
|
466
|
-
function deriveContractAddress(deployer, nonce, contractHash) {
|
|
467
|
-
return (0, _thruputchain_protocol.contractAddress)((0, _noble_hashes_sha2_js.sha256)((0, _thruputchain_protocol.serialize)({
|
|
468
|
-
deployer_address: deployer,
|
|
469
|
-
nonce,
|
|
470
|
-
contract_hash: contractHash
|
|
471
|
-
}, _thruputchain_protocol.codec.ContractDeploymentHashPreimage)));
|
|
472
|
-
}
|
|
473
|
-
//#endregion
|
|
474
483
|
//#region src/contract/codec.ts
|
|
475
484
|
const contextCodecs = /* @__PURE__ */ new WeakMap();
|
|
476
485
|
const contextEnums = /* @__PURE__ */ new WeakMap();
|
|
@@ -543,7 +552,7 @@ function createContractCodec(context, type, label = "contract value") {
|
|
|
543
552
|
/** @internal */
|
|
544
553
|
function decodeComplete(valueCodec, data, label) {
|
|
545
554
|
try {
|
|
546
|
-
const reader = new _thruputchain_xdr.CxsReader(data);
|
|
555
|
+
const reader = new _thruputchain_xdr.CxsReader(data, contractValueLimits(data.length));
|
|
547
556
|
const value = valueCodec.decode(reader);
|
|
548
557
|
if (reader.remaining !== 0) throw new Error(`${reader.remaining} trailing bytes`);
|
|
549
558
|
return value;
|
|
@@ -553,277 +562,90 @@ function decodeComplete(valueCodec, data, label) {
|
|
|
553
562
|
}
|
|
554
563
|
}
|
|
555
564
|
//#endregion
|
|
556
|
-
//#region src/
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
representation: metadata.representation,
|
|
576
|
-
codec: resolveContextCodec(definition.codecs, { v: signed ? _thruputchain_contract_spec.ContractTypeKind.I256 : _thruputchain_contract_spec.ContractTypeKind.U256 }),
|
|
577
|
-
min: signed ? -bound : 0n,
|
|
578
|
-
max: bound - 1n
|
|
579
|
-
};
|
|
580
|
-
}
|
|
581
|
-
return {
|
|
582
|
-
name: metadata.name,
|
|
583
|
-
representation: metadata.representation
|
|
565
|
+
//#region src/actions/viewFreshness.ts
|
|
566
|
+
/** Reject a successful view that violates the caller's snapshot constraint. */
|
|
567
|
+
function assertViewFreshness(height, minHeight) {
|
|
568
|
+
if (minHeight !== void 0 && height < minHeight) throw new Error(`View response height ${height} is below the requested minimum ${minHeight}`);
|
|
569
|
+
}
|
|
570
|
+
//#endregion
|
|
571
|
+
//#region src/actions/viewAuthClaim.ts
|
|
572
|
+
function u32(value, name) {
|
|
573
|
+
require_codec.serializeU64(value, name);
|
|
574
|
+
if (value > 4294967295n) throw new RangeError(`${name} must fit uint32`);
|
|
575
|
+
return Number(value);
|
|
576
|
+
}
|
|
577
|
+
/** Project public authorization assumptions; this neither signs nor verifies authorization. */
|
|
578
|
+
function toAssumedAuth(claim) {
|
|
579
|
+
switch (claim.claim_type) {
|
|
580
|
+
case _thruputchain_protocol.AuthClaimType.kSourceAddressAuthData: return {
|
|
581
|
+
kind: "sourceAddress",
|
|
582
|
+
allowedKeys: u32(claim.source_address_claim.allowed_keys, "allowedKeys"),
|
|
583
|
+
requiredWeight: u32(claim.source_address_claim.required_weight, "requiredWeight")
|
|
584
584
|
};
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
}));
|
|
590
|
-
const descriptor = {
|
|
591
|
-
get signature() {
|
|
592
|
-
return Uint8Array.from(signature);
|
|
593
|
-
},
|
|
594
|
-
decode(event) {
|
|
595
|
-
if (!bytesEqual(event.topics[0], signature)) return null;
|
|
596
|
-
try {
|
|
597
|
-
const expectedTopicCount = resolvedTopics.length + 1;
|
|
598
|
-
if (event.topics.length !== expectedTopicCount) throw new Error(`expected ${expectedTopicCount} topics, got ${event.topics.length}`);
|
|
599
|
-
assertEventSize(event.topics, event.data);
|
|
600
|
-
const decodedTopics = {};
|
|
601
|
-
for (const [index, metadata] of resolvedTopics.entries()) {
|
|
602
|
-
const rawTopic = event.topics[index + 1];
|
|
603
|
-
if (rawTopic === void 0) throw new Error(`missing topic "${metadata.name}"`);
|
|
604
|
-
const value = decodeTopic(metadata, (0, _thruputchain_xdr.fixedBytes)(rawTopic, 32));
|
|
605
|
-
defineRecordValue(decodedTopics, metadata.name, value);
|
|
606
|
-
}
|
|
607
|
-
const reader = new _thruputchain_xdr.CxsReader(event.data);
|
|
608
|
-
const decodedFields = {};
|
|
609
|
-
for (const field of fields) defineRecordValue(decodedFields, field.metadata.name, field.codec.decode(reader));
|
|
610
|
-
if (reader.remaining !== 0) throw new Error(`${reader.remaining} trailing payload bytes`);
|
|
611
|
-
return {
|
|
612
|
-
name: eventName,
|
|
613
|
-
topics: decodedTopics,
|
|
614
|
-
fields: decodedFields
|
|
615
|
-
};
|
|
616
|
-
} catch (cause) {
|
|
617
|
-
if (cause instanceof ContractDecodeError) throw cause;
|
|
618
|
-
throw new ContractDecodeError(`Failed to decode ${eventName}${cause instanceof Error ? `: ${cause.message}` : ""}`, { cause });
|
|
619
|
-
}
|
|
620
|
-
},
|
|
621
|
-
filter(input) {
|
|
622
|
-
if (input.addresses.length === 0) throw new Error(`${eventName} filter requires a contract address`);
|
|
623
|
-
if (input.addresses.length > maxFilterContractAddresses) throw new Error(`${eventName} filter supports at most ${maxFilterContractAddresses} contract addresses`);
|
|
624
|
-
for (const address of input.addresses) (0, _thruputchain_xdr.fixedBytes)(address, 32);
|
|
625
|
-
const filterTopics = { topic0: [Uint8Array.from(signature)] };
|
|
626
|
-
const constraints = input.topics;
|
|
627
|
-
if (constraints) {
|
|
628
|
-
const directTopics = new Map(resolvedTopics.map((metadata, index) => ({
|
|
629
|
-
metadata,
|
|
630
|
-
index
|
|
631
|
-
})).filter(({ metadata }) => metadata.representation !== "hash").map((topic) => [topic.metadata.name, topic]));
|
|
632
|
-
for (const [name, constraint] of Object.entries(constraints)) {
|
|
633
|
-
const topic = directTopics.get(name);
|
|
634
|
-
if (topic === void 0) throw new Error(`${eventName} topic "${name}" is not directly filterable`);
|
|
635
|
-
const { metadata, index } = topic;
|
|
636
|
-
const values = filterValues(constraint, eventName, metadata);
|
|
637
|
-
const key = `topic${index + 1}`;
|
|
638
|
-
filterTopics[key] = values.map((value) => encodeTopic(metadata, value, eventName));
|
|
639
|
-
}
|
|
640
|
-
}
|
|
585
|
+
case _thruputchain_protocol.AuthClaimType.kPubkeySignature: {
|
|
586
|
+
const { pubkey_hash, nonce } = claim.pubkey_preimage;
|
|
587
|
+
if (!(pubkey_hash instanceof Uint8Array) || pubkey_hash.length !== 32) throw new TypeError("Assumed pubkey hash must contain 32 bytes");
|
|
588
|
+
require_codec.serializeU64(nonce, "accountNonce");
|
|
641
589
|
return {
|
|
642
|
-
|
|
643
|
-
|
|
590
|
+
kind: "pubkeySignature",
|
|
591
|
+
pubkeyHash: pubkey_hash.slice(),
|
|
592
|
+
accountNonce: nonce
|
|
644
593
|
};
|
|
645
|
-
},
|
|
646
|
-
allFromReceipt(receipt, options) {
|
|
647
|
-
const matches = [];
|
|
648
|
-
for (const event of receipt.events) {
|
|
649
|
-
if (!bytesEqual(event.address, options.address)) continue;
|
|
650
|
-
const decoded = descriptor.decode(event);
|
|
651
|
-
if (decoded !== null) matches.push({
|
|
652
|
-
...decoded,
|
|
653
|
-
address: (0, _thruputchain_protocol.contractAddress)(event.address.slice())
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
|
-
return matches;
|
|
657
|
-
},
|
|
658
|
-
singleFromReceipt(receipt, options) {
|
|
659
|
-
const matches = descriptor.allFromReceipt(receipt, options);
|
|
660
|
-
const match = matches[0];
|
|
661
|
-
if (matches.length !== 1 || match === void 0) throw new Error(`Expected exactly one ${eventName} event, got ${matches.length}`);
|
|
662
|
-
return match;
|
|
663
594
|
}
|
|
664
|
-
|
|
665
|
-
|
|
595
|
+
default: throw new TypeError("Unsupported authorization assumption");
|
|
596
|
+
}
|
|
666
597
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
598
|
+
//#endregion
|
|
599
|
+
//#region src/contract/method.ts
|
|
600
|
+
/** Error thrown by the view API for aborts, faults, or unsupported outcomes. */
|
|
601
|
+
var ContractViewError = class extends Error {
|
|
602
|
+
outcome;
|
|
603
|
+
constructor(outcome) {
|
|
604
|
+
const selector = outcome.kind === "abort" ? exceptionSelector(outcome.exception) : void 0;
|
|
605
|
+
super(outcome.kind === "abort" ? `Contract view aborted with ${formatException(outcome.exception, selector)}` : outcome.kind === "unsupported" ? "Unsupported contract simulation outcome" : "Contract view faulted");
|
|
606
|
+
this.name = "ContractViewError";
|
|
607
|
+
this.outcome = outcome;
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
/** Error thrown when a root contract receipt does not produce a typed return. */
|
|
611
|
+
var ContractReceiptError = class extends Error {
|
|
612
|
+
outcome;
|
|
613
|
+
constructor(outcome) {
|
|
614
|
+
const selector = outcome.kind === "abort" ? exceptionSelector(outcome.exception) : void 0;
|
|
615
|
+
super(outcome.kind === "abort" ? `Contract transaction ${(0, _thruputchain_protocol_utils.toBase58)(outcome.transactionHash)} aborted with ${formatException(outcome.exception, selector)}` : `Contract transaction ${(0, _thruputchain_protocol_utils.toBase58)(outcome.transactionHash)} did not return a supported successful result`);
|
|
616
|
+
this.name = "ContractReceiptError";
|
|
617
|
+
this.outcome = outcome;
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
function formatException(exception, selector) {
|
|
621
|
+
if (exception && typeof exception === "object" && "name" in exception && typeof exception.name === "string" && "value" in exception) {
|
|
622
|
+
const value = typeof exception.value === "bigint" ? `${exception.value}n` : JSON.stringify(exception.value, (_, v) => typeof v === "bigint" ? `${v}n` : v);
|
|
623
|
+
const variant = "variant" in exception && typeof exception.variant === "string" ? `.${exception.variant}` : "";
|
|
624
|
+
return `${exception.name}${variant} (${value})`;
|
|
625
|
+
}
|
|
626
|
+
return `unknown selector ${selector === void 0 ? "unknown" : `0x${selector.toString(16)}`}`;
|
|
680
627
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
for (let index = 0; index < left.length; index++) if (left[index] !== right[index]) return false;
|
|
685
|
-
return true;
|
|
628
|
+
function exceptionSelector(exception) {
|
|
629
|
+
if (!exception || typeof exception !== "object" || !("selector" in exception)) return;
|
|
630
|
+
return typeof exception.selector === "bigint" ? exception.selector : void 0;
|
|
686
631
|
}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
if (!Array.isArray(values) || values.length === 0) throw new Error(`${eventName} topic "${topic.name}" oneOf must not be empty`);
|
|
691
|
-
if (values.length > maxFilterTopicValues) throw new Error(`${eventName} topic "${topic.name}" oneOf supports at most ${maxFilterTopicValues} values`);
|
|
692
|
-
return values;
|
|
693
|
-
}
|
|
694
|
-
return [constraint];
|
|
632
|
+
/** Build one generated method descriptor from direct ABI metadata. */
|
|
633
|
+
function defineContractMethod(definition) {
|
|
634
|
+
return defineContractMethodWithExceptions(definition, resolveContractExceptions(definition));
|
|
695
635
|
}
|
|
696
|
-
function
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
function encodeTopic(topic, value, eventName) {
|
|
709
|
-
switch (topic.representation) {
|
|
710
|
-
case "address":
|
|
711
|
-
case "fixedBytes32": return (0, _thruputchain_xdr.fixedBytes)(requireBytes(value, eventName, topic.name), 32);
|
|
712
|
-
case "hash": throw new Error(`${eventName} topic "${topic.name}" is not directly filterable`);
|
|
713
|
-
default: {
|
|
714
|
-
const writer = new _thruputchain_xdr.CxsWriter();
|
|
715
|
-
try {
|
|
716
|
-
if (topic.representation === "bool") {
|
|
717
|
-
if (typeof value !== "boolean") throw new TypeError("Bool expected boolean");
|
|
718
|
-
value = value ? 1n : 0n;
|
|
719
|
-
}
|
|
720
|
-
topic.codec.encode(writer, value);
|
|
721
|
-
assertScalarRange(topic, value);
|
|
722
|
-
} catch (cause) {
|
|
723
|
-
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
724
|
-
throw new Error(`${eventName} topic "${topic.name}" failed to encode${detail}`, { cause });
|
|
725
|
-
}
|
|
726
|
-
return (0, _thruputchain_xdr.fixedBytes)(writer.finish(), 32);
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
function assertScalarRange(topic, value) {
|
|
731
|
-
if (value < topic.min || value > topic.max) throw new RangeError(`topic "${topic.name}" is outside the ${topic.representation} range`);
|
|
732
|
-
}
|
|
733
|
-
function requireBytes(value, eventName, topicName) {
|
|
734
|
-
if (!(value instanceof Uint8Array)) throw new TypeError(`${eventName} topic "${topicName}" must be bytes`);
|
|
735
|
-
return value;
|
|
736
|
-
}
|
|
737
|
-
function assertEventSize(topics, data) {
|
|
738
|
-
const size = topics.reduce((total, topic) => total + topic.length, data.length);
|
|
739
|
-
const limit = Number(_thruputchain_protocol.kMaxEventLength);
|
|
740
|
-
if (size > limit) throw new Error(`serialized event size ${size} exceeds ${limit} bytes`);
|
|
741
|
-
}
|
|
742
|
-
function assertUniqueNames(values, label) {
|
|
743
|
-
const names = /* @__PURE__ */ new Set();
|
|
744
|
-
for (const value of values) {
|
|
745
|
-
if (names.has(value.name)) throw new Error(`Duplicate ${label} name "${value.name}"`);
|
|
746
|
-
names.add(value.name);
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
function defineRecordValue(record, name, value) {
|
|
750
|
-
Object.defineProperty(record, name, {
|
|
751
|
-
configurable: true,
|
|
752
|
-
enumerable: true,
|
|
753
|
-
value,
|
|
754
|
-
writable: true
|
|
755
|
-
});
|
|
756
|
-
}
|
|
757
|
-
//#endregion
|
|
758
|
-
//#region src/actions/viewFreshness.ts
|
|
759
|
-
/** Reject a successful view that violates the caller's snapshot constraint. */
|
|
760
|
-
function assertViewFreshness(height, minHeight) {
|
|
761
|
-
if (minHeight !== void 0 && height < minHeight) throw new Error(`View response height ${height} is below the requested minimum ${minHeight}`);
|
|
762
|
-
}
|
|
763
|
-
//#endregion
|
|
764
|
-
//#region src/actions/viewAuthClaim.ts
|
|
765
|
-
function u32(value, name) {
|
|
766
|
-
require_codec.serializeU64(value, name);
|
|
767
|
-
if (value > 4294967295n) throw new RangeError(`${name} must fit uint32`);
|
|
768
|
-
return Number(value);
|
|
769
|
-
}
|
|
770
|
-
/** Project public authorization assumptions; this neither signs nor verifies authorization. */
|
|
771
|
-
function toAssumedAuth(claim) {
|
|
772
|
-
switch (claim.claim_type) {
|
|
773
|
-
case _thruputchain_protocol.AuthClaimType.kSourceAddressAuthData: return {
|
|
774
|
-
kind: "sourceAddress",
|
|
775
|
-
allowedKeys: u32(claim.source_address_claim.allowed_keys, "allowedKeys"),
|
|
776
|
-
requiredWeight: u32(claim.source_address_claim.required_weight, "requiredWeight")
|
|
777
|
-
};
|
|
778
|
-
case _thruputchain_protocol.AuthClaimType.kPubkeySignature: {
|
|
779
|
-
const { pubkey_hash, nonce } = claim.pubkey_preimage;
|
|
780
|
-
if (!(pubkey_hash instanceof Uint8Array) || pubkey_hash.length !== 32) throw new TypeError("Assumed pubkey hash must contain 32 bytes");
|
|
781
|
-
require_codec.serializeU64(nonce, "accountNonce");
|
|
782
|
-
return {
|
|
783
|
-
kind: "pubkeySignature",
|
|
784
|
-
pubkeyHash: pubkey_hash.slice(),
|
|
785
|
-
accountNonce: nonce
|
|
786
|
-
};
|
|
787
|
-
}
|
|
788
|
-
default: throw new TypeError("Unsupported authorization assumption");
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
//#endregion
|
|
792
|
-
//#region src/contract/method.ts
|
|
793
|
-
/** Error thrown by the view API for aborts, faults, or unsupported outcomes. */
|
|
794
|
-
var ContractViewError = class extends Error {
|
|
795
|
-
outcome;
|
|
796
|
-
constructor(outcome) {
|
|
797
|
-
const selector = outcome.kind === "abort" ? exceptionSelector(outcome.exception) : void 0;
|
|
798
|
-
super(outcome.kind === "abort" ? `Contract view aborted with ${formatException(outcome.exception, selector)}` : outcome.kind === "unsupported" ? "Unsupported contract simulation outcome" : "Contract view faulted");
|
|
799
|
-
this.name = "ContractViewError";
|
|
800
|
-
this.outcome = outcome;
|
|
801
|
-
}
|
|
802
|
-
};
|
|
803
|
-
/** Error thrown when a root contract receipt does not produce a typed return. */
|
|
804
|
-
var ContractReceiptError = class extends Error {
|
|
805
|
-
outcome;
|
|
806
|
-
constructor(outcome) {
|
|
807
|
-
const selector = outcome.kind === "abort" ? exceptionSelector(outcome.exception) : void 0;
|
|
808
|
-
super(outcome.kind === "abort" ? `Contract transaction ${(0, _thruputchain_protocol_utils.toBase58)(outcome.transactionHash)} aborted with ${formatException(outcome.exception, selector)}` : `Contract transaction ${(0, _thruputchain_protocol_utils.toBase58)(outcome.transactionHash)} did not return a supported successful result`);
|
|
809
|
-
this.name = "ContractReceiptError";
|
|
810
|
-
this.outcome = outcome;
|
|
811
|
-
}
|
|
812
|
-
};
|
|
813
|
-
function formatException(exception, selector) {
|
|
814
|
-
if (exception && typeof exception === "object" && "name" in exception && typeof exception.name === "string" && "value" in exception) {
|
|
815
|
-
const value = typeof exception.value === "bigint" ? `${exception.value}n` : JSON.stringify(exception.value, (_, v) => typeof v === "bigint" ? `${v}n` : v);
|
|
816
|
-
const variant = "variant" in exception && typeof exception.variant === "string" ? `.${exception.variant}` : "";
|
|
817
|
-
return `${exception.name}${variant} (${value})`;
|
|
636
|
+
function resolveContractExceptions(definition) {
|
|
637
|
+
const exceptions = /* @__PURE__ */ new Map();
|
|
638
|
+
for (const metadata of definition.throws) {
|
|
639
|
+
if (exceptions.has(metadata.selector)) throw new Error(`Duplicate contract exception selector ${metadata.selector}`);
|
|
640
|
+
exceptions.set(metadata.selector, {
|
|
641
|
+
name: metadata.name,
|
|
642
|
+
codec: resolveContextCodec(definition.codecs, metadata.type),
|
|
643
|
+
variant: (value) => contextEnumVariant(definition.codecs, metadata.type, value)
|
|
644
|
+
});
|
|
818
645
|
}
|
|
819
|
-
return
|
|
646
|
+
return exceptions;
|
|
820
647
|
}
|
|
821
|
-
function
|
|
822
|
-
if (!exception || typeof exception !== "object" || !("selector" in exception)) return;
|
|
823
|
-
return typeof exception.selector === "bigint" ? exception.selector : void 0;
|
|
824
|
-
}
|
|
825
|
-
/** Build one generated method descriptor from direct ABI metadata. */
|
|
826
|
-
function defineContractMethod(definition) {
|
|
648
|
+
function defineContractMethodWithExceptions(definition, exceptionBySelector) {
|
|
827
649
|
const entrypoint = copyEntrypoint(definition.entrypoint);
|
|
828
650
|
const effect = definition.effect ?? null;
|
|
829
651
|
if (effect !== null && ![
|
|
@@ -844,15 +666,6 @@ function defineContractMethod(definition) {
|
|
|
844
666
|
codec: resolveContextCodec(definition.codecs, argument.type)
|
|
845
667
|
}));
|
|
846
668
|
const returnCodec = resolveContextCodec(definition.codecs, definition.returnType);
|
|
847
|
-
const exceptionBySelector = /* @__PURE__ */ new Map();
|
|
848
|
-
for (const metadata of definition.throws) {
|
|
849
|
-
if (exceptionBySelector.has(metadata.selector)) throw new Error(`Duplicate contract exception selector ${metadata.selector}`);
|
|
850
|
-
exceptionBySelector.set(metadata.selector, {
|
|
851
|
-
name: metadata.name,
|
|
852
|
-
codec: resolveContextCodec(definition.codecs, metadata.type),
|
|
853
|
-
variant: (value) => contextEnumVariant(definition.codecs, metadata.type, value)
|
|
854
|
-
});
|
|
855
|
-
}
|
|
856
669
|
const method = {
|
|
857
670
|
name,
|
|
858
671
|
effect,
|
|
@@ -893,7 +706,7 @@ function defineContractMethod(definition) {
|
|
|
893
706
|
};
|
|
894
707
|
},
|
|
895
708
|
decodeArgs(data) {
|
|
896
|
-
const reader = new _thruputchain_xdr.CxsReader(data);
|
|
709
|
+
const reader = new _thruputchain_xdr.CxsReader(data, contractValueLimits(data.length));
|
|
897
710
|
const args = Object.fromEntries(argumentCodecs.map((argument) => {
|
|
898
711
|
try {
|
|
899
712
|
return [argument.publicName, argument.codec.decode(reader)];
|
|
@@ -1040,7 +853,7 @@ function encodeArguments(argumentsAndCodecs, args) {
|
|
|
1040
853
|
}
|
|
1041
854
|
function decodeException(data, exceptionBySelector) {
|
|
1042
855
|
try {
|
|
1043
|
-
const reader = new _thruputchain_xdr.CxsReader(data);
|
|
856
|
+
const reader = new _thruputchain_xdr.CxsReader(data, contractValueLimits(data.length));
|
|
1044
857
|
const selector = reader.readUint64();
|
|
1045
858
|
const declared = exceptionBySelector.get(selector);
|
|
1046
859
|
if (!declared) return {
|
|
@@ -1063,6 +876,560 @@ function decodeException(data, exceptionBySelector) {
|
|
|
1063
876
|
}
|
|
1064
877
|
}
|
|
1065
878
|
//#endregion
|
|
879
|
+
//#region src/contract/specMethod.ts
|
|
880
|
+
/**
|
|
881
|
+
* Decode a selector and arguments with the generated-binding codecs.
|
|
882
|
+
* Unknown selectors return null; malformed known inputs throw. The caller must
|
|
883
|
+
* establish the spec's identity. A current spec does not prove historical code.
|
|
884
|
+
*/
|
|
885
|
+
function createContractCallDecoder(spec) {
|
|
886
|
+
const snapshot = structuredClone(spec);
|
|
887
|
+
const codecs = createContractCodecContext(snapshot.type_defs);
|
|
888
|
+
const methods = /* @__PURE__ */ new Map();
|
|
889
|
+
for (const iface of snapshot.interfaces) {
|
|
890
|
+
const throws = iface.throws.map((exception) => ({
|
|
891
|
+
selector: exception.selector,
|
|
892
|
+
name: null,
|
|
893
|
+
type: exception.ty
|
|
894
|
+
}));
|
|
895
|
+
const exceptions = resolveContractExceptions({
|
|
896
|
+
throws,
|
|
897
|
+
codecs
|
|
898
|
+
});
|
|
899
|
+
for (const method of iface.methods) {
|
|
900
|
+
const key = deriveEntrypointID(iface.name, iface.vers, method.name).join(",");
|
|
901
|
+
if (methods.has(key)) throw new ContractDecodeError("Duplicate method selector in contract spec");
|
|
902
|
+
if (new Set(method.args.map((arg) => arg.name)).size !== method.args.length) throw new ContractDecodeError("Duplicate argument name in contract spec");
|
|
903
|
+
const descriptor = defineContractMethodWithExceptions({
|
|
904
|
+
interfaceName: iface.name,
|
|
905
|
+
interfaceVersion: iface.vers,
|
|
906
|
+
publicName: method.name,
|
|
907
|
+
abiName: method.name,
|
|
908
|
+
entrypoint: deriveEntrypointID(iface.name, iface.vers, method.name),
|
|
909
|
+
effect: method.effect,
|
|
910
|
+
args: method.args.map((arg) => ({
|
|
911
|
+
abiName: arg.name,
|
|
912
|
+
publicName: arg.name,
|
|
913
|
+
type: arg.ty
|
|
914
|
+
})),
|
|
915
|
+
returnType: method.return_type,
|
|
916
|
+
throws,
|
|
917
|
+
codecs
|
|
918
|
+
}, exceptions);
|
|
919
|
+
methods.set(key, {
|
|
920
|
+
interfaceName: iface.name,
|
|
921
|
+
interfaceVersion: iface.vers,
|
|
922
|
+
methodName: method.name,
|
|
923
|
+
method: descriptor
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
return (invocation) => {
|
|
928
|
+
const found = methods.get(invocation.entrypoint.join(","));
|
|
929
|
+
if (!found) return null;
|
|
930
|
+
return {
|
|
931
|
+
...found,
|
|
932
|
+
args: found.method.decodeArgs(invocation.calldata) || void 0
|
|
933
|
+
};
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
//#endregion
|
|
937
|
+
//#region src/contract/hash.ts
|
|
938
|
+
/**
|
|
939
|
+
* Return the network identity of a protocol contract.
|
|
940
|
+
*
|
|
941
|
+
* The hash covers the canonical encoded `Contract`, including its type and
|
|
942
|
+
* payload framing. Hashing only the raw artifact bytes produces a different
|
|
943
|
+
* value and therefore a different derived address.
|
|
944
|
+
*
|
|
945
|
+
* @example
|
|
946
|
+
* ```ts
|
|
947
|
+
* import {
|
|
948
|
+
* deriveContractAddress,
|
|
949
|
+
* hashContract,
|
|
950
|
+
* } from "@thruputchain/client";
|
|
951
|
+
*
|
|
952
|
+
* const contractHash = hashContract(contract);
|
|
953
|
+
* const address = deriveContractAddress(wallet, deploymentNonce, contractHash);
|
|
954
|
+
* ```
|
|
955
|
+
*/
|
|
956
|
+
function hashContract(contract) {
|
|
957
|
+
return (0, _thruputchain_protocol.sha256Hash)((0, _noble_hashes_sha2_js.sha256)((0, _thruputchain_protocol.serialize)(contract, _thruputchain_protocol.codec.Contract)));
|
|
958
|
+
}
|
|
959
|
+
//#endregion
|
|
960
|
+
//#region src/contract/event.ts
|
|
961
|
+
/**
|
|
962
|
+
* Build a decoder from a parsed spec using the same codecs as generated bindings.
|
|
963
|
+
* Unknown signatures return null; malformed known events throw. The caller must
|
|
964
|
+
* establish that this spec belongs to the contract that emitted the event.
|
|
965
|
+
*/
|
|
966
|
+
function createContractEventDecoder(spec) {
|
|
967
|
+
const codecs = createContractCodecContext(spec.type_defs);
|
|
968
|
+
const signatures = /* @__PURE__ */ new Set();
|
|
969
|
+
const events = spec.events.map((event) => {
|
|
970
|
+
const key = event.signature.join(",");
|
|
971
|
+
if (signatures.has(key)) throw new ContractDecodeError("Duplicate event signature in contract spec");
|
|
972
|
+
signatures.add(key);
|
|
973
|
+
return defineContractEvent({
|
|
974
|
+
name: formatSpecId(event.name),
|
|
975
|
+
signature: event.signature,
|
|
976
|
+
topics: event.topics.map((field) => ({
|
|
977
|
+
name: field.name,
|
|
978
|
+
type: field.ty,
|
|
979
|
+
representation: topicRepresentation(field.ty)
|
|
980
|
+
})),
|
|
981
|
+
fields: event.fields.map((field) => ({
|
|
982
|
+
name: field.name,
|
|
983
|
+
type: field.ty
|
|
984
|
+
})),
|
|
985
|
+
codecs
|
|
986
|
+
});
|
|
987
|
+
});
|
|
988
|
+
return (event) => {
|
|
989
|
+
for (const descriptor of events) {
|
|
990
|
+
const decoded = descriptor.decode(event);
|
|
991
|
+
if (decoded !== null) return decoded;
|
|
992
|
+
}
|
|
993
|
+
return null;
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
const maxFilterContractAddresses = 5;
|
|
997
|
+
const maxFilterTopicValues = 16;
|
|
998
|
+
/** Build one generated event descriptor from direct ABI metadata. */
|
|
999
|
+
function defineContractEvent(definition) {
|
|
1000
|
+
const eventName = definition.name;
|
|
1001
|
+
const signature = (0, _thruputchain_xdr.fixedBytes)(Uint8Array.from(definition.signature), 32);
|
|
1002
|
+
if (definition.topics.length > 2) throw new Error(`${eventName} declares ${definition.topics.length + 1} topics; protocol maximum is 3`);
|
|
1003
|
+
assertUniqueNames(definition.topics, `${eventName} topic`);
|
|
1004
|
+
assertUniqueNames(definition.fields, `${eventName} field`);
|
|
1005
|
+
const resolvedTopics = definition.topics.map((metadata) => {
|
|
1006
|
+
const expected = topicRepresentation(metadata.type);
|
|
1007
|
+
if (metadata.representation !== expected) throw new Error(`${eventName} topic "${metadata.name}" representation "${metadata.representation}" does not match its declared type; expected "${expected}"`);
|
|
1008
|
+
if (metadata.representation !== "address" && metadata.representation !== "fixedBytes32" && metadata.representation !== "hash") {
|
|
1009
|
+
const signed = metadata.representation.startsWith("i");
|
|
1010
|
+
const bits = metadata.representation === "bool" ? 1 : Number(metadata.representation.slice(1));
|
|
1011
|
+
const bound = 1n << BigInt(signed ? bits - 1 : bits);
|
|
1012
|
+
return {
|
|
1013
|
+
name: metadata.name,
|
|
1014
|
+
representation: metadata.representation,
|
|
1015
|
+
codec: resolveContextCodec(definition.codecs, { v: signed ? _thruputchain_contract_spec.ContractTypeKind.I256 : _thruputchain_contract_spec.ContractTypeKind.U256 }),
|
|
1016
|
+
min: signed ? -bound : 0n,
|
|
1017
|
+
max: bound - 1n
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
return {
|
|
1021
|
+
name: metadata.name,
|
|
1022
|
+
representation: metadata.representation
|
|
1023
|
+
};
|
|
1024
|
+
});
|
|
1025
|
+
const fields = definition.fields.map((metadata) => ({
|
|
1026
|
+
metadata: { ...metadata },
|
|
1027
|
+
codec: resolveContextCodec(definition.codecs, metadata.type)
|
|
1028
|
+
}));
|
|
1029
|
+
const descriptor = {
|
|
1030
|
+
get signature() {
|
|
1031
|
+
return Uint8Array.from(signature);
|
|
1032
|
+
},
|
|
1033
|
+
decode(event) {
|
|
1034
|
+
if (!bytesEqual(event.topics[0], signature)) return null;
|
|
1035
|
+
try {
|
|
1036
|
+
const expectedTopicCount = resolvedTopics.length + 1;
|
|
1037
|
+
if (event.topics.length !== expectedTopicCount) throw new Error(`expected ${expectedTopicCount} topics, got ${event.topics.length}`);
|
|
1038
|
+
assertEventSize(event.topics, event.data);
|
|
1039
|
+
const decodedTopics = {};
|
|
1040
|
+
for (const [index, metadata] of resolvedTopics.entries()) {
|
|
1041
|
+
const rawTopic = event.topics[index + 1];
|
|
1042
|
+
if (rawTopic === void 0) throw new Error(`missing topic "${metadata.name}"`);
|
|
1043
|
+
const value = decodeTopic(metadata, (0, _thruputchain_xdr.fixedBytes)(rawTopic, 32));
|
|
1044
|
+
defineRecordValue(decodedTopics, metadata.name, value);
|
|
1045
|
+
}
|
|
1046
|
+
const reader = new _thruputchain_xdr.CxsReader(event.data, contractValueLimits(event.data.length));
|
|
1047
|
+
const decodedFields = {};
|
|
1048
|
+
for (const field of fields) defineRecordValue(decodedFields, field.metadata.name, field.codec.decode(reader));
|
|
1049
|
+
if (reader.remaining !== 0) throw new Error(`${reader.remaining} trailing payload bytes`);
|
|
1050
|
+
return {
|
|
1051
|
+
name: eventName,
|
|
1052
|
+
topics: decodedTopics,
|
|
1053
|
+
fields: decodedFields
|
|
1054
|
+
};
|
|
1055
|
+
} catch (cause) {
|
|
1056
|
+
if (cause instanceof ContractDecodeError) throw cause;
|
|
1057
|
+
throw new ContractDecodeError(`Failed to decode ${eventName}${cause instanceof Error ? `: ${cause.message}` : ""}`, { cause });
|
|
1058
|
+
}
|
|
1059
|
+
},
|
|
1060
|
+
filter(input) {
|
|
1061
|
+
if (input.addresses.length === 0) throw new Error(`${eventName} filter requires a contract address`);
|
|
1062
|
+
if (input.addresses.length > maxFilterContractAddresses) throw new Error(`${eventName} filter supports at most ${maxFilterContractAddresses} contract addresses`);
|
|
1063
|
+
for (const address of input.addresses) (0, _thruputchain_xdr.fixedBytes)(address, 32);
|
|
1064
|
+
const filterTopics = { topic0: [Uint8Array.from(signature)] };
|
|
1065
|
+
const constraints = input.topics;
|
|
1066
|
+
if (constraints) {
|
|
1067
|
+
const directTopics = new Map(resolvedTopics.map((metadata, index) => ({
|
|
1068
|
+
metadata,
|
|
1069
|
+
index
|
|
1070
|
+
})).filter(({ metadata }) => metadata.representation !== "hash").map((topic) => [topic.metadata.name, topic]));
|
|
1071
|
+
for (const [name, constraint] of Object.entries(constraints)) {
|
|
1072
|
+
const topic = directTopics.get(name);
|
|
1073
|
+
if (topic === void 0) throw new Error(`${eventName} topic "${name}" is not directly filterable`);
|
|
1074
|
+
const { metadata, index } = topic;
|
|
1075
|
+
const values = filterValues(constraint, eventName, metadata);
|
|
1076
|
+
const key = `topic${index + 1}`;
|
|
1077
|
+
filterTopics[key] = values.map((value) => encodeTopic(metadata, value, eventName));
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
return {
|
|
1081
|
+
addresses: [...input.addresses],
|
|
1082
|
+
topics: filterTopics
|
|
1083
|
+
};
|
|
1084
|
+
},
|
|
1085
|
+
allFromReceipt(receipt, options) {
|
|
1086
|
+
const matches = [];
|
|
1087
|
+
for (const event of receipt.events) {
|
|
1088
|
+
if (!bytesEqual(event.address, options.address)) continue;
|
|
1089
|
+
const decoded = descriptor.decode(event);
|
|
1090
|
+
if (decoded !== null) matches.push({
|
|
1091
|
+
...decoded,
|
|
1092
|
+
address: (0, _thruputchain_protocol.contractAddress)(event.address.slice())
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
return matches;
|
|
1096
|
+
},
|
|
1097
|
+
singleFromReceipt(receipt, options) {
|
|
1098
|
+
const matches = descriptor.allFromReceipt(receipt, options);
|
|
1099
|
+
const match = matches[0];
|
|
1100
|
+
if (matches.length !== 1 || match === void 0) throw new Error(`Expected exactly one ${eventName} event, got ${matches.length}`);
|
|
1101
|
+
return match;
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
return descriptor;
|
|
1105
|
+
}
|
|
1106
|
+
/** Distinguish directly encoded indexed values from irreversible topic hashes. */
|
|
1107
|
+
function topicRepresentation(type) {
|
|
1108
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.Address) return "address";
|
|
1109
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.U32) return "u32";
|
|
1110
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.U64) return "u64";
|
|
1111
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.U128) return "u128";
|
|
1112
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.U256) return "u256";
|
|
1113
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.I32) return "i32";
|
|
1114
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.I64) return "i64";
|
|
1115
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.I128) return "i128";
|
|
1116
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.I256) return "i256";
|
|
1117
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.Bool) return "bool";
|
|
1118
|
+
if (type.v === _thruputchain_contract_spec.ContractTypeKind.Array && type.array.len === 32n && type.array.element?.v === _thruputchain_contract_spec.ContractTypeKind.U8) return "fixedBytes32";
|
|
1119
|
+
return "hash";
|
|
1120
|
+
}
|
|
1121
|
+
/** Compare public byte identifiers. This function is not constant-time. */
|
|
1122
|
+
function bytesEqual(left, right) {
|
|
1123
|
+
if (!left || left.length !== right.length) return false;
|
|
1124
|
+
for (let index = 0; index < left.length; index++) if (left[index] !== right[index]) return false;
|
|
1125
|
+
return true;
|
|
1126
|
+
}
|
|
1127
|
+
function filterValues(constraint, eventName, topic) {
|
|
1128
|
+
if (constraint && typeof constraint === "object" && !Array.isArray(constraint) && !(constraint instanceof Uint8Array) && "oneOf" in constraint) {
|
|
1129
|
+
const values = constraint.oneOf;
|
|
1130
|
+
if (!Array.isArray(values) || values.length === 0) throw new Error(`${eventName} topic "${topic.name}" oneOf must not be empty`);
|
|
1131
|
+
if (values.length > maxFilterTopicValues) throw new Error(`${eventName} topic "${topic.name}" oneOf supports at most ${maxFilterTopicValues} values`);
|
|
1132
|
+
return values;
|
|
1133
|
+
}
|
|
1134
|
+
return [constraint];
|
|
1135
|
+
}
|
|
1136
|
+
function decodeTopic(topic, bytes) {
|
|
1137
|
+
switch (topic.representation) {
|
|
1138
|
+
case "address": return (0, _thruputchain_protocol.contractAddress)(bytes);
|
|
1139
|
+
case "fixedBytes32":
|
|
1140
|
+
case "hash": return bytes;
|
|
1141
|
+
default: {
|
|
1142
|
+
const value = topic.codec.decode(new _thruputchain_xdr.CxsReader(bytes, contractValueLimits(bytes.length)));
|
|
1143
|
+
assertScalarRange(topic, value);
|
|
1144
|
+
return topic.representation === "bool" ? value === 1n : value;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
function encodeTopic(topic, value, eventName) {
|
|
1149
|
+
switch (topic.representation) {
|
|
1150
|
+
case "address":
|
|
1151
|
+
case "fixedBytes32": return (0, _thruputchain_xdr.fixedBytes)(requireBytes(value, eventName, topic.name), 32);
|
|
1152
|
+
case "hash": throw new Error(`${eventName} topic "${topic.name}" is not directly filterable`);
|
|
1153
|
+
default: {
|
|
1154
|
+
const writer = new _thruputchain_xdr.CxsWriter();
|
|
1155
|
+
try {
|
|
1156
|
+
if (topic.representation === "bool") {
|
|
1157
|
+
if (typeof value !== "boolean") throw new TypeError("Bool expected boolean");
|
|
1158
|
+
value = value ? 1n : 0n;
|
|
1159
|
+
}
|
|
1160
|
+
topic.codec.encode(writer, value);
|
|
1161
|
+
assertScalarRange(topic, value);
|
|
1162
|
+
} catch (cause) {
|
|
1163
|
+
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
1164
|
+
throw new Error(`${eventName} topic "${topic.name}" failed to encode${detail}`, { cause });
|
|
1165
|
+
}
|
|
1166
|
+
return (0, _thruputchain_xdr.fixedBytes)(writer.finish(), 32);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
function assertScalarRange(topic, value) {
|
|
1171
|
+
if (value < topic.min || value > topic.max) throw new RangeError(`topic "${topic.name}" is outside the ${topic.representation} range`);
|
|
1172
|
+
}
|
|
1173
|
+
function requireBytes(value, eventName, topicName) {
|
|
1174
|
+
if (!(value instanceof Uint8Array)) throw new TypeError(`${eventName} topic "${topicName}" must be bytes`);
|
|
1175
|
+
return value;
|
|
1176
|
+
}
|
|
1177
|
+
function assertEventSize(topics, data) {
|
|
1178
|
+
const size = topics.reduce((total, topic) => total + topic.length, data.length);
|
|
1179
|
+
const limit = Number(_thruputchain_protocol.kMaxEventLength);
|
|
1180
|
+
if (size > limit) throw new Error(`serialized event size ${size} exceeds ${limit} bytes`);
|
|
1181
|
+
}
|
|
1182
|
+
function assertUniqueNames(values, label) {
|
|
1183
|
+
const names = /* @__PURE__ */ new Set();
|
|
1184
|
+
for (const value of values) {
|
|
1185
|
+
if (names.has(value.name)) throw new Error(`Duplicate ${label} name "${value.name}"`);
|
|
1186
|
+
names.add(value.name);
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
function defineRecordValue(record, name, value) {
|
|
1190
|
+
Object.defineProperty(record, name, {
|
|
1191
|
+
configurable: true,
|
|
1192
|
+
enumerable: true,
|
|
1193
|
+
value,
|
|
1194
|
+
writable: true
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1197
|
+
//#endregion
|
|
1198
|
+
//#region src/contract/specValidation.ts
|
|
1199
|
+
/** Validate references after schema-1 wire preflight has bounded the structure. */
|
|
1200
|
+
function validateArtifactSpec(spec) {
|
|
1201
|
+
createContractCallDecoder(spec);
|
|
1202
|
+
createContractEventDecoder(spec);
|
|
1203
|
+
}
|
|
1204
|
+
//#endregion
|
|
1205
|
+
//#region src/contract/specPreflight.ts
|
|
1206
|
+
const contractSpecCodecLimits = {
|
|
1207
|
+
maxDepth: 256,
|
|
1208
|
+
maxWork: 1e6
|
|
1209
|
+
};
|
|
1210
|
+
/**
|
|
1211
|
+
* Schema-1 tooling limits, mirrored by tools' contract_spec/bounded.rs.
|
|
1212
|
+
* Count collection entries and type nodes (tuple elements count in both).
|
|
1213
|
+
* Type depth starts at one; named references are leaves, not expansions.
|
|
1214
|
+
* This bounds the generated decoder before it recurses or allocates lists.
|
|
1215
|
+
*/
|
|
1216
|
+
function preflightContractSpec(bytes) {
|
|
1217
|
+
if (bytes.length > 16 * 1024 * 1024) throw new ContractDecodeError("Contract spec exceeds byte limit (16777216)");
|
|
1218
|
+
const reader = new _thruputchain_xdr.XdrReader(bytes);
|
|
1219
|
+
let work = 1e5;
|
|
1220
|
+
const spend = (count) => {
|
|
1221
|
+
work -= count;
|
|
1222
|
+
if (work < 0) throw new ContractDecodeError("Contract spec exceeds work limit (100000)");
|
|
1223
|
+
};
|
|
1224
|
+
const list = (item) => {
|
|
1225
|
+
const count = Number(reader.readUint32());
|
|
1226
|
+
if (count > Math.floor(reader.remaining / 4)) throw new ContractDecodeError("Truncated contract spec collection");
|
|
1227
|
+
spend(count);
|
|
1228
|
+
for (let i = 0; i < count; i++) item();
|
|
1229
|
+
};
|
|
1230
|
+
const string = () => {
|
|
1231
|
+
_thruputchain_contract_spec.codec.ContractSpecString.decode(reader);
|
|
1232
|
+
};
|
|
1233
|
+
const id = () => {
|
|
1234
|
+
_thruputchain_contract_spec.codec.ContractSpecId.decode(reader);
|
|
1235
|
+
};
|
|
1236
|
+
const type = (depth = 1) => {
|
|
1237
|
+
if (depth > 64) throw new ContractDecodeError("Contract spec exceeds type nesting limit (64)");
|
|
1238
|
+
spend(1);
|
|
1239
|
+
const kind = Number(reader.readInt32());
|
|
1240
|
+
switch (kind) {
|
|
1241
|
+
case _thruputchain_contract_spec.ContractTypeKind.Void:
|
|
1242
|
+
case _thruputchain_contract_spec.ContractTypeKind.Bool:
|
|
1243
|
+
case _thruputchain_contract_spec.ContractTypeKind.U8:
|
|
1244
|
+
case _thruputchain_contract_spec.ContractTypeKind.U32:
|
|
1245
|
+
case _thruputchain_contract_spec.ContractTypeKind.I32:
|
|
1246
|
+
case _thruputchain_contract_spec.ContractTypeKind.U64:
|
|
1247
|
+
case _thruputchain_contract_spec.ContractTypeKind.I64:
|
|
1248
|
+
case _thruputchain_contract_spec.ContractTypeKind.U128:
|
|
1249
|
+
case _thruputchain_contract_spec.ContractTypeKind.I128:
|
|
1250
|
+
case _thruputchain_contract_spec.ContractTypeKind.U256:
|
|
1251
|
+
case _thruputchain_contract_spec.ContractTypeKind.I256:
|
|
1252
|
+
case _thruputchain_contract_spec.ContractTypeKind.String:
|
|
1253
|
+
case _thruputchain_contract_spec.ContractTypeKind.Bytes:
|
|
1254
|
+
case _thruputchain_contract_spec.ContractTypeKind.Address: return;
|
|
1255
|
+
case _thruputchain_contract_spec.ContractTypeKind.Option:
|
|
1256
|
+
type(depth + 1);
|
|
1257
|
+
return;
|
|
1258
|
+
case _thruputchain_contract_spec.ContractTypeKind.Vector:
|
|
1259
|
+
case _thruputchain_contract_spec.ContractTypeKind.Array:
|
|
1260
|
+
type(depth + 1);
|
|
1261
|
+
reader.readUint32();
|
|
1262
|
+
return;
|
|
1263
|
+
case _thruputchain_contract_spec.ContractTypeKind.Tuple:
|
|
1264
|
+
list(() => type(depth + 1));
|
|
1265
|
+
return;
|
|
1266
|
+
case _thruputchain_contract_spec.ContractTypeKind.Named:
|
|
1267
|
+
id();
|
|
1268
|
+
return;
|
|
1269
|
+
case _thruputchain_contract_spec.ContractTypeKind.SignedToken:
|
|
1270
|
+
if (reader.readBool()) type(depth + 1);
|
|
1271
|
+
return;
|
|
1272
|
+
default: throw new ContractDecodeError(`Unknown contract spec type ${kind}`);
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
const field = () => {
|
|
1276
|
+
string();
|
|
1277
|
+
type();
|
|
1278
|
+
};
|
|
1279
|
+
try {
|
|
1280
|
+
list(() => {
|
|
1281
|
+
string();
|
|
1282
|
+
string();
|
|
1283
|
+
list(() => {
|
|
1284
|
+
reader.readUint64();
|
|
1285
|
+
type();
|
|
1286
|
+
});
|
|
1287
|
+
list(() => {
|
|
1288
|
+
string();
|
|
1289
|
+
list(field);
|
|
1290
|
+
type();
|
|
1291
|
+
if (reader.readBool()) _thruputchain_contract_spec.codec.ContractMethodEffect.decode(reader);
|
|
1292
|
+
});
|
|
1293
|
+
});
|
|
1294
|
+
list(() => {
|
|
1295
|
+
const kind = Number(reader.readInt32());
|
|
1296
|
+
id();
|
|
1297
|
+
switch (kind) {
|
|
1298
|
+
case _thruputchain_contract_spec.ContractTypeDefKind.TypeDefStruct:
|
|
1299
|
+
list(field);
|
|
1300
|
+
break;
|
|
1301
|
+
case _thruputchain_contract_spec.ContractTypeDefKind.TypeDefEnum:
|
|
1302
|
+
list(() => {
|
|
1303
|
+
_thruputchain_contract_spec.codec.ContractEnumVariant.decode(reader);
|
|
1304
|
+
});
|
|
1305
|
+
break;
|
|
1306
|
+
case _thruputchain_contract_spec.ContractTypeDefKind.TypeDefUnion:
|
|
1307
|
+
list(() => {
|
|
1308
|
+
string();
|
|
1309
|
+
reader.readInt32();
|
|
1310
|
+
const kind = Number(reader.readInt32());
|
|
1311
|
+
switch (kind) {
|
|
1312
|
+
case _thruputchain_contract_spec.ContractUnionCaseKind.UnionCaseVoid: break;
|
|
1313
|
+
case _thruputchain_contract_spec.ContractUnionCaseKind.UnionCaseTuple:
|
|
1314
|
+
list(() => type());
|
|
1315
|
+
break;
|
|
1316
|
+
case _thruputchain_contract_spec.ContractUnionCaseKind.UnionCaseStruct:
|
|
1317
|
+
list(field);
|
|
1318
|
+
break;
|
|
1319
|
+
default: throw new ContractDecodeError(`Unknown contract spec union case ${kind}`);
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1322
|
+
break;
|
|
1323
|
+
default: throw new ContractDecodeError(`Unknown contract spec definition ${kind}`);
|
|
1324
|
+
}
|
|
1325
|
+
});
|
|
1326
|
+
list(() => {
|
|
1327
|
+
id();
|
|
1328
|
+
reader.readFixedOpaque(32);
|
|
1329
|
+
list(field);
|
|
1330
|
+
list(field);
|
|
1331
|
+
});
|
|
1332
|
+
if (reader.remaining !== 0) throw new ContractDecodeError("Trailing contract spec bytes");
|
|
1333
|
+
} catch (cause) {
|
|
1334
|
+
if (cause instanceof ContractDecodeError) throw cause;
|
|
1335
|
+
throw new ContractDecodeError("Invalid contract spec XDR", { cause });
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
//#endregion
|
|
1339
|
+
//#region src/contract/artifact.ts
|
|
1340
|
+
/**
|
|
1341
|
+
* Verify complete Contract XDR against the requested hash, then read its spec.
|
|
1342
|
+
* Rejects malformed known envelopes. Unknown versions remain explicit results.
|
|
1343
|
+
* Reads v5 as having no spec. In v6, validates all extension framing and skips
|
|
1344
|
+
* unknown kinds. Only the contract-spec extension body is interpreted.
|
|
1345
|
+
* This proves byte identity, not deployment, executable validity, or spec truth.
|
|
1346
|
+
*/
|
|
1347
|
+
function readContractArtifactSpec(contractXdr, expectedHash) {
|
|
1348
|
+
try {
|
|
1349
|
+
return readArtifact(contractXdr, expectedHash);
|
|
1350
|
+
} catch (cause) {
|
|
1351
|
+
throw new ContractVerificationError(cause instanceof Error ? cause.message : "Invalid contract artifact", { cause });
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
function readArtifact(contractXdr, expectedHash) {
|
|
1355
|
+
const bytes = contractXdr.slice();
|
|
1356
|
+
if (!bytesEqual((0, _noble_hashes_sha2_js.sha256)(bytes), expectedHash)) throw new Error("Contract artifact hash mismatch");
|
|
1357
|
+
const contract = (0, _thruputchain_protocol.deserialize)(bytes, _thruputchain_protocol.codec.Contract);
|
|
1358
|
+
if (!bytesEqual((0, _thruputchain_protocol.serialize)(contract, _thruputchain_protocol.codec.Contract), bytes)) throw new Error("Noncanonical Contract XDR");
|
|
1359
|
+
if (contract.contract_type === _thruputchain_protocol.ContractType.kNative) return { kind: "native" };
|
|
1360
|
+
if (contract.contract_type !== _thruputchain_protocol.ContractType.kRv32) return {
|
|
1361
|
+
kind: "unsupportedContract",
|
|
1362
|
+
contractType: contract.contract_type
|
|
1363
|
+
};
|
|
1364
|
+
const artifact = contract.rv32_contract.data;
|
|
1365
|
+
if (artifact.length < 12 || !bytesEqual(artifact.subarray(0, 8), new TextEncoder().encode("GHRV32A1"))) throw new Error("Invalid RV32 artifact header");
|
|
1366
|
+
const header = new DataView(artifact.buffer, artifact.byteOffset, artifact.byteLength);
|
|
1367
|
+
const formatVersion = header.getUint32(8, true);
|
|
1368
|
+
if (formatVersion !== 5 && formatVersion !== 6) return {
|
|
1369
|
+
kind: "unsupportedFormat",
|
|
1370
|
+
formatVersion
|
|
1371
|
+
};
|
|
1372
|
+
if (artifact.length < 128 || header.getUint32(12, true) !== 128) throw new Error("Invalid RV32 header size");
|
|
1373
|
+
const codeSize = header.getUint32(52, true);
|
|
1374
|
+
const memorySize = header.getUint32(56, true);
|
|
1375
|
+
const sectionsEnd = 128 + codeSize + memorySize;
|
|
1376
|
+
if (sectionsEnd > artifact.length) throw new Error("Invalid RV32 section layout");
|
|
1377
|
+
if (formatVersion === 5) {
|
|
1378
|
+
if (sectionsEnd !== artifact.length) throw new Error("Invalid RV32 v5 section layout");
|
|
1379
|
+
return { kind: "absent" };
|
|
1380
|
+
}
|
|
1381
|
+
const body = readSpecExtension(artifact.subarray(sectionsEnd));
|
|
1382
|
+
if (body === void 0) return { kind: "absent" };
|
|
1383
|
+
const reader = new _thruputchain_xdr.XdrReader(body);
|
|
1384
|
+
const schemaVersion = Number(reader.readUint32());
|
|
1385
|
+
const specBytes = reader.readVarOpaque(reader.remaining);
|
|
1386
|
+
if (reader.remaining !== 0) throw new Error("Trailing bytes in contract spec extension");
|
|
1387
|
+
if (schemaVersion !== 1) return {
|
|
1388
|
+
kind: "unsupportedSchema",
|
|
1389
|
+
schemaVersion,
|
|
1390
|
+
bytes: specBytes
|
|
1391
|
+
};
|
|
1392
|
+
preflightContractSpec(specBytes);
|
|
1393
|
+
const spec = (0, _thruputchain_protocol.deserialize)(specBytes, _thruputchain_contract_spec.codec.ContractSpec, contractSpecCodecLimits);
|
|
1394
|
+
validateArtifactSpec(spec);
|
|
1395
|
+
if (!bytesEqual((0, _thruputchain_protocol.serialize)(spec, _thruputchain_contract_spec.codec.ContractSpec, contractSpecCodecLimits), specBytes)) throw new Error("Noncanonical contract spec XDR");
|
|
1396
|
+
return {
|
|
1397
|
+
kind: "spec",
|
|
1398
|
+
schemaVersion,
|
|
1399
|
+
spec
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
function readSpecExtension(bytes) {
|
|
1403
|
+
const reader = new _thruputchain_xdr.XdrReader(bytes);
|
|
1404
|
+
const count = Number(reader.readUint32());
|
|
1405
|
+
if (count > Math.floor(reader.remaining / 8)) throw new Error("Truncated RV32 extension list");
|
|
1406
|
+
let previousKind = 0;
|
|
1407
|
+
let spec;
|
|
1408
|
+
for (let i = 0; i < count; i++) {
|
|
1409
|
+
const kind = Number(reader.readUint32());
|
|
1410
|
+
if (kind <= previousKind) throw new Error("RV32 extension kinds must be nonzero and strictly ascending");
|
|
1411
|
+
previousKind = kind;
|
|
1412
|
+
const body = reader.readVarOpaque(reader.remaining);
|
|
1413
|
+
if (kind === 1) spec = body;
|
|
1414
|
+
}
|
|
1415
|
+
if (reader.remaining !== 0) throw new Error("Trailing bytes in RV32 extension list");
|
|
1416
|
+
return spec;
|
|
1417
|
+
}
|
|
1418
|
+
//#endregion
|
|
1419
|
+
//#region src/derivation/contractAddress.ts
|
|
1420
|
+
/**
|
|
1421
|
+
* The on-chain address a deployment produces:
|
|
1422
|
+
* `sha256(txs(ContractDeploymentHashPreimage))` over the deployer, nonce, and
|
|
1423
|
+
* contract hash.
|
|
1424
|
+
*/
|
|
1425
|
+
function deriveContractAddress(deployer, nonce, contractHash) {
|
|
1426
|
+
return (0, _thruputchain_protocol.contractAddress)((0, _noble_hashes_sha2_js.sha256)((0, _thruputchain_protocol.serialize)({
|
|
1427
|
+
deployer_address: deployer,
|
|
1428
|
+
nonce,
|
|
1429
|
+
contract_hash: contractHash
|
|
1430
|
+
}, _thruputchain_protocol.codec.ContractDeploymentHashPreimage)));
|
|
1431
|
+
}
|
|
1432
|
+
//#endregion
|
|
1066
1433
|
//#region src/contract/instance.ts
|
|
1067
1434
|
/**
|
|
1068
1435
|
* Bind a generated interface to an address and public client.
|
|
@@ -1113,6 +1480,12 @@ Object.defineProperty(exports, "ContractReceiptError", {
|
|
|
1113
1480
|
return ContractReceiptError;
|
|
1114
1481
|
}
|
|
1115
1482
|
});
|
|
1483
|
+
Object.defineProperty(exports, "ContractVerificationError", {
|
|
1484
|
+
enumerable: true,
|
|
1485
|
+
get: function() {
|
|
1486
|
+
return ContractVerificationError;
|
|
1487
|
+
}
|
|
1488
|
+
});
|
|
1116
1489
|
Object.defineProperty(exports, "ContractViewError", {
|
|
1117
1490
|
enumerable: true,
|
|
1118
1491
|
get: function() {
|
|
@@ -1131,6 +1504,12 @@ Object.defineProperty(exports, "bytesEqual", {
|
|
|
1131
1504
|
return bytesEqual;
|
|
1132
1505
|
}
|
|
1133
1506
|
});
|
|
1507
|
+
Object.defineProperty(exports, "createContractCallDecoder", {
|
|
1508
|
+
enumerable: true,
|
|
1509
|
+
get: function() {
|
|
1510
|
+
return createContractCallDecoder;
|
|
1511
|
+
}
|
|
1512
|
+
});
|
|
1134
1513
|
Object.defineProperty(exports, "createContractCodec", {
|
|
1135
1514
|
enumerable: true,
|
|
1136
1515
|
get: function() {
|
|
@@ -1143,6 +1522,12 @@ Object.defineProperty(exports, "createContractCodecContext", {
|
|
|
1143
1522
|
return createContractCodecContext;
|
|
1144
1523
|
}
|
|
1145
1524
|
});
|
|
1525
|
+
Object.defineProperty(exports, "createContractEventDecoder", {
|
|
1526
|
+
enumerable: true,
|
|
1527
|
+
get: function() {
|
|
1528
|
+
return createContractEventDecoder;
|
|
1529
|
+
}
|
|
1530
|
+
});
|
|
1146
1531
|
Object.defineProperty(exports, "decodeCalldata", {
|
|
1147
1532
|
enumerable: true,
|
|
1148
1533
|
get: function() {
|
|
@@ -1167,6 +1552,12 @@ Object.defineProperty(exports, "deriveContractAddress", {
|
|
|
1167
1552
|
return deriveContractAddress;
|
|
1168
1553
|
}
|
|
1169
1554
|
});
|
|
1555
|
+
Object.defineProperty(exports, "deriveEntrypointID", {
|
|
1556
|
+
enumerable: true,
|
|
1557
|
+
get: function() {
|
|
1558
|
+
return deriveEntrypointID;
|
|
1559
|
+
}
|
|
1560
|
+
});
|
|
1170
1561
|
Object.defineProperty(exports, "encodeCalldata", {
|
|
1171
1562
|
enumerable: true,
|
|
1172
1563
|
get: function() {
|
|
@@ -1197,6 +1588,18 @@ Object.defineProperty(exports, "isSignedToken", {
|
|
|
1197
1588
|
return isSignedToken;
|
|
1198
1589
|
}
|
|
1199
1590
|
});
|
|
1591
|
+
Object.defineProperty(exports, "majorVersionFromSemver", {
|
|
1592
|
+
enumerable: true,
|
|
1593
|
+
get: function() {
|
|
1594
|
+
return majorVersionFromSemver;
|
|
1595
|
+
}
|
|
1596
|
+
});
|
|
1597
|
+
Object.defineProperty(exports, "readContractArtifactSpec", {
|
|
1598
|
+
enumerable: true,
|
|
1599
|
+
get: function() {
|
|
1600
|
+
return readContractArtifactSpec;
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1200
1603
|
Object.defineProperty(exports, "resolveCodec", {
|
|
1201
1604
|
enumerable: true,
|
|
1202
1605
|
get: function() {
|
|
@@ -1215,5 +1618,11 @@ Object.defineProperty(exports, "specIdKey", {
|
|
|
1215
1618
|
return specIdKey;
|
|
1216
1619
|
}
|
|
1217
1620
|
});
|
|
1621
|
+
Object.defineProperty(exports, "topicRepresentation", {
|
|
1622
|
+
enumerable: true,
|
|
1623
|
+
get: function() {
|
|
1624
|
+
return topicRepresentation;
|
|
1625
|
+
}
|
|
1626
|
+
});
|
|
1218
1627
|
|
|
1219
|
-
//# sourceMappingURL=contract-
|
|
1628
|
+
//# sourceMappingURL=contract-C7T2H2Oi.cjs.map
|