@qntx/wallet-worker 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,8 +25,8 @@
25
25
  },
26
26
  {
27
27
  "name": "@qntx/nostr",
28
- "version": "0.15.3",
29
- "integrity": "sha512-AefcRXe/3basIgDA0gBGzzQ2Y9k131SfrU0hZ+eJqfLzCSzsVJyum5XKzffJ74VU6FroQPbS/fSS7W5mGd22ow=="
28
+ "version": "0.15.4",
29
+ "integrity": "sha512-kxWfN3HxKm7bTDn7Mzd+X80J74fbzNwUHOx8in5qMcb89/FgytR0jXllnF6eqGyO7VYG3pKSj94JnhgbkV73BQ=="
30
30
  },
31
31
  {
32
32
  "name": "@qntx/siwx",
@@ -76,7 +76,7 @@
76
76
  },
77
77
  {
78
78
  "file": "kernel.node.js",
79
- "sha256": "3e9912ec823015b718d3a8319ad022130d13a52aa4eec4ba64ae486c0d280faa"
79
+ "sha256": "8e6c67314354feffed226c629c8f40f3fc686ee628088ddbb0e9bb3d11ddd0e5"
80
80
  },
81
81
  {
82
82
  "file": "signer-h69ffzx5.js",
@@ -84,7 +84,7 @@
84
84
  },
85
85
  {
86
86
  "file": "kernel.worker.js",
87
- "sha256": "9a0202e34595363e39f3f100a5f6afb838292a82e654b6a31b108026c827c262"
87
+ "sha256": "9dcef60b0a774dbe57597f666b0790eea0a69023fa3012f43c7b16d567dd0b9e"
88
88
  },
