@did-btcr2/bitcoin 0.3.2 → 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 (62) hide show
  1. package/dist/cjs/client/rpc/index.js +59 -21
  2. package/dist/cjs/client/rpc/index.js.map +1 -1
  3. package/dist/cjs/client/rpc/interface.js +6 -1
  4. package/dist/cjs/client/rpc/interface.js.map +1 -1
  5. package/dist/cjs/client/rpc/json-rpc.js +0 -1
  6. package/dist/cjs/client/rpc/json-rpc.js.map +1 -1
  7. package/dist/cjs/constants.js +4 -4
  8. package/dist/cjs/constants.js.map +1 -1
  9. package/dist/cjs/errors.js +3 -1
  10. package/dist/cjs/errors.js.map +1 -1
  11. package/dist/cjs/index.js +0 -1
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/types.js +0 -3
  14. package/dist/cjs/types.js.map +1 -1
  15. package/dist/esm/client/rpc/index.js +59 -21
  16. package/dist/esm/client/rpc/index.js.map +1 -1
  17. package/dist/esm/client/rpc/interface.js +6 -1
  18. package/dist/esm/client/rpc/interface.js.map +1 -1
  19. package/dist/esm/client/rpc/json-rpc.js +0 -1
  20. package/dist/esm/client/rpc/json-rpc.js.map +1 -1
  21. package/dist/esm/constants.js +4 -4
  22. package/dist/esm/constants.js.map +1 -1
  23. package/dist/esm/errors.js +3 -1
  24. package/dist/esm/errors.js.map +1 -1
  25. package/dist/esm/index.js +0 -1
  26. package/dist/esm/index.js.map +1 -1
  27. package/dist/esm/types.js +0 -3
  28. package/dist/esm/types.js.map +1 -1
  29. package/dist/types/client/rpc/index.d.ts +22 -5
  30. package/dist/types/client/rpc/index.d.ts.map +1 -1
  31. package/dist/types/client/rpc/interface.d.ts +1 -0
  32. package/dist/types/client/rpc/interface.d.ts.map +1 -1
  33. package/dist/types/client/rpc/json-rpc.d.ts.map +1 -1
  34. package/dist/types/constants.d.ts +4 -4
  35. package/dist/types/errors.d.ts +3 -2
  36. package/dist/types/errors.d.ts.map +1 -1
  37. package/dist/types/index.d.ts +0 -1
  38. package/dist/types/index.d.ts.map +1 -1
  39. package/dist/types/types.d.ts +0 -1
  40. package/dist/types/types.d.ts.map +1 -1
  41. package/package.json +2 -2
  42. package/src/client/rpc/index.ts +66 -26
  43. package/src/client/rpc/interface.ts +7 -0
  44. package/src/client/rpc/json-rpc.ts +0 -1
  45. package/src/constants.ts +4 -4
  46. package/src/errors.ts +4 -2
  47. package/src/index.ts +0 -1
  48. package/src/types.ts +0 -4
  49. package/dist/cjs/musig2.js +0 -74
  50. package/dist/cjs/musig2.js.map +0 -1
  51. package/dist/cjs/taproot.js +0 -219
  52. package/dist/cjs/taproot.js.map +0 -1
  53. package/dist/esm/musig2.js +0 -74
  54. package/dist/esm/musig2.js.map +0 -1
  55. package/dist/esm/taproot.js +0 -219
  56. package/dist/esm/taproot.js.map +0 -1
  57. package/dist/types/musig2.d.ts +0 -44
  58. package/dist/types/musig2.d.ts.map +0 -1
  59. package/dist/types/taproot.d.ts +0 -34
  60. package/dist/types/taproot.d.ts.map +0 -1
  61. package/src/musig2.ts +0 -117
  62. package/src/taproot.ts +0 -237
