@arkade-os/sdk 0.5.12 → 0.5.14

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.
Files changed (106) hide show
  1. package/README.md +6 -0
  2. package/dist/adapters/expo.cjs +7 -7
  3. package/dist/adapters/expo.d.cts +2 -2
  4. package/dist/adapters/expo.d.ts +2 -2
  5. package/dist/adapters/expo.js +4 -4
  6. package/dist/{asyncStorageTaskQueue-DTNS2qfN.d.cts → asyncStorageTaskQueue-DzO5SBcm.d.cts} +2 -2
  7. package/dist/{asyncStorageTaskQueue-Bsty8W7d.d.ts → asyncStorageTaskQueue-p7zeFhA3.d.ts} +2 -2
  8. package/dist/{chunk-4OY5BCIE.js → chunk-4HZQSKWG.js} +628 -23
  9. package/dist/chunk-4HZQSKWG.js.map +1 -0
  10. package/dist/{chunk-4X4SY3RH.cjs → chunk-6J4GABF7.cjs} +28 -20
  11. package/dist/chunk-6J4GABF7.cjs.map +1 -0
  12. package/dist/{chunk-YAVF2PR6.cjs → chunk-6TBTL42M.cjs} +87 -2
  13. package/dist/chunk-6TBTL42M.cjs.map +1 -0
  14. package/dist/{chunk-5NY5EOS2.js → chunk-72EC3ASW.js} +4 -4
  15. package/dist/{chunk-5NY5EOS2.js.map → chunk-72EC3ASW.js.map} +1 -1
  16. package/dist/{chunk-QV7AOZZN.cjs → chunk-7LPT6AYH.cjs} +21 -21
  17. package/dist/{chunk-QV7AOZZN.cjs.map → chunk-7LPT6AYH.cjs.map} +1 -1
  18. package/dist/{chunk-JA4SYFC7.cjs → chunk-ACIQZLWL.cjs} +379 -20
  19. package/dist/chunk-ACIQZLWL.cjs.map +1 -0
  20. package/dist/{chunk-BA3GPO5C.js → chunk-AZHBGWVC.js} +5 -5
  21. package/dist/{chunk-BA3GPO5C.js.map → chunk-AZHBGWVC.js.map} +1 -1
  22. package/dist/{chunk-HNLC6F73.cjs → chunk-EVVLVFY7.cjs} +20 -20
  23. package/dist/{chunk-HNLC6F73.cjs.map → chunk-EVVLVFY7.cjs.map} +1 -1
  24. package/dist/{chunk-2YXVU5TK.js → chunk-GB67AGXG.js} +81 -3
  25. package/dist/chunk-GB67AGXG.js.map +1 -0
  26. package/dist/{chunk-WBZYYRAB.js → chunk-HWWSLCQN.js} +9724 -8151
  27. package/dist/chunk-HWWSLCQN.js.map +1 -0
  28. package/dist/{chunk-4TMJBLJT.js → chunk-J2JZZTOE.js} +20 -14
  29. package/dist/chunk-J2JZZTOE.js.map +1 -0
  30. package/dist/{chunk-3RO63OPW.js → chunk-LCGRZ24T.js} +4 -4
  31. package/dist/{chunk-3RO63OPW.js.map → chunk-LCGRZ24T.js.map} +1 -1
  32. package/dist/{chunk-CJUVKHK4.cjs → chunk-LQH7DACV.cjs} +10204 -8628
  33. package/dist/chunk-LQH7DACV.cjs.map +1 -0
  34. package/dist/{chunk-QDX2HUBK.cjs → chunk-MEIE2AEU.cjs} +11 -11
  35. package/dist/{chunk-QDX2HUBK.cjs.map → chunk-MEIE2AEU.cjs.map} +1 -1
  36. package/dist/{chunk-3ABF4EMM.js → chunk-X57WKONW.js} +369 -10
  37. package/dist/chunk-X57WKONW.js.map +1 -0
  38. package/dist/{chunk-C3NKBKRY.cjs → chunk-XZHTYYW5.cjs} +680 -68
  39. package/dist/chunk-XZHTYYW5.cjs.map +1 -0
  40. package/dist/compiler.cjs +3 -4
  41. package/dist/compiler.cjs.map +1 -1
  42. package/dist/compiler.d.cts +1 -1
  43. package/dist/compiler.d.ts +1 -1
  44. package/dist/compiler.js +2 -3
  45. package/dist/compiler.js.map +1 -1
  46. package/dist/contracts/handlers/index.cjs +15 -11
  47. package/dist/contracts/handlers/index.d.cts +5 -5
  48. package/dist/contracts/handlers/index.d.ts +5 -5
  49. package/dist/contracts/handlers/index.js +4 -4
  50. package/dist/{index-D_zLtU-F.d.ts → index-CaOBA9Ev.d.ts} +2 -2
  51. package/dist/{index-BBmZXXqk.d.cts → index-Cy57QdcL.d.cts} +589 -23
  52. package/dist/{index-DJ32LpfD.d.ts → index-Db5qzYRi.d.ts} +589 -23
  53. package/dist/{index-DenQfCn1.d.cts → index-Lq_u0hqj.d.cts} +2 -2
  54. package/dist/index.cjs +403 -368
  55. package/dist/index.d.cts +197 -117
  56. package/dist/index.d.ts +197 -117
  57. package/dist/index.js +6 -7
  58. package/dist/{program-DPzf5qsk.d.cts → program-ZWUAWy_9.d.cts} +1 -1
  59. package/dist/{program-38A2Twvj.d.ts → program-zdDO4cMt.d.ts} +1 -1
  60. package/dist/repositories/realm/index.cjs +39 -40
  61. package/dist/repositories/realm/index.cjs.map +1 -1
  62. package/dist/repositories/realm/index.d.cts +2 -2
  63. package/dist/repositories/realm/index.d.ts +2 -2
  64. package/dist/repositories/realm/index.js +7 -8
  65. package/dist/repositories/realm/index.js.map +1 -1
  66. package/dist/repositories/sqlite/index.cjs +37 -38
  67. package/dist/repositories/sqlite/index.cjs.map +1 -1
  68. package/dist/repositories/sqlite/index.d.cts +2 -2
  69. package/dist/repositories/sqlite/index.d.ts +2 -2
  70. package/dist/repositories/sqlite/index.js +7 -8
  71. package/dist/repositories/sqlite/index.js.map +1 -1
  72. package/dist/{taskRunner-BBGaN4cq.d.cts → taskRunner-DhZ-KQBW.d.cts} +2 -2
  73. package/dist/{taskRunner-Cck24OnC.d.ts → taskRunner-amPuilMW.d.ts} +2 -2
  74. package/dist/wallet/expo/background.cjs +16 -17
  75. package/dist/wallet/expo/background.cjs.map +1 -1
  76. package/dist/wallet/expo/background.d.cts +3 -3
  77. package/dist/wallet/expo/background.d.ts +3 -3
  78. package/dist/wallet/expo/background.js +8 -9
  79. package/dist/wallet/expo/background.js.map +1 -1
  80. package/dist/wallet/expo/index.cjs +17 -18
  81. package/dist/wallet/expo/index.cjs.map +1 -1
  82. package/dist/wallet/expo/index.d.cts +2 -2
  83. package/dist/wallet/expo/index.d.ts +2 -2
  84. package/dist/wallet/expo/index.js +7 -8
  85. package/dist/wallet/expo/index.js.map +1 -1
  86. package/dist/worker/expo/index.cjs +11 -12
  87. package/dist/worker/expo/index.cjs.map +1 -1
  88. package/dist/worker/expo/index.d.cts +4 -4
  89. package/dist/worker/expo/index.d.ts +4 -4
  90. package/dist/worker/expo/index.js +7 -8
  91. package/dist/worker/expo/index.js.map +1 -1
  92. package/package.json +1 -1
  93. package/dist/chunk-2YXVU5TK.js.map +0 -1
  94. package/dist/chunk-3ABF4EMM.js.map +0 -1
  95. package/dist/chunk-4OY5BCIE.js.map +0 -1
  96. package/dist/chunk-4TMJBLJT.js.map +0 -1
  97. package/dist/chunk-4X4SY3RH.cjs.map +0 -1
  98. package/dist/chunk-C3NKBKRY.cjs.map +0 -1
  99. package/dist/chunk-CJUVKHK4.cjs.map +0 -1
  100. package/dist/chunk-JA4SYFC7.cjs.map +0 -1
  101. package/dist/chunk-PHOJF7OK.cjs +0 -376
  102. package/dist/chunk-PHOJF7OK.cjs.map +0 -1
  103. package/dist/chunk-UXLETBNK.js +0 -373
  104. package/dist/chunk-UXLETBNK.js.map +0 -1
  105. package/dist/chunk-WBZYYRAB.js.map +0 -1
  106. package/dist/chunk-YAVF2PR6.cjs.map +0 -1
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkYAVF2PR6_cjs = require('./chunk-YAVF2PR6.cjs');
3
+ var chunk6TBTL42M_cjs = require('./chunk-6TBTL42M.cjs');
4
4
  var base = require('@scure/base');
