@ocap/mpc-hsm 0.0.0-stage → 1.33.7
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/esm/bip39-english-wordlist.d.mts +11 -0
- package/esm/bip39-english-wordlist.mjs +31 -0
- package/esm/constants.d.mts +140 -0
- package/esm/constants.mjs +142 -0
- package/esm/dkg.d.mts +279 -0
- package/esm/dkg.mjs +903 -0
- package/esm/index.d.mts +17 -0
- package/esm/index.mjs +18 -0
- package/esm/mta.d.mts +63 -0
- package/esm/mta.mjs +275 -0
- package/esm/ocap-clear-signing.d.mts +125 -0
- package/esm/ocap-clear-signing.mjs +1170 -0
- package/esm/offline-ceremony.d.mts +127 -0
- package/esm/offline-ceremony.mjs +481 -0
- package/esm/ot.d.mts +193 -0
- package/esm/ot.mjs +726 -0
- package/esm/personal-vault.d.mts +1292 -0
- package/esm/personal-vault.mjs +3615 -0
- package/esm/personal.d.mts +10 -0
- package/esm/personal.mjs +11 -0
- package/esm/presign.d.mts +242 -0
- package/esm/presign.mjs +951 -0
- package/esm/rlp-eip1559.d.mts +75 -0
- package/esm/rlp-eip1559.mjs +293 -0
- package/esm/secp256k1-scalar.d.mts +141 -0
- package/esm/secp256k1-scalar.mjs +474 -0
- package/esm/sign.d.mts +113 -0
- package/esm/sign.mjs +225 -0
- package/esm/steel-mnemonic.d.mts +55 -0
- package/esm/steel-mnemonic.mjs +247 -0
- package/esm/tweak.d.mts +100 -0
- package/esm/tweak.mjs +168 -0
- package/esm/ves-backup.d.mts +296 -0
- package/esm/ves-backup.mjs +1025 -0
- package/esm/x402-agent-guard.d.mts +111 -0
- package/esm/x402-agent-guard.mjs +780 -0
- package/lib/bip39-english-wordlist.cjs +33 -0
- package/lib/bip39-english-wordlist.d.cts +11 -0
- package/lib/constants.cjs +264 -0
- package/lib/constants.d.cts +140 -0
- package/lib/dkg.cjs +910 -0
- package/lib/dkg.d.cts +279 -0
- package/lib/index.cjs +420 -0
- package/lib/index.d.cts +17 -0
- package/lib/mta.cjs +280 -0
- package/lib/mta.d.cts +63 -0
- package/lib/ocap-clear-signing.cjs +1176 -0
- package/lib/ocap-clear-signing.d.cts +125 -0
- package/lib/offline-ceremony.cjs +487 -0
- package/lib/offline-ceremony.d.cts +127 -0
- package/lib/ot.cjs +744 -0
- package/lib/ot.d.cts +193 -0
- package/lib/personal-vault.cjs +3725 -0
- package/lib/personal-vault.d.cts +1292 -0
- package/lib/personal.cjs +338 -0
- package/lib/personal.d.cts +10 -0
- package/lib/presign.cjs +954 -0
- package/lib/presign.d.cts +242 -0
- package/lib/rlp-eip1559.cjs +303 -0
- package/lib/rlp-eip1559.d.cts +75 -0
- package/lib/secp256k1-scalar.cjs +527 -0
- package/lib/secp256k1-scalar.d.cts +141 -0
- package/lib/sign.cjs +233 -0
- package/lib/sign.d.cts +113 -0
- package/lib/steel-mnemonic.cjs +253 -0
- package/lib/steel-mnemonic.d.cts +55 -0
- package/lib/tweak.cjs +178 -0
- package/lib/tweak.d.cts +100 -0
- package/lib/ves-backup.cjs +1053 -0
- package/lib/ves-backup.d.cts +296 -0
- package/lib/x402-agent-guard.cjs +788 -0
- package/lib/x402-agent-guard.d.cts +111 -0
- package/package.json +80 -4
- package/README.md +0 -3
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { LinearPedersenDleqProof, PedersenPokProof, RandomBytesFn, SchnorrPokProof } from "./secp256k1-scalar.cjs";
|
|
2
|
+
import { BaseOtReceiverState, BaseOtSenderState, OtExtStep1Message, OtExtStep2Message, OtExtStep3Message } from "./ot.cjs";
|
|
3
|
+
import { GadgetMtaPayload } from "./mta.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/presign.d.ts
|
|
6
|
+
interface MpcPublicClusterInfo {
|
|
7
|
+
readonly rootId: string;
|
|
8
|
+
readonly keyEpoch: bigint;
|
|
9
|
+
readonly t: number;
|
|
10
|
+
readonly nParties: number;
|
|
11
|
+
readonly hDkg32: Uint8Array;
|
|
12
|
+
readonly rootPublicKeyComp33: Uint8Array;
|
|
13
|
+
/** Map from 1-based party `slot` (`1..nParties`) to compressed 33-byte public share `X_slot`. */
|
|
14
|
+
readonly publicSharesComp33BySlot: ReadonlyMap<number, Uint8Array>;
|
|
15
|
+
}
|
|
16
|
+
interface MpcPartyBaseOtChannels {
|
|
17
|
+
readonly ownerSlot: number;
|
|
18
|
+
/** Used when `ownerSlot` acts as Extended Sender `A` against peer `receiverSlotB`. */
|
|
19
|
+
readonly asExtendedSenderA: Map<number, BaseOtReceiverState>;
|
|
20
|
+
/** Used when `ownerSlot` acts as Extended Receiver `B` against peer `senderSlotA`. */
|
|
21
|
+
readonly asExtendedReceiverB: Map<number, BaseOtSenderState>;
|
|
22
|
+
}
|
|
23
|
+
interface PresignRound1CommitmentMsg {
|
|
24
|
+
readonly slot: number;
|
|
25
|
+
readonly commitment32: Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
interface PresignRound1RevealMsg {
|
|
28
|
+
readonly slot: number;
|
|
29
|
+
readonly comSalt32: Uint8Array;
|
|
30
|
+
readonly gammaPointComp33: Uint8Array;
|
|
31
|
+
readonly bPointComp33: Uint8Array;
|
|
32
|
+
readonly ckPointComp33: Uint8Array;
|
|
33
|
+
readonly zetaEpk32: Uint8Array;
|
|
34
|
+
readonly pokGamma: SchnorrPokProof;
|
|
35
|
+
readonly pokB: SchnorrPokProof;
|
|
36
|
+
readonly pokCk: PedersenPokProof;
|
|
37
|
+
}
|
|
38
|
+
type MtaInstanceKind = 'k_gamma' | 'k_b';
|
|
39
|
+
interface PresignMtaOtStep1Bundle {
|
|
40
|
+
readonly fromReceiverSlotB: number;
|
|
41
|
+
readonly toSenderSlotA: number;
|
|
42
|
+
readonly kGammaOt1: OtExtStep1Message;
|
|
43
|
+
readonly kBOt1: OtExtStep1Message;
|
|
44
|
+
}
|
|
45
|
+
interface PresignMtaOtStep2Bundle {
|
|
46
|
+
readonly fromSenderSlotA: number;
|
|
47
|
+
readonly toReceiverSlotB: number;
|
|
48
|
+
readonly kGammaOt2: OtExtStep2Message;
|
|
49
|
+
readonly kBOt2: OtExtStep2Message;
|
|
50
|
+
}
|
|
51
|
+
interface PresignMtaOtStep3Bundle {
|
|
52
|
+
readonly fromReceiverSlotB: number;
|
|
53
|
+
readonly toSenderSlotA: number;
|
|
54
|
+
readonly kGammaOt3: OtExtStep3Message;
|
|
55
|
+
readonly kBOt3: OtExtStep3Message;
|
|
56
|
+
}
|
|
57
|
+
interface PresignMtaStep4PayloadBundle {
|
|
58
|
+
readonly fromSenderSlotA: number;
|
|
59
|
+
readonly toReceiverSlotB: number;
|
|
60
|
+
readonly kGammaMta: GadgetMtaPayload;
|
|
61
|
+
readonly kBMta: GadgetMtaPayload;
|
|
62
|
+
}
|
|
63
|
+
interface PresignRound3BroadcastMsg {
|
|
64
|
+
readonly slot: number;
|
|
65
|
+
readonly deltaShareBytes32: Uint8Array;
|
|
66
|
+
readonly eShareBytes32: Uint8Array;
|
|
67
|
+
}
|
|
68
|
+
interface PresignRound4BroadcastMsg {
|
|
69
|
+
readonly slot: number;
|
|
70
|
+
readonly kSharePointComp33: Uint8Array;
|
|
71
|
+
readonly zSharePointComp33: Uint8Array;
|
|
72
|
+
readonly piLinK: LinearPedersenDleqProof;
|
|
73
|
+
readonly piPokZ: SchnorrPokProof;
|
|
74
|
+
}
|
|
75
|
+
interface PresignaturePublicVerification {
|
|
76
|
+
readonly presigId32: Uint8Array;
|
|
77
|
+
readonly presigIdHex: string;
|
|
78
|
+
readonly rootId: string;
|
|
79
|
+
readonly keyEpoch: bigint;
|
|
80
|
+
readonly subset: ReadonlyArray<number>;
|
|
81
|
+
readonly hDkg32: Uint8Array;
|
|
82
|
+
readonly rootPublicKeyComp33: Uint8Array;
|
|
83
|
+
readonly rPointComp33: Uint8Array;
|
|
84
|
+
readonly kPointsComp33BySlot: ReadonlyMap<number, Uint8Array>;
|
|
85
|
+
readonly zPointsComp33BySlot: ReadonlyMap<number, Uint8Array>;
|
|
86
|
+
}
|
|
87
|
+
interface PresignatureShareRecord {
|
|
88
|
+
readonly slot: number;
|
|
89
|
+
readonly publicVerification: PresignaturePublicVerification;
|
|
90
|
+
/** Zeroized immediately upon consumption or abort/panic wipe. */
|
|
91
|
+
readonly kShareBytes32: Uint8Array;
|
|
92
|
+
/** Zeroized immediately upon consumption or abort/panic wipe. */
|
|
93
|
+
readonly chiShareBytes32: Uint8Array;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Computes the canonical subset-bound `presigId` (`design.md` §4.5(1)):
|
|
97
|
+
* `SHA256("ARC-MPC-PRESIG-ID-V1" || LP(rootId) || u64be(keyEpoch) || sortedSubsetBytes(S) || H_dkg || Com^{pre}_{i_1..i_t})`.
|
|
98
|
+
*/
|
|
99
|
+
declare function computeSubsetBoundPresigId(params: {
|
|
100
|
+
rootId: string;
|
|
101
|
+
keyEpoch: bigint;
|
|
102
|
+
subset: ReadonlyArray<number>;
|
|
103
|
+
hDkg32: Uint8Array;
|
|
104
|
+
orderedCommitments32: ReadonlyArray<Uint8Array>;
|
|
105
|
+
}): Uint8Array;
|
|
106
|
+
/**
|
|
107
|
+
* Stateful single-party 4-round Beaver-masked Presignature participant (`design.md` §4.5(1)).
|
|
108
|
+
* Enforces strict phase ordering, non-reuse of `b_i`, and immediate zeroization of all single-use
|
|
109
|
+
* ephemeral scalars upon completion or any abort!
|
|
110
|
+
*/
|
|
111
|
+
declare class PresignPartySession {
|
|
112
|
+
readonly slot: number;
|
|
113
|
+
readonly cluster: MpcPublicClusterInfo;
|
|
114
|
+
readonly subset: ReadonlyArray<number>;
|
|
115
|
+
readonly sessionNonce32: Uint8Array;
|
|
116
|
+
private readonly baseOtChannels;
|
|
117
|
+
private readonly rng;
|
|
118
|
+
private phase;
|
|
119
|
+
private readonly xTildeBytes32;
|
|
120
|
+
private readonly kBytes32;
|
|
121
|
+
private readonly gammaBytes32;
|
|
122
|
+
private readonly bBytes32;
|
|
123
|
+
private readonly rhoKBytes32;
|
|
124
|
+
private readonly zetaEsk32;
|
|
125
|
+
private readonly cShareBytes32;
|
|
126
|
+
private readonly deltaShareBytes32;
|
|
127
|
+
private readonly chiShareBytes32;
|
|
128
|
+
private ownRevealMsg;
|
|
129
|
+
private round1CommitmentsBySlot;
|
|
130
|
+
private round1RevealsBySlot;
|
|
131
|
+
private linearizedPublicSharesBySlot;
|
|
132
|
+
private presigId32;
|
|
133
|
+
private aggGammaPoint;
|
|
134
|
+
private rPoint;
|
|
135
|
+
private pendingOtReceiverByPeer;
|
|
136
|
+
private pendingOtSenderByPeer;
|
|
137
|
+
private verifiedOtReceiverByPeer;
|
|
138
|
+
private senderMtaGammaAcc;
|
|
139
|
+
private senderMtaBAcc;
|
|
140
|
+
constructor(params: {
|
|
141
|
+
slot: number;
|
|
142
|
+
nodeSecretShareBytes32: Uint8Array;
|
|
143
|
+
cluster: MpcPublicClusterInfo;
|
|
144
|
+
subset: ReadonlyArray<number>;
|
|
145
|
+
sessionNonce32: Uint8Array;
|
|
146
|
+
baseOtChannels: MpcPartyBaseOtChannels;
|
|
147
|
+
rng?: RandomBytesFn;
|
|
148
|
+
});
|
|
149
|
+
get isAborted(): boolean;
|
|
150
|
+
abort(): void;
|
|
151
|
+
private wipeAllEphemeralSecrets;
|
|
152
|
+
private assertPhase;
|
|
153
|
+
private pokContextParts;
|
|
154
|
+
/**
|
|
155
|
+
* Round 1a: Generates hash commitment `Com^{pre}_i` over `(\Gamma_i, B_i, C_{k,i}, \pi_\Gamma, \pi_B, \pi_{C_k})`.
|
|
156
|
+
*/
|
|
157
|
+
round1CreateCommitment(): PresignRound1CommitmentMsg;
|
|
158
|
+
/**
|
|
159
|
+
* Round 1b: Locks all `t` commitments, derives subset-bound `presigId`, and reveals `Reveal^{pre}_i`.
|
|
160
|
+
*/
|
|
161
|
+
round1ReceiveCommitmentsAndReveal(commitments: ReadonlyArray<PresignRound1CommitmentMsg>): {
|
|
162
|
+
presigId32: Uint8Array;
|
|
163
|
+
reveal: PresignRound1RevealMsg;
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Verifies all `t` Round-1 reveals & PoKs, attributing `culpritSlot` on any invalid commitment or PoK.
|
|
167
|
+
*/
|
|
168
|
+
round1VerifyAllReveals(reveals: ReadonlyArray<PresignRound1RevealMsg>): void;
|
|
169
|
+
/**
|
|
170
|
+
* Round 2 Step 1 (acting as Extended Receiver `B` for `(\gamma_i, b_i)` against each peer `j != i`):
|
|
171
|
+
* Encodes `\gamma_i` and `b_i` via LHL NUMS gadget (`768` bits + `256` sacrifice bits) and emits
|
|
172
|
+
* `OT-Ext Step 1` columns (`\tilde{x}_i` NEVER enters OT!).
|
|
173
|
+
*/
|
|
174
|
+
round2CreateOtStep1Messages(): PresignMtaOtStep1Bundle[];
|
|
175
|
+
/**
|
|
176
|
+
* Round 2 Step 2 (acting as Extended Sender `A` against each peer `j != i`):
|
|
177
|
+
* Transposes `Q` and issues coin-toss `xiSeed32` challenges BEFORE touching `k_i`.
|
|
178
|
+
*/
|
|
179
|
+
round2ProcessOtStep1AndCreateStep2(incomingStep1: ReadonlyArray<PresignMtaOtStep1Bundle>): PresignMtaOtStep2Bundle[];
|
|
180
|
+
/**
|
|
181
|
+
* Round 2 Step 3 (acting as Extended Receiver `B`):
|
|
182
|
+
* Emits `KOS15` correlation proofs `(x, T_chk)`.
|
|
183
|
+
*/
|
|
184
|
+
round2ProcessOtStep2AndCreateStep3(incomingStep2: ReadonlyArray<PresignMtaOtStep2Bundle>): PresignMtaOtStep3Bundle[];
|
|
185
|
+
/**
|
|
186
|
+
* Round 2 Step 4a (acting as Extended Sender `A`):
|
|
187
|
+
* Runs `KOS15` check strictly BEFORE computing single-row dual-pad `GadgetMtA` messages
|
|
188
|
+
* anchored to Round-1 `C_{k,i}`.
|
|
189
|
+
*/
|
|
190
|
+
round2VerifyKos15AndCreateMtaPayloads(incomingStep3: ReadonlyArray<PresignMtaOtStep3Bundle>): PresignMtaStep4PayloadBundle[];
|
|
191
|
+
/**
|
|
192
|
+
* Round 2 Step 4b (acting as Extended Receiver `B`):
|
|
193
|
+
* Verifies `C_alpha === C_{k,j}`, Pedersen openings, and all 768 single-row dual-pad checks,
|
|
194
|
+
* then accumulates `\delta_i` and `c_i`.
|
|
195
|
+
*/
|
|
196
|
+
round2VerifyMtaPayloads(incomingMta: ReadonlyArray<PresignMtaStep4PayloadBundle>): void;
|
|
197
|
+
/**
|
|
198
|
+
* Round 3a: Reveals `(\delta_i, e_i = \tilde{x}_i - b_i mod n)` and IMMEDIATELY zeroizes
|
|
199
|
+
* `b_i` and `\tilde{x}_i` so `b_i` can never be reused across any abort or retry!
|
|
200
|
+
*/
|
|
201
|
+
round3CreateBroadcast(): PresignRound3BroadcastMsg;
|
|
202
|
+
/**
|
|
203
|
+
* Round 3b: Verifies `e_j * G == \tilde{X}_j - B_j` (attributing exact `culpritSlot = j` on
|
|
204
|
+
* corruption), computes `R = \delta^{-1} \Gamma` (`R != O && R_x < n`), and derives `\chi_i`
|
|
205
|
+
* with pairwise cancelling `\zeta_{i,j} = -\zeta_{j,i} mod n`.
|
|
206
|
+
*/
|
|
207
|
+
round3VerifyAndCreateRound4Broadcast(round3Messages: ReadonlyArray<PresignRound3BroadcastMsg>): PresignRound4BroadcastMsg;
|
|
208
|
+
/**
|
|
209
|
+
* Round 4b: Verifies `\pi^{lin}_{K,j}` and `\pi^{pok}_{Z,j}` in base `R` for every `j \in S`
|
|
210
|
+
* (attributing exact `culpritSlot = j` on invalid proof), checks `\sum K_j == G` and
|
|
211
|
+
* `\sum Z_j == P_root`, and outputs the single-use `PresignatureShareRecord`.
|
|
212
|
+
*/
|
|
213
|
+
round4VerifyAndFinalize(round4Messages: ReadonlyArray<PresignRound4BroadcastMsg>): PresignatureShareRecord;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Volatile in-RAM single-use presignature pool (`design.md` §4.5(1), §7):
|
|
217
|
+
* - Synchronous `consume-before-use` atomic lock: under concurrent `Promise.all` contention,
|
|
218
|
+
* exact 1 caller acquires the `PresignatureShareRecord` and all others synchronously fail.
|
|
219
|
+
* - Strict `requestedSubset === presig.S` enforcement.
|
|
220
|
+
* - `wipeRoot(rootId)` / `wipeAll()` zeroize all RAM presignature scalars on Panic or shutdown.
|
|
221
|
+
*/
|
|
222
|
+
declare class VolatilePresignaturePool {
|
|
223
|
+
private readonly activeByPresigIdHex;
|
|
224
|
+
private readonly tombstonedPresigIdHexes;
|
|
225
|
+
get size(): number;
|
|
226
|
+
isTombstoned(presigId: Uint8Array | string): boolean;
|
|
227
|
+
store(record: PresignatureShareRecord): void;
|
|
228
|
+
/**
|
|
229
|
+
* Synchronously validates `(rootId, requestedSubset)` and atomically removes + tombstones
|
|
230
|
+
* `presigId` in a single JS turn with zero `await` points.
|
|
231
|
+
*/
|
|
232
|
+
consume(params: {
|
|
233
|
+
rootId: string;
|
|
234
|
+
presigId: Uint8Array | string;
|
|
235
|
+
requestedSubset: ReadonlyArray<number>;
|
|
236
|
+
preValidateRecord?: (record: PresignatureShareRecord) => void;
|
|
237
|
+
}): PresignatureShareRecord;
|
|
238
|
+
wipeRoot(rootId: string): number;
|
|
239
|
+
wipeAll(): number;
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
242
|
+
export { MpcPartyBaseOtChannels, MpcPublicClusterInfo, MtaInstanceKind, PresignMtaOtStep1Bundle, PresignMtaOtStep2Bundle, PresignMtaOtStep3Bundle, PresignMtaStep4PayloadBundle, PresignPartySession, PresignRound1CommitmentMsg, PresignRound1RevealMsg, PresignRound3BroadcastMsg, PresignRound4BroadcastMsg, PresignaturePublicVerification, PresignatureShareRecord, VolatilePresignaturePool, computeSubsetBoundPresigId };
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_secp256k1_scalar = require('./secp256k1-scalar.cjs');
|
|
3
|
+
const require_tweak = require('./tweak.cjs');
|
|
4
|
+
let _noble_hashes_sha3_js = require("@noble/hashes/sha3.js");
|
|
5
|
+
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
6
|
+
|
|
7
|
+
//#region src/rlp-eip1559.ts
|
|
8
|
+
const EIP1559_TX_TYPE_BYTE = 2;
|
|
9
|
+
const ERC20_TRANSFER_SELECTOR_HEX = "a9059cbb";
|
|
10
|
+
const MAX_UINT64 = (1n << 64n) - 1n;
|
|
11
|
+
const MAX_UINT256 = (1n << 256n) - 1n;
|
|
12
|
+
function bigintToMinimalBytesBE(value, label = "uint") {
|
|
13
|
+
if (typeof value !== "bigint" || value < 0n || value > MAX_UINT256) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `${label} must satisfy 0 <= v <= 2^256 - 1`);
|
|
14
|
+
if (value === 0n) return new Uint8Array(0);
|
|
15
|
+
const bytes = [];
|
|
16
|
+
let rem = value;
|
|
17
|
+
while (rem > 0n) {
|
|
18
|
+
bytes.push(Number(rem & 255n));
|
|
19
|
+
rem >>= 8n;
|
|
20
|
+
}
|
|
21
|
+
bytes.reverse();
|
|
22
|
+
return Uint8Array.from(bytes);
|
|
23
|
+
}
|
|
24
|
+
function encodeLengthPrefix(length, offsetBase) {
|
|
25
|
+
if (length < 56) return Uint8Array.of(offsetBase + length);
|
|
26
|
+
const lenBytes = bigintToMinimalBytesBE(BigInt(length), "rlpLength");
|
|
27
|
+
return require_secp256k1_scalar.concatBytes(Uint8Array.of(offsetBase + 55 + lenBytes.length), lenBytes);
|
|
28
|
+
}
|
|
29
|
+
function rlpEncode(item) {
|
|
30
|
+
if (item instanceof Uint8Array) {
|
|
31
|
+
if (item.length === 1 && item[0] <= 127) return new Uint8Array(item);
|
|
32
|
+
return require_secp256k1_scalar.concatBytes(encodeLengthPrefix(item.length, 128), item);
|
|
33
|
+
}
|
|
34
|
+
if (Array.isArray(item)) {
|
|
35
|
+
const payload = require_secp256k1_scalar.concatBytes(...item.map((child) => rlpEncode(child)));
|
|
36
|
+
return require_secp256k1_scalar.concatBytes(encodeLengthPrefix(payload.length, 192), payload);
|
|
37
|
+
}
|
|
38
|
+
throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Unsupported RLP item type");
|
|
39
|
+
}
|
|
40
|
+
const MAX_RLP_DECODE_DEPTH = 4;
|
|
41
|
+
function rlpDecodeItem(buf, offset, depth = 0) {
|
|
42
|
+
if (depth > MAX_RLP_DECODE_DEPTH) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `RLP nesting depth exceeds maximum (${MAX_RLP_DECODE_DEPTH})`);
|
|
43
|
+
if (offset >= buf.length) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Unexpected EOF during RLP decode");
|
|
44
|
+
const prefix = buf[offset];
|
|
45
|
+
if (prefix <= 127) return {
|
|
46
|
+
item: buf.subarray(offset, offset + 1),
|
|
47
|
+
nextOffset: offset + 1
|
|
48
|
+
};
|
|
49
|
+
if (prefix <= 183) {
|
|
50
|
+
const strLen = prefix - 128;
|
|
51
|
+
const start = offset + 1;
|
|
52
|
+
const end$1 = start + strLen;
|
|
53
|
+
if (end$1 > buf.length) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Truncated short RLP byte string");
|
|
54
|
+
if (strLen === 1 && buf[start] <= 127) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Non-canonical single-byte RLP string");
|
|
55
|
+
return {
|
|
56
|
+
item: buf.subarray(start, end$1),
|
|
57
|
+
nextOffset: end$1
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (prefix <= 191) {
|
|
61
|
+
const lenOfLen$1 = prefix - 183;
|
|
62
|
+
const lenStart$1 = offset + 1;
|
|
63
|
+
const lenEnd$1 = lenStart$1 + lenOfLen$1;
|
|
64
|
+
if (lenEnd$1 > buf.length || buf[lenStart$1] === 0) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Malformed long RLP string length");
|
|
65
|
+
const strLen = Number(require_secp256k1_scalar.bytesToBigIntBE(buf.subarray(lenStart$1, lenEnd$1)));
|
|
66
|
+
if (!Number.isSafeInteger(strLen) || strLen < 56 || lenEnd$1 + strLen > buf.length) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or truncated long RLP string");
|
|
67
|
+
return {
|
|
68
|
+
item: buf.subarray(lenEnd$1, lenEnd$1 + strLen),
|
|
69
|
+
nextOffset: lenEnd$1 + strLen
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (prefix <= 247) {
|
|
73
|
+
const listLen$1 = prefix - 192;
|
|
74
|
+
let cur$1 = offset + 1;
|
|
75
|
+
const end$1 = cur$1 + listLen$1;
|
|
76
|
+
if (end$1 > buf.length) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Truncated short RLP list");
|
|
77
|
+
const list$1 = [];
|
|
78
|
+
while (cur$1 < end$1) {
|
|
79
|
+
const child = rlpDecodeItem(buf, cur$1, depth + 1);
|
|
80
|
+
list$1.push(child.item);
|
|
81
|
+
cur$1 = child.nextOffset;
|
|
82
|
+
}
|
|
83
|
+
if (cur$1 !== end$1) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Misaligned short RLP list boundary");
|
|
84
|
+
return {
|
|
85
|
+
item: list$1,
|
|
86
|
+
nextOffset: end$1
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const lenOfLen = prefix - 247;
|
|
90
|
+
const lenStart = offset + 1;
|
|
91
|
+
const lenEnd = lenStart + lenOfLen;
|
|
92
|
+
if (lenEnd > buf.length || buf[lenStart] === 0) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Malformed long RLP list length");
|
|
93
|
+
const listLen = Number(require_secp256k1_scalar.bytesToBigIntBE(buf.subarray(lenStart, lenEnd)));
|
|
94
|
+
if (!Number.isSafeInteger(listLen) || listLen < 56 || lenEnd + listLen > buf.length) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or truncated long RLP list");
|
|
95
|
+
let cur = lenEnd;
|
|
96
|
+
const end = lenEnd + listLen;
|
|
97
|
+
const list = [];
|
|
98
|
+
while (cur < end) {
|
|
99
|
+
const child = rlpDecodeItem(buf, cur, depth + 1);
|
|
100
|
+
list.push(child.item);
|
|
101
|
+
cur = child.nextOffset;
|
|
102
|
+
}
|
|
103
|
+
if (cur !== end) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Misaligned long RLP list boundary");
|
|
104
|
+
return {
|
|
105
|
+
item: list,
|
|
106
|
+
nextOffset: end
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function rlpDecode(bytes) {
|
|
110
|
+
const { item, nextOffset } = rlpDecodeItem(bytes, 0);
|
|
111
|
+
if (nextOffset !== bytes.length) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Trailing bytes after RLP item");
|
|
112
|
+
return item;
|
|
113
|
+
}
|
|
114
|
+
function canonicalRlpBytesToBigInt(bytes, label) {
|
|
115
|
+
if (bytes.length > 32) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `${label} exceeds 32 bytes`);
|
|
116
|
+
if (bytes.length > 0 && bytes[0] === 0) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `Non-canonical leading zero in ${label}`);
|
|
117
|
+
return require_secp256k1_scalar.bytesToBigIntBE(bytes);
|
|
118
|
+
}
|
|
119
|
+
function validateAndBuildCoreRlpFields(tx) {
|
|
120
|
+
if (typeof tx.chainId !== "bigint" || tx.chainId < 1n || tx.chainId > MAX_UINT64) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid chainId: ${String(tx.chainId)}`);
|
|
121
|
+
if (typeof tx.nonce !== "bigint" || tx.nonce < 0n || tx.nonce >= MAX_UINT64) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid nonce: ${String(tx.nonce)}`);
|
|
122
|
+
if (typeof tx.maxPriorityFeePerGasWei !== "bigint" || typeof tx.maxFeePerGasWei !== "bigint" || tx.maxPriorityFeePerGasWei < 0n || tx.maxFeePerGasWei < 0n || tx.maxFeePerGasWei > MAX_UINT256 || tx.maxFeePerGasWei < tx.maxPriorityFeePerGasWei) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid EIP-1559 fee parameters (require 0 <= maxPriorityFeePerGasWei <= maxFeePerGasWei)");
|
|
123
|
+
if (typeof tx.gasLimit !== "bigint" || tx.gasLimit < 21000n || tx.gasLimit > MAX_UINT64) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid gasLimit: ${String(tx.gasLimit)}`);
|
|
124
|
+
if (typeof tx.valueWei !== "bigint" || tx.valueWei < 0n || tx.valueWei > MAX_UINT256) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid valueWei: ${String(tx.valueWei)}`);
|
|
125
|
+
const parsedTo = require_tweak.parseStrictEthAddress(tx.to);
|
|
126
|
+
const dataBytes = tx.data === void 0 || tx.data === "0x" || tx.data === "" ? new Uint8Array(0) : require_secp256k1_scalar.normalizeHexBytes(tx.data);
|
|
127
|
+
const encodedAccessList = (tx.accessList ?? []).map((entry) => {
|
|
128
|
+
const addr = require_tweak.parseStrictEthAddress(entry.address);
|
|
129
|
+
const keys = entry.storageKeys.map((k) => require_secp256k1_scalar.normalizeHexBytes(k, 32));
|
|
130
|
+
return [addr.addressBytes20, keys];
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
coreFields: [
|
|
134
|
+
bigintToMinimalBytesBE(tx.chainId, "chainId"),
|
|
135
|
+
bigintToMinimalBytesBE(tx.nonce, "nonce"),
|
|
136
|
+
bigintToMinimalBytesBE(tx.maxPriorityFeePerGasWei, "maxPriorityFeePerGasWei"),
|
|
137
|
+
bigintToMinimalBytesBE(tx.maxFeePerGasWei, "maxFeePerGasWei"),
|
|
138
|
+
bigintToMinimalBytesBE(tx.gasLimit, "gasLimit"),
|
|
139
|
+
parsedTo.addressBytes20,
|
|
140
|
+
bigintToMinimalBytesBE(tx.valueWei, "valueWei"),
|
|
141
|
+
dataBytes,
|
|
142
|
+
encodedAccessList
|
|
143
|
+
],
|
|
144
|
+
normalizedToChecksum: parsedTo.checksumAddress,
|
|
145
|
+
dataBytes
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function encodeUnsignedEip1559Tx(tx) {
|
|
149
|
+
const { coreFields, normalizedToChecksum } = validateAndBuildCoreRlpFields(tx);
|
|
150
|
+
const unsignedPayloadBytes = require_secp256k1_scalar.concatBytes(require_secp256k1_scalar.u8(EIP1559_TX_TYPE_BYTE), rlpEncode(coreFields));
|
|
151
|
+
const signingDigest32 = (0, _noble_hashes_sha3_js.keccak_256)(unsignedPayloadBytes);
|
|
152
|
+
return {
|
|
153
|
+
unsignedPayloadBytes,
|
|
154
|
+
signingDigest32,
|
|
155
|
+
signingDigestHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(signingDigest32)}`,
|
|
156
|
+
toChecksumAddress: normalizedToChecksum
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function encodeSignedEip1559Tx(params) {
|
|
160
|
+
const { tx, yParity, r, s } = params;
|
|
161
|
+
if (yParity !== 0 && yParity !== 1) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "yParity must be 0 or 1");
|
|
162
|
+
if (typeof r !== "bigint" || r < 1n || r >= require_constants.SECP256K1_N) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid ECDSA r scalar");
|
|
163
|
+
if (typeof s !== "bigint" || s < 1n || s > require_constants.SECP256K1_HALF_N) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or high-S ECDSA s scalar");
|
|
164
|
+
const { coreFields } = validateAndBuildCoreRlpFields(tx);
|
|
165
|
+
const signingDigest32 = (0, _noble_hashes_sha3_js.keccak_256)(require_secp256k1_scalar.concatBytes(require_secp256k1_scalar.u8(EIP1559_TX_TYPE_BYTE), rlpEncode(coreFields)));
|
|
166
|
+
const signedFields = [
|
|
167
|
+
...coreFields,
|
|
168
|
+
bigintToMinimalBytesBE(BigInt(yParity), "yParity"),
|
|
169
|
+
bigintToMinimalBytesBE(r, "r"),
|
|
170
|
+
bigintToMinimalBytesBE(s, "s")
|
|
171
|
+
];
|
|
172
|
+
const rawSignedTxBytes = require_secp256k1_scalar.concatBytes(require_secp256k1_scalar.u8(EIP1559_TX_TYPE_BYTE), rlpEncode(signedFields));
|
|
173
|
+
const txHash32 = (0, _noble_hashes_sha3_js.keccak_256)(rawSignedTxBytes);
|
|
174
|
+
return {
|
|
175
|
+
rawSignedTxBytes,
|
|
176
|
+
rawSignedTxHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(rawSignedTxBytes)}`,
|
|
177
|
+
txHash32,
|
|
178
|
+
txHashHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(txHash32)}`,
|
|
179
|
+
signingDigest32
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function decodeAndRecoverSignedEip1559Tx(rawTx) {
|
|
183
|
+
const rawBytes = require_secp256k1_scalar.normalizeHexBytes(rawTx);
|
|
184
|
+
if (rawBytes.length < 4 || rawBytes[0] !== EIP1559_TX_TYPE_BYTE) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Expected EIP-1559 Type 0x02 transaction envelope");
|
|
185
|
+
const decoded = rlpDecode(rawBytes.subarray(1));
|
|
186
|
+
if (!Array.isArray(decoded) || decoded.length !== 12) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Signed EIP-1559 RLP list must have 12 elements");
|
|
187
|
+
for (let i = 0; i < 12; i++) if (i === 8) {
|
|
188
|
+
if (!Array.isArray(decoded[8])) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "accessList must be an RLP list");
|
|
189
|
+
} else if (!(decoded[i] instanceof Uint8Array)) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", `Field ${i} must be an RLP byte string`);
|
|
190
|
+
const chainId = canonicalRlpBytesToBigInt(decoded[0], "chainId");
|
|
191
|
+
const nonce = canonicalRlpBytesToBigInt(decoded[1], "nonce");
|
|
192
|
+
const maxPriorityFeePerGasWei = canonicalRlpBytesToBigInt(decoded[2], "maxPriorityFeePerGasWei");
|
|
193
|
+
const maxFeePerGasWei = canonicalRlpBytesToBigInt(decoded[3], "maxFeePerGasWei");
|
|
194
|
+
const gasLimit = canonicalRlpBytesToBigInt(decoded[4], "gasLimit");
|
|
195
|
+
const toBytes = decoded[5];
|
|
196
|
+
if (toBytes.length !== 20) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Destination address must be 20 bytes");
|
|
197
|
+
const toLowerHex = (0, _noble_hashes_utils_js.bytesToHex)(toBytes);
|
|
198
|
+
if (/^0+$/.test(toLowerHex)) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Zero destination address rejected");
|
|
199
|
+
const toChecksumAddress = require_tweak.toEip55ChecksumAddress(toLowerHex);
|
|
200
|
+
const valueWei = canonicalRlpBytesToBigInt(decoded[6], "valueWei");
|
|
201
|
+
const dataBytes = new Uint8Array(decoded[7]);
|
|
202
|
+
const parsedAccessList = decoded[8].map((entry) => {
|
|
203
|
+
if (!Array.isArray(entry) || entry.length !== 2 || !(entry[0] instanceof Uint8Array) || !Array.isArray(entry[1])) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Malformed EIP-2930 accessList tuple");
|
|
204
|
+
if (entry[0].length !== 20) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "accessList address must be 20 bytes");
|
|
205
|
+
const addrHex = (0, _noble_hashes_utils_js.bytesToHex)(entry[0]);
|
|
206
|
+
if (/^0+$/.test(addrHex)) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Zero address in accessList rejected");
|
|
207
|
+
const storageKeys = entry[1].map((sk) => {
|
|
208
|
+
if (!(sk instanceof Uint8Array) || sk.length !== 32) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "accessList storageKey must be 32 bytes");
|
|
209
|
+
return sk;
|
|
210
|
+
});
|
|
211
|
+
return {
|
|
212
|
+
address: require_tweak.toEip55ChecksumAddress(addrHex),
|
|
213
|
+
storageKeys
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
const yParityBig = canonicalRlpBytesToBigInt(decoded[9], "yParity");
|
|
217
|
+
if (yParityBig !== 0n && yParityBig !== 1n) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "yParity must be 0 or 1");
|
|
218
|
+
const yParity = Number(yParityBig);
|
|
219
|
+
const r = canonicalRlpBytesToBigInt(decoded[10], "r");
|
|
220
|
+
const s = canonicalRlpBytesToBigInt(decoded[11], "s");
|
|
221
|
+
if (r < 1n || r >= require_constants.SECP256K1_N || s < 1n || s > require_constants.SECP256K1_HALF_N) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or high-S signature in signed EIP-1559 tx");
|
|
222
|
+
const reencoded = encodeSignedEip1559Tx({
|
|
223
|
+
tx: {
|
|
224
|
+
chainId,
|
|
225
|
+
nonce,
|
|
226
|
+
maxPriorityFeePerGasWei,
|
|
227
|
+
maxFeePerGasWei,
|
|
228
|
+
gasLimit,
|
|
229
|
+
to: toChecksumAddress,
|
|
230
|
+
valueWei,
|
|
231
|
+
data: dataBytes,
|
|
232
|
+
accessList: parsedAccessList
|
|
233
|
+
},
|
|
234
|
+
yParity,
|
|
235
|
+
r,
|
|
236
|
+
s
|
|
237
|
+
});
|
|
238
|
+
if (!require_secp256k1_scalar.constantTimeEqual(rawBytes, reencoded.rawSignedTxBytes)) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Non-canonical EIP-1559 RLP encoding");
|
|
239
|
+
const signingDigest32 = reencoded.signingDigest32;
|
|
240
|
+
return {
|
|
241
|
+
chainId,
|
|
242
|
+
nonce,
|
|
243
|
+
maxPriorityFeePerGasWei,
|
|
244
|
+
maxFeePerGasWei,
|
|
245
|
+
gasLimit,
|
|
246
|
+
toChecksumAddress,
|
|
247
|
+
valueWei,
|
|
248
|
+
dataBytes,
|
|
249
|
+
accessList: parsedAccessList,
|
|
250
|
+
yParity,
|
|
251
|
+
r,
|
|
252
|
+
s,
|
|
253
|
+
senderChecksumAddress: require_tweak.ecrecoverEthAddress({
|
|
254
|
+
msgDigest32: signingDigest32,
|
|
255
|
+
r,
|
|
256
|
+
s,
|
|
257
|
+
yParity
|
|
258
|
+
}).checksumAddress,
|
|
259
|
+
signingDigest32,
|
|
260
|
+
txHashHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)((0, _noble_hashes_sha3_js.keccak_256)(rawBytes))}`
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Encodes canonical 68-byte ERC-20 `transfer(address,uint256)` calldata (`0xa9059cbb` + 32B recipient + 32B amount).
|
|
265
|
+
*/
|
|
266
|
+
function encodeErc20TransferCalldata(recipientAddress, tokenAmount) {
|
|
267
|
+
const parsedRecipient = require_tweak.parseStrictEthAddress(recipientAddress);
|
|
268
|
+
if (typeof tokenAmount !== "bigint" || tokenAmount <= 0n || tokenAmount > MAX_UINT256) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "ERC-20 transfer amount must satisfy 1 <= amount <= 2^256-1");
|
|
269
|
+
const out = new Uint8Array(68);
|
|
270
|
+
out.set(require_secp256k1_scalar.normalizeHexBytes(ERC20_TRANSFER_SELECTOR_HEX, 4), 0);
|
|
271
|
+
out.set(parsedRecipient.addressBytes20, 16);
|
|
272
|
+
let rem = tokenAmount;
|
|
273
|
+
for (let i = 67; i >= 36; i--) {
|
|
274
|
+
out[i] = Number(rem & 255n);
|
|
275
|
+
rem >>= 8n;
|
|
276
|
+
}
|
|
277
|
+
return out;
|
|
278
|
+
}
|
|
279
|
+
function decodeErc20TransferCalldata(calldata) {
|
|
280
|
+
const bytes = require_secp256k1_scalar.normalizeHexBytes(calldata);
|
|
281
|
+
if (bytes.length !== 68 || (0, _noble_hashes_utils_js.bytesToHex)(bytes.subarray(0, 4)) !== ERC20_TRANSFER_SELECTOR_HEX) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Expected 68-byte ERC-20 transfer(address,uint256) calldata");
|
|
282
|
+
for (let i = 4; i < 16; i++) if (bytes[i] !== 0) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Non-zero padding in ABI-encoded ERC-20 recipient word");
|
|
283
|
+
const addrLower = (0, _noble_hashes_utils_js.bytesToHex)(bytes.subarray(16, 36));
|
|
284
|
+
if (/^0+$/.test(addrLower)) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "Zero recipient in ERC-20 transfer calldata");
|
|
285
|
+
const tokenAmount = require_secp256k1_scalar.bytesToBigIntBE(bytes.subarray(36, 68));
|
|
286
|
+
if (tokenAmount <= 0n) throw new require_constants.MpcProtocolError("RLP_INVALID_TRANSACTION", "ERC-20 transfer amount must be > 0");
|
|
287
|
+
return {
|
|
288
|
+
recipientChecksumAddress: require_tweak.toEip55ChecksumAddress(addrLower),
|
|
289
|
+
tokenAmount
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
//#endregion
|
|
294
|
+
exports.EIP1559_TX_TYPE_BYTE = EIP1559_TX_TYPE_BYTE;
|
|
295
|
+
exports.ERC20_TRANSFER_SELECTOR_HEX = ERC20_TRANSFER_SELECTOR_HEX;
|
|
296
|
+
exports.bigintToMinimalBytesBE = bigintToMinimalBytesBE;
|
|
297
|
+
exports.decodeAndRecoverSignedEip1559Tx = decodeAndRecoverSignedEip1559Tx;
|
|
298
|
+
exports.decodeErc20TransferCalldata = decodeErc20TransferCalldata;
|
|
299
|
+
exports.encodeErc20TransferCalldata = encodeErc20TransferCalldata;
|
|
300
|
+
exports.encodeSignedEip1559Tx = encodeSignedEip1559Tx;
|
|
301
|
+
exports.encodeUnsignedEip1559Tx = encodeUnsignedEip1559Tx;
|
|
302
|
+
exports.rlpDecode = rlpDecode;
|
|
303
|
+
exports.rlpEncode = rlpEncode;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
//#region src/rlp-eip1559.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Minimal, zero-dependency canonical RLP encoder/decoder and EIP-1559 (EIP-2718 Type `0x02`)
|
|
4
|
+
* transaction serializer for air-gapped disaster recovery and clear-signing (`design.md` §5 & §7).
|
|
5
|
+
*
|
|
6
|
+
* Enforces Yellow Paper Appendix B canonical integer encoding (`0n -> 0x80` empty byte string,
|
|
7
|
+
* zero leading `0x00` bytes on positive integers) and EIP-2 low-S (`s <= SECP256K1_HALF_N`).
|
|
8
|
+
*/
|
|
9
|
+
type RlpInput = Uint8Array | ReadonlyArray<RlpInput>;
|
|
10
|
+
declare const EIP1559_TX_TYPE_BYTE: 2;
|
|
11
|
+
declare const ERC20_TRANSFER_SELECTOR_HEX: "a9059cbb";
|
|
12
|
+
declare function bigintToMinimalBytesBE(value: bigint, label?: string): Uint8Array;
|
|
13
|
+
declare function rlpEncode(item: RlpInput): Uint8Array;
|
|
14
|
+
declare function rlpDecode(bytes: Uint8Array): RlpInput;
|
|
15
|
+
interface Eip1559AccessListItem {
|
|
16
|
+
readonly address: string;
|
|
17
|
+
readonly storageKeys: ReadonlyArray<Uint8Array | string>;
|
|
18
|
+
}
|
|
19
|
+
interface Eip1559UnsignedTxFields {
|
|
20
|
+
readonly chainId: bigint;
|
|
21
|
+
readonly nonce: bigint;
|
|
22
|
+
readonly maxPriorityFeePerGasWei: bigint;
|
|
23
|
+
readonly maxFeePerGasWei: bigint;
|
|
24
|
+
readonly gasLimit: bigint;
|
|
25
|
+
readonly to: string;
|
|
26
|
+
readonly valueWei: bigint;
|
|
27
|
+
readonly data?: Uint8Array | string;
|
|
28
|
+
readonly accessList?: ReadonlyArray<Eip1559AccessListItem>;
|
|
29
|
+
}
|
|
30
|
+
declare function encodeUnsignedEip1559Tx(tx: Eip1559UnsignedTxFields): {
|
|
31
|
+
readonly unsignedPayloadBytes: Uint8Array;
|
|
32
|
+
readonly signingDigest32: Uint8Array;
|
|
33
|
+
readonly signingDigestHex: string;
|
|
34
|
+
readonly toChecksumAddress: string;
|
|
35
|
+
};
|
|
36
|
+
declare function encodeSignedEip1559Tx(params: {
|
|
37
|
+
readonly tx: Eip1559UnsignedTxFields;
|
|
38
|
+
readonly yParity: 0 | 1;
|
|
39
|
+
readonly r: bigint;
|
|
40
|
+
readonly s: bigint;
|
|
41
|
+
}): {
|
|
42
|
+
readonly rawSignedTxBytes: Uint8Array;
|
|
43
|
+
readonly rawSignedTxHex: string;
|
|
44
|
+
readonly txHash32: Uint8Array;
|
|
45
|
+
readonly txHashHex: string;
|
|
46
|
+
readonly signingDigest32: Uint8Array;
|
|
47
|
+
};
|
|
48
|
+
interface DecodedSignedEip1559Tx {
|
|
49
|
+
readonly chainId: bigint;
|
|
50
|
+
readonly nonce: bigint;
|
|
51
|
+
readonly maxPriorityFeePerGasWei: bigint;
|
|
52
|
+
readonly maxFeePerGasWei: bigint;
|
|
53
|
+
readonly gasLimit: bigint;
|
|
54
|
+
readonly toChecksumAddress: string;
|
|
55
|
+
readonly valueWei: bigint;
|
|
56
|
+
readonly dataBytes: Uint8Array;
|
|
57
|
+
readonly accessList: ReadonlyArray<Eip1559AccessListItem>;
|
|
58
|
+
readonly yParity: 0 | 1;
|
|
59
|
+
readonly r: bigint;
|
|
60
|
+
readonly s: bigint;
|
|
61
|
+
readonly senderChecksumAddress: string;
|
|
62
|
+
readonly signingDigest32: Uint8Array;
|
|
63
|
+
readonly txHashHex: string;
|
|
64
|
+
}
|
|
65
|
+
declare function decodeAndRecoverSignedEip1559Tx(rawTx: Uint8Array | string): DecodedSignedEip1559Tx;
|
|
66
|
+
/**
|
|
67
|
+
* Encodes canonical 68-byte ERC-20 `transfer(address,uint256)` calldata (`0xa9059cbb` + 32B recipient + 32B amount).
|
|
68
|
+
*/
|
|
69
|
+
declare function encodeErc20TransferCalldata(recipientAddress: string, tokenAmount: bigint): Uint8Array;
|
|
70
|
+
declare function decodeErc20TransferCalldata(calldata: Uint8Array | string): {
|
|
71
|
+
readonly recipientChecksumAddress: string;
|
|
72
|
+
readonly tokenAmount: bigint;
|
|
73
|
+
};
|
|
74
|
+
//#endregion
|
|
75
|
+
export { DecodedSignedEip1559Tx, EIP1559_TX_TYPE_BYTE, ERC20_TRANSFER_SELECTOR_HEX, Eip1559AccessListItem, Eip1559UnsignedTxFields, RlpInput, bigintToMinimalBytesBE, decodeAndRecoverSignedEip1559Tx, decodeErc20TransferCalldata, encodeErc20TransferCalldata, encodeSignedEip1559Tx, encodeUnsignedEip1559Tx, rlpDecode, rlpEncode };
|