@fhevm/sdk 0.14.3 → 0.14.4
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/_cjs/core/_version.js +1 -1
- package/_cjs/core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.js +40 -47
- package/_cjs/core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.js.map +1 -1
- package/_cjs/core/modules/relayer/cleartext/fetchPublicDecrypt.js +13 -7
- package/_cjs/core/modules/relayer/cleartext/fetchPublicDecrypt.js.map +1 -1
- package/_cjs/core/modules/relayer/cleartext/fetchUserDecryptV1.js +38 -45
- package/_cjs/core/modules/relayer/cleartext/fetchUserDecryptV1.js.map +1 -1
- package/_cjs/core/modules/relayer/cleartext/fetchUserDecryptV2.js +104 -2
- package/_cjs/core/modules/relayer/cleartext/fetchUserDecryptV2.js.map +1 -1
- package/_cjs/core/modules/relayer/cleartext/signers.js +35 -0
- package/_cjs/core/modules/relayer/cleartext/signers.js.map +1 -1
- package/_cjs/wasm/tfhe/loadTfheLib.js.map +1 -1
- package/_cjs/wasm/tkms/loadKmsLib.js.map +1 -1
- package/_esm/core/_version.js +1 -1
- package/_esm/core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.js +43 -51
- package/_esm/core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.js.map +1 -1
- package/_esm/core/modules/relayer/cleartext/fetchPublicDecrypt.js +15 -9
- package/_esm/core/modules/relayer/cleartext/fetchPublicDecrypt.js.map +1 -1
- package/_esm/core/modules/relayer/cleartext/fetchUserDecryptV1.js +42 -49
- package/_esm/core/modules/relayer/cleartext/fetchUserDecryptV1.js.map +1 -1
- package/_esm/core/modules/relayer/cleartext/fetchUserDecryptV2.js +111 -3
- package/_esm/core/modules/relayer/cleartext/fetchUserDecryptV2.js.map +1 -1
- package/_esm/core/modules/relayer/cleartext/signers.js +41 -0
- package/_esm/core/modules/relayer/cleartext/signers.js.map +1 -1
- package/_types/core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.d.ts.map +1 -1
- package/_types/core/modules/relayer/cleartext/fetchPublicDecrypt.d.ts.map +1 -1
- package/_types/core/modules/relayer/cleartext/fetchUserDecryptV1.d.ts.map +1 -1
- package/_types/core/modules/relayer/cleartext/fetchUserDecryptV2.d.ts +1 -1
- package/_types/core/modules/relayer/cleartext/fetchUserDecryptV2.d.ts.map +1 -1
- package/_types/core/modules/relayer/cleartext/signers.d.ts +13 -0
- package/_types/core/modules/relayer/cleartext/signers.d.ts.map +1 -1
- package/core/_version.ts +1 -1
- package/core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.ts +49 -60
- package/core/modules/relayer/cleartext/fetchPublicDecrypt.ts +19 -9
- package/core/modules/relayer/cleartext/fetchUserDecryptV1.ts +48 -58
- package/core/modules/relayer/cleartext/fetchUserDecryptV2.ts +133 -4
- package/core/modules/relayer/cleartext/signers.ts +61 -0
- package/package.json +1 -1
|
@@ -1,6 +1,114 @@
|
|
|
1
|
+
import { asUint32BigInt, tryParseUintBigIntString } from '../../../base/uint.js';
|
|
2
|
+
import { getTrustedClient } from '../../../runtime/CoreFhevm-p.js';
|
|
3
|
+
import { signKmsSigncryptedShares } from './signers.js';
|
|
1
4
|
////////////////////////////////////////////////////////////////////////////////
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
+
// runUserDecryptV2OnChain
|
|
6
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
7
|
+
const userDecryptV2Abi = [
|
|
8
|
+
{
|
|
9
|
+
type: 'function',
|
|
10
|
+
name: 'userDecryptV2',
|
|
11
|
+
inputs: [
|
|
12
|
+
{
|
|
13
|
+
name: 'handles',
|
|
14
|
+
type: 'tuple[]',
|
|
15
|
+
internalType: 'struct HandleEntry[]',
|
|
16
|
+
components: [
|
|
17
|
+
{ name: 'handle', type: 'bytes32', internalType: 'bytes32' },
|
|
18
|
+
{
|
|
19
|
+
name: 'contractAddress',
|
|
20
|
+
type: 'address',
|
|
21
|
+
internalType: 'address',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: 'ownerAddress',
|
|
25
|
+
type: 'address',
|
|
26
|
+
internalType: 'address',
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'permit',
|
|
32
|
+
type: 'tuple',
|
|
33
|
+
internalType: 'struct UserDecryptPermitV2',
|
|
34
|
+
components: [
|
|
35
|
+
{ name: 'userAddress', type: 'address', internalType: 'address' },
|
|
36
|
+
{ name: 'publicKey', type: 'bytes', internalType: 'bytes' },
|
|
37
|
+
{
|
|
38
|
+
name: 'allowedContracts',
|
|
39
|
+
type: 'address[]',
|
|
40
|
+
internalType: 'address[]',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'startTimestamp',
|
|
44
|
+
type: 'uint256',
|
|
45
|
+
internalType: 'uint256',
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'durationSeconds',
|
|
49
|
+
type: 'uint256',
|
|
50
|
+
internalType: 'uint256',
|
|
51
|
+
},
|
|
52
|
+
{ name: 'extraData', type: 'bytes', internalType: 'bytes' },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
{ name: 'signature', type: 'bytes', internalType: 'bytes' },
|
|
56
|
+
],
|
|
57
|
+
outputs: [
|
|
58
|
+
{ name: 'payload', type: 'bytes', internalType: 'bytes' },
|
|
59
|
+
{ name: 'signers', type: 'address[]', internalType: 'address[]' },
|
|
60
|
+
{ name: 'threshold', type: 'uint256', internalType: 'uint256' },
|
|
61
|
+
{ name: 'signedExtraData', type: 'bytes', internalType: 'bytes' },
|
|
62
|
+
],
|
|
63
|
+
stateMutability: 'view',
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
async function runUserDecryptV2OnChain(relayerClient, payload) {
|
|
67
|
+
const { kmsDecryptEip712Message, kmsDecryptEip712Signature } = payload;
|
|
68
|
+
const pairs = payload.handleContractPairs;
|
|
69
|
+
const trustedClient = getTrustedClient(relayerClient);
|
|
70
|
+
// The signed permit fields are passed verbatim, extraData included: the host runs the gateway's and the
|
|
71
|
+
// kms-worker's checks against them and returns the signers of the KMS context the extraData names.
|
|
72
|
+
const res = (await relayerClient.runtime.ethereum.readContract(trustedClient, {
|
|
73
|
+
abi: userDecryptV2Abi,
|
|
74
|
+
address: relayerClient.chain.fhevm.contracts.kmsVerifier.address,
|
|
75
|
+
args: [
|
|
76
|
+
pairs.map((p) => ({
|
|
77
|
+
handle: p.handle.bytes32Hex,
|
|
78
|
+
contractAddress: p.contractAddress,
|
|
79
|
+
ownerAddress: p.ownerAddress,
|
|
80
|
+
})),
|
|
81
|
+
{
|
|
82
|
+
userAddress: kmsDecryptEip712Message.userAddress,
|
|
83
|
+
publicKey: kmsDecryptEip712Message.publicKey,
|
|
84
|
+
allowedContracts: kmsDecryptEip712Message.allowedContracts,
|
|
85
|
+
startTimestamp: _parseUintBigIntString('kmsDecryptEip712Message.startTimestamp', kmsDecryptEip712Message.startTimestamp),
|
|
86
|
+
durationSeconds: _parseUintBigIntString('kmsDecryptEip712Message.durationSeconds', kmsDecryptEip712Message.durationSeconds),
|
|
87
|
+
extraData: kmsDecryptEip712Message.extraData,
|
|
88
|
+
},
|
|
89
|
+
kmsDecryptEip712Signature,
|
|
90
|
+
],
|
|
91
|
+
functionName: userDecryptV2Abi[0].name,
|
|
92
|
+
}));
|
|
93
|
+
return {
|
|
94
|
+
commonKmsSigncryptedSharePayload: res[0],
|
|
95
|
+
signersAddress: res[1],
|
|
96
|
+
threshold: asUint32BigInt(res[2]),
|
|
97
|
+
extraData: res[3],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
101
|
+
export async function fetchUserDecryptV2(relayerClient, parameters) {
|
|
102
|
+
// On chain only: forge-fhevm v1 has no unified (V2) decryption permit path.
|
|
103
|
+
const res = await runUserDecryptV2OnChain(relayerClient, parameters.payload);
|
|
104
|
+
return signKmsSigncryptedShares(relayerClient, { ...res, threshold: Number(res.threshold) });
|
|
105
|
+
}
|
|
106
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
107
|
+
function _parseUintBigIntString(label, value) {
|
|
108
|
+
const bn = tryParseUintBigIntString(value);
|
|
109
|
+
if (bn === undefined) {
|
|
110
|
+
throw new Error(`${label} is not a valid uint string, got ${JSON.stringify(value)}`);
|
|
111
|
+
}
|
|
112
|
+
return bn;
|
|
5
113
|
}
|
|
6
114
|
//# sourceMappingURL=fetchUserDecryptV2.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchUserDecryptV2.js","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchUserDecryptV2.ts"],"names":[],"mappings":"AAEA,gFAAgF;AAEhF,
|
|
1
|
+
{"version":3,"file":"fetchUserDecryptV2.js","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchUserDecryptV2.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AACjF,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAExD,gFAAgF;AAChF,0BAA0B;AAC1B,gFAAgF;AAEhF,MAAM,gBAAgB,GAAG;IACvB;QACE,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,eAAe;QACrB,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,SAAS;gBACf,IAAI,EAAE,SAAS;gBACf,YAAY,EAAE,sBAAsB;gBACpC,UAAU,EAAE;oBACV,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,YAAY,EAAE,SAAS,EAAE;oBAC5D;wBACE,IAAI,EAAE,iBAAiB;wBACvB,IAAI,EAAE,SAAS;wBACf,YAAY,EAAE,SAAS;qBACxB;oBACD;wBACE,IAAI,EAAE,cA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CAAC,CAAC,CAAC;QACjC,SAAS,EAAE,GAAG,CAAC,CAAC,CAAa;KAC9B,CAAC;AACJ,CAAC;AAED,gFAAgF;AAEhF,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,aAAuC,EACvC,UAAwC;IAExC,4EAA4E;IAC5E,MAAM,GAAG,GAAG,MAAM,uBAAuB,CAAC,aAAa,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;IAC7E,OAAO,wBAAwB,CAAC,aAAa,EAAE,EAAE,GAAG,GAAG,EAAE,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAC/F,CAAC;AAED,gFAAgF;AAEhF,SAAS,sBAAsB,CAAC,KAAa,EAAE,KAAa;IAC1D,MAAM,EAAE,GAAG,wBAAwB,CAAC,KAAK,CAAC,CAAC;IAC3C,IAAI,EAAE,KAAK,SAAS,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,oCAAoC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACvF,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { remove0x } from '../../../base/string.js';
|
|
2
|
+
import { randomUniqueUints } from '../../../base/uint.js';
|
|
3
|
+
import { userDecryptResultToKmsSigncryptedShares } from '../utils.js';
|
|
1
4
|
////////////////////////////////////////////////////////////////////////////////
|
|
2
5
|
const FHEVM_TEST_MNEMONIC = 'test test test test test test test future home engine virtual motion';
|
|
3
6
|
const COPROCESSOR_PATH = "0'/2/";
|
|
@@ -47,6 +50,44 @@ export function getKmsSignersPrivateKeyMap(relayerClient) {
|
|
|
47
50
|
return signersPrivateKeyMap.kms;
|
|
48
51
|
}
|
|
49
52
|
////////////////////////////////////////////////////////////////////////////////
|
|
53
|
+
/**
|
|
54
|
+
* The KMS nodes that answer a request, as a relayer forwards one response per node: a random subset of
|
|
55
|
+
* `signersAddress`, of random size between `threshold` and all of them.
|
|
56
|
+
*/
|
|
57
|
+
export function pickRespondingKmsSigners(signersAddress, threshold) {
|
|
58
|
+
const n = signersAddress.length;
|
|
59
|
+
if (!Number.isInteger(threshold) || threshold < 1 || threshold > n) {
|
|
60
|
+
throw new Error(`Invalid KMS threshold ${threshold} for ${n} signers`);
|
|
61
|
+
}
|
|
62
|
+
let count = threshold + Math.floor(Math.random() * (n - threshold + 1));
|
|
63
|
+
// Defensive: the guard above already keeps count within [threshold, n].
|
|
64
|
+
if (count < 1) {
|
|
65
|
+
count = 1;
|
|
66
|
+
}
|
|
67
|
+
if (count > n) {
|
|
68
|
+
count = n;
|
|
69
|
+
}
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
71
|
+
return randomUniqueUints(n, count).map((i) => signersAddress[i]);
|
|
72
|
+
}
|
|
73
|
+
/** One signcrypted share per responding KMS node, each signing the common payload. */
|
|
74
|
+
export async function signKmsSigncryptedShares(relayerClient, parameters) {
|
|
75
|
+
const cleartextEthereumModule = relayerClient.runtime.ethereum;
|
|
76
|
+
const { commonKmsSigncryptedSharePayload: payload, extraData, signersAddress, threshold } = parameters;
|
|
77
|
+
const privateKeys = getKmsSignersPrivateKeyMap(relayerClient);
|
|
78
|
+
const responders = pickRespondingKmsSigners(signersAddress, threshold);
|
|
79
|
+
const result = [];
|
|
80
|
+
for (const signerAddress of responders) {
|
|
81
|
+
const privateKey = privateKeys.get(signerAddress);
|
|
82
|
+
if (privateKey === undefined) {
|
|
83
|
+
throw new Error(`Unable to find KMS signer for address ${signerAddress} (${responders.length} of ${signersAddress.length} responding)`);
|
|
84
|
+
}
|
|
85
|
+
const signature = await cleartextEthereumModule.sign({ hash: payload, privateKey });
|
|
86
|
+
result.push({ signature: remove0x(signature), payload: remove0x(payload), extraData });
|
|
87
|
+
}
|
|
88
|
+
return userDecryptResultToKmsSigncryptedShares(result);
|
|
89
|
+
}
|
|
90
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
50
91
|
function _fillSignersPrivateKey(relayerClient, hdPathSuffix, count, map) {
|
|
51
92
|
const cleartextEthereumModule = relayerClient.runtime.ethereum;
|
|
52
93
|
for (let i = 0; i < count; ++i) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signers.js","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/signers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"signers.js","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/signers.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,uCAAuC,EAAE,MAAM,aAAa,CAAC;AAEtE,gFAAgF;AAEhF,MAAM,mBAAmB,GAAG,sEAAsE,CAAC;AACnG,MAAM,gBAAgB,GAAG,OAAO,CAAC;AACjC,MAAM,QAAQ,GAAG,OAAO,CAAC;AAEzB,gFAAgF;AAChF,EAAE;AACF,iBAAiB;AACjB,iBAAiB;AACjB,EAAE;AACF,oFAAoF;AACpF,kEAAkE;AAClE,mFAAmF;AACnF,qFAAqF;AACrF,+EAA+E;AAC/E,kFAAkF;AAClF,EAAE;AACF,gFAAgF;AAEhF,MAAM,iCAAiC,GAA4E;IACjH,OAAO,EAAE,4CAAkE;IAC3E,UAAU,EAAE,oEAAgF;CAC7F,CAAC;AAEF,MAAM,yBAAyB,GAA4E;IACzG,OAAO,EAAE,4CAAkE;IAC3E,UAAU,EAAE,oEAAgF;CAC7F,CAAC;AAEF,MAAM,UAAU,uBAAuB,CAAC,aAAiC;IACvE,OAAO,aAAa,EAAE,WAAW,EAAE,KAAK,yBAAyB,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;AAC1F,CAAC;AAMD,MAAM,oBAAoB,GAAyE;IACjG,WAAW,EAAE,IAAI,GAAG,EAAgC;IACpD,GAAG,EAAE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AC,SAAS,CAAC,EAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC;IACzF,CAAC;IAED,OAAO,uCAAuC,CAAC,MAAM,CAAC,CAAC;AACzD,CAAC;AAED,gFAAgF;AAEhF,SAAS,sBAAsB,CAC7B,aAAuC,EACvC,YAAoB,EACpB,KAAa,EACb,GAAkB;IAElB,MAAM,uBAAuB,GAAG,aAAa,CAAC,OAAO,CAAC,QAAmC,CAAC;IAC1F,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC;QAC/B,MAAM,OAAO,GAAG,uBAAuB,CAAC,iBAAiB,CAAC;YACxD,QAAQ,EAAE,mBAAmB;YAC7B,IAAI,EAAE,aAAa,YAAY,GAAG,CAAC,EAAE;SACtC,CAAC,CAAC;QACH,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchDelegatedUserDecryptV1.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchDelegatedUserDecryptV1.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchDelegatedUserDecryptV1.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mCAAmC,EACnC,0BAA0B,EAC1B,wBAAwB,EACzB,MAAM,aAAa,CAAC;AAmLrB,wBAAsB,yBAAyB,CAC7C,aAAa,EAAE,wBAAwB,EACvC,UAAU,EAAE,mCAAmC,GAC9C,OAAO,CAAC,0BAA0B,CAAC,CA6BrC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchPublicDecrypt.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchPublicDecrypt.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,4BAA4B,EAAE,4BAA4B,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"fetchPublicDecrypt.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchPublicDecrypt.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,4BAA4B,EAAE,4BAA4B,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAiJxH,wBAAsB,kBAAkB,CACtC,aAAa,EAAE,wBAAwB,EACvC,UAAU,EAAE,4BAA4B,GACvC,OAAO,CAAC,4BAA4B,CAAC,CAmCvC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchUserDecryptV1.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchUserDecryptV1.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchUserDecryptV1.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchUserDecryptV1.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,4BAA4B,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAkLtH,wBAAsB,kBAAkB,CACtC,aAAa,EAAE,wBAAwB,EACvC,UAAU,EAAE,4BAA4B,GACvC,OAAO,CAAC,0BAA0B,CAAC,CA6BrC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { FetchUserDecryptParametersV2, FetchUserDecryptReturnType, RelayerClientWithRuntime } from '../types.js';
|
|
2
|
-
export declare function fetchUserDecryptV2(
|
|
2
|
+
export declare function fetchUserDecryptV2(relayerClient: RelayerClientWithRuntime, parameters: FetchUserDecryptParametersV2): Promise<FetchUserDecryptReturnType>;
|
|
3
3
|
//# sourceMappingURL=fetchUserDecryptV2.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchUserDecryptV2.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchUserDecryptV2.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchUserDecryptV2.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/fetchUserDecryptV2.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,4BAA4B,EAAE,0BAA0B,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAyHtH,wBAAsB,kBAAkB,CACtC,aAAa,EAAE,wBAAwB,EACvC,UAAU,EAAE,4BAA4B,GACvC,OAAO,CAAC,0BAA0B,CAAC,CAIrC"}
|
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import type { BytesHex, ChecksummedAddress } from '../../../types/primitives.js';
|
|
2
|
+
import type { KmsSigncryptedShare } from '../../../types/kms-p.js';
|
|
2
3
|
import type { RelayerClientWithRuntime } from '../types.js';
|
|
3
4
|
export declare function isForgeFhevmV1KmsSigner(signerAddress: string | undefined): boolean;
|
|
4
5
|
type PrivateKeyMap = Map<ChecksummedAddress, BytesHex>;
|
|
5
6
|
export declare function getCoprocessorSignersPrivateKeyMap(relayerClient: RelayerClientWithRuntime): PrivateKeyMap;
|
|
6
7
|
export declare function getKmsSignersPrivateKeyMap(relayerClient: RelayerClientWithRuntime): PrivateKeyMap;
|
|
8
|
+
/**
|
|
9
|
+
* The KMS nodes that answer a request, as a relayer forwards one response per node: a random subset of
|
|
10
|
+
* `signersAddress`, of random size between `threshold` and all of them.
|
|
11
|
+
*/
|
|
12
|
+
export declare function pickRespondingKmsSigners(signersAddress: readonly ChecksummedAddress[], threshold: number): ChecksummedAddress[];
|
|
13
|
+
/** One signcrypted share per responding KMS node, each signing the common payload. */
|
|
14
|
+
export declare function signKmsSigncryptedShares(relayerClient: RelayerClientWithRuntime, parameters: {
|
|
15
|
+
readonly commonKmsSigncryptedSharePayload: BytesHex;
|
|
16
|
+
readonly extraData: BytesHex;
|
|
17
|
+
readonly signersAddress: readonly ChecksummedAddress[];
|
|
18
|
+
readonly threshold: number;
|
|
19
|
+
}): Promise<readonly KmsSigncryptedShare[]>;
|
|
7
20
|
export {};
|
|
8
21
|
//# sourceMappingURL=signers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signers.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/signers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"signers.d.ts","sourceRoot":"","sources":["../../../../../core/modules/relayer/cleartext/signers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AACjF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAGnE,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAmC5D,wBAAgB,uBAAuB,CAAC,aAAa,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAElF;AAID,KAAK,aAAa,GAAG,GAAG,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAC;AASvD,wBAAgB,kCAAkC,CAAC,aAAa,EAAE,wBAAwB,GAAG,aAAa,CASzG;AAID,wBAAgB,0BAA0B,CAAC,aAAa,EAAE,wBAAwB,GAAG,aAAa,CAMjG;AAID;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,cAAc,EAAE,SAAS,kBAAkB,EAAE,EAC7C,SAAS,EAAE,MAAM,GAChB,kBAAkB,EAAE,CAetB;AAED,sFAAsF;AACtF,wBAAsB,wBAAwB,CAC5C,aAAa,EAAE,wBAAwB,EACvC,UAAU,EAAE;IACV,QAAQ,CAAC,gCAAgC,EAAE,QAAQ,CAAC;IACpD,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,cAAc,EAAE,SAAS,kBAAkB,EAAE,CAAC;IACvD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,GACA,OAAO,CAAC,SAAS,mBAAmB,EAAE,CAAC,CAmBzC"}
|
package/core/_version.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import type { BytesHex, ChecksummedAddress, Uint32BigInt } from '../../../types/primitives.js';
|
|
2
|
-
import type { FetchUserDecryptResultItem } from '../../../types/relayer.js';
|
|
3
|
-
import type { CleartextEthereumModule } from '../../ethereum/types-ct.js';
|
|
4
2
|
import type {
|
|
5
3
|
FetchDelegatedUserDecryptParameters,
|
|
6
4
|
FetchUserDecryptReturnType,
|
|
@@ -8,11 +6,9 @@ import type {
|
|
|
8
6
|
} from '../types.js';
|
|
9
7
|
import type { KmsSignersContext } from '../../../types/kmsSignersContext.js';
|
|
10
8
|
import type { FhevmClientFrozenContext } from '../../../types/fhevmClientFrozenContext-p.js';
|
|
11
|
-
import {
|
|
12
|
-
import { asUint32BigInt, tryParseUintBigIntString, randomUniqueUints } from '../../../base/uint.js';
|
|
9
|
+
import { asUint32BigInt, tryParseUintBigIntString } from '../../../base/uint.js';
|
|
13
10
|
import { getTrustedClient } from '../../../runtime/CoreFhevm-p.js';
|
|
14
|
-
import {
|
|
15
|
-
import { getKmsSignersPrivateKeyMap } from './signers.js';
|
|
11
|
+
import { signKmsSigncryptedShares } from './signers.js';
|
|
16
12
|
import { isForgeFhevmV1, readPlaintexts, xorMaskWithPublicKey } from './forgeFhevmV1.js';
|
|
17
13
|
import { readCurrentKmsSignersContext } from '../../../host-contracts/readKmsSignersContext-p.js';
|
|
18
14
|
|
|
@@ -74,10 +70,10 @@ async function runDelegatedUserDecryptOffChain(
|
|
|
74
70
|
// runDelegatedUserDecryptOnChain
|
|
75
71
|
////////////////////////////////////////////////////////////////////////////////
|
|
76
72
|
|
|
77
|
-
const
|
|
73
|
+
const delegatedUserDecryptV1Abi = [
|
|
78
74
|
{
|
|
79
75
|
type: 'function',
|
|
80
|
-
name: '
|
|
76
|
+
name: 'delegatedUserDecryptV1',
|
|
81
77
|
inputs: [
|
|
82
78
|
{
|
|
83
79
|
name: 'pairs',
|
|
@@ -92,31 +88,39 @@ const delegatedUserDecryptAbi = [
|
|
|
92
88
|
},
|
|
93
89
|
],
|
|
94
90
|
},
|
|
95
|
-
{ name: 'delegator', type: 'address', internalType: 'address' },
|
|
96
|
-
{ name: 'delegate', type: 'address', internalType: 'address' },
|
|
97
|
-
{ name: 'publicKey', type: 'bytes', internalType: 'bytes' },
|
|
98
91
|
{
|
|
99
|
-
name: '
|
|
100
|
-
type: '
|
|
101
|
-
internalType: '
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
92
|
+
name: 'permit',
|
|
93
|
+
type: 'tuple',
|
|
94
|
+
internalType: 'struct DelegatedUserDecryptPermitV1',
|
|
95
|
+
components: [
|
|
96
|
+
{ name: 'delegateAddress', type: 'address', internalType: 'address' },
|
|
97
|
+
{ name: 'publicKey', type: 'bytes', internalType: 'bytes' },
|
|
98
|
+
{
|
|
99
|
+
name: 'contractAddresses',
|
|
100
|
+
type: 'address[]',
|
|
101
|
+
internalType: 'address[]',
|
|
102
|
+
},
|
|
103
|
+
{ name: 'delegatorAddress', type: 'address', internalType: 'address' },
|
|
104
|
+
{
|
|
105
|
+
name: 'startTimestamp',
|
|
106
|
+
type: 'uint256',
|
|
107
|
+
internalType: 'uint256',
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'durationDays',
|
|
111
|
+
type: 'uint256',
|
|
112
|
+
internalType: 'uint256',
|
|
113
|
+
},
|
|
114
|
+
{ name: 'extraData', type: 'bytes', internalType: 'bytes' },
|
|
115
|
+
],
|
|
112
116
|
},
|
|
113
|
-
{ name: '
|
|
117
|
+
{ name: 'signature', type: 'bytes', internalType: 'bytes' },
|
|
114
118
|
],
|
|
115
119
|
outputs: [
|
|
116
120
|
{ name: 'payload', type: 'bytes', internalType: 'bytes' },
|
|
117
121
|
{ name: 'signers', type: 'address[]', internalType: 'address[]' },
|
|
118
122
|
{ name: 'threshold', type: 'uint256', internalType: 'uint256' },
|
|
119
|
-
{ name: '
|
|
123
|
+
{ name: 'signedExtraData', type: 'bytes', internalType: 'bytes' },
|
|
120
124
|
],
|
|
121
125
|
stateMutability: 'view',
|
|
122
126
|
},
|
|
@@ -134,23 +138,33 @@ async function runDelegatedUserDecryptOnChain(
|
|
|
134
138
|
const { kmsDecryptEip712Message, kmsDecryptEip712Signer, kmsDecryptEip712Signature } = payload;
|
|
135
139
|
const pairs = payload.handleContractPairs;
|
|
136
140
|
const trustedClient = getTrustedClient(relayerClient);
|
|
141
|
+
// The permit's extraData is passed verbatim, as in fetchUserDecryptV1.
|
|
137
142
|
const res = (await relayerClient.runtime.ethereum.readContract(trustedClient, {
|
|
138
|
-
abi:
|
|
143
|
+
abi: delegatedUserDecryptV1Abi,
|
|
139
144
|
address: relayerClient.chain.fhevm.contracts.kmsVerifier.address as ChecksummedAddress,
|
|
140
145
|
args: [
|
|
141
146
|
pairs.map((p) => ({
|
|
142
147
|
handle: p.handle.bytes32Hex,
|
|
143
148
|
contractAddress: p.contractAddress,
|
|
144
149
|
})),
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
150
|
+
{
|
|
151
|
+
delegateAddress: kmsDecryptEip712Signer,
|
|
152
|
+
publicKey: kmsDecryptEip712Message.publicKey,
|
|
153
|
+
contractAddresses: kmsDecryptEip712Message.contractAddresses,
|
|
154
|
+
delegatorAddress: kmsDecryptEip712Message.delegatorAddress,
|
|
155
|
+
startTimestamp: _parseUintBigIntString(
|
|
156
|
+
'kmsDecryptEip712Message.startTimestamp',
|
|
157
|
+
kmsDecryptEip712Message.startTimestamp,
|
|
158
|
+
),
|
|
159
|
+
durationDays: _parseUintBigIntString(
|
|
160
|
+
'kmsDecryptEip712Message.durationDays',
|
|
161
|
+
kmsDecryptEip712Message.durationDays,
|
|
162
|
+
),
|
|
163
|
+
extraData: kmsDecryptEip712Message.extraData,
|
|
164
|
+
},
|
|
151
165
|
kmsDecryptEip712Signature,
|
|
152
166
|
],
|
|
153
|
-
functionName:
|
|
167
|
+
functionName: delegatedUserDecryptV1Abi[0].name,
|
|
154
168
|
})) as unknown[];
|
|
155
169
|
|
|
156
170
|
const commonKmsSigncryptedSharePayload = res[0] as BytesHex;
|
|
@@ -172,7 +186,6 @@ export async function fetchDelegatedUserDecrypt(
|
|
|
172
186
|
relayerClient: RelayerClientWithRuntime,
|
|
173
187
|
parameters: FetchDelegatedUserDecryptParameters,
|
|
174
188
|
): Promise<FetchUserDecryptReturnType> {
|
|
175
|
-
const cleartextEthereumModule = relayerClient.runtime.ethereum as CleartextEthereumModule;
|
|
176
189
|
const { payload, fhevmContext } = parameters;
|
|
177
190
|
const pairs = payload.handleContractPairs;
|
|
178
191
|
const contractAddresses = payload.kmsDecryptEip712Message.contractAddresses;
|
|
@@ -200,31 +213,7 @@ export async function fetchDelegatedUserDecrypt(
|
|
|
200
213
|
res = await runDelegatedUserDecryptOnChain(relayerClient, payload);
|
|
201
214
|
}
|
|
202
215
|
|
|
203
|
-
|
|
204
|
-
const extraData = res.extraData;
|
|
205
|
-
const threshold = res.threshold;
|
|
206
|
-
const signersAddress = res.signersAddress;
|
|
207
|
-
|
|
208
|
-
const signers = getKmsSignersPrivateKeyMap(relayerClient);
|
|
209
|
-
|
|
210
|
-
const randomSignersAddress = randomUniqueUints(signersAddress.length, Number(threshold)).map(
|
|
211
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
212
|
-
(i) => signersAddress[i]!,
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
const result: FetchUserDecryptResultItem[] = [];
|
|
216
|
-
|
|
217
|
-
for (const signerAddress of randomSignersAddress) {
|
|
218
|
-
const privateKey = signers.get(signerAddress);
|
|
219
|
-
if (privateKey === undefined) {
|
|
220
|
-
throw new Error(`Unable to find KMS signer for address ${signerAddress}`);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const signature = await cleartextEthereumModule.sign({ hash: commonKmsSigncryptedSharePayload, privateKey });
|
|
224
|
-
result.push({ signature: remove0x(signature), payload: remove0x(commonKmsSigncryptedSharePayload), extraData });
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return userDecryptResultToKmsSigncryptedShares(result);
|
|
216
|
+
return signKmsSigncryptedShares(relayerClient, { ...res, threshold: Number(res.threshold) });
|
|
228
217
|
}
|
|
229
218
|
|
|
230
219
|
////////////////////////////////////////////////////////////////////////////////
|
|
@@ -4,7 +4,7 @@ import type { FetchPublicDecryptParameters, FetchPublicDecryptReturnType, Relaye
|
|
|
4
4
|
import type { KmsSignersContext } from '../../../types/kmsSignersContext.js';
|
|
5
5
|
import type { FhevmClientFrozenContext } from '../../../types/fhevmClientFrozenContext-p.js';
|
|
6
6
|
import { getTrustedClient } from '../../../runtime/CoreFhevm-p.js';
|
|
7
|
-
import { getKmsSignersPrivateKeyMap } from './signers.js';
|
|
7
|
+
import { getKmsSignersPrivateKeyMap, pickRespondingKmsSigners } from './signers.js';
|
|
8
8
|
import { encodeTypedCleartexts, isForgeFhevmV1, readPlaintexts } from './forgeFhevmV1.js';
|
|
9
9
|
import { createKmsEip712Domain } from '../../../kms/createKmsEip712Domain.js';
|
|
10
10
|
import { readCurrentKmsSignersContext } from '../../../host-contracts/readKmsSignersContext-p.js';
|
|
@@ -16,7 +16,10 @@ const publicDecryptAbi = [
|
|
|
16
16
|
{
|
|
17
17
|
type: 'function',
|
|
18
18
|
name: 'publicDecrypt',
|
|
19
|
-
inputs: [
|
|
19
|
+
inputs: [
|
|
20
|
+
{ name: 'handles', type: 'bytes32[]', internalType: 'bytes32[]' },
|
|
21
|
+
{ name: 'extraData', type: 'bytes', internalType: 'bytes' },
|
|
22
|
+
],
|
|
20
23
|
outputs: [
|
|
21
24
|
{
|
|
22
25
|
name: 'abiEncodedCleartexts',
|
|
@@ -26,7 +29,7 @@ const publicDecryptAbi = [
|
|
|
26
29
|
{ name: 'digest', type: 'bytes32', internalType: 'bytes32' },
|
|
27
30
|
{ name: 'signers', type: 'address[]', internalType: 'address[]' },
|
|
28
31
|
{ name: 'threshold', type: 'uint256', internalType: 'uint256' },
|
|
29
|
-
{ name: '
|
|
32
|
+
{ name: 'signedExtraData', type: 'bytes', internalType: 'bytes' },
|
|
30
33
|
],
|
|
31
34
|
stateMutability: 'view',
|
|
32
35
|
},
|
|
@@ -42,6 +45,7 @@ async function runPublicDecryptOffChain(
|
|
|
42
45
|
readonly orderedAbiEncodedClearValues: BytesHex;
|
|
43
46
|
readonly digest: Bytes32Hex;
|
|
44
47
|
readonly signersAddress: readonly ChecksummedAddress[];
|
|
48
|
+
readonly threshold: number;
|
|
45
49
|
readonly extraData: BytesHex;
|
|
46
50
|
}> {
|
|
47
51
|
const cleartextEthereumModule = relayerClient.runtime.ethereum as CleartextEthereumModule;
|
|
@@ -96,6 +100,7 @@ async function runPublicDecryptOffChain(
|
|
|
96
100
|
orderedAbiEncodedClearValues,
|
|
97
101
|
digest,
|
|
98
102
|
signersAddress: currentKmsSignersContext.signers,
|
|
103
|
+
threshold: currentKmsSignersContext.threshold,
|
|
99
104
|
extraData,
|
|
100
105
|
};
|
|
101
106
|
}
|
|
@@ -111,25 +116,29 @@ async function runPublicDecryptOnChain(
|
|
|
111
116
|
readonly orderedAbiEncodedClearValues: BytesHex;
|
|
112
117
|
readonly digest: Bytes32Hex;
|
|
113
118
|
readonly signersAddress: readonly ChecksummedAddress[];
|
|
119
|
+
readonly threshold: number;
|
|
114
120
|
readonly extraData: BytesHex;
|
|
115
121
|
}> {
|
|
116
122
|
const trustedClient = getTrustedClient(relayerClient);
|
|
123
|
+
// The host signs over exactly the requested extraData and returns the signers of the context it names.
|
|
117
124
|
const res = (await relayerClient.runtime.ethereum.readContract(trustedClient, {
|
|
118
125
|
abi: publicDecryptAbi,
|
|
119
126
|
address: relayerClient.chain.fhevm.contracts.kmsVerifier.address as ChecksummedAddress,
|
|
120
|
-
args: [payload.orderedHandles.map((h) => h.bytes32Hex)],
|
|
127
|
+
args: [payload.orderedHandles.map((h) => h.bytes32Hex), payload.extraData],
|
|
121
128
|
functionName: publicDecryptAbi[0].name,
|
|
122
129
|
})) as unknown[];
|
|
123
130
|
|
|
124
131
|
const orderedAbiEncodedClearValues = res[0] as BytesHex;
|
|
125
132
|
const digest = res[1] as Bytes32Hex;
|
|
126
133
|
const signersAddress = res[2] as readonly ChecksummedAddress[];
|
|
134
|
+
const threshold = Number(res[3]);
|
|
127
135
|
const extraData = res[4] as BytesHex;
|
|
128
136
|
|
|
129
137
|
return {
|
|
130
138
|
orderedAbiEncodedClearValues,
|
|
131
139
|
digest,
|
|
132
140
|
signersAddress,
|
|
141
|
+
threshold,
|
|
133
142
|
extraData,
|
|
134
143
|
};
|
|
135
144
|
}
|
|
@@ -152,16 +161,17 @@ export async function fetchPublicDecrypt(
|
|
|
152
161
|
}
|
|
153
162
|
|
|
154
163
|
const digest = res.digest;
|
|
155
|
-
const signersAddress = res.signersAddress;
|
|
156
164
|
const signers = getKmsSignersPrivateKeyMap(relayerClient);
|
|
165
|
+
const responders = pickRespondingKmsSigners(res.signersAddress, res.threshold);
|
|
157
166
|
|
|
158
167
|
const kmsPublicDecryptEip712Signatures: Bytes65Hex[] = [];
|
|
159
168
|
|
|
160
|
-
for (
|
|
161
|
-
|
|
162
|
-
const privateKey = signers.get(signersAddress[i]!);
|
|
169
|
+
for (const signerAddress of responders) {
|
|
170
|
+
const privateKey = signers.get(signerAddress);
|
|
163
171
|
if (privateKey === undefined) {
|
|
164
|
-
throw new Error(
|
|
172
|
+
throw new Error(
|
|
173
|
+
`Unable to find KMS signer for address ${signerAddress} (${responders.length} of ${res.signersAddress.length} responding)`,
|
|
174
|
+
);
|
|
165
175
|
}
|
|
166
176
|
|
|
167
177
|
const signature = await cleartextEthereumModule.sign({ hash: digest, privateKey });
|