Преглед изворни кода

cosmwasm: Add wormhole-bindings package

This package defines the bindings into the native wormhole module on
wormchain.

Part of #1880 and #1881.
Chirantan Ekbote пре 3 година
родитељ
комит
b1fce660c8

+ 41 - 0
cosmwasm/Cargo.lock

@@ -28,6 +28,12 @@ dependencies = [
  "version_check",
 ]
 
+[[package]]
+name = "anyhow"
+version = "1.0.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
+
 [[package]]
 name = "autocfg"
 version = "1.0.1"
@@ -184,6 +190,30 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "cosmwasm-schema"
+version = "1.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a227cfeb9a7152b26a354b1c990e930e962f75fd68f57ab5ae2ef888c8524292"
+dependencies = [
+ "cosmwasm-schema-derive",
+ "schemars",
+ "serde",
+ "serde_json",
+ "thiserror",
+]
+
+[[package]]
+name = "cosmwasm-schema-derive"
+version = "1.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3626cb42eef870de67f791e873711255325224d86f281bf628c42abd295f3a14"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "cosmwasm-std"
 version = "1.0.0"
@@ -2052,6 +2082,17 @@ version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 
+[[package]]
+name = "wormhole-bindings"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "cosmwasm-schema",
+ "cosmwasm-std",
+ "schemars",
+ "serde",
+]
+
 [[package]]
 name = "wormhole-bridge-terra-2"
 version = "0.1.0"

+ 2 - 0
cosmwasm/Cargo.toml

@@ -6,6 +6,7 @@ members = [
     "contracts/token-bridge",
     "contracts/shutdown-token-bridge",
     "contracts/mock-bridge-integration",
+    "packages/wormhole-bindings",
 ]
 
 [profile.release]
@@ -22,5 +23,6 @@ overflow-checks = true
 [patch.crates-io]
 cw20-wrapped-2 = { path = "contracts/cw20-wrapped" }
 token-bridge-terra-2 = { path = "contracts/token-bridge" }
+wormhole-bindings = { path = "packages/wormhole-bindings" }
 wormhole-bridge-terra-2 = { path = "contracts/wormhole" }
 

+ 1 - 0
cosmwasm/Dockerfile

@@ -6,6 +6,7 @@ FROM cosmwasm/workspace-optimizer:0.12.6@sha256:e6565a5e87c830ef3e8775a9035006b3
 COPY Cargo.lock /code/
 COPY Cargo.toml /code/
 COPY contracts /code/contracts
+COPY packages /code/packages
 
 # Support additional root CAs
 COPY README.md cert.pem* /certs/

+ 12 - 0
cosmwasm/packages/wormhole-bindings/Cargo.toml

@@ -0,0 +1,12 @@
+[package]
+name = "wormhole-bindings"
+version = "0.1.0"
+authors = ["Wormhole Project Contributors"]
+edition = "2021"
+
+[dependencies]
+anyhow = "1"
+cosmwasm-schema = "1"
+cosmwasm-std = "1"
+schemars = "0.8.8"
+serde = { version = "1.0.137", default-features = false, features = ["derive"] }

+ 127 - 0
cosmwasm/packages/wormhole-bindings/schema/wormhole_query.json

