Skip to content

Commit

Permalink
Support solana addresses for owner and payer (#461)
Browse files Browse the repository at this point in the history
* Support solana addresses for owner and payer

For legacy usage support this supports both helium public keys as well
as solana addresses.

* Remove fees from output since they’re not used

* Remove txn_fee traits
  • Loading branch information
madninja authored Oct 3, 2023
1 parent 61b488f commit f3b52bb
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 134 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ clap = { version = "4", default-features = false, features = [
"std",
"error-context",
] }
bs58 = "0"
semver = "0"
config = { version = "0", default-features = false, features = ["toml"] }
serde = { workspace = true }
Expand Down
17 changes: 2 additions & 15 deletions src/api/server.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use super::{
AddGatewayReq, AddGatewayRes, PubkeyReq, PubkeyRes, RegionReq, RegionRes, RouterReq, RouterRes,
};
use crate::{
packet_router, region_watcher, settings::StakingMode, Error, Keypair, PublicKey, Result,
Settings, TxnFee, TxnFeeConfig,
};
use crate::{packet_router, region_watcher, Error, Keypair, PublicKey, Result, Settings};
use futures::TryFutureExt;
use helium_crypto::Sign;
use helium_proto::services::local::{Api, Server};
Expand Down Expand Up @@ -90,23 +87,13 @@ impl Api for LocalServer {
let _ = PublicKey::from_bytes(&request.payer)
.map_err(|_err| Status::invalid_argument("Invalid payer address"))?;

let mode = StakingMode::from(request.staking_mode());
let fee_config = TxnFeeConfig::default();
let mut txn = BlockchainTxnAddGatewayV1 {
gateway: self.keypair.public_key().to_vec(),
owner: request.owner.clone(),
payer: request.payer,
fee: 0,
staking_fee: fee_config.get_staking_fee(&mode),
owner_signature: vec![],
gateway_signature: vec![],
payer_signature: vec![],
..Default::default()
};

txn.fee = txn
.txn_fee(&fee_config)
.map_err(|_err| Status::internal("Failed to get txn fees"))?;

let signature = self
.keypair
.sign(&txn.encode_to_vec())
Expand Down
File renamed without changes.
34 changes: 26 additions & 8 deletions src/cmd/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use serde_json::json;
/// Construct an add gateway transaction for this gateway.
#[derive(Debug, clap::Args)]
pub struct Cmd {
/// The target owner account of this gateway
#[arg(long)]
/// The solana address of the target owner for this gateway
#[arg(long, value_parser = parse_pubkey)]
owner: PublicKey,

/// The account that will pay account for this addition
#[arg(long)]
/// The solana address of the payer account that will pay account for this
/// addition
#[arg(long, value_parser = parse_pubkey)]
payer: PublicKey,

/// The staking mode for adding the gateway
Expand All @@ -33,13 +34,30 @@ fn print_txn(mode: &StakingMode, txn: BlockchainTxnAddGatewayV1) -> Result {
let table = json!({
"mode": mode.to_string(),
"address": PublicKey::from_bytes(&txn.gateway)?.to_string(),
"payer": PublicKey::from_bytes(&txn.payer)?.to_string(),
"owner": PublicKey::from_bytes(&txn.owner)?.to_string(),
"fee": txn.fee,
"staking fee": txn.staking_fee,
"payer": PublicKey::from_bytes(&txn.payer).and_then(solana_pubkey)?,
"owner": PublicKey::from_bytes(&txn.owner).and_then(solana_pubkey)?,
"txn": BlockchainTxn {
txn: Some(Txn::AddGateway(txn))
}.encode_to_vec().to_b64()
});
print_json(&table)
}

fn parse_pubkey(str: &str) -> Result<PublicKey> {
use helium_crypto::{ed25519, ReadFrom};
use std::{io::Cursor, str::FromStr};

match PublicKey::from_str(str) {
Ok(pk) => Ok(pk),
Err(_) => {
let bytes = bs58::decode(str).into_vec()?;
let public_key = ed25519::PublicKey::read_from(&mut Cursor::new(bytes))?;
Ok(public_key.into())
}
}
}

fn solana_pubkey(key: PublicKey) -> std::result::Result<String, helium_crypto::Error> {
let bytes = &key.to_vec()[1..];
Ok(bs58::encode(bytes).into_string())
}
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum DecodeError {
KeypairUri(String),
#[error("json decode: {0}")]
Json(#[from] serde_json::Error),
#[error("base58 decode: {0}")]
Base58(#[from] bs58::decode::Error),
#[error("base64 decode: {0}")]
Base64(#[from] base64::DecodeError),
#[error("network address decode: {0}")]
Expand Down Expand Up @@ -115,6 +117,7 @@ from_err!(EncodeError, prost::EncodeError);
// Decode Errors
from_err!(DecodeError, http::uri::InvalidUri);
from_err!(DecodeError, base64::DecodeError);
from_err!(DecodeError, bs58::decode::Error);
from_err!(DecodeError, serde_json::Error);
from_err!(DecodeError, net::AddrParseError);
from_err!(DecodeError, prost::DecodeError);
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub mod settings;
pub mod sync;

mod api;
mod traits;
mod base64;

pub(crate) use base64::*;
pub use beacon::{Region, RegionParams};
pub use error::{Error, Result};
pub use keyed_uri::KeyedUri;
pub use keypair::{Keypair, PublicKey};
pub use packet::{PacketDown, PacketUp};
pub use settings::Settings;
pub(crate) use traits::*;

use futures::{Future as StdFuture, Stream as StdStream};
use std::pin::Pin;
Expand Down
5 changes: 0 additions & 5 deletions src/traits/mod.rs

This file was deleted.

104 changes: 0 additions & 104 deletions src/traits/txn_fee.rs

This file was deleted.

0 comments on commit f3b52bb

Please sign in to comment.