@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,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 };
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { MpcProtocolError, SECP256K1_HALF_N, SECP256K1_N } from "./constants.mjs";
|
|
2
|
+
import { bytesToBigIntBE, bytesToHex, concatBytes, constantTimeEqual, normalizeHexBytes, u8 } from "./secp256k1-scalar.mjs";
|
|
3
|
+
import { ecrecoverEthAddress, parseStrictEthAddress, toEip55ChecksumAddress } from "./tweak.mjs";
|
|
4
|
+
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
5
|
+
|
|
6
|
+
//#region src/rlp-eip1559.ts
|
|
7
|
+
const EIP1559_TX_TYPE_BYTE = 2;
|
|
8
|
+
const ERC20_TRANSFER_SELECTOR_HEX = "a9059cbb";
|
|
9
|
+
const MAX_UINT64 = (1n << 64n) - 1n;
|
|
10
|
+
const MAX_UINT256 = (1n << 256n) - 1n;
|
|
11
|
+
function bigintToMinimalBytesBE(value, label = "uint") {
|
|
12
|
+
if (typeof value !== "bigint" || value < 0n || value > MAX_UINT256) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `${label} must satisfy 0 <= v <= 2^256 - 1`);
|
|
13
|
+
if (value === 0n) return new Uint8Array(0);
|
|
14
|
+
const bytes = [];
|
|
15
|
+
let rem = value;
|
|
16
|
+
while (rem > 0n) {
|
|
17
|
+
bytes.push(Number(rem & 255n));
|
|
18
|
+
rem >>= 8n;
|
|
19
|
+
}
|
|
20
|
+
bytes.reverse();
|
|
21
|
+
return Uint8Array.from(bytes);
|
|
22
|
+
}
|
|
23
|
+
function encodeLengthPrefix(length, offsetBase) {
|
|
24
|
+
if (length < 56) return Uint8Array.of(offsetBase + length);
|
|
25
|
+
const lenBytes = bigintToMinimalBytesBE(BigInt(length), "rlpLength");
|
|
26
|
+
return concatBytes(Uint8Array.of(offsetBase + 55 + lenBytes.length), lenBytes);
|
|
27
|
+
}
|
|
28
|
+
function rlpEncode(item) {
|
|
29
|
+
if (item instanceof Uint8Array) {
|
|
30
|
+
if (item.length === 1 && item[0] <= 127) return new Uint8Array(item);
|
|
31
|
+
return concatBytes(encodeLengthPrefix(item.length, 128), item);
|
|
32
|
+
}
|
|
33
|
+
if (Array.isArray(item)) {
|
|
34
|
+
const payload = concatBytes(...item.map((child) => rlpEncode(child)));
|
|
35
|
+
return concatBytes(encodeLengthPrefix(payload.length, 192), payload);
|
|
36
|
+
}
|
|
37
|
+
throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Unsupported RLP item type");
|
|
38
|
+
}
|
|
39
|
+
const MAX_RLP_DECODE_DEPTH = 4;
|
|
40
|
+
function rlpDecodeItem(buf, offset, depth = 0) {
|
|
41
|
+
if (depth > MAX_RLP_DECODE_DEPTH) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `RLP nesting depth exceeds maximum (${MAX_RLP_DECODE_DEPTH})`);
|
|
42
|
+
if (offset >= buf.length) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Unexpected EOF during RLP decode");
|
|
43
|
+
const prefix = buf[offset];
|
|
44
|
+
if (prefix <= 127) return {
|
|
45
|
+
item: buf.subarray(offset, offset + 1),
|
|
46
|
+
nextOffset: offset + 1
|
|
47
|
+
};
|
|
48
|
+
if (prefix <= 183) {
|
|
49
|
+
const strLen = prefix - 128;
|
|
50
|
+
const start = offset + 1;
|
|
51
|
+
const end$1 = start + strLen;
|
|
52
|
+
if (end$1 > buf.length) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Truncated short RLP byte string");
|
|
53
|
+
if (strLen === 1 && buf[start] <= 127) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Non-canonical single-byte RLP string");
|
|
54
|
+
return {
|
|
55
|
+
item: buf.subarray(start, end$1),
|
|
56
|
+
nextOffset: end$1
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (prefix <= 191) {
|
|
60
|
+
const lenOfLen$1 = prefix - 183;
|
|
61
|
+
const lenStart$1 = offset + 1;
|
|
62
|
+
const lenEnd$1 = lenStart$1 + lenOfLen$1;
|
|
63
|
+
if (lenEnd$1 > buf.length || buf[lenStart$1] === 0) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Malformed long RLP string length");
|
|
64
|
+
const strLen = Number(bytesToBigIntBE(buf.subarray(lenStart$1, lenEnd$1)));
|
|
65
|
+
if (!Number.isSafeInteger(strLen) || strLen < 56 || lenEnd$1 + strLen > buf.length) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or truncated long RLP string");
|
|
66
|
+
return {
|
|
67
|
+
item: buf.subarray(lenEnd$1, lenEnd$1 + strLen),
|
|
68
|
+
nextOffset: lenEnd$1 + strLen
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (prefix <= 247) {
|
|
72
|
+
const listLen$1 = prefix - 192;
|
|
73
|
+
let cur$1 = offset + 1;
|
|
74
|
+
const end$1 = cur$1 + listLen$1;
|
|
75
|
+
if (end$1 > buf.length) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Truncated short RLP list");
|
|
76
|
+
const list$1 = [];
|
|
77
|
+
while (cur$1 < end$1) {
|
|
78
|
+
const child = rlpDecodeItem(buf, cur$1, depth + 1);
|
|
79
|
+
list$1.push(child.item);
|
|
80
|
+
cur$1 = child.nextOffset;
|
|
81
|
+
}
|
|
82
|
+
if (cur$1 !== end$1) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Misaligned short RLP list boundary");
|
|
83
|
+
return {
|
|
84
|
+
item: list$1,
|
|
85
|
+
nextOffset: end$1
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const lenOfLen = prefix - 247;
|
|
89
|
+
const lenStart = offset + 1;
|
|
90
|
+
const lenEnd = lenStart + lenOfLen;
|
|
91
|
+
if (lenEnd > buf.length || buf[lenStart] === 0) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Malformed long RLP list length");
|
|
92
|
+
const listLen = Number(bytesToBigIntBE(buf.subarray(lenStart, lenEnd)));
|
|
93
|
+
if (!Number.isSafeInteger(listLen) || listLen < 56 || lenEnd + listLen > buf.length) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or truncated long RLP list");
|
|
94
|
+
let cur = lenEnd;
|
|
95
|
+
const end = lenEnd + listLen;
|
|
96
|
+
const list = [];
|
|
97
|
+
while (cur < end) {
|
|
98
|
+
const child = rlpDecodeItem(buf, cur, depth + 1);
|
|
99
|
+
list.push(child.item);
|
|
100
|
+
cur = child.nextOffset;
|
|
101
|
+
}
|
|
102
|
+
if (cur !== end) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Misaligned long RLP list boundary");
|
|
103
|
+
return {
|
|
104
|
+
item: list,
|
|
105
|
+
nextOffset: end
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function rlpDecode(bytes) {
|
|
109
|
+
const { item, nextOffset } = rlpDecodeItem(bytes, 0);
|
|
110
|
+
if (nextOffset !== bytes.length) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Trailing bytes after RLP item");
|
|
111
|
+
return item;
|
|
112
|
+
}
|
|
113
|
+
function canonicalRlpBytesToBigInt(bytes, label) {
|
|
114
|
+
if (bytes.length > 32) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `${label} exceeds 32 bytes`);
|
|
115
|
+
if (bytes.length > 0 && bytes[0] === 0) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `Non-canonical leading zero in ${label}`);
|
|
116
|
+
return bytesToBigIntBE(bytes);
|
|
117
|
+
}
|
|
118
|
+
function validateAndBuildCoreRlpFields(tx) {
|
|
119
|
+
if (typeof tx.chainId !== "bigint" || tx.chainId < 1n || tx.chainId > MAX_UINT64) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid chainId: ${String(tx.chainId)}`);
|
|
120
|
+
if (typeof tx.nonce !== "bigint" || tx.nonce < 0n || tx.nonce >= MAX_UINT64) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid nonce: ${String(tx.nonce)}`);
|
|
121
|
+
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 MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid EIP-1559 fee parameters (require 0 <= maxPriorityFeePerGasWei <= maxFeePerGasWei)");
|
|
122
|
+
if (typeof tx.gasLimit !== "bigint" || tx.gasLimit < 21000n || tx.gasLimit > MAX_UINT64) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid gasLimit: ${String(tx.gasLimit)}`);
|
|
123
|
+
if (typeof tx.valueWei !== "bigint" || tx.valueWei < 0n || tx.valueWei > MAX_UINT256) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid valueWei: ${String(tx.valueWei)}`);
|
|
124
|
+
const parsedTo = parseStrictEthAddress(tx.to);
|
|
125
|
+
const dataBytes = tx.data === void 0 || tx.data === "0x" || tx.data === "" ? new Uint8Array(0) : normalizeHexBytes(tx.data);
|
|
126
|
+
const encodedAccessList = (tx.accessList ?? []).map((entry) => {
|
|
127
|
+
const addr = parseStrictEthAddress(entry.address);
|
|
128
|
+
const keys = entry.storageKeys.map((k) => normalizeHexBytes(k, 32));
|
|
129
|
+
return [addr.addressBytes20, keys];
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
coreFields: [
|
|
133
|
+
bigintToMinimalBytesBE(tx.chainId, "chainId"),
|
|
134
|
+
bigintToMinimalBytesBE(tx.nonce, "nonce"),
|
|
135
|
+
bigintToMinimalBytesBE(tx.maxPriorityFeePerGasWei, "maxPriorityFeePerGasWei"),
|
|
136
|
+
bigintToMinimalBytesBE(tx.maxFeePerGasWei, "maxFeePerGasWei"),
|
|
137
|
+
bigintToMinimalBytesBE(tx.gasLimit, "gasLimit"),
|
|
138
|
+
parsedTo.addressBytes20,
|
|
139
|
+
bigintToMinimalBytesBE(tx.valueWei, "valueWei"),
|
|
140
|
+
dataBytes,
|
|
141
|
+
encodedAccessList
|
|
142
|
+
],
|
|
143
|
+
normalizedToChecksum: parsedTo.checksumAddress,
|
|
144
|
+
dataBytes
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function encodeUnsignedEip1559Tx(tx) {
|
|
148
|
+
const { coreFields, normalizedToChecksum } = validateAndBuildCoreRlpFields(tx);
|
|
149
|
+
const unsignedPayloadBytes = concatBytes(u8(EIP1559_TX_TYPE_BYTE), rlpEncode(coreFields));
|
|
150
|
+
const signingDigest32 = keccak_256(unsignedPayloadBytes);
|
|
151
|
+
return {
|
|
152
|
+
unsignedPayloadBytes,
|
|
153
|
+
signingDigest32,
|
|
154
|
+
signingDigestHex: `0x${bytesToHex(signingDigest32)}`,
|
|
155
|
+
toChecksumAddress: normalizedToChecksum
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function encodeSignedEip1559Tx(params) {
|
|
159
|
+
const { tx, yParity, r, s } = params;
|
|
160
|
+
if (yParity !== 0 && yParity !== 1) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "yParity must be 0 or 1");
|
|
161
|
+
if (typeof r !== "bigint" || r < 1n || r >= SECP256K1_N) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid ECDSA r scalar");
|
|
162
|
+
if (typeof s !== "bigint" || s < 1n || s > SECP256K1_HALF_N) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or high-S ECDSA s scalar");
|
|
163
|
+
const { coreFields } = validateAndBuildCoreRlpFields(tx);
|
|
164
|
+
const signingDigest32 = keccak_256(concatBytes(u8(EIP1559_TX_TYPE_BYTE), rlpEncode(coreFields)));
|
|
165
|
+
const signedFields = [
|
|
166
|
+
...coreFields,
|
|
167
|
+
bigintToMinimalBytesBE(BigInt(yParity), "yParity"),
|
|
168
|
+
bigintToMinimalBytesBE(r, "r"),
|
|
169
|
+
bigintToMinimalBytesBE(s, "s")
|
|
170
|
+
];
|
|
171
|
+
const rawSignedTxBytes = concatBytes(u8(EIP1559_TX_TYPE_BYTE), rlpEncode(signedFields));
|
|
172
|
+
const txHash32 = keccak_256(rawSignedTxBytes);
|
|
173
|
+
return {
|
|
174
|
+
rawSignedTxBytes,
|
|
175
|
+
rawSignedTxHex: `0x${bytesToHex(rawSignedTxBytes)}`,
|
|
176
|
+
txHash32,
|
|
177
|
+
txHashHex: `0x${bytesToHex(txHash32)}`,
|
|
178
|
+
signingDigest32
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function decodeAndRecoverSignedEip1559Tx(rawTx) {
|
|
182
|
+
const rawBytes = normalizeHexBytes(rawTx);
|
|
183
|
+
if (rawBytes.length < 4 || rawBytes[0] !== EIP1559_TX_TYPE_BYTE) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Expected EIP-1559 Type 0x02 transaction envelope");
|
|
184
|
+
const decoded = rlpDecode(rawBytes.subarray(1));
|
|
185
|
+
if (!Array.isArray(decoded) || decoded.length !== 12) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Signed EIP-1559 RLP list must have 12 elements");
|
|
186
|
+
for (let i = 0; i < 12; i++) if (i === 8) {
|
|
187
|
+
if (!Array.isArray(decoded[8])) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "accessList must be an RLP list");
|
|
188
|
+
} else if (!(decoded[i] instanceof Uint8Array)) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", `Field ${i} must be an RLP byte string`);
|
|
189
|
+
const chainId = canonicalRlpBytesToBigInt(decoded[0], "chainId");
|
|
190
|
+
const nonce = canonicalRlpBytesToBigInt(decoded[1], "nonce");
|
|
191
|
+
const maxPriorityFeePerGasWei = canonicalRlpBytesToBigInt(decoded[2], "maxPriorityFeePerGasWei");
|
|
192
|
+
const maxFeePerGasWei = canonicalRlpBytesToBigInt(decoded[3], "maxFeePerGasWei");
|
|
193
|
+
const gasLimit = canonicalRlpBytesToBigInt(decoded[4], "gasLimit");
|
|
194
|
+
const toBytes = decoded[5];
|
|
195
|
+
if (toBytes.length !== 20) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Destination address must be 20 bytes");
|
|
196
|
+
const toLowerHex = bytesToHex(toBytes);
|
|
197
|
+
if (/^0+$/.test(toLowerHex)) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Zero destination address rejected");
|
|
198
|
+
const toChecksumAddress = toEip55ChecksumAddress(toLowerHex);
|
|
199
|
+
const valueWei = canonicalRlpBytesToBigInt(decoded[6], "valueWei");
|
|
200
|
+
const dataBytes = new Uint8Array(decoded[7]);
|
|
201
|
+
const parsedAccessList = decoded[8].map((entry) => {
|
|
202
|
+
if (!Array.isArray(entry) || entry.length !== 2 || !(entry[0] instanceof Uint8Array) || !Array.isArray(entry[1])) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Malformed EIP-2930 accessList tuple");
|
|
203
|
+
if (entry[0].length !== 20) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "accessList address must be 20 bytes");
|
|
204
|
+
const addrHex = bytesToHex(entry[0]);
|
|
205
|
+
if (/^0+$/.test(addrHex)) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Zero address in accessList rejected");
|
|
206
|
+
const storageKeys = entry[1].map((sk) => {
|
|
207
|
+
if (!(sk instanceof Uint8Array) || sk.length !== 32) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "accessList storageKey must be 32 bytes");
|
|
208
|
+
return sk;
|
|
209
|
+
});
|
|
210
|
+
return {
|
|
211
|
+
address: toEip55ChecksumAddress(addrHex),
|
|
212
|
+
storageKeys
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
const yParityBig = canonicalRlpBytesToBigInt(decoded[9], "yParity");
|
|
216
|
+
if (yParityBig !== 0n && yParityBig !== 1n) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "yParity must be 0 or 1");
|
|
217
|
+
const yParity = Number(yParityBig);
|
|
218
|
+
const r = canonicalRlpBytesToBigInt(decoded[10], "r");
|
|
219
|
+
const s = canonicalRlpBytesToBigInt(decoded[11], "s");
|
|
220
|
+
if (r < 1n || r >= SECP256K1_N || s < 1n || s > SECP256K1_HALF_N) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Invalid or high-S signature in signed EIP-1559 tx");
|
|
221
|
+
const reencoded = encodeSignedEip1559Tx({
|
|
222
|
+
tx: {
|
|
223
|
+
chainId,
|
|
224
|
+
nonce,
|
|
225
|
+
maxPriorityFeePerGasWei,
|
|
226
|
+
maxFeePerGasWei,
|
|
227
|
+
gasLimit,
|
|
228
|
+
to: toChecksumAddress,
|
|
229
|
+
valueWei,
|
|
230
|
+
data: dataBytes,
|
|
231
|
+
accessList: parsedAccessList
|
|
232
|
+
},
|
|
233
|
+
yParity,
|
|
234
|
+
r,
|
|
235
|
+
s
|
|
236
|
+
});
|
|
237
|
+
if (!constantTimeEqual(rawBytes, reencoded.rawSignedTxBytes)) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Non-canonical EIP-1559 RLP encoding");
|
|
238
|
+
const signingDigest32 = reencoded.signingDigest32;
|
|
239
|
+
return {
|
|
240
|
+
chainId,
|
|
241
|
+
nonce,
|
|
242
|
+
maxPriorityFeePerGasWei,
|
|
243
|
+
maxFeePerGasWei,
|
|
244
|
+
gasLimit,
|
|
245
|
+
toChecksumAddress,
|
|
246
|
+
valueWei,
|
|
247
|
+
dataBytes,
|
|
248
|
+
accessList: parsedAccessList,
|
|
249
|
+
yParity,
|
|
250
|
+
r,
|
|
251
|
+
s,
|
|
252
|
+
senderChecksumAddress: ecrecoverEthAddress({
|
|
253
|
+
msgDigest32: signingDigest32,
|
|
254
|
+
r,
|
|
255
|
+
s,
|
|
256
|
+
yParity
|
|
257
|
+
}).checksumAddress,
|
|
258
|
+
signingDigest32,
|
|
259
|
+
txHashHex: `0x${bytesToHex(keccak_256(rawBytes))}`
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Encodes canonical 68-byte ERC-20 `transfer(address,uint256)` calldata (`0xa9059cbb` + 32B recipient + 32B amount).
|
|
264
|
+
*/
|
|
265
|
+
function encodeErc20TransferCalldata(recipientAddress, tokenAmount) {
|
|
266
|
+
const parsedRecipient = parseStrictEthAddress(recipientAddress);
|
|
267
|
+
if (typeof tokenAmount !== "bigint" || tokenAmount <= 0n || tokenAmount > MAX_UINT256) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "ERC-20 transfer amount must satisfy 1 <= amount <= 2^256-1");
|
|
268
|
+
const out = new Uint8Array(68);
|
|
269
|
+
out.set(normalizeHexBytes(ERC20_TRANSFER_SELECTOR_HEX, 4), 0);
|
|
270
|
+
out.set(parsedRecipient.addressBytes20, 16);
|
|
271
|
+
let rem = tokenAmount;
|
|
272
|
+
for (let i = 67; i >= 36; i--) {
|
|
273
|
+
out[i] = Number(rem & 255n);
|
|
274
|
+
rem >>= 8n;
|
|
275
|
+
}
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
function decodeErc20TransferCalldata(calldata) {
|
|
279
|
+
const bytes = normalizeHexBytes(calldata);
|
|
280
|
+
if (bytes.length !== 68 || bytesToHex(bytes.subarray(0, 4)) !== ERC20_TRANSFER_SELECTOR_HEX) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Expected 68-byte ERC-20 transfer(address,uint256) calldata");
|
|
281
|
+
for (let i = 4; i < 16; i++) if (bytes[i] !== 0) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Non-zero padding in ABI-encoded ERC-20 recipient word");
|
|
282
|
+
const addrLower = bytesToHex(bytes.subarray(16, 36));
|
|
283
|
+
if (/^0+$/.test(addrLower)) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "Zero recipient in ERC-20 transfer calldata");
|
|
284
|
+
const tokenAmount = bytesToBigIntBE(bytes.subarray(36, 68));
|
|
285
|
+
if (tokenAmount <= 0n) throw new MpcProtocolError("RLP_INVALID_TRANSACTION", "ERC-20 transfer amount must be > 0");
|
|
286
|
+
return {
|
|
287
|
+
recipientChecksumAddress: toEip55ChecksumAddress(addrLower),
|
|
288
|
+
tokenAmount
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
//#endregion
|
|
293
|
+
export { EIP1559_TX_TYPE_BYTE, ERC20_TRANSFER_SELECTOR_HEX, bigintToMinimalBytesBE, decodeAndRecoverSignedEip1559Tx, decodeErc20TransferCalldata, encodeErc20TransferCalldata, encodeSignedEip1559Tx, encodeUnsignedEip1559Tx, rlpDecode, rlpEncode };
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { MpcErrorCode } from "./constants.mjs";
|
|
2
|
+
import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
|
|
3
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
4
|
+
import * as _noble_curves_abstract_modular_js0 from "@noble/curves/abstract/modular.js";
|
|
5
|
+
|
|
6
|
+
//#region src/secp256k1-scalar.d.ts
|
|
7
|
+
type Secp256k1Point = InstanceType<typeof secp256k1.Point>;
|
|
8
|
+
type RandomBytesFn = (length: number) => Uint8Array;
|
|
9
|
+
declare const Fn: _noble_curves_abstract_modular_js0.IField<bigint>;
|
|
10
|
+
declare const SECP256K1_G: Secp256k1Point;
|
|
11
|
+
declare const SECP256K1_ZERO: Secp256k1Point;
|
|
12
|
+
declare function utf8Bytes(text: string): Uint8Array;
|
|
13
|
+
declare function zeroizeBytes(...buffers: Array<Uint8Array | null | undefined>): void;
|
|
14
|
+
declare function constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean;
|
|
15
|
+
declare function defaultRandomBytes(length: number): Uint8Array;
|
|
16
|
+
declare const PEDERSEN_H: Secp256k1Point;
|
|
17
|
+
declare const PEDERSEN_H_COMP33: Uint8Array;
|
|
18
|
+
/**
|
|
19
|
+
* Validates a 32-byte X25519 public key upfront, rejecting non-canonical encodings (`>= 2^255 - 19`)
|
|
20
|
+
* and all Curve25519 low-order torsion points, attributing `culpritSlot` on failure.
|
|
21
|
+
*/
|
|
22
|
+
declare function assertValidX25519PublicKey(pubKey32: Uint8Array, errorCode: MpcErrorCode, culpritSlot?: number): void;
|
|
23
|
+
declare function computeX25519SharedSecret(secretKey32: Uint8Array, peerPublicKey32: Uint8Array, errorCode: MpcErrorCode, culpritSlot?: number): Uint8Array;
|
|
24
|
+
declare function u8(value: number): Uint8Array;
|
|
25
|
+
declare function u16be(value: number): Uint8Array;
|
|
26
|
+
declare function u32be(value: number): Uint8Array;
|
|
27
|
+
declare function u64be(value: bigint): Uint8Array;
|
|
28
|
+
declare function concatBytes(...arrays: ReadonlyArray<Uint8Array>): Uint8Array;
|
|
29
|
+
declare function lengthPrefixedEncode(tag: string, parts: ReadonlyArray<Uint8Array>): Uint8Array;
|
|
30
|
+
declare function hashTranscriptSha256(tag: string, ...parts: ReadonlyArray<Uint8Array>): Uint8Array;
|
|
31
|
+
declare function bytesToBigIntBE(bytes: Uint8Array): bigint;
|
|
32
|
+
declare function scalarToBytes32(scalar: bigint): Uint8Array;
|
|
33
|
+
declare function bytes32ToScalarStrict(bytes: Uint8Array): bigint;
|
|
34
|
+
declare function bytes32ToScalarNonZero(bytes: Uint8Array): bigint;
|
|
35
|
+
declare function assertScalarNonZero(scalar: bigint, label?: string): void;
|
|
36
|
+
declare function assertScalarInField(scalar: bigint, label?: string): void;
|
|
37
|
+
declare function modN(a: bigint): bigint;
|
|
38
|
+
declare function addModN(a: bigint, b: bigint): bigint;
|
|
39
|
+
declare function subModN(a: bigint, b: bigint): bigint;
|
|
40
|
+
declare function mulModN(a: bigint, b: bigint): bigint;
|
|
41
|
+
declare function negModN(a: bigint): bigint;
|
|
42
|
+
declare function invModN(a: bigint): bigint;
|
|
43
|
+
/**
|
|
44
|
+
* Unbiased rejection-sampled random scalar in `Z_n^* = [1, n - 1]`.
|
|
45
|
+
* Draws 32-byte blocks from CSPRNG and zeroizes each temporary buffer immediately.
|
|
46
|
+
*/
|
|
47
|
+
declare function sampleScalarNonZero(rng?: RandomBytesFn): bigint;
|
|
48
|
+
/**
|
|
49
|
+
* Unbiased rejection-sampled hash-to-scalar `H_scalar(tag, ...parts) -> Z_n^*`.
|
|
50
|
+
* Streams 32-byte `SHAKE256` XOF blocks over injective length-prefixed framing until `1 <= u < n`.
|
|
51
|
+
*/
|
|
52
|
+
declare function H_scalar(tag: string, ...parts: ReadonlyArray<Uint8Array>): bigint;
|
|
53
|
+
declare function mulPoint(point: Secp256k1Point, scalar: bigint): Secp256k1Point;
|
|
54
|
+
declare function mulBaseG(scalar: bigint): Secp256k1Point;
|
|
55
|
+
declare function pedersenCommit(valueScalar: bigint, blindingScalar: bigint): Secp256k1Point;
|
|
56
|
+
declare function decodePointCompressed(bytes: Uint8Array, allowIdentity?: boolean): Secp256k1Point;
|
|
57
|
+
declare function encodePointCompressed(point: Secp256k1Point): Uint8Array;
|
|
58
|
+
declare function normalizeHexBytes(value: Uint8Array | string, expectedLength?: number): Uint8Array;
|
|
59
|
+
declare function normalizeSortedSubset(subset: ReadonlyArray<number>, t?: number, nParties?: number): number[];
|
|
60
|
+
declare function sortedSubsetBytes(subset: ReadonlyArray<number>): Uint8Array;
|
|
61
|
+
declare function subsetsEqual(a: ReadonlyArray<number>, b: ReadonlyArray<number>): boolean;
|
|
62
|
+
declare function enumerateCombinations(nParties: number, t: number): number[][];
|
|
63
|
+
/**
|
|
64
|
+
* Computes Shamir Lagrange interpolation coefficient at 0 for `slot` within subset `subset`:
|
|
65
|
+
* `\lambda_{slot, S}(0) = \prod_{j \in S \setminus \{slot\}} (-j) * (slot - j)^{-1} mod n`.
|
|
66
|
+
*/
|
|
67
|
+
declare function lagrangeCoefficientAtZero(slot: number, subset: ReadonlyArray<number>): bigint;
|
|
68
|
+
declare function evaluatePolynomialModN(coeffs: ReadonlyArray<bigint>, x: bigint): bigint;
|
|
69
|
+
declare function evaluateFeldmanCommitments(commitments: ReadonlyArray<Secp256k1Point>, slotIndex: number): Secp256k1Point;
|
|
70
|
+
interface SchnorrPokProof {
|
|
71
|
+
noncePointComp33: Uint8Array;
|
|
72
|
+
responseBytes32: Uint8Array;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Schnorr Proof of Knowledge of discrete logarithm `secret` in arbitrary prime-order base `basePoint`
|
|
76
|
+
* (`statementPoint = secret * basePoint`), Fiat-Shamir bound to `(dst, contextParts, basePoint, statementPoint, R)`.
|
|
77
|
+
*/
|
|
78
|
+
declare function createSchnorrPokInBase(params: {
|
|
79
|
+
dst: string;
|
|
80
|
+
basePoint: Secp256k1Point;
|
|
81
|
+
statementPoint: Secp256k1Point;
|
|
82
|
+
secretScalar: bigint;
|
|
83
|
+
contextParts: ReadonlyArray<Uint8Array>;
|
|
84
|
+
rng?: RandomBytesFn;
|
|
85
|
+
}): SchnorrPokProof;
|
|
86
|
+
declare function verifySchnorrPokInBase(params: {
|
|
87
|
+
dst: string;
|
|
88
|
+
basePoint: Secp256k1Point;
|
|
89
|
+
statementPoint: Secp256k1Point;
|
|
90
|
+
proof: SchnorrPokProof;
|
|
91
|
+
contextParts: ReadonlyArray<Uint8Array>;
|
|
92
|
+
}): boolean;
|
|
93
|
+
interface PedersenPokProof {
|
|
94
|
+
noncePointComp33: Uint8Array;
|
|
95
|
+
responseValueBytes32: Uint8Array;
|
|
96
|
+
responseBlindBytes32: Uint8Array;
|
|
97
|
+
}
|
|
98
|
+
declare function createPedersenPok(params: {
|
|
99
|
+
dst: string;
|
|
100
|
+
commitmentPoint: Secp256k1Point;
|
|
101
|
+
valueScalar: bigint;
|
|
102
|
+
blindingScalar: bigint;
|
|
103
|
+
contextParts: ReadonlyArray<Uint8Array>;
|
|
104
|
+
rng?: RandomBytesFn;
|
|
105
|
+
}): PedersenPokProof;
|
|
106
|
+
declare function verifyPedersenPok(params: {
|
|
107
|
+
dst: string;
|
|
108
|
+
commitmentPoint: Secp256k1Point;
|
|
109
|
+
proof: PedersenPokProof;
|
|
110
|
+
contextParts: ReadonlyArray<Uint8Array>;
|
|
111
|
+
}): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Proof `\pi^{lin}_{K,i}` proving knowledge of `(k_i, \rho_{k,i})` such that
|
|
114
|
+
* `K_i = k_i * R` AND `C_{k,i} = k_i * G + \rho_{k,i} * H`.
|
|
115
|
+
*/
|
|
116
|
+
interface LinearPedersenDleqProof {
|
|
117
|
+
a1Comp33: Uint8Array;
|
|
118
|
+
a2Comp33: Uint8Array;
|
|
119
|
+
zKBytes32: Uint8Array;
|
|
120
|
+
zRhoBytes32: Uint8Array;
|
|
121
|
+
}
|
|
122
|
+
declare function createLinearPedersenDleqProof(params: {
|
|
123
|
+
dst: string;
|
|
124
|
+
baseR: Secp256k1Point;
|
|
125
|
+
pointK: Secp256k1Point;
|
|
126
|
+
commitmentCk: Secp256k1Point;
|
|
127
|
+
kScalar: bigint;
|
|
128
|
+
rhoScalar: bigint;
|
|
129
|
+
contextParts: ReadonlyArray<Uint8Array>;
|
|
130
|
+
rng?: RandomBytesFn;
|
|
131
|
+
}): LinearPedersenDleqProof;
|
|
132
|
+
declare function verifyLinearPedersenDleqProof(params: {
|
|
133
|
+
dst: string;
|
|
134
|
+
baseR: Secp256k1Point;
|
|
135
|
+
pointK: Secp256k1Point;
|
|
136
|
+
commitmentCk: Secp256k1Point;
|
|
137
|
+
proof: LinearPedersenDleqProof;
|
|
138
|
+
contextParts: ReadonlyArray<Uint8Array>;
|
|
139
|
+
}): boolean;
|
|
140
|
+
//#endregion
|
|
141
|
+
export { Fn, H_scalar, LinearPedersenDleqProof, PEDERSEN_H, PEDERSEN_H_COMP33, PedersenPokProof, RandomBytesFn, SECP256K1_G, SECP256K1_ZERO, SchnorrPokProof, Secp256k1Point, addModN, assertScalarInField, assertScalarNonZero, assertValidX25519PublicKey, bytes32ToScalarNonZero, bytes32ToScalarStrict, bytesToBigIntBE, bytesToHex, computeX25519SharedSecret, concatBytes, constantTimeEqual, createLinearPedersenDleqProof, createPedersenPok, createSchnorrPokInBase, decodePointCompressed, defaultRandomBytes, encodePointCompressed, enumerateCombinations, evaluateFeldmanCommitments, evaluatePolynomialModN, hashTranscriptSha256, hexToBytes, invModN, lagrangeCoefficientAtZero, lengthPrefixedEncode, modN, mulBaseG, mulModN, mulPoint, negModN, normalizeHexBytes, normalizeSortedSubset, pedersenCommit, sampleScalarNonZero, scalarToBytes32, secp256k1, sortedSubsetBytes, subModN, subsetsEqual, u16be, u32be, u64be, u8, utf8Bytes, verifyLinearPedersenDleqProof, verifyPedersenPok, verifySchnorrPokInBase, zeroizeBytes };
|