@@ -0,0 +1,127 @@
+{
+  "$schema": "http://json-schema.org/draft-07/schema#",
+  "title": "WormholeQuery",
+  "oneOf": [
+    {
+      "description": "Verifies that `data` has been signed by a quorum of guardians from `guardian_set_index`.",
+      "type": "object",
+      "required": [
+        "verify_quorum"
+      ],
+      "properties": {
+        "verify_quorum": {
+          "type": "object",
+          "required": [
+            "data",
+            "guardian_set_index",
+            "signatures"
+          ],
+          "properties": {
+            "data": {
+              "$ref": "#/definitions/Binary"
+            },
+            "guardian_set_index": {
+              "type": "integer",
+              "format": "uint32",
+              "minimum": 0.0
+            },
+            "signatures": {
+              "type": "array",
+              "items": {
+                "$ref": "#/definitions/Signature"
+              }
+            }
+          },
+          "additionalProperties": false
+        }
+      },
+      "additionalProperties": false
+    },
+    {
+      "description": "Verifies that `data` has been signed by a guardian from `guardian_set_index`.",
+      "type": "object",
+      "required": [
+        "verify_signature"
+      ],
+      "properties": {
+        "verify_signature": {
+          "type": "object",
+          "required": [
+            "data",
+            "guardian_set_index",
+            "signature"
+          ],
+          "properties": {
+            "data": {
+              "$ref": "#/definitions/Binary"
+            },
+            "guardian_set_index": {
+              "type": "integer",
+              "format": "uint32",
+              "minimum": 0.0
+            },
+            "signature": {
+              "$ref": "#/definitions/Signature"
+            }
+          },
+          "additionalProperties": false
+        }
+      },
+      "additionalProperties": false
+    },
+    {
+      "description": "Returns the number of signatures necessary for quorum for the given guardian set index.",
+      "type": "object",
+      "required": [
+        "calculate_quorum"
+      ],
+      "properties": {
+        "calculate_quorum": {
+          "type": "object",
+          "required": [
+            "guardian_set_index"
+          ],
+          "properties": {
+            "guardian_set_index": {
+              "type": "integer",
+              "format": "uint32",
+              "minimum": 0.0
+            }
+          },
+          "additionalProperties": false
+        }
+      },
+      "additionalProperties": false
+    }
+  ],
+  "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"
+    },
+    "Signature": {
+      "type": "object",
+      "required": [
+        "index",
+        "signature"
+      ],
+      "properties": {
+        "index": {
+          "description": "The index of the guardian in the guardian set.",
+          "type": "integer",
+          "format": "uint8",
+          "minimum": 0.0
+        },
+        "signature": {
+          "description": "The signature, which should be exactly 65 bytes with the following layout:\n\n```markdown 0  .. 64: Signature   (ECDSA) 64 .. 65: Recovery ID (ECDSA) ```",
+          "allOf": [
+            {
+              "$ref": "#/definitions/Binary"
+            }
+          ]
+        }
+      },
+      "additionalProperties": false
+    }
+  }
+}

+ 6 - 0
cosmwasm/packages/wormhole-bindings/src/lib.rs

@@ -0,0 +1,6 @@
+mod query;
+
+pub use query::*;
+
+#[no_mangle]
+extern "C" fn requires_wormhole() {}

+ 42 - 0
cosmwasm/packages/wormhole-bindings/src/query.rs

@@ -0,0 +1,42 @@
+use cosmwasm_schema::{cw_serde, QueryResponses};
+use cosmwasm_std::{Binary, CustomQuery, Empty};
+
+#[cw_serde]
+pub struct Signature {
+    /// The index of the guardian in the guardian set.
+    pub index: u8,
+
+    /// The signature, which should be exactly 65 bytes with the following layout:
+    ///
+    /// ```markdown
+    /// 0  .. 64: Signature   (ECDSA)
+    /// 64 .. 65: Recovery ID (ECDSA)
+    /// ```
+    pub signature: Binary,
+}
+
+#[cw_serde]
+#[derive(QueryResponses)]
+pub enum WormholeQuery {
+    /// Verifies that `data` has been signed by a quorum of guardians from `guardian_set_index`.
+    #[returns(Empty)]
+    VerifyQuorum {
+        data: Binary,
+        guardian_set_index: u32,
+        signatures: Vec<Signature>,
+    },
+
+    /// Verifies that `data` has been signed by a guardian from `guardian_set_index`.
+    #[returns(Empty)]
+    VerifySignature {
+        data: Binary,
+        guardian_set_index: u32,
+        signature: Signature,
+    },
+
+    /// Returns the number of signatures necessary for quorum for the given guardian set index.
+    #[returns(u32)]
+    CalculateQuorum { guardian_set_index: u32 },
+}
+
+impl CustomQuery for WormholeQuery {}