use anyhow::{Context, Result};
use serde_json::json;
use crate::api::{ApiClient, GetPubkeyRequest};
use crate::config::{self, NetworkConfig};
use crate::crypto;
use crate::near::{ContractCaller, NearClient};
pub async fn create(network: &NetworkConfig) -> Result<()> {
let creds = config::load_credentials(network)?;
let near = NearClient::new(network);
let caller = ContractCaller::from_credentials(&creds, network)?;
let api = ApiClient::new(network);
let nonce = near
.get_next_payment_key_nonce(&creds.account_id)
.await
.context("Failed to get next payment key nonce")?;
eprintln!("Creating payment key (nonce: {nonce})...");
let secret = crypto::generate_payment_key_secret();
let secrets_json = json!({
"key": secret,
"project_ids": [],
"max_per_call": "0",
"initial_balance": "0"
})
.to_string();
let pubkey = api
.get_secrets_pubkey(
&GetPubkeyRequest {
accessor: json!({ "type": "System", "PaymentKey": {} }),
owner: creds.account_id.clone(),
profile: Some(nonce.to_string()),
secrets_json: secrets_json.clone(),
},
None,
)
.await
.context("Failed to get keystore pubkey")?;
let encrypted = crypto::encrypt_secrets(&pubkey.pubkey, &secrets_json)?;
let api_key = format!("{}:{}:{}", creds.account_id, nonce, secret);
let stored_at = config::save_payment_key(&network.network_id, &creds.account_id, nonce, &api_key)
.context("Refusing to create a payment key that cannot be written down")?;
let deposit = 100_000_000_000_000_000_000_000u128; let gas = 100_000_000_000_000u64;
caller
.call_contract(
"store_secrets",
json!({
"accessor": { "System": "PaymentKey" },
"profile": nonce.to_string(),
"encrypted_secrets_base64": encrypted,
"access": "AllowAll",
"vault_id": null,
}),
gas,
deposit,
)
.await
.context("Failed to store payment key")?;
eprintln!("Payment key created (nonce: {nonce})");
println!("{api_key}");
eprintln!(
"\nSaved to {} (owner-only). Read it back with: outlayer keys show {nonce}",
stored_at.display()
);
eprintln!("Add a balance before using it: outlayer keys topup {nonce} --usd 1");
Ok(())
}
pub fn show(network: &NetworkConfig, nonce: u32) -> Result<()> {
let creds = config::load_credentials(network)?;
match config::load_payment_key(&network.network_id, &creds.account_id, nonce) {
Some(key) => {
println!("{key}");
Ok(())
}
None => anyhow::bail!(
"No payment key {}:{} on this machine. It is not recoverable from the chain — \
the contract holds it encrypted for the keystore and nothing gives it back. \
If the key still has a balance, withdraw it and create a new key.",
creds.account_id,
nonce
),
}
}
pub async fn list(network: &NetworkConfig) -> Result<()> {
let creds = config::load_credentials(network)?;
let near = NearClient::new(network);
let api = ApiClient::new(network);
let secrets = near.list_user_secrets(&creds.account_id).await?;
let payment_keys: Vec<_> = secrets
.iter()
.filter(|s| s.accessor.to_string().contains("System"))
.collect();
if payment_keys.is_empty() {
eprintln!("No payment keys. Create one: outlayer keys create");
return Ok(());
}
println!(
"{:<8} {:>12} {:>12} {:>12}",
"NONCE", "AVAILABLE", "SPENT", "INITIAL"
);
for pk in &payment_keys {
let nonce: u32 = pk.profile.parse().unwrap_or(0);
match api
.get_payment_key_balance(&creds.account_id, nonce)
.await
{
Ok(balance) => {
println!(
"{:<8} {:>12} {:>12} {:>12}",
nonce,
format_usd(&balance.available),
format_usd(&balance.spent),
format_usd(&balance.initial_balance),
);
}
Err(_) => {
println!(
"{:<8} {:>12} {:>12} {:>12}",
nonce, "---", "---", "---"
);
}
}
}
Ok(())
}
pub async fn balance(network: &NetworkConfig, nonce: u32) -> Result<()> {
let creds = config::load_credentials(network)?;
let api = ApiClient::new(network);
let balance = api
.get_payment_key_balance(&creds.account_id, nonce)
.await?;
println!("Balance: {}", format_usd(&balance.available));
println!("Spent: {}", format_usd(&balance.spent));
println!("Reserved: {}", format_usd(&balance.reserved));
println!("Initial: {}", format_usd(&balance.initial_balance));
if let Some(last_used) = &balance.last_used_at {
println!("Last used: {last_used}");
}
Ok(())
}
async fn stablecoin_contract(near: &NearClient) -> Result<String> {
let token: Option<String> = near
.view_call("get_payment_token_contract", json!({}))
.await
.context("Failed to ask the contract which token it settles in")?;
token.ok_or_else(|| {
anyhow::anyhow!("This contract has no payment token set, so a key cannot hold a balance")
})
}
pub async fn topup_usd(network: &NetworkConfig, nonce: u32, amount_usd: f64) -> Result<()> {
let creds = config::load_credentials(network)?;
if !(amount_usd > 0.0) {
anyhow::bail!("Top-up amount must be greater than zero");
}
let minimal = (amount_usd * 1_000_000.0).round() as u128;
let near = NearClient::new(network);
let token = stablecoin_contract(&near).await?;
let caller = ContractCaller::from_credentials(&creds, network)?;
eprintln!("Topping up key nonce {nonce} with ${amount_usd} ({token})...");
caller
.call_contract_at(
&token,
"ft_transfer_call",
json!({
"receiver_id": network.contract_id,
"amount": minimal.to_string(),
"msg": json!({ "action": "top_up_payment_key", "nonce": nonce }).to_string(),
}),
100_000_000_000_000u64, 1, )
.await
.context("Top-up failed")?;
eprintln!("Top-up sent. The balance appears once the worker has re-encrypted the key.");
eprintln!("Check balance: outlayer keys balance {nonce}");
Ok(())
}
pub async fn topup(network: &NetworkConfig, nonce: u32, amount_near: f64) -> Result<()> {
let creds = config::load_credentials(network)?;
if network.network_id != "mainnet" {
anyhow::bail!(
"Top-up with NEAR is only available on mainnet — it swaps through Intents, \
which has no testnet deployment. Use --usd to send the stablecoin directly."
);
}
let deposit = (amount_near * 1e24) as u128;
let min_deposit = 35_000_000_000_000_000_000_000u128; if deposit < min_deposit {
anyhow::bail!("Minimum top-up is 0.035 NEAR (0.01 deposit + 0.025 execution fees).");
}
let caller = ContractCaller::from_credentials(&creds, network)?;
let gas = 200_000_000_000_000u64;
eprintln!("Topping up key nonce {nonce} with {amount_near} NEAR...");
caller
.call_contract(
"top_up_payment_key_with_near",
json!({
"nonce": nonce,
"swap_contract_id": "intents.near"
}),
gas,
deposit,
)
.await
.context("Top-up failed")?;
eprintln!("Top-up successful. NEAR will be swapped to USDC via Intents.");
eprintln!("Check balance: outlayer keys balance {nonce}");
Ok(())
}
pub async fn delete(network: &NetworkConfig, nonce: u32) -> Result<()> {
let creds = config::load_credentials(network)?;
let caller = ContractCaller::from_credentials(&creds, network)?;
let gas = 100_000_000_000_000u64;
eprintln!("Deleting payment key nonce {nonce}...");
caller
.call_contract(
"delete_payment_key",
json!({ "nonce": nonce }),
gas,
1, )
.await
.context("Failed to delete payment key")?;
eprintln!("Payment key deleted. Storage deposit refunded.");
Ok(())
}
fn format_usd(minimal_units: &str) -> String {
let units: u64 = minimal_units.parse().unwrap_or(0);
let dollars = units as f64 / 1_000_000.0;
format!("${:.2}", dollars)
}