@arkade-os/sdk 0.4.78 → 0.4.79

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 (76) hide show
  1. package/README.md +1 -1
  2. package/dist/adapters/expo.cjs +5 -5
  3. package/dist/adapters/expo.d.cts +3 -3
  4. package/dist/adapters/expo.d.ts +3 -3
  5. package/dist/adapters/expo.js +3 -3
  6. package/dist/{asyncStorageTaskQueue-YW2t31_o.d.ts → asyncStorageTaskQueue-C5yQLJhm.d.ts} +1 -1
  7. package/dist/{asyncStorageTaskQueue-DiXxP-nQ.d.cts → asyncStorageTaskQueue-Shsih-1l.d.cts} +1 -1
  8. package/dist/{chunk-ZOCJ7E6M.js → chunk-4MXW2NDN.js} +10 -515
  9. package/dist/chunk-4MXW2NDN.js.map +1 -0
  10. package/dist/{chunk-KEEZSFLI.cjs → chunk-54FXLZ3T.cjs} +10 -10
  11. package/dist/{chunk-KEEZSFLI.cjs.map → chunk-54FXLZ3T.cjs.map} +1 -1
  12. package/dist/{chunk-PWIESIO6.cjs → chunk-DAYCCIRO.cjs} +372 -198
  13. package/dist/chunk-DAYCCIRO.cjs.map +1 -0
  14. package/dist/{chunk-Y7BVR6SQ.cjs → chunk-FUW3OM5X.cjs} +76 -561
  15. package/dist/chunk-FUW3OM5X.cjs.map +1 -0
  16. package/dist/{chunk-VJ7M6WKB.cjs → chunk-HAM3SHBV.cjs} +587 -532
  17. package/dist/chunk-HAM3SHBV.cjs.map +1 -0
  18. package/dist/{chunk-7KIGEOYT.js → chunk-JCPVP7TJ.js} +373 -201
  19. package/dist/chunk-JCPVP7TJ.js.map +1 -0
  20. package/dist/{chunk-DFWLP3MD.cjs → chunk-LASRYLMQ.cjs} +17 -17
  21. package/dist/{chunk-DFWLP3MD.cjs.map → chunk-LASRYLMQ.cjs.map} +1 -1
  22. package/dist/{chunk-CTFYT24G.js → chunk-QDXOTONI.js} +3 -3
  23. package/dist/{chunk-CTFYT24G.js.map → chunk-QDXOTONI.js.map} +1 -1
  24. package/dist/{chunk-IAM5IEVT.cjs → chunk-SJPPBR2V.cjs} +17 -17
  25. package/dist/{chunk-IAM5IEVT.cjs.map → chunk-SJPPBR2V.cjs.map} +1 -1
  26. package/dist/{chunk-P766MOPU.js → chunk-SK4OZRMB.js} +4 -4
  27. package/dist/{chunk-P766MOPU.js.map → chunk-SK4OZRMB.js.map} +1 -1
  28. package/dist/{chunk-OUHUC26M.js → chunk-VAFNWP3I.js} +4 -4
  29. package/dist/{chunk-OUHUC26M.js.map → chunk-VAFNWP3I.js.map} +1 -1
  30. package/dist/{chunk-LKAASQTL.js → chunk-YPQ37P3H.js} +128 -73
  31. package/dist/chunk-YPQ37P3H.js.map +1 -0
  32. package/dist/contracts/handlers/index.cjs +9 -9
  33. package/dist/contracts/handlers/index.d.cts +4 -4
  34. package/dist/contracts/handlers/index.d.ts +4 -4
  35. package/dist/contracts/handlers/index.js +2 -2
  36. package/dist/{delegate-DaNNUI_B.d.cts → delegate-B4bKASut.d.cts} +1 -1
  37. package/dist/{delegate-b2Wiymoj.d.ts → delegate-C9lpdrb0.d.ts} +1 -1
  38. package/dist/{hdWalletCapable-B2XVTOvu.d.ts → hdWalletCapable-CUJDKRKO.d.cts} +2 -3
  39. package/dist/{hdWalletCapable-D6PqWpuW.d.cts → hdWalletCapable-DJClhYMC.d.ts} +2 -3
  40. package/dist/{index-CAS4GyN-.d.ts → index-Ctul0N3Y.d.cts} +27 -6
  41. package/dist/{index-CAS4GyN-.d.cts → index-Ctul0N3Y.d.ts} +27 -6
  42. package/dist/{index-CYsKAXog.d.ts → index-D0qe2qVY.d.ts} +2 -2
  43. package/dist/{index-BlB97fYl.d.cts → index-YBFmScVS.d.cts} +2 -2
  44. package/dist/index.cjs +304 -304
  45. package/dist/index.d.cts +123 -123
  46. package/dist/index.d.ts +123 -123
  47. package/dist/index.js +4 -4
  48. package/dist/repositories/realm/index.cjs +20 -20
  49. package/dist/repositories/realm/index.d.cts +3 -3
  50. package/dist/repositories/realm/index.d.ts +3 -3
  51. package/dist/repositories/realm/index.js +5 -5
  52. package/dist/repositories/sqlite/index.cjs +19 -19
  53. package/dist/repositories/sqlite/index.d.cts +3 -3
  54. package/dist/repositories/sqlite/index.d.ts +3 -3
  55. package/dist/repositories/sqlite/index.js +5 -5
  56. package/dist/{taskRunner-BJj7xe35.d.ts → taskRunner-C08egG5a.d.ts} +1 -1
  57. package/dist/{taskRunner-HS2d2oda.d.cts → taskRunner-DB17db5n.d.cts} +1 -1
  58. package/dist/wallet/expo/background.cjs +14 -14
  59. package/dist/wallet/expo/background.d.cts +4 -4
  60. package/dist/wallet/expo/background.d.ts +4 -4
  61. package/dist/wallet/expo/background.js +6 -6
  62. package/dist/wallet/expo/index.cjs +14 -14
  63. package/dist/wallet/expo/index.d.cts +5 -5
  64. package/dist/wallet/expo/index.d.ts +5 -5
  65. package/dist/wallet/expo/index.js +5 -5
  66. package/dist/worker/expo/index.cjs +9 -9
  67. package/dist/worker/expo/index.d.cts +5 -5
  68. package/dist/worker/expo/index.d.ts +5 -5
  69. package/dist/worker/expo/index.js +5 -5
  70. package/package.json +6 -5
  71. package/dist/chunk-7KIGEOYT.js.map +0 -1
  72. package/dist/chunk-LKAASQTL.js.map +0 -1
  73. package/dist/chunk-PWIESIO6.cjs.map +0 -1
  74. package/dist/chunk-VJ7M6WKB.cjs.map +0 -1
  75. package/dist/chunk-Y7BVR6SQ.cjs.map +0 -1
  76. package/dist/chunk-ZOCJ7E6M.js.map +0 -1
