b017 0.0.0-b

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 (32) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE.txt +56 -0
  3. package/README.md +206 -0
  4. package/dist/index.d.ts +13 -0
  5. package/dist/index.js +15 -0
  6. package/dist/lib/boltLib.d.ts +49 -0
  7. package/dist/lib/boltLib.js +177 -0
  8. package/dist/lib/multi/multiBoltLib.d.ts +4 -0
  9. package/dist/lib/multi/multiBoltLib.js +473 -0
  10. package/dist/lib/scanner/fingerprints.d.ts +25 -0
  11. package/dist/lib/scanner/fingerprints.js +95 -0
  12. package/dist/lib/scanner/verifyEvents.d.ts +37 -0
  13. package/dist/lib/scanner/verifyEvents.js +294 -0
  14. package/dist/lib/single/singleAncestor.d.ts +8 -0
  15. package/dist/lib/single/singleAncestor.js +72 -0
  16. package/dist/lib/single/singleSpend.d.ts +31 -0
  17. package/dist/lib/single/singleSpend.js +110 -0
  18. package/dist/tokens/BOLT.d.ts +17 -0
  19. package/dist/tokens/BOLT.js +9 -0
  20. package/dist/tokens/MultiBOLT.d.ts +44 -0
  21. package/dist/tokens/MultiBOLT.js +465 -0
  22. package/dist/tokens/templates/MinSimple.sx.template.d.ts +23 -0
  23. package/dist/tokens/templates/MinSimple.sx.template.js +57 -0
  24. package/dist/tokens/templates/MinSimpleBalance.sx.template.d.ts +24 -0
  25. package/dist/tokens/templates/MinSimpleBalance.sx.template.js +59 -0
  26. package/dist/tokens/templates/MinSimpleDiscount.sx.template.d.ts +23 -0
  27. package/dist/tokens/templates/MinSimpleDiscount.sx.template.js +58 -0
  28. package/dist/tokens/templates/SimpleMulti.sx.template.d.ts +19 -0
  29. package/dist/tokens/templates/SimpleMulti.sx.template.js +214 -0
  30. package/dist/tokens/templates/pay2Proof.d.ts +8 -0
  31. package/dist/tokens/templates/pay2Proof.js +59 -0
  32. package/package.json +58 -0
