@bsv/sdk 1.1.24 → 1.1.25

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 (47) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/totp/totp.js +1 -1
  3. package/dist/cjs/src/totp/totp.js.map +1 -1
  4. package/dist/cjs/src/transaction/Beef.js +492 -0
  5. package/dist/cjs/src/transaction/Beef.js.map +1 -0
  6. package/dist/cjs/src/transaction/BeefParty.js +97 -0
  7. package/dist/cjs/src/transaction/BeefParty.js.map +1 -0
  8. package/dist/cjs/src/transaction/BeefTx.js +123 -0
  9. package/dist/cjs/src/transaction/BeefTx.js.map +1 -0
  10. package/dist/cjs/src/transaction/Transaction.js +2 -1
  11. package/dist/cjs/src/transaction/Transaction.js.map +1 -1
  12. package/dist/cjs/src/transaction/index.js +7 -1
  13. package/dist/cjs/src/transaction/index.js.map +1 -1
  14. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  15. package/dist/esm/src/totp/totp.js +1 -1
  16. package/dist/esm/src/totp/totp.js.map +1 -1
  17. package/dist/esm/src/transaction/Beef.js +485 -0
  18. package/dist/esm/src/transaction/Beef.js.map +1 -0
  19. package/dist/esm/src/transaction/BeefParty.js +93 -0
  20. package/dist/esm/src/transaction/BeefParty.js.map +1 -0
  21. package/dist/esm/src/transaction/BeefTx.js +121 -0
  22. package/dist/esm/src/transaction/BeefTx.js.map +1 -0
  23. package/dist/esm/src/transaction/Transaction.js +2 -1
  24. package/dist/esm/src/transaction/Transaction.js.map +1 -1
  25. package/dist/esm/src/transaction/index.js +3 -0
  26. package/dist/esm/src/transaction/index.js.map +1 -1
  27. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  28. package/dist/types/src/transaction/Beef.d.ts +143 -0
  29. package/dist/types/src/transaction/Beef.d.ts.map +1 -0
  30. package/dist/types/src/transaction/BeefParty.d.ts +62 -0
  31. package/dist/types/src/transaction/BeefParty.d.ts.map +1 -0
  32. package/dist/types/src/transaction/BeefTx.d.ts +35 -0
  33. package/dist/types/src/transaction/BeefTx.d.ts.map +1 -0
  34. package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
  35. package/dist/types/src/transaction/index.d.ts +3 -0
  36. package/dist/types/src/transaction/index.d.ts.map +1 -1
  37. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  38. package/docs/primitives.md +372 -55
  39. package/docs/transaction.md +466 -0
  40. package/package.json +1 -1
  41. package/src/totp/totp.ts +1 -1
  42. package/src/transaction/Beef.ts +533 -0
  43. package/src/transaction/BeefParty.ts +100 -0
  44. package/src/transaction/BeefTx.ts +121 -0
  45. package/src/transaction/Transaction.ts +2 -1
  46. package/src/transaction/__tests/Beef.test.ts +290 -0
  47. package/src/transaction/index.ts +3 -0
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.1.24",
3
+ "version": "1.1.25",
4
4
  "type": "commonjs",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "files": [
@@ -66,7 +66,7 @@ class TOTP {
66
66
  exports.TOTP = TOTP;
67
67
  function generateHOTP(secret, counter, options) {
68
68
  const timePad = new BigNumber_js_1.default(counter).toArray('be', 8);
69
- console.log({ timePad });
69
+ //console.log({ timePad })
70
70
  const hmac = calcHMAC(secret, timePad, options.algorithm);
71
71
  const signature = hmac.digest();
72
72
  // RFC 4226 https://datatracker.ietf.org/doc/html/rfc4226#section-5.4
@@ -1 +1 @@
1
- {"version":3,"file":"totp.js","sourceRoot":"","sources":["../../../../src/totp/totp.ts"],"names":[],"mappings":";;;;;;AAAA,mDAAwE;AACxE,8EAAkD;AA0BlD,MAAa,IAAI;IACf;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAE,MAAgB,EAAE,OAAqB;QACtD,MAAM,QAAQ,GAAG,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAA;QAEjD,MAAM,OAAO,GAAG,IAAI,CAAC,UA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,MAAM,CAAC,OAAO,CAAC,CAAA;QAC/C,KAAK,SAAS;YACZ,OAAO,IAAI,oBAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QAC/C;YACE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;IACjD,CAAC;AACH,CAAC"}
@@ -0,0 +1,492 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Beef = exports.BEEF_MAGIC_TXID_ONLY_EXTENSION = exports.BEEF_MAGIC_V2 = exports.BEEF_MAGIC = void 0;
7
+ const MerklePath_js_1 = __importDefault(require("./MerklePath.js"));
8
+ const BeefTx_js_1 = __importDefault(require("./BeefTx.js"));
9
+ const utils_js_1 = require("../primitives/utils.js");
10
+ exports.BEEF_MAGIC = 4022206465; // 0100BEEF in LE order
11
+ exports.BEEF_MAGIC_V2 = 4022206466; // 0200BEEF in LE order
12
+ exports.BEEF_MAGIC_TXID_ONLY_EXTENSION = 4022206465; // 0100BEEF in LE order
13
+ /*
14
+ * BEEF standard: BRC-62: Background Evaluation Extended Format (BEEF) Transactions
15
+ * https://github.com/bitcoin-sv/BRCs/blob/master/transactions/0062.md
16
+ *
17
+ * BUMP standard: BRC-74: BSV Unified Merkle Path (BUMP) Format
18
+ * https://github.com/bitcoin-sv/BRCs/blob/master/transactions/0074.md
19
+ *
20
+ * A valid serialized BEEF is the cornerstone of Simplified Payment Validation (SPV)
21
+ * where they are exchanged between two non-trusting parties to establish the
22
+ * validity of a newly constructed bitcoin transaction and its inputs from prior
23
+ * transactions.
24
+ *
25
+ * A `Beef` is fundamentally an list of `BUMP`s and a list of transactions.
26
+ *
27
+ * A `BUMP` is a partial merkle tree for a 'mined' bitcoin block.
28
+ * It can therefore be used to prove the validity of transaction data
29
+ * for each transaction txid whose merkle path is included in the tree.
30
+ *
31
+ * To be valid, the list of transactions must be sorted in dependency order:
32
+ * oldest transaction first;
33
+ * and each transaction must either
34
+ * have a merkle path in one of the BUMPs, or
35
+ * have all of its input transactions included in the list of transactions.
36
+ *
37
+ * The `Beef` class supports the construction of valid BEEFs by allowing BUMPs
38
+ * (merkle paths) and transactions to be merged sequentially.
39
+ *
40
+ * The `Beef` class also extends the standard by supporting 'known' transactions.
41
+ * A 'known' transaction is represented solely by its txid.
42
+ * To become valid, all the 'known' transactions in a `Beef` must be replaced by full
43
+ * transactions and merkle paths, if they are mined.
44
+ *
45
+ * The purpose of supporting 'known' transactions is that one or both parties
46
+ * generating and exchanging BEEFs often possess partial knowledge of valid transactions
47
+ * due to their history.
48
+ *
49
+ * A valid `Beef` is only required when sent to a party with no shared history,
50
+ * such as a transaction processor.
51
+ */
52
+ class Beef {
53
+ constructor() {
54
+ this.bumps = [];
55
+ this.txs = [];
56
+ }
57
+ /**
58
+ * BEEF_MAGIC is the original V1 version.
59
+ * BEEF_MAGIC_V2 includes support for txidOnly transactions in serialized beefs.
60
+ * @returns version based on current contents.
61
+ */
62
+ get version() {
63
+ const hasTxidOnly = !this.txs.every(tx => !tx.isTxidOnly);
64
+ if (hasTxidOnly)
65
+ return exports.BEEF_MAGIC_V2;
66
+ else
67
+ return exports.BEEF_MAGIC;
68
+ }
69
+ /**
70
+ * @param txid of `beefTx` to find
71
+ * @returns `BeefTx` in `txs` with `txid`.
72
+ */
73
+ findTxid(txid) {
74
+ return this.txs.find(tx => tx.txid === txid);
75
+ }
76
+ /**
77
+ * Merge a MerklePath that is assumed to be fully valid.
78
+ * @param bump
79
+ * @returns index of merged bump
80
+ */
81
+ mergeBump(bump) {
82
+ let bumpIndex = undefined;
83
+ // If this proof is identical to another one previously added, we use that first. Otherwise, we try to merge it with proofs from the same block.
84
+ for (let i = 0; i < this.bumps.length; i++) {
85
+ const b = this.bumps[i];
86
+ if (b === bump) { // Literally the same
87
+ return i;
88
+ }
89
+ if (b.blockHeight === bump.blockHeight) {
90
+ // Probably the same...
91
+ const rootA = b.computeRoot();
92
+ const rootB = bump.computeRoot();
93
+ if (rootA === rootB) {
94
+ // Definitely the same... combine them to save space
95
+ b.combine(bump);
96
+ bumpIndex = i;
97
+ break;
98
+ }
99
+ }
100
+ }
101
+ // if the proof is not yet added, add a new path.
102
+ if (bumpIndex === undefined) {
103
+ bumpIndex = this.bumps.length;
104
+ this.bumps.push(bump);
105
+ }
106
+ // review if any transactions are proven by this bump
107
+ const b = this.bumps[bumpIndex];
108
+ for (const tx of this.txs) {
109
+ const txid = tx.txid;
110
+ if (!tx.bumpIndex) {
111
+ for (const n of b.path[0]) {
112
+ if (n.hash === txid) {
113
+ tx.bumpIndex = bumpIndex;
114
+ n.txid = true;
115
+ break;
116
+ }
117
+ }
118
+ }
119
+ }
120
+ return bumpIndex;
121
+ }
122
+ /**
123
+ * Merge a serialized transaction.
124
+ *
125
+ * Checks that a transaction with the same txid hasn't already been merged.
126
+ *
127
+ * Replaces existing transaction with same txid.
128
+ *
129
+ * @param rawTx
130
+ * @param bumpIndex Optional. If a number, must be valid index into bumps array.
131
+ * @returns txid of rawTx
132
+ */
133
+ mergeRawTx(rawTx, bumpIndex) {
134
+ const newTx = new BeefTx_js_1.default(rawTx, bumpIndex);
135
+ this.removeExistingTxid(newTx.txid);
136
+ this.txs.push(newTx);
137
+ this.tryToValidateBumpIndex(newTx);
138
+ return newTx;
139
+ }
140
+ /**
141
+ * Merge a `Transaction` and any referenced `merklePath` and `sourceTransaction`, recursifely.
142
+ *
143
+ * Replaces existing transaction with same txid.
144
+ *
145
+ * Attempts to match an existing bump to the new transaction.
146
+ *
147
+ * @param tx
148
+ * @returns txid of tx
149
+ */
150
+ mergeTransaction(tx) {
151
+ const txid = tx.id('hex');
152
+ this.removeExistingTxid(txid);
153
+ let bumpIndex = undefined;
154
+ if (tx.merklePath)
155
+ bumpIndex = this.mergeBump(tx.merklePath);
156
+ const newTx = new BeefTx_js_1.default(tx, bumpIndex);
157
+ this.txs.push(newTx);
158
+ this.tryToValidateBumpIndex(newTx);
159
+ bumpIndex = newTx.bumpIndex;
160
+ if (bumpIndex === undefined) {
161
+ for (const input of tx.inputs) {
162
+ if (input.sourceTransaction)
163
+ this.mergeTransaction(input.sourceTransaction);
164
+ }
165
+ }
166
+ return newTx;
167
+ }
168
+ /**
169
+ * Removes an existing transaction from the BEEF, given its TXID
170
+ * @param txid TXID of the transaction to remove
171
+ */
172
+ removeExistingTxid(txid) {
173
+ const existingTxIndex = this.txs.findIndex(t => t.txid === txid);
174
+ if (existingTxIndex >= 0)
175
+ this.txs.splice(existingTxIndex, 1);
176
+ }
177
+ mergeTxidOnly(txid) {
178
+ let tx = this.txs.find(t => t.txid === txid);
179
+ if (!tx) {
180
+ tx = new BeefTx_js_1.default(txid);
181
+ this.txs.push(tx);
182
+ this.tryToValidateBumpIndex(tx);
183
+ }
184
+ return tx;
185
+ }
186
+ mergeBeefTx(btx) {
187
+ let beefTx = this.findTxid(btx.txid);
188
+ if (!beefTx && btx.isTxidOnly)
189
+ beefTx = this.mergeTxidOnly(btx.txid);
190
+ else if (!beefTx || beefTx.isTxidOnly) {
191
+ if (btx._tx)
192
+ beefTx = this.mergeTransaction(btx._tx);
193
+ else
194
+ beefTx = this.mergeRawTx(btx._rawTx);
195
+ }
196
+ return beefTx;
197
+ }
198
+ mergeBeef(beef) {
199
+ const b = Array.isArray(beef) ? Beef.fromBinary(beef) : beef;
200
+ for (const bump of b.bumps)
201
+ this.mergeBump(bump);
202
+ for (const tx of b.txs)
203
+ this.mergeBeefTx(tx);
204
+ }
205
+ /**
206
+ * Sorts `txs` and checks structural validity of beef.
207
+ *
208
+ * Does NOT verify merkle roots.
209
+ *
210
+ * Validity requirements:
211
+ * 1. No 'known' txids, unless `allowTxidOnly` is true.
212
+ * 2. All transactions have bumps or their inputs chain back to bumps (or are known).
213
+ * 3. Order of transactions satisfies dependencies before dependents.
214
+ * 4. No transactions with duplicate txids.
215
+ *
216
+ * @param allowTxidOnly optional. If true, transaction txid only is assumed valid
217
+ */
218
+ isValid(allowTxidOnly) {
219
+ return this.verifyValid(allowTxidOnly).valid;
220
+ }
221
+ /**
222
+ * Sorts `txs` and confirms validity of transaction data contained in beef
223
+ * by validating structure of this beef and confirming computed merkle roots
224
+ * using `chainTracker`.
225
+ *
226
+ * Validity requirements:
227
+ * 1. No 'known' txids, unless `allowTxidOnly` is true.
228
+ * 2. All transactions have bumps or their inputs chain back to bumps (or are known).
229
+ * 3. Order of transactions satisfies dependencies before dependents.
230
+ * 4. No transactions with duplicate txids.
231
+ *
232
+ * @param chainTracker Used to verify computed merkle path roots for all bump txids.
233
+ * @param allowTxidOnly optional. If true, transaction txid is assumed valid
234
+ */
235
+ async verify(chainTracker, allowTxidOnly) {
236
+ const r = this.verifyValid(allowTxidOnly);
237
+ if (!r.valid)
238
+ return false;
239
+ for (const height of Object.keys(r.roots)) {
240
+ const isValid = await chainTracker.isValidRootForHeight(r.roots[height], Number(height));
241
+ if (!isValid)
242
+ return false;
243
+ }
244
+ return true;
245
+ }
246
+ verifyValid(allowTxidOnly) {
247
+ const r = { valid: false, roots: {} };
248
+ this.sortTxs();
249
+ // valid txids: only txids if allowed, bump txids, then txids with input's in txids
250
+ const txids = {};
251
+ for (const tx of this.txs) {
252
+ if (tx.isTxidOnly) {
253
+ if (!allowTxidOnly)
254
+ return r;
255
+ txids[tx.txid] = true;
256
+ }
257
+ }
258
+ const confirmComputedRoot = (b, txid) => {
259
+ const root = b.computeRoot(txid);
260
+ if (!r.roots[b.blockHeight]) {
261
+ // accept the root as valid for this block and reuse for subsequent txids
262
+ r.roots[b.blockHeight] = root;
263
+ }
264
+ if (r.roots[b.blockHeight] !== root)
265
+ return false;
266
+ return true;
267
+ };
268
+ for (const b of this.bumps) {
269
+ for (const n of b.path[0]) {
270
+ if (n.txid && n.hash) {
271
+ txids[n.hash] = true;
272
+ // all txid hashes in all bumps must have agree on computed merkle path roots
273
+ if (!confirmComputedRoot(b, n.hash))
274
+ return r;
275
+ }
276
+ }
277
+ }
278
+ for (const t of this.txs) {
279
+ for (const i of t.inputTxids)
280
+ // all input txids must be included before they are referenced
281
+ if (!txids[i])
282
+ return r;
283
+ txids[t.txid] = true;
284
+ }
285
+ r.valid = true;
286
+ return r;
287
+ }
288
+ /**
289
+ * Returns a binary array representing the serialized BEEF
290
+ * @returns A binary array representing the BEEF
291
+ */
292
+ toBinary() {
293
+ const writer = new utils_js_1.Writer();
294
+ writer.writeUInt32LE(this.version);
295
+ writer.writeVarIntNum(this.bumps.length);
296
+ for (const b of this.bumps) {
297
+ writer.write(b.toBinary());
298
+ }
299
+ writer.writeVarIntNum(this.txs.length);
300
+ for (const tx of this.txs) {
301
+ tx.toWriter(writer);
302
+ }
303
+ return writer.toArray();
304
+ }
305
+ /**
306
+ * Returns a hex string representing the serialized BEEF
307
+ * @returns A hex string representing the BEEF
308
+ */
309
+ toHex() {
310
+ return (0, utils_js_1.toHex)(this.toBinary());
311
+ }
312
+ static fromReader(br) {
313
+ const version = br.readUInt32LE();
314
+ if (version !== exports.BEEF_MAGIC && version !== exports.BEEF_MAGIC_V2)
315
+ throw new Error(`Serialized BEEF must start with ${exports.BEEF_MAGIC} or ${exports.BEEF_MAGIC_V2} but starts with ${version}`);
316
+ const beef = new Beef();
317
+ const bumpsLength = br.readVarIntNum();
318
+ for (let i = 0; i < bumpsLength; i++) {
319
+ const bump = MerklePath_js_1.default.fromReader(br);
320
+ beef.bumps.push(bump);
321
+ }
322
+ const txsLength = br.readVarIntNum();
323
+ for (let i = 0; i < txsLength; i++) {
324
+ const beefTx = BeefTx_js_1.default.fromReader(br);
325
+ beef.txs.push(beefTx);
326
+ }
327
+ return beef;
328
+ }
329
+ /**
330
+ * Constructs an instance of the Beef class based on the provided binary array
331
+ * @param bin The binary array from which to construct BEEF
332
+ * @returns An instance of the Beef class constructed from the binary data
333
+ */
334
+ static fromBinary(bin) {
335
+ const br = new utils_js_1.Reader(bin);
336
+ return Beef.fromReader(br);
337
+ }
338
+ /**
339
+ * Constructs an instance of the Beef class based on the provided string
340
+ * @param s The string value from which to construct BEEF
341
+ * @param enc The encoding of the string value from which BEEF should be constructed
342
+ * @returns An instance of the Beef class constructed from the string
343
+ */
344
+ static fromString(s, enc) {
345
+ enc || (enc = 'hex');
346
+ const bin = (0, utils_js_1.toArray)(s, enc);
347
+ const br = new utils_js_1.Reader(bin);
348
+ return Beef.fromReader(br);
349
+ }
350
+ /**
351
+ * Try to validate newTx.bumpIndex by looking for an existing bump
352
+ * that proves newTx.txid
353
+ *
354
+ * @param newTx A new `BeefTx` that has been added to this.txs
355
+ * @returns true if a bump was found, false otherwise
356
+ */
357
+ tryToValidateBumpIndex(newTx) {
358
+ if (newTx.bumpIndex !== undefined)
359
+ return true;
360
+ const txid = newTx.txid;
361
+ for (let i = 0; i < this.bumps.length; i++) {
362
+ const j = this.bumps[i].path[0].findIndex(b => b.hash === txid);
363
+ if (j >= 0) {
364
+ newTx.bumpIndex = i;
365
+ this.bumps[i].path[0][j].txid = true;
366
+ return true;
367
+ }
368
+ }
369
+ return false;
370
+ }
371
+ /**
372
+ * Sort the `txs` by input txid dependency order.
373
+ * @returns array of input txids of unproven transactions that aren't included in txs.
374
+ */
375
+ sortTxs() {
376
+ const missingInputs = {};
377
+ const txidToTx = {};
378
+ for (const tx of this.txs) {
379
+ txidToTx[tx.txid] = tx;
380
+ // All transactions in this beef start at degree zero.
381
+ tx.degree = 0;
382
+ }
383
+ for (const tx of this.txs) {
384
+ if (tx.bumpIndex === undefined) {
385
+ // For all the unproven transactions,
386
+ // link their inputs that exist in this beef,
387
+ // make a note of missing inputs.
388
+ for (const inputTxid of tx.inputTxids) {
389
+ if (!txidToTx[inputTxid])
390
+ missingInputs[inputTxid] = true;
391
+ }
392
+ }
393
+ }
394
+ // queue of transactions that no unsorted transactions depend upon...
395
+ const queue = [];
396
+ // sorted transactions
397
+ const result = [];
398
+ // Increment each txid's degree for every input reference to it by another txid
399
+ for (const tx of this.txs) {
400
+ for (const inputTxid of tx.inputTxids) {
401
+ const tx = txidToTx[inputTxid];
402
+ if (tx)
403
+ tx.degree++;
404
+ }
405
+ }
406
+ // Since circular dependencies aren't possible, start with the txids no one depends on.
407
+ // These are the transactions that should be sent last...
408
+ for (const tx of this.txs) {
409
+ if (tx.degree === 0) {
410
+ queue.push(tx);
411
+ }
412
+ }
413
+ // As long as we have transactions to send...
414
+ while (queue.length) {
415
+ let tx = queue.shift();
416
+ // Add it as new first to send
417
+ result.unshift(tx);
418
+ // And remove its impact on degree
419
+ // noting that any tx achieving a
420
+ // value of zero can be sent...
421
+ for (const inputTxid of tx.inputTxids) {
422
+ const inputTx = txidToTx[inputTxid];
423
+ if (inputTx) {
424
+ inputTx.degree--;
425
+ if (inputTx.degree === 0) {
426
+ queue.push(inputTx);
427
+ }
428
+ }
429
+ }
430
+ }
431
+ this.txs = result;
432
+ return Object.keys(missingInputs);
433
+ }
434
+ /**
435
+ * @returns a shallow copy of this beef
436
+ */
437
+ clone() {
438
+ const c = new Beef();
439
+ c.bumps = Array.from(this.bumps);
440
+ c.txs = Array.from(this.txs);
441
+ return c;
442
+ }
443
+ /**
444
+ * Ensure that all the txids in `knownTxids` are txidOnly
445
+ * @param knownTxids
446
+ */
447
+ trimKnownTxids(knownTxids) {
448
+ for (let i = 0; i < this.txs.length;) {
449
+ const tx = this.txs[i];
450
+ if (tx.isTxidOnly && -1 < knownTxids.indexOf(tx.txid)) {
451
+ this.txs.splice(i, 1);
452
+ }
453
+ else {
454
+ i++;
455
+ }
456
+ }
457
+ // TODO: bumps could be trimmed to eliminate unreferenced proofs.
458
+ }
459
+ /**
460
+ * @returns Summary of `Beef` contents as multi-line string.
461
+ */
462
+ toLogString() {
463
+ let log = '';
464
+ log += `BEEF with ${this.bumps.length} BUMPS and ${this.txs.length} Transactions, isValid ${this.isValid()}\n`;
465
+ let i = -1;
466
+ for (const b of this.bumps) {
467
+ i++;
468
+ log += ` BUMP ${i}\n block: ${b.blockHeight}\n txids: [\n${b.path[0].filter(n => !!n.txid).map(n => ` '${n.hash}'`).join(',\n')}\n ]\n`;
469
+ }
470
+ i = -1;
471
+ for (const t of this.txs) {
472
+ i++;
473
+ log += ` TX ${i}\n txid: ${t.txid}\n`;
474
+ if (t.bumpIndex !== undefined) {
475
+ log += ` bumpIndex: ${t.bumpIndex}\n`;
476
+ }
477
+ if (t.isTxidOnly) {
478
+ log += ` txidOnly\n`;
479
+ }
480
+ else {
481
+ log += ` rawTx length=${t.rawTx.length}\n`;
482
+ }
483
+ if (t.inputTxids.length > 0) {
484
+ log += ` inputs: [\n${t.inputTxids.map(it => ` '${it}'`).join(',\n')}\n ]\n`;
485
+ }
486
+ }
487
+ return log;
488
+ }
489
+ }
490
+ exports.Beef = Beef;
491
+ exports.default = Beef;
492
+ //# sourceMappingURL=Beef.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Beef.js","sourceRoot":"","sources":["../../../../src/transaction/Beef.ts"],"names":[],"mappings":";;;;;;AAAA,oEAAyC;AAGzC,4DAAiC;AACjC,qDAAuE;AAE1D,QAAA,UAAU,GAAG,UAAU,CAAA,CAAI,uBAAuB;AAClD,QAAA,aAAa,GAAG,UAAU,CAAA,CAAC,uBAAuB;AAClD,QAAA,8BAA8B,GAAG,UAAU,CAAA,CAAC,uBAAuB;AAEhF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAa,IAAI;IAIb;QAHA,UAAK,GAAiB,EAAE,CAAA;QACxB,QAAG,GAAa,EAAE,CAAA;IAGlB,CAAC;IAED;;;;OAIG;IACH,IAAI,OAAO;QACP,MAAM,WAAW,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAA;QACzD,IAAI,WAAW;YACX,OAAO,qBAAa,CAAA;;YAEpB,OAAO,kBAAU,CAAA;IACzB,CAAC;IAED;;;OAGG;IACH,QAAQ,CAAC,IAAY;QACjB,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exports, "__esModule", { value: true });
3
+ exports.BeefParty = void 0;
4
+ const Beef_js_1 = require("./Beef.js");
5
+ /**
6
+ * Extends `Beef` that is used to exchange transaction validity data with more than one external party.
7
+ *
8
+ * Use `addKnownTxidsForParty` to keep track of who knows what to reduce re-transmission of potentially large transactions.
9
+ *
10
+ * Use `getTrimmedBeefForParty` to obtain a `Beef` trimmed of transaction validity data known to a specific party.
11
+ *
12
+ * Typical usage scenario:
13
+ *
14
+ * 1. Query a wallet storage provider for spendable outputs.
15
+ * 2. The provider replies with a Beef validating the returned outputs.
16
+ * 3. Construct a new transaction using some of the queried outputs as inputs, including Beef validating all the inputs.
17
+ * 4. Receive new valid raw transaction after processing and Beef validating change outputs added to original inputs.
18
+ * 5. Return to step 1, continuing to build on old and new spendable outputs.
19
+ *
20
+ * By default, each Beef is required to be complete and valid: All transactions appear as full serialized bitcoin transactions and
21
+ * each transaction either has a merkle path proof (it has been mined) or all of its input transactions are included.
22
+ *
23
+ * The size and redundancy of these Beefs becomes a problem when chained transaction creation out-paces the block mining rate.
24
+ *
25
+ */
26
+ class BeefParty extends Beef_js_1.Beef {
27
+ /**
28
+ *
29
+ * @param parties Optional array of initial unique party identifiers.
30
+ */
31
+ constructor(parties) {
32
+ super();
33
+ /**
34
+ * keys are party identifiers.
35
+ * values are records of txids with truthy value for which the party already has validity proof.
36
+ */
37
+ this.knownTo = {};
38
+ if (parties) {
39
+ for (const party of parties)
40
+ this.addParty(party);
41
+ }
42
+ }
43
+ /**
44
+ * @param party
45
+ * @returns `true` if `party` has already beed added to this `BeefParty`.
46
+ */
47
+ isParty(party) {
48
+ const r = Object.keys(this.knownTo).includes(party);
49
+ return r;
50
+ }
51
+ /**
52
+ * Adds a new unique party identifier to this `BeefParty`.
53
+ * @param party
54
+ */
55
+ addParty(party) {
56
+ if (this.isParty(party))
57
+ throw new Error(`Party ${party} already exists.`);
58
+ this.knownTo[party] = {};
59
+ }
60
+ /**
61
+ * @param party
62
+ * @returns Array of txids "known" to `party`.
63
+ */
64
+ getKnownTxidsForParty(party) {
65
+ const knownTxids = this.knownTo[party];
66
+ if (!knownTxids)
67
+ throw new Error(`Party ${party} is unknown.`);
68
+ return Object.keys(knownTxids);
69
+ }
70
+ /**
71
+ * @param party
72
+ * @returns trimmed beef of unknown transactions and proofs for `party`
73
+ */
74
+ getTrimmedBeefForParty(party) {
75
+ const knownTxids = this.getKnownTxidsForParty(party);
76
+ const prunedBeef = this.clone();
77
+ prunedBeef.trimKnownTxids(knownTxids);
78
+ return prunedBeef;
79
+ }
80
+ /**
81
+ * Make note of additional txids "known" to `party`.
82
+ * @param party unique identifier, added if new.
83
+ * @param knownTxids
84
+ */
85
+ addKnownTxidsForParty(party, knownTxids) {
86
+ if (!this.isParty(party))
87
+ this.addParty(party);
88
+ const kts = this.knownTo[party];
89
+ for (const txid of knownTxids) {
90
+ kts[txid] = true;
91
+ this.mergeTxidOnly(txid);
92
+ }
93
+ }
94
+ }
95
+ exports.BeefParty = BeefParty;
96
+ exports.default = BeefParty;
97
+ //# sourceMappingURL=BeefParty.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BeefParty.js","sourceRoot":"","sources":["../../../../src/transaction/BeefParty.ts"],"names":[],"mappings":";;;AAAA,uCAAiC;AAEjC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,SAAU,SAAQ,cAAI;IAO/B;;;OAGG;IACH,YAAY,OAAkB;QAC1B,KAAK,EAAE,CAAA;QAXX;;;WAGG;QACH,YAAO,GAA4C,EAAE,CAAA;QAQjD,IAAI,OAAO,EAAE,CAAC;YACV,KAAK,MAAM,KAAK,IAAI,OAAO;gBACvB,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC5B,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,KAAa;QACjB,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;Q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