@@ -1,11 +1,12 @@
1
1
  'use strict';
2
2
 
3
- var btcSigner = require('@scure/btc-signer');
4
- var bip68 = require('bip68');
5
3
  var utils_js = require('@scure/btc-signer/utils.js');
6
4
  var base = require('@scure/base');
7
5
  var script_js = require('@scure/btc-signer/script.js');
6
+ var bip68 = require('bip68');
7
+ var btcSigner = require('@scure/btc-signer');
8
8
  var payment_js = require('@scure/btc-signer/payment.js');
9
+ var P = require('micro-packed');
9
10
  var psbt_js = require('@scure/btc-signer/psbt.js');
10
11
  var secp256k1_js = require('@noble/curves/secp256k1.js');
11
12
 
@@ -28,187 +29,9 @@ function _interopNamespace(e) {
28
29
  }
29
30
 
30
31
  var bip68__namespace = /*#__PURE__*/_interopNamespace(bip68);
32
+ var P__namespace = /*#__PURE__*/_interopNamespace(P);
31
33
 
32
- // src/utils/transaction.ts
33
- var Transaction = class extends btcSigner.Transaction {
34
- static ARK_TX_OPTS = {
35
- allowUnknown: true,
36
- allowUnknownOutputs: true,
37
- allowUnknownInputs: true
38
- };
39
- constructor(opts) {
40
- super(withArkOpts(opts));
41
- }
42
- static fromPSBT(psbt_, opts) {
43
- return btcSigner.Transaction.fromPSBT(psbt_, withArkOpts(opts));
44
- }
45
- static fromRaw(raw, opts) {
46
- return btcSigner.Transaction.fromRaw(raw, withArkOpts(opts));
47
- }
48
- };
49
- function withArkOpts(opts) {
50
- return { ...Transaction.ARK_TX_OPTS, ...opts };
51
- }
52
- function formatSighash(type) {
53
- return `0x${type.toString(16).padStart(2, "0")}`;
54
- }
55
- function assertAllowedSighashTypes(tx, allowedSighashTypes = [btcSigner.SigHash.DEFAULT]) {
56
- for (let i = 0; i < tx.inputsLength; i++) {
57
- const declared = tx.getInput(i).sighashType;
58
- if (declared === void 0) continue;
59
- if (!allowedSighashTypes.includes(declared)) {
60
- throw new Error(`Unallowed sighash type ${formatSighash(declared)} for input ${i}.`);
61
- }
62
- }
63
- }
64
- function timelockToSequence(timelock) {
65
- return bip68__namespace.encode(
66
- timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
67
- );
68
- }
69
- function sequenceToTimelock(sequence) {
70
- const decoded = bip68__namespace.decode(sequence);
71
- if ("blocks" in decoded && decoded.blocks !== void 0) {
72
- return { type: "blocks", value: BigInt(decoded.blocks) };
73
- }
74
- if ("seconds" in decoded && decoded.seconds !== void 0) {
75
- return { type: "seconds", value: BigInt(decoded.seconds) };
76
- }
77
- throw new Error(`Invalid BIP68 sequence: ${sequence}`);
78
- }
79
- var ArkPsbtFieldKey = /* @__PURE__ */ ((ArkPsbtFieldKey2) => {
80
- ArkPsbtFieldKey2["VtxoTaprootTree"] = "taptree";
81
- ArkPsbtFieldKey2["VtxoTreeExpiry"] = "expiry";
82
- ArkPsbtFieldKey2["Cosigner"] = "cosigner";
83
- ArkPsbtFieldKey2["ConditionWitness"] = "condition";
84
- ArkPsbtFieldKey2["PrevArkTx"] = "prevarktx";
85
- ArkPsbtFieldKey2["PrevoutTx"] = "prevouttx";
86
- return ArkPsbtFieldKey2;
87
- })(ArkPsbtFieldKey || {});
88
- var ArkPsbtFieldKeyType = 222;
89
- function setArkPsbtField(tx, inputIndex, coder, value) {
90
- tx.updateInput(inputIndex, {
91
- unknown: [...tx.getInput(inputIndex)?.unknown ?? [], coder.encode(value)]
92
- });
93
- }
94
- function getArkPsbtFields(tx, inputIndex, coder) {
95
- const unknown = tx.getInput(inputIndex)?.unknown ?? [];
96
- const fields = [];
97
- for (const u of unknown) {
98
- const v = coder.decode(u);
99
- if (v !== null) fields.push(v);
100
- }
101
- return fields;
102
- }
103
- var VtxoTaprootTree = {
104
- key: "taptree" /* VtxoTaprootTree */,
105
- encode: (value) => [
106
- {
107
- type: ArkPsbtFieldKeyType,
108
- key: encodedPsbtFieldKey["taptree" /* VtxoTaprootTree */]
109
- },
110
- value
111
- ],
112
- decode: (value) => nullIfCatch(() => {
113
- if (!checkKeyMatch(value[0], "taptree" /* VtxoTaprootTree */)) return null;
114
- return value[1];
115
- })
116
- };
117
- var ConditionWitness = {
118
- key: "condition" /* ConditionWitness */,
119
- encode: (value) => [
120
- {
121
- type: ArkPsbtFieldKeyType,
122
- key: encodedPsbtFieldKey["condition" /* ConditionWitness */]
123
- },
124
- btcSigner.RawWitness.encode(value)
125
- ],
126
- decode: (value) => nullIfCatch(() => {
127
- if (!checkKeyMatch(value[0], "condition" /* ConditionWitness */)) return null;
128
- return btcSigner.RawWitness.decode(value[1]);
129
- })
130
- };
131
- var PrevArkTxField = {
132
- key: "prevarktx" /* PrevArkTx */,
133
- encode: (value) => [
134
- {
135
- type: ArkPsbtFieldKeyType,
136
- key: encodedPsbtFieldKey["prevarktx" /* PrevArkTx */]
137
- },
138
- value
139
- ],
140
- decode: (value) => nullIfCatch(() => {
141
- if (!checkKeyMatch(value[0], "prevarktx" /* PrevArkTx */)) return null;
142
- return value[1];
143
- })
144
- };
145
- var PrevoutTxField = {
146
- key: "prevouttx" /* PrevoutTx */,
147
- encode: (value) => [
148
- {
149
- type: ArkPsbtFieldKeyType,
150
- key: encodedPsbtFieldKey["prevouttx" /* PrevoutTx */]
151
- },
152
- value
153
- ],
154
- decode: (value) => nullIfCatch(() => {
155
- if (!checkKeyMatch(value[0], "prevouttx" /* PrevoutTx */)) return null;
156
- return value[1];
157
- })
158
- };
159
- var CosignerPublicKey = {
160
- key: "cosigner" /* Cosigner */,
161
- encode: (value) => [
162
- {
163
- type: ArkPsbtFieldKeyType,
164
- key: new Uint8Array([...encodedPsbtFieldKey["cosigner" /* Cosigner */], value.index])
165
- },
166
- value.key
167
- ],
168
- decode: (unknown) => nullIfCatch(() => {
169
- if (!checkKeyMatch(unknown[0], "cosigner" /* Cosigner */, true)) return null;
170
- return {
171
- index: unknown[0].key[unknown[0].key.length - 1],
172
- key: unknown[1]
173
- };
174
- })
175
- };
176
- var VtxoTreeExpiry = {
177
- key: "expiry" /* VtxoTreeExpiry */,
178
- encode: (value) => [
179
- {
180
- type: ArkPsbtFieldKeyType,
181
- key: encodedPsbtFieldKey["expiry" /* VtxoTreeExpiry */]
182
- },
183
- btcSigner.ScriptNum(6, true).encode(value.value === 0n ? 0n : value.value)
184
- ],
185
- decode: (unknown) => nullIfCatch(() => {
186
- if (!checkKeyMatch(unknown[0], "expiry" /* VtxoTreeExpiry */)) return null;
187
- const v = btcSigner.ScriptNum(6, true).decode(unknown[1]);
188
- if (!v) return null;
189
- return sequenceToTimelock(Number(v));
190
- })
191
- };
192
- var encodedPsbtFieldKey = Object.fromEntries(
193
- Object.values(ArkPsbtFieldKey).map((key) => [key, new TextEncoder().encode(key)])
194
- );
195
- var nullIfCatch = (fn) => {
196
- try {
197
- return fn();
198
- } catch {
199
- return null;
200
- }
201
- };
202
- function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
203
- if (key.type !== ArkPsbtFieldKeyType) return false;
204
- const expected = encodedPsbtFieldKey[arkPsbtFieldKey];
205
- if (key.key.length < expected.length) return false;
206
- if (!prefixOnly && key.key.length !== expected.length) return false;
207
- for (let i = 0; i < expected.length; i++) {
208
- if (key.key[i] !== expected[i]) return false;
209
- }
210
- return true;
211
- }
34
+ // src/networks.ts
212
35
  var getNetwork = (network) => {
213
36
  const found = networks[network];
214
37
  if (!found) throw new Error(`Unsupported network: ${network}`);
@@ -344,6 +167,21 @@ var ArkAddress = class _ArkAddress {
344
167
  return script_js.Script.encode(["RETURN", this.vtxoTaprootKey]);
345
168
  }
346
169
  };
