-
Notifications
You must be signed in to change notification settings - Fork 39
/
sign_ed25519.rs
66 lines (55 loc) · 2.23 KB
/
sign_ed25519.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! In this example we will sign with Ed25519.
//!
//! Rename `.env.example` to `.env` first, then run the command:
//! ```sh
//! cargo run --release --all-features --example sign_ed25519
//! ```
use crypto::keys::bip44::Bip44;
use iota_sdk::{
client::{
constants::SHIMMER_COIN_TYPE,
hex_public_key_to_bech32_address,
secret::{stronghold::StrongholdSecretManager, SecretManage, SecretManager},
},
crypto::keys::bip39::Mnemonic,
wallet::Result,
};
const FOUNDRY_METADATA: &str = r#"{"standard":"IRC30","name":"NativeToken","description":"A native token","symbol":"NT","decimals":6,"logoUrl":"https://my.website/nativeToken.png"}"#;
const ACCOUNT_INDEX: u32 = 0;
const INTERNAL_ADDRESS: bool = false;
const ADDRESS_INDEX: u32 = 0;
#[tokio::main]
async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();
for var in ["STRONGHOLD_PASSWORD", "MNEMONIC"] {
std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example"));
}
// Setup Stronghold secret_manager
let stronghold = StrongholdSecretManager::builder()
.password(std::env::var("STRONGHOLD_PASSWORD").unwrap())
.build("sign_ed25519.stronghold")?;
stronghold
.store_mnemonic(Mnemonic::from(std::env::var("MNEMONIC").unwrap()))
.await?;
let bip44_chain = Bip44::new(SHIMMER_COIN_TYPE)
.with_account(ACCOUNT_INDEX)
.with_change(INTERNAL_ADDRESS as _)
.with_address_index(ADDRESS_INDEX);
let message = FOUNDRY_METADATA.as_bytes();
let signature = SecretManager::Stronghold(stronghold)
.sign_ed25519(message, bip44_chain)
.await?;
println!(
"Public key: {}\nSignature: {}",
prefix_hex::encode(signature.public_key_bytes().as_ref()),
prefix_hex::encode(signature.signature().to_bytes()),
);
// Hash the public key to get the address
let bech32_address =
hex_public_key_to_bech32_address(&prefix_hex::encode(signature.public_key_bytes().as_ref()), "rms")?;
println!("Address: {bech32_address}");
Ok(())
}