diff --git a/Cargo.lock b/Cargo.lock index 26bb4b71a..1be22c88f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -877,6 +877,7 @@ dependencies = [ "ed25519-dalek", "rand 0.7.3", "soroban-sdk", + "stellar-strkey", ] [[package]] diff --git a/soroban-auth/Cargo.toml b/soroban-auth/Cargo.toml index 7b8697136..93cf8b2ed 100644 --- a/soroban-auth/Cargo.toml +++ b/soroban-auth/Cargo.toml @@ -26,6 +26,7 @@ rand = { version = "0.7.3", optional = true } [dev_dependencies] soroban-sdk = { workspace = true, features = ["testutils"] } +stellar-strkey = { workspace = true } ed25519-dalek = { version = "1.0.1" } rand = { version = "0.7.3" } diff --git a/soroban-auth/src/tests/test_ed25519.rs b/soroban-auth/src/tests/test_ed25519.rs index a1cbe0094..79fa73fda 100644 --- a/soroban-auth/src/tests/test_ed25519.rs +++ b/soroban-auth/src/tests/test_ed25519.rs @@ -1,5 +1,7 @@ extern crate std; +use core::str::FromStr; + use soroban_sdk::{ contractimpl, symbol, testutils::{Ledger, LedgerInfo}, @@ -7,7 +9,7 @@ use soroban_sdk::{ }; use crate::{ - testutils::ed25519::{generate, sign}, + testutils::ed25519::{generate, sign, signer}, verify, Signature, }; @@ -58,3 +60,41 @@ fn test() { client.examplefn(&sig, &1, &2); } + +#[test] +fn test_build_keypair() { + let env = Env::default(); + let contract_id = BytesN::from_array(&env, &[0; 32]); + env.register_contract(&contract_id, ExampleContract); + let client = ExampleContractClient::new(&env, &contract_id); + + env.ledger().set(LedgerInfo { + base_reserve: 0, + network_passphrase: "soroban-auth test".as_bytes().to_vec(), + protocol_version: 0, + sequence_number: 0, + timestamp: 0, + }); + + std::println!("network: {:?}", env.ledger().network_passphrase()); + std::println!("contract id: {:?}", contract_id); + std::println!("name: {:?}", symbol!("examplefn")); + + let key = stellar_strkey::StrkeyPrivateKeyEd25519::from_str( + "SC24O4H2LT4PVOYCWMKUSD2DL4UL26IYGPFKANDH7S4MU6JVQEFOS7DC", + ) + .unwrap(); + let (id, signer) = signer(&env, &key.0); + std::println!("signer: {:?}", signer); + std::println!("id: {:?}", id); + let sig = sign( + &env, + &signer, + &contract_id, + symbol!("examplefn"), + (&id, &1, &2), + ); + std::println!("signature: {:?}", sig); + + client.examplefn(&sig, &1, &2); +} diff --git a/soroban-auth/src/testutils.rs b/soroban-auth/src/testutils.rs index 2a5840e6c..93767f5ab 100644 --- a/soroban-auth/src/testutils.rs +++ b/soroban-auth/src/testutils.rs @@ -38,6 +38,25 @@ pub mod ed25519 { (signer.identifier(env), signer) } + /// Create a signer from a ed25519 secret key. + pub fn signer( + env: &Env, + secret_key: &[u8; 32], + ) -> ( + IdentifierValue, + impl Identifier + Sign + Debug, + ) { + let secret = ed25519_dalek::SecretKey::from_bytes(secret_key).unwrap(); + let public = ed25519_dalek::PublicKey::from(&secret); + let signer = ed25519_dalek::Keypair { secret, public }; + (signer.identifier(env), signer) + } + + /// Create an identifier from a ed25519 public key. + pub fn identifier(env: &Env, public_key: &[u8; 32]) -> IdentifierValue { + IdentifierValue::Ed25519(public_key.into_val(env)) + } + /// Sign a [`SignaturePayload`] constructed using the arguments. /// /// The returned [`Signature`] can be verified by [`verify`](crate::verify)