@@ -0,0 +1,473 @@
1
+ // multiBoltLib.ts
2
+ // SimpleMultiBolt-specific ancestor reconstruction + CTX helpers.
3
+ //
4
+ // SimpleMultiBolt is the optimised fungible contract: 16-byte balance, mandatory change/funding.
5
+ // - 11 lock data chunks.
6
+ // - 198 unlock args; 89 ancestor pieces per ancestor.
7
+ //
8
+ // Layout constants:
9
+ // - ctxHeader sits at unlock chunk index 192 -> ARGS2CTX_SMB = 192
10
+ // - lock data-push count -> PUSHDATAS_SMB = 11
11
+ // - live section start in a token unlock script -> SKIP_SMB = 178
12
+ // - txoType lock chunk index -> TXOTYPE_IDX_SMB = 6
13
+ //
14
+ // Self-contained: depends only on @bsv/sdk (no sx tooling at runtime).
15
+ import { Script, Utils } from "@bsv/sdk";
16
+ import { txVersion, txLockTime, spentOutpoint, vinChunk, vinSequence, vinScript, voutChunk, outputValue, outputScript, scriptChunksFromBin, } from "../boltLib.js";
17
+ const { Reader, Writer } = Utils;
18
+ const ARGS2CTX_SMB = 192;
19
+ const PUSHDATAS_SMB = 11;
20
+ const SKIP_SMB = 178;
21
+ const TXOTYPE_IDX_SMB = 6;
22
+ // ---- CTX reconstruction from a prior token tx's vin unlocking script ----
23
+ // ctxHeader is at ARGS2CTX_SMB; ctxCodeLockScriptCode/footer/lockLen at +3/+4/+5.
24
+ // piece 0 = ctxHeader (104 bytes); piece 2 = ctxFooter (the bytes after the scriptCode).
25
+ const getVinCTXPieceSMB = (tx, vinIndex, piece) => {
26
+ const w = new Utils.Writer();
27
+ const inputScript = tx.inputs[vinIndex]?.unlockingScript;
28
+ if (!inputScript?.chunks?.[ARGS2CTX_SMB])
29
+ return [];
30
+ const ctxHeader = inputScript.chunks[ARGS2CTX_SMB].data;
31
+ const ctxCodeLockScriptCode = inputScript.chunks[ARGS2CTX_SMB + 3]?.data;
32
+ const ctxFooter = inputScript.chunks[ARGS2CTX_SMB + 4]?.data;
33
+ const ctxCodeLockLen = inputScript.chunks[ARGS2CTX_SMB + 5]?.data;
34
+ const ctx = (ctxHeader || [])
35
+ .concat(ctxCodeLockLen || [])
36
+ .concat(ctxCodeLockScriptCode || [])
37
+ .concat(ctxFooter || []);
38
+ const tmpScript = new Script();
39
+ tmpScript.writeBin(ctx);
40
+ const scriptBin = tmpScript.toBinary();
41
+ const headerLen = scriptBin.length - ctx.length;
42
+ const scriptCodeStart = 104 + headerLen;
43
+ const reader = new Utils.Reader(scriptBin.slice(scriptCodeStart));
44
+ const scriptCodeLen = reader.readVarIntNum();
45
+ if (piece === 0)
46
+ w.write(ctx.slice(0, 104));
47
+ else
48
+ w.write(scriptBin.slice(scriptCodeStart + scriptCodeLen + reader.pos)); // piece 2: footer
49
+ return w.toArray();
50
+ };
51
+ // Extract a single committed lock-data arg out of the reconstructed CTX scriptCode.
52
+ // argIdx indexes the 11 SMB lock data chunks (balance=0 ... issuerPubKey=10).
53
+ const getVinCTXDataArgSMB = (tx, vinIndex, argIdx) => {
54
+ const inputScript = tx.inputs[vinIndex]?.unlockingScript;
55
+ if (!inputScript?.chunks?.[ARGS2CTX_SMB])
56
+ return [];
57
+ const ctxHeader = inputScript.chunks[ARGS2CTX_SMB].data;
58
+ const ctxCodeLockScriptCode = inputScript.chunks[ARGS2CTX_SMB + 3]?.data;
59
+ const ctxFooter = inputScript.chunks[ARGS2CTX_SMB + 4]?.data;
60
+ const ctxCodeLockLen = inputScript.chunks[ARGS2CTX_SMB + 5]?.data;
61
+ const ctx = (ctxHeader || [])
62
+ .concat(ctxCodeLockLen || [])
63
+ .concat(ctxCodeLockScriptCode || [])
64
+ .concat(ctxFooter || []);
65
+ const tmpScript = new Script();
66
+ tmpScript.writeBin(ctx);
67
+ const scriptBin = tmpScript.toBinary();
68
+ const headerLen = scriptBin.length - ctx.length;
69
+ const scriptCodeStart = 104 + headerLen;
70
+ const scriptCodeBuf = scriptBin.slice(scriptCodeStart);
71
+ const reader = new Utils.Reader(scriptCodeBuf);
72
+ const scriptCodeLen = reader.readVarIntNum();
73
+ const scriptCode = Script.fromBinary(scriptCodeBuf.slice(reader.pos, reader.pos + scriptCodeLen));
74
+ const chunk = scriptCode.chunks[argIdx];
75
+ if (chunk && chunk.op === 0)
76
+ return [];
77
+ return chunk?.data ?? [];
78
+ };
79
+ // SimpleMultiBolt action bytes (no swap): 20 transferSettle, 21 transferCommit,
80
+ // 22 splitSettle, 23 splitCommit, 24 mergeSettle, 25 mergeCommit.
81
+ const determineTxTypeSMB = (tx) => {
82
+ const firstToken = tx.outputs[0];
83
+ const txoType = firstToken.lockingScript.chunks[TXOTYPE_IDX_SMB];
84
+ const typeByte = txoType?.data?.[0] || -1;
85
+ return typeByte.toString(16);
86
+ };
87
+ // The 89 SimpleMultiBolt ancestor piece names per ancestor.
88
+ export const PIECE_NAMES = [
89
+ "Version",
90
+ "Vin1Outpoint",
91
+ "Vin1GrandparentProofVoutIdx", "Vin1InteropProofVoutIdx",
92
+ "Vin1InteropPubKeyHash", "Vin1InteropOutpoint", "Vin1InteropParentOutpoint",
93
+ "Vin1FundOutpoint", "Vin1ChangeOutput",
94
+ "Vin1PubKeyHash1", "Vin1PubKeyHash2", "Vin1NextBalanceCommit", "Vin1NextTxoType", "Vin1InputIndexN",
95
+ "Vin1Sig", "Vin1PubKey",
96
+ "Vin1CTXHeader", "Vin1CTXBalance", "Vin1CTXBalanceCommit",
97
+ "Vin1CTXPubKeyHash", "Vin1CTXPubKeyHashCommit", "Vin1CTXPubKeyHashCommit2",
98
+ "Vin1CTXOtherGrandparentOutpoint",
99
+ "Vin1CTXTxoType", "Vin1CTXOutputIndexN",
100
+ "Vin1CTXParentOutpoint", "Vin1CTXGrandparentOutpoint", "Vin1CTXIssuerPubKey",
101
+ "Vin1CTXFooter", "Vin1NSequence",
102
+ "Vin2Outpoint",
103
+ "Vin2GrandparentProofVoutIdx", "Vin2InteropProofVoutIdx",
104
+ "Vin2InteropPubKeyHash", "Vin2InteropOutpoint", "Vin2InteropParentOutpoint",
105
+ "Vin2FundOutpoint", "Vin2ChangeOutput",
106
+ "Vin2PubKeyHash1", "Vin2PubKeyHash2", "Vin2NextBalanceCommit", "Vin2NextTxoType", "Vin2InputIndexN",
107
+ "Vin2Sig", "Vin2PubKey",
108
+ "Vin2CTXHeader", "Vin2CTXBalance", "Vin2CTXBalanceCommit",
109
+ "Vin2CTXPubKeyHash", "Vin2CTXPubKeyHashCommit", "Vin2CTXPubKeyHashCommit2",
110
+ "Vin2CTXOtherGrandparentOutpoint",
111
+ "Vin2CTXTxoType", "Vin2CTXOutputIndexN",
112
+ "Vin2CTXParentOutpoint", "Vin2CTXGrandparentOutpoint", "Vin2CTXIssuerPubKey",
113
+ "Vin2CTXFooter", "Vin2NSequence",
114
+ "VinFundOutpoint", "VinFundScript", "VinFundNSequence",
115
+ "Vout1Balance", "Vout1BalanceCommit",
116
+ "Vout1PubKeyHash", "Vout1PubKeyHashCommit", "Vout1PubKeyHashCommit2",
117
+ "Vout1OtherGrandparentOutpoint",
118
+ "Vout1TxoType", "Vout1OutputIndexN",
119
+ "Vout1ParentOutpoint", "Vout1GrandparentOutpoint", "Vout1IssuerPubKey",
120
+ "Vout2Balance", "Vout2BalanceCommit",
121
+ "Vout2PubKeyHash", "Vout2PubKeyHashCommit", "Vout2PubKeyHashCommit2",
122
+ "Vout2OtherGrandparentOutpoint",
123
+ "Vout2TxoType", "Vout2OutputIndexN",
124
+ "Vout2ParentOutpoint", "Vout2GrandparentOutpoint", "Vout2IssuerPubKey",
125
+ "ProofPubKeyHash1", "ProofPubKeyHash2",
126
+ "ChangeValue", "ChangeScript", "NLockTime",
127
+ ];
128
+ // Extract one named ancestor piece from a SimpleMultiBolt transaction.
129
+ export const ancestorPiece = (name, tx) => {
130
+ let res = [];
131
+ const ancestorTx = tx;
132
+ const txType = determineTxTypeSMB(ancestorTx);
133
+ const hasTwoTokenInputs = txType === "25"; // mergeCommit (swap removed)
134
+ const hasTwoBolts = txType === "23"; // splitCommit
135
+ const fundVinIdx = ancestorTx.inputs.length - 1;
136
+ const changeVoutIdx = ancestorTx.outputs.length - 1;
137
+ const lastOutput = ancestorTx.outputs[changeVoutIdx];
138
+ const hasChange = lastOutput?.lockingScript?.chunks?.length === 5;
139
+ const hasFunding = ancestorTx.inputs.length > (hasTwoTokenInputs ? 2 : 1);
140
+ switch (name) {
141
+ case "Version":
142
+ res = txVersion(ancestorTx);
143
+ break;
144
+ // ---- Vin1 (token, always present) ----
145
+ case "Vin1Outpoint":
146
+ res = spentOutpoint(ancestorTx, 0);
147
+ break;
148
+ case "Vin1GrandparentProofVoutIdx":
149
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 0);
150
+ break;
151
+ case "Vin1InteropProofVoutIdx":
152
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 1);
153
+ break;
154
+ case "Vin1InteropPubKeyHash":
155
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 2);
156
+ break;
157
+ case "Vin1InteropOutpoint":
158
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 3);
159
+ break;
160
+ case "Vin1InteropParentOutpoint":
161
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 4);
162
+ break;
163
+ case "Vin1FundOutpoint":
164
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 5);
165
+ break;
166
+ case "Vin1ChangeOutput":
167
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 6);
168
+ break;
169
+ case "Vin1PubKeyHash1":
170
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 7);
171
+ break;
172
+ case "Vin1PubKeyHash2":
173
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 8);
174
+ break;
175
+ case "Vin1NextBalanceCommit":
176
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 9);
177
+ break;
178
+ case "Vin1NextTxoType":
179
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 10);
180
+ break;
181
+ case "Vin1InputIndexN":
182
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 11);
183
+ break;
184
+ case "Vin1Sig":
185
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 12);
186
+ break;
187
+ case "Vin1PubKey":
188
+ res = vinChunk(ancestorTx, 0, SKIP_SMB + 13);
189
+ break;
190
+ case "Vin1CTXHeader":
191
+ res = getVinCTXPieceSMB(ancestorTx, 0, 0);
192
+ break;
193
+ case "Vin1CTXBalance":
194
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 0);
195
+ break;
196
+ case "Vin1CTXBalanceCommit":
197
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 1);
198
+ break;
199
+ case "Vin1CTXPubKeyHash":
200
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 2);
201
+ break;
202
+ case "Vin1CTXPubKeyHashCommit":
203
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 3);
204
+ break;
205
+ case "Vin1CTXPubKeyHashCommit2":
206
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 4);
207
+ break;
208
+ case "Vin1CTXOtherGrandparentOutpoint":
209
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 5);
210
+ break;
211
+ case "Vin1CTXTxoType":
212
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 6);
213
+ break;
214
+ case "Vin1CTXOutputIndexN":
215
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 7);
216
+ break;
217
+ case "Vin1CTXParentOutpoint":
218
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 8);
219
+ break;
220
+ case "Vin1CTXGrandparentOutpoint":
221
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 9);
222
+ break;
223
+ case "Vin1CTXIssuerPubKey":
224
+ res = getVinCTXDataArgSMB(ancestorTx, 0, 10);
225
+ break;
226
+ case "Vin1CTXFooter":
227
+ res = getVinCTXPieceSMB(ancestorTx, 0, 2);
228
+ break;
229
+ case "Vin1NSequence":
230
+ res = vinSequence(ancestorTx, 0);
231
+ break;
232
+ // ---- Vin2 (token, only mergeCommit) ----
233
+ case "Vin2Outpoint":
234
+ if (hasTwoTokenInputs)
235
+ res = spentOutpoint(ancestorTx, 1);
236
+ break;
237
+ case "Vin2GrandparentProofVoutIdx":
238
+ if (hasTwoTokenInputs)
239
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 0);
240
+ break;
241
+ case "Vin2InteropProofVoutIdx":
242
+ if (hasTwoTokenInputs)
243
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 1);
244
+ break;
245
+ case "Vin2InteropPubKeyHash":
246
+ if (hasTwoTokenInputs)
247
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 2);
248
+ break;
249
+ case "Vin2InteropOutpoint":
250
+ if (hasTwoTokenInputs)
251
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 3);
252
+ break;
253
+ case "Vin2InteropParentOutpoint":
254
+ if (hasTwoTokenInputs)
255
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 4);
256
+ break;
257
+ case "Vin2FundOutpoint":
258
+ if (hasTwoTokenInputs)
259
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 5);
260
+ break;
261
+ case "Vin2ChangeOutput":
262
+ if (hasTwoTokenInputs)
263
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 6);
264
+ break;
265
+ case "Vin2PubKeyHash1":
266
+ if (hasTwoTokenInputs)
267
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 7);
268
+ break;
269
+ case "Vin2PubKeyHash2":
270
+ if (hasTwoTokenInputs)
271
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 8);
272
+ break;
273
+ case "Vin2NextBalanceCommit":
274
+ if (hasTwoTokenInputs)
275
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 9);
276
+ break;
277
+ case "Vin2NextTxoType":
278
+ if (hasTwoTokenInputs)
279
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 10);
280
+ break;
281
+ case "Vin2InputIndexN":
282
+ if (hasTwoTokenInputs)
283
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 11);
284
+ break;
285
+ case "Vin2Sig":
286
+ if (hasTwoTokenInputs)
287
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 12);
288
+ break;
289
+ case "Vin2PubKey":
290
+ if (hasTwoTokenInputs)
291
+ res = vinChunk(ancestorTx, 1, SKIP_SMB + 13);
292
+ break;
293
+ case "Vin2CTXHeader":
294
+ if (hasTwoTokenInputs)
295
+ res = getVinCTXPieceSMB(ancestorTx, 1, 0);
296
+ break;
297
+ case "Vin2CTXBalance":
298
+ if (hasTwoTokenInputs)
299
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 0);
300
+ break;
301
+ case "Vin2CTXBalanceCommit":
302
+ if (hasTwoTokenInputs)
303
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 1);
304
+ break;
305
+ case "Vin2CTXPubKeyHash":
306
+ if (hasTwoTokenInputs)
307
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 2);
308
+ break;
309
+ case "Vin2CTXPubKeyHashCommit":
310
+ if (hasTwoTokenInputs)
311
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 3);
312
+ break;
313
+ case "Vin2CTXPubKeyHashCommit2":
314
+ if (hasTwoTokenInputs)
315
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 4);
316
+ break;
317
+ case "Vin2CTXOtherGrandparentOutpoint":
318
+ if (hasTwoTokenInputs)
319
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 5);
320
+ break;
321
+ case "Vin2CTXTxoType":
322
+ if (hasTwoTokenInputs)
323
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 6);
324
+ break;
325
+ case "Vin2CTXOutputIndexN":
326
+ if (hasTwoTokenInputs)
327
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 7);
328
+ break;
329
+ case "Vin2CTXParentOutpoint":
330
+ if (hasTwoTokenInputs)
331
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 8);
332
+ break;
333
+ case "Vin2CTXGrandparentOutpoint":
334
+ if (hasTwoTokenInputs)
335
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 9);
336
+ break;
337
+ case "Vin2CTXIssuerPubKey":
338
+ if (hasTwoTokenInputs)
339
+ res = getVinCTXDataArgSMB(ancestorTx, 1, 10);
340
+ break;
341
+ case "Vin2CTXFooter":
342
+ if (hasTwoTokenInputs)
343
+ res = getVinCTXPieceSMB(ancestorTx, 1, 2);
344
+ break;
345
+ case "Vin2NSequence":
346
+ if (hasTwoTokenInputs)
347
+ res = vinSequence(ancestorTx, 1);
348
+ break;
349
+ // ---- Funding vin (always last input) ----
350
+ case "VinFundOutpoint":
351
+ if (hasFunding)
352
+ res = spentOutpoint(ancestorTx, fundVinIdx);
353
+ break;
354
+ case "VinFundScript":
355
+ if (hasFunding)
356
+ res = vinScript(ancestorTx, fundVinIdx);
357
+ break;
358
+ case "VinFundNSequence":
359
+ if (hasFunding)
360
+ res = vinSequence(ancestorTx, fundVinIdx);
361
+ break;
362
+ // ---- Vout1 (first token output, always present) ----
363
+ case "Vout1Balance":
364
+ res = voutChunk(ancestorTx, 0, 0);
365
+ break;
366
+ case "Vout1BalanceCommit":
367
+ res = voutChunk(ancestorTx, 0, 1);
368
+ break;
369
+ case "Vout1PubKeyHash":
370
+ res = voutChunk(ancestorTx, 0, 2);
371
+ break;
372
+ case "Vout1PubKeyHashCommit":
373
+ res = voutChunk(ancestorTx, 0, 3);
374
+ break;
375
+ case "Vout1PubKeyHashCommit2":
376
+ res = voutChunk(ancestorTx, 0, 4);
377
+ break;
378
+ case "Vout1OtherGrandparentOutpoint":
379
+ res = voutChunk(ancestorTx, 0, 5);
380
+ break;
381
+ case "Vout1TxoType":
382
+ res = voutChunk(ancestorTx, 0, 6);
383
+ break;
384
+ case "Vout1OutputIndexN":
385
+ res = voutChunk(ancestorTx, 0, 7);
386
+ break;
387
+ case "Vout1ParentOutpoint":
388
+ res = voutChunk(ancestorTx, 0, 8);
389
+ break;
390
+ case "Vout1GrandparentOutpoint":
391
+ res = voutChunk(ancestorTx, 0, 9);
392
+ break;
393
+ case "Vout1IssuerPubKey":
394
+ res = voutChunk(ancestorTx, 0, 10);
395
+ break;
396
+ // ---- Vout2 (second token output, only splitSettle) ----
397
+ case "Vout2Balance":
398
+ if (hasTwoBolts)
399
+ res = voutChunk(ancestorTx, 1, 0);
400
+ break;
401
+ case "Vout2BalanceCommit":
402
+ if (hasTwoBolts)
403
+ res = voutChunk(ancestorTx, 1, 1);
404
+ break;
405
+ case "Vout2PubKeyHash":
406
+ if (hasTwoBolts)
407
+ res = voutChunk(ancestorTx, 1, 2);
408
+ break;
409
+ case "Vout2PubKeyHashCommit":
410
+ if (hasTwoBolts)
411
+ res = voutChunk(ancestorTx, 1, 3);
412
+ break;
413
+ case "Vout2PubKeyHashCommit2":
414
+ if (hasTwoBolts)
415
+ res = voutChunk(ancestorTx, 1, 4);
416
+ break;
417
+ case "Vout2OtherGrandparentOutpoint":
418
+ if (hasTwoBolts)
419
+ res = voutChunk(ancestorTx, 1, 5);
420
+ break;
421
+ case "Vout2TxoType":
422
+ if (hasTwoBolts)
423
+ res = voutChunk(ancestorTx, 1, 6);
424
+ break;
425
+ case "Vout2OutputIndexN":
426
+ if (hasTwoBolts)
427
+ res = voutChunk(ancestorTx, 1, 7);
428
+ break;
429
+ case "Vout2ParentOutpoint":
430
+ if (hasTwoBolts)
431
+ res = voutChunk(ancestorTx, 1, 8);
432
+ break;
433
+ case "Vout2GrandparentOutpoint":
434
+ if (hasTwoBolts)
435
+ res = voutChunk(ancestorTx, 1, 9);
436
+ break;
437
+ case "Vout2IssuerPubKey":
438
+ if (hasTwoBolts)
439
+ res = voutChunk(ancestorTx, 1, 10);
440
+ break;
441
+ // ---- Bolt (pay2Bolt) outputs ----
442
+ case "ProofPubKeyHash1":
443
+ // 1 token output always -> bolt1 at vout 1
444
+ res = voutChunk(ancestorTx, 1, 4);
445
+ break;
446
+ case "ProofPubKeyHash2":
447
+ // splitCommit emits a 2nd bolt commitment at vout 2
448
+ if (hasTwoBolts)
449
+ res = voutChunk(ancestorTx, 2, 4);
450
+ break;
451
+ // ---- Change + locktime ----
452
+ case "ChangeValue":
453
+ if (hasChange)
454
+ res = outputValue(ancestorTx, changeVoutIdx);
455
+ break;
456
+ case "ChangeScript":
457
+ if (hasChange)
458
+ res = outputScript(ancestorTx, changeVoutIdx);
459
+ break;
460
+ case "NLockTime":
461
+ res = txLockTime(ancestorTx);
462
+ break;
463
+ }
464
+ return res;
465
+ };
466
+ // 89 ancestorA + 89 ancestorB = 178 empty chunks (no leading miscData) for melt.
467
+ export const createEmptyFungibleAncestorChunksSMB = () => {
468
+ const emptyChunks = [];
469
+ for (let i = 0; i < SKIP_SMB; i++) {
470
+ emptyChunks.push(...scriptChunksFromBin([]));
471
+ }
472
+ return emptyChunks;
473
+ };
@@ -0,0 +1,25 @@
1
+ import { Script } from "@bsv/sdk";
2
+ export type TokenType = "MinSimpleBOLT" | "MinSimpleDiscountBOLT" | "MinSimpleBalanceBOLT" | "SimpleMultiBOLT";
3
+ export interface TypeSpec {
4
+ type: TokenType;
5
+ dataPushCount: number;
6
+ pushLengths: number[];
7
+ suffixHashHex: string;
8
+ }
9
+ export declare const sha256Hex: (bin: number[]) => string;
10
+ export declare const REGISTRY: Record<TokenType, TypeSpec>;
11
+ /**
12
+ * Recognise a locking script as a genuine BOLT token of a known type (or null).
13
+ * Requires BOTH the leading-push layout AND the static-code hash to match a registered type —
14
+ * so a tampered contract body or a non-token script is rejected.
15
+ */
16
+ export declare function recognizeType(lock: Script | null | undefined, expected?: TokenType): TokenType | null;
17
+ /** The issuerPubKey is always the last dynamic data push of a recognised token. */
18
+ export declare function issuerPubKeyOf(lock: Script, type: TokenType): number[];
19
+ /**
20
+ * Recognise a locking script as a genuine p2Proof (the b017 marker proof) output, golden-strict:
21
+ * the static skeleton hash (incl. the b017 marker) must match Pay2ProofTemplate AND the single
22
+ * dynamic field must be a 20-byte pkh. Used to fingerprint commit proof outputs and settle proof
23
+ * inputs. Returns false for anything else (token, plain p2pkh, tampered marker/opcodes).
24
+ */
25
+ export declare function recognizeP2P(lock: Script | null | undefined): boolean;
@@ -0,0 +1,95 @@
1
+ // fingerprints.ts — per-type recognition primitive for the BOLT token scanner.
2
+ // A genuine BOLT token's locking script = [N leading dynamic data pushes] + [static contract code].
3
+ // A type's fingerprint is { pushLengths (the data-push byte layout) , suffixHash = sha256(static code) }.
4
+ // issuerPubKey is always the LAST dynamic push (33 bytes); it identifies the issuer, not the type.
5
+ //
6
+ // The same golden-hash idea also fingerprints the p2Proof output (the b017 marker proof carried by
7
+ // commit txs and consumed by settle txs): its only dynamic field is the 20-byte beneficiary pkh, so
8
+ // recognizeP2P hashes the static skeleton (incl. the b017 marker bytes) and checks the pkh length.
9
+ import { Hash, Script, Utils } from "@bsv/sdk";
10
+ import MinSimpleTemplate from "../../tokens/templates/MinSimple.sx.template.js";
11
+ import MinSimpleDiscountTemplate from "../../tokens/templates/MinSimpleDiscount.sx.template.js";
12
+ import MinSimpleBalanceTemplate from "../../tokens/templates/MinSimpleBalance.sx.template.js";
13
+ import SimpleMultiTemplate from "../../tokens/templates/SimpleMulti.sx.template.js";
14
+ import Pay2ProofTemplate from "../../tokens/templates/pay2Proof.js";
15
+ // Leading dynamic-push byte lengths per type (issuerPubKey is always the last, 33 bytes).
16
+ const LAYOUTS = Object.freeze({
17
+ MinSimpleBOLT: [20, 20, 1, 36, 36, 33],
18
+ MinSimpleDiscountBOLT: [1, 20, 20, 1, 36, 36, 33],
19
+ MinSimpleBalanceBOLT: [16, 20, 20, 1, 36, 36, 33],
20
+ SimpleMultiBOLT: [16, 16, 20, 20, 20, 36, 1, 1, 36, 36, 33],
21
+ });
22
+ const SUFFIX = {
23
+ MinSimpleBOLT: new MinSimpleTemplate().staticSuffix(),
24
+ MinSimpleDiscountBOLT: new MinSimpleDiscountTemplate().staticSuffix(),
25
+ MinSimpleBalanceBOLT: new MinSimpleBalanceTemplate().staticSuffix(),
26
+ SimpleMultiBOLT: new SimpleMultiTemplate().staticSuffix(),
27
+ };
28
+ export const sha256Hex = (bin) => Utils.toHex(Hash.sha256(bin));
29
+ export const REGISTRY = Object.freeze(Object.fromEntries(Object.keys(LAYOUTS).map((type) => [
30
+ type,
31
+ Object.freeze({
32
+ type,
33
+ dataPushCount: LAYOUTS[type].length,
34
+ pushLengths: Object.freeze([...LAYOUTS[type]]),
35
+ suffixHashHex: sha256Hex(SUFFIX[type].toBinary()),
36
+ }),
37
+ ])));
38
+ /**
39
+ * Recognise a locking script as a genuine BOLT token of a known type (or null).
40
+ * Requires BOTH the leading-push layout AND the static-code hash to match a registered type —
41
+ * so a tampered contract body or a non-token script is rejected.
42
+ */
43
+ export function recognizeType(lock, expected) {
44
+ if (!lock || !Array.isArray(lock.chunks))
45
+ return null;
46
+ for (const spec of Object.values(REGISTRY)) {
47
+ if (expected && spec.type !== expected)
48
+ continue;
49
+ const n = spec.dataPushCount;
50
+ if (lock.chunks.length <= n)
51
+ continue;
52
+ const lens = lock.chunks.slice(0, n).map((c) => c.data?.length ?? 0);
53
+ if (!lens.every((l, i) => l === spec.pushLengths[i]))
54
+ continue;
55
+ const suffix = new Script(lock.chunks.slice(n));
56
+ if (sha256Hex(suffix.toBinary()) !== spec.suffixHashHex)
57
+ continue;
58
+ return spec.type;
59
+ }
60
+ return null;
61
+ }
62
+ /** The issuerPubKey is always the last dynamic data push of a recognised token. */
63
+ export function issuerPubKeyOf(lock, type) {
64
+ return lock.chunks[REGISTRY[type].dataPushCount - 1]?.data ?? [];
65
+ }
66
+ // ---- p2Proof golden fingerprint --------------------------------------------------------------
67
+ // The b017 proof output has exactly ONE dynamic field — the 20-byte beneficiary pkh at index
68
+ // P2P_PKH_IDX. Everything else (the b017 marker bytes + the surrounding opcodes) is static, so a
69
+ // genuine p2Proof is fingerprinted by sha256(static skeleton) + the pkh push length, derived from
70
+ // Pay2ProofTemplate so there is a single source of truth.
71
+ const P2P_PKH_IDX = 4;
72
+ const P2P_REF = new Pay2ProofTemplate().lock(new Array(20).fill(0));
73
+ const P2P_LEN = P2P_REF.chunks.length;
74
+ /** Serialise a script with its single dynamic push zeroed so only static structure is hashed. */
75
+ function p2pSkeleton(lock) {
76
+ const chunks = lock.chunks.map((c, i) =>
77
+ /* v8 ignore next -- recognizeP2P guarantees chunk[P2P_PKH_IDX] carries 20 bytes before this runs */
78
+ i === P2P_PKH_IDX ? { op: c.op, data: new Array(c.data?.length ?? 0).fill(0) } : c);
79
+ return new Script(chunks).toBinary();
80
+ }
81
+ const P2P_SKELETON_HASH = sha256Hex(p2pSkeleton(P2P_REF));
82
+ /**
83
+ * Recognise a locking script as a genuine p2Proof (the b017 marker proof) output, golden-strict:
84
+ * the static skeleton hash (incl. the b017 marker) must match Pay2ProofTemplate AND the single
85
+ * dynamic field must be a 20-byte pkh. Used to fingerprint commit proof outputs and settle proof
86
+ * inputs. Returns false for anything else (token, plain p2pkh, tampered marker/opcodes).
87
+ */
88
+ export function recognizeP2P(lock) {
89
+ if (!lock || !Array.isArray(lock.chunks) || lock.chunks.length !== P2P_LEN)
90
+ return false;
91
+ const pkh = lock.chunks[P2P_PKH_IDX]?.data;
92
+ if (!pkh || pkh.length !== 20)
93
+ return false;
94
+ return sha256Hex(p2pSkeleton(lock)) === P2P_SKELETON_HASH;
95
+ }
@@ -0,0 +1,37 @@
1
+ import { Transaction } from "@bsv/sdk";
2
+ import { type TokenType } from "./fingerprints.js";
3
+ export interface ScanOpts {
4
+ expectedType?: TokenType;
5
+ trustedIssuerPubKey?: number[] | string;
6
+ }
7
+ export type EventKind = "mint" | "transfer" | "split" | "merge" | "melt";
8
+ export interface ScanResult {
9
+ ok: boolean;
10
+ reason?: string;
11
+ type?: TokenType;
12
+ issuerPubKeyHex?: string;
13
+ events?: {
14
+ kind: EventKind;
15
+ txids: string[];
16
+ }[];
17
+ }
18
+ export interface EventResult {
19
+ ok: boolean;
20
+ reason?: string;
21
+ type?: TokenType;
22
+ kind?: EventKind;
23
+ }
24
+ /**
25
+ * Verify ONE token event — a mint, a commit->settle pair, or a melt. Categorises each tx by its
26
+ * token's txoType action, fingerprints EVERY interface against the action's golden shape, and (for
27
+ * a commit+settle pair) checks the settle links back to the commit via parentOutpoint.
28
+ */
29
+ export declare function verifyEvent(eventTxs: (Transaction | string)[], opts?: ScanOpts): EventResult;
30
+ /**
31
+ * Verify a BATCH of transactional events end to end. The multistep inspection: recognise the type,
32
+ * pin the issuer across every token output in the batch, fingerprint every tx's interface
33
+ * arrangement, then pair the events — every commit (txoType 21/23/25) must be matched by a settle
34
+ * that links back via parentOutpoint, and every settle must link to a commit in the batch. A lone
35
+ * mint (genesis) or melt (terminal) is a valid single-tx event.
36
+ */
37
+ export declare function verifyEvents(txsIn: (Transaction | string)[], opts?: ScanOpts): ScanResult;