mixnet_contract/signing/
storage.rs

1// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::constants::SIGNING_NONCES_NAMESPACE;
5use cosmwasm_std::{Addr, StdResult, Storage};
6use cw_storage_plus::Map;
7use nym_contracts_common::signing::Nonce;
8
9pub const NONCES: Map<'_, Addr, Nonce> = Map::new(SIGNING_NONCES_NAMESPACE);
10
11pub fn get_signing_nonce(storage: &dyn Storage, address: Addr) -> StdResult<Nonce> {
12    let nonce = NONCES.may_load(storage, address)?.unwrap_or(0);
13    Ok(nonce)
14}
15
16pub fn update_signing_nonce(
17    storage: &mut dyn Storage,
18    address: Addr,
19    value: Nonce,
20) -> StdResult<()> {
21    NONCES.save(storage, address, &value)
22}
23
24pub fn increment_signing_nonce(storage: &mut dyn Storage, address: Addr) -> StdResult<()> {
25    // get the current nonce
26    let nonce = get_signing_nonce(storage, address.clone())?;
27
28    // increment it for the next use
29    update_signing_nonce(storage, address, nonce + 1)
30}