Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key-Value Store #5

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions TNLS-Gateways/secret/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R

// create hash of entire packet (used to verify the message wasn't modified in transit)
let data = [
env.block.chain_id.as_bytes(), // source network
routing_info.as_bytes(), // task_destination_network
msg.task.task_id.as_bytes(), // task ID
task_info.payload_hash.as_slice(), // original payload message
result.as_slice(), // result
task_info.callback_address.as_slice(), // callback address
task_info.callback_selector.as_slice(), // callback selector
env.block.chain_id.as_bytes(), // source network
routing_info.as_bytes(), // task_destination_network
msg.task.task_id.as_bytes(), // task ID
task_info.payload_hash.as_slice(), // original payload message
result.as_slice(), // result
task_info.callback_address.as_slice(), // callback address
task_info.callback_selector.as_slice(), // callback selector
]
.concat();
hasher.update(&data);
Expand Down Expand Up @@ -385,7 +385,7 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
// convert the hashes and signatures into hex byte strings
// NOTE: we need to perform the additional sha_256 because that is what the secret network API method does
// NOTE: The result_signature and packet_signature are both missing the recovery ID (v = 0 or 1), due to a Ethereum bug (v = 27 or 28).
// we need to either manually check both recovery IDs (v = 27 && v = 28) in the solidity contract. (i've leaved this the hard way.)
// we need to either manually check both recovery IDs (v = 27 && v = 28) in the solidity contract (I've leaved this the hard way.)

// or we can find out the two recovery IDs inside of this contract here and keep the solidity contract slim (which is probably the better way when it comes to gas costs):

Expand Down
3 changes: 3 additions & 0 deletions TNLS-Samples/Storage/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[alias]
unit-test = "test --lib --features backtraces"
schema = "run --example schema"
11 changes: 11 additions & 0 deletions TNLS-Samples/Storage/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.rs]
indent_size = 4
19 changes: 19 additions & 0 deletions TNLS-Samples/Storage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Build results
/target
Cargo.lock

# Cargo+Git helper file (https://github.com/rust-lang/cargo/blob/0.44.1/src/cargo/sources/git/utils.rs#L320-L327)
.cargo-ok

# Text file backups
**/*.rs.bk

# macOS
.DS_Store

# IDEs
*.iml
.idea
.vscode

*/node_modules
48 changes: 48 additions & 0 deletions TNLS-Samples/Storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "secret_evm_storage"
version = "0.1.0"
authors = ["SecretSaturn"]
edition = "2021"
exclude = ["contract.wasm", "hash.txt"]

[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[features]
default = []
# debug-print = ["cosmwasm-std/debug-print"] doesn't work anymore?
# for quicker tests, cargo test --lib
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
schema = ["cosmwasm-schema"]

[dependencies]
cosmwasm-schema = { version = "1.1.0", optional = true }
cosmwasm-std = { package = "secret-cosmwasm-std", version = "1.0.0" }
cosmwasm-storage = { package = "secret-cosmwasm-storage", version = "1.0.0" }
schemars = "0.8.11"
secret-toolkit = { version = "0.10.0", default-features = false, features = ["utils", "storage", "serialization", "viewing-key", "permit"] }
serde = { version = "1.0.144", default-features = false, features = ["derive"] }
serde-json-wasm = "1.0.0"
sha3 = "0.10.4"
base64 = "0.21.0"
secret-toolkit-serialization = { version = "0.10.0", features = ["base64"] }
# snafu = { version = "0.7.1" }
# thiserror = { version = "1.0.31" }
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 }

[[bin]]
name = "schema"
required-features = ["schema"]
72 changes: 72 additions & 0 deletions TNLS-Samples/Storage/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
.PHONY: check
check:
cargo check

.PHONY: clippy
clippy:
cargo clippy

PHONY: test
test: unit-test

.PHONY: unit-test
unit-test:
cargo unit-test

# This is a local build with debug-prints activated. Debug prints only show up
# in the local development chain (see the `start-server` command below)
# and mainnet won't accept contracts built with the feature enabled.
.PHONY: build _build
build: _build compress-wasm
_build:
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown --features="debug-print"

# This is a build suitable for uploading to mainnet.
# Calls to `debug_print` get removed by the compiler.
.PHONY: build-mainnet _build-mainnet
build-mainnet: _build-mainnet compress-wasm
_build-mainnet:
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown

