Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Oct 15, 2024
1 parent dc2ccdd commit 2c9cc9a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
7 changes: 7 additions & 0 deletions packages/secret-contracts/nunya-contract/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/secret-contracts/nunya-contract/cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ schema = ["cosmwasm-schema"]

[dependencies]
cosmwasm-schema = { version = "2.0.0", optional = true }
cosmwasm-std = { package = "secret-cosmwasm-std", version = "1.0.0" }
cosmwasm-std = { package = "secret-cosmwasm-std", version = "1.1.10", features = ["stargate"] }
cosmwasm-storage = { package = "secret-cosmwasm-storage", version = "1.0.0" }
schemars = "0.8.11"
secret-toolkit = { version = "0.10.0", default-features = false, features = [
Expand All @@ -49,6 +49,8 @@ secret-toolkit-serialization = { version = "0.10.0", features = ["base64"] }
tnls = { git = "https://github.com/SecretSaturn/TNLS", branch = "main", package = "secret_gateway", default-features = false }
# cw-storage-plus = { version = "0.14.0", default-features = false }

anybuf = {version = "0.5.0"}

[[bin]]
name = "schema"
required-features = ["schema"]
54 changes: 49 additions & 5 deletions packages/secret-contracts/nunya-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
};
use cosmwasm_std::{
entry_point, to_binary, coin, Binary, Coin, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, Uint128, Uint256
entry_point, to_binary, to_vec, coin, Binary, Coin, ContractResult, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, SystemResult, Uint128, Uint256
};
use secret_toolkit::viewing_key::{ViewingKey, ViewingKeyStore};
use secret_toolkit::utils::{pad_handle_result, pad_query_result, HandleCallback};
Expand All @@ -20,6 +20,8 @@ use tnls::{
state::Task,
};

use anybuf::Anybuf;

/// pad handle responses and log attributes to blocks of 256 bytes to prevent leaking info based on
/// response size
pub const BLOCK_SIZE: usize = 256;
Expand Down Expand Up @@ -166,6 +168,9 @@ fn create_new_auth_out(

let result = base64::encode(json_string);

// Get the contract's code hash using the gateway address
let gateway_code_hash = get_contract_code_hash(deps, config.gateway_address.to_string())?;

let callback_msg = GatewayMsg::Output {
// Sepolia network gateway contract Solidity source code
// https://github.com/SecretSaturn/SecretPath/blob/main/TNLS-Gateways/public-gateway/src/Gateway.sol
Expand All @@ -180,7 +185,7 @@ fn create_new_auth_out(
},
}
.to_cosmos_msg(
config.gateway_hash,
gateway_code_hash,
config.gateway_address.to_string(),
None,
)?;
Expand Down Expand Up @@ -272,6 +277,9 @@ fn create_payment_reference(

let result = base64::encode(json_string);

// Get the contract's code hash using the gateway address
let gateway_code_hash = get_contract_code_hash(deps, config.gateway_address.to_string())?;

let callback_msg = GatewayMsg::Output {
outputs: PostExecutionMsg {
result,
Expand All @@ -280,7 +288,7 @@ fn create_payment_reference(
},
}
.to_cosmos_msg(
config.gateway_hash,
gateway_code_hash,
config.gateway_address.to_string(),
None,
)?;
Expand Down Expand Up @@ -425,6 +433,9 @@ fn create_pay(

let result = base64::encode(json_string);

// Get the contract's code hash using the gateway address
let gateway_code_hash = get_contract_code_hash(deps, config.gateway_address.to_string())?;

let callback_msg = GatewayMsg::Output {
outputs: PostExecutionMsg {
result,
Expand All @@ -433,7 +444,7 @@ fn create_pay(
},
}
.to_cosmos_msg(
config.gateway_hash,
gateway_code_hash,
config.gateway_address.to_string(),
None,
)?;
Expand Down Expand Up @@ -540,6 +551,9 @@ fn create_withdraw_to(

let result = base64::encode(json_string);

// Get the contract's code hash using the gateway address
let gateway_code_hash = get_contract_code_hash(deps, config.gateway_address.to_string())?;

let callback_msg = GatewayMsg::Output {
outputs: PostExecutionMsg {
result,
Expand All @@ -548,7 +562,7 @@ fn create_withdraw_to(
},
}
.to_cosmos_msg(
config.gateway_hash,
gateway_code_hash,
config.gateway_address.to_string(),
None,
)?;
Expand All @@ -558,6 +572,36 @@ fn create_withdraw_to(
.add_attribute("status", "DO_THE_WITHDRAWAL"))
}

// reference: https://github.com/writersblockchain/secretpath-voting/commit/b0b5d8ac7b7d691c7d7ebf0bc52e5aedb8da7e86#diff-a982e501ba4bd05192a8c497bd5093517ea5606f9e341b9b7d09b233068da829R232
fn get_contract_code_hash(deps: DepsMut, contract_address: String) -> StdResult<String> {
let code_hash_query: cosmwasm_std::QueryRequest<cosmwasm_std::Empty> =
cosmwasm_std::QueryRequest::Stargate {
path: "/secret.compute.v1beta1.Query/CodeHashByContractAddress".into(),
data: Binary(Anybuf::new().append_string(1, contract_address).into_vec()),
};
let raw = to_vec(&code_hash_query).map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {}", serialize_err))
})?;
let code_hash = match deps.querier.raw_query(&raw) {
SystemResult::Err(system_err) => Err(StdError::generic_err(format!(
"Querier system error: {}",
system_err
))),
SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(format!(
"Querier contract error: {}",
contract_err
))),
SystemResult::Ok(ContractResult::Ok(value)) => Ok(value),
}?;
// Remove the "\n@" if it exists at the start of the code_hash
let mut code_hash_str = String::from_utf8(code_hash.to_vec())
.map_err(|err| StdError::generic_err(format!("Invalid UTF-8 sequence: {}", err)))?;
if code_hash_str.starts_with("\n@") {
code_hash_str = code_hash_str.trim_start_matches("\n@").to_string();
}
Ok(code_hash_str)
}

#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
let response = match msg {
Expand Down
2 changes: 1 addition & 1 deletion packages/secret-upload-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"scripts": {
"build": "./node_modules/.bin/tsc --build",
"clean": "./node_modules/.bin/tsc --clean",
"clean": "./node_modules/.bin/tsc --build --clean",
"start": "yarn build && node ./dist/index.js"
},
"dependencies": {
Expand Down
21 changes: 18 additions & 3 deletions packages/secret-upload-contract/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@ const rootPath = path.resolve(__dirname, '../../../'); // relative to ./dist
console.log('rootPath', rootPath)
const contract_wasm: any = fs.readFileSync(`${rootPath}/packages/secret-contracts/nunya-contract/contract.wasm`);

const gatewayAddress = "secret10ex7r7c4y704xyu086lf74ymhrqhypayfk7fkj";
// Secret Testnet
// reference: https://docs.scrt.network/secret-network-documentation/confidential-computing-layer/ethereum-evm-developer-toolkit/supported-networks/secret-gateway/secretpath-testnet-pulsar-3-contracts

// Code ID 3375
const gatewayAddress = "secret10ex7r7c4y704xyu086lf74ymhrqhypayfk7fkj";
const gatewayHash =
"012dd8efab9526dec294b6898c812ef6f6ad853e32172788f54ef3c305c1ecc5";

"ad8ca07ffba1cb26ebf952c29bc4eced8319c171430993e5b5089887f27b3f70";
const gatewayPublicKey =
"0x046d0aac3ef10e69055e934ca899f508ba516832dc74aa4ed4d741052ed5a568774d99d3bfed641a7935ae73aac8e34938db747c2f0e8b2aa95c25d069a575cc8b";

// Secret Mainnet
// reference: https://docs.scrt.network/secret-network-documentation/confidential-computing-layer/ethereum-evm-developer-toolkit/supported-networks/secret-gateway/secretpath-mainnet-secret-4-contracts

// Code ID 1533
// const gatewayAddress = "secret1qzk574v8lckjmqdg3r3qf3337pk45m7qd8x02a";
// const gatewayHash =
// "012dd8efab9526dec294b6898c812ef6f6ad853e32172788f54ef3c305c1ecc5";
// // TODO: is it correct that the Gateway public key is the same for both Mainnet and Testnet as shown in the docs here, even though they have a different Gateway address? https://docs.scrt.network/secret-network-documentation/confidential-computing-layer/ethereum-evm-developer-toolkit/supported-networks/secret-gateway
// const gatewayPublicKey =
// "0x04a0d632acd0d2f5da02fc385ea30a8deab4d5639d1a821a3a552625ad0f1759d0d2e80ca3adb236d90caf1b12e0ddf3a351c5729b5e00505472dca6fed5c31e2a";

const gatewayPublicKeyBytes = Buffer.from(
gatewayPublicKey.substring(2),
"hex"
Expand Down Expand Up @@ -88,6 +101,8 @@ async function main () {
tx?.arrayLog?.find((log: any) => log?.type === "message" && log?.key === "code_id")?.value
);

console.log("tx.rawLog: ", tx.rawLog);

console.log("codeId: ", codeId);

contractCodeHash = (
Expand Down

0 comments on commit 2c9cc9a

Please sign in to comment.