From ae5d254e732b17743edbfb66ca9d9cda9099c6c6 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Tue, 28 Sep 2021 13:10:43 -0700 Subject: [PATCH] Move solana-specific parts of crypto/ into sdk/ --- zk-token-sdk/Cargo.toml | 24 +++++ zk-token-sdk/src/lib.rs | 2 + .../src/zk_token_proof_instruction.rs | 92 +++++++++++++++++++ zk-token-sdk/src/zk_token_proof_program.rs | 2 + 4 files changed, 120 insertions(+) create mode 100644 zk-token-sdk/Cargo.toml create mode 100644 zk-token-sdk/src/lib.rs create mode 100644 zk-token-sdk/src/zk_token_proof_instruction.rs create mode 100644 zk-token-sdk/src/zk_token_proof_program.rs diff --git a/zk-token-sdk/Cargo.toml b/zk-token-sdk/Cargo.toml new file mode 100644 index 000000000..c76da2e06 --- /dev/null +++ b/zk-token-sdk/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "spl-zk-token-program-sdk" +description = "Solana Program Library ZkToken Program SDK" +authors = ["Solana Maintainers "] +repository = "https://github.com/solana-labs/solana-program-library" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +bytemuck = { version = "1.7.2", features = ["derive"] } +#getrandom = { version = "0.1", features = ["dummy"] } +num-derive = "0.3" +num-traits = "0.2" +solana-program = "=1.7.11" +spl-zk-token-crypto = { path = "../crypto" } + +#[features] +#test-bpf = [] +# +#[dev-dependencies] +#assert_matches = "1.4.0" +#solana-program-test = "=1.7.11" +#solana-validator = "=1.7.11" diff --git a/zk-token-sdk/src/lib.rs b/zk-token-sdk/src/lib.rs new file mode 100644 index 000000000..373a9c8d1 --- /dev/null +++ b/zk-token-sdk/src/lib.rs @@ -0,0 +1,2 @@ +pub mod zk_token_proof_instruction; +pub mod zk_token_proof_program; diff --git a/zk-token-sdk/src/zk_token_proof_instruction.rs b/zk-token-sdk/src/zk_token_proof_instruction.rs new file mode 100644 index 000000000..1cbf1854f --- /dev/null +++ b/zk-token-sdk/src/zk_token_proof_instruction.rs @@ -0,0 +1,92 @@ +///! Instructions provided by the ZkToken Proof program +pub use spl_zk_token_crypto::instruction::*; + +use { + bytemuck::Pod, + num_derive::{FromPrimitive, ToPrimitive}, + num_traits::{FromPrimitive, ToPrimitive}, + solana_program::{instruction::Instruction, pubkey::Pubkey}, + spl_zk_token_crypto::pod::*, +}; + +#[derive(Clone, Copy, Debug, FromPrimitive, ToPrimitive, PartialEq)] +#[repr(u8)] +pub enum ProofInstruction { + /// Verify a `UpdateAccountPkData` struct + /// + /// Accounts expected by this instruction: + /// None + /// + /// Data expected by this instruction: + /// `UpdateAccountPkData` + /// + VerifyUpdateAccountPk, + + /// Verify a `CloseAccountData` struct + /// + /// Accounts expected by this instruction: + /// None + /// + /// Data expected by this instruction: + /// `CloseAccountData` + /// + VerifyCloseAccount, + + /// Verify a `WithdrawData` struct + /// + /// Accounts expected by this instruction: + /// None + /// + /// Data expected by this instruction: + /// `WithdrawData` + /// + VerifyWithdraw, + + /// Verify a `TransferRangeProofData` struct + /// + /// Accounts expected by this instruction: + /// None + /// + /// Data expected by this instruction: + /// `TransferRangeProofData` + /// + VerifyTransferRangeProofData, + + /// Verify a `TransferValidityProofData` struct + /// + /// Accounts expected by this instruction: + /// None + /// + /// Data expected by this instruction: + /// `TransferValidityProofData` + /// + VerifyTransferValidityProofData, +} + +impl ProofInstruction { + pub fn encode(&self, proof: &T) -> Instruction { + let mut data = vec![ToPrimitive::to_u8(self).unwrap()]; + data.extend_from_slice(pod_bytes_of(proof)); + Instruction { + program_id: crate::zk_token_proof_program::id(), + accounts: vec![], + data, + } + } + + pub fn decode_type(program_id: &Pubkey, input: &[u8]) -> Option { + if *program_id != crate::zk_token_proof_program::id() || input.is_empty() { + None + } else { + FromPrimitive::from_u8(input[0]) + } + } + + pub fn decode_data(input: &[u8]) -> Option<&T> { + if input.is_empty() { + None + } else { + pod_from_bytes(&input[1..]) + } + } +} diff --git a/zk-token-sdk/src/zk_token_proof_program.rs b/zk-token-sdk/src/zk_token_proof_program.rs new file mode 100644 index 000000000..498c4cce8 --- /dev/null +++ b/zk-token-sdk/src/zk_token_proof_program.rs @@ -0,0 +1,2 @@ +// Program Id of the ZkToken Proof program +solana_program::declare_id!("ZkTokenProof1111111111111111111111111111111");