@@ -1 +0,0 @@
1
- {"version":3,"file":"musig2.d.ts","sourceRoot":"","sources":["../../src/musig2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAIxC,OAAO,EAAS,KAAK,EAAmB,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAY,IAAI,EAAE,MAAM,eAAe,CAAC;AAG/C,UAAU,aAAa;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,GAAG,EAAE,CAAC;IACpB,YAAY,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC5B,eAAe,CAAC,EAAE,GAAG,CAAC;IACtB,aAAa,CAAC,EAAE,UAAU,CAAC;IAC3B,WAAW,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;CAC5B;AAED,UAAU,uBAAuB;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,GAAG,CAAC;CAClB;AAED,UAAU,qBAAqB;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,qBAAqB,EAAE,GAAG,CAAC;CAC5B;AAED,UAAU,yBAAyB;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,GAAG,CAAC;CACZ;AAED,qBAAa,iBAAiB;IACrB,SAAS,EAAE,UAAU,CAAC;IACtB,SAAS,EAAE,UAAU,CAAC;IACtB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,QAAQ,CAAC,EAAE,KAAK,CAAC;gBAEZ,SAAS,CAAC,EAAE,UAAU;IAK3B,aAAa,IAAI,uBAAuB;IAWxC,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,GAAG,yBAAyB;CAsBxF;AAED,qBAAa,iBAAiB;IACrB,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAa;IAEjD,YAAY,CAAC,OAAO,EAAE,uBAAuB;IAK7C,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,qBAAqB;IAezD,gBAAgB,CAAC,IAAI,EAAE,yBAAyB;IAOhD,mBAAmB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;CAI/C"}
@@ -1,34 +0,0 @@
1
- import { payments } from 'bitcoinjs-lib';
2
- /**
3
- * TapRootMultiSig: builds Taproot outputs and trees for multisig and MuSig branches
4
- */
5
- export declare class TapRootMultiSig {
6
- readonly points: Uint8Array[];
7
- readonly k: number;
8
- readonly defaultInternalPubkey: Uint8Array;
9
- constructor(points: Uint8Array[], k: number);
10
- /**
11
- * Single multisig leaf as the only script path
12
- */
13
- singleLeaf(locktime?: number, sequence?: number): payments.Payment;
14
- /**
15
- * All k-of-n multisig combinations as separate leaf scripts, combined into one tree
16
- */
17
- multiLeafTree(locktime?: number, sequence?: number): payments.Payment;
18
- /**
19
- * MuSig key-path scripts for each k-of-n combination in the script tree
20
- */
21
- musigTree(): payments.Payment;
22
- /**
23
- * A two-branch tree: one branch is the singleLeaf script, the other is the muSig tree
24
- */
25
- musigAndSingleLeafTree(locktime?: number, sequence?: number): payments.Payment;
26
- /**
27
- * Nested tree of singleLeaf, multiLeafTree, and musigTree
28
- */
29
- everythingTree(locktime?: number, sequence?: number): payments.Payment;
30
- /**
31
- * Degrading multisig: k-of-n initially, then (k-1)-of-n after delay, ... until 1-of-n
32
- */
33
- degradingMultisigTree(sequenceBlockInterval?: number, sequenceTimeInterval?: number): payments.Payment;
34
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"taproot.d.ts","sourceRoot":"","sources":["../../src/taproot.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAmB,MAAM,eAAe,CAAC;AAsG1D;;GAEG;AACH,qBAAa,eAAe;IAC1B,SAAgB,MAAM,EAAE,UAAU,EAAE,CAAC;IACrC,SAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAgB,qBAAqB,EAAE,UAAU,CAAC;gBAEtC,MAAM,EAAE,UAAU,EAAE,EAAE,CAAC,EAAE,MAAM;IAU3C;;OAEG;IACH,UAAU,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;IAQ/C;;OAEG;IACH,aAAa,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;IAYlD;;OAEG;IACH,SAAS;IAYT;;OAEG;IACH,sBAAsB,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;IAgB3D;;OAEG;IACH,cAAc,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;IAyBnD;;OAEG;IACH,qBAAqB,CACnB,qBAAqB,CAAC,EAAE,MAAM,EAC9B,oBAAoB,CAAC,EAAE,MAAM;CAsBhC"}
package/src/musig2.ts DELETED
@@ -1,117 +0,0 @@
1
- import { Hex } from '@did-btcr2/common';
2
- import { Secp256k1SecretKey } from '@did-btcr2/keypair';
3
- import { schnorr } from '@noble/curves/secp256k1';
4
- import { bytesToHex } from '@noble/hashes/utils.js';
5
- import { CURVE, Point, ProjectivePoint } from '@noble/secp256k1';
6
- import { payments, Psbt } from 'bitcoinjs-lib';
7
- import { taggedHash } from 'bitcoinjs-lib/src/crypto';
8
-
9
- interface MuSig2Session {
10
- sessionId: string;
11
- beaconAddress: string;
12
- participants: Hex[];
13
- publicNonces: Map<Hex, Hex>; // pubkey => nonce
14
- aggregatedNonce?: Hex;
15
- messageToSign?: Uint8Array;
16
- partialSigs: Map<Hex, Hex>; // pubkey => sig
17
- }
18
-
19
- interface MuSig2NonceContribution {
20
- sessionId: string;
21
- beaconAddress: string;
22
- noncePoints: Hex;
23
- }
24
-
25
- interface AggregatedMuSig2Nonce {
26
- sessionId: string;
27
- beaconAddress: string;
28
- aggregatedNoncePoints: Hex;
29
- }
30
-
31
- interface BeaconSignalAuthorization {
32
- sessionId: string;
33
- beaconAddress: string;
34
- txSig: Hex;
35
- }
36
-
37
- export class MuSig2Participant {
38
- public secretKey: Uint8Array;
39
- public publicKey: Uint8Array;
40
- public nonce?: Uint8Array;
41
- public noncePub?: Point;
42
-
43
- constructor(secretKey?: Uint8Array) {
44
- this.secretKey = secretKey ?? Secp256k1SecretKey.random();
45
- this.publicKey = schnorr.getPublicKey(this.secretKey);
46
- }
47
-
48
- public generateNonce(): MuSig2NonceContribution {
49
- this.nonce = Secp256k1SecretKey.random();
50
- this.noncePub = ProjectivePoint.fromPrivateKey(this.nonce);
51
-
52
- return {
53
- sessionId : 'SESSION_ID',
54
- beaconAddress : Buffer.from(this.publicKey).toString('hex'),
55
- noncePoints : this.noncePub.toHex(true), // compressed
56
- };
57
- }
58
-
59
- public sign(message: Uint8Array, aggregatedNonceHex: string): BeaconSignalAuthorization {
60
- const R = ProjectivePoint.fromHex(aggregatedNonceHex);
61
- const L = ProjectivePoint.fromHex(this.publicKey);
62
-
63
- const e = taggedHash('BIP0340/challenge', new Uint8Array([
64
- ...R.toRawBytes(true),
65
- ...L.toRawBytes(true),
66
- ...message,
67
- ]));
68
-
69
- const eNum = BigInt('0x' + bytesToHex(e));
70
- const rNum = BigInt('0x' + bytesToHex(this.nonce!));
71
- const xNum = BigInt('0x' + bytesToHex(this.secretKey));
72
-
73
- const s = (rNum + eNum * xNum) % CURVE.n;
74
-
75
- return {
76
- sessionId : crypto.randomUUID(),
77
- beaconAddress : payments.p2tr({ internalPubkey: this.publicKey }).address || '',
78
- txSig : s.toString(16).padStart(64, '0'),
79
- };
80
- }
81
- }
82
-
83
- export class MuSig2Coordinator {
84
- public sessions: Map<string, MuSig2Session> = new Map();
85
-
86
- public receiveNonce(contrib: MuSig2NonceContribution) {
87
- const session = this.sessions.get(contrib.sessionId);
88
- session?.publicNonces.set(contrib.beaconAddress, contrib.noncePoints);
89
- }
90
-
91
- public aggregateNonces(sessionId: string): AggregatedMuSig2Nonce {
92
- const session = this.sessions.get(sessionId);
93
- if (!session) throw new Error('Session not found');
94
-
95
- const points = Array.from(session.publicNonces.values()).map(nonceHex => ProjectivePoint.fromHex(nonceHex));
96
- const aggregated = points.reduce((acc, pt) => acc.add(pt), ProjectivePoint.ZERO);
97
- session.aggregatedNonce = aggregated.toHex();
98
-
99
- return {
100
- sessionId,
101
- beaconAddress : session.beaconAddress,
102
- aggregatedNoncePoints : session.aggregatedNonce,
103
- };
104
- }
105
-
106
- public collectSignature(auth: BeaconSignalAuthorization) {
107
- const session = this.sessions.get(auth.sessionId);
108
- if (session) {
109
- session.partialSigs.set(auth.beaconAddress, auth.txSig);
110
- }
111
- }
112
-
113
- public finalizeTransaction(psbt: Psbt): string {
114
- // Combine partial signatures (assuming MuSig2 multi-party logic handled externally)
115
- return psbt.toHex();
116
- }
117
- }
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
- }