b017 0.0.0-b → 0.1.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +280 -1
  2. package/README.md +120 -25
  3. package/dist/index.d.ts +15 -7
  4. package/dist/index.js +14 -5
  5. package/dist/lib/boltLib.d.ts +29 -3
  6. package/dist/lib/boltLib.js +60 -12
  7. package/dist/lib/genesis.d.ts +20 -0
  8. package/dist/lib/genesis.js +45 -0
  9. package/dist/lib/multi/multiBoltLib.d.ts +22 -2
  10. package/dist/lib/multi/multiBoltLib.js +111 -117
  11. package/dist/lib/scanner/beef.d.ts +11 -0
  12. package/dist/lib/scanner/beef.js +63 -0
  13. package/dist/lib/scanner/fingerprints.d.ts +12 -2
  14. package/dist/lib/scanner/fingerprints.js +44 -11
  15. package/dist/lib/scanner/verifyEvents.d.ts +84 -6
  16. package/dist/lib/scanner/verifyEvents.js +519 -44
  17. package/dist/lib/single/singleAncestor.d.ts +27 -3
  18. package/dist/lib/single/singleAncestor.js +49 -18
  19. package/dist/lib/single/singleSpend.d.ts +12 -3
  20. package/dist/lib/single/singleSpend.js +48 -16
  21. package/dist/tokens/BOLT.d.ts +6 -5
  22. package/dist/tokens/GenesisMultiBOLT.d.ts +29 -0
  23. package/dist/tokens/GenesisMultiBOLT.js +48 -0
  24. package/dist/tokens/MultiBOLT.d.ts +17 -11
  25. package/dist/tokens/MultiBOLT.js +79 -68
  26. package/dist/tokens/templates/AuthBolt.sx.template.d.ts +35 -0
  27. package/dist/tokens/templates/AuthBolt.sx.template.js +82 -0
  28. package/dist/tokens/templates/FixedMulti.sx.template.d.ts +13 -0
  29. package/dist/tokens/templates/FixedMulti.sx.template.js +23 -0
  30. package/dist/tokens/templates/MinSimple.sx.template.d.ts +10 -1
  31. package/dist/tokens/templates/MinSimple.sx.template.js +15 -1
  32. package/dist/tokens/templates/OpenMulti.sx.template.d.ts +13 -0
  33. package/dist/tokens/templates/OpenMulti.sx.template.js +23 -0
  34. package/dist/tokens/templates/SerialBolt.sx.template.d.ts +23 -0
  35. package/dist/tokens/templates/SerialBolt.sx.template.js +61 -0
  36. package/dist/tokens/templates/SimpleMulti.sx.template.d.ts +11 -5
  37. package/dist/tokens/templates/SimpleMulti.sx.template.js +30 -21
  38. package/dist/tokens/templates/pay2Proof.d.ts +2 -1
  39. package/dist/tokens/templates/pay2Proof.js +1 -1
  40. package/package.json +8 -3
  41. package/dist/tokens/templates/MinSimpleBalance.sx.template.d.ts +0 -24
  42. package/dist/tokens/templates/MinSimpleBalance.sx.template.js +0 -59
  43. package/dist/tokens/templates/MinSimpleDiscount.sx.template.d.ts +0 -23
  44. package/dist/tokens/templates/MinSimpleDiscount.sx.template.js +0 -58
@@ -1,8 +1,32 @@
1
1
  import { Transaction } from "@bsv/sdk";
2
2
  /** The 26 NFT ancestor-piece names, in unlockArgs order [0..25]. */
3
3
  export declare const PIECE_NAMES: string[];
