base58_encoding.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::borsh_encoding::BorshToken;
  3. use crate::{account_new, build_solidity};
  4. use base58::ToBase58;
  5. #[test]
  6. fn print_addresses() {
  7. let mut vm = build_solidity(
  8. r#"
  9. contract Base58 {
  10. function print_this(address addr) pure public {
  11. print("{}".format(addr));
  12. }
  13. function print_as_hex(address addr) pure public {
  14. print("{:x}".format(addr));
  15. }
  16. }
  17. "#,
  18. );
  19. let data_account = vm.initialize_data_account();
  20. vm.function("new")
  21. .accounts(vec![("dataAccount", data_account)])
  22. .call();
  23. for _ in 0..10 {
  24. let account = account_new();
  25. let _ = vm
  26. .function("print_this")
  27. .arguments(&[BorshToken::Address(account)])
  28. .call();
  29. let mut base_58 = account.to_base58();
  30. while base_58.len() < 44 {
  31. // Rust's to_base58() ignores leading zeros in the byte array,
  32. // so it won't transform them into ones. On the other hand, Solana addresses
  33. // can start with leading ones: 11128aXFh5abEooZ2ouNDjPjk2TqDaHjG6JkX74vK4q is
  34. // a valid address.
  35. base_58.insert(0, '1');
  36. }
  37. assert_eq!(vm.logs, base_58);
  38. vm.logs.clear();
  39. let _ = vm
  40. .function("print_as_hex")
  41. .arguments(&[BorshToken::Address(account)])
  42. .call();
  43. let decoded = hex::decode(vm.logs.as_str()).unwrap();
  44. assert_eq!(account, decoded.as_ref());
  45. vm.logs.clear();
  46. }
  47. }