# like build-mainnet, but slower and more deterministic
.PHONY: build-mainnet-reproducible
build-mainnet-reproducible:
docker run --rm -v "$$(pwd)":/contract \
--mount type=volume,source="$$(basename "$$(pwd)")_cache",target=/contract/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
enigmampc/secret-contract-optimizer:1.0.10

.PHONY: compress-wasm
compress-wasm:
cp ./target/wasm32-unknown-unknown/release/*.wasm ./contract.wasm
@## The following line is not necessary, may work only on linux (extra size optimization)
@# wasm-opt -Os ./contract.wasm -o ./contract.wasm
cat ./contract.wasm | gzip -9 > ./contract.wasm.gz

.PHONY: schema
schema:
cargo run --example schema

# Run local development chain with four funded accounts (named a, b, c, and d)
.PHONY: start-server
start-server: # CTRL+C to stop
docker run -it --rm \
-p 26657:26657 -p 26656:26656 -p 1317:1317 -p 5000:5000 \
-v $$(pwd):/root/code \
--name localsecret ghcr.io/scrtlabs/localsecret:v1.6.0

# This relies on running `start-server` in another console
# You can run other commands on the secretcli inside the dev image
# by using `docker exec localsecret secretcli`.
.PHONY: store-contract-local
store-contract-local:
docker exec localsecret secretcli tx compute store -y --from a --gas 1000000 /root/code/contract.wasm.gz

.PHONY: integration-test
integration-test:
npx ts-node tests/integration.ts

.PHONY: clean
clean:
cargo clean
-rm -f ./contract.wasm ./contract.wasm.gz
15 changes: 15 additions & 0 deletions TNLS-Samples/Storage/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# stable
newline_style = "unix"
hard_tabs = false
tab_spaces = 4

# unstable... should we require `rustup run nightly cargo fmt` ?
# or just update the style guide when they are stable?
#fn_single_line = true
#format_code_in_doc_comments = true
#overflow_delimited_expr = true
#reorder_impl_items = true
#struct_field_align_threshold = 20
#struct_lit_single_line = true
#report_todo = "Always"

85 changes: 85 additions & 0 deletions TNLS-Samples/Storage/schema/handle_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "HandleMsg",
"anyOf": [
{
"type": "object",
"required": [
"input"
],
"properties": {
"input": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"$ref": "#/definitions/PrivContractHandleMsg"
}
}
}
}
}
],
"definitions": {
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>",
"type": "string"
},
"HumanAddr": {
"type": "string"
},
"PrivContractHandleMsg": {
"type": "object",
"required": [
"handle",
"input_hash",
"input_values",
"signature",
"task_id",
"user_address"
],
"properties": {
"handle": {
"description": "Handle function to be called in the destination contract.",
"type": "string"
},
"input_hash": {
"description": "SHA256 hash of `input_values`.",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"input_values": {
"description": "JSON string of decrypted user inputs.",
"type": "string"
},
"signature": {
"description": "Signature of `input_hash`, signed by the private gateway.",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"task_id": {
"description": "Task ID passed along for later verification.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"user_address": {
"description": "Public network user address.",
"allOf": [
{
"$ref": "#/definitions/HumanAddr"
}
]
}
}
}
}
}
30 changes: 30 additions & 0 deletions TNLS-Samples/Storage/schema/init_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InitMsg",
"type": "object",
"required": [
"gateway_address",
"gateway_hash",
"gateway_key"
],
"properties": {
"gateway_address": {
"$ref": "#/definitions/HumanAddr"
},
"gateway_hash": {
"type": "string"
},
"gateway_key": {
"$ref": "#/definitions/Binary"
}
},
"definitions": {
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>",
"type": "string"
},
"HumanAddr": {
"type": "string"
}
}
}
17 changes: 17 additions & 0 deletions TNLS-Samples/Storage/schema/query_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"anyOf": [
{
"type": "object",
"required": [
"query"
],
"properties": {
"query": {
"type": "object"
}
}
}
]
}
17 changes: 17 additions & 0 deletions TNLS-Samples/Storage/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use secret_rng::msg::{HandleMsg, InitMsg, QueryMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InitMsg), &out_dir);
export_schema(&schema_for!(HandleMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
}
Loading
Loading