فهرست منبع

Add emitter type and add wasm function for it

- It requires solitaire and it requires nightly rust
- No logic is applied, code is from p2w solana contract. (Eventually will be removed from there)
Ali Behjati 3 سال پیش
والد
کامیت
fa052ff355

+ 50 - 54
third_party/pyth/p2w-sdk/rust/Cargo.lock

@@ -4,14 +4,9 @@ version = 3
 
 [[package]]
 name = "ahash"
-version = "0.7.6"
+version = "0.4.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
-dependencies = [
- "getrandom 0.2.4",
- "once_cell",
- "version_check",
-]
+checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
 
 [[package]]
 name = "aho-corasick"
@@ -119,9 +114,9 @@ checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae"
 
 [[package]]
 name = "borsh"
-version = "0.9.3"
+version = "0.9.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "15bf3650200d8bffa99015595e10f1fbd17de07abbc25bb067da79e769939bfa"
+checksum = "18dda7dc709193c0d86a1a51050a926dc3df1cf262ec46a23a25dba421ea1924"
 dependencies = [
  "borsh-derive",
  "hashbrown",
@@ -129,9 +124,9 @@ dependencies = [
 
 [[package]]
 name = "borsh-derive"
-version = "0.9.3"
+version = "0.9.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6441c552f230375d18e3cc377677914d2ca2b0d36e52129fe15450a2dce46775"
+checksum = "684155372435f578c0fa1acd13ebbb182cc19d6b38b64ae7901da4393217d264"
 dependencies = [
  "borsh-derive-internal",
  "borsh-schema-derive-internal",
@@ -142,9 +137,9 @@ dependencies = [
 
 [[package]]
 name = "borsh-derive-internal"
-version = "0.9.3"
+version = "0.9.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5449c28a7b352f2d1e592a8a28bf139bc71afb0764a14f3c02500935d8c44065"
+checksum = "2102f62f8b6d3edeab871830782285b64cc1830168094db05c8e458f209bc5c3"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -153,9 +148,9 @@ dependencies = [
 
 [[package]]
 name = "borsh-schema-derive-internal"
-version = "0.9.3"
+version = "0.9.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cdbd5696d8bfa21d53d9fe39a714a18538bad11492a42d066dbbc395fb1951c0"
+checksum = "196c978c4c9b0b142d446ef3240690bf5a8a33497074a113ff9a337ccb750483"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -361,26 +356,15 @@ dependencies = [
  "cfg-if",
  "js-sys",
  "libc",
- "wasi 0.9.0+wasi-snapshot-preview1",
+ "wasi",
  "wasm-bindgen",
 ]
 
-[[package]]
-name = "getrandom"
-version = "0.2.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c"
-dependencies = [
- "cfg-if",
- "libc",
- "wasi 0.10.2+wasi-snapshot-preview1",
-]
-
 [[package]]
 name = "hashbrown"
-version = "0.11.2"
+version = "0.9.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
+checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
 dependencies = [
  "ahash",
 ]
@@ -573,12 +557,6 @@ dependencies = [
  "autocfg",
 ]
 
-[[package]]
-name = "once_cell"
-version = "1.9.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
-
 [[package]]
 name = "opaque-debug"
 version = "0.3.0"
@@ -592,6 +570,7 @@ dependencies = [
  "pyth-client",
  "serde",
  "solana-program",
+ "solitaire",
  "wasm-bindgen",
 ]
 
@@ -675,7 +654,7 @@ version = "0.7.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
 dependencies = [
- "getrandom 0.1.16",
+ "getrandom",
  "libc",
  "rand_chacha",
  "rand_core",
@@ -698,7 +677,7 @@ version = "0.5.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
 dependencies = [
- "getrandom 0.1.16",
+ "getrandom",
 ]
 
 [[package]]
@@ -736,6 +715,18 @@ version = "0.6.25"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
 
+[[package]]
+name = "rocksalt"
+version = "0.1.0"
+dependencies = [
+ "byteorder",
+ "proc-macro2",
+ "quote",
+ "sha3",
+ "solana-program",
+ "syn",
+]
+
 [[package]]
 name = "rustc_version"
 version = "0.4.0"
@@ -842,9 +833,9 @@ checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
 
 [[package]]
 name = "solana-frozen-abi"
-version = "1.9.8"
+version = "1.9.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bd5652ad6d0238e00a16ffdbb4d9c9fb9b7cca55d5c2beb6f2a3c360afdb0d4c"
+checksum = "c89bcde59ac3e8d4dbf7c4d990b0627b8ca0d25394c4ce17896dde7a1452e40c"
 dependencies = [
  "bs58",
  "bv",
@@ -862,9 +853,9 @@ dependencies = [
 
 [[package]]
 name = "solana-frozen-abi-macro"
-version = "1.9.8"
+version = "1.9.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5be158b263744a7be50af5cf061f2d377f3aba3a67318701dd8e17f2f559fea1"
+checksum = "56a7d630da35993631ecc4dd155f92d0d58000cdde3d5e2764fe9fd49d20a3a8"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -874,9 +865,9 @@ dependencies = [
 
 [[package]]
 name = "solana-logger"
-version = "1.9.8"
+version = "1.9.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6464c60efcc0a31a27bdcfaaf9b77f259e413aacca3736714240334bbf3be576"
+checksum = "6eaf925bb665de46f96fcea2c8a900d0d870a96fd1f50cf2bad16e22a1da71c4"
 dependencies = [
  "env_logger",
  "lazy_static",
@@ -885,9 +876,9 @@ dependencies = [
 
 [[package]]
 name = "solana-program"
-version = "1.9.8"
+version = "1.9.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "815b58c51e1d634280de50ab70695cf2a837cd62cafec59a1004a77412b478f8"
+checksum = "2fc4d7a0baa649a3bda06d6a1cc30bd3d8ac692702a75fa8e76369cf7b3f6329"
 dependencies = [
  "base64 0.13.0",
  "bincode",
@@ -901,7 +892,7 @@ dependencies = [
  "console_error_panic_hook",
  "console_log",
  "curve25519-dalek",
- "getrandom 0.1.16",
+ "getrandom",
  "itertools",
  "js-sys",
  "lazy_static",
@@ -928,9 +919,9 @@ dependencies = [
 
 [[package]]
 name = "solana-sdk-macro"
-version = "1.9.8"
+version = "1.9.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ac08315d63f5a7e3153ac6a0dbc97ecde3e8bbd2935537d8394d52aaa4623b18"
+checksum = "ec22a924c73abe3376a2046715a2f6a9ae4094095b8ea08e8e56e8de198264ad"
 dependencies = [
  "bs58",
  "proc-macro2",
@@ -939,6 +930,17 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "solitaire"
+version = "0.1.0"
+dependencies = [
+ "borsh",
+ "byteorder",
+ "rocksalt",
+ "sha3",
+ "solana-program",
+]
+
 [[package]]
 name = "subtle"
 version = "2.4.1"
@@ -1018,12 +1020,6 @@ version = "0.9.0+wasi-snapshot-preview1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
 
-[[package]]
-name = "wasi"
-version = "0.10.2+wasi-snapshot-preview1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
-
 [[package]]
 name = "wasm-bindgen"
 version = "0.2.79"

+ 2 - 1
third_party/pyth/p2w-sdk/rust/Cargo.toml

@@ -3,7 +3,7 @@ name = "p2w-sdk"
 version = "0.1.0"
 authors = ["Wormhole Contributors <contact@certus.one>"]
 edition = "2018"
-description = "Pyth Price Sdk"
+description = "Pyth to Wormhole SDK"
 
 
 [lib]
@@ -16,4 +16,5 @@ wasm = ["wasm-bindgen"]
 serde = { version = "1.0.103", default-features = false, features = ["derive"] }
 pyth-client = { version = "0.5.0", features = ["no-entrypoint"] }
 wasm-bindgen = { version = "0.2.74", features = ["serde-serialize"], optional = true}
+solitaire = { path = "../../../../solana/solitaire/program" }
 solana-program = "1.8.16"

+ 4 - 0
third_party/pyth/p2w-sdk/rust/README.md

@@ -0,0 +1,4 @@
+# P2W SDK
+
+## Development
+This crate requires rust nightly to compile.

+ 9 - 0
third_party/pyth/p2w-sdk/rust/src/lib.rs

@@ -18,6 +18,12 @@ use pyth_client::{
     PriceStatus,
     PriceType,
 };
+
+use solitaire::{
+    Derive,
+    Info,
+};
+
 use solana_program::clock::UnixTimestamp;
 use solana_program::pubkey::Pubkey;
 
@@ -36,6 +42,9 @@ pub const P2W_FORMAT_VERSION: u16 = 2;
 
 pub const PUBKEY_LEN: usize = 32;
 
+/// Emmitter Address to wormhole is a PDA with seed p2w-emmiter from attestation contract
+pub type P2WEmitter<'b> = Derive<Info<'b>, "p2w-emitter">;
+
 /// Decides the format of following bytes
 #[repr(u8)]
 pub enum PayloadId {

+ 13 - 0
third_party/pyth/p2w-sdk/rust/src/wasm.rs

@@ -1,10 +1,23 @@
+use solana_program::pubkey::Pubkey;
+use solitaire::Seeded;
+use std::str::FromStr;
 use wasm_bindgen::prelude::*;
 
+
 use crate::{
     BatchPriceAttestation,
+    P2WEmitter,
     PriceAttestation,
 };
 
+#[wasm_bindgen]
+pub fn get_emitter_address(program_id: String) -> Vec<u8> {
+    let program_id = Pubkey::from_str(program_id.as_str()).unwrap();
+    let emitter = P2WEmitter::key(None, &program_id);
+
+    emitter.to_bytes().to_vec()
+}
+
 #[wasm_bindgen]
 pub fn parse_attestation(bytes: Vec<u8>) -> JsValue {
     let a = PriceAttestation::deserialize(bytes.as_slice()).unwrap();