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