Skip to content

Commit 666cdce

Browse files
committed
wip
1 parent 3d72480 commit 666cdce

File tree

8 files changed

+348
-27
lines changed

8 files changed

+348
-27
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"secret:setEVMGatewayAddress": "yarn workspace secret-contracts-scripts setEVMGatewayAddress",
4444
"secret:queryPubkey": "yarn workspace secret-contracts-scripts queryPubkey",
4545
"secret:submit": "yarn workspace secret-contracts-scripts submit",
46+
"secret:submitRequestValue": "yarn workspace secret-contracts-scripts submitRequestValue",
4647
"secret:requestValue": "yarn workspace secret-contracts-scripts requestValue",
4748
"secret:clean": "yarn workspace secret-contracts-scripts clean",
4849
"secret:upload": "yarn workspace secret-contracts-scripts upload",

packages/hardhat/contracts/Gateway.sol

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ contract Gateway is Ownable, Utils {
5353
string public routing_info = "";
5454
string public routing_code_hash = "";
5555

56+
bytes public owner_public_key;
57+
5658
/*//////////////////////////////////////////////////////////////
5759
Structs
5860
//////////////////////////////////////////////////////////////*/
@@ -267,10 +269,11 @@ contract Gateway is Ownable, Utils {
267269
event FulfilledRequestValue(uint256 indexed requestId);
268270

269271
// Constructor
270-
constructor(address nunyaContractAddress) {
272+
constructor(address nunyaContractAddress, bytes memory deployerPublicKeyBytes) {
271273
// Initializer
272274
// Set owner to be the deployed NunyaBusiness.sol contract
273275
owner = nunyaContractAddress;
276+
owner_public_key = deployerPublicKeyBytes;
274277

275278
taskId = 1;
276279
nonce = 0;
@@ -502,7 +505,7 @@ contract Gateway is Ownable, Utils {
502505

503506
// Note - It is only possible to call this function `encodeAddressToBase64` three times
504507
// in this function, otherwise it generates error `Error: Transaction reverted without a reason`.
505-
bytes28 senderAddressBase64 = encodeAddressToBase64(address(msg.sender));
508+
bytes28 senderAddressBase64 = encodeAddressToBase64(msg.sender);
506509

507510
requestId = taskId;
508511

@@ -543,9 +546,7 @@ contract Gateway is Ownable, Utils {
543546
bytes memory payload_info = abi.encodePacked(
544547
'}","routing_info":"',routing_info,
545548
'","routing_code_hash":"',routing_code_hash,
546-
'","user_address":"',address(msg.sender),
547-
'","user_key":"',senderAddressBase64,
548-
'","callback_address":"'
549+
'","user_address":"0x0000","user_key":"AAA=","callback_address":"'
549550
);
550551

551552
//
@@ -559,12 +560,12 @@ contract Gateway is Ownable, Utils {
559560
// );
560561
// console.log("------ Gateway.requestValue - payload_info: ", payload_info);
561562

562-
uint32 _myArg = 123;
563+
// uint32 _myArg = 123;
563564
//construct the payload that is sent into the Secret Gateway
564565
// FIXME: Error parsing into type secret_gateway::types::Payload: Invalid unicode code point.: execute contract failed
565566
bytes memory payload = bytes.concat(
566567
'{"data":"{\\"myArg\\":',
567-
uint256toBytesString(_myArg),
568+
uint256toBytesString(123),
568569
payload_info,
569570
senderAddressBase64, //callback_address
570571
// callback selector should be a hex value already converted into base64 to be used
@@ -597,12 +598,12 @@ contract Gateway is Ownable, Utils {
597598
bytes memory emptyBytes = hex"0000";
598599

599600
// TODO - make `user_key` a unique key different from `user_pubkey`
600-
bytes memory userKey = bytes.concat(senderAddressBase64); // equals AAA= in base64
601+
// bytes memory userKey = bytes.concat(senderAddressBase64); // equals AAA= in base64
601602

602603
// ExecutionInfo struct
603604
ExecutionInfo memory executionInfo = ExecutionInfo({
604-
user_key: userKey, // FIXME - use this instead when resolve issue
605-
// user_key: emptyBytes,
605+
// user_key: userKey, // FIXME - use this instead when resolve issue
606+
user_key: emptyBytes, // equals AAA= in base64
606607
// FIXME: use of `secret_gateway_signer_pubkey` does not compile, what alternative to use?
607608
// user_pubkey: uint256toBytesString(secret_gateway_signer_pubkey),
608609
user_pubkey: emptyBytes, // Fill with 0 bytes
@@ -614,7 +615,7 @@ contract Gateway is Ownable, Utils {
614615
payload: payload,
615616
// TODO: add a payload signature
616617
// Signature of hash of encrypted input values
617-
payload_signature: emptyBytes
618+
payload_signature: emptyBytes // empty signature, fill with 0 bytes
618619
// payload_signature: bytes32ToBytes(payloadHash)
619620
});
620621

packages/hardhat/deploy/01_deploy_your_contract.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HardhatRuntimeEnvironment } from "hardhat/types";
22
import { DeployFunction } from "hardhat-deploy/types";
3-
import { Contract, SigningKey, Wallet, ethers, formatEther, parseEther, parseUnits } from "ethers";
3+
import { Contract, SigningKey, Wallet, getBytes, ethers, formatEther, parseEther, parseUnits } from "ethers";
44
import config from "../hardhat.config";
55
import * as dotenv from "dotenv";
66
dotenv.config();
@@ -13,6 +13,17 @@ const value = 0;
1313
// const value = 1337;
1414
// const value = ethers.utils.parseEther("0.0000001337");
1515

16+
function getDeployerPublicKeyBytes(deployer: string) {
17+
const deployerWallet = new Wallet(deployer);
18+
console.log("Generating keys for deployer public address:", deployerWallet.address, "\n");
19+
const userPublicKey = new SigningKey(deployerWallet.privateKey).compressedPublicKey;
20+
console.log('userPublicKey: ', userPublicKey);
21+
// https://github.com/ethers-io/ethers.js/issues/3795#issue-1589631066
22+
const userPublicKeyBytes = getBytes(userPublicKey);
23+
console.log('userPublicKeyBytes: ', userPublicKeyBytes);
24+
return userPublicKeyBytes;
25+
}
26+
1627
/**
1728
* Deploys a contract named "YourContract" using the deployer account and
1829
* constructor arguments set to the deployer address
@@ -38,31 +49,26 @@ const deployYourContract: DeployFunction = async function (hre: HardhatRuntimeEn
3849
let providerRpc;
3950
let deployer;
4051
let deployerAddress;
52+
let deployerPublicKeyBytes;
53+
// Ethereum Local Network
4154
// Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
4255
// Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
56+
const deployerPrivateKeyLocalNetwork = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
4357
const deployerAddressLocalNetwork = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
4458

45-
// const deployerWallet = new Wallet(deployer);
46-
// const deployerPublicKey = new SigningKey(deployerWallet.privateKey).compressedPublicKey;
47-
// const deployerAddressPublicKey = deployerPublicKey || "0x0";
48-
49-
// if (deployerAddressPublicKey == "0x0") {
50-
// console.error("Invalid public key");
51-
// return;
52-
// }
53-
5459
if (hre.network.name == "sepolia") {
5560
console.log("hre.network.name: ", hre.network.name);
5661
if (process.env.DEPLOYER_ADDRESS == "" || process.env.DEPLOYER_PRIVATE_KEY == "") {
5762
throw new Error(`Please add deployer address and private key to the .env file for Sepolia Network.`)
5863
}
5964
deployer = process.env.DEPLOYER_PRIVATE_KEY || ""; // config.networks?.sepolia?.accounts[0];
6065
deployerAddress = process.env.DEPLOYER_ADDRESS || "";
66+
deployerPublicKeyBytes = getDeployerPublicKeyBytes(deployer);
6167
providerRpc = String(config.networks?.sepolia);
6268
} else {
63-
const accounts = await hre.getNamedAccounts();
64-
deployer = accounts.deployer || "";
69+
deployer = deployerPrivateKeyLocalNetwork;
6570
deployerAddress = deployerAddressLocalNetwork;
71+
deployerPublicKeyBytes = getDeployerPublicKeyBytes(deployer);
6672
providerRpc = "http://127.0.0.1:8545/";
6773
}
6874

@@ -88,7 +94,7 @@ const deployYourContract: DeployFunction = async function (hre: HardhatRuntimeEn
8894

8995
const gateway = await deploy("Gateway", {
9096
from: deployer,
91-
args: [nunyaContract.address],
97+
args: [nunyaContract.address, deployerPublicKeyBytes],
9298
log: true,
9399
gasLimit: 30000000,
94100
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by

packages/secret-contracts-scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"setEVMGatewayAddress": "yarn build && node ./dist/evm/setEVMGatewayAddress.js",
99
"queryPubkey": "yarn build && node ./dist/functions/query/queryPubkey.js",
1010
"submit": "yarn build && node ./dist/submit.js",
11+
"submitRequestValue": "yarn build && node ./dist/submitRequestValue.js",
1112
"requestValue": "yarn build && node ./dist/evm/requestValue.js",
1213
"build": "./node_modules/.bin/tsc --build",
1314
"clean": "./node_modules/.bin/tsc --build --clean",

packages/secret-contracts-scripts/src/config/deploy.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const config = {
1212
endpoint: "http://127.0.0.1:8545/",
1313
// Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
1414
privateKey: "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
15+
// Obtained from ./packages/secret-contracts-scripts/src/functions/secretpath/generateKeys.ts
16+
publicKey: "0x038318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75",
1517
nunyaBusinessContractAddress: "0x5FbDB2315678afecb367f032d93F642f64180aa3", // only know after deploy
1618
gatewayContractAddress: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512", // only know after deploy
1719
},

packages/secret-contracts-scripts/src/evm/getPublicKey.ts

Whitespace-only changes.

packages/secret-contracts-scripts/src/functions/secretpath/generateKeys.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ export async function generateKeys() {
1111
? config.secret.testnet
1212
: config.secret.localhost;
1313

14+
let vars;
15+
if (config.evm.network == "sepolia") {
16+
vars = config.evm.sepolia;
17+
} else if (config.evm.network == "localhost") {
18+
vars = config.evm.localhost;
19+
} else {
20+
throw new Error(`Unsupported network.`)
21+
}
22+
const { chainId, endpoint, nunyaBusinessContractAddress, gatewayContractAddress, privateKey } = vars;
23+
1424
if (gatewayContractEncryptionKeyForChaChaPoly1305 == "") {
1525
throw Error("Unable to obtain Secret Network Gateway information");
1626
}
1727

18-
const privateKey = process.env.DEPLOYER_PRIVATE_KEY;
19-
2028
if (!privateKey) {
2129
console.log("🚫️ You don't have a deployer account configured in environment variables");
2230
return;
@@ -39,6 +47,7 @@ export async function generateKeys() {
3947

4048
// https://github.com/SolarRepublic/neutrino/blob/main/src/secp256k1.ts#L334
4149
const sharedKey = await sha256(ecdh(userPrivateKeyBytes, gatewayContractPublicKeyBytes));
50+
console.log('sharedKey: ', sharedKey);
4251

43-
return { userPrivateKeyBytes, userPublicKeyBytes, sharedKey };
52+
return { userPublicKey, userPrivateKeyBytes, userPublicKeyBytes, sharedKey };
4453
}

0 commit comments

Comments
 (0)