5
5
  var btcSigner = require('@scure/btc-signer');
6
6
  var utils_js$1 = require('@scure/btc-signer/utils.js');
@@ -28,12 +28,6 @@ function _interopNamespace(e) {
28
28
 
29
29
  var P__namespace = /*#__PURE__*/_interopNamespace(P);
30
30
 
31
- // src/utils/keys.ts
32
- function toXOnly(key, label = "public key") {
33
- if (key.length === 32) return key;
34
- if (key.length === 33 && (key[0] === 2 || key[0] === 3)) return key.subarray(1);
35
- throw new Error(`${label} is not a compressed or x-only public key`);
36
- }
37
31
  var ARKADE_OP = {
38
32
  // Merkle Branch Verification (0xb3 — repurposed NOP4 slot)
39
33
  MERKLEBRANCHVERIFY: 179,
@@ -172,7 +166,7 @@ function getOpcodeValue(name) {
172
166
 
173
167
  // src/arkade/bignum.ts
174
168
  var bignum_exports = {};
175
- chunkYAVF2PR6_cjs.__export(bignum_exports, {
169
+ chunk6TBTL42M_cjs.__export(bignum_exports, {
176
170
  BIGNUM_MAX_BYTES: () => BIGNUM_MAX_BYTES,
177
171
  decode: () => decode,
178
172
  encode: () => encode
@@ -343,7 +337,7 @@ function arkadeWitnessHash(witness) {
343
337
  }
344
338
  function computeArkadeScriptPublicKey(pubKey, script) {
345
339
  const hash = arkadeScriptHash(script);
346
- const point = secp256k1_js.schnorr.utils.lift_x(utils_js.bytesToNumberBE(toXOnly(pubKey, "emulator key")));
340
+ const point = secp256k1_js.schnorr.utils.lift_x(utils_js.bytesToNumberBE(chunk6TBTL42M_cjs.toXOnly(pubKey, "emulator key")));
347
341
  const scalar = utils_js.bytesToNumberBE(hash) % secp256k1_js.secp256k1.Point.CURVE().n;
348
342
  const tweak = secp256k1_js.secp256k1.Point.BASE.multiply(scalar);
349
343
  return secp256k1_js.schnorr.utils.pointToBytes(point.add(tweak));
@@ -392,7 +386,7 @@ function resolveTimelockValue(value, args) {
392
386
  function resolveCsvTimelock(csv, args) {
393
387
  const value = resolveTimelockValue(csv.value, args);
394
388
  if (csv.type !== "sequence") return { type: csv.type, value };
395
- const timelock = chunkYAVF2PR6_cjs.sequenceToCanonicalTimelock(value);
389
+ const timelock = chunk6TBTL42M_cjs.sequenceToCanonicalTimelock(value);
396
390
  if (!timelock) {
397
391
  throw new Error(
398
392
  `csv '${csv.value}' resolved to ${value}, which is not a canonical BIP68 sequence`
@@ -547,27 +541,27 @@ function encodeTapscriptSegment(seg, pubkeys, args) {
547
541
  if (seg.csv) {
548
542
  const timelock = resolveCsvTimelock(seg.csv, args);
549
543
  if (seg.asm) {
550
- return chunkYAVF2PR6_cjs.ConditionCSVMultisigTapscript.encode({
544
+ return chunk6TBTL42M_cjs.ConditionCSVMultisigTapscript.encode({
551
545
  conditionScript: resolveAsm(seg.asm, args),
552
546
  timelock,
553
547
  pubkeys
554
548
  });
555
549
  }
556
- return chunkYAVF2PR6_cjs.CSVMultisigTapscript.encode({ timelock, pubkeys });
550
+ return chunk6TBTL42M_cjs.CSVMultisigTapscript.encode({ timelock, pubkeys });
557
551
  }
558
552
  if (seg.cltv !== void 0) {
559
- return chunkYAVF2PR6_cjs.CLTVMultisigTapscript.encode({
553
+ return chunk6TBTL42M_cjs.CLTVMultisigTapscript.encode({
560
554
  absoluteTimelock: resolveTimelockValue(seg.cltv, args),
561
555
  pubkeys
562
556
  });
563
557
  }
564
558
  if (seg.asm) {
565
- return chunkYAVF2PR6_cjs.ConditionMultisigTapscript.encode({
559
+ return chunk6TBTL42M_cjs.ConditionMultisigTapscript.encode({
566
560
  conditionScript: resolveAsm(seg.asm, args),
567
561
  pubkeys
568
562
  });
569
563
  }
570
- return chunkYAVF2PR6_cjs.MultisigTapscript.encode({ pubkeys });
564
+ return chunk6TBTL42M_cjs.MultisigTapscript.encode({ pubkeys });
571
565
  }
572
566
  function compileFunctions(program, args, keys) {
573
567
  if (program.version !== SUPPORTED_PROGRAM_VERSION) {
@@ -646,7 +640,7 @@ function compileFunctions(program, args, keys) {
646
640
  return { name: names[i], def, leafScript, arkadeScript, signerKeys: pubkeys };
647
641
  });
648
642
  }
649
- var ArkadeProgramScript = class extends chunkYAVF2PR6_cjs.VtxoScript {
643
+ var ArkadeProgramScript = class extends chunk6TBTL42M_cjs.VtxoScript {
650
644
  constructor(program, args, keys) {
651
645
  const partial = compileFunctions(program, args, keys);
652
646
  super(partial.map((p) => p.leafScript));
@@ -847,13 +841,377 @@ function deserializeArkadeContractParams(params) {
847
841
  ...params.emulatorKey ? { emulatorKey: base.hex.decode(params.emulatorKey) } : {}
848
842
  };
849
843
  }
844
+ var NATIVE_STRUCTS = {
845
+ AssetId: [
846
+ ["txid", "bytes32"],
847
+ ["gidx", "int"]
848
+ ],
849
+ Outpoint: [
850
+ ["txid", "bytes32"],
851
+ ["vout", "int"]
852
+ ],
853
+ ECPoint: [
854
+ ["x", "int"],
855
+ ["y", "int"]
856
+ ],
857
+ ScriptPubKey: [
858
+ ["program", "bytes"],
859
+ ["version", "int"]
860
+ ],
861
+ AssetEntry: [
862
+ ["assetId", "AssetId"],
863
+ ["amount", "int"]
864
+ ],
865
+ AssetLookup: [
866
+ ["amount", "int"],
867
+ ["found", "bool"]
868
+ ],
869
+ G2Point: [
870
+ ["xC1", "int"],
871
+ ["xC0", "int"],
872
+ ["yC1", "int"],
873
+ ["yC0", "int"]
874
+ ]
875
+ };
876
+ var HASH_OPCODES = /* @__PURE__ */ new Set(["OP_SHA256", "OP_HASH160", "OP_HASH256", "OP_RIPEMD160"]);
877
+ function isRecord(value) {
878
+ return typeof value === "object" && value !== null && !Array.isArray(value);
879
+ }
880
+ function isNamed(value) {
881
+ return isRecord(value) && typeof value.name === "string" && value.name.length > 0 && typeof value.type === "string" && value.type.length > 0;
882
+ }
883
+ function isContractArtifact(value) {
884
+ if (!isRecord(value) || typeof value.contractName !== "string") return false;
885
+ if (!Array.isArray(value.constructorInputs)) return false;
886
+ if (value.structs !== void 0 && !Array.isArray(value.structs)) return false;
887
+ if (!Array.isArray(value.functions) || value.functions.length === 0) return false;
888
+ return value.functions.every(
889
+ (group) => isRecord(group) && typeof group.name === "string" && group.name.length > 0 && Array.isArray(group.leaves) && group.leaves.length > 0
890
+ );
891
+ }
892
+ function fail(detail) {
893
+ throw new Error(`programFromArtifact: ${detail}`);
894
+ }
895
+ var isPair = (value) => Array.isArray(value) && value.length === 2 && value.every((n) => Number.isInteger(n) && n >= 0);
896
+ function sourceMaps(artifact, debug) {
897
+ if (!isRecord(debug) || !isRecord(debug.functions)) fail("debug file needs a functions object");
898
+ if (debug.formatVersion !== 1) {
899
+ fail(`unsupported debug format version ${debug.formatVersion}; expected 1`);
900
+ }
901
+ if (artifact.fingerprint === void 0 || debug.artifactFingerprint !== artifact.fingerprint) {
902
+ fail(
903
+ `debug file fingerprint ${debug.artifactFingerprint ?? "(missing)"} does not match the artifact's ${artifact.fingerprint ?? "(missing)"}`
904
+ );
905
+ }
906
+ const maps = /* @__PURE__ */ new Map();
907
+ for (const [name, entry] of Object.entries(debug.functions)) {
908
+ const asm = artifact.functions.find((group) => isRecord(group) && group.name === name)?.arkade?.asm;
909
+ if (!Array.isArray(asm)) fail(`debug file maps no covenant '${name}'`);
910
+ if (!isRecord(entry) || !Array.isArray(entry.sourceMap)) {
911
+ fail(`debug file: covenant '${name}' needs a sourceMap array`);
912
+ }
913
+ const copies = entry.sourceMap.map((range) => {
914
+ if (!isRecord(range) || !isPair(range.asm) || !isPair(range.start) || !isPair(range.end) || range.asm[0] > range.asm[1] || range.asm[1] > asm.length || range.message !== void 0 && typeof range.message !== "string") {
915
+ fail(
916
+ `debug file: covenant '${name}' has a malformed range ${JSON.stringify(range)}`
917
+ );
918
+ }
919
+ return { ...range, asm: [...range.asm], start: [...range.start], end: [...range.end] };
920
+ });
921
+ maps.set(name, copies);
922
+ }
923
+ return maps;
924
+ }
925
+ function claim(seen, name, kind) {
926
+ if (seen.has(name)) fail(`duplicate ${kind} '${name}'`);
927
+ seen.add(name);
928
+ }
929
+ function inputDef(name, type) {
930
+ return { name, type: argType(type) };
931
+ }
932
+ function argType(arkType) {
933
+ if (!Object.hasOwn(ARKADE_TYPE_ENCODINGS, arkType)) fail(`unknown type '${arkType}'`);
934
+ return arkType;
935
+ }
936
+ function arrayParts(type) {
937
+ const open = type.indexOf("[");
938
+ if (open < 0 || !type.endsWith("]")) return [type, 0];
939
+ const length = Number(type.slice(open + 1, -1));
940
+ return Number.isSafeInteger(length) && length > 0 && /^\d+$/.test(type.slice(open + 1, -1)) ? [type.slice(0, open), length] : [type, 0];
941
+ }
942
+ var MAX_LAYOUT_CHARS = 1 << 18;
943
+ function flatten(param, structs, budget, stack = []) {
944
+ if (!isNamed(param)) fail("parameter needs a name and a type");
945
+ const [element, length] = arrayParts(param.type);
946
+ budget.left -= param.name.length;
947
+ if (budget.left < length) fail(`layouts exceed ${MAX_LAYOUT_CHARS} name characters`);
948
+ if (length > 0) {
949
+ return Array.from(
950
+ { length },
951
+ (_, index) => flatten({ name: `${param.name}.${index}`, type: element }, structs, budget, stack)
952
+ ).flat();
953
+ }
954
+ if (param.type.includes("[") || param.type.includes("]")) {
955
+ fail(`invalid array type '${param.type}'`);
956
+ }
957
+ if (Object.hasOwn(NATIVE_STRUCTS, param.type)) {
958
+ return NATIVE_STRUCTS[param.type].flatMap(
959
+ ([field, type]) => flatten({ name: `${param.name}.${field}`, type }, structs, budget, stack)
960
+ );
961
+ }
962
+ const declared = structs.get(param.type);
963
+ if (declared) {
964
+ if (stack.includes(param.type)) fail(`recursive struct layout '${param.type}'`);
965
+ return declared.fields.flatMap(
966
+ (field) => flatten({ name: `${param.name}.${field.name}`, type: field.type }, structs, budget, [
967
+ ...stack,
968
+ param.type
969
+ ])
970
+ );
971
+ }
972
+ return [inputDef(param.name, param.type)];
973
+ }
974
+ function opcodeToken(token) {
975
+ const base = token.slice(3);
976
+ const name = base === "0" || /^([1-9]|1[0-6])$/.test(base) ? token : base;
977
+ if (!Object.hasOwn(ARKADE_OPS, name)) {
978
+ fail(`opcode '${token}' is not in this SDK's table`);
979
+ }
980
+ return name;
981
+ }
982
+ function instantiationParam(body) {
983
+ return `contract_${body.replace(/[^A-Za-z0-9]+/g, "_")}`.replace(/_+$/, "");
984
+ }
985
+ function asmToken(token, instantiations) {
986
+ if (token.startsWith("OP_")) return opcodeToken(token);
987
+ if (token.startsWith("<") && token.endsWith(">")) {
988
+ const inner = token.slice(1, -1);
989
+ if (inner.startsWith("CONTRACT:")) {
990
+ const body = inner.slice("CONTRACT:".length);
991
+ const name = instantiationParam(body);
992
+ const seen = instantiations.get(name);
993
+ if (seen !== void 0 && seen !== body) {
994
+ fail(`instantiations '${seen}' and '${body}' both map to parameter '${name}'`);
995
+ }
996
+ instantiations.set(name, body);
997
+ return `$${name}`;
998
+ }
999
+ if (inner.startsWith("VTXO:")) {
1000
+ fail(`'${token}' is not a placeholder; the compiler emits <CONTRACT:\u2026>`);
1001
+ }
1002
+ if (inner === "SERVER_KEY" || inner.startsWith("EMULATOR_KEY:")) {
1003
+ fail(`${token} is a signer role and cannot appear in a covenant`);
1004
+ }
1005
+ return `$${inner}`;
1006
+ }
1007
+ if (token.startsWith("0x")) return base.hex.decode(token.slice(2));
1008
+ try {
1009
+ return BigInt(token);
1010
+ } catch {
1011
+ fail(`unrecognized assembly token '${token}'`);
1012
+ }
1013
+ }
1014
+ function providedWitness(witness) {
1015
+ if (witness === void 0) return [];
1016
+ if (!Array.isArray(witness)) fail("leaf witness must be an array");
1017
+ return witness.filter((item) => {
1018
+ if (!isNamed(item)) fail("witness item needs a name and a type");
1019
+ const type = argType(item.type);
1020
+ if (item.injected !== void 0 && typeof item.injected !== "boolean")
1021
+ fail(`witness '${item.name}': injected must be boolean`);
1022
+ if (item.injected && item.type !== "signature")
1023
+ fail(`witness '${item.name}': only infrastructure signatures can be injected`);
1024
+ if (item.encoding !== void 0 && item.encoding !== ARKADE_TYPE_ENCODINGS[type])
1025
+ fail(
1026
+ `witness '${item.name}': encoding '${item.encoding}' does not match type '${item.type}'`
1027
+ );
1028
+ return !item.injected && item.type !== "signature";
1029
+ });
1030
+ }
1031
+ function csvTimelock(leafName, operand) {
1032
+ if (typeof operand === "string") return { type: "sequence", value: operand };
1033
+ const timelock = chunk6TBTL42M_cjs.sequenceToCanonicalTimelock(operand);
1034
+ if (!timelock) {
1035
+ fail(`leaf '${leafName}': CSV literal ${operand} is not a canonical BIP68 sequence`);
1036
+ }
1037
+ return timelock;
1038
+ }
1039
+ function parseLeaf(leaf, hasCovenant, instantiations, extras) {
1040
+ const asm = leaf.asm;
1041
+ let index = 0;
1042
+ let condition;
1043
+ const hashIndex = asm[0] === "OP_SIZE" && (asm[1] === "20" || asm[1] === "32") && asm[2] === "OP_EQUALVERIFY" ? 3 : 0;
1044
+ if (HASH_OPCODES.has(asm[hashIndex]) && asm[hashIndex + 2] === "OP_EQUAL") {
1045
+ if (asm[hashIndex + 3] !== "OP_VERIFY")
1046
+ fail(`leaf '${leaf.name}': hash condition must end in OP_VERIFY`);
1047
+ condition = asm.slice(0, hashIndex + 3).map((token) => asmToken(token, instantiations));
1048
+ index = hashIndex + 4;
1049
+ }
1050
+ let csv;
1051
+ let cltv;
1052
+ if (asm.length >= index + 3 && asm[index + 2] === "OP_DROP") {
1053
+ const operand = asm[index].startsWith("<") && asm[index].endsWith(">") ? `$${asm[index].slice(1, -1)}` : BigInt(asm[index]);
1054
+ if (asm[index + 1] === "OP_CHECKSEQUENCEVERIFY") csv = csvTimelock(leaf.name, operand);
1055
+ else if (asm[index + 1] === "OP_CHECKLOCKTIMEVERIFY") cltv = operand;
1056
+ else fail(`leaf '${leaf.name}': unexpected timelock opcode ${asm[index + 1]}`);
1057
+ index += 3;
1058
+ }
1059
+ const signers = [];
1060
+ let emulator;
1061
+ let emulatorIndex;
1062
+ while (index < asm.length) {
1063
+ const key = asm[index];
1064
+ const terminator = asm[index + 1];
1065
+ const last = terminator === "OP_CHECKSIG";
1066
+ if (last !== (index + 2 === asm.length)) {
1067
+ fail(`leaf '${leaf.name}': only the final signer must end in OP_CHECKSIG`);
1068
+ }
1069
+ if (!last && terminator !== "OP_CHECKSIGVERIFY") {
1070
+ fail(`leaf '${leaf.name}': expected CHECKSIG after '${key}', found '${terminator}'`);
1071
+ }
1072
+ if (!key.startsWith("<") || !key.endsWith(">")) {
1073
+ fail(`leaf '${leaf.name}': unsupported key operand '${key}'`);
1074
+ }
1075
+ const inner = key.slice(1, -1);
1076
+ if (inner === "SERVER_KEY") signers.push("$server");
1077
+ else if (inner.startsWith("TWEAK:")) {
1078
+ const [, base, fn] = inner.match(/^TWEAK:([^:]+):([^:]+)$/) ?? [];
1079
+ if (!base || !fn) fail(`leaf '${leaf.name}': malformed tweak '${key}'`);
1080
+ signers.push({ tweak: `$${base}`, fn });
1081
+ } else if (inner.startsWith("EMULATOR_KEY:")) {
1082
+ if (!hasCovenant) fail(`leaf '${leaf.name}': ${key} needs a covenant`);
1083
+ if (emulator !== void 0) fail(`leaf '${leaf.name}': duplicate emulator signer`);
1084
+ emulator = inner.slice("EMULATOR_KEY:".length);
1085
+ emulatorIndex = signers.length;
1086
+ } else signers.push(`$${inner}`);
1087
+ index += 2;
1088
+ }
1089
+ const constructorTweak = signers.some((signer) => typeof signer !== "string");
1090
+ if (hasCovenant && emulator === void 0 && !constructorTweak) {
1091
+ fail(
1092
+ `leaf '${leaf.name}': covenant leaf must sign with the emulator or a tweaked constructor key`
1093
+ );
1094
+ }
1095
+ if (signers.length === 0 && emulator === void 0)
1096
+ fail(`leaf '${leaf.name}': at least one named signer is required`);
1097
+ return {
1098
+ signers,
1099
+ ...emulator !== void 0 ? { emulator } : hasCovenant ? { emulator: null } : {},
1100
+ ...emulatorIndex !== void 0 ? { emulatorIndex } : {},
1101
+ ...condition ? { asm: condition } : {},
1102
+ ...csv ? { csv } : {},
1103
+ ...cltv !== void 0 ? { cltv } : {},
1104
+ ...extras.length > 0 ? {
1105
+ inputs: extras.map((item) => inputDef(item.name, item.type)),
1106
+ witness: extras.map((item) => item.name)
1107
+ } : {}
1108
+ };
1109
+ }
1110
+ function programFromArtifact(artifact, debug) {
1111
+ if (!isContractArtifact(artifact)) {
1112
+ fail(
1113
+ "expected a complete arkadec artifact with contractName, constructorInputs, and non-empty functions"
1114
+ );
1115
+ }
1116
+ if (artifact.formatVersion !== void 0 && artifact.formatVersion !== 1) {
1117
+ fail(`unsupported artifact format version ${artifact.formatVersion}; expected 1`);
1118
+ }
1119
+ const maps = debug === void 0 ? void 0 : sourceMaps(artifact, debug);
1120
+ const structs = artifact.structs ?? [];
1121
+ const structNames = /* @__PURE__ */ new Set();
1122
+ for (const struct of structs) {
1123
+ if (!isRecord(struct) || typeof struct.name !== "string" || !Array.isArray(struct.fields)) {
1124
+ fail("struct needs a name and fields array");
1125
+ }
1126
+ const fields = /* @__PURE__ */ new Set();
1127
+ for (const field of struct.fields) {
1128
+ if (!isNamed(field)) fail(`struct '${struct.name}': field needs a name and type`);
1129
+ claim(fields, field.name, "struct field");
1130
+ }
1131
+ if (Object.hasOwn(ARKADE_TYPE_ENCODINGS, struct.name) || Object.hasOwn(NATIVE_STRUCTS, struct.name)) {
1132
+ fail(`struct name '${struct.name}' shadows a built-in type`);
1133
+ }
1134
+ claim(structNames, struct.name, "struct");
1135
+ }
1136
+ const structsByName = new Map(structs.map((struct) => [struct.name, struct]));
1137
+ const budget = { left: MAX_LAYOUT_CHARS };
1138
+ const instantiations = /* @__PURE__ */ new Map();
1139
+ const functions = /* @__PURE__ */ Object.create(null);
1140
+ const groups = /* @__PURE__ */ new Set();
1141
+ const leafNames = /* @__PURE__ */ new Set();
1142
+ for (const group of artifact.functions) {
1143
+ claim(groups, group.name, "spend group");
1144
+ if (group.arkade !== void 0 && (!isRecord(group.arkade) || !Array.isArray(group.arkade.inputs) || !Array.isArray(group.arkade.asm) || !group.arkade.asm.every((t) => typeof t === "string"))) {
1145
+ fail(`group '${group.name}': covenant needs inputs and string assembly arrays`);
1146
+ }
1147
+ const covenantInputs = (group.arkade?.inputs ?? []).flatMap(
1148
+ (input) => flatten(input, structsByName, budget)
1149
+ );
1150
+ const sourceMap = maps?.get(group.name);
1151
+ const arkadeScript = group.arkade ? {
1152
+ asm: group.arkade.asm.map((token) => asmToken(token, instantiations)),
1153
+ ...covenantInputs.length > 0 ? { inputs: covenantInputs } : {},
1154
+ // Covenant witness is reverse declaration order.
1155
+ witness: covenantInputs.map((field) => field.name).reverse(),
1156
+ ...sourceMap ? { sourceMap } : {}
1157
+ } : void 0;
1158
+ const covenantNames = /* @__PURE__ */ new Set();
1159
+ for (const input of covenantInputs) claim(covenantNames, input.name, "covenant input");
1160
+ for (const leaf of group.leaves) {
1161
+ if (!isRecord(leaf) || typeof leaf.name !== "string" || leaf.name.length === 0 || !Array.isArray(leaf.asm) || leaf.asm.length === 0 || !leaf.asm.every((t) => typeof t === "string")) {
1162
+ fail(`group '${group.name}': leaf needs a name and non-empty string assembly`);
1163
+ }
1164
+ const name = leaf.name;
1165
+ claim(leafNames, name, "function name");
1166
+ const extras = providedWitness(leaf.witness);
1167
+ for (const input of extras) {
1168
+ if (leaf.asm.includes(`<${input.name}>`)) {
1169
+ fail(
1170
+ `leaf '${leaf.name}': call input '${input.name}' is a script operand; it must be a constructor binding to derive a fixed Taproot tree`
1171
+ );
1172
+ }
1173
+ }
1174
+ const witnessNames = /* @__PURE__ */ new Set();
1175
+ for (const item of leaf.witness ?? []) claim(witnessNames, item.name, "leaf witness");
1176
+ const tapscript = parseLeaf(leaf, group.arkade !== void 0, instantiations, extras);
1177
+ if (tapscript.emulator !== void 0 && tapscript.emulator !== null && tapscript.emulator !== group.name) {
1178
+ fail(
1179
+ `leaf '${leaf.name}': emulator covenant '${tapscript.emulator}' must match group '${group.name}'`
1180
+ );
1181
+ }
1182
+ const signatures = (leaf.witness ?? []).filter((item) => item.type === "signature" && !item.injected).map((item) => inputDef(item.name, item.type));
1183
+ if (signatures.length > 0) tapscript.signatures = signatures;
1184
+ functions[name] = {
1185
+ group: group.name,
1186
+ tapscript,
1187
+ ...arkadeScript ? { arkadeScript } : {}
1188
+ };
1189
+ }
1190
+ }
1191
+ const params = [
1192
+ ...artifact.constructorInputs.flatMap((input) => flatten(input, structsByName, budget)),
1193
+ { name: "server", type: "pubkey" },
1194
+ ...Object.values(functions).some((fn) => fn.tapscript.csv?.value === "$SERVER_EXIT_DELAY") ? [{ name: "SERVER_EXIT_DELAY", type: "int" }] : [],
1195
+ ...[...instantiations.keys()].map((name) => ({
1196
+ name,
1197
+ type: "bytes32"
1198
+ }))
1199
+ ];
1200
+ const paramNames = /* @__PURE__ */ new Set();
1201
+ for (const param of params) claim(paramNames, param.name, "program parameter");
1202
+ return {
1203
+ version: SUPPORTED_PROGRAM_VERSION,
1204
+ name: artifact.contractName,
1205
+ params,
1206
+ functions
1207
+ };
1208
+ }
850
1209
 
851
1210
  exports.ARKADE_OP = ARKADE_OP;
852
1211
  exports.ARKADE_OPCODES = ARKADE_OPCODES;
853
1212
  exports.ARKADE_OPCODE_NAMES = ARKADE_OPCODE_NAMES;
854
1213
  exports.ARKADE_OPCODE_VALUES = ARKADE_OPCODE_VALUES;
855
1214
  exports.ARKADE_OPS = ARKADE_OPS;
856
- exports.ARKADE_TYPE_ENCODINGS = ARKADE_TYPE_ENCODINGS;
857
1215
  exports.ArkadeProgramScript = ArkadeProgramScript;
858
1216
  exports.ArkadeScript = ArkadeScript;
859
1217
  exports.OPCODE_NAMES = OPCODE_NAMES;
@@ -870,7 +1228,9 @@ exports.deserializeArkadeContractParams = deserializeArkadeContractParams;
870
1228
  exports.fromASM = fromASM;
871
1229
  exports.getOpcodeName = getOpcodeName;
872
1230
  exports.getOpcodeValue = getOpcodeValue;
1231
+ exports.isContractArtifact = isContractArtifact;
873
1232
  exports.parseArtifact = parseArtifact;
1233
+ exports.programFromArtifact = programFromArtifact;
874
1234
  exports.resolveAsm = resolveAsm;
875
1235
  exports.resolveCsvTimelock = resolveCsvTimelock;
876
1236
  exports.resolveTimelockValue = resolveTimelockValue;
@@ -878,9 +1238,8 @@ exports.serializeArkadeContractParams = serializeArkadeContractParams;
878
1238
  exports.staticWitness = staticWitness;
879
1239
  exports.stringifyArtifact = stringifyArtifact;
880
1240
  exports.toASM = toASM;
881
- exports.toXOnly = toXOnly;
882
1241
  exports.validateParamValue = validateParamValue;
883
1242
  exports.validateProgram = validateProgram;
884
1243
  exports.witnessRefToBytes = witnessRefToBytes;
885
- //# sourceMappingURL=chunk-JA4SYFC7.cjs.map
886
- //# sourceMappingURL=chunk-JA4SYFC7.cjs.map
1244
+ //# sourceMappingURL=chunk-ACIQZLWL.cjs.map
1245
+ //# sourceMappingURL=chunk-ACIQZLWL.cjs.map