170
+ function timelockToSequence(timelock) {
171
+ return bip68__namespace.encode(
172
+ timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
173
+ );
174
+ }
175
+ function sequenceToTimelock(sequence) {
176
+ const decoded = bip68__namespace.decode(sequence);
177
+ if ("blocks" in decoded && decoded.blocks !== void 0) {
178
+ return { type: "blocks", value: BigInt(decoded.blocks) };
179
+ }
180
+ if ("seconds" in decoded && decoded.seconds !== void 0) {
181
+ return { type: "seconds", value: BigInt(decoded.seconds) };
182
+ }
183
+ throw new Error(`Invalid BIP68 sequence: ${sequence}`);
184
+ }
347
185
  var MinimalScriptNum = btcSigner.ScriptNum(void 0, true);
348
186
  function decodeTapscript(script) {
349
187
  const types = [
@@ -876,7 +714,25 @@ function assembleBtcdTaprootTree(scripts) {
876
714
  }
877
715
  return branches[0];
878
716
  }
879
- var TapTreeCoder = psbt_js.PSBTOutput.tapTree[2];
717
+ function btcdLeafLayout(count) {
718
+ const placeholders = Array.from({ length: count }, () => new Uint8Array());
719
+ const indexOf = new Map(placeholders.map((script, index) => [script, index]));
720
+ const layout = [];
721
+ const walk = (node, depth) => {
722
+ if (Array.isArray(node)) {
723
+ walk(node[0], depth + 1);
724
+ walk(node[1], depth + 1);
725
+ } else {
726
+ layout.push({ index: indexOf.get(node.script), depth });
727
+ }
728
+ };
729
+ walk(assembleBtcdTaprootTree(placeholders), 0);
730
+ return layout;
731
+ }
732
+ var TapTreeCoder = P__namespace.array(
733
+ null,
734
+ P__namespace.struct({ depth: P__namespace.U8, version: P__namespace.U8, script: script_js.VarBytes })
735
+ );
880
736
  function scriptFromTapLeafScript(leaf) {
881
737
  return leaf[1].subarray(0, leaf[1].length - 1);
882
738
  }
@@ -896,7 +752,7 @@ var VtxoScript = class _VtxoScript {
896
752
  constructor(scripts) {
897
753
  this.scripts = scripts;
898
754
  const tapTree = assembleBtcdTaprootTree(scripts);
899
- const payment = btcSigner.p2tr(btcSigner.TAPROOT_UNSPENDABLE_KEY, tapTree, void 0, true);
755
+ const payment = btcSigner.p2tr(btcSigner.taprootNumsKey(), tapTree, void 0, true);
900
756
  if (!payment.tapLeafScript || payment.tapLeafScript.length !== scripts.length) {
901
757
  throw new Error("invalid scripts");
902
758
  }
@@ -911,15 +767,46 @@ var VtxoScript = class _VtxoScript {
911
767
  /**
912
768
  * Decode a virtual output script from an encoded TapTree.
913
769
  *
770
+ * Leaves are normally read in construction order and their depths
771
+ * ignored: that covers the {@link encode} form (every depth 1) and the
772
+ * placeholder depths other writers use, such as arkd's. The exception is
773
+ * the BIP-371 form {@link toBIP371TapTree} writes, whose leaves are in the
774
+ * tree's DFS order: for some leaf counts (6, 7, 10, ...) that differs from
775
+ * construction order. A tree whose depths are exactly the shape
776
+ * `assembleBtcdTaprootTree` builds is that form, so its leaves are mapped
777
+ * back to construction order first. No construction-order writer produces
778
+ * that depth sequence when the two orders differ, so this never reorders
779
+ * a tree that decoded correctly before.
780
+ *
781
+ * Every leaf must carry the base tapscript version: the tree is rebuilt
782
+ * with {@link TAP_LEAF_VERSION} leaves, so any other version would
783
+ * silently derive a different taproot key.
784
+ *
914
785
  * @param tapTree - Encoded TapTree bytes
915
786
  * @returns Decoded virtual output script
916
787
  * @throws Error if the TapTree cannot be decoded into a valid script set
788
+ * or a leaf carries a version other than {@link TAP_LEAF_VERSION}
917
789
  * @see encode
918
790
  */
919
791
  static decode(tapTree) {
920
792
  const leaves = TapTreeCoder.decode(tapTree);
793
+ const foreign = leaves.find((leaf) => leaf.version !== payment_js.TAP_LEAF_VERSION);
794
+ if (foreign) {
795
+ throw new Error(`unsupported tap leaf version 0x${foreign.version.toString(16)}`);
796
+ }
921
797
  const scripts = leaves.map((leaf) => leaf.script);
922
- return new _VtxoScript(scripts);
798
+ if (leaves.every((leaf) => leaf.depth === 1)) {
799
+ return new _VtxoScript(scripts);
800
+ }
801
+ const layout = btcdLeafLayout(leaves.length);
802
+ if (layout.some((slot, i) => slot.depth !== leaves[i].depth)) {
803
+ return new _VtxoScript(scripts);
804
+ }
805
+ const ordered = new Array(leaves.length);
806
+ layout.forEach((slot, i) => {
807
+ ordered[slot.index] = scripts[i];
808
+ });
809
+ return new _VtxoScript(ordered);
923
810
  }
924
811
  /**
925
812
  * Encode the virtual output script to a TapTree byte representation.
@@ -1002,6 +889,15 @@ var VtxoScript = class _VtxoScript {
1002
889
  return paths;
1003
890
  }
1004
891
  };
892
+ function toBIP371TapTree(tapTree) {
893
+ const scripts = TapTreeCoder.decode(tapTree).map((leaf) => leaf.script);
894
+ const vtxoScript = new VtxoScript(scripts);
895
+ return vtxoScript.leaves.map((leaf) => ({
896
+ depth: leaf[0].merklePath.length,
897
+ version: payment_js.TAP_LEAF_VERSION,
898
+ script: scriptFromTapLeafScript(leaf)
899
+ }));
900
+ }
1005
901
  function getSequence(tapLeafScript) {
1006
902
  let sequence = void 0;
1007
903
  try {
@@ -1018,6 +914,290 @@ function getSequence(tapLeafScript) {
1018
914
  }
1019
915
  return sequence;
1020
916
  }
917
+ var Transaction = class extends btcSigner.Transaction {
918
+ static ARK_TX_OPTS = {
919
+ unknown: "ignore",
920
+ allowUnknownOutputs: true,
921
+ allowUnknownInputs: true
922
+ };
923
+ constructor(opts) {
924
+ super(withArkOpts(opts));
925
+ }
926
+ /**
927
+ * Decode a PSBT. PSBTs carrying a legacy `PSBT_OUT_TAP_TREE` (see
928
+ * {@link repairLegacyOutputTapTrees}) are repaired and decoded again
929
+ * instead of being rejected.
930
+ */
931
+ static fromPSBT(psbt_, opts) {
932
+ try {
933
+ return btcSigner.Transaction.fromPSBT(psbt_, withArkOpts(opts));
934
+ } catch (err) {
935
+ const repaired = repairLegacyOutputTapTrees(psbt_);
936
+ if (!repaired) throw err;
937
+ return btcSigner.Transaction.fromPSBT(repaired, withArkOpts(opts));
938
+ }
939
+ }
940
+ static fromRaw(raw, opts) {
941
+ return btcSigner.Transaction.fromRaw(raw, withArkOpts(opts));
942
+ }
943
+ };
944
+ function withArkOpts(opts) {
945
+ return { ...Transaction.ARK_TX_OPTS, ...opts };
946
+ }
947
+ var PSBT_MAGIC = new Uint8Array([112, 115, 98, 116, 255]);
948
+ var PSBT_GLOBAL_UNSIGNED_TX = 0;
949
+ var PSBT_GLOBAL_INPUT_COUNT = 4;
950
+ var PSBT_OUT_TAP_TREE = 6;
951
+ function repairLegacyOutputTapTrees(psbt) {
952
+ let maps;
953
+ let inputsCount;
954
+ try {
955
+ maps = readPsbtMaps(psbt);
956
+ inputsCount = psbtInputsCount(maps[0]);
957
+ } catch {
958
+ return void 0;
959
+ }
960
+ let repaired = false;
961
+ for (const output of maps.slice(1 + inputsCount)) {
962
+ for (const kv of output) {
963
+ if (kv.key.length !== 1 || kv.key[0] !== PSBT_OUT_TAP_TREE) continue;
964
+ const value = legacyTapTreeToBIP371(kv.value);
965
+ if (!value) continue;
966
+ kv.value = value;
967
+ repaired = true;
968
+ }
969
+ }
970
+ return repaired ? writePsbtMaps(maps) : void 0;
971
+ }
972
+ function legacyTapTreeToBIP371(value) {
973
+ try {
974
+ psbt_js.PSBTOutput.tapTree[2].decode(value);
975
+ return void 0;
976
+ } catch {
977
+ }
978
+ let leaves;
979
+ try {
980
+ leaves = TapTreeCoder.decode(value);
981
+ } catch {
982
+ return void 0;
983
+ }
984
+ if (leaves.length === 0 || leaves.some((leaf) => leaf.depth !== 1)) return void 0;
985
+ try {
986
+ return TapTreeCoder.encode(toBIP371TapTree(value));
987
+ } catch {
988
+ return void 0;
989
+ }
990
+ }
991
+ function psbtInputsCount(global) {
992
+ for (const { key, value } of global) {
993
+ if (key.length !== 1) continue;
994
+ if (key[0] === PSBT_GLOBAL_UNSIGNED_TX) return btcSigner.RawTx.decode(value).inputs.length;
995
+ if (key[0] === PSBT_GLOBAL_INPUT_COUNT) return readCompactSize(value, 0)[0];
996
+ }
997
+ throw new Error("PSBT: missing input count");
998
+ }
999
+ function readPsbtMaps(psbt) {
1000
+ for (let i = 0; i < PSBT_MAGIC.length; i++) {
1001
+ if (psbt[i] !== PSBT_MAGIC[i]) throw new Error("PSBT: invalid magic");
1002
+ }
1003
+ const maps = [];
1004
+ let pos = PSBT_MAGIC.length;
1005
+ while (pos < psbt.length) {
1006
+ const map = [];
1007
+ for (; ; ) {
1008
+ const [keyLen, afterKeyLen] = readCompactSize(psbt, pos);
1009
+ pos = afterKeyLen;
1010
+ if (keyLen === 0) break;
1011
+ const key = readBytes(psbt, pos, keyLen);
1012
+ pos += keyLen;
1013
+ const [valueLen, afterValueLen] = readCompactSize(psbt, pos);
1014
+ pos = afterValueLen;
1015
+ const value = readBytes(psbt, pos, valueLen);
1016
+ pos += valueLen;
1017
+ map.push({ key, value });
1018
+ }
1019
+ maps.push(map);
1020
+ }
1021
+ if (maps.length === 0) throw new Error("PSBT: missing global map");
1022
+ return maps;
1023
+ }
1024
+ function writePsbtMaps(maps) {
1025
+ const parts = [PSBT_MAGIC];
1026
+ for (const map of maps) {
1027
+ for (const { key, value } of map) {
1028
+ parts.push(writeCompactSize(key.length), key, writeCompactSize(value.length), value);
1029
+ }
1030
+ parts.push(new Uint8Array([0]));
1031
+ }
1032
+ return utils_js.concatBytes(...parts);
1033
+ }
1034
+ function readBytes(data, pos, len) {
1035
+ if (pos + len > data.length) throw new Error("PSBT: unexpected end of data");
1036
+ return data.slice(pos, pos + len);
1037
+ }
1038
+ function readCompactSize(data, pos) {
1039
+ const first = readBytes(data, pos, 1)[0];
1040
+ if (first < 253) return [first, pos + 1];
1041
+ const width = first === 253 ? 2 : first === 254 ? 4 : 8;
1042
+ const bytes = readBytes(data, pos + 1, width);
1043
+ let value = 0;
1044
+ for (let i = width - 1; i >= 0; i--) value = value * 256 + bytes[i];
1045
+ if (!Number.isSafeInteger(value)) throw new Error("PSBT: compact size too large");
1046
+ return [value, pos + 1 + width];
1047
+ }
1048
+ function writeCompactSize(value) {
1049
+ if (value < 253) return new Uint8Array([value]);
1050
+ const width = value <= 65535 ? 2 : value <= 4294967295 ? 4 : 8;
1051
+ const out = new Uint8Array(1 + width);
1052
+ out[0] = width === 2 ? 253 : width === 4 ? 254 : 255;
1053
+ for (let i = 0, v = value; i < width; i++, v = Math.floor(v / 256)) out[1 + i] = v % 256;
1054
+ return out;
1055
+ }
1056
+ function formatSighash(type) {
1057
+ return `0x${type.toString(16).padStart(2, "0")}`;
1058
+ }
1059
+ function assertAllowedSighashTypes(tx, allowedSighashTypes = [btcSigner.SigHash.DEFAULT]) {
1060
+ for (let i = 0; i < tx.inputsLength; i++) {
1061
+ const declared = tx.getInput(i).sighashType;
1062
+ if (declared === void 0) continue;
1063
+ if (!allowedSighashTypes.includes(declared)) {
1064
+ throw new Error(`Unallowed sighash type ${formatSighash(declared)} for input ${i}.`);
1065
+ }
1066
+ }
1067
+ }
1068
+ var ArkPsbtFieldKey = /* @__PURE__ */ ((ArkPsbtFieldKey2) => {
1069
+ ArkPsbtFieldKey2["VtxoTaprootTree"] = "taptree";
1070
+ ArkPsbtFieldKey2["VtxoTreeExpiry"] = "expiry";
1071
+ ArkPsbtFieldKey2["Cosigner"] = "cosigner";
1072
+ ArkPsbtFieldKey2["ConditionWitness"] = "condition";
1073
+ ArkPsbtFieldKey2["PrevArkTx"] = "prevarktx";
1074
+ ArkPsbtFieldKey2["PrevoutTx"] = "prevouttx";
1075
+ return ArkPsbtFieldKey2;
1076
+ })(ArkPsbtFieldKey || {});
1077
+ var ArkPsbtFieldKeyType = 222;
1078
+ function setArkPsbtField(tx, inputIndex, coder, value) {
1079
+ tx.updateInput(inputIndex, {
1080
+ unknown: [...tx.getInput(inputIndex)?.unknown ?? [], coder.encode(value)]
1081
+ });
1082
+ }
1083
+ function getArkPsbtFields(tx, inputIndex, coder) {
1084
+ const unknown = tx.getInput(inputIndex)?.unknown ?? [];
1085
+ const fields = [];
1086
+ for (const u of unknown) {
1087
+ const v = coder.decode(u);
1088
+ if (v !== null) fields.push(v);
1089
+ }
1090
+ return fields;
1091
+ }
1092
+ var VtxoTaprootTree = {
1093
+ key: "taptree" /* VtxoTaprootTree */,
1094
+ encode: (value) => [
1095
+ {
1096
+ type: ArkPsbtFieldKeyType,
1097
+ key: encodedPsbtFieldKey["taptree" /* VtxoTaprootTree */]
1098
+ },
1099
+ value
1100
+ ],
1101
+ decode: (value) => nullIfCatch(() => {
1102
+ if (!checkKeyMatch(value[0], "taptree" /* VtxoTaprootTree */)) return null;
1103
+ return value[1];
1104
+ })
1105
+ };
1106
+ var ConditionWitness = {
1107
+ key: "condition" /* ConditionWitness */,
1108
+ encode: (value) => [
1109
+ {
1110
+ type: ArkPsbtFieldKeyType,
1111
+ key: encodedPsbtFieldKey["condition" /* ConditionWitness */]
1112
+ },
1113
+ btcSigner.RawWitness.encode(value)
1114
+ ],
1115
+ decode: (value) => nullIfCatch(() => {
1116
+ if (!checkKeyMatch(value[0], "condition" /* ConditionWitness */)) return null;
1117
+ return btcSigner.RawWitness.decode(value[1]);
1118
+ })
1119
+ };
1120
+ var PrevArkTxField = {
1121
+ key: "prevarktx" /* PrevArkTx */,
1122
+ encode: (value) => [
1123
+ {
1124
+ type: ArkPsbtFieldKeyType,
1125
+ key: encodedPsbtFieldKey["prevarktx" /* PrevArkTx */]
1126
+ },
1127
+ value
1128
+ ],
1129
+ decode: (value) => nullIfCatch(() => {
1130
+ if (!checkKeyMatch(value[0], "prevarktx" /* PrevArkTx */)) return null;
1131
+ return value[1];
1132
+ })
1133
+ };
1134
+ var PrevoutTxField = {
1135
+ key: "prevouttx" /* PrevoutTx */,
1136
+ encode: (value) => [
1137
+ {
1138
+ type: ArkPsbtFieldKeyType,
1139
+ key: encodedPsbtFieldKey["prevouttx" /* PrevoutTx */]
1140
+ },
1141
+ value
1142
+ ],
1143
+ decode: (value) => nullIfCatch(() => {
1144
+ if (!checkKeyMatch(value[0], "prevouttx" /* PrevoutTx */)) return null;
1145
+ return value[1];
1146
+ })
1147
+ };
1148
+ var CosignerPublicKey = {
1149
+ key: "cosigner" /* Cosigner */,
1150
+ encode: (value) => [
1151
+ {
1152
+ type: ArkPsbtFieldKeyType,
1153
+ key: new Uint8Array([...encodedPsbtFieldKey["cosigner" /* Cosigner */], value.index])
1154
+ },
1155
+ value.key
1156
+ ],
1157
+ decode: (unknown) => nullIfCatch(() => {
1158
+ if (!checkKeyMatch(unknown[0], "cosigner" /* Cosigner */, true)) return null;
1159
+ return {
1160
+ index: unknown[0].key[unknown[0].key.length - 1],
1161
+ key: unknown[1]
1162
+ };
1163
+ })
1164
+ };
1165
+ var VtxoTreeExpiry = {
1166
+ key: "expiry" /* VtxoTreeExpiry */,
1167
+ encode: (value) => [
1168
+ {
1169
+ type: ArkPsbtFieldKeyType,
1170
+ key: encodedPsbtFieldKey["expiry" /* VtxoTreeExpiry */]
1171
+ },
1172
+ btcSigner.ScriptNum(6, true).encode(value.value === 0n ? 0n : value.value)
1173
+ ],
1174
+ decode: (unknown) => nullIfCatch(() => {
1175
+ if (!checkKeyMatch(unknown[0], "expiry" /* VtxoTreeExpiry */)) return null;
1176
+ const v = btcSigner.ScriptNum(6, true).decode(unknown[1]);
1177
+ if (!v) return null;
1178
+ return sequenceToTimelock(Number(v));
1179
+ })
1180
+ };
1181
+ var encodedPsbtFieldKey = Object.fromEntries(
1182
+ Object.values(ArkPsbtFieldKey).map((key) => [key, new TextEncoder().encode(key)])
1183
+ );
1184
+ var nullIfCatch = (fn) => {
1185
+ try {
1186
+ return fn();
1187
+ } catch {
1188
+ return null;
1189
+ }
1190
+ };
1191
+ function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
1192
+ if (key.type !== ArkPsbtFieldKeyType) return false;
1193
+ const expected = encodedPsbtFieldKey[arkPsbtFieldKey];
1194
+ if (key.key.length < expected.length) return false;
1195
+ if (!prefixOnly && key.key.length !== expected.length) return false;
1196
+ for (let i = 0; i < expected.length; i++) {
1197
+ if (key.key[i] !== expected[i]) return false;
1198
+ }
1199
+ return true;
1200
+ }
1021
1201
 
1022
1202
  // src/providers/eventSource.ts
1023
1203
  var GUIDANCE = "Pass one with configureEventSource(url => new EventSource(url)) \u2014 in Node, from the `eventsource` package or by running with --experimental-eventsource. In React Native, use ExpoArkProvider / ExpoIndexerProvider instead.";
@@ -1044,7 +1224,7 @@ function resolveEventSource(override) {
1044
1224
  }
1045
1225
 
1046
1226
  // package.json
1047
- var version = "0.4.78";
1227
+ var version = "0.4.79";
1048
1228
 
1049
1229
  // src/utils/fetch.ts
1050
1230
  var buildVersion = "0.9.9";
@@ -1285,7 +1465,6 @@ var OP_RETURN_EMPTY_PKSCRIPT = new Uint8Array([btcSigner.OP.RETURN]);
1285
1465
  var ZERO_32 = new Uint8Array(32).fill(0);
1286
1466
  var MAX_INDEX = 4294967295;
1287
1467
  var TAG_INTENT_PROOF = "ark-intent-proof-message";
1288
- var PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE = 9;
1289
1468
  function validateInput(input) {
1290
1469
  if (input.index === void 0) throw new Error("intent proof input requires index");
1291
1470
  if (input.txid === void 0) throw new Error("intent proof input requires txid");
@@ -1332,8 +1511,9 @@ function craftToSignTx(toSpend, inputs, outputs, message) {
1332
1511
  version: 2,
1333
1512
  lockTime: 0
1334
1513
  });
1514
+ const { unknown: _, ...toSpendInput } = firstInput;
1335
1515
  tx.addInput({
1336
- ...firstInput,
1516
+ ...toSpendInput,
1337
1517
  txid: toSpend.id,
1338
1518
  index: 0,
1339
1519
  witnessUtxo: {
@@ -1367,14 +1547,7 @@ function craftToSignTx(toSpend, inputs, outputs, message) {
1367
1547
  script: output.script
1368
1548
  });
1369
1549
  }
1370
- const global = tx.global;
1371
- global.unknown = [
1372
- ...global.unknown ?? [],
1373
- [
1374
- { type: PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE, key: new Uint8Array() },
1375
- new TextEncoder().encode(message)
1376
- ]
1377
- ];
1550
+ tx.global.genericSignedMessage = new TextEncoder().encode(message);
1378
1551
  return tx;
1379
1552
  }
1380
1553
  function hashMessage(message, tag = TAG_INTENT_PROOF) {
@@ -2572,9 +2745,10 @@ exports.sequenceToTimelock = sequenceToTimelock;
2572
2745
  exports.setArkPsbtField = setArkPsbtField;
2573
2746
  exports.throwIfHttpUnavailable = throwIfHttpUnavailable;
2574
2747
  exports.timelockToSequence = timelockToSequence;
2748
+ exports.toBIP371TapTree = toBIP371TapTree;
2575
2749
  exports.toBatchExpiry = toBatchExpiry;
2576
2750
  exports.toOffchainInputFeeParams = toOffchainInputFeeParams;
2577
2751
  exports.toProviderUnavailable = toProviderUnavailable;
2578
2752
  exports.toVirtualStatus = toVirtualStatus;
2579
- //# sourceMappingURL=chunk-PWIESIO6.cjs.map
2580
- //# sourceMappingURL=chunk-PWIESIO6.cjs.map
2753
+ //# sourceMappingURL=chunk-DAYCCIRO.cjs.map
2754
+ //# sourceMappingURL=chunk-DAYCCIRO.cjs.map