@did-btcr2/bitcoin 0.3.1 → 0.3.3

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 (73) hide show
  1. package/dist/cjs/client/rest/block.js.map +1 -1
  2. package/dist/cjs/client/rest/index.js +2 -11
  3. package/dist/cjs/client/rest/index.js.map +1 -1
  4. package/dist/cjs/client/rpc/index.js +59 -21
  5. package/dist/cjs/client/rpc/index.js.map +1 -1
  6. package/dist/cjs/client/rpc/interface.js +6 -1
  7. package/dist/cjs/client/rpc/interface.js.map +1 -1
  8. package/dist/cjs/client/rpc/json-rpc.js +0 -1
  9. package/dist/cjs/client/rpc/json-rpc.js.map +1 -1
  10. package/dist/cjs/constants.js +4 -4
  11. package/dist/cjs/constants.js.map +1 -1
  12. package/dist/cjs/errors.js +3 -1
  13. package/dist/cjs/errors.js.map +1 -1
  14. package/dist/cjs/index.js +0 -1
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/types.js +10 -3
  17. package/dist/cjs/types.js.map +1 -1
  18. package/dist/esm/client/rest/block.js.map +1 -1
  19. package/dist/esm/client/rest/index.js +2 -11
  20. package/dist/esm/client/rest/index.js.map +1 -1
  21. package/dist/esm/client/rpc/index.js +59 -21
  22. package/dist/esm/client/rpc/index.js.map +1 -1
  23. package/dist/esm/client/rpc/interface.js +6 -1
  24. package/dist/esm/client/rpc/interface.js.map +1 -1
  25. package/dist/esm/client/rpc/json-rpc.js +0 -1
  26. package/dist/esm/client/rpc/json-rpc.js.map +1 -1
  27. package/dist/esm/constants.js +4 -4
  28. package/dist/esm/constants.js.map +1 -1
  29. package/dist/esm/errors.js +3 -1
  30. package/dist/esm/errors.js.map +1 -1
  31. package/dist/esm/index.js +0 -1
  32. package/dist/esm/index.js.map +1 -1
  33. package/dist/esm/types.js +10 -3
  34. package/dist/esm/types.js.map +1 -1
  35. package/dist/types/client/rest/address.d.ts +1 -1
  36. package/dist/types/client/rest/address.d.ts.map +1 -1
  37. package/dist/types/client/rest/block.d.ts +1 -2
  38. package/dist/types/client/rest/block.d.ts.map +1 -1
  39. package/dist/types/client/rest/index.d.ts +1 -84
  40. package/dist/types/client/rest/index.d.ts.map +1 -1
  41. package/dist/types/client/rest/transaction.d.ts +1 -1
  42. package/dist/types/client/rest/transaction.d.ts.map +1 -1
  43. package/dist/types/client/rpc/index.d.ts +22 -5
  44. package/dist/types/client/rpc/index.d.ts.map +1 -1
  45. package/dist/types/client/rpc/interface.d.ts +1 -0
  46. package/dist/types/client/rpc/interface.d.ts.map +1 -1
  47. package/dist/types/client/rpc/json-rpc.d.ts.map +1 -1
  48. package/dist/types/constants.d.ts +4 -4
  49. package/dist/types/errors.d.ts +3 -2
  50. package/dist/types/errors.d.ts.map +1 -1
  51. package/dist/types/index.d.ts +0 -1
  52. package/dist/types/index.d.ts.map +1 -1
  53. package/dist/types/types.d.ts +83 -2
  54. package/dist/types/types.d.ts.map +1 -1
  55. package/package.json +6 -3
  56. package/src/client/rest/address.ts +1 -1
  57. package/src/client/rest/block.ts +1 -2
  58. package/src/client/rest/index.ts +3 -91
  59. package/src/client/rest/transaction.ts +1 -1
  60. package/src/client/rpc/index.ts +66 -26
  61. package/src/client/rpc/interface.ts +7 -0
  62. package/src/client/rpc/json-rpc.ts +0 -1
  63. package/src/constants.ts +4 -4
  64. package/src/errors.ts +4 -2
  65. package/src/index.ts +0 -1
  66. package/src/types.ts +90 -5
  67. package/dist/cjs/taproot.js +0 -219
  68. package/dist/cjs/taproot.js.map +0 -1
  69. package/dist/esm/taproot.js +0 -219
  70. package/dist/esm/taproot.js.map +0 -1
  71. package/dist/types/taproot.d.ts +0 -34
  72. package/dist/types/taproot.d.ts.map +0 -1
  73. package/src/taproot.ts +0 -237
