Skip to content

Commit

Permalink
Merge pull request #282 from gregdhill/fix/register-public-key
Browse files Browse the repository at this point in the history
fix: only register public key after funding
  • Loading branch information
gregdhill authored Apr 23, 2022
2 parents 7ed42f4 + c4ba5e6 commit dd70c98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
8 changes: 7 additions & 1 deletion vault/src/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use hex::FromHex;
use jsonrpc_core::Value;
use jsonrpc_core_client::{transports::http as jsonrpc_http, TypedClient};
use parity_scale_codec::{Decode, Encode};
use runtime::{AccountId, CurrencyId, CurrencyIdExt, InterBtcParachain, VaultId, VaultRegistryPallet, TX_FEES};
use runtime::{
AccountId, BtcPublicKey, CurrencyId, CurrencyIdExt, InterBtcParachain, VaultId, VaultRegistryPallet, TX_FEES,
};
use serde::{Deserialize, Deserializer};

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -47,6 +49,7 @@ pub async fn fund_and_register(
parachain_rpc: &InterBtcParachain,
faucet_url: &str,
vault_id: &VaultId,
maybe_public_key: Option<BtcPublicKey>,
) -> Result<(), Error> {
tracing::info!("Connecting to the faucet");
let connection = jsonrpc_http::connect::<TypedClient>(faucet_url).await?;
Expand All @@ -65,6 +68,9 @@ pub async fn fund_and_register(
.ok_or(Error::ArithmeticUnderflow)?;

tracing::info!("Registering the vault");
if let Some(public_key) = maybe_public_key {
parachain_rpc.register_public_key(public_key).await?;
}
parachain_rpc.register_vault(vault_id, registration_collateral).await?;

// Receive vault allowance from faucet
Expand Down
21 changes: 13 additions & 8 deletions vault/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use futures::{
use git_version::git_version;
use runtime::{
cli::{parse_duration_minutes, parse_duration_ms},
parse_collateral_currency, BtcRelayPallet, CollateralBalancesPallet, CurrencyId, Error as RuntimeError,
InterBtcParachain, PrettyPrint, RegisterVaultEvent, StoreMainChainHeaderEvent, UpdateActiveBlockEvent, UtilFuncs,
VaultCurrencyPair, VaultId, VaultRegistryPallet,
parse_collateral_currency, BtcPublicKey, BtcRelayPallet, CollateralBalancesPallet, CurrencyId,
Error as RuntimeError, InterBtcParachain, PrettyPrint, RegisterVaultEvent, StoreMainChainHeaderEvent,
UpdateActiveBlockEvent, UtilFuncs, VaultCurrencyPair, VaultId, VaultRegistryPallet,
};
use service::{wait_or_shutdown, Error as ServiceError, MonitoringConfig, Service, ShutdownSender};
use std::{collections::HashMap, pin::Pin, sync::Arc, time::Duration};
Expand Down Expand Up @@ -690,8 +690,6 @@ impl VaultService {
}

async fn maybe_register_vault(&self) -> Result<(), Error> {
self.maybe_register_public_key().await?;

let vault_id = self.get_vault_id();

if is_vault_registered(&self.btc_parachain, &vault_id).await? {
Expand All @@ -702,9 +700,8 @@ impl VaultService {
} else {
tracing::info!("[{}] Not registered", vault_id.pretty_print());

self.vault_id_manager.add_vault_id(vault_id.clone()).await?;

if let Some(collateral) = self.config.auto_register_with_collateral {
self.maybe_register_public_key().await?;
tracing::info!("[{}] Automatically registering...", vault_id.pretty_print());
let free_balance = self
.btc_parachain
Expand All @@ -727,8 +724,16 @@ impl VaultService {
.await?;
} else if let Some(faucet_url) = &self.config.auto_register_with_faucet_url {
tracing::info!("[{}] Automatically registering...", vault_id.pretty_print());
faucet::fund_and_register(&self.btc_parachain, faucet_url, &vault_id).await?;
let maybe_public_key = if let None = self.btc_parachain.get_public_key().await? {
tracing::info!("Created new bitcoin public key");
Some(self.btc_rpc_master_wallet.get_new_public_key::<BtcPublicKey>().await?)
} else {
None
};
faucet::fund_and_register(&self.btc_parachain, faucet_url, &vault_id, maybe_public_key).await?;
}

self.vault_id_manager.add_vault_id(vault_id.clone()).await?;
}
Ok(())
}
Expand Down

0 comments on commit dd70c98

Please sign in to comment.