89
89
  {
90
90
  "file": "worker-gsare3mw.js",
@@ -72,7 +72,7 @@ import {
72
72
  // src/worker/node.ts
73
73
  import { readFile } from "node:fs/promises";
74
74
  import { parentPort, workerData } from "node:worker_threads";
75
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/util-BcRBO6o-.mjs
75
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/util-D8wUYmm0.mjs
76
76
  var NostrError = class extends Error {
77
77
  name = "NostrError";
78
78
  };
@@ -141,7 +141,7 @@ function hasLoneSurrogate(value) {
141
141
  return false;
142
142
  }
143
143
 
144
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/kind-I2Tvbe82.mjs
144
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/kind-BAnfDHdg.mjs
145
145
  var Kind = {
146
146
  Metadata: 0,
147
147
  TextNote: 1,
@@ -173,7 +173,7 @@ var Kind = {
173
173
  StarterPack: 39089
174
174
  };
175
175
 
176
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/event-DHtcm0jM.mjs
176
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/event-B__SmruU.mjs
177
177
  function isTag(value) {
178
178
  if (!Array.isArray(value) || value.length === 0)
179
179
  return false;
@@ -254,7 +254,7 @@ function getEventHash(event) {
254
254
  return bytesToHex$1(sha256(utf8Encoder.encode(serialized)));
255
255
  }
256
256
 
257
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/key-Bt0ow7mI.mjs
257
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/key-BxwA02Gx.mjs
258
258
  var SecretKey = class SecretKey {
259
259
  #bytes;
260
260
  constructor(bytes) {
@@ -383,7 +383,7 @@ function signEvent(unsigned, secretKey, auxRand) {
383
383
  return event;
384
384
  }
385
385
 
386
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/verifier-DBSPHzGg.mjs
386
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/verifier-iQR5ivRF.mjs
387
387
  function createEventVerifier(backend) {
388
388
  return (event) => {
389
389
  if (isMarkedVerified(event))
@@ -403,7 +403,7 @@ function createEventVerifier(backend) {
403
403
  };
404
404
  }
405
405
  var verifyEvent = createEventVerifier(nobleBackend);
406
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/error-CoP2myne.mjs
406
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/error-Cord_cbw.mjs
407
407
  var SignerDisposedError = class extends NostrError {
408
408
  name = "SignerDisposedError";
409
409
  };
@@ -862,7 +862,7 @@ var cbc = /* @__PURE__ */ wrapCipher({ blockSize: 16, nonceLength: 16 }, functio
862
862
  };
863
863
  });
864
864
 
865
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip04.mjs
865
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip04.mjs
866
866
  function normalizeSharedSecret(privkey, pubkey, backend) {
867
867
  assertHex32(pubkey, "public key");
868
868
  const sharedX = backend.ecdhX(privkey, hexToBytes$1(lowerAscii(pubkey)));
@@ -1536,7 +1536,7 @@ var hkdf = (hash, ikm, salt, info, length) => {
1536
1536
  return expand(hash, HMAC.digest(), info, length, HMAC);
1537
1537
  };
1538
1538
 
1539
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip44.mjs
1539
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip44.mjs
1540
1540
  var minPlaintextSize = 1;
1541
1541
  var maxPlaintextSize = 4294967295;
1542
1542
  var extendedPrefixThreshold = 65536;
@@ -1662,7 +1662,7 @@ function decrypt3(payload, conversationKey, opts) {
1662
1662
  return unpad(chacha20(chacha_key, chacha_nonce, ciphertext));
1663
1663
  }
1664
1664
 
1665
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip59.mjs
1665
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip59.mjs
1666
1666
  var Nip59Error = class extends NostrError {
1667
1667
  name = "Nip59Error";
1668
1668
  };
@@ -1735,7 +1735,7 @@ async function unwrap(crypto2, giftWrap, options) {
1735
1735
  return rumor;
1736
1736
  }
1737
1737
 
1738
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/signer-DQzjm3pt.mjs
1738
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/signer-Y3gcghwd.mjs
1739
1739
  var KeysSigner = class {
1740
1740
  #keys;
1741
1741
  #convKeys = /* @__PURE__ */ new Map;
@@ -2212,7 +2212,7 @@ var CORE_METHODS = {
2212
2212
  }
2213
2213
  };
2214
2214
 
2215
- // ../wallet/dist/protocol-DqBfdhN5.mjs
2215
+ // ../wallet/dist/protocol-C8x0H3Rr.mjs
2216
2216
  var MAX_FACTOR_CHARS = 512;
2217
2217
  var MAX_STRING_BYTES = 64000;
2218
2218
  var MAX_PAYLOAD_CHARS = 1400000;
@@ -2247,6 +2247,7 @@ var backupInfo = obj({
2247
2247
  createdAt: safeTime
2248
2248
  });
2249
2249
  var template = union(literal("evm.bip44"), literal("solana.bip44-change"), union(literal("btc.bip44"), literal("btc.bip49"), literal("btc.bip84")), literal("btc.bip86"));
2250
+ var btcTemplate = union(literal("btc.bip44"), literal("btc.bip49"), literal("btc.bip84"), literal("btc.bip86"));
2250
2251
  var caip2 = str(200, /^[a-z0-9-]{3,64}:[0-9a-zA-Z-]{1,128}$/);
2251
2252
  var addressResult = (t) => obj({
2252
2253
  template: literal(t),
@@ -2330,9 +2331,16 @@ var signRequest = union(obj({
2330
2331
  }), obj({
2331
2332
  kind: literal("bitcoin.psbt"),
2332
2333
  network: caip2,
2333
- account: accountRef,
2334
+ account: obj({
2335
+ kind: literal("hd"),
2336
+ walletId,
2337
+ groupIndex
2338
+ }),
2334
2339
  psbt: bytes(1, 131072),
2335
- sign: arr(int(0, 1024), 1024)
2340
+ sign: arr(obj({
2341
+ index: int(0, 1024),
2342
+ template: btcTemplate
2343
+ }), 1024)
2336
2344
  }), obj({
2337
2345
  kind: literal("bitcoin.message"),
2338
2346
  network: caip2,
@@ -9740,6 +9748,13 @@ var checkDerivations = (tx, coinType) => {
9740
9748
  };
9741
9749
  var DEFAULT_SIGHASH = 0;
9742
9750
  var SIGHASH_ALL = 1;
9751
+ var TEMPLATES = /* @__PURE__ */ new Set([
9752
+ "btc.bip44",
9753
+ "btc.bip49",
9754
+ "btc.bip84",
9755
+ "btc.bip86"
9756
+ ]);
9757
+ var isSignEntry = (value) => isRecord$1(value) && typeof value["index"] === "number" && Number.isSafeInteger(value["index"]) && value["index"] >= 0 && typeof value["template"] === "string" && TEMPLATES.has(value["template"]);
9743
9758
  var isTaprootInput = (input) => input.tapInternalKey !== undefined || getInputType(input).type === "taproot";
9744
9759
  var normalizeBitcoinPsbt = (psbt, sign, network) => {
9745
9760
  const net = bitcoinNetwork(network);
@@ -9749,18 +9764,19 @@ var normalizeBitcoinPsbt = (psbt, sign, network) => {
9749
9764
  } catch {
9750
9765
  throw new ChainError("invalid-request", "PSBT failed to decode");
9751
9766
  }
9752
- if (!Array.isArray(sign) || sign.length === 0 || !sign.every((i) => Number.isSafeInteger(i) && i >= 0))
9753
- throw new ChainError("invalid-request", "sign must be a non-empty array of input indices");
9754
- const unique = [...new Set(sign.map(Number))].sort((a, b) => a - b);
9755
- const last = unique.at(-1);
9756
- if (unique.length !== sign.length || last === undefined || last >= tx.inputsLength)
9767
+ if (!Array.isArray(sign) || sign.length === 0 || !sign.every(isSignEntry))
9768
+ throw new ChainError("invalid-request", "sign must be a non-empty array of {index, template} entries");
9769
+ const sorted = [...sign].sort((a, b) => a.index - b.index);
9770
+ const indices = new Set(sorted.map((s) => s.index));
9771
+ const last = sorted.at(-1)?.index;
9772
+ if (indices.size !== sorted.length || last === undefined || last >= tx.inputsLength)
9757
9773
  throw new ChainError("invalid-request", "sign must be unique, ascending, and in range");
9758
9774
  checkDerivations(tx, net.coinType);
9759
- for (const idx of unique) {
9775
+ for (const { index: idx, template } of sorted) {
9760
9776
  const input = tx.getInput(idx);
9761
9777
  if (input.witnessUtxo === undefined && input.nonWitnessUtxo === undefined)
9762
9778
  throw new ChainError("invalid-request", `input ${idx} carries neither witnessUtxo nor nonWitnessUtxo`);
9763
- if (input.bip32Derivation === undefined && input.tapBip32Derivation === undefined)
9779
+ if (template === "btc.bip86" ? input.tapBip32Derivation === undefined : input.bip32Derivation === undefined)
9764
9780
  throw new ChainError("invalid-request", `input ${idx} is missing a bip32 derivation`);
9765
9781
  const { type } = getInputType(input);
9766
9782
  if ((type === "legacy" || type === "nonwitness-script") && input.nonWitnessUtxo === undefined)
@@ -9773,7 +9789,7 @@ var normalizeBitcoinPsbt = (psbt, sign, network) => {
9773
9789
  }
9774
9790
  return {
9775
9791
  psbt: tx.toPSBT(0),
9776
- sign: unique
9792
+ sign: sorted
9777
9793
  };
9778
9794
  };
9779
9795
  var SATS = "sats";
@@ -9809,7 +9825,7 @@ var decodeOutput = (script, amount) => {
9809
9825
  var decodeBitcoinPsbt = (request, ownScripts = /* @__PURE__ */ new Set) => {
9810
9826
  const tx = parsePsbt(request.psbt);
9811
9827
  const coder = Address(bitcoinNetwork(request.network).params);
9812
- const signed = new Set(request.sign);
9828
+ const signed = new Set(request.sign.map((s) => s.index));
9813
9829
  const outflows = [];
9814
9830
  let canFee = true;
9815
9831
  let outSum = 0n;
@@ -9849,7 +9865,7 @@ var decodeBitcoinPsbt = (request, ownScripts = /* @__PURE__ */ new Set) => {
9849
9865
  }
9850
9866
  const external = tx.inputsLength - signed.size > 0;
9851
9867
  const undecodable = outflows.some((o) => !o.verified);
9852
- const listing = request.sign.some((i) => tx.getInput(i).sighashType === SIGHASH_SINGLE_ANYONECANPAY);
9868
+ const listing = request.sign.some((s) => tx.getInput(s.index).sighashType === SIGHASH_SINGLE_ANYONECANPAY);
9853
9869
  const verifiability = external ? "opaque" : listing || undecodable ? "partial" : "full";
9854
9870
  return {
9855
9871
  family: "bitcoin",
@@ -9902,20 +9918,32 @@ var messageTarget = (address, network, format) => {
9902
9918
  var normalizeBitcoin = (request) => {
9903
9919
  if (!isRecord2(request) || typeof request["kind"] !== "string")
9904
9920
  throw new ChainError("invalid-request", "request must be an object with a kind");
9905
- const { network, account } = common(request);
9906
9921
  if (request["kind"] === "bitcoin.psbt") {
9907
- const { psbt } = request;
9922
+ const { network, account, psbt } = request;
9923
+ if (typeof network !== "string")
9924
+ throw new ChainError("invalid-request", "request.network must be a string");
9925
+ bitcoinNetwork(network);
9926
+ if (!isRecord2(account) || account["kind"] !== "hd")
9927
+ throw new ChainError("invalid-request", "request.account must be a wallet group");
9928
+ const { walletId, groupIndex } = account;
9929
+ if (typeof walletId !== "string" || typeof groupIndex !== "number" || !Number.isSafeInteger(groupIndex) || groupIndex < 0)
9930
+ throw new ChainError("invalid-request", "request.account must be a wallet group");
9908
9931
  if (!(psbt instanceof Uint8Array))
9909
9932
  throw new ChainError("invalid-request", "psbt must be bytes");
9910
9933
  const normalized = normalizeBitcoinPsbt(psbt, request["sign"], network);
9911
9934
  return {
9912
9935
  kind: "bitcoin.psbt",
9913
9936
  network,
9914
- account,
9937
+ account: {
9938
+ kind: "hd",
9939
+ walletId,
9940
+ groupIndex
9941
+ },
9915
9942
  psbt: normalized.psbt,
9916
9943
  sign: normalized.sign
9917
9944
  };
9918
9945
  }
9946
+ const { network, account } = common(request);
9919
9947
  if (request["kind"] === "bitcoin.message") {
9920
9948
  const { format } = request;
9921
9949
  const target = messageTarget(request["address"], network, format);
@@ -16735,6 +16763,18 @@ function mapChainError(error) {
16735
16763
  // ../wallet-kernel/src/modules/chains/bitcoin.ts
16736
16764
  var isPath2 = (path) => Array.isArray(path) && path.every((p) => typeof p === "number");
16737
16765
  var hex = (bytes) => bytes.reduce((acc, b) => acc + b.toString(16).padStart(2, "0"), "");
16766
+ var PURPOSES = [44, 49, 84, 86];
16767
+ var contextFor = (b, purpose) => {
16768
+ let ctx = b.purposes.get(purpose);
16769
+ if (ctx === undefined) {
16770
+ ctx = {
16771
+ accountNode: b.master.derive(`m/${purpose}'/${b.coinType}'/${b.groupIndex}'`),
16772
+ children: []
16773
+ };
16774
+ b.purposes.set(purpose, ctx);
16775
+ }
16776
+ return ctx;
16777
+ };
16738
16778
  var derivationOf = (owner, fingerprint, purpose) => {
16739
16779
  if (purpose === 86) {
16740
16780
  const claimed = (der) => der.fingerprint === fingerprint && isPath2(der.path);
@@ -16754,46 +16794,52 @@ var derivationOf = (owner, fingerprint, purpose) => {
16754
16794
  };
16755
16795
  var expectedPub = (purpose, pub) => purpose === 86 ? pub.slice(1) : pub;
16756
16796
  var expectedScript = (purpose, pub) => purpose === 86 ? p2tr(pub.slice(1)).script : purpose === 49 ? p2sh(p2wpkh(pub)).script : purpose === 44 ? p2pkh(pub).script : p2wpkh(pub).script;
16757
- var childAt = (b, path) => {
16797
+ var childAt = (b, purpose, path) => {
16758
16798
  const [purposePath, coinPath, groupPath, change, index] = path;
16759
- if (path.length !== 5 || purposePath !== b.purpose + b.hardened || coinPath !== b.coinType + b.hardened || groupPath !== b.account.groupIndex + b.hardened || change === undefined || index === undefined || change !== 0 && change !== 1 || index < 0 || index >= b.hardened) {
16799
+ if (path.length !== 5 || purposePath !== purpose + b.hardened || coinPath !== b.coinType + b.hardened || groupPath !== b.groupIndex + b.hardened || change === undefined || index === undefined || change !== 0 && change !== 1 || index < 0 || index >= b.hardened) {
16760
16800
  throw new KernelError("invalid-input", "derivation path is not this account's");
16761
16801
  }
16762
- const child = b.accountNode.deriveChild(change).deriveChild(index);
16763
- b.children.push(child);
16802
+ const ctx = contextFor(b, purpose);
16803
+ const child = ctx.accountNode.deriveChild(change).deriveChild(index);
16804
+ ctx.children.push(child);
16764
16805
  return child;
16765
16806
  };
16766
- var bitcoinInput = (b, tx, idx) => {
16807
+ var bitcoinInput = (b, tx, entry) => {
16808
+ const purpose = BTC_PURPOSE[entry.template];
16809
+ if (purpose === undefined) {
16810
+ throw new KernelError("invalid-input", "sign template is not a bitcoin account template");
16811
+ }
16812
+ const { index: idx } = entry;
16767
16813
  const input = tx.getInput(idx);
16768
- const entry = derivationOf(input, b.master.fingerprint, b.purpose);
16769
- if (entry === undefined) {
16814
+ const der = derivationOf(input, b.master.fingerprint, purpose);
16815
+ if (der === undefined) {
16770
16816
  throw new KernelError("invalid-input", `input ${idx}: derivation entry is not this account's`);
16771
16817
  }
16772
- const child = childAt(b, entry.path);
16818
+ const child = childAt(b, purpose, der.path);
16773
16819
  const pub = child.publicKey;
16774
16820
  const priv = child.privateKey;
16775
16821
  if (pub === null || priv === null) {
16776
16822
  throw new KernelError("integrity", "no key at derivation path");
16777
16823
  }
16778
- if (!deepEqual(entry.pubkey, expectedPub(b.purpose, pub))) {
16824
+ if (!deepEqual(der.pubkey, expectedPub(purpose, pub))) {
16779
16825
  throw new KernelError("invalid-input", `input ${idx}: derivation pubkey is not ours`);
16780
16826
  }
16781
- if (b.purpose === 44) {
16827
+ if (purpose === 44) {
16782
16828
  if (input.nonWitnessUtxo === undefined) {
16783
16829
  throw new KernelError("invalid-input", `input ${idx}: P2PKH spend requires nonWitnessUtxo`);
16784
16830
  }
16785
16831
  const vout = input.index;
16786
16832
  const outs = input.nonWitnessUtxo.outputs;
16787
16833
  const prevScript = vout === undefined ? undefined : outs[vout]?.script;
16788
- if (prevScript === undefined || !deepEqual(prevScript, expectedScript(b.purpose, pub))) {
16834
+ if (prevScript === undefined || !deepEqual(prevScript, expectedScript(purpose, pub))) {
16789
16835
  throw new KernelError("invalid-input", `input ${idx}: nonWitnessUtxo prevout is not this key's`);
16790
16836
  }
16791
16837
  } else {
16792
16838
  const prevout = input.witnessUtxo;
16793
- if (prevout === undefined || !deepEqual(prevout.script, expectedScript(b.purpose, pub))) {
16839
+ if (prevout === undefined || !deepEqual(prevout.script, expectedScript(purpose, pub))) {
16794
16840
  throw new KernelError("invalid-input", `input ${idx}: prevout script is not this key's`);
16795
16841
  }
16796
- if (b.purpose === 49 && (input.redeemScript === undefined || !deepEqual(input.redeemScript, p2wpkh(pub).script))) {
16842
+ if (purpose === 49 && (input.redeemScript === undefined || !deepEqual(input.redeemScript, p2wpkh(pub).script))) {
16797
16843
  throw new KernelError("invalid-input", `input ${idx}: redeemScript is not this key's`);
16798
16844
  }
16799
16845
  }
@@ -16806,25 +16852,27 @@ var bitcoinOwnScripts = (b, tx) => {
16806
16852
  if (output.script === undefined) {
16807
16853
  continue;
16808
16854
  }
16809
- const entry = derivationOf(output, b.master.fingerprint, b.purpose);
16810
- if (entry === undefined) {
16811
- continue;
16812
- }
16813
- try {
16814
- const child = childAt(b, entry.path);
16815
- const pub = child.publicKey;
16816
- if (pub !== null && deepEqual(output.script, expectedScript(b.purpose, pub))) {
16817
- ownScripts.add(hex(output.script));
16855
+ for (const purpose of PURPOSES) {
16856
+ const entry = derivationOf(output, b.master.fingerprint, purpose);
16857
+ if (entry === undefined) {
16858
+ continue;
16859
+ }
16860
+ try {
16861
+ const child = childAt(b, purpose, entry.path);
16862
+ const pub = child.publicKey;
16863
+ if (pub !== null && deepEqual(output.script, expectedScript(purpose, pub))) {
16864
+ ownScripts.add(hex(output.script));
16865
+ }
16866
+ } catch {
16867
+ continue;
16818
16868
  }
16819
- } catch {
16820
- continue;
16821
16869
  }
16822
16870
  }
16823
16871
  return ownScripts;
16824
16872
  };
16825
16873
  var executeBitcoinPsbt = (b, request, random, intentOnly) => {
16826
16874
  const tx = Transaction.fromPSBT(request.psbt);
16827
- const inputs = request.sign.map((idx) => bitcoinInput(b, tx, idx));
16875
+ const inputs = request.sign.map((entry) => bitcoinInput(b, tx, entry));
16828
16876
  const ownScripts = bitcoinOwnScripts(b, tx);
16829
16877
  let normalized;
16830
16878
  try {
@@ -16849,14 +16897,14 @@ var executeBitcoinPsbt = (b, request, random, intentOnly) => {
16849
16897
  signedPsbt: signBitcoinPsbt(request.psbt, inputs, () => random(32))
16850
16898
  };
16851
16899
  };
16852
- var executeBitcoinMessage = (b, request, random, intentOnly) => {
16853
- if (request.format === "legacy" && b.account.template !== "btc.bip44") {
16900
+ var executeBitcoinMessage = (b, purpose, request, random, intentOnly) => {
16901
+ if (request.format === "legacy" && purpose !== 44) {
16854
16902
  throw new KernelError("unsupported", "legacy message format is btc.bip44-only");
16855
16903
  }
16856
- const child = childAt(b, [
16857
- b.purpose + b.hardened,
16904
+ const child = childAt(b, purpose, [
16905
+ purpose + b.hardened,
16858
16906
  b.coinType + b.hardened,
16859
- b.account.groupIndex + b.hardened,
16907
+ b.groupIndex + b.hardened,
16860
16908
  request.change,
16861
16909
  request.index
16862
16910
  ]);
@@ -16865,7 +16913,7 @@ var executeBitcoinMessage = (b, request, random, intentOnly) => {
16865
16913
  if (pub === null || priv === null) {
16866
16914
  throw new KernelError("integrity", "no key at derivation path");
16867
16915
  }
16868
- const derived = b.purpose === 86 ? p2tr(pub.slice(1), undefined, b.net.params).address : b.purpose === 49 ? p2sh(p2wpkh(pub, b.net.params), b.net.params).address : b.purpose === 44 ? p2pkh(pub, b.net.params).address : p2wpkh(pub, b.net.params).address;
16916
+ const derived = purpose === 86 ? p2tr(pub.slice(1), undefined, b.net.params).address : purpose === 49 ? p2sh(p2wpkh(pub, b.net.params), b.net.params).address : purpose === 44 ? p2pkh(pub, b.net.params).address : p2wpkh(pub, b.net.params).address;
16869
16917
  if (derived !== request.address) {
16870
16918
  throw new KernelError("invalid-input", "address is not ours");
16871
16919
  }
@@ -16894,34 +16942,33 @@ var executeBitcoinMessage = (b, request, random, intentOnly) => {
16894
16942
  var executeBitcoin = (random, request, account, seed, intentOnly) => {
16895
16943
  const net = bitcoinNetwork(request.network);
16896
16944
  const master = HDKey.fromMasterSeed(seed, net.bip32);
16945
+ const b = {
16946
+ master,
16947
+ purposes: new Map,
16948
+ coinType: net.coinType,
16949
+ hardened: 2147483648,
16950
+ groupIndex: account.groupIndex,
16951
+ net
16952
+ };
16897
16953
  try {
16898
- const purpose = BTC_PURPOSE[account.template] ?? 84;
16899
- const { coinType } = net;
16900
- const hardened = 2147483648;
16901
- const accountNode = master.derive(`m/${purpose}'/${coinType}'/${account.groupIndex}'`);
16902
- const children = [];
16903
- const b = {
16904
- master,
16905
- accountNode,
16906
- children,
16907
- purpose,
16908
- coinType,
16909
- hardened,
16910
- account,
16911
- net
16912
- };
16913
- try {
16914
- if (request.kind === "bitcoin.psbt") {
16915
- return executeBitcoinPsbt(b, request, random, intentOnly);
16916
- }
16917
- return executeBitcoinMessage(b, request, random, intentOnly);
16918
- } finally {
16919
- for (const child of children) {
16954
+ if (request.kind === "bitcoin.psbt") {
16955
+ return executeBitcoinPsbt(b, request, random, intentOnly);
16956
+ }
16957
+ if (request.account.kind !== "hd") {
16958
+ throw new KernelError("unsupported", "imported accounts are not yet supported");
16959
+ }
16960
+ const purpose = BTC_PURPOSE[request.account.template];
16961
+ if (purpose === undefined) {
16962
+ throw new KernelError("invalid-input", "account template is not a bitcoin template");
16963
+ }
16964
+ return executeBitcoinMessage(b, purpose, request, random, intentOnly);
16965
+ } finally {
16966
+ for (const ctx of b.purposes.values()) {
16967
+ for (const child of ctx.children) {
16920
16968
  child.wipePrivateData();
16921
16969
  }
16922
- accountNode.wipePrivateData();
16970
+ ctx.accountNode.wipePrivateData();
16923
16971
  }
16924
- } finally {
16925
16972
  master.wipePrivateData();
16926
16973
  }
16927
16974
  };
@@ -17180,23 +17227,35 @@ var chainSign = async (ctx, params, session) => {
17180
17227
  if (account.kind !== "hd") {
17181
17228
  throw new KernelError("unsupported", "imported accounts are not yet supported");
17182
17229
  }
17183
- const [family] = request.kind.split(".", 1);
17184
- const expected = family === "evm" ? ["evm.bip44"] : family === "solana" ? ["solana.bip44-change"] : ["btc.bip44", "btc.bip49", "btc.bip84", "btc.bip86"];
17185
- if (!expected.includes(account.template)) {
17186
- throw new KernelError("invalid-input", "account template does not match the request family");
17230
+ if (request.kind === "bitcoin.psbt") {
17231
+ if (request.sign.some((s) => BTC_PURPOSE[s.template] === undefined)) {
17232
+ throw new KernelError("invalid-input", "sign template is not a bitcoin account template");
17233
+ }
17234
+ } else {
17235
+ const [family] = request.kind.split(".", 1);
17236
+ const expected = family === "evm" ? ["evm.bip44"] : family === "solana" ? ["solana.bip44-change"] : ["btc.bip44", "btc.bip49", "btc.bip84", "btc.bip86"];
17237
+ if (!("template" in account) || !expected.includes(account.template)) {
17238
+ throw new KernelError("invalid-input", "account template does not match the request family");
17239
+ }
17187
17240
  }
17188
17241
  const record = ctx.image.records.find((entry) => entry.kind === "entropy" && entry.id === account.walletId);
17189
17242
  if (record === undefined) {
17190
17243
  throw new KernelError("not-found", "no such wallet");
17191
17244
  }
17192
17245
  const execute = (seed, intentOnly) => {
17193
- if (request.kind === "evm.transaction" || request.kind === "evm.personal-message" || request.kind === "evm.typed-data") {
17194
- return executeEvm(request, account, seed, intentOnly);
17246
+ if (request.kind === "bitcoin.psbt") {
17247
+ return executeBitcoin(ctx.platform.random, request, request.account, seed, intentOnly);
17248
+ }
17249
+ if (request.account.kind !== "hd") {
17250
+ throw new KernelError("unsupported", "imported accounts are not yet supported");
17195
17251
  }
17196
- if (request.kind === "bitcoin.psbt" || request.kind === "bitcoin.message") {
17197
- return executeBitcoin(ctx.platform.random, request, account, seed, intentOnly);
17252
+ if (request.kind === "bitcoin.message") {
17253
+ return executeBitcoin(ctx.platform.random, request, request.account, seed, intentOnly);
17254
+ }
17255
+ if (request.kind === "evm.transaction" || request.kind === "evm.personal-message" || request.kind === "evm.typed-data") {
17256
+ return executeEvm(request, request.account, seed, intentOnly);
17198
17257
  }
17199
- return executeSolana(request, account, seed, intentOnly);
17258
+ return executeSolana(request, request.account, seed, intentOnly);
17200
17259
  };
17201
17260
  const withSeed = async (run) => withWalletSeed(ctx.platform.pbkdf2, dek, record, run);
17202
17261
  const prepared = await withSeed((seed) => execute(seed, true));
@@ -17249,7 +17308,7 @@ function nostrKeyAt(master, index) {
17249
17308
  }
17250
17309
  }
17251
17310
 
17252
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip19.mjs
17311
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip19.mjs
17253
17312
  var Bech32MaxSize2 = 5000;
17254
17313
  function encodeBech32(prefix, data) {
17255
17314
  const words = bech32.toWords(data);
@@ -17422,7 +17481,7 @@ async function scryptAsync(password, salt, opts) {
17422
17481
  return scryptOutput(password, dkLen, B, V, tmp);
17423
17482
  }
17424
17483
 
17425
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip49.mjs
17484
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip49.mjs
17426
17485
  var VERSION2 = 2;
17427
17486
  var SALT_LEN = 16;
17428
17487
  var NONCE_LEN = 24;
@@ -98,8 +98,8 @@ var __chunks = (base, ids, nodes, entry) => {
98
98
  __chunkSeen ||= {};
99
99
  __preload(ids[entry], 1);
100
100
  };
101
- __chunks(import.meta.url,["ewnv0yfm","h69ffzx5","1vpxmqwn","k5etevb7","gsare3mw"],[["./kernel.worker.js",3],["./signer-h69ffzx5.js",3,4],["./browser-1vpxmqwn.js",4],["./worker-k5etevb7.js"],["./worker-gsare3mw.js"]],0);
102
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/util-BcRBO6o-.mjs
101
+ __chunks(import.meta.url,["s5tqy9kw","h69ffzx5","1vpxmqwn","k5etevb7","gsare3mw"],[["./kernel.worker.js",3],["./signer-h69ffzx5.js",3,4],["./browser-1vpxmqwn.js",4],["./worker-k5etevb7.js"],["./worker-gsare3mw.js"]],0);
102
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/util-D8wUYmm0.mjs
103
103
  var NostrError = class extends Error {
104
104
  name = "NostrError";
105
105
  };
@@ -168,7 +168,7 @@ function hasLoneSurrogate(value) {
168
168
  return false;
169
169
  }
170
170
 
171
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/kind-I2Tvbe82.mjs
171
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/kind-BAnfDHdg.mjs
172
172
  var Kind = {
173
173
  Metadata: 0,
174
174
  TextNote: 1,
@@ -200,7 +200,7 @@ var Kind = {
200
200
  StarterPack: 39089
201
201
  };
202
202
 
203
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/event-DHtcm0jM.mjs
203
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/event-B__SmruU.mjs
204
204
  function isTag(value) {
205
205
  if (!Array.isArray(value) || value.length === 0)
206
206
  return false;
@@ -281,7 +281,7 @@ function getEventHash(event) {
281
281
  return bytesToHex$1(sha256(utf8Encoder.encode(serialized)));
282
282
  }
283
283
 
284
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/key-Bt0ow7mI.mjs
284
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/key-BxwA02Gx.mjs
285
285
  var SecretKey = class SecretKey {
286
286
  #bytes;
287
287
  constructor(bytes) {
@@ -410,7 +410,7 @@ function signEvent(unsigned, secretKey, auxRand) {
410
410
  return event;
411
411
  }
412
412
 
413
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/verifier-DBSPHzGg.mjs
413
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/verifier-iQR5ivRF.mjs
414
414
  function createEventVerifier(backend) {
415
415
  return (event) => {
416
416
  if (isMarkedVerified(event))
@@ -430,7 +430,7 @@ function createEventVerifier(backend) {
430
430
  };
431
431
  }
432
432
  var verifyEvent = createEventVerifier(nobleBackend);
433
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/error-CoP2myne.mjs
433
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/error-Cord_cbw.mjs
434
434
  var SignerDisposedError = class extends NostrError {
435
435
  name = "SignerDisposedError";
436
436
  };
@@ -889,7 +889,7 @@ var cbc = /* @__PURE__ */ wrapCipher({ blockSize: 16, nonceLength: 16 }, functio
889
889
  };
890
890
  });
891
891
 
892
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip04.mjs
892
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip04.mjs
893
893
  function normalizeSharedSecret(privkey, pubkey, backend) {
894
894
  assertHex32(pubkey, "public key");
895
895
  const sharedX = backend.ecdhX(privkey, hexToBytes$1(lowerAscii(pubkey)));
@@ -1563,7 +1563,7 @@ var hkdf = (hash, ikm, salt, info, length) => {
1563
1563
  return expand(hash, HMAC.digest(), info, length, HMAC);
1564
1564
  };
1565
1565
 
1566
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip44.mjs
1566
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip44.mjs
1567
1567
  var minPlaintextSize = 1;
1568
1568
  var maxPlaintextSize = 4294967295;
1569
1569
  var extendedPrefixThreshold = 65536;
@@ -1689,7 +1689,7 @@ function decrypt3(payload, conversationKey, opts) {
1689
1689
  return unpad(chacha20(chacha_key, chacha_nonce, ciphertext));
1690
1690
  }
1691
1691
 
1692
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip59.mjs
1692
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip59.mjs
1693
1693
  var Nip59Error = class extends NostrError {
1694
1694
  name = "Nip59Error";
1695
1695
  };
@@ -1762,7 +1762,7 @@ async function unwrap(crypto2, giftWrap, options) {
1762
1762
  return rumor;
1763
1763
  }
1764
1764
 
1765
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/signer-DQzjm3pt.mjs
1765
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/signer-Y3gcghwd.mjs
1766
1766
  var KeysSigner = class {
1767
1767
  #keys;
1768
1768
  #convKeys = /* @__PURE__ */ new Map;
@@ -2239,7 +2239,7 @@ var CORE_METHODS = {
2239
2239
  }
2240
2240
  };
2241
2241
 
2242
- // ../wallet/dist/protocol-DqBfdhN5.mjs
2242
+ // ../wallet/dist/protocol-C8x0H3Rr.mjs
2243
2243
  var MAX_FACTOR_CHARS = 512;
2244
2244
  var MAX_STRING_BYTES = 64000;
2245
2245
  var MAX_PAYLOAD_CHARS = 1400000;
@@ -2274,6 +2274,7 @@ var backupInfo = obj({
2274
2274
  createdAt: safeTime
2275
2275
  });
2276
2276
  var template = union(literal("evm.bip44"), literal("solana.bip44-change"), union(literal("btc.bip44"), literal("btc.bip49"), literal("btc.bip84")), literal("btc.bip86"));
2277
+ var btcTemplate = union(literal("btc.bip44"), literal("btc.bip49"), literal("btc.bip84"), literal("btc.bip86"));
2277
2278
  var caip2 = str(200, /^[a-z0-9-]{3,64}:[0-9a-zA-Z-]{1,128}$/);
2278
2279
  var addressResult = (t) => obj({
2279
2280
  template: literal(t),
@@ -2357,9 +2358,16 @@ var signRequest = union(obj({
2357
2358
  }), obj({
2358
2359
  kind: literal("bitcoin.psbt"),
2359
2360
  network: caip2,
2360
- account: accountRef,
2361
+ account: obj({
2362
+ kind: literal("hd"),
2363
+ walletId,
2364
+ groupIndex
2365
+ }),
2361
2366
  psbt: bytes(1, 131072),
2362
- sign: arr(int(0, 1024), 1024)
2367
+ sign: arr(obj({
2368
+ index: int(0, 1024),
2369
+ template: btcTemplate
2370
+ }), 1024)
2363
2371
  }), obj({
2364
2372
  kind: literal("bitcoin.message"),
2365
2373
  network: caip2,
@@ -9767,6 +9775,13 @@ var checkDerivations = (tx, coinType) => {
9767
9775
  };
9768
9776
  var DEFAULT_SIGHASH = 0;
9769
9777
  var SIGHASH_ALL = 1;
9778
+ var TEMPLATES = /* @__PURE__ */ new Set([
9779
+ "btc.bip44",
9780
+ "btc.bip49",
9781
+ "btc.bip84",
9782
+ "btc.bip86"
9783
+ ]);
9784
+ var isSignEntry = (value) => isRecord$1(value) && typeof value["index"] === "number" && Number.isSafeInteger(value["index"]) && value["index"] >= 0 && typeof value["template"] === "string" && TEMPLATES.has(value["template"]);
9770
9785
  var isTaprootInput = (input) => input.tapInternalKey !== undefined || getInputType(input).type === "taproot";
9771
9786
  var normalizeBitcoinPsbt = (psbt, sign, network) => {
9772
9787
  const net = bitcoinNetwork(network);
@@ -9776,18 +9791,19 @@ var normalizeBitcoinPsbt = (psbt, sign, network) => {
9776
9791
  } catch {
9777
9792
  throw new ChainError("invalid-request", "PSBT failed to decode");
9778
9793
  }
9779
- if (!Array.isArray(sign) || sign.length === 0 || !sign.every((i) => Number.isSafeInteger(i) && i >= 0))
9780
- throw new ChainError("invalid-request", "sign must be a non-empty array of input indices");
9781
- const unique = [...new Set(sign.map(Number))].sort((a, b) => a - b);
9782
- const last = unique.at(-1);
9783
- if (unique.length !== sign.length || last === undefined || last >= tx.inputsLength)
9794
+ if (!Array.isArray(sign) || sign.length === 0 || !sign.every(isSignEntry))
9795
+ throw new ChainError("invalid-request", "sign must be a non-empty array of {index, template} entries");
9796
+ const sorted = [...sign].sort((a, b) => a.index - b.index);
9797
+ const indices = new Set(sorted.map((s) => s.index));
9798
+ const last = sorted.at(-1)?.index;
9799
+ if (indices.size !== sorted.length || last === undefined || last >= tx.inputsLength)
9784
9800
  throw new ChainError("invalid-request", "sign must be unique, ascending, and in range");
9785
9801
  checkDerivations(tx, net.coinType);
9786
- for (const idx of unique) {
9802
+ for (const { index: idx, template } of sorted) {
9787
9803
  const input = tx.getInput(idx);
9788
9804
  if (input.witnessUtxo === undefined && input.nonWitnessUtxo === undefined)
9789
9805
  throw new ChainError("invalid-request", `input ${idx} carries neither witnessUtxo nor nonWitnessUtxo`);
9790
- if (input.bip32Derivation === undefined && input.tapBip32Derivation === undefined)
9806
+ if (template === "btc.bip86" ? input.tapBip32Derivation === undefined : input.bip32Derivation === undefined)
9791
9807
  throw new ChainError("invalid-request", `input ${idx} is missing a bip32 derivation`);
9792
9808
  const { type } = getInputType(input);
9793
9809
  if ((type === "legacy" || type === "nonwitness-script") && input.nonWitnessUtxo === undefined)
@@ -9800,7 +9816,7 @@ var normalizeBitcoinPsbt = (psbt, sign, network) => {
9800
9816
  }
9801
9817
  return {
9802
9818
  psbt: tx.toPSBT(0),
9803
- sign: unique
9819
+ sign: sorted
9804
9820
  };
9805
9821
  };
9806
9822
  var SATS = "sats";
@@ -9836,7 +9852,7 @@ var decodeOutput = (script, amount) => {
9836
9852
  var decodeBitcoinPsbt = (request, ownScripts = /* @__PURE__ */ new Set) => {
9837
9853
  const tx = parsePsbt(request.psbt);
9838
9854
  const coder = Address(bitcoinNetwork(request.network).params);
9839
- const signed = new Set(request.sign);
9855
+ const signed = new Set(request.sign.map((s) => s.index));
9840
9856
  const outflows = [];
9841
9857
  let canFee = true;
9842
9858
  let outSum = 0n;
@@ -9876,7 +9892,7 @@ var decodeBitcoinPsbt = (request, ownScripts = /* @__PURE__ */ new Set) => {
9876
9892
  }
9877
9893
  const external = tx.inputsLength - signed.size > 0;
9878
9894
  const undecodable = outflows.some((o) => !o.verified);
9879
- const listing = request.sign.some((i) => tx.getInput(i).sighashType === SIGHASH_SINGLE_ANYONECANPAY);
9895
+ const listing = request.sign.some((s) => tx.getInput(s.index).sighashType === SIGHASH_SINGLE_ANYONECANPAY);
9880
9896
  const verifiability = external ? "opaque" : listing || undecodable ? "partial" : "full";
9881
9897
  return {
9882
9898
  family: "bitcoin",
@@ -9929,20 +9945,32 @@ var messageTarget = (address, network, format) => {
9929
9945
  var normalizeBitcoin = (request) => {
9930
9946
  if (!isRecord2(request) || typeof request["kind"] !== "string")
9931
9947
  throw new ChainError("invalid-request", "request must be an object with a kind");
9932
- const { network, account } = common(request);
9933
9948
  if (request["kind"] === "bitcoin.psbt") {
9934
- const { psbt } = request;
9949
+ const { network, account, psbt } = request;
9950
+ if (typeof network !== "string")
9951
+ throw new ChainError("invalid-request", "request.network must be a string");
9952
+ bitcoinNetwork(network);
9953
+ if (!isRecord2(account) || account["kind"] !== "hd")
9954
+ throw new ChainError("invalid-request", "request.account must be a wallet group");
9955
+ const { walletId, groupIndex } = account;
9956
+ if (typeof walletId !== "string" || typeof groupIndex !== "number" || !Number.isSafeInteger(groupIndex) || groupIndex < 0)
9957
+ throw new ChainError("invalid-request", "request.account must be a wallet group");
9935
9958
  if (!(psbt instanceof Uint8Array))
9936
9959
  throw new ChainError("invalid-request", "psbt must be bytes");
9937
9960
  const normalized = normalizeBitcoinPsbt(psbt, request["sign"], network);
9938
9961
  return {
9939
9962
  kind: "bitcoin.psbt",
9940
9963
  network,
9941
- account,
9964
+ account: {
9965
+ kind: "hd",
9966
+ walletId,
9967
+ groupIndex
9968
+ },
9942
9969
  psbt: normalized.psbt,
9943
9970
  sign: normalized.sign
9944
9971
  };
9945
9972
  }
9973
+ const { network, account } = common(request);
9946
9974
  if (request["kind"] === "bitcoin.message") {
9947
9975
  const { format } = request;
9948
9976
  const target = messageTarget(request["address"], network, format);
@@ -16762,6 +16790,18 @@ function mapChainError(error) {
16762
16790
  // ../wallet-kernel/src/modules/chains/bitcoin.ts
16763
16791
  var isPath2 = (path) => Array.isArray(path) && path.every((p) => typeof p === "number");
16764
16792
  var hex = (bytes) => bytes.reduce((acc, b) => acc + b.toString(16).padStart(2, "0"), "");
16793
+ var PURPOSES = [44, 49, 84, 86];
16794
+ var contextFor = (b, purpose) => {
16795
+ let ctx = b.purposes.get(purpose);
16796
+ if (ctx === undefined) {
16797
+ ctx = {
16798
+ accountNode: b.master.derive(`m/${purpose}'/${b.coinType}'/${b.groupIndex}'`),
16799
+ children: []
16800
+ };
16801
+ b.purposes.set(purpose, ctx);
16802
+ }
16803
+ return ctx;
16804
+ };
16765
16805
  var derivationOf = (owner, fingerprint, purpose) => {
16766
16806
  if (purpose === 86) {
16767
16807
  const claimed = (der) => der.fingerprint === fingerprint && isPath2(der.path);
@@ -16781,46 +16821,52 @@ var derivationOf = (owner, fingerprint, purpose) => {
16781
16821
  };
16782
16822
  var expectedPub = (purpose, pub) => purpose === 86 ? pub.slice(1) : pub;
16783
16823
  var expectedScript = (purpose, pub) => purpose === 86 ? p2tr(pub.slice(1)).script : purpose === 49 ? p2sh(p2wpkh(pub)).script : purpose === 44 ? p2pkh(pub).script : p2wpkh(pub).script;
16784
- var childAt = (b, path) => {
16824
+ var childAt = (b, purpose, path) => {
16785
16825
  const [purposePath, coinPath, groupPath, change, index] = path;
16786
- if (path.length !== 5 || purposePath !== b.purpose + b.hardened || coinPath !== b.coinType + b.hardened || groupPath !== b.account.groupIndex + b.hardened || change === undefined || index === undefined || change !== 0 && change !== 1 || index < 0 || index >= b.hardened) {
16826
+ if (path.length !== 5 || purposePath !== purpose + b.hardened || coinPath !== b.coinType + b.hardened || groupPath !== b.groupIndex + b.hardened || change === undefined || index === undefined || change !== 0 && change !== 1 || index < 0 || index >= b.hardened) {
16787
16827
  throw new KernelError("invalid-input", "derivation path is not this account's");
16788
16828
  }
16789
- const child = b.accountNode.deriveChild(change).deriveChild(index);
16790
- b.children.push(child);
16829
+ const ctx = contextFor(b, purpose);
16830
+ const child = ctx.accountNode.deriveChild(change).deriveChild(index);
16831
+ ctx.children.push(child);
16791
16832
  return child;
16792
16833
  };
16793
- var bitcoinInput = (b, tx, idx) => {
16834
+ var bitcoinInput = (b, tx, entry) => {
16835
+ const purpose = BTC_PURPOSE[entry.template];
16836
+ if (purpose === undefined) {
16837
+ throw new KernelError("invalid-input", "sign template is not a bitcoin account template");
16838
+ }
16839
+ const { index: idx } = entry;
16794
16840
  const input = tx.getInput(idx);
16795
- const entry = derivationOf(input, b.master.fingerprint, b.purpose);
16796
- if (entry === undefined) {
16841
+ const der = derivationOf(input, b.master.fingerprint, purpose);
16842
+ if (der === undefined) {
16797
16843
  throw new KernelError("invalid-input", `input ${idx}: derivation entry is not this account's`);
16798
16844
  }
16799
- const child = childAt(b, entry.path);
16845
+ const child = childAt(b, purpose, der.path);
16800
16846
  const pub = child.publicKey;
16801
16847
  const priv = child.privateKey;
16802
16848
  if (pub === null || priv === null) {
16803
16849
  throw new KernelError("integrity", "no key at derivation path");
16804
16850
  }
16805
- if (!deepEqual(entry.pubkey, expectedPub(b.purpose, pub))) {
16851
+ if (!deepEqual(der.pubkey, expectedPub(purpose, pub))) {
16806
16852
  throw new KernelError("invalid-input", `input ${idx}: derivation pubkey is not ours`);
16807
16853
  }
16808
- if (b.purpose === 44) {
16854
+ if (purpose === 44) {
16809
16855
  if (input.nonWitnessUtxo === undefined) {
16810
16856
  throw new KernelError("invalid-input", `input ${idx}: P2PKH spend requires nonWitnessUtxo`);
16811
16857
  }
16812
16858
  const vout = input.index;
16813
16859
  const outs = input.nonWitnessUtxo.outputs;
16814
16860
  const prevScript = vout === undefined ? undefined : outs[vout]?.script;
16815
- if (prevScript === undefined || !deepEqual(prevScript, expectedScript(b.purpose, pub))) {
16861
+ if (prevScript === undefined || !deepEqual(prevScript, expectedScript(purpose, pub))) {
16816
16862
  throw new KernelError("invalid-input", `input ${idx}: nonWitnessUtxo prevout is not this key's`);
16817
16863
  }
16818
16864
  } else {
16819
16865
  const prevout = input.witnessUtxo;
16820
- if (prevout === undefined || !deepEqual(prevout.script, expectedScript(b.purpose, pub))) {
16866
+ if (prevout === undefined || !deepEqual(prevout.script, expectedScript(purpose, pub))) {
16821
16867
  throw new KernelError("invalid-input", `input ${idx}: prevout script is not this key's`);
16822
16868
  }
16823
- if (b.purpose === 49 && (input.redeemScript === undefined || !deepEqual(input.redeemScript, p2wpkh(pub).script))) {
16869
+ if (purpose === 49 && (input.redeemScript === undefined || !deepEqual(input.redeemScript, p2wpkh(pub).script))) {
16824
16870
  throw new KernelError("invalid-input", `input ${idx}: redeemScript is not this key's`);
16825
16871
  }
16826
16872
  }
@@ -16833,25 +16879,27 @@ var bitcoinOwnScripts = (b, tx) => {
16833
16879
  if (output.script === undefined) {
16834
16880
  continue;
16835
16881
  }
16836
- const entry = derivationOf(output, b.master.fingerprint, b.purpose);
16837
- if (entry === undefined) {
16838
- continue;
16839
- }
16840
- try {
16841
- const child = childAt(b, entry.path);
16842
- const pub = child.publicKey;
16843
- if (pub !== null && deepEqual(output.script, expectedScript(b.purpose, pub))) {
16844
- ownScripts.add(hex(output.script));
16882
+ for (const purpose of PURPOSES) {
16883
+ const entry = derivationOf(output, b.master.fingerprint, purpose);
16884
+ if (entry === undefined) {
16885
+ continue;
16886
+ }
16887
+ try {
16888
+ const child = childAt(b, purpose, entry.path);
16889
+ const pub = child.publicKey;
16890
+ if (pub !== null && deepEqual(output.script, expectedScript(purpose, pub))) {
16891
+ ownScripts.add(hex(output.script));
16892
+ }
16893
+ } catch {
16894
+ continue;
16845
16895
  }
16846
- } catch {
16847
- continue;
16848
16896
  }
16849
16897
  }
16850
16898
  return ownScripts;
16851
16899
  };
16852
16900
  var executeBitcoinPsbt = (b, request, random, intentOnly) => {
16853
16901
  const tx = Transaction.fromPSBT(request.psbt);
16854
- const inputs = request.sign.map((idx) => bitcoinInput(b, tx, idx));
16902
+ const inputs = request.sign.map((entry) => bitcoinInput(b, tx, entry));
16855
16903
  const ownScripts = bitcoinOwnScripts(b, tx);
16856
16904
  let normalized;
16857
16905
  try {
@@ -16876,14 +16924,14 @@ var executeBitcoinPsbt = (b, request, random, intentOnly) => {
16876
16924
  signedPsbt: signBitcoinPsbt(request.psbt, inputs, () => random(32))
16877
16925
  };
16878
16926
  };
16879
- var executeBitcoinMessage = (b, request, random, intentOnly) => {
16880
- if (request.format === "legacy" && b.account.template !== "btc.bip44") {
16927
+ var executeBitcoinMessage = (b, purpose, request, random, intentOnly) => {
16928
+ if (request.format === "legacy" && purpose !== 44) {
16881
16929
  throw new KernelError("unsupported", "legacy message format is btc.bip44-only");
16882
16930
  }
16883
- const child = childAt(b, [
16884
- b.purpose + b.hardened,
16931
+ const child = childAt(b, purpose, [
16932
+ purpose + b.hardened,
16885
16933
  b.coinType + b.hardened,
16886
- b.account.groupIndex + b.hardened,
16934
+ b.groupIndex + b.hardened,
16887
16935
  request.change,
16888
16936
  request.index
16889
16937
  ]);
@@ -16892,7 +16940,7 @@ var executeBitcoinMessage = (b, request, random, intentOnly) => {
16892
16940
  if (pub === null || priv === null) {
16893
16941
  throw new KernelError("integrity", "no key at derivation path");
16894
16942
  }
16895
- const derived = b.purpose === 86 ? p2tr(pub.slice(1), undefined, b.net.params).address : b.purpose === 49 ? p2sh(p2wpkh(pub, b.net.params), b.net.params).address : b.purpose === 44 ? p2pkh(pub, b.net.params).address : p2wpkh(pub, b.net.params).address;
16943
+ const derived = purpose === 86 ? p2tr(pub.slice(1), undefined, b.net.params).address : purpose === 49 ? p2sh(p2wpkh(pub, b.net.params), b.net.params).address : purpose === 44 ? p2pkh(pub, b.net.params).address : p2wpkh(pub, b.net.params).address;
16896
16944
  if (derived !== request.address) {
16897
16945
  throw new KernelError("invalid-input", "address is not ours");
16898
16946
  }
@@ -16921,34 +16969,33 @@ var executeBitcoinMessage = (b, request, random, intentOnly) => {
16921
16969
  var executeBitcoin = (random, request, account, seed, intentOnly) => {
16922
16970
  const net = bitcoinNetwork(request.network);
16923
16971
  const master = HDKey.fromMasterSeed(seed, net.bip32);
16972
+ const b = {
16973
+ master,
16974
+ purposes: new Map,
16975
+ coinType: net.coinType,
16976
+ hardened: 2147483648,
16977
+ groupIndex: account.groupIndex,
16978
+ net
16979
+ };
16924
16980
  try {
16925
- const purpose = BTC_PURPOSE[account.template] ?? 84;
16926
- const { coinType } = net;
16927
- const hardened = 2147483648;
16928
- const accountNode = master.derive(`m/${purpose}'/${coinType}'/${account.groupIndex}'`);
16929
- const children = [];
16930
- const b = {
16931
- master,
16932
- accountNode,
16933
- children,
16934
- purpose,
16935
- coinType,
16936
- hardened,
16937
- account,
16938
- net
16939
- };
16940
- try {
16941
- if (request.kind === "bitcoin.psbt") {
16942
- return executeBitcoinPsbt(b, request, random, intentOnly);
16943
- }
16944
- return executeBitcoinMessage(b, request, random, intentOnly);
16945
- } finally {
16946
- for (const child of children) {
16981
+ if (request.kind === "bitcoin.psbt") {
16982
+ return executeBitcoinPsbt(b, request, random, intentOnly);
16983
+ }
16984
+ if (request.account.kind !== "hd") {
16985
+ throw new KernelError("unsupported", "imported accounts are not yet supported");
16986
+ }
16987
+ const purpose = BTC_PURPOSE[request.account.template];
16988
+ if (purpose === undefined) {
16989
+ throw new KernelError("invalid-input", "account template is not a bitcoin template");
16990
+ }
16991
+ return executeBitcoinMessage(b, purpose, request, random, intentOnly);
16992
+ } finally {
16993
+ for (const ctx of b.purposes.values()) {
16994
+ for (const child of ctx.children) {
16947
16995
  child.wipePrivateData();
16948
16996
  }
16949
- accountNode.wipePrivateData();
16997
+ ctx.accountNode.wipePrivateData();
16950
16998
  }
16951
- } finally {
16952
16999
  master.wipePrivateData();
16953
17000
  }
16954
17001
  };
@@ -17207,23 +17254,35 @@ var chainSign = async (ctx, params, session) => {
17207
17254
  if (account.kind !== "hd") {
17208
17255
  throw new KernelError("unsupported", "imported accounts are not yet supported");
17209
17256
  }
17210
- const [family] = request.kind.split(".", 1);
17211
- const expected = family === "evm" ? ["evm.bip44"] : family === "solana" ? ["solana.bip44-change"] : ["btc.bip44", "btc.bip49", "btc.bip84", "btc.bip86"];
17212
- if (!expected.includes(account.template)) {
17213
- throw new KernelError("invalid-input", "account template does not match the request family");
17257
+ if (request.kind === "bitcoin.psbt") {
17258
+ if (request.sign.some((s) => BTC_PURPOSE[s.template] === undefined)) {
17259
+ throw new KernelError("invalid-input", "sign template is not a bitcoin account template");
17260
+ }
17261
+ } else {
17262
+ const [family] = request.kind.split(".", 1);
17263
+ const expected = family === "evm" ? ["evm.bip44"] : family === "solana" ? ["solana.bip44-change"] : ["btc.bip44", "btc.bip49", "btc.bip84", "btc.bip86"];
17264
+ if (!("template" in account) || !expected.includes(account.template)) {
17265
+ throw new KernelError("invalid-input", "account template does not match the request family");
17266
+ }
17214
17267
  }
17215
17268
  const record = ctx.image.records.find((entry) => entry.kind === "entropy" && entry.id === account.walletId);
17216
17269
  if (record === undefined) {
17217
17270
  throw new KernelError("not-found", "no such wallet");
17218
17271
  }
17219
17272
  const execute = (seed, intentOnly) => {
17220
- if (request.kind === "evm.transaction" || request.kind === "evm.personal-message" || request.kind === "evm.typed-data") {
17221
- return executeEvm(request, account, seed, intentOnly);
17273
+ if (request.kind === "bitcoin.psbt") {
17274
+ return executeBitcoin(ctx.platform.random, request, request.account, seed, intentOnly);
17275
+ }
17276
+ if (request.account.kind !== "hd") {
17277
+ throw new KernelError("unsupported", "imported accounts are not yet supported");
17222
17278
  }
17223
- if (request.kind === "bitcoin.psbt" || request.kind === "bitcoin.message") {
17224
- return executeBitcoin(ctx.platform.random, request, account, seed, intentOnly);
17279
+ if (request.kind === "bitcoin.message") {
17280
+ return executeBitcoin(ctx.platform.random, request, request.account, seed, intentOnly);
17281
+ }
17282
+ if (request.kind === "evm.transaction" || request.kind === "evm.personal-message" || request.kind === "evm.typed-data") {
17283
+ return executeEvm(request, request.account, seed, intentOnly);
17225
17284
  }
17226
- return executeSolana(request, account, seed, intentOnly);
17285
+ return executeSolana(request, request.account, seed, intentOnly);
17227
17286
  };
17228
17287
  const withSeed = async (run) => withWalletSeed(ctx.platform.pbkdf2, dek, record, run);
17229
17288
  const prepared = await withSeed((seed) => execute(seed, true));
@@ -17276,7 +17335,7 @@ function nostrKeyAt(master, index) {
17276
17335
  }
17277
17336
  }
17278
17337
 
17279
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip19.mjs
17338
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip19.mjs
17280
17339
  var Bech32MaxSize2 = 5000;
17281
17340
  function encodeBech32(prefix, data) {
17282
17341
  const words = bech32.toWords(data);
@@ -17449,7 +17508,7 @@ async function scryptAsync(password, salt, opts) {
17449
17508
  return scryptOutput(password, dkLen, B, V, tmp);
17450
17509
  }
17451
17510
 
17452
- // ../../node_modules/.bun/@qntx+nostr@0.15.3+ea9f70ecde856c26/node_modules/@qntx/nostr/dist/nips/nip49.mjs
17511
+ // ../../node_modules/.bun/@qntx+nostr@0.15.4+cb72dcd9952e90a1/node_modules/@qntx/nostr/dist/nips/nip49.mjs
17453
17512
  var VERSION2 = 2;
17454
17513
  var SALT_LEN = 16;
17455
17514
  var NONCE_LEN = 24;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qntx/wallet-worker",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Web Worker and worker_threads hosts for @qntx/wallet",
5
5
  "keywords": [
6
6
  "nostr",
@@ -63,12 +63,12 @@
63
63
  "@arethetypeswrong/cli": "0.18.5",
64
64
  "@noble/hashes": "2.4.0",
65
65
  "@playwright/test": "1.64.1",
66
- "@qntx/nostr": "0.15.3",
66
+ "@qntx/nostr": "0.15.4",
67
67
  "@qntx/spark": "0.5.2",
68
68
  "@qntx/spark-wasm": "0.5.2",
69
69
  "@qntx/tsconfig": "1.0.1",
70
- "@qntx/wallet": "0.11.0",
71
- "@qntx/wallet-kernel": "0.11.0",
70
+ "@qntx/wallet": "0.12.0",
71
+ "@qntx/wallet-kernel": "0.12.0",
72
72
  "@scure/base": "2.4.0",
73
73
  "@scure/btc-signer": "2.4.1",
74
74
  "@types/node": "26.6.6",
@@ -77,7 +77,7 @@
77
77
  "vite-plus": "1.4.0"
78
78
  },
79
79
  "peerDependencies": {
80
- "@qntx/wallet": "0.11.0"
80
+ "@qntx/wallet": "0.12.0"
81
81
  },
82
82
  "devEngines": {
83
83
  "packageManager": {