@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,192 +1,14 @@
1
- import { ScriptNum, OP, p2tr_ms, Script, SigHash, p2tr, TAPROOT_UNSPENDABLE_KEY, Address, RawWitness, Transaction as Transaction$1 } from '@scure/btc-signer';
2
- import * as bip68 from 'bip68';
3
- import { TEST_NETWORK, NETWORK } from '@scure/btc-signer/utils.js';
1
+ import { TEST_NETWORK, NETWORK, concatBytes } from '@scure/btc-signer/utils.js';
4
2
  import { hex, bech32m } from '@scure/base';
5
- import { Script as Script$1 } from '@scure/btc-signer/script.js';
3
+ import { VarBytes, Script as Script$1 } from '@scure/btc-signer/script.js';
4
+ import * as bip68 from 'bip68';
5
+ import { ScriptNum, OP, p2tr_ms, Script, SigHash, p2tr, taprootNumsKey, Address, RawWitness, Transaction as Transaction$1, RawTx } from '@scure/btc-signer';
6
6
  import { TAP_LEAF_VERSION } from '@scure/btc-signer/payment.js';
7
+ import * as P from 'micro-packed';
7
8
  import { PSBTOutput } from '@scure/btc-signer/psbt.js';
8
9
  import { schnorr } from '@noble/curves/secp256k1.js';
9
10
 
