@sovereign-sdk/multisig 0.1.1
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.
- package/LICENSE +201 -0
- package/README.md +129 -0
- package/dist/index.d.mts +114 -0
- package/dist/index.d.ts +114 -0
- package/dist/index.js +220 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +191 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# @sovereign-sdk/multisig
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@sovereign-sdk/multisig)
|
|
4
|
+
[](https://github.com/Sovereign-Labs/sovereign-sdk-web3-js/actions/workflows/ci.yaml)
|
|
5
|
+
|
|
6
|
+
A TypeScript library for creating and managing multisig transactions on Sovereign SDK rollups.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @sovereign-sdk/multisig
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- **Multisig Transaction Management**: Create and manage transactions requiring multiple signatures
|
|
17
|
+
- **Threshold Signatures**: Configurable minimum signature requirements (M-of-N)
|
|
18
|
+
- **Signature Collection**: Collect signatures from multiple parties before submission
|
|
19
|
+
- **Transaction Conversion**: Convert between V0 (single-sig) and V1 (multisig) transaction formats
|
|
20
|
+
- **Safety Checks**: Validates transaction consistency and prevents replay attacks
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic Multisig Workflow
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { StandardRollup, createStandardRollup } from "@sovereign-sdk/web3";
|
|
28
|
+
import { MultisigTransaction } from "@sovereign-sdk/multisig";
|
|
29
|
+
import type { UnsignedTransaction } from "@sovereign-sdk/types";
|
|
30
|
+
|
|
31
|
+
// 1. Create an unsigned transaction
|
|
32
|
+
const unsignedTx: UnsignedTransaction<YourRuntimeCall> = {
|
|
33
|
+
runtime_call: {
|
|
34
|
+
// Your rollup-specific call data
|
|
35
|
+
value_setter: { set_value: { value: 100, gas: null } },
|
|
36
|
+
},
|
|
37
|
+
uniqueness: { nonce: 1 }, // Must be nonce-based for multisig
|
|
38
|
+
details: {
|
|
39
|
+
max_priority_fee_bips: 100,
|
|
40
|
+
max_fee: "1000000",
|
|
41
|
+
gas_limit: null,
|
|
42
|
+
chain_id: 4321,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// 2. Initialize multisig with all participant public keys
|
|
47
|
+
const allPubKeys = ["0xpubkey1", "0xpubkey2", "0xpubkey3"];
|
|
48
|
+
const minSigners = 2; // 2-of-3 multisig
|
|
49
|
+
|
|
50
|
+
const multisig = MultisigTransaction.empty(unsignedTx, minSigners, allPubKeys);
|
|
51
|
+
|
|
52
|
+
// 3. Add signatures as they're collected
|
|
53
|
+
multisig.addSignature("0xsignature1", "0xpubkey1");
|
|
54
|
+
multisig.addSignature("0xsignature2", "0xpubkey2");
|
|
55
|
+
|
|
56
|
+
// 4. Check if ready for submission
|
|
57
|
+
if (multisig.isComplete) {
|
|
58
|
+
// Convert to rollup transaction for submission
|
|
59
|
+
const finalTx = multisig.asTransaction();
|
|
60
|
+
const rollup = await createStandardRollup();
|
|
61
|
+
// Submit the transaction to the rollup
|
|
62
|
+
const result = await rollup.submitTransaction(finalTx);
|
|
63
|
+
console.log("Multisig transaction submitted", result);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Creating from Existing Signed Transactions
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { MultisigTransaction } from '@sovereign-sdk/multisig';
|
|
71
|
+
import type { Transaction } from '@sovereign-sdk/types';
|
|
72
|
+
|
|
73
|
+
// If you have individual V0 transactions from different signers
|
|
74
|
+
const signedTxns: Transaction<YourRuntimeCall>[] = [
|
|
75
|
+
{
|
|
76
|
+
V0: {
|
|
77
|
+
pub_key: "0xpubkey1",
|
|
78
|
+
signature: "0xsignature1",
|
|
79
|
+
runtime_call: /* same call data */,
|
|
80
|
+
uniqueness: { nonce: 1 },
|
|
81
|
+
details: /* same details */,
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
V0: {
|
|
86
|
+
pub_key: "0xpubkey2",
|
|
87
|
+
signature: "0xsignature2",
|
|
88
|
+
runtime_call: /* same call data */,
|
|
89
|
+
uniqueness: { nonce: 1 },
|
|
90
|
+
details: /* same details */,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
// Create multisig from existing transactions
|
|
96
|
+
const multisig = MultisigTransaction.fromTransactions({
|
|
97
|
+
txns: signedTxns,
|
|
98
|
+
minSigners: 2,
|
|
99
|
+
allPubKeys: ["0xpubkey1", "0xpubkey2", "0xpubkey3"]
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Adding Individual Signed Transactions
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// Add a complete signed transaction to the multisig
|
|
107
|
+
const signedTx: Transaction<YourRuntimeCall> = {
|
|
108
|
+
V0: {
|
|
109
|
+
pub_key: "0xpubkey3",
|
|
110
|
+
signature: "0xsignature3",
|
|
111
|
+
// ... same transaction data
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
multisig.addSignedTransaction(signedTx);
|
|
116
|
+
|
|
117
|
+
// Now check if complete and submit
|
|
118
|
+
if (multisig.isComplete) {
|
|
119
|
+
const finalTx = multisig.asTransaction();
|
|
120
|
+
// Submit to rollup...
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Important Notes
|
|
125
|
+
|
|
126
|
+
- **Nonce-based transactions only**: Multisig currently only supports transactions with `{ nonce: number }` uniqueness
|
|
127
|
+
- **Transaction consistency**: All signatures must be for the exact same unsigned transaction
|
|
128
|
+
- **Public key validation**: Public keys must be known members of the multisig and can only sign once
|
|
129
|
+
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { UnsignedTransaction, SignatureAndPubKey, Transaction } from '@sovereign-sdk/types';
|
|
2
|
+
import { HexString } from '@sovereign-sdk/utils';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for multisig-related errors.
|
|
6
|
+
*/
|
|
7
|
+
declare class MultisigError extends Error {
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Error thrown when a transaction has an unsupported version for multisig operations.
|
|
11
|
+
*/
|
|
12
|
+
declare class InvalidTransactionVersionError extends MultisigError {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Error thrown when a provided transaction doesn't match the expected unsigned transaction.
|
|
16
|
+
*/
|
|
17
|
+
declare class TransactionMismatchError extends Error {
|
|
18
|
+
constructor();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Error thrown when multisig is constructed or used with invalid parameters.
|
|
22
|
+
*/
|
|
23
|
+
declare class InvalidMultisigParameterError extends MultisigError {
|
|
24
|
+
constructor(desc: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parameters for constructing a MultisigTransaction.
|
|
28
|
+
*/
|
|
29
|
+
type MultisigParams = {
|
|
30
|
+
/** The unsigned transaction to be signed by multiple parties */
|
|
31
|
+
unsignedTx: UnsignedTransaction<unknown>;
|
|
32
|
+
/** Array of signatures with pubkeys already collected */
|
|
33
|
+
signatures: SignatureAndPubKey[];
|
|
34
|
+
/** Array of public keys that haven't signed yet */
|
|
35
|
+
unusedPubKeys: HexString[];
|
|
36
|
+
/** Minimum number of signatures required */
|
|
37
|
+
minSigners: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Parameters for creating a MultisigTransaction from existing signed transactions.
|
|
41
|
+
*/
|
|
42
|
+
type FromTransactionsParams = {
|
|
43
|
+
/** Array of signed transactions to extract signatures from */
|
|
44
|
+
txns: Transaction<unknown>[];
|
|
45
|
+
/** Minimum number of signatures required */
|
|
46
|
+
minSigners: number;
|
|
47
|
+
/** All public keys that are members of the multisig */
|
|
48
|
+
allPubKeys: HexString[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Represents a multisig transaction that collects signatures from multiple parties.
|
|
52
|
+
* Only supports nonce-based transactions and V0 transaction variants for signature collection.
|
|
53
|
+
*/
|
|
54
|
+
declare class MultisigTransaction {
|
|
55
|
+
private unsignedTx;
|
|
56
|
+
private signatures;
|
|
57
|
+
private minSigners;
|
|
58
|
+
private unusedPubKeys;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new MultisigTransaction instance.
|
|
61
|
+
* @param params - The multisig parameters including unsigned transaction, signatures, and public keys
|
|
62
|
+
*/
|
|
63
|
+
constructor({ unsignedTx, signatures, unusedPubKeys, minSigners, }: MultisigParams);
|
|
64
|
+
/**
|
|
65
|
+
* Creates an empty MultisigTransaction with no signatures collected yet.
|
|
66
|
+
* @param unsignedTx - The unsigned transaction to be signed by multiple parties
|
|
67
|
+
* @param minSigners - Minimum number of signatures required for completion
|
|
68
|
+
* @param allPubKeys - All public keys that are members of the multisig
|
|
69
|
+
* @returns A new empty MultisigTransaction instance
|
|
70
|
+
*/
|
|
71
|
+
static empty(unsignedTx: UnsignedTransaction<unknown>, minSigners: number, allPubKeys: HexString[]): MultisigTransaction;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a MultisigTransaction from an array of already-signed transactions.
|
|
74
|
+
* Extracts signatures and validates that all transactions are identical except for signatures.
|
|
75
|
+
* @param params - Parameters including the signed transactions, minimum signers, and all public keys
|
|
76
|
+
* @returns A new MultisigTransaction instance
|
|
77
|
+
* @throws {InvalidTransactionVersionError} If any transaction is not V0 variant
|
|
78
|
+
* @throws {TransactionMismatchError} If transactions don't match each other
|
|
79
|
+
* @throws {InvalidMultisigParameterError} If public keys are invalid or transaction is not nonce-based
|
|
80
|
+
*/
|
|
81
|
+
static fromTransactions({ txns, minSigners, allPubKeys, }: FromTransactionsParams): MultisigTransaction;
|
|
82
|
+
/**
|
|
83
|
+
* Adds a signature & public key from a signed transaction to the multisig transaction.
|
|
84
|
+
* @param tx - The signed transaction to extract signature from
|
|
85
|
+
* @throws {InvalidTransactionVersionError} If the transaction is not V0 variant
|
|
86
|
+
* @throws {TransactionMismatchError} If the transaction doesn't match the unsigned transaction
|
|
87
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
88
|
+
*/
|
|
89
|
+
addSignedTransaction(tx: Transaction<unknown>): void;
|
|
90
|
+
/**
|
|
91
|
+
* Adds a signature to the multisig transaction.
|
|
92
|
+
* @param signature - The signature to add
|
|
93
|
+
* @param pubKey - The public key corresponding to the signature
|
|
94
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
95
|
+
*/
|
|
96
|
+
addSignature(signature: HexString, pubKey: HexString): void;
|
|
97
|
+
/**
|
|
98
|
+
* Checks if the multisig has collected enough signatures to be complete.
|
|
99
|
+
* @returns True if the number of signatures meets or exceeds the minimum required
|
|
100
|
+
*/
|
|
101
|
+
get isComplete(): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Gets the unsigned transaction.
|
|
104
|
+
* @returns A readonly view of the unsigned transaction
|
|
105
|
+
*/
|
|
106
|
+
get unsignedTransaction(): Readonly<UnsignedTransaction<unknown>>;
|
|
107
|
+
/**
|
|
108
|
+
* Converts the multisig to a V1 transaction that can be submitted to the network.
|
|
109
|
+
* @returns A V1 transaction containing all collected signatures and unused public keys
|
|
110
|
+
*/
|
|
111
|
+
asTransaction(): Transaction<unknown>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { type FromTransactionsParams, InvalidMultisigParameterError, InvalidTransactionVersionError, MultisigError, type MultisigParams, MultisigTransaction, TransactionMismatchError };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { UnsignedTransaction, SignatureAndPubKey, Transaction } from '@sovereign-sdk/types';
|
|
2
|
+
import { HexString } from '@sovereign-sdk/utils';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for multisig-related errors.
|
|
6
|
+
*/
|
|
7
|
+
declare class MultisigError extends Error {
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Error thrown when a transaction has an unsupported version for multisig operations.
|
|
11
|
+
*/
|
|
12
|
+
declare class InvalidTransactionVersionError extends MultisigError {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Error thrown when a provided transaction doesn't match the expected unsigned transaction.
|
|
16
|
+
*/
|
|
17
|
+
declare class TransactionMismatchError extends Error {
|
|
18
|
+
constructor();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Error thrown when multisig is constructed or used with invalid parameters.
|
|
22
|
+
*/
|
|
23
|
+
declare class InvalidMultisigParameterError extends MultisigError {
|
|
24
|
+
constructor(desc: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parameters for constructing a MultisigTransaction.
|
|
28
|
+
*/
|
|
29
|
+
type MultisigParams = {
|
|
30
|
+
/** The unsigned transaction to be signed by multiple parties */
|
|
31
|
+
unsignedTx: UnsignedTransaction<unknown>;
|
|
32
|
+
/** Array of signatures with pubkeys already collected */
|
|
33
|
+
signatures: SignatureAndPubKey[];
|
|
34
|
+
/** Array of public keys that haven't signed yet */
|
|
35
|
+
unusedPubKeys: HexString[];
|
|
36
|
+
/** Minimum number of signatures required */
|
|
37
|
+
minSigners: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Parameters for creating a MultisigTransaction from existing signed transactions.
|
|
41
|
+
*/
|
|
42
|
+
type FromTransactionsParams = {
|
|
43
|
+
/** Array of signed transactions to extract signatures from */
|
|
44
|
+
txns: Transaction<unknown>[];
|
|
45
|
+
/** Minimum number of signatures required */
|
|
46
|
+
minSigners: number;
|
|
47
|
+
/** All public keys that are members of the multisig */
|
|
48
|
+
allPubKeys: HexString[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Represents a multisig transaction that collects signatures from multiple parties.
|
|
52
|
+
* Only supports nonce-based transactions and V0 transaction variants for signature collection.
|
|
53
|
+
*/
|
|
54
|
+
declare class MultisigTransaction {
|
|
55
|
+
private unsignedTx;
|
|
56
|
+
private signatures;
|
|
57
|
+
private minSigners;
|
|
58
|
+
private unusedPubKeys;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new MultisigTransaction instance.
|
|
61
|
+
* @param params - The multisig parameters including unsigned transaction, signatures, and public keys
|
|
62
|
+
*/
|
|
63
|
+
constructor({ unsignedTx, signatures, unusedPubKeys, minSigners, }: MultisigParams);
|
|
64
|
+
/**
|
|
65
|
+
* Creates an empty MultisigTransaction with no signatures collected yet.
|
|
66
|
+
* @param unsignedTx - The unsigned transaction to be signed by multiple parties
|
|
67
|
+
* @param minSigners - Minimum number of signatures required for completion
|
|
68
|
+
* @param allPubKeys - All public keys that are members of the multisig
|
|
69
|
+
* @returns A new empty MultisigTransaction instance
|
|
70
|
+
*/
|
|
71
|
+
static empty(unsignedTx: UnsignedTransaction<unknown>, minSigners: number, allPubKeys: HexString[]): MultisigTransaction;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a MultisigTransaction from an array of already-signed transactions.
|
|
74
|
+
* Extracts signatures and validates that all transactions are identical except for signatures.
|
|
75
|
+
* @param params - Parameters including the signed transactions, minimum signers, and all public keys
|
|
76
|
+
* @returns A new MultisigTransaction instance
|
|
77
|
+
* @throws {InvalidTransactionVersionError} If any transaction is not V0 variant
|
|
78
|
+
* @throws {TransactionMismatchError} If transactions don't match each other
|
|
79
|
+
* @throws {InvalidMultisigParameterError} If public keys are invalid or transaction is not nonce-based
|
|
80
|
+
*/
|
|
81
|
+
static fromTransactions({ txns, minSigners, allPubKeys, }: FromTransactionsParams): MultisigTransaction;
|
|
82
|
+
/**
|
|
83
|
+
* Adds a signature & public key from a signed transaction to the multisig transaction.
|
|
84
|
+
* @param tx - The signed transaction to extract signature from
|
|
85
|
+
* @throws {InvalidTransactionVersionError} If the transaction is not V0 variant
|
|
86
|
+
* @throws {TransactionMismatchError} If the transaction doesn't match the unsigned transaction
|
|
87
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
88
|
+
*/
|
|
89
|
+
addSignedTransaction(tx: Transaction<unknown>): void;
|
|
90
|
+
/**
|
|
91
|
+
* Adds a signature to the multisig transaction.
|
|
92
|
+
* @param signature - The signature to add
|
|
93
|
+
* @param pubKey - The public key corresponding to the signature
|
|
94
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
95
|
+
*/
|
|
96
|
+
addSignature(signature: HexString, pubKey: HexString): void;
|
|
97
|
+
/**
|
|
98
|
+
* Checks if the multisig has collected enough signatures to be complete.
|
|
99
|
+
* @returns True if the number of signatures meets or exceeds the minimum required
|
|
100
|
+
*/
|
|
101
|
+
get isComplete(): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Gets the unsigned transaction.
|
|
104
|
+
* @returns A readonly view of the unsigned transaction
|
|
105
|
+
*/
|
|
106
|
+
get unsignedTransaction(): Readonly<UnsignedTransaction<unknown>>;
|
|
107
|
+
/**
|
|
108
|
+
* Converts the multisig to a V1 transaction that can be submitted to the network.
|
|
109
|
+
* @returns A V1 transaction containing all collected signatures and unused public keys
|
|
110
|
+
*/
|
|
111
|
+
asTransaction(): Transaction<unknown>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { type FromTransactionsParams, InvalidMultisigParameterError, InvalidTransactionVersionError, MultisigError, type MultisigParams, MultisigTransaction, TransactionMismatchError };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
InvalidMultisigParameterError: () => InvalidMultisigParameterError,
|
|
24
|
+
InvalidTransactionVersionError: () => InvalidTransactionVersionError,
|
|
25
|
+
MultisigError: () => MultisigError,
|
|
26
|
+
MultisigTransaction: () => MultisigTransaction,
|
|
27
|
+
TransactionMismatchError: () => TransactionMismatchError
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
var MultisigError = class extends Error {
|
|
31
|
+
};
|
|
32
|
+
var InvalidTransactionVersionError = class extends MultisigError {
|
|
33
|
+
};
|
|
34
|
+
var TransactionMismatchError = class extends Error {
|
|
35
|
+
constructor() {
|
|
36
|
+
super(
|
|
37
|
+
"Provided transaction does not match the expected unsigned transaction"
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var InvalidMultisigParameterError = class extends MultisigError {
|
|
42
|
+
constructor(desc) {
|
|
43
|
+
super(`Multisig was constructed with invalid parameter: ${desc}`);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var MultisigTransaction = class _MultisigTransaction {
|
|
47
|
+
/**
|
|
48
|
+
* Creates a new MultisigTransaction instance.
|
|
49
|
+
* @param params - The multisig parameters including unsigned transaction, signatures, and public keys
|
|
50
|
+
*/
|
|
51
|
+
constructor({
|
|
52
|
+
unsignedTx,
|
|
53
|
+
signatures,
|
|
54
|
+
unusedPubKeys,
|
|
55
|
+
minSigners
|
|
56
|
+
}) {
|
|
57
|
+
this.signatures = [];
|
|
58
|
+
this.unusedPubKeys = /* @__PURE__ */ new Set();
|
|
59
|
+
this.unsignedTx = unsignedTx;
|
|
60
|
+
this.signatures = signatures;
|
|
61
|
+
this.unusedPubKeys = new Set(unusedPubKeys);
|
|
62
|
+
this.minSigners = minSigners;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Creates an empty MultisigTransaction with no signatures collected yet.
|
|
66
|
+
* @param unsignedTx - The unsigned transaction to be signed by multiple parties
|
|
67
|
+
* @param minSigners - Minimum number of signatures required for completion
|
|
68
|
+
* @param allPubKeys - All public keys that are members of the multisig
|
|
69
|
+
* @returns A new empty MultisigTransaction instance
|
|
70
|
+
*/
|
|
71
|
+
static empty(unsignedTx, minSigners, allPubKeys) {
|
|
72
|
+
return new _MultisigTransaction({
|
|
73
|
+
unsignedTx,
|
|
74
|
+
signatures: [],
|
|
75
|
+
unusedPubKeys: allPubKeys,
|
|
76
|
+
minSigners
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates a MultisigTransaction from an array of already-signed transactions.
|
|
81
|
+
* Extracts signatures and validates that all transactions are identical except for signatures.
|
|
82
|
+
* @param params - Parameters including the signed transactions, minimum signers, and all public keys
|
|
83
|
+
* @returns A new MultisigTransaction instance
|
|
84
|
+
* @throws {InvalidTransactionVersionError} If any transaction is not V0 variant
|
|
85
|
+
* @throws {TransactionMismatchError} If transactions don't match each other
|
|
86
|
+
* @throws {InvalidMultisigParameterError} If public keys are invalid or transaction is not nonce-based
|
|
87
|
+
*/
|
|
88
|
+
static fromTransactions({
|
|
89
|
+
txns,
|
|
90
|
+
minSigners,
|
|
91
|
+
allPubKeys
|
|
92
|
+
}) {
|
|
93
|
+
const signatures = [];
|
|
94
|
+
const unsignedTx = asUnsignedTransaction(txns[0]);
|
|
95
|
+
const unusedPubKeys = new Set(allPubKeys);
|
|
96
|
+
assertIsNonceBasedTx(unsignedTx);
|
|
97
|
+
for (const tx of txns) {
|
|
98
|
+
assertTxVariant(tx);
|
|
99
|
+
assertTxMatchesUnsignedTx(tx, unsignedTx);
|
|
100
|
+
const { pub_key, signature } = tx.V0;
|
|
101
|
+
if (!unusedPubKeys.delete(pub_key)) {
|
|
102
|
+
throw new InvalidMultisigParameterError(
|
|
103
|
+
`Public key is not a member of the multisig or has already signed: ${pub_key}`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
signatures.push({ pub_key, signature });
|
|
107
|
+
}
|
|
108
|
+
return new _MultisigTransaction({
|
|
109
|
+
unsignedTx,
|
|
110
|
+
signatures,
|
|
111
|
+
unusedPubKeys: Array.from(unusedPubKeys),
|
|
112
|
+
minSigners
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Adds a signature & public key from a signed transaction to the multisig transaction.
|
|
117
|
+
* @param tx - The signed transaction to extract signature from
|
|
118
|
+
* @throws {InvalidTransactionVersionError} If the transaction is not V0 variant
|
|
119
|
+
* @throws {TransactionMismatchError} If the transaction doesn't match the unsigned transaction
|
|
120
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
121
|
+
*/
|
|
122
|
+
addSignedTransaction(tx) {
|
|
123
|
+
assertTxVariant(tx);
|
|
124
|
+
assertTxMatchesUnsignedTx(tx, this.unsignedTx);
|
|
125
|
+
const { pub_key, signature } = tx.V0;
|
|
126
|
+
this.addSignature(signature, pub_key);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Adds a signature to the multisig transaction.
|
|
130
|
+
* @param signature - The signature to add
|
|
131
|
+
* @param pubKey - The public key corresponding to the signature
|
|
132
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
133
|
+
*/
|
|
134
|
+
addSignature(signature, pubKey) {
|
|
135
|
+
if (!this.unusedPubKeys.delete(pubKey)) {
|
|
136
|
+
throw new InvalidMultisigParameterError(
|
|
137
|
+
`Public key is not a member of the multisig or has already signed: ${pubKey}`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
this.signatures.push({ pub_key: pubKey, signature });
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Checks if the multisig has collected enough signatures to be complete.
|
|
144
|
+
* @returns True if the number of signatures meets or exceeds the minimum required
|
|
145
|
+
*/
|
|
146
|
+
get isComplete() {
|
|
147
|
+
return this.signatures.length >= this.minSigners;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Gets the unsigned transaction.
|
|
151
|
+
* @returns A readonly view of the unsigned transaction
|
|
152
|
+
*/
|
|
153
|
+
get unsignedTransaction() {
|
|
154
|
+
return this.unsignedTx;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Converts the multisig to a V1 transaction that can be submitted to the network.
|
|
158
|
+
* @returns A V1 transaction containing all collected signatures and unused public keys
|
|
159
|
+
*/
|
|
160
|
+
asTransaction() {
|
|
161
|
+
return {
|
|
162
|
+
V1: {
|
|
163
|
+
runtime_call: this.unsignedTx.runtime_call,
|
|
164
|
+
uniqueness: this.unsignedTx.uniqueness,
|
|
165
|
+
details: this.unsignedTx.details,
|
|
166
|
+
unused_pub_keys: Array.from(this.unusedPubKeys),
|
|
167
|
+
signatures: this.signatures,
|
|
168
|
+
min_signers: this.minSigners
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
function assertTxVariant(tx) {
|
|
174
|
+
if ("V1" in tx) {
|
|
175
|
+
throw new InvalidTransactionVersionError(
|
|
176
|
+
"Input transaction was an unsupported variant (V1)"
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function asUnsignedTransaction(tx) {
|
|
181
|
+
if ("V0" in tx) {
|
|
182
|
+
return {
|
|
183
|
+
runtime_call: tx.V0.runtime_call,
|
|
184
|
+
uniqueness: tx.V0.uniqueness,
|
|
185
|
+
details: tx.V0.details
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if ("V1" in tx) {
|
|
189
|
+
return {
|
|
190
|
+
runtime_call: tx.V1.runtime_call,
|
|
191
|
+
uniqueness: tx.V1.uniqueness,
|
|
192
|
+
details: tx.V1.details
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
throw new InvalidTransactionVersionError(
|
|
196
|
+
"Transaction variant is neither V0 nor V1"
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
function assertTxMatchesUnsignedTx(tx, unsignedTx) {
|
|
200
|
+
const txAsUnsigned = asUnsignedTransaction(tx);
|
|
201
|
+
if (JSON.stringify(txAsUnsigned) !== JSON.stringify(unsignedTx)) {
|
|
202
|
+
throw new TransactionMismatchError();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function assertIsNonceBasedTx(unsignedTx) {
|
|
206
|
+
if (!("nonce" in unsignedTx.uniqueness)) {
|
|
207
|
+
throw new InvalidMultisigParameterError(
|
|
208
|
+
"Only nonce-based transactions are supported for multisig"
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
213
|
+
0 && (module.exports = {
|
|
214
|
+
InvalidMultisigParameterError,
|
|
215
|
+
InvalidTransactionVersionError,
|
|
216
|
+
MultisigError,
|
|
217
|
+
MultisigTransaction,
|
|
218
|
+
TransactionMismatchError
|
|
219
|
+
});
|
|
220
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n SignatureAndPubKey,\n Transaction,\n TransactionV0,\n UnsignedTransaction,\n} from \"@sovereign-sdk/types\";\nimport type { HexString } from \"@sovereign-sdk/utils\";\n\n/**\n * Base error class for multisig-related errors.\n */\nexport class MultisigError extends Error {}\n\n/**\n * Error thrown when a transaction has an unsupported version for multisig operations.\n */\nexport class InvalidTransactionVersionError extends MultisigError {}\n\n/**\n * Error thrown when a provided transaction doesn't match the expected unsigned transaction.\n */\nexport class TransactionMismatchError extends Error {\n constructor() {\n super(\n \"Provided transaction does not match the expected unsigned transaction\",\n );\n }\n}\n\n/**\n * Error thrown when multisig is constructed or used with invalid parameters.\n */\nexport class InvalidMultisigParameterError extends MultisigError {\n constructor(desc: string) {\n super(`Multisig was constructed with invalid parameter: ${desc}`);\n }\n}\n\n/**\n * Parameters for constructing a MultisigTransaction.\n */\nexport type MultisigParams = {\n /** The unsigned transaction to be signed by multiple parties */\n unsignedTx: UnsignedTransaction<unknown>;\n /** Array of signatures with pubkeys already collected */\n signatures: SignatureAndPubKey[];\n /** Array of public keys that haven't signed yet */\n unusedPubKeys: HexString[];\n /** Minimum number of signatures required */\n minSigners: number;\n};\n\n/**\n * Parameters for creating a MultisigTransaction from existing signed transactions.\n */\nexport type FromTransactionsParams = {\n /** Array of signed transactions to extract signatures from */\n txns: Transaction<unknown>[];\n /** Minimum number of signatures required */\n minSigners: number;\n /** All public keys that are members of the multisig */\n allPubKeys: HexString[];\n};\n\n/**\n * Represents a multisig transaction that collects signatures from multiple parties.\n * Only supports nonce-based transactions and V0 transaction variants for signature collection.\n */\nexport class MultisigTransaction {\n private unsignedTx: UnsignedTransaction<unknown>;\n private signatures: SignatureAndPubKey[] = [];\n private minSigners: number;\n private unusedPubKeys = new Set<HexString>();\n\n /**\n * Creates a new MultisigTransaction instance.\n * @param params - The multisig parameters including unsigned transaction, signatures, and public keys\n */\n constructor({\n unsignedTx,\n signatures,\n unusedPubKeys,\n minSigners,\n }: MultisigParams) {\n this.unsignedTx = unsignedTx;\n this.signatures = signatures;\n this.unusedPubKeys = new Set(unusedPubKeys);\n this.minSigners = minSigners;\n }\n\n /**\n * Creates an empty MultisigTransaction with no signatures collected yet.\n * @param unsignedTx - The unsigned transaction to be signed by multiple parties\n * @param minSigners - Minimum number of signatures required for completion\n * @param allPubKeys - All public keys that are members of the multisig\n * @returns A new empty MultisigTransaction instance\n */\n static empty(\n unsignedTx: UnsignedTransaction<unknown>,\n minSigners: number,\n allPubKeys: HexString[],\n ): MultisigTransaction {\n return new MultisigTransaction({\n unsignedTx,\n signatures: [],\n unusedPubKeys: allPubKeys,\n minSigners,\n });\n }\n\n /**\n * Creates a MultisigTransaction from an array of already-signed transactions.\n * Extracts signatures and validates that all transactions are identical except for signatures.\n * @param params - Parameters including the signed transactions, minimum signers, and all public keys\n * @returns A new MultisigTransaction instance\n * @throws {InvalidTransactionVersionError} If any transaction is not V0 variant\n * @throws {TransactionMismatchError} If transactions don't match each other\n * @throws {InvalidMultisigParameterError} If public keys are invalid or transaction is not nonce-based\n */\n static fromTransactions({\n txns,\n minSigners,\n allPubKeys,\n }: FromTransactionsParams): MultisigTransaction {\n const signatures: SignatureAndPubKey[] = [];\n const unsignedTx = asUnsignedTransaction(txns[0]);\n const unusedPubKeys = new Set<HexString>(allPubKeys);\n\n assertIsNonceBasedTx(unsignedTx);\n\n for (const tx of txns) {\n assertTxVariant(tx);\n assertTxMatchesUnsignedTx(tx, unsignedTx);\n\n const { pub_key, signature } = tx.V0;\n\n if (!unusedPubKeys.delete(pub_key)) {\n throw new InvalidMultisigParameterError(\n `Public key is not a member of the multisig or has already signed: ${pub_key}`,\n );\n }\n\n signatures.push({ pub_key, signature });\n }\n\n return new MultisigTransaction({\n unsignedTx,\n signatures,\n unusedPubKeys: Array.from(unusedPubKeys),\n minSigners,\n });\n }\n\n /**\n * Adds a signature & public key from a signed transaction to the multisig transaction.\n * @param tx - The signed transaction to extract signature from\n * @throws {InvalidTransactionVersionError} If the transaction is not V0 variant\n * @throws {TransactionMismatchError} If the transaction doesn't match the unsigned transaction\n * @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed\n */\n addSignedTransaction(tx: Transaction<unknown>): void {\n assertTxVariant(tx);\n assertTxMatchesUnsignedTx(tx, this.unsignedTx);\n\n const { pub_key, signature } = tx.V0;\n\n this.addSignature(signature, pub_key);\n }\n\n /**\n * Adds a signature to the multisig transaction.\n * @param signature - The signature to add\n * @param pubKey - The public key corresponding to the signature\n * @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed\n */\n addSignature(signature: HexString, pubKey: HexString): void {\n if (!this.unusedPubKeys.delete(pubKey)) {\n throw new InvalidMultisigParameterError(\n `Public key is not a member of the multisig or has already signed: ${pubKey}`,\n );\n }\n\n this.signatures.push({ pub_key: pubKey, signature });\n }\n\n /**\n * Checks if the multisig has collected enough signatures to be complete.\n * @returns True if the number of signatures meets or exceeds the minimum required\n */\n get isComplete(): boolean {\n return this.signatures.length >= this.minSigners;\n }\n\n /**\n * Gets the unsigned transaction.\n * @returns A readonly view of the unsigned transaction\n */\n get unsignedTransaction(): Readonly<UnsignedTransaction<unknown>> {\n return this.unsignedTx;\n }\n\n /**\n * Converts the multisig to a V1 transaction that can be submitted to the network.\n * @returns A V1 transaction containing all collected signatures and unused public keys\n */\n asTransaction(): Transaction<unknown> {\n return {\n V1: {\n runtime_call: this.unsignedTx.runtime_call,\n uniqueness: this.unsignedTx.uniqueness,\n details: this.unsignedTx.details,\n unused_pub_keys: Array.from(this.unusedPubKeys),\n signatures: this.signatures,\n min_signers: this.minSigners,\n },\n };\n }\n}\n\nfunction assertTxVariant(\n tx: Transaction<unknown>,\n): asserts tx is TransactionV0<unknown> {\n if (\"V1\" in tx) {\n throw new InvalidTransactionVersionError(\n \"Input transaction was an unsupported variant (V1)\",\n );\n }\n}\n\nfunction asUnsignedTransaction(\n tx: Transaction<unknown>,\n): UnsignedTransaction<unknown> {\n if (\"V0\" in tx) {\n return {\n runtime_call: tx.V0.runtime_call,\n uniqueness: tx.V0.uniqueness,\n details: tx.V0.details,\n };\n }\n\n if (\"V1\" in tx) {\n return {\n runtime_call: tx.V1.runtime_call,\n uniqueness: tx.V1.uniqueness,\n details: tx.V1.details,\n };\n }\n\n throw new InvalidTransactionVersionError(\n \"Transaction variant is neither V0 nor V1\",\n );\n}\n\nfunction assertTxMatchesUnsignedTx(\n tx: Transaction<unknown>,\n unsignedTx: UnsignedTransaction<unknown>,\n): void {\n const txAsUnsigned = asUnsignedTransaction(tx);\n\n if (JSON.stringify(txAsUnsigned) !== JSON.stringify(unsignedTx)) {\n throw new TransactionMismatchError();\n }\n}\n\nfunction assertIsNonceBasedTx(unsignedTx: UnsignedTransaction<unknown>): void {\n if (!(\"nonce\" in unsignedTx.uniqueness)) {\n throw new InvalidMultisigParameterError(\n \"Only nonce-based transactions are supported for multisig\",\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,IAAM,gBAAN,cAA4B,MAAM;AAAC;AAKnC,IAAM,iCAAN,cAA6C,cAAc;AAAC;AAK5D,IAAM,2BAAN,cAAuC,MAAM;AAAA,EAClD,cAAc;AACZ;AAAA,MACE;AAAA,IACF;AAAA,EACF;AACF;AAKO,IAAM,gCAAN,cAA4C,cAAc;AAAA,EAC/D,YAAY,MAAc;AACxB,UAAM,oDAAoD,IAAI,EAAE;AAAA,EAClE;AACF;AAgCO,IAAM,sBAAN,MAAM,qBAAoB;AAAA;AAAA;AAAA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AA,IACjB;AAAA,EACF;AAEA,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;AAEA,SAAS,0BACP,IACA,YACM;AACN,QAAM,eAAe,sBAAsB,EAAE;AAE7C,MAAI,KAAK,UAAU,YAAY,MAAM,KAAK,UAAU,UAAU,GAAG;AAC/D,UAAM,IAAI,yBAAyB;AAAA,EACrC;AACF;AAEA,SAAS,qBAAqB,YAAgD;AAC5E,MAAI,EAAE,WAAW,WAAW,aAAa;AACvC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var MultisigError = class extends Error {
|
|
3
|
+
};
|
|
4
|
+
var InvalidTransactionVersionError = class extends MultisigError {
|
|
5
|
+
};
|
|
6
|
+
var TransactionMismatchError = class extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(
|
|
9
|
+
"Provided transaction does not match the expected unsigned transaction"
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var InvalidMultisigParameterError = class extends MultisigError {
|
|
14
|
+
constructor(desc) {
|
|
15
|
+
super(`Multisig was constructed with invalid parameter: ${desc}`);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var MultisigTransaction = class _MultisigTransaction {
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new MultisigTransaction instance.
|
|
21
|
+
* @param params - The multisig parameters including unsigned transaction, signatures, and public keys
|
|
22
|
+
*/
|
|
23
|
+
constructor({
|
|
24
|
+
unsignedTx,
|
|
25
|
+
signatures,
|
|
26
|
+
unusedPubKeys,
|
|
27
|
+
minSigners
|
|
28
|
+
}) {
|
|
29
|
+
this.signatures = [];
|
|
30
|
+
this.unusedPubKeys = /* @__PURE__ */ new Set();
|
|
31
|
+
this.unsignedTx = unsignedTx;
|
|
32
|
+
this.signatures = signatures;
|
|
33
|
+
this.unusedPubKeys = new Set(unusedPubKeys);
|
|
34
|
+
this.minSigners = minSigners;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates an empty MultisigTransaction with no signatures collected yet.
|
|
38
|
+
* @param unsignedTx - The unsigned transaction to be signed by multiple parties
|
|
39
|
+
* @param minSigners - Minimum number of signatures required for completion
|
|
40
|
+
* @param allPubKeys - All public keys that are members of the multisig
|
|
41
|
+
* @returns A new empty MultisigTransaction instance
|
|
42
|
+
*/
|
|
43
|
+
static empty(unsignedTx, minSigners, allPubKeys) {
|
|
44
|
+
return new _MultisigTransaction({
|
|
45
|
+
unsignedTx,
|
|
46
|
+
signatures: [],
|
|
47
|
+
unusedPubKeys: allPubKeys,
|
|
48
|
+
minSigners
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates a MultisigTransaction from an array of already-signed transactions.
|
|
53
|
+
* Extracts signatures and validates that all transactions are identical except for signatures.
|
|
54
|
+
* @param params - Parameters including the signed transactions, minimum signers, and all public keys
|
|
55
|
+
* @returns A new MultisigTransaction instance
|
|
56
|
+
* @throws {InvalidTransactionVersionError} If any transaction is not V0 variant
|
|
57
|
+
* @throws {TransactionMismatchError} If transactions don't match each other
|
|
58
|
+
* @throws {InvalidMultisigParameterError} If public keys are invalid or transaction is not nonce-based
|
|
59
|
+
*/
|
|
60
|
+
static fromTransactions({
|
|
61
|
+
txns,
|
|
62
|
+
minSigners,
|
|
63
|
+
allPubKeys
|
|
64
|
+
}) {
|
|
65
|
+
const signatures = [];
|
|
66
|
+
const unsignedTx = asUnsignedTransaction(txns[0]);
|
|
67
|
+
const unusedPubKeys = new Set(allPubKeys);
|
|
68
|
+
assertIsNonceBasedTx(unsignedTx);
|
|
69
|
+
for (const tx of txns) {
|
|
70
|
+
assertTxVariant(tx);
|
|
71
|
+
assertTxMatchesUnsignedTx(tx, unsignedTx);
|
|
72
|
+
const { pub_key, signature } = tx.V0;
|
|
73
|
+
if (!unusedPubKeys.delete(pub_key)) {
|
|
74
|
+
throw new InvalidMultisigParameterError(
|
|
75
|
+
`Public key is not a member of the multisig or has already signed: ${pub_key}`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
signatures.push({ pub_key, signature });
|
|
79
|
+
}
|
|
80
|
+
return new _MultisigTransaction({
|
|
81
|
+
unsignedTx,
|
|
82
|
+
signatures,
|
|
83
|
+
unusedPubKeys: Array.from(unusedPubKeys),
|
|
84
|
+
minSigners
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Adds a signature & public key from a signed transaction to the multisig transaction.
|
|
89
|
+
* @param tx - The signed transaction to extract signature from
|
|
90
|
+
* @throws {InvalidTransactionVersionError} If the transaction is not V0 variant
|
|
91
|
+
* @throws {TransactionMismatchError} If the transaction doesn't match the unsigned transaction
|
|
92
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
93
|
+
*/
|
|
94
|
+
addSignedTransaction(tx) {
|
|
95
|
+
assertTxVariant(tx);
|
|
96
|
+
assertTxMatchesUnsignedTx(tx, this.unsignedTx);
|
|
97
|
+
const { pub_key, signature } = tx.V0;
|
|
98
|
+
this.addSignature(signature, pub_key);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Adds a signature to the multisig transaction.
|
|
102
|
+
* @param signature - The signature to add
|
|
103
|
+
* @param pubKey - The public key corresponding to the signature
|
|
104
|
+
* @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed
|
|
105
|
+
*/
|
|
106
|
+
addSignature(signature, pubKey) {
|
|
107
|
+
if (!this.unusedPubKeys.delete(pubKey)) {
|
|
108
|
+
throw new InvalidMultisigParameterError(
|
|
109
|
+
`Public key is not a member of the multisig or has already signed: ${pubKey}`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
this.signatures.push({ pub_key: pubKey, signature });
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Checks if the multisig has collected enough signatures to be complete.
|
|
116
|
+
* @returns True if the number of signatures meets or exceeds the minimum required
|
|
117
|
+
*/
|
|
118
|
+
get isComplete() {
|
|
119
|
+
return this.signatures.length >= this.minSigners;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Gets the unsigned transaction.
|
|
123
|
+
* @returns A readonly view of the unsigned transaction
|
|
124
|
+
*/
|
|
125
|
+
get unsignedTransaction() {
|
|
126
|
+
return this.unsignedTx;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Converts the multisig to a V1 transaction that can be submitted to the network.
|
|
130
|
+
* @returns A V1 transaction containing all collected signatures and unused public keys
|
|
131
|
+
*/
|
|
132
|
+
asTransaction() {
|
|
133
|
+
return {
|
|
134
|
+
V1: {
|
|
135
|
+
runtime_call: this.unsignedTx.runtime_call,
|
|
136
|
+
uniqueness: this.unsignedTx.uniqueness,
|
|
137
|
+
details: this.unsignedTx.details,
|
|
138
|
+
unused_pub_keys: Array.from(this.unusedPubKeys),
|
|
139
|
+
signatures: this.signatures,
|
|
140
|
+
min_signers: this.minSigners
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
function assertTxVariant(tx) {
|
|
146
|
+
if ("V1" in tx) {
|
|
147
|
+
throw new InvalidTransactionVersionError(
|
|
148
|
+
"Input transaction was an unsupported variant (V1)"
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function asUnsignedTransaction(tx) {
|
|
153
|
+
if ("V0" in tx) {
|
|
154
|
+
return {
|
|
155
|
+
runtime_call: tx.V0.runtime_call,
|
|
156
|
+
uniqueness: tx.V0.uniqueness,
|
|
157
|
+
details: tx.V0.details
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if ("V1" in tx) {
|
|
161
|
+
return {
|
|
162
|
+
runtime_call: tx.V1.runtime_call,
|
|
163
|
+
uniqueness: tx.V1.uniqueness,
|
|
164
|
+
details: tx.V1.details
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
throw new InvalidTransactionVersionError(
|
|
168
|
+
"Transaction variant is neither V0 nor V1"
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
function assertTxMatchesUnsignedTx(tx, unsignedTx) {
|
|
172
|
+
const txAsUnsigned = asUnsignedTransaction(tx);
|
|
173
|
+
if (JSON.stringify(txAsUnsigned) !== JSON.stringify(unsignedTx)) {
|
|
174
|
+
throw new TransactionMismatchError();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function assertIsNonceBasedTx(unsignedTx) {
|
|
178
|
+
if (!("nonce" in unsignedTx.uniqueness)) {
|
|
179
|
+
throw new InvalidMultisigParameterError(
|
|
180
|
+
"Only nonce-based transactions are supported for multisig"
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
export {
|
|
185
|
+
InvalidMultisigParameterError,
|
|
186
|
+
InvalidTransactionVersionError,
|
|
187
|
+
MultisigError,
|
|
188
|
+
MultisigTransaction,
|
|
189
|
+
TransactionMismatchError
|
|
190
|
+
};
|
|
191
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n SignatureAndPubKey,\n Transaction,\n TransactionV0,\n UnsignedTransaction,\n} from \"@sovereign-sdk/types\";\nimport type { HexString } from \"@sovereign-sdk/utils\";\n\n/**\n * Base error class for multisig-related errors.\n */\nexport class MultisigError extends Error {}\n\n/**\n * Error thrown when a transaction has an unsupported version for multisig operations.\n */\nexport class InvalidTransactionVersionError extends MultisigError {}\n\n/**\n * Error thrown when a provided transaction doesn't match the expected unsigned transaction.\n */\nexport class TransactionMismatchError extends Error {\n constructor() {\n super(\n \"Provided transaction does not match the expected unsigned transaction\",\n );\n }\n}\n\n/**\n * Error thrown when multisig is constructed or used with invalid parameters.\n */\nexport class InvalidMultisigParameterError extends MultisigError {\n constructor(desc: string) {\n super(`Multisig was constructed with invalid parameter: ${desc}`);\n }\n}\n\n/**\n * Parameters for constructing a MultisigTransaction.\n */\nexport type MultisigParams = {\n /** The unsigned transaction to be signed by multiple parties */\n unsignedTx: UnsignedTransaction<unknown>;\n /** Array of signatures with pubkeys already collected */\n signatures: SignatureAndPubKey[];\n /** Array of public keys that haven't signed yet */\n unusedPubKeys: HexString[];\n /** Minimum number of signatures required */\n minSigners: number;\n};\n\n/**\n * Parameters for creating a MultisigTransaction from existing signed transactions.\n */\nexport type FromTransactionsParams = {\n /** Array of signed transactions to extract signatures from */\n txns: Transaction<unknown>[];\n /** Minimum number of signatures required */\n minSigners: number;\n /** All public keys that are members of the multisig */\n allPubKeys: HexString[];\n};\n\n/**\n * Represents a multisig transaction that collects signatures from multiple parties.\n * Only supports nonce-based transactions and V0 transaction variants for signature collection.\n */\nexport class MultisigTransaction {\n private unsignedTx: UnsignedTransaction<unknown>;\n private signatures: SignatureAndPubKey[] = [];\n private minSigners: number;\n private unusedPubKeys = new Set<HexString>();\n\n /**\n * Creates a new MultisigTransaction instance.\n * @param params - The multisig parameters including unsigned transaction, signatures, and public keys\n */\n constructor({\n unsignedTx,\n signatures,\n unusedPubKeys,\n minSigners,\n }: MultisigParams) {\n this.unsignedTx = unsignedTx;\n this.signatures = signatures;\n this.unusedPubKeys = new Set(unusedPubKeys);\n this.minSigners = minSigners;\n }\n\n /**\n * Creates an empty MultisigTransaction with no signatures collected yet.\n * @param unsignedTx - The unsigned transaction to be signed by multiple parties\n * @param minSigners - Minimum number of signatures required for completion\n * @param allPubKeys - All public keys that are members of the multisig\n * @returns A new empty MultisigTransaction instance\n */\n static empty(\n unsignedTx: UnsignedTransaction<unknown>,\n minSigners: number,\n allPubKeys: HexString[],\n ): MultisigTransaction {\n return new MultisigTransaction({\n unsignedTx,\n signatures: [],\n unusedPubKeys: allPubKeys,\n minSigners,\n });\n }\n\n /**\n * Creates a MultisigTransaction from an array of already-signed transactions.\n * Extracts signatures and validates that all transactions are identical except for signatures.\n * @param params - Parameters including the signed transactions, minimum signers, and all public keys\n * @returns A new MultisigTransaction instance\n * @throws {InvalidTransactionVersionError} If any transaction is not V0 variant\n * @throws {TransactionMismatchError} If transactions don't match each other\n * @throws {InvalidMultisigParameterError} If public keys are invalid or transaction is not nonce-based\n */\n static fromTransactions({\n txns,\n minSigners,\n allPubKeys,\n }: FromTransactionsParams): MultisigTransaction {\n const signatures: SignatureAndPubKey[] = [];\n const unsignedTx = asUnsignedTransaction(txns[0]);\n const unusedPubKeys = new Set<HexString>(allPubKeys);\n\n assertIsNonceBasedTx(unsignedTx);\n\n for (const tx of txns) {\n assertTxVariant(tx);\n assertTxMatchesUnsignedTx(tx, unsignedTx);\n\n const { pub_key, signature } = tx.V0;\n\n if (!unusedPubKeys.delete(pub_key)) {\n throw new InvalidMultisigParameterError(\n `Public key is not a member of the multisig or has already signed: ${pub_key}`,\n );\n }\n\n signatures.push({ pub_key, signature });\n }\n\n return new MultisigTransaction({\n unsignedTx,\n signatures,\n unusedPubKeys: Array.from(unusedPubKeys),\n minSigners,\n });\n }\n\n /**\n * Adds a signature & public key from a signed transaction to the multisig transaction.\n * @param tx - The signed transaction to extract signature from\n * @throws {InvalidTransactionVersionError} If the transaction is not V0 variant\n * @throws {TransactionMismatchError} If the transaction doesn't match the unsigned transaction\n * @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed\n */\n addSignedTransaction(tx: Transaction<unknown>): void {\n assertTxVariant(tx);\n assertTxMatchesUnsignedTx(tx, this.unsignedTx);\n\n const { pub_key, signature } = tx.V0;\n\n this.addSignature(signature, pub_key);\n }\n\n /**\n * Adds a signature to the multisig transaction.\n * @param signature - The signature to add\n * @param pubKey - The public key corresponding to the signature\n * @throws {InvalidMultisigParameterError} If the public key is not a member or has already signed\n */\n addSignature(signature: HexString, pubKey: HexString): void {\n if (!this.unusedPubKeys.delete(pubKey)) {\n throw new InvalidMultisigParameterError(\n `Public key is not a member of the multisig or has already signed: ${pubKey}`,\n );\n }\n\n this.signatures.push({ pub_key: pubKey, signature });\n }\n\n /**\n * Checks if the multisig has collected enough signatures to be complete.\n * @returns True if the number of signatures meets or exceeds the minimum required\n */\n get isComplete(): boolean {\n return this.signatures.length >= this.minSigners;\n }\n\n /**\n * Gets the unsigned transaction.\n * @returns A readonly view of the unsigned transaction\n */\n get unsignedTransaction(): Readonly<UnsignedTransaction<unknown>> {\n return this.unsignedTx;\n }\n\n /**\n * Converts the multisig to a V1 transaction that can be submitted to the network.\n * @returns A V1 transaction containing all collected signatures and unused public keys\n */\n asTransaction(): Transaction<unknown> {\n return {\n V1: {\n runtime_call: this.unsignedTx.runtime_call,\n uniqueness: this.unsignedTx.uniqueness,\n details: this.unsignedTx.details,\n unused_pub_keys: Array.from(this.unusedPubKeys),\n signatures: this.signatures,\n min_signers: this.minSigners,\n },\n };\n }\n}\n\nfunction assertTxVariant(\n tx: Transaction<unknown>,\n): asserts tx is TransactionV0<unknown> {\n if (\"V1\" in tx) {\n throw new InvalidTransactionVersionError(\n \"Input transaction was an unsupported variant (V1)\",\n );\n }\n}\n\nfunction asUnsignedTransaction(\n tx: Transaction<unknown>,\n): UnsignedTransaction<unknown> {\n if (\"V0\" in tx) {\n return {\n runtime_call: tx.V0.runtime_call,\n uniqueness: tx.V0.uniqueness,\n details: tx.V0.details,\n };\n }\n\n if (\"V1\" in tx) {\n return {\n runtime_call: tx.V1.runtime_call,\n uniqueness: tx.V1.uniqueness,\n details: tx.V1.details,\n };\n }\n\n throw new InvalidTransactionVersionError(\n \"Transaction variant is neither V0 nor V1\",\n );\n}\n\nfunction assertTxMatchesUnsignedTx(\n tx: Transaction<unknown>,\n unsignedTx: UnsignedTransaction<unknown>,\n): void {\n const txAsUnsigned = asUnsignedTransaction(tx);\n\n if (JSON.stringify(txAsUnsigned) !== JSON.stringify(unsignedTx)) {\n throw new TransactionMismatchError();\n }\n}\n\nfunction assertIsNonceBasedTx(unsignedTx: UnsignedTransaction<unknown>): void {\n if (!(\"nonce\" in unsignedTx.uniqueness)) {\n throw new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AAS,gBACP,IACsC;AACtC,MAAI,QAAQ,IAAI;AACd,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,sBACP,IAC8B;AAC9B,MAAI,QAAQ,IAAI;AACd,WAAO;AAAA,MACL,cAAc,GAAG,GAAG;AAAA,MACpB,YAAY,GAAG,GAAG;AAAA,MAClB,SAAS,GAAG,GAAG;AAAA,IACjB;AAAA,EACF;AAEA,MAAI,QAAQ,IAAI;AACd,WAAO;AAAA,MACL,cAAc,GAAG,GAAG;AAAA,MACpB,YAAY,GAAG,GAAG;AAAA,MAClB,SAAS,GAAG,GAAG;AAAA,IACjB;AAAA,EACF;AAEA,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;AAEA,SAAS,0BACP,IACA,YACM;AACN,QAAM,eAAe,sBAAsB,EAAE;AAE7C,MAAI,KAAK,UAAU,YAAY,MAAM,KAAK,UAAU,UAAU,GAAG;AAC/D,UAAM,IAAI,yBAAyB;AAAA,EACrC;AACF;AAEA,SAAS,qBAAqB,YAAgD;AAC5E,MAAI,EAAE,WAAW,WAAW,aAAa;AACvC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sovereign-sdk/multisig",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Multisig functionality for Sovereign SDK applications",
|
|
5
|
+
"repository": "github:Sovereign-Labs/sovereign-sdk-web3-js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"README.md",
|
|
9
|
+
"package.json"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"require": "./dist/index.js",
|
|
15
|
+
"import": "./dist/index.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"module": "./dist/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"tsup": "^8.3.0",
|
|
23
|
+
"typescript": "^5.6.3",
|
|
24
|
+
"vitest": "^3.0.5",
|
|
25
|
+
"@sovereign-sdk/types": "^0.1.2",
|
|
26
|
+
"@sovereign-sdk/web3": "^0.11.1"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"tsup": "^8.3.0",
|
|
30
|
+
"typescript": "^5.6.3"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@sovereign-sdk/utils": "^0.0.4",
|
|
37
|
+
"@sovereign-sdk/signers": "^0.3.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsup",
|
|
41
|
+
"ci": "biome ci",
|
|
42
|
+
"fix": "biome check --write",
|
|
43
|
+
"lint": "biome lint",
|
|
44
|
+
"test": "vitest **/*.test.ts",
|
|
45
|
+
"test:integration": "vitest **/*.integration-test.ts",
|
|
46
|
+
"typecheck": "tsc --noEmit"
|
|
47
|
+
}
|
|
48
|
+
}
|