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, projects: &[String]) -> Result<()> {
for project in projects {
check_project_scope(project)?;
}
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": projects,
"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: "{}".to_string(),
},
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})");
if projects.is_empty() {
eprintln!("It pays for any project.");
} else {
eprintln!("It pays only for: {}", projects.join(", "));
}
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 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; the remaining balance is forfeited (`outlayer keys close` would have refunded it).");
Ok(())
}
#[derive(Debug, serde::Deserialize)]
struct RefundLedgerView {
user_funded: String,
refunded: String,
refundable_cap: String,
closing_since: Option<u64>,
claimable_at: Option<u64>,
}
async fn refund_ledger(near: &NearClient, owner: &str, nonce: u32) -> Result<Option<RefundLedgerView>> {
near.view_call(
"get_payment_key_refund_ledger",
json!({ "owner": owner, "nonce": nonce }),
)
.await
.context("Failed to read the key's refund ledger from the contract")
}
async fn refund_ledger_once_final(
near: &NearClient,
owner: &str,
nonce: u32,
method: &str,
tx_hash: Option<&str>,
) -> Result<Option<RefundLedgerView>> {
let tx_hash = tx_hash
.with_context(|| format!("{method} went through, and no transaction hash came back to wait on"))?;
near.wait_until_final(tx_hash, owner)
.await
.with_context(|| format!("{method} executed in {tx_hash}"))?;
refund_ledger(near, owner, nonce)
.await
.with_context(|| format!("{method} executed in {tx_hash}, and is final"))
}
fn now_ns() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos() as u64)
.unwrap_or(0)
}
fn until(ns: u64) -> String {
let now = now_ns();
if ns <= now {
return "now".to_string();
}
let secs = (ns - now) / 1_000_000_000;
format!("in {}h {:02}m", secs / 3600, (secs % 3600) / 60)
}
pub async fn close(network: &NetworkConfig, nonce: u32) -> Result<()> {
let creds = config::load_credentials(network)?;
let near = NearClient::new(network);
let caller = ContractCaller::from_credentials(&creds, network)?;
let ledger = refund_ledger(&near, &creds.account_id, nonce).await?;
if let Some(l) = &ledger {
if let (Some(_), Some(at)) = (l.closing_since, l.claimable_at) {
anyhow::bail!(
"Payment key {nonce} is already closing. Claim its refund with \
`outlayer keys claim {nonce}` ({}).",
until(at)
);
}
}
let cap = ledger.as_ref().map(|l| l.refundable_cap.as_str()).unwrap_or("0");
eprintln!("Closing payment key {nonce}.");
eprintln!(" From now: no new calls, no top-ups. Calls already running settle normally.");
if cap == "0" {
eprintln!(" Nothing on this key is refundable on chain: it was funded before refunds existed, or");
eprintln!(" by a grant. Closing it deletes it; whatever it can still spend is spent through the key.");
} else {
eprintln!(" Refundable on chain: {} — what this account deposited on the key since refunds exist, less refunds.", format_usd(cap));
}
eprintln!(" Grants, sponsor credit and trial allowance are never refunded.");
eprintln!(" Do not close while a NEAR top-up of this key is in progress: the swapped stablecoin");
eprintln!(" would go back to the swap account, not to you.");
eprintln!(" In 24 hours: `outlayer keys claim {nonce}` pays it out and deletes the key (one claim a day).");
let closed = caller
.call_contract(
"close_payment_key",
json!({ "nonce": nonce }),
100_000_000_000_000u64, 1, )
.await
.context("Failed to close the payment key")?;
let after = refund_ledger_once_final(&near, &creds.account_id, nonce, "close_payment_key", closed.tx_hash.as_deref())
.await
.with_context(|| {
format!("Run `outlayer keys close {nonce}` again in a moment: it says whether the key is closing")
})?;
match after {
Some(l) if l.closing_since.is_some() => {
eprintln!(
"Payment key {nonce} is closing. Claim its refund {}: outlayer keys claim {nonce}",
l.claimable_at.map(until).unwrap_or_else(|| "in 24 hours".to_string())
);
}
_ => {
eprintln!(
"The transaction went through but the key is not marked closing: the coordinator \
did not confirm the close in time. The key is still live; run this again."
);
}
}
Ok(())
}
pub async fn claim(network: &NetworkConfig, nonce: u32) -> Result<()> {
let creds = config::load_credentials(network)?;
let near = NearClient::new(network);
let caller = ContractCaller::from_credentials(&creds, network)?;
let before = refund_ledger(&near, &creds.account_id, nonce).await?;
let Some(ledger) = before.as_ref().filter(|l| l.closing_since.is_some()) else {
anyhow::bail!(
"Payment key {nonce} is not closing. Close it first: outlayer keys close {nonce}"
);
};
if let Some(at) = ledger.claimable_at {
if at > now_ns() {
anyhow::bail!(
"Payment key {nonce} closed too recently: its refund can be claimed {}.",
until(at)
);
}
}
eprintln!("Claiming the refund of payment key {nonce}.");
eprintln!(
" Refundable on chain: at most {} (deposited {}, refunded {}).",
format_usd(&ledger.refundable_cap),
format_usd(&ledger.user_funded),
format_usd(&ledger.refunded)
);
eprintln!(" The key is deleted and its storage deposit returned; its nonce is not reused.");
let result = caller
.call_contract(
"claim_payment_key_refund",
json!({ "nonce": nonce }),
100_000_000_000_000u64, 1, )
.await
.context("Failed to claim the refund")?;
let paid = result
.value
.as_ref()
.and_then(|v| v.as_str())
.unwrap_or("0")
.to_string();
if paid != "0" {
eprintln!("Refunded {} to {}. Payment key {nonce} is deleted.", format_usd(&paid), creds.account_id);
return Ok(());
}
let after = refund_ledger_once_final(&near, &creds.account_id, nonce, "claim_payment_key_refund", result.tx_hash.as_deref())
.await
.with_context(|| {
format!("Nothing was paid. `outlayer keys list` no longer lists key {nonce} once it is deleted")
})?;
match after {
Some(l) if l.closing_since.is_some() => {
eprintln!(
"Nothing was paid and the key stays closing: the coordinator still has money of this \
key in flight, or did not answer in time. Claim again tomorrow: outlayer keys claim {nonce}"
);
}
_ => {
eprintln!(
"Payment key {nonce} is deleted; nothing was refundable on chain (deposits made before \
refunds existed, grants and trial credit are spent through the key, not returned)."
);
}
}
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)
}
fn check_project_scope(project: &str) -> Result<()> {
let (owner, name) = project
.split_once('/')
.ok_or_else(|| anyhow::anyhow!("--project {project:?}: expected owner/name, for example alice.near/my-app"))?;
if owner.is_empty() || name.is_empty() || name.contains('/') || owner.contains(char::is_whitespace) || name.contains(char::is_whitespace) {
anyhow::bail!("--project {project:?}: expected owner/name, or owner/* for every project of one owner");
}
if name.contains('*') && name != "*" {
anyhow::bail!("--project {project:?}: `*` stands for a whole project name only (owner/*), not a prefix");
}
Ok(())
}
#[cfg(test)]
mod project_scope_tests {
use super::check_project_scope;
#[test]
fn a_scope_is_owner_slash_name_or_owner_slash_star() {
assert!(check_project_scope("zavodil2.testnet/compliance-gateway").is_ok());
assert!(check_project_scope("alice.near/*").is_ok());
for bad in ["compliance-gateway", "/app", "alice.near/", "a/b/c", "alice.near/app*", "alice near/app"] {
assert!(check_project_scope(bad).is_err(), "{bad} must be refused");
}
}
}
#[cfg(test)]
mod read_back_tests {
use super::*;
use crate::near::fake_rpc::*;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
const HASH: &str = "11111111111111111111111111111111";
fn chain(before: serde_json::Value, after: serde_json::Value) -> NearClient {
let final_seen = Arc::new(AtomicBool::new(false));
let tx_asked = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let url = serve(move |request| match request["method"].as_str() {
Some("tx") => {
if tx_asked.fetch_add(1, Ordering::SeqCst) == 0 {
tx_unknown()
} else {
final_seen.store(true, Ordering::SeqCst);
tx_final()
}
}
Some("query") => {
assert_eq!(request["params"]["finality"], "final", "{request}");
assert_eq!(request["params"]["method_name"], "get_payment_key_refund_ledger", "{request}");
view_result(if final_seen.load(Ordering::SeqCst) { &after } else { &before })
}
other => panic!("unexpected method {other:?}"),
});
NearClient::new(&network(&url))
}
fn ledger(closing: bool) -> serde_json::Value {
json!({
"user_funded": "5000000",
"refunded": "0",
"refundable_cap": "5000000",
"closing_since": if closing { json!(1_700_000_000_000_000_000u64) } else { json!(null) },
"claimable_at": if closing { json!(1_700_086_400_000_000_000u64) } else { json!(null) },
})
}
#[tokio::test]
async fn a_close_is_read_back_once_its_block_is_final() {
let near = chain(ledger(false), ledger(true));
let read = refund_ledger_once_final(&near, "alice.testnet", 7, "close_payment_key", Some(HASH))
.await
.expect("read");
assert!(read.is_some_and(|l| l.closing_since.is_some()), "the key reads as closing");
}
#[tokio::test]
async fn a_claim_is_read_back_once_its_block_is_final() {
let near = chain(ledger(true), json!(null));
let read = refund_ledger_once_final(&near, "alice.testnet", 7, "claim_payment_key_refund", Some(HASH))
.await
.expect("read");
assert!(read.is_none(), "the key reads as deleted");
}
#[tokio::test]
async fn no_hash_is_an_error_that_says_the_call_went_through() {
let near = chain(ledger(false), ledger(true));
let e = refund_ledger_once_final(&near, "alice.testnet", 7, "close_payment_key", None)
.await
.expect_err("nothing to wait on");
assert!(format!("{e:#}").contains("close_payment_key went through"), "{e:#}");
}
}