10
- // src/utils/transaction.ts
11
- var Transaction = class extends Transaction$1 {
12
- static ARK_TX_OPTS = {
13
- allowUnknown: true,
14
- allowUnknownOutputs: true,
15
- allowUnknownInputs: true
16
- };
17
- constructor(opts) {
18
- super(withArkOpts(opts));
19
- }
20
- static fromPSBT(psbt_, opts) {
21
- return Transaction$1.fromPSBT(psbt_, withArkOpts(opts));
22
- }
23
- static fromRaw(raw, opts) {
24
- return Transaction$1.fromRaw(raw, withArkOpts(opts));
25
- }
26
- };
27
- function withArkOpts(opts) {
28
- return { ...Transaction.ARK_TX_OPTS, ...opts };
29
- }
30
- function formatSighash(type) {
31
- return `0x${type.toString(16).padStart(2, "0")}`;
32
- }
33
- function assertAllowedSighashTypes(tx, allowedSighashTypes = [SigHash.DEFAULT]) {
34
- for (let i = 0; i < tx.inputsLength; i++) {
35
- const declared = tx.getInput(i).sighashType;
36
- if (declared === void 0) continue;
37
- if (!allowedSighashTypes.includes(declared)) {
38
- throw new Error(`Unallowed sighash type ${formatSighash(declared)} for input ${i}.`);
39
- }
40
- }
41
- }
42
- function timelockToSequence(timelock) {
43
- return bip68.encode(
44
- timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
45
- );
46
- }
47
- function sequenceToTimelock(sequence) {
48
- const decoded = bip68.decode(sequence);
49
- if ("blocks" in decoded && decoded.blocks !== void 0) {
50
- return { type: "blocks", value: BigInt(decoded.blocks) };
51
- }
52
- if ("seconds" in decoded && decoded.seconds !== void 0) {
53
- return { type: "seconds", value: BigInt(decoded.seconds) };
54
- }
55
- throw new Error(`Invalid BIP68 sequence: ${sequence}`);
56
- }
57
- var ArkPsbtFieldKey = /* @__PURE__ */ ((ArkPsbtFieldKey2) => {
58
- ArkPsbtFieldKey2["VtxoTaprootTree"] = "taptree";
59
- ArkPsbtFieldKey2["VtxoTreeExpiry"] = "expiry";
60
- ArkPsbtFieldKey2["Cosigner"] = "cosigner";
61
- ArkPsbtFieldKey2["ConditionWitness"] = "condition";
62
- ArkPsbtFieldKey2["PrevArkTx"] = "prevarktx";
63
- ArkPsbtFieldKey2["PrevoutTx"] = "prevouttx";
64
- return ArkPsbtFieldKey2;
65
- })(ArkPsbtFieldKey || {});
66
- var ArkPsbtFieldKeyType = 222;
67
- function setArkPsbtField(tx, inputIndex, coder, value) {
68
- tx.updateInput(inputIndex, {
69
- unknown: [...tx.getInput(inputIndex)?.unknown ?? [], coder.encode(value)]
70
- });
71
- }
72
- function getArkPsbtFields(tx, inputIndex, coder) {
73
- const unknown = tx.getInput(inputIndex)?.unknown ?? [];
74
- const fields = [];
75
- for (const u of unknown) {
76
- const v = coder.decode(u);
77
- if (v !== null) fields.push(v);
78
- }
79
- return fields;
80
- }
81
- var VtxoTaprootTree = {
82
- key: "taptree" /* VtxoTaprootTree */,
83
- encode: (value) => [
84
- {
85
- type: ArkPsbtFieldKeyType,
86
- key: encodedPsbtFieldKey["taptree" /* VtxoTaprootTree */]
87
- },
88
- value
89
- ],
90
- decode: (value) => nullIfCatch(() => {
91
- if (!checkKeyMatch(value[0], "taptree" /* VtxoTaprootTree */)) return null;
92
- return value[1];
93
- })
94
- };
95
- var ConditionWitness = {
96
- key: "condition" /* ConditionWitness */,
97
- encode: (value) => [
98
- {
99
- type: ArkPsbtFieldKeyType,
100
- key: encodedPsbtFieldKey["condition" /* ConditionWitness */]
101
- },
102
- RawWitness.encode(value)
103
- ],
104
- decode: (value) => nullIfCatch(() => {
105
- if (!checkKeyMatch(value[0], "condition" /* ConditionWitness */)) return null;
106
- return RawWitness.decode(value[1]);
107
- })
108
- };
109
- var PrevArkTxField = {
110
- key: "prevarktx" /* PrevArkTx */,
111
- encode: (value) => [
112
- {
113
- type: ArkPsbtFieldKeyType,
114
- key: encodedPsbtFieldKey["prevarktx" /* PrevArkTx */]
115
- },
116
- value
117
- ],
118
- decode: (value) => nullIfCatch(() => {
119
- if (!checkKeyMatch(value[0], "prevarktx" /* PrevArkTx */)) return null;
120
- return value[1];
121
- })
122
- };
123
- var PrevoutTxField = {
124
- key: "prevouttx" /* PrevoutTx */,
125
- encode: (value) => [
126
- {
127
- type: ArkPsbtFieldKeyType,
128
- key: encodedPsbtFieldKey["prevouttx" /* PrevoutTx */]
129
- },
130
- value
131
- ],
132
- decode: (value) => nullIfCatch(() => {
133
- if (!checkKeyMatch(value[0], "prevouttx" /* PrevoutTx */)) return null;
134
- return value[1];
135
- })
136
- };
137
- var CosignerPublicKey = {
138
- key: "cosigner" /* Cosigner */,
139
- encode: (value) => [
140
- {
141
- type: ArkPsbtFieldKeyType,
142
- key: new Uint8Array([...encodedPsbtFieldKey["cosigner" /* Cosigner */], value.index])
143
- },
144
- value.key
145
- ],
146
- decode: (unknown) => nullIfCatch(() => {
147
- if (!checkKeyMatch(unknown[0], "cosigner" /* Cosigner */, true)) return null;
148
- return {
149
- index: unknown[0].key[unknown[0].key.length - 1],
150
- key: unknown[1]
151
- };
152
- })
153
- };
154
- var VtxoTreeExpiry = {
155
- key: "expiry" /* VtxoTreeExpiry */,
156
- encode: (value) => [
157
- {
158
- type: ArkPsbtFieldKeyType,
159
- key: encodedPsbtFieldKey["expiry" /* VtxoTreeExpiry */]
160
- },
161
- ScriptNum(6, true).encode(value.value === 0n ? 0n : value.value)
162
- ],
163
- decode: (unknown) => nullIfCatch(() => {
164
- if (!checkKeyMatch(unknown[0], "expiry" /* VtxoTreeExpiry */)) return null;
165
- const v = ScriptNum(6, true).decode(unknown[1]);
166
- if (!v) return null;
167
- return sequenceToTimelock(Number(v));
168
- })
169
- };
170
- var encodedPsbtFieldKey = Object.fromEntries(
171
- Object.values(ArkPsbtFieldKey).map((key) => [key, new TextEncoder().encode(key)])
172
- );
173
- var nullIfCatch = (fn) => {
174
- try {
175
- return fn();
176
- } catch {
177
- return null;
178
- }
179
- };
180
- function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
181
- if (key.type !== ArkPsbtFieldKeyType) return false;
182
- const expected = encodedPsbtFieldKey[arkPsbtFieldKey];
183
- if (key.key.length < expected.length) return false;
184
- if (!prefixOnly && key.key.length !== expected.length) return false;
185
- for (let i = 0; i < expected.length; i++) {
186
- if (key.key[i] !== expected[i]) return false;
187
- }
188
- return true;
189
- }
11
+ // src/networks.ts
190
12
  var getNetwork = (network) => {
191
13
  const found = networks[network];
192
14
  if (!found) throw new Error(`Unsupported network: ${network}`);
@@ -322,6 +144,21 @@ var ArkAddress = class _ArkAddress {
322
144
  return Script$1.encode(["RETURN", this.vtxoTaprootKey]);
323
145
  }
324
146
  };