4
+ /** AuthBolt's 27 ancestor pieces: PIECE_NAMES plus `Vin1AuthOrMiscData` (the ancestor commit's own owner
5
+ * authOrMiscData push, which LEADS its scriptSig) right after `Vin1Outpoint`. */
6
+ export declare const AUTH_PIECE_NAMES: string[];
7
+ /** SerialBolt's 27 ancestor pieces (genesis layout v3, built on MSBoltZF): PIECE_NAMES plus
8
+ * `Vin1CTXScriptCodeMintData` (the ancestor commit's spent token's mintData: the mint's bytes when the
9
+ * ancestor is the genesis spend, else empty) right after `Vin1CTXHeader`. Its lock leads with two pushes
10
+ * (genesisOutpoint, mintData), so it is read with leadingValuePushes = 2. */
11
+ export declare const SERIAL_PIECE_NAMES: string[];
12
+ /** The unlock-arg layout of one NFT-family contract: its ancestor pieces, and whether the unlock LEADS with
13
+ * an `authOrMiscData` arg (AuthBolt: [authOrMiscData, 27 ancestor pieces, 11 current-tx args]). */
14
+ export interface SingleLayout {
15
+ pieceNames: readonly string[];
16
+ hasAuth: boolean;
17
+ }
18
+ /** MinSimpleBolt: 37 args = 26 ancestor pieces + 11 current-tx args. */
19
+ export declare const MIN_SIMPLE_LAYOUT: SingleLayout;
20
+ /** AuthBolt: 39 args = authOrMiscData + 27 ancestor pieces + 11 current-tx args. */
21
+ export declare const AUTH_BOLT_LAYOUT: SingleLayout;
22
+ /** SerialBolt: 38 args = 27 ancestor pieces + 11 current-tx args (zero-funding, as MinSimpleBolt). */
23
+ export declare const SERIAL_BOLT_LAYOUT: SingleLayout;
24
+ /** SerialBolt's leading lock pushes (genesisOutpoint, mintData) = its leadingValuePushes. */
25
+ export declare const SERIAL_BOLT_LEADING = 2;
26
+ /** Unlock index of the first current-tx arg (`fundOutpoint`): after the optional auth arg and the ancestor pieces. */
27
+ export declare const currentArgsStart: (l: SingleLayout) => number;
4
28
  /** Extract one named NFT ancestor piece from an ancestor commit tx (its in[0] must have its
5
29
  * sourceTransaction attached). `leadingValuePushes`: 0 = identity, 1 = discount/balance. */
6
- export declare function ancestorPiece(name: string, ancestorTx: Transaction, leadingValuePushes: number): number[];
7
- /** All 26 ancestor pieces in unlockArgs order — PIECE_NAMES mapped through ancestorPiece. */
8
- export declare function singleAncestorPieces(ancestorTx: Transaction, leadingValuePushes: number): number[][];
30
+ export declare function ancestorPiece(name: string, ancestorTx: Transaction, leadingValuePushes: number, layout?: SingleLayout): number[];
31
+ /** All ancestor pieces in unlockArgs order: the layout's piece names mapped through ancestorPiece. */
32
+ export declare function singleAncestorPieces(ancestorTx: Transaction, leadingValuePushes: number, layout?: SingleLayout): number[][];
@@ -25,48 +25,79 @@ export const PIECE_NAMES = [
25
25
  "Vout1PubKeyHash", "Vout1PubKeyHashCommitment", "Vout1TxoType", "Vout1ParentOutpoint", "Vout1GrandparentOutpoint",
26
26
  "ChangeValue", "ChangeScript", "NLockTime",
27
27
  ];
28
+ /** AuthBolt's 27 ancestor pieces: PIECE_NAMES plus `Vin1AuthOrMiscData` (the ancestor commit's own owner
29
+ * authOrMiscData push, which LEADS its scriptSig) right after `Vin1Outpoint`. */
30
+ export const AUTH_PIECE_NAMES = [
31
+ "Version",
32
+ "Vin1Outpoint", "Vin1AuthOrMiscData", ...PIECE_NAMES.slice(2),
33
+ ];
34
+ /** SerialBolt's 27 ancestor pieces (genesis layout v3, built on MSBoltZF): PIECE_NAMES plus
35
+ * `Vin1CTXScriptCodeMintData` (the ancestor commit's spent token's mintData: the mint's bytes when the
36
+ * ancestor is the genesis spend, else empty) right after `Vin1CTXHeader`. Its lock leads with two pushes
37
+ * (genesisOutpoint, mintData), so it is read with leadingValuePushes = 2. */
38
+ export const SERIAL_PIECE_NAMES = [
39
+ ...PIECE_NAMES.slice(0, PIECE_NAMES.indexOf("Vin1CTXHeader") + 1),
40
+ "Vin1CTXScriptCodeMintData",
41
+ ...PIECE_NAMES.slice(PIECE_NAMES.indexOf("Vin1CTXHeader") + 1),
42
+ ];
43
+ /** MinSimpleBolt: 37 args = 26 ancestor pieces + 11 current-tx args. */
44
+ export const MIN_SIMPLE_LAYOUT = { pieceNames: PIECE_NAMES, hasAuth: false };
45
+ /** AuthBolt: 39 args = authOrMiscData + 27 ancestor pieces + 11 current-tx args. */
46
+ export const AUTH_BOLT_LAYOUT = { pieceNames: AUTH_PIECE_NAMES, hasAuth: true };
47
+ /** SerialBolt: 38 args = 27 ancestor pieces + 11 current-tx args (zero-funding, as MinSimpleBolt). */
48
+ export const SERIAL_BOLT_LAYOUT = { pieceNames: SERIAL_PIECE_NAMES, hasAuth: false };
49
+ /** SerialBolt's leading lock pushes (genesisOutpoint, mintData) = its leadingValuePushes. */
50
+ export const SERIAL_BOLT_LEADING = 2;
51
+ /** Unlock index of the first current-tx arg (`fundOutpoint`): after the optional auth arg and the ancestor pieces. */
52
+ export const currentArgsStart = (l) => (l.hasAuth ? 1 : 0) + l.pieceNames.length;
28
53
  /** Extract one named NFT ancestor piece from an ancestor commit tx (its in[0] must have its
29
54
  * sourceTransaction attached). `leadingValuePushes`: 0 = identity, 1 = discount/balance. */