package/src/taproot.ts DELETED
@@ -1,237 +0,0 @@
1
- import * as tinysecp from 'tiny-secp256k1';
2
- import { payments, script, opcodes } from 'bitcoinjs-lib';
3
- import { CompressedSecp256k1PublicKey } from '@did-btcr2/keypair';
4
-
5
- /**
6
- * Aggregate an array of public keys by point addition
7
- */
8
- function aggregatePubkeys(pubkeys: Uint8Array[]): Uint8Array {
9
- if (pubkeys.length === 1) return pubkeys[0];
10
- return pubkeys.reduce((sum: Uint8Array, pk: Uint8Array) => {
11
- const added = tinysecp.pointAdd(sum, pk);
12
- if (!added) throw new Error('Point addition failed');
13
- return added;
14
- }, pubkeys[0]);
15
- }
16
-
17
- /**
18
- * Build a P2TR script leaf for a k-of-n multisig with optional locktime/sequence
19
- */
20
- function buildTapLeafScript(
21
- pubkeys: Uint8Array[],
22
- k: number,
23
- locktime?: number,
24
- sequence?: number
25
- ): Uint8Array {
26
- const ops: (Uint8Array | number)[] = [];
27
-
28
- // absolute timelock
29
- if (locktime !== undefined) {
30
- ops.push(script.number.encode(locktime));
31
- ops.push(opcodes.OP_CHECKLOCKTIMEVERIFY);
32
- ops.push(opcodes.OP_DROP);
33
- }
34
- // relative timelock
35
- if (sequence !== undefined) {
36
- ops.push(script.number.encode(sequence));
37
- ops.push(opcodes.OP_CHECKSEQUENCEVERIFY);
38
- ops.push(opcodes.OP_DROP);
39
- }
40
-
41
- // push each key as x-only pubkey
42
- for (const pk of pubkeys) {
43
- const [xOnly] = new CompressedSecp256k1PublicKey(pk).xOnly;
44
- ops.push(xOnly);
45
- }
46
- // push threshold k and total keys n
47
- ops.push(script.number.encode(k));
48
- ops.push(script.number.encode(pubkeys.length));
49
- ops.push(opcodes.OP_CHECKMULTISIG);
50
-
51
- return script.compile(ops);
52
- }
53
-
54
- /**
55
- * Build a P2TR script leaf for a aggregated MuSig key (key-path only)
56
- */
57
- function buildMusigLeafScript(pubkeys: Uint8Array[]): Uint8Array {
58
- const agg = aggregatePubkeys(pubkeys);
59
- const [xOnly] = new CompressedSecp256k1PublicKey(agg).xOnly;
60
- // In a taproot script path, OP_CHECKSIG runs schnorr
61
- return script.compile([xOnly, opcodes.OP_CHECKSIG]);
62
- }
63
-
64
- /**
65
- * Recursively combine an array of leaves into a balanced Merkle tree structure
66
- */
67
- function buildMerkleTree(nodes: Uint8Array[]): any {
68
- if (nodes.length === 1) return { output: nodes[0] };
69
- const next: any[] = [];
70
- for (let i = 0; i < nodes.length; i += 2) {
71
- if (i + 1 === nodes.length) {
72
- next.push({ output: nodes[i] });
73
- } else {
74
- next.push([
75
- { output: nodes[i] },
76
- { output: nodes[i + 1] },
77
- ]);
78
- }
79
- }
80
- return buildMerkleTree(next);
81
- }
82
-
83
- /**
84
- * Generate combinations of length k from an array
85
- */
86
- function* combinations<T>(array: T[], k: number): Generator<T[]> {
87
- const n = array.length;
88
- if (k > n || k < 0) return;
89
- const indices = Array.from({ length: k }, (_, i) => i);
90
- while (true) {
91
- yield indices.map(i => array[i]!);
92
- let i = k - 1;
93
- while (i >= 0 && indices[i] === i + n - k) {
94
- i--;
95
- }
96
- if (i < 0) break;
97
- indices[i]++;
98
- for (let j = i + 1; j < k; j++) {
99
- indices[j] = indices[j - 1] + 1;
100
- }
101
- }
102
- }
103
-
104
- /**
105
- * TapRootMultiSig: builds Taproot outputs and trees for multisig and MuSig branches
106
- */
107
- export class TapRootMultiSig {
108
- public readonly points: Uint8Array[];
109
- public readonly k: number;
110
- public readonly defaultInternalPubkey: Uint8Array;
111
-
112
- constructor(points: Uint8Array[], k: number) {
113
- if (points.length < k || k < 1) {
114
- throw new Error(`${k} is invalid for ${points.length} keys`);
115
- }
116
- this.points = points;
117
- this.k = k;
118
- // MuSig aggregation for default internal key
119
- this.defaultInternalPubkey = aggregatePubkeys(points);
120
- }
121
-
122
- /**
123
- * Single multisig leaf as the only script path
124
- */
125
- singleLeaf(locktime?: number, sequence?: number) {
126
- const leaf = buildTapLeafScript(this.points, this.k, locktime, sequence);
127
- return payments.p2tr({
128
- internalPubkey : this.defaultInternalPubkey,
129
- scriptTree : { output: leaf },
130
- });
131
- }
132
-
133
- /**
134
- * All k-of-n multisig combinations as separate leaf scripts, combined into one tree
135
- */
136
- multiLeafTree(locktime?: number, sequence?: number) {
137
- const leaves: Uint8Array[] = [];
138
- for (const combo of combinations(this.points, this.k)) {
139
- leaves.push(buildTapLeafScript(combo, this.k, locktime, sequence));
140
- }
141
- const tree = buildMerkleTree(leaves);
142
- return payments.p2tr({
143
- internalPubkey : this.defaultInternalPubkey,
144
- scriptTree : tree,
145
- });
146
- }
147
-
148
- /**
149
- * MuSig key-path scripts for each k-of-n combination in the script tree
150
- */
151
- musigTree() {
152
- const leaves: Uint8Array[] = [];
153
- for (const combo of combinations(this.points, this.k)) {
154
- leaves.push(buildMusigLeafScript(combo));
155
- }
156
- const tree = buildMerkleTree(leaves);
157
- return payments.p2tr({
158
- internalPubkey : this.defaultInternalPubkey,
159
- scriptTree : tree,
160
- });
161
- }
162
-
163
- /**
164
- * A two-branch tree: one branch is the singleLeaf script, the other is the muSig tree
165
- */
166
- musigAndSingleLeafTree(locktime?: number, sequence?: number) {
167
- const single = buildTapLeafScript(this.points, this.k, locktime, sequence);
168
- const musigLeaves: Uint8Array[] = [];
169
- for (const combo of combinations(this.points, this.k)) {
170
- musigLeaves.push(buildMusigLeafScript(combo));
171
- }
172
- const tree = {
173
- output : single,
174
- scriptTree : buildMerkleTree(musigLeaves),
175
- };
176
- return payments.p2tr({
177
- internalPubkey : this.defaultInternalPubkey,
178
- scriptTree : tree,
179
- });
180
- }
181
-
182
- /**
183
- * Nested tree of singleLeaf, multiLeafTree, and musigTree
184
- */
185
- everythingTree(locktime?: number, sequence?: number) {
186
- const single = buildTapLeafScript(this.points, this.k, locktime, sequence);
187
-
188
- const multiLeaves: Uint8Array[] = [];
189
- for (const combo of combinations(this.points, this.k)) {
190
- multiLeaves.push(buildTapLeafScript(combo, this.k, locktime, sequence));
191
- }
192
- const multiTree = buildMerkleTree(multiLeaves);
193
-
194
- const musigLeaves: Uint8Array[] = [];
195
- for (const combo of combinations(this.points, this.k)) {
196
- musigLeaves.push(buildMusigLeafScript(combo));
197
- }
198
- const musigTree = buildMerkleTree(musigLeaves);
199
-
200
- const tree = {
201
- output : single,
202
- scriptTree : [multiTree, musigTree],
203
- };
204
- return payments.p2tr({
205
- internalPubkey : this.defaultInternalPubkey,
206
- scriptTree : tree,
207
- });
208
- }
209
-
210
- /**
211
- * Degrading multisig: k-of-n initially, then (k-1)-of-n after delay, ... until 1-of-n
212
- */
213
- degradingMultisigTree(
214
- sequenceBlockInterval?: number,
215
- sequenceTimeInterval?: number
216
- ) {
217
- const leaves: Uint8Array[] = [];
218
- for (let num = this.k; num >= 1; num--) {
219
- let seq: number | undefined;
220
- if (num === this.k) {
221
- seq = undefined;
222
- } else if (sequenceBlockInterval != null) {
223
- seq = sequenceBlockInterval * (this.k - num);
224
- } else if (sequenceTimeInterval != null) {
225
- seq = sequenceTimeInterval * (this.k - num);
226
- }
227
- for (const combo of combinations(this.points, num)) {
228
- leaves.push(buildTapLeafScript(combo, num, undefined, seq));
229
- }
230
- }
231
- const tree = buildMerkleTree(leaves);
232
- return payments.p2tr({
233
- internalPubkey : this.defaultInternalPubkey,
234
- scriptTree : tree,
235
- });
236
- }
237
- }