147
+ function timelockToSequence(timelock) {
148
+ return bip68.encode(
149
+ timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
150
+ );
151
+ }
152
+ function sequenceToTimelock(sequence) {
153
+ const decoded = bip68.decode(sequence);
154
+ if ("blocks" in decoded && decoded.blocks !== void 0) {
155
+ return { type: "blocks", value: BigInt(decoded.blocks) };
156
+ }
157
+ if ("seconds" in decoded && decoded.seconds !== void 0) {
158
+ return { type: "seconds", value: BigInt(decoded.seconds) };
159
+ }
160
+ throw new Error(`Invalid BIP68 sequence: ${sequence}`);
161
+ }
325
162
  var MinimalScriptNum = ScriptNum(void 0, true);
326
163
  function decodeTapscript(script) {
327
164
  const types = [
@@ -854,7 +691,25 @@ function assembleBtcdTaprootTree(scripts) {
854
691
  }
855
692
  return branches[0];
856
693
  }
857
- var TapTreeCoder = PSBTOutput.tapTree[2];
694
+ function btcdLeafLayout(count) {
695
+ const placeholders = Array.from({ length: count }, () => new Uint8Array());
696
+ const indexOf = new Map(placeholders.map((script, index) => [script, index]));
697
+ const layout = [];
698
+ const walk = (node, depth) => {
699
+ if (Array.isArray(node)) {
700
+ walk(node[0], depth + 1);
701
+ walk(node[1], depth + 1);
702
+ } else {
703
+ layout.push({ index: indexOf.get(node.script), depth });
704
+ }
705
+ };
706
+ walk(assembleBtcdTaprootTree(placeholders), 0);
707
+ return layout;
708
+ }
709
+ var TapTreeCoder = P.array(
710
+ null,
711
+ P.struct({ depth: P.U8, version: P.U8, script: VarBytes })
712
+ );
858
713
  function scriptFromTapLeafScript(leaf) {
859
714
  return leaf[1].subarray(0, leaf[1].length - 1);
860
715
  }
@@ -874,7 +729,7 @@ var VtxoScript = class _VtxoScript {
874
729
  constructor(scripts) {
875
730
  this.scripts = scripts;
876
731
  const tapTree = assembleBtcdTaprootTree(scripts);
877
- const payment = p2tr(TAPROOT_UNSPENDABLE_KEY, tapTree, void 0, true);
732
+ const payment = p2tr(taprootNumsKey(), tapTree, void 0, true);
878
733
  if (!payment.tapLeafScript || payment.tapLeafScript.length !== scripts.length) {
879
734
  throw new Error("invalid scripts");
880
735
  }
@@ -889,15 +744,46 @@ var VtxoScript = class _VtxoScript {
889
744
  /**
890
745
  * Decode a virtual output script from an encoded TapTree.
891
746
  *
747
+ * Leaves are normally read in construction order and their depths
748
+ * ignored: that covers the {@link encode} form (every depth 1) and the
749
+ * placeholder depths other writers use, such as arkd's. The exception is
750
+ * the BIP-371 form {@link toBIP371TapTree} writes, whose leaves are in the
751
+ * tree's DFS order: for some leaf counts (6, 7, 10, ...) that differs from
752
+ * construction order. A tree whose depths are exactly the shape
753
+ * `assembleBtcdTaprootTree` builds is that form, so its leaves are mapped
754
+ * back to construction order first. No construction-order writer produces
755
+ * that depth sequence when the two orders differ, so this never reorders
756
+ * a tree that decoded correctly before.
757
+ *
758
+ * Every leaf must carry the base tapscript version: the tree is rebuilt
759
+ * with {@link TAP_LEAF_VERSION} leaves, so any other version would
760
+ * silently derive a different taproot key.
761
+ *
892
762
  * @param tapTree - Encoded TapTree bytes
893
763
  * @returns Decoded virtual output script
894
764
  * @throws Error if the TapTree cannot be decoded into a valid script set
765
+ * or a leaf carries a version other than {@link TAP_LEAF_VERSION}
895
766
  * @see encode
896
767
  */
897
768
  static decode(tapTree) {
898
769
  const leaves = TapTreeCoder.decode(tapTree);
770
+ const foreign = leaves.find((leaf) => leaf.version !== TAP_LEAF_VERSION);
771
+ if (foreign) {
772
+ throw new Error(`unsupported tap leaf version 0x${foreign.version.toString(16)}`);
773
+ }
899
774
  const scripts = leaves.map((leaf) => leaf.script);
900
- return new _VtxoScript(scripts);
775
+ if (leaves.every((leaf) => leaf.depth === 1)) {
776
+ return new _VtxoScript(scripts);
777
+ }
778
+ const layout = btcdLeafLayout(leaves.length);
779
+ if (layout.some((slot, i) => slot.depth !== leaves[i].depth)) {
780
+ return new _VtxoScript(scripts);
781
+ }
782
+ const ordered = new Array(leaves.length);
783
+ layout.forEach((slot, i) => {
784
+ ordered[slot.index] = scripts[i];
785
+ });
786
+ return new _VtxoScript(ordered);
901
787
  }
902
788
  /**
903
789
  * Encode the virtual output script to a TapTree byte representation.
@@ -980,6 +866,15 @@ var VtxoScript = class _VtxoScript {
980
866
  return paths;
981
867
  }
982
868
  };
869
+ function toBIP371TapTree(tapTree) {
870
+ const scripts = TapTreeCoder.decode(tapTree).map((leaf) => leaf.script);
871
+ const vtxoScript = new VtxoScript(scripts);
872
+ return vtxoScript.leaves.map((leaf) => ({
873
+ depth: leaf[0].merklePath.length,
874
+ version: TAP_LEAF_VERSION,
875
+ script: scriptFromTapLeafScript(leaf)
876
+ }));
877
+ }
983
878
  function getSequence(tapLeafScript) {
984
879
  let sequence = void 0;
985
880
  try {
@@ -996,6 +891,290 @@ function getSequence(tapLeafScript) {
996
891
  }
997
892
  return sequence;
998
893
  }
894
+ var Transaction = class extends Transaction$1 {
895
+ static ARK_TX_OPTS = {
896
+ unknown: "ignore",
897
+ allowUnknownOutputs: true,
898
+ allowUnknownInputs: true
899
+ };
900
+ constructor(opts) {
901
+ super(withArkOpts(opts));
902
+ }
903
+ /**
904
+ * Decode a PSBT. PSBTs carrying a legacy `PSBT_OUT_TAP_TREE` (see
905
+ * {@link repairLegacyOutputTapTrees}) are repaired and decoded again
906
+ * instead of being rejected.
907
+ */
908
+ static fromPSBT(psbt_, opts) {
909
+ try {
910
+ return Transaction$1.fromPSBT(psbt_, withArkOpts(opts));
911
+ } catch (err) {
912
+ const repaired = repairLegacyOutputTapTrees(psbt_);
913
+ if (!repaired) throw err;
914
+ return Transaction$1.fromPSBT(repaired, withArkOpts(opts));
915
+ }
916
+ }
917
+ static fromRaw(raw, opts) {
918
+ return Transaction$1.fromRaw(raw, withArkOpts(opts));
919
+ }
920
+ };
921
+ function withArkOpts(opts) {
922
+ return { ...Transaction.ARK_TX_OPTS, ...opts };
923
+ }
924
+ var PSBT_MAGIC = new Uint8Array([112, 115, 98, 116, 255]);
925
+ var PSBT_GLOBAL_UNSIGNED_TX = 0;
926
+ var PSBT_GLOBAL_INPUT_COUNT = 4;
927
+ var PSBT_OUT_TAP_TREE = 6;
928
+ function repairLegacyOutputTapTrees(psbt) {
929
+ let maps;
930
+ let inputsCount;
931
+ try {
932
+ maps = readPsbtMaps(psbt);
933
+ inputsCount = psbtInputsCount(maps[0]);
934
+ } catch {
935
+ return void 0;
936
+ }
937
+ let repaired = false;
938
+ for (const output of maps.slice(1 + inputsCount)) {
939
+ for (const kv of output) {
940
+ if (kv.key.length !== 1 || kv.key[0] !== PSBT_OUT_TAP_TREE) continue;
941
+ const value = legacyTapTreeToBIP371(kv.value);
942
+ if (!value) continue;
943
+ kv.value = value;
944
+ repaired = true;
945
+ }
946
+ }
947
+ return repaired ? writePsbtMaps(maps) : void 0;
948
+ }
949
+ function legacyTapTreeToBIP371(value) {
950
+ try {
951
+ PSBTOutput.tapTree[2].decode(value);
952
+ return void 0;
953
+ } catch {
954
+ }
955
+ let leaves;
956
+ try {
957
+ leaves = TapTreeCoder.decode(value);
958
+ } catch {
959
+ return void 0;
960
+ }
961
+ if (leaves.length === 0 || leaves.some((leaf) => leaf.depth !== 1)) return void 0;
962
+ try {
963
+ return TapTreeCoder.encode(toBIP371TapTree(value));
964
+ } catch {
965
+ return void 0;
966
+ }
967
+ }
968
+ function psbtInputsCount(global) {
969
+ for (const { key, value } of global) {
970
+ if (key.length !== 1) continue;
971
+ if (key[0] === PSBT_GLOBAL_UNSIGNED_TX) return RawTx.decode(value).inputs.length;
972
+ if (key[0] === PSBT_GLOBAL_INPUT_COUNT) return readCompactSize(value, 0)[0];
973
+ }
974
+ throw new Error("PSBT: missing input count");
975
+ }
976
+ function readPsbtMaps(psbt) {
977
+ for (let i = 0; i < PSBT_MAGIC.length; i++) {
978
+ if (psbt[i] !== PSBT_MAGIC[i]) throw new Error("PSBT: invalid magic");
979
+ }
980
+ const maps = [];
981
+ let pos = PSBT_MAGIC.length;
982
+ while (pos < psbt.length) {
983
+ const map = [];
984
+ for (; ; ) {
985
+ const [keyLen, afterKeyLen] = readCompactSize(psbt, pos);
986
+ pos = afterKeyLen;
987
+ if (keyLen === 0) break;
988
+ const key = readBytes(psbt, pos, keyLen);
989
+ pos += keyLen;
990
+ const [valueLen, afterValueLen] = readCompactSize(psbt, pos);
991
+ pos = afterValueLen;
992
+ const value = readBytes(psbt, pos, valueLen);
993
+ pos += valueLen;
994
+ map.push({ key, value });
995
+ }
996
+ maps.push(map);
997
+ }
998
+ if (maps.length === 0) throw new Error("PSBT: missing global map");
999
+ return maps;
1000
+ }
1001
+ function writePsbtMaps(maps) {
1002
+ const parts = [PSBT_MAGIC];
1003
+ for (const map of maps) {
1004
+ for (const { key, value } of map) {
1005
+ parts.push(writeCompactSize(key.length), key, writeCompactSize(value.length), value);
1006
+ }
1007
+ parts.push(new Uint8Array([0]));
1008
+ }
1009
+ return concatBytes(...parts);
1010
+ }
1011
+ function readBytes(data, pos, len) {
1012
+ if (pos + len > data.length) throw new Error("PSBT: unexpected end of data");
1013
+ return data.slice(pos, pos + len);
1014
+ }
1015
+ function readCompactSize(data, pos) {
1016
+ const first = readBytes(data, pos, 1)[0];
1017
+ if (first < 253) return [first, pos + 1];
1018
+ const width = first === 253 ? 2 : first === 254 ? 4 : 8;
1019
+ const bytes = readBytes(data, pos + 1, width);
1020
+ let value = 0;
1021
+ for (let i = width - 1; i >= 0; i--) value = value * 256 + bytes[i];
1022
+ if (!Number.isSafeInteger(value)) throw new Error("PSBT: compact size too large");
1023
+ return [value, pos + 1 + width];
1024
+ }
1025
+ function writeCompactSize(value) {
1026
+ if (value < 253) return new Uint8Array([value]);
1027
+ const width = value <= 65535 ? 2 : value <= 4294967295 ? 4 : 8;
1028
+ const out = new Uint8Array(1 + width);
1029
+ out[0] = width === 2 ? 253 : width === 4 ? 254 : 255;
1030
+ for (let i = 0, v = value; i < width; i++, v = Math.floor(v / 256)) out[1 + i] = v % 256;
1031
+ return out;
1032
+ }
1033
+ function formatSighash(type) {
1034
+ return `0x${type.toString(16).padStart(2, "0")}`;
1035
+ }
1036
+ function assertAllowedSighashTypes(tx, allowedSighashTypes = [SigHash.DEFAULT]) {
1037
+ for (let i = 0; i < tx.inputsLength; i++) {
1038
+ const declared = tx.getInput(i).sighashType;
1039
+ if (declared === void 0) continue;
1040
+ if (!allowedSighashTypes.includes(declared)) {
1041
+ throw new Error(`Unallowed sighash type ${formatSighash(declared)} for input ${i}.`);
1042
+ }
1043
+ }
1044
+ }
1045
+ var ArkPsbtFieldKey = /* @__PURE__ */ ((ArkPsbtFieldKey2) => {
1046
+ ArkPsbtFieldKey2["VtxoTaprootTree"] = "taptree";
1047
+ ArkPsbtFieldKey2["VtxoTreeExpiry"] = "expiry";
1048
+ ArkPsbtFieldKey2["Cosigner"] = "cosigner";
1049
+ ArkPsbtFieldKey2["ConditionWitness"] = "condition";
1050
+ ArkPsbtFieldKey2["PrevArkTx"] = "prevarktx";
1051
+ ArkPsbtFieldKey2["PrevoutTx"] = "prevouttx";
1052
+ return ArkPsbtFieldKey2;
1053
+ })(ArkPsbtFieldKey || {});
1054
+ var ArkPsbtFieldKeyType = 222;
1055
+ function setArkPsbtField(tx, inputIndex, coder, value) {
1056
+ tx.updateInput(inputIndex, {
1057
+ unknown: [...tx.getInput(inputIndex)?.unknown ?? [], coder.encode(value)]
1058
+ });
1059
+ }
1060
+ function getArkPsbtFields(tx, inputIndex, coder) {
1061
+ const unknown = tx.getInput(inputIndex)?.unknown ?? [];
1062
+ const fields = [];
1063
+ for (const u of unknown) {
1064
+ const v = coder.decode(u);
1065
+ if (v !== null) fields.push(v);
1066
+ }
1067
+ return fields;
1068
+ }
1069
+ var VtxoTaprootTree = {
1070
+ key: "taptree" /* VtxoTaprootTree */,
1071
+ encode: (value) => [
1072
+ {
1073
+ type: ArkPsbtFieldKeyType,
1074
+ key: encodedPsbtFieldKey["taptree" /* VtxoTaprootTree */]
1075
+ },
1076
+ value
1077
+ ],
1078
+ decode: (value) => nullIfCatch(() => {
1079
+ if (!checkKeyMatch(value[0], "taptree" /* VtxoTaprootTree */)) return null;
1080
+ return value[1];
1081
+ })
1082
+ };
1083
+ var ConditionWitness = {
1084
+ key: "condition" /* ConditionWitness */,
1085
+ encode: (value) => [
1086
+ {
1087
+ type: ArkPsbtFieldKeyType,
1088
+ key: encodedPsbtFieldKey["condition" /* ConditionWitness */]
1089
+ },
1090
+ RawWitness.encode(value)
1091
+ ],
1092
+ decode: (value) => nullIfCatch(() => {
1093
+ if (!checkKeyMatch(value[0], "condition" /* ConditionWitness */)) return null;
1094
+ return RawWitness.decode(value[1]);
1095
+ })
1096
+ };
1097
+ var PrevArkTxField = {
1098
+ key: "prevarktx" /* PrevArkTx */,
1099
+ encode: (value) => [
1100
+ {
1101
+ type: ArkPsbtFieldKeyType,
1102
+ key: encodedPsbtFieldKey["prevarktx" /* PrevArkTx */]
1103
+ },
1104
+ value
1105
+ ],
1106
+ decode: (value) => nullIfCatch(() => {
1107
+ if (!checkKeyMatch(value[0], "prevarktx" /* PrevArkTx */)) return null;
1108
+ return value[1];
1109
+ })
1110
+ };
1111
+ var PrevoutTxField = {
1112
+ key: "prevouttx" /* PrevoutTx */,
1113
+ encode: (value) => [
1114
+ {
1115
+ type: ArkPsbtFieldKeyType,
1116
+ key: encodedPsbtFieldKey["prevouttx" /* PrevoutTx */]
1117
+ },
1118
+ value
1119
+ ],
1120
+ decode: (value) => nullIfCatch(() => {
1121
+ if (!checkKeyMatch(value[0], "prevouttx" /* PrevoutTx */)) return null;
1122
+ return value[1];
1123
+ })
1124
+ };
1125
+ var CosignerPublicKey = {
1126
+ key: "cosigner" /* Cosigner */,
1127
+ encode: (value) => [
1128
+ {
1129
+ type: ArkPsbtFieldKeyType,
1130
+ key: new Uint8Array([...encodedPsbtFieldKey["cosigner" /* Cosigner */], value.index])
1131
+ },
1132
+ value.key
1133
+ ],
1134
+ decode: (unknown) => nullIfCatch(() => {
1135
+ if (!checkKeyMatch(unknown[0], "cosigner" /* Cosigner */, true)) return null;
1136
+ return {
1137
+ index: unknown[0].key[unknown[0].key.length - 1],
1138
+ key: unknown[1]
1139
+ };
1140
+ })
1141
+ };
1142
+ var VtxoTreeExpiry = {
1143
+ key: "expiry" /* VtxoTreeExpiry */,
1144
+ encode: (value) => [
1145
+ {
1146
+ type: ArkPsbtFieldKeyType,
1147
+ key: encodedPsbtFieldKey["expiry" /* VtxoTreeExpiry */]
1148
+ },
1149
+ ScriptNum(6, true).encode(value.value === 0n ? 0n : value.value)
1150
+ ],
1151
+ decode: (unknown) => nullIfCatch(() => {
1152
+ if (!checkKeyMatch(unknown[0], "expiry" /* VtxoTreeExpiry */)) return null;
1153
+ const v = ScriptNum(6, true).decode(unknown[1]);
1154
+ if (!v) return null;
1155
+ return sequenceToTimelock(Number(v));
1156
+ })
1157
+ };
1158
+ var encodedPsbtFieldKey = Object.fromEntries(
1159
+ Object.values(ArkPsbtFieldKey).map((key) => [key, new TextEncoder().encode(key)])
1160
+ );
1161
+ var nullIfCatch = (fn) => {
1162
+ try {
1163
+ return fn();
1164
+ } catch {
1165
+ return null;
1166
+ }
1167
+ };
1168
+ function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
1169
+ if (key.type !== ArkPsbtFieldKeyType) return false;
1170
+ const expected = encodedPsbtFieldKey[arkPsbtFieldKey];
1171
+ if (key.key.length < expected.length) return false;
1172
+ if (!prefixOnly && key.key.length !== expected.length) return false;
1173
+ for (let i = 0; i < expected.length; i++) {
1174
+ if (key.key[i] !== expected[i]) return false;
1175
+ }
1176
+ return true;
1177
+ }
999
1178
 
1000
1179
  // src/providers/eventSource.ts
1001
1180
  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.";
@@ -1022,7 +1201,7 @@ function resolveEventSource(override) {
1022
1201
  }
1023
1202
 
1024
1203
  // package.json
1025
- var version = "0.4.78";
1204
+ var version = "0.4.79";
1026
1205
 
1027
1206
  // src/utils/fetch.ts
1028
1207
  var buildVersion = "0.9.9";
@@ -1263,7 +1442,6 @@ var OP_RETURN_EMPTY_PKSCRIPT = new Uint8Array([OP.RETURN]);
1263
1442
  var ZERO_32 = new Uint8Array(32).fill(0);
1264
1443
  var MAX_INDEX = 4294967295;
1265
1444
  var TAG_INTENT_PROOF = "ark-intent-proof-message";
1266
- var PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE = 9;
1267
1445
  function validateInput(input) {
1268
1446
  if (input.index === void 0) throw new Error("intent proof input requires index");
1269
1447
  if (input.txid === void 0) throw new Error("intent proof input requires txid");
@@ -1310,8 +1488,9 @@ function craftToSignTx(toSpend, inputs, outputs, message) {
1310
1488
  version: 2,
1311
1489
  lockTime: 0
1312
1490
  });
1491
+ const { unknown: _, ...toSpendInput } = firstInput;
1313
1492
  tx.addInput({
1314
- ...firstInput,
1493
+ ...toSpendInput,
1315
1494
  txid: toSpend.id,
1316
1495
  index: 0,
1317
1496
  witnessUtxo: {
@@ -1345,14 +1524,7 @@ function craftToSignTx(toSpend, inputs, outputs, message) {
1345
1524
  script: output.script
1346
1525
  });
1347
1526
  }
1348
- const global = tx.global;
1349
- global.unknown = [
1350
- ...global.unknown ?? [],
1351
- [
1352
- { type: PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE, key: new Uint8Array() },
1353
- new TextEncoder().encode(message)
1354
- ]
1355
- ];
1527
+ tx.global.genericSignedMessage = new TextEncoder().encode(message);
1356
1528
  return tx;
1357
1529
  }
1358
1530
  function hashMessage(message, tag = TAG_INTENT_PROOF) {
@@ -2470,6 +2642,6 @@ function isExpired(vtxo) {
2470
2642
  return n.isSwept || n.expiresAt !== void 0 && n.expiresAt.getTime() <= Date.now();
2471
2643
  }
2472
2644
 
2473
- export { ARKADE_VERSION_HEADERS, ArkAddress, ArkError, ArkErrorName, ArkPsbtFieldKey, ArkPsbtFieldKeyType, BITCOIN_EMULATOR_PUBKEY, CLTVMultisigTapscript, CSVMultisigTapscript, ConditionCSVMultisigTapscript, ConditionMultisigTapscript, ConditionWitness, CosignerPublicKey, DEFAULT_ARKADE_SERVER_URL, DEFAULT_NETWORK, DEFAULT_NETWORK_NAME, DEFAULT_PAGE_SIZE, DigestMismatchError, EventSourceUnavailableError, FetchError, Intent, MUTINYNET_EMULATOR_PUBKEY, MultisigTapscript, OP_RETURN_EMPTY_PKSCRIPT, PrevArkTxField, PrevoutTxField, ProviderUnavailableError, READ_TIMEOUT_MS, REGTEST_EMULATOR_PUBKEY, RestArkProvider, SCRIPT_QUERY_CHUNK_SIZE, ServerResponseMismatchError, SettlementEventType, TapTreeCoder, Transaction, VtxoScript, VtxoTaprootTree, VtxoTreeExpiry, assembleBtcdTaprootTree, assertAllowedSighashTypes, baseFetch, buildVersion, canRecoverOnchain, canSpendOffchain, canSweepOnchain, configureEventSource, convertVtxo, craftToSpendTx, decodeTapscript, defaultEmulatorPubkey, eventSourceIterator, fetchVtxoCreatedAtByTxid, formatSighash, getAllNormalizedVtxos, getArkPsbtFields, getConfiguredEventSource, getNetwork, getNormalizedVtxos, getSequence, hasTerminalSpend, isArkError, isEventSourceError, isEventSourceUnavailableError, isExpired, isFetchTimeoutError, isPastExpiry, isRecoverable, isRetryableProviderError, isSpendable, isVirtualCoin, isVtxoSpent, maybeArkError, networks, normalizeVtxo, parseLegacyExpiry, rateGate, requiresForfeit, resolveEmulatorPubkey, resolveEventSource, resolveTimeHeight, scriptFromTapLeafScript, sdkVersion, sequenceToTimelock, setArkPsbtField, throwIfHttpUnavailable, timelockToSequence, toBatchExpiry, toOffchainInputFeeParams, toProviderUnavailable, toVirtualStatus };
2474
- //# sourceMappingURL=chunk-7KIGEOYT.js.map
2475
- //# sourceMappingURL=chunk-7KIGEOYT.js.map
2645
+ export { ARKADE_VERSION_HEADERS, ArkAddress, ArkError, ArkErrorName, ArkPsbtFieldKey, ArkPsbtFieldKeyType, BITCOIN_EMULATOR_PUBKEY, CLTVMultisigTapscript, CSVMultisigTapscript, ConditionCSVMultisigTapscript, ConditionMultisigTapscript, ConditionWitness, CosignerPublicKey, DEFAULT_ARKADE_SERVER_URL, DEFAULT_NETWORK, DEFAULT_NETWORK_NAME, DEFAULT_PAGE_SIZE, DigestMismatchError, EventSourceUnavailableError, FetchError, Intent, MUTINYNET_EMULATOR_PUBKEY, MultisigTapscript, OP_RETURN_EMPTY_PKSCRIPT, PrevArkTxField, PrevoutTxField, ProviderUnavailableError, READ_TIMEOUT_MS, REGTEST_EMULATOR_PUBKEY, RestArkProvider, SCRIPT_QUERY_CHUNK_SIZE, ServerResponseMismatchError, SettlementEventType, TapTreeCoder, Transaction, VtxoScript, VtxoTaprootTree, VtxoTreeExpiry, assembleBtcdTaprootTree, assertAllowedSighashTypes, baseFetch, buildVersion, canRecoverOnchain, canSpendOffchain, canSweepOnchain, configureEventSource, convertVtxo, craftToSpendTx, decodeTapscript, defaultEmulatorPubkey, eventSourceIterator, fetchVtxoCreatedAtByTxid, formatSighash, getAllNormalizedVtxos, getArkPsbtFields, getConfiguredEventSource, getNetwork, getNormalizedVtxos, getSequence, hasTerminalSpend, isArkError, isEventSourceError, isEventSourceUnavailableError, isExpired, isFetchTimeoutError, isPastExpiry, isRecoverable, isRetryableProviderError, isSpendable, isVirtualCoin, isVtxoSpent, maybeArkError, networks, normalizeVtxo, parseLegacyExpiry, rateGate, requiresForfeit, resolveEmulatorPubkey, resolveEventSource, resolveTimeHeight, scriptFromTapLeafScript, sdkVersion, sequenceToTimelock, setArkPsbtField, throwIfHttpUnavailable, timelockToSequence, toBIP371TapTree, toBatchExpiry, toOffchainInputFeeParams, toProviderUnavailable, toVirtualStatus };
2646
+ //# sourceMappingURL=chunk-JCPVP7TJ.js.map
2647
+ //# sourceMappingURL=chunk-JCPVP7TJ.js.map