30
- export function ancestorPiece(name, ancestorTx, leadingValuePushes) {
55
+ export function ancestorPiece(name, ancestorTx, leadingValuePushes, layout = MIN_SIMPLE_LAYOUT) {
31
56
  const in0 = ancestorTx.inputs[0];
32
57
  const in1 = ancestorTx.inputs[1];
33
- const u = in0.unlockingScript; // ancestor's bolt-spend unlock (37-arg layout)
34
- const spentLock = Script.fromBinary(u.chunks[34]?.data ?? []); // its in[0] CTX scriptCode
58
+ const u = in0.unlockingScript; // ancestor's bolt-spend unlock (37 / 39-arg layout)
59
+ const cur = currentArgsStart(layout); // unlock index of the ancestor's own fundOutpoint
60
+ const spentLock = Script.fromBinary(u.chunks[cur + 8]?.data ?? []); // its in[0] CTX scriptCode
35
61
  const sd = (i) => scriptChunk(spentLock, leadingValuePushes + i); // prior token's data fields
36
62
  const outLock = ancestorTx.outputs[0].lockingScript; // ancestor's OUTPUT token (vout0)
37
63
  const od = (i) => scriptChunk(outLock, leadingValuePushes + i);
64
+ // Zero-funding: an UNFUNDED ancestor commit has no in[1], a change-less one no out[2]. The contract
65
+ // derives its vin/vout counts from the SIZES of these pieces, so an absent input/output is passed as
66
+ // empty (OP_0), never invented.
38
67
  const changeOut = ancestorTx.outputs[2];
39
68
  switch (name) {
40
69
  case "Version": return le32(ancestorTx.version);
41
70
  case "Vin1Outpoint": return spentOutpoint(ancestorTx, 0);
42
- case "Vin1FundOutpoint": return scriptChunk(u, 26);
43
- case "Vin1ChangeOutput": return scriptChunk(u, 27);
44
- case "Vin1BeneficiaryPubKeyHash": return scriptChunk(u, 28);
45
- case "Vin1Sig": return scriptChunk(u, 29);
46
- case "Vin1PubKey": return scriptChunk(u, 30);
47
- case "Vin1CTXHeader": return scriptChunk(u, 31);
71
+ case "Vin1AuthOrMiscData": return scriptChunk(u, 0); // AuthBolt: the commit's own authOrMiscData (its first unlock push)
72
+ case "Vin1FundOutpoint": return scriptChunk(u, cur);
73
+ case "Vin1ChangeOutput": return scriptChunk(u, cur + 1);
74
+ case "Vin1BeneficiaryPubKeyHash": return scriptChunk(u, cur + 2);
75
+ case "Vin1Sig": return scriptChunk(u, cur + 3);
76
+ case "Vin1PubKey": return scriptChunk(u, cur + 4);
77
+ case "Vin1CTXHeader": return scriptChunk(u, cur + 5);
78
+ case "Vin1CTXScriptCodeMintData": return scriptChunk(spentLock, 1); // SerialBolt: lock chunk 1 (after genesisOutpoint)
48
79
  case "Vin1CTXScriptCodePubKeyHash": return sd(0);
49
80
  case "Vin1CTXScriptCodePubKeyHashCommitment": return sd(1);
50
81
  case "Vin1CTXScriptCodeTxoType": return sd(2);
51
82
  case "Vin1CTXScriptCodeParentOutpoint": return sd(3);
52
83
  case "Vin1CTXScriptCodeGrandparentOutpoint": return sd(4);
53
- case "Vin1CTXFooter": return scriptChunk(u, 35);
84
+ case "Vin1CTXFooter": return scriptChunk(u, cur + 9);
54
85
  case "Vin1NSequence": return le32(in0.sequence ?? 0xffffffff);
55
- case "Vin2Outpoint": return spentOutpoint(ancestorTx, 1);
56
- case "Vin2Script": return in1.unlockingScript.toBinary();
57
- case "Vin2NSequence": return le32(in1.sequence ?? 0xffffffff);
86
+ case "Vin2Outpoint": return in1 ? spentOutpoint(ancestorTx, 1) : [];
87
+ case "Vin2Script": return in1 ? in1.unlockingScript.toBinary() : [];
88
+ case "Vin2NSequence": return in1 ? le32(in1.sequence ?? 0xffffffff) : [];
58
89
  case "Vout1PubKeyHash": return od(0);
59
90
  case "Vout1PubKeyHashCommitment": return od(1);
60
91
  case "Vout1TxoType": return od(2);
61
92
  case "Vout1ParentOutpoint": return od(3);
62
93
  case "Vout1GrandparentOutpoint": return od(4);
63
- case "ChangeValue": return le64(changeOut.satoshis);
64
- case "ChangeScript": return changeOut.lockingScript.toBinary();
94
+ case "ChangeValue": return changeOut ? le64(changeOut.satoshis) : [];
95
+ case "ChangeScript": return changeOut ? changeOut.lockingScript.toBinary() : [];
65
96
  case "NLockTime": return le32(ancestorTx.lockTime);
66
97
  default: return [];
67
98
  }
68
99
  }
69
- /** All 26 ancestor pieces in unlockArgs order — PIECE_NAMES mapped through ancestorPiece. */
70
- export function singleAncestorPieces(ancestorTx, leadingValuePushes) {
71
- return PIECE_NAMES.map((name) => ancestorPiece(name, ancestorTx, leadingValuePushes));
100
+ /** All ancestor pieces in unlockArgs order: the layout's piece names mapped through ancestorPiece. */
101
+ export function singleAncestorPieces(ancestorTx, leadingValuePushes, layout = MIN_SIMPLE_LAYOUT) {
102
+ return layout.pieceNames.map((name) => ancestorPiece(name, ancestorTx, leadingValuePushes, layout));
72
103
  }
@@ -1,10 +1,12 @@
1
1
  import { Script, UnlockingScript, Transaction, PrivateKey, Hash } from "@bsv/sdk";
2
+ import { type Signer } from "../boltLib.js";
3
+ import { type SingleLayout } from "./singleAncestor.js";
2
4
  /** Number of leading ancestor-reconstruction args in the NFT unlock layout. */
3
5
  export declare const SINGLE_ANCESTOR_ARG_COUNT = 26;
4
- /** 26 empty ancestor pushes — the simple-transfer (and melt) case. */
5
- export declare const emptySingleAncestorChunks: () => any[];
6
+ /** Empty ancestor pushes (26 for MinSimple, 27 for AuthBolt) — the simple-transfer (and melt) case. */
7
+ export declare const emptySingleAncestorChunks: (count?: number) => any[];
6
8
  export interface SingleUnlockParams {
7
- privateKey: PrivateKey;
9
+ privateKey: PrivateKey | Signer;
8
10
  /** The next owner's 20-byte pubKeyHash (commit + settle both commit-to the recipient). */
9
11
  beneficiaryPubKeyHash: number[];
10
12
  /** UNLOCK_SCRIPT_SUFFIX ASM for the specific contract (patched from its artifact). */
@@ -19,6 +21,13 @@ export interface SingleUnlockParams {
19
21
  /** Leading immutable value pushes in the lock (0 = identity, 1 = discount/balance). Needed to read
20
22
  * the ancestor's token-data fields at the right chunk offset during back-reaching reconstruction. */
21
23
  leadingValuePushes?: number;
24
+ /** The contract's unlock-arg layout (default MinSimple: 26 ancestor pieces; AuthBolt adds a leading auth arg + a 27th piece). */
25
+ layout?: SingleLayout;
26
+ /** AuthBolt only: the owner-supplied/transaction negotiated/challenge based authOrMiscData (<= 75 B), the FIRST unlock arg. Omitted / [] = OP_0. */
27
+ authOrMiscData?: number[];
28
+ /** MELT: burn the token. Every arg is OP_0 (a null CTX takes the lock's melt branch) except the owner's
29
+ * signature and pubKey; the tx has no token output. */
30
+ melt?: boolean;
22
31
  }
23
32
  /**
24
33
  * Build a ScriptTemplate-compatible unlocker for an NFT-family bolt spend (transfer commit/settle, and
@@ -9,13 +9,15 @@
9
9
  // The ancestor-reconstruction path (a settle reaching back over chains >= 4 txs, e.g. a coupon's 2nd
10
10
  // hop) is implemented via singleAncestorPieces, validated against the canonical sx golden.
11
11
  //
12
- // All three compile to an IDENTICAL 37-arg unlock layout (no mintData / issuerPubKey /
12
+ // MinSimpleBolt compiles to a 37-arg unlock layout; AuthBolt to 39 (a leading authOrMiscData + a 27th ancestor
13
+ // piece, `Vin1AuthOrMiscData`; see SingleLayout in singleAncestor.ts). The indices below are MinSimple's.
14
+ // All three (Min/Discount/Balance) shared an IDENTICAL 37-arg unlock layout (no mintData / issuerPubKey /
13
15
  // genesisOutpoint / miscData in the ancestor reconstruction):
14
16
  //
15
17
  // [0..25] ancestor pieces (26) — EMPTY for the simple transfer (mint->commit->settle); populated
16
18
  // by singleAncestorPieces when a settle reaches back over a chain >= 4 txs (coupon 2nd hop).
17
- // [26] fundOutpoint — the funding input's outpoint (36B)
18
- // [27] changeOutput — serialised change output (value + varint len + script)
19
+ // [26] fundOutpoint — the funding input's outpoint (36B); OP_0 when UNFUNDED (zero-funding)
20
+ // [27] changeOutput — serialised change output (value + varint len + script); OP_0 when change-less
19
21
  // [28] beneficiaryPubKeyHash — the next owner's 20-byte pkh
20
22
  // [29] sig — checksig-format signature over the lock-only preimage
21
23
  // [30] pubKey — signer's 33-byte compressed pubkey
@@ -26,14 +28,19 @@
26
28
  // unlock-script-code prefix `ad68`), exactly as SimpleMultiBolt. splitCtx(ctx, 2) accordingly.
27
29
  import { Script, UnlockingScript, TransactionSignature, Hash, } from "@bsv/sdk";
28
30
  import { splitCtx, buildOutpoint, buildChangeOutput, createSignature, scriptChunksFromBin } from "../boltLib.js";
29
- import { singleAncestorPieces } from "./singleAncestor.js";
31
+ import { singleAncestorPieces, MIN_SIMPLE_LAYOUT } from "./singleAncestor.js";
30
32
  /** Number of leading ancestor-reconstruction args in the NFT unlock layout. */
31
33
  export const SINGLE_ANCESTOR_ARG_COUNT = 26;
34
+ /** The p2pb proof lock leads with the 2-byte b017 marker push (b0 17). */
35
+ const isProofLock = (s) => {
36
+ const d = s?.chunks[0]?.data;
37
+ return !!d && d.length === 2 && d[0] === 0xb0 && d[1] === 0x17;
38
+ };
32
39
  const SIGNATURE_SCOPE = TransactionSignature.SIGHASH_FORKID | TransactionSignature.SIGHASH_ALL;
33
- /** 26 empty ancestor pushes — the simple-transfer (and melt) case. */
34
- export const emptySingleAncestorChunks = () => {
40
+ /** Empty ancestor pushes (26 for MinSimple, 27 for AuthBolt) — the simple-transfer (and melt) case. */
41
+ export const emptySingleAncestorChunks = (count = SINGLE_ANCESTOR_ARG_COUNT) => {
35
42
  const out = [];
36
- for (let i = 0; i < SINGLE_ANCESTOR_ARG_COUNT; i++)
43
+ for (let i = 0; i < count; i++)
37
44
  out.push(...scriptChunksFromBin([]));
38
45
  return out;
39
46
  };
@@ -43,6 +50,7 @@ export const emptySingleAncestorChunks = () => {
43
50
  */
44
51
  export function singleSpendUnlock(params) {
45
52
  const { privateKey, beneficiaryPubKeyHash, unlockScriptSuffixASM, forceNoChange, forceNoFund, prevTxs } = params;
53
+ const layout = params.layout ?? MIN_SIMPLE_LAYOUT;
46
54
  return {
47
55
  sign: async (tx, inputIndex) => {
48
56
  const input = tx.inputs[inputIndex];
@@ -63,8 +71,8 @@ export function singleSpendUnlock(params) {
63
71
  const ancestorIdx = txIdx - 3;
64
72
  const hasAncestor = ancestorIdx >= 1 && txIdx >= 4 && txIdx % 2 === 0;
65
73
  const ancestorChunks = hasAncestor
66
- ? singleAncestorPieces(prevTxs[ancestorIdx], params.leadingValuePushes ?? 0).flatMap((p) => scriptChunksFromBin(p))
67
- : emptySingleAncestorChunks();
74
+ ? singleAncestorPieces(prevTxs[ancestorIdx], params.leadingValuePushes ?? 0, layout).flatMap((p) => scriptChunksFromBin(p))
75
+ : emptySingleAncestorChunks(layout.pieceNames.length);
68
76
  const otherInputs = tx.inputs.filter((_, i) => i !== inputIndex);
69
77
  const ocsSubScript = new Script(Script.fromASM("OP_CHECKSIGVERIFY OP_ENDIF").chunks.concat(lockingScript.chunks));
70
78
  const ctx = TransactionSignature.format({
@@ -82,14 +90,38 @@ export function singleSpendUnlock(params) {
82
90
  });
83
91
  const { ctxHeader, ctxCodeLen, ctxCodeUnlockScriptCode, ctxCodeLockScriptCode, ctxFooter, ctxCodeLockLen } = splitCtx(ctx, 2);
84
92
  const ctxForSig = ctxHeader.concat(...[ctxCodeLockLen, ctxCodeLockScriptCode, ctxFooter]);
85
- const { sigForScript, pubkeyForScript } = createSignature(privateKey, ctxForSig, SIGNATURE_SCOPE);
86
- const fundInput = tx.inputs[tx.inputs.length - 1];
87
- const fundOutpoint = forceNoFund
88
- ? []
89
- : buildOutpoint(fundInput.sourceTransaction, fundInput.sourceOutputIndex);
90
- const changeOutput = forceNoChange ? [] : buildChangeOutput(tx, tx.outputs.length - 1);
93
+ const { sigForScript, pubkeyForScript } = await createSignature(privateKey, ctxForSig, SIGNATURE_SCOPE);
94
+ // Melt: a null CTX (every CTX piece OP_0) takes the lock's melt branch, which checks only the owner's
95
+ // signature and pubKey (plus the issuer guard on a genesis / 2nd-tx token). No ancestor, fund or change args.
96
+ if (params.melt) {
97
+ const empty = () => scriptChunksFromBin([]);
98
+ return new UnlockingScript([
99
+ ...(layout.hasAuth ? empty() : []),
100
+ ...emptySingleAncestorChunks(layout.pieceNames.length),
101
+ ...empty(), ...empty(), ...empty(), // fundOutpoint, changeOutput, beneficiaryPubKeyHash
102
+ ...scriptChunksFromBin(sigForScript),
103
+ ...scriptChunksFromBin(pubkeyForScript),
104
+ ...empty(), ...empty(), ...empty(), ...empty(), ...empty(), ...empty(), // the six CTX pieces
105
+ ...Script.fromASM(unlockScriptSuffixASM).chunks,
106
+ ]);
107
+ }
108
+ // Zero-funding: the funding input and the change output are each OPTIONAL (null -> OP_0). Inputs are
109
+ // [token, proof?, funding?] and outputs [token, p2pb?, change?]: a settle that reaches back (hop >= 2)
110
+ // carries the p2pb proof as input 1; a commit carries the p2pb as output 1. Detect them from the tx
111
+ // itself so an unfunded or change-less spend is described correctly (forceNo* still force-omit).
112
+ const nextIn = tx.inputs[inputIndex + 1];
113
+ const nextLock = nextIn?.sourceTransaction?.outputs[nextIn.sourceOutputIndex]?.lockingScript;
114
+ const hasProof = nextIn !== undefined && (nextLock ? isProofLock(nextLock) : hasAncestor);
115
+ const fundInput = forceNoFund ? undefined : tx.inputs[inputIndex + 1 + (hasProof ? 1 : 0)];
116
+ const changeIdx = isProofLock(tx.outputs[1]?.lockingScript) ? 2 : 1; // commit: [token, p2pb, change]
117
+ const hasChange = !forceNoChange && tx.outputs.length > changeIdx;
118
+ if (hasChange && !fundInput)
119
+ throw new Error("an unfunded spend has no change to return (change needs a funding input)");
120
+ const fundOutpoint = fundInput ? buildOutpoint(fundInput.sourceTransaction, fundInput.sourceOutputIndex) : [];
121
+ const changeOutput = hasChange ? buildChangeOutput(tx, changeIdx) : [];
91
122
  return new UnlockingScript([
92
- ...ancestorChunks, // [0..25]
123
+ ...(layout.hasAuth ? scriptChunksFromBin(params.authOrMiscData ?? []) : []), // AuthBolt: [0] authOrMiscData
124
+ ...ancestorChunks, // MinSimple [0..25]; AuthBolt [1..27]
93
125
  ...scriptChunksFromBin(fundOutpoint), // [26]
94
126
  ...scriptChunksFromBin(changeOutput), // [27]
95
127
  ...scriptChunksFromBin(beneficiaryPubKeyHash), // [28]
@@ -1,4 +1,5 @@
1
1
  import { Transaction, PrivateKey } from "@bsv/sdk";
2
+ import type { Signer, Recipient } from "../lib/boltLib.js";
2
3
  export declare abstract class BOLT {
3
4
  tx?: Transaction;
4
5
  voutIdx?: number;
@@ -6,12 +7,12 @@ export declare abstract class BOLT {
6
7
  pubKey: number[];
7
8
  issuerPubKey: number[];
8
9
  genesisOutpoint: number[];
9
- privKey: PrivateKey;
10
+ signer: Signer;
10
11
  mintData?: number[];
11
12
  pubKeyHash?: number[];
12
13
  constructor();
13
- abstract mint(privKey: PrivateKey, sourceTransaction: Transaction, mintData?: string): any;
14
- abstract commit(toPrivKey: PrivateKey): any;
15
- abstract settle(toPrivKey: PrivateKey): any;
16
- abstract transfer(toPrivKey: PrivateKey): any;
14
+ abstract mint(owner: PrivateKey | Signer, sourceTransaction: Transaction, mintData?: string): any;
15
+ abstract commit(to: Recipient): any;
16
+ abstract settle(to: Recipient): any;
17
+ abstract transfer(to: Recipient): any;
17
18
  }
@@ -0,0 +1,29 @@
1
+ import { PrivateKey, Transaction } from "@bsv/sdk";
2
+ import { SimpleMultiBOLT } from "./MultiBOLT.js";
3
+ import SimpleMultiTemplate from "./templates/SimpleMulti.sx.template.js";
4
+ type Funding = {
5
+ tx: Transaction;
6
+ vout: number;
7
+ key: PrivateKey;
8
+ };
9
+ /**
10
+ * FixedBOLT: one genesis per asset. `genesisOutpoint` is null on the mint, the mint outpoint from the genesis
11
+ * spend on, and copied forever: it is the asset id. Only tokens of ONE genesis merge (a cross-genesis or a
12
+ * cross-issuer merge is refused by the covenant at the merge commit).
13
+ */
14
+ export declare class FixedBOLT extends SimpleMultiBOLT {
15
+ protected readonly leading: number;
16
+ protected template(): SimpleMultiTemplate;
17
+ merge(other: SimpleMultiBOLT, toKey: PrivateKey, fundingSource?: Funding): Promise<SimpleMultiBOLT>;
18
+ }
19
+ /**
20
+ * OpenBOLT: many mints under one issuer. `lastMergeOrGenesisOutpoint` (LMG) starts like FixedBOLT's
21
+ * genesisOutpoint, but tokens of different genesis roots may merge: the merge commit re-roots the merged
22
+ * token's LMG to vin0's token outpoint. A cross-issuer merge is refused by the covenant.
23
+ */
24
+ export declare class OpenBOLT extends SimpleMultiBOLT {
25
+ protected readonly leading: number;
26
+ protected template(): SimpleMultiTemplate;
27
+ merge(other: SimpleMultiBOLT, toKey: PrivateKey, fundingSource?: Funding): Promise<SimpleMultiBOLT>;
28
+ }
29
+ export {};
@@ -0,0 +1,48 @@
1
+ import { Utils } from "@bsv/sdk";
2
+ import { SimpleMultiBOLT } from "./MultiBOLT.js";
3
+ import FixedMultiTemplate from "./templates/FixedMulti.sx.template.js";
4
+ import OpenMultiTemplate from "./templates/OpenMulti.sx.template.js";
5
+ const leadingField = (t) => Utils.toHex(t.tx.outputs[t.voutIdx].lockingScript.chunks[0].data);
6
+ const issuerOf = (t) => Utils.toHex(t.tx.outputs[t.voutIdx].lockingScript.chunks[12].data);
7
+ function assertSameIssuer(a, b, type) {
8
+ if (issuerOf(a) !== issuerOf(b))
9
+ throw new Error(`${type}: cannot merge tokens of different issuers`);
10
+ }
11
+ /**
12
+ * FixedBOLT: one genesis per asset. `genesisOutpoint` is null on the mint, the mint outpoint from the genesis
13
+ * spend on, and copied forever: it is the asset id. Only tokens of ONE genesis merge (a cross-genesis or a
14
+ * cross-issuer merge is refused by the covenant at the merge commit).
15
+ */
16
+ export class FixedBOLT extends SimpleMultiBOLT {
17
+ constructor() {
18
+ super(...arguments);
19
+ this.leading = 2;
20
+ }
21
+ template() {
22
+ return new FixedMultiTemplate(this.mintData);
23
+ }
24
+ async merge(other, toKey, fundingSource) {
25
+ assertSameIssuer(this, other, "FixedBOLT");
26
+ if (leadingField(this) !== leadingField(other))
27
+ throw new Error("FixedBOLT: cannot merge tokens of different genesis outpoints (different assets)");
28
+ return super.merge(other, toKey, fundingSource);
29
+ }
30
+ }
31
+ /**
32
+ * OpenBOLT: many mints under one issuer. `lastMergeOrGenesisOutpoint` (LMG) starts like FixedBOLT's
33
+ * genesisOutpoint, but tokens of different genesis roots may merge: the merge commit re-roots the merged
34
+ * token's LMG to vin0's token outpoint. A cross-issuer merge is refused by the covenant.
35
+ */
36
+ export class OpenBOLT extends SimpleMultiBOLT {
37
+ constructor() {
38
+ super(...arguments);
39
+ this.leading = 2;
40
+ }
41
+ template() {
42
+ return new OpenMultiTemplate(this.mintData);
43
+ }
44
+ async merge(other, toKey, fundingSource) {
45
+ assertSameIssuer(this, other, "OpenBOLT");
46
+ return super.merge(other, toKey, fundingSource);
47
+ }
48
+ }
@@ -1,4 +1,6 @@
1
1
  import { Transaction, PrivateKey, TransactionOutput, TransactionInput } from "@bsv/sdk";
2
+ import SimpleMultiTemplate from "./templates/SimpleMulti.sx.template.js";
3
+ import { type Signer, type Recipient } from "../lib/boltLib.js";
2
4
  import { BOLT } from "./BOLT.js";
3
5
  export type VerifierType = 'bsv';
4
6
  export declare class SimpleMultiBOLT extends BOLT {
@@ -7,8 +9,12 @@ export declare class SimpleMultiBOLT extends BOLT {
7
9
  balance: number[];
8
10
  balanceCommit: number[];
9
11
  outputIndexN: number[];
10
- mint(privKey: PrivateKey, sourceTransaction: Transaction, _mintData?: string, balance?: number[]): Promise<this>;
11
- createTransferInputs: (toPrivKey: PrivateKey, _miscData: string, isCommitTx?: boolean, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean) => (TransactionInput | {
12
+ /** Lock pushes that lead the SimpleMultiBolt fields (0 here; FixedBOLT / OpenBOLT 2). */
13
+ protected readonly leading: number;
14
+ /** The script template for this token type; FixedBOLT / OpenBOLT override it. */
15
+ protected template(): SimpleMultiTemplate;
16
+ mint(owner: PrivateKey | Signer, sourceTransaction: Transaction, mintData?: string | number[], balance?: number[]): Promise<this>;
17
+ createTransferInputs: (toPrivKey: Recipient, _miscData: string, isCommitTx?: boolean, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean) => (TransactionInput | {
12
18
  sourceTransaction: Transaction;
13
19
  sourceOutputIndex: number;
14
20
  unlockingScriptTemplate: {
@@ -17,10 +23,10 @@ export declare class SimpleMultiBOLT extends BOLT {
17
23
  };
18
24
  sequence: number;
19
25
  })[];
20
- createTransferOutputs: (toPrivKey: PrivateKey, isCommitTx?: boolean, forceNoChange?: boolean, customChangeScript?: any) => TransactionOutput[];
21
- commit(toPrivKey: PrivateKey, commitTxMiscData?: string, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean, customChangeScript?: any): Promise<this>;
22
- settle(toPrivKey: PrivateKey, settleTxMiscData?: string, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean, customChangeScript?: any): Promise<this>;
23
- transfer(toPrivKey: PrivateKey, commitTxMiscData?: string, settleTxMiscData?: string, skipSettle?: boolean, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean, customChangeScript?: any): Promise<this>;
26
+ createTransferOutputs: (toPrivKey: Recipient, isCommitTx?: boolean, forceNoChange?: boolean, customChangeScript?: any) => TransactionOutput[];
27
+ commit(toPrivKey: Recipient, commitTxMiscData?: string, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean, customChangeScript?: any): Promise<this>;
28
+ settle(toPrivKey: Recipient, settleTxMiscData?: string, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean, customChangeScript?: any): Promise<this>;
29
+ transfer(toPrivKey: Recipient, commitTxMiscData?: string, settleTxMiscData?: string, skipSettle?: boolean, forceNoChange?: boolean, fundOverride?: TransactionInput | undefined, forceNoFund?: boolean, customChangeScript?: any): Promise<this>;
24
30
  private balanceToBigInt;
25
31
  private bigIntToBalance;
26
32
  private addBalances;
@@ -28,16 +34,16 @@ export declare class SimpleMultiBOLT extends BOLT {
28
34
  private findProofVout;
29
35
  private voutLE;
30
36
  private signAndClean;
31
- private getParentOutpoint;
32
- merge(other: SimpleMultiBOLT, toKey: PrivateKey, fundingSource?: {
37
+ protected getParentOutpoint(): number[];
38
+ merge(other: SimpleMultiBOLT, toKey: Recipient, fundingSource?: {
33
39
  tx: Transaction;
34
40
  vout: number;
35
- key: PrivateKey;
41
+ key?: PrivateKey | Signer;
36
42
  }): Promise<SimpleMultiBOLT>;
37
- split(toKeyA: PrivateKey, toKeyB: PrivateKey, splitBalanceCommit: number[], fundingSource?: {
43
+ split(toKeyA: Recipient, toKeyB: Recipient, splitBalanceCommit: number[], fundingSource?: {
38
44
  tx: Transaction;
39
45
  vout: number;
40
- key: PrivateKey;
46
+ key?: PrivateKey | Signer;
41
47
  }): Promise<[SimpleMultiBOLT, SimpleMultiBOLT]>;
42
48
  melt(meltPubKeyHash?: number[]): Promise<this>;
43
49
  private verifyAndLogTransaction;