-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
68 lines (59 loc) · 1.61 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { decryptNodeResponse, encryptDataField } from "@swisstronik/utils";
import { network } from "hardhat";
import { BaseContract, Provider } from "ethers";
const NODE_RPC_URL = (network.config as any).url;
export const sendShieldedTransaction = async (
signer: any,
destination: string,
data: string,
value: string
) => {
// Encrypt transaction data
const [encryptedData] = await encryptDataField(NODE_RPC_URL, data);
// Construct and sign transaction with encrypted data
return await signer.sendTransaction({
from: signer.address,
to: destination,
data: encryptedData,
value,
gasLimit: 2000000,
// gasPrice: 0 // We're using 0 gas price in tests. Comment it, if you're running tests on actual network
});
};
export const sendShieldedQuery = async (
provider: any,
destination: string,
data: string,
value: string
) => {
// Encrypt call data
const [encryptedData, usedEncryptedKey] = await encryptDataField(
NODE_RPC_URL,
data
);
// Do call
const response = await provider.call({
to: destination,
data: encryptedData,
value,
});
if (response.startsWith("0x08c379a0")) {
return response;
}
// Decrypt call result
return await decryptNodeResponse(NODE_RPC_URL, response, usedEncryptedKey);
};
export const readContractData = async (
provider: Provider,
contract: BaseContract,
method: string,
args?: any[]
) => {
const res = await sendShieldedQuery(
provider,
contract.target as string,
contract.interface.encodeFunctionData(method, args),
"0"
);
return contract.interface.decodeFunctionResult(method, res);
};