config.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use crate::gmssl::sm2::{SM2_KEY, SM2_POINT};
  2. #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
  3. #[serde(rename_all = "camelCase")]
  4. pub struct SdConfig {
  5. pub public_key: String,
  6. pub private_key: String,
  7. pub sin_key: String,
  8. pub supplier_code: String,
  9. pub callback_url: reqwest::Url,
  10. }
  11. impl SdConfig {
  12. pub fn new() -> Self {
  13. Default::default()
  14. }
  15. pub fn get_pk(&self) -> anyhow::Result<SM2_KEY> {
  16. unsafe {
  17. let mut key: SM2_KEY = std::mem::zeroed();
  18. let public_key = <SM2_POINT as TryFrom<&str>>::try_from(&self.public_key)?;
  19. key.set_pk(public_key);
  20. Ok(key)
  21. }
  22. }
  23. pub fn get_sk(&self) -> anyhow::Result<SM2_KEY> {
  24. unsafe {
  25. let mut key: SM2_KEY = std::mem::zeroed();
  26. key.set_sk(&self.private_key);
  27. Ok(key)
  28. }
  29. }
  30. }
  31. impl Default for SdConfig {
  32. fn default() -> Self {
  33. let key = SM2_KEY::key_generate();
  34. Self {
  35. public_key: key.get_pk(false),
  36. private_key: key.get_sk(),
  37. sin_key: "ZctLTt2tugU2ntZdVgMXssrGuENPH6V7b7ROYN88Msw=".to_string(),
  38. supplier_code: "CESHI".to_string(),
  39. callback_url: reqwest::Url::parse("https://api-test.schengle.com/gateway").unwrap(),
  40. }
  41. }
  42. }
  43. #[derive(serde::Serialize, serde::Deserialize, Debug)]
  44. #[serde(rename_all = "camelCase")]
  45. pub struct FscgConfig {
  46. pub pay_url: reqwest::Url,
  47. pub search_url: reqwest::Url,
  48. pub balance_url: reqwest::Url,
  49. pub app_id: String,
  50. pub app_secret: String,
  51. }
  52. impl Default for FscgConfig {
  53. fn default() -> Self {
  54. Self {
  55. app_id: "som app_id".to_string(),
  56. app_secret: "som app_secret".to_string(),
  57. balance_url: reqwest::Url::parse(
  58. "http://192.144.212.211:36000/gateway/flowservice/queryAccount.ws",
  59. )
  60. .unwrap(),
  61. search_url: reqwest::Url::parse(
  62. "http://192.144.212.211:36000/gateway/flowservice/queryorder.ws",
  63. )
  64. .unwrap(),
  65. pay_url: reqwest::Url::parse(
  66. "http://192.144.212.211:36000/gateway/flowservice/makeorder.ws",
  67. )
  68. .unwrap(),
  69. }
  70. }
  71. }
  72. #[derive(serde::Serialize, serde::Deserialize, Debug)]
  73. #[serde(rename_all = "camelCase")]
  74. pub struct Config {
  75. pub sd_config: SdConfig,
  76. pub addr: std::net::SocketAddr,
  77. pub timeout: u64,
  78. pub fscg_config: FscgConfig,
  79. }
  80. impl Default for Config {
  81. fn default() -> Self {
  82. Self {
  83. sd_config: Default::default(),
  84. addr: "0.0.0.0:8848".parse().unwrap(),
  85. timeout: Default::default(),
  86. fscg_config: Default::default(),
  87. }
  88. }
  89. }
  90. #[cfg(test)]
  91. mod tests {
  92. use super::*;
  93. #[test]
  94. fn test_name() {
  95. let config = SdConfig::default();
  96. let r = config.get_pk().unwrap();
  97. // let r = r.encrypt("sdgasfawerashfrgLLLLLLLLLLLLLLLLLLLLLLLJFB;ljebfffffffffffffffffffffffffffffffffffffffffffsdqwdadfwefdf");
  98. println!("{r:?}")
  99. }
  100. }