Skip to content

Commit

Permalink
burning delegate dc take router key as String (#457)
Browse files Browse the repository at this point in the history
* burning delegate dc take router key as String

ecc_compact keys cannot be turned into solana pubkeys, but they can still be used to derive escrow accounts.

* Use AsEntityKey trait for burn_delegated txn

* Take AsEntityKey as a reference for key argument consistency

* impl AsEntityKey for PublicKeyBinary

* add From for solana Keypair to helium-lib Keypair

* go through string from PublicKeyBinary to ensure correct encoding
  • Loading branch information
michaeldjeffrey authored Feb 14, 2025
1 parent c3f5bab commit f7e0651
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions helium-lib/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ impl SubDao {
}
}

pub fn delegated_dc_key(&self, router_key: &str) -> Pubkey {
let hash = Sha256::digest(router_key);
pub fn delegated_dc_key<E: AsEntityKey>(&self, router_key: &E) -> Pubkey {
let hash = Sha256::digest(router_key.as_entity_key());
let (key, _) = Pubkey::find_program_address(
&[b"delegated_data_credits", self.key().as_ref(), &hash],
&data_credits::id(),
Expand Down
20 changes: 10 additions & 10 deletions helium-lib/src/dc.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{
anchor_lang::AccountDeserialize,
anchor_lang::{InstructionData, ToAccountMetas},
anchor_lang::{AccountDeserialize, InstructionData, ToAccountMetas},
anchor_spl, circuit_breaker,
client::{GetAnchorAccount, SolanaRpcClient},
dao::{Dao, SubDao},
data_credits,
entity_key::AsEntityKey,
error::{DecodeError, Error},
keypair::{Keypair, Pubkey},
message, priority_fee,
Expand Down Expand Up @@ -127,7 +127,7 @@ pub async fn delegate_message<C: AsRef<SolanaRpcClient>>(
}
}

let delegated_dc_key = subdao.delegated_dc_key(payer_key);
let delegated_dc_key = subdao.delegated_dc_key(&payer_key);
let ix = Instruction {
program_id: data_credits::id(),
accounts: mk_accounts(delegated_dc_key, subdao, *owner).to_account_metas(None),
Expand Down Expand Up @@ -221,21 +221,21 @@ pub async fn burn<C: AsRef<SolanaRpcClient>>(
Ok((txn, block_height))
}

pub async fn burn_delegated_message<C: AsRef<SolanaRpcClient>>(
pub async fn burn_delegated_message<C: AsRef<SolanaRpcClient>, E: AsEntityKey>(
client: &C,
sub_dao: SubDao,
amount: u64,
router_key: Pubkey,
router_key: &E,
payer: &Pubkey,
opts: &TransactionOpts,
) -> Result<(message::VersionedMessage, u64), Error> {
fn mk_accounts(
fn mk_accounts<E: AsEntityKey>(
sub_dao: SubDao,
router_key: Pubkey,
router_key: &E,
dc_burn_authority: Pubkey,
registrar: Pubkey,
) -> BurnDelegatedDataCreditsV0 {
let delegated_data_credits = sub_dao.delegated_dc_key(&router_key.to_string());
let delegated_data_credits = sub_dao.delegated_dc_key(router_key);
let escrow_account = sub_dao.escrow_key(&delegated_data_credits);

BurnDelegatedDataCreditsV0 {
Expand Down Expand Up @@ -291,12 +291,12 @@ pub async fn burn_delegated_message<C: AsRef<SolanaRpcClient>>(
message::mk_message(client, ixs, &opts.lut_addresses, payer).await
}

pub async fn burn_delegated<C: AsRef<SolanaRpcClient>>(
pub async fn burn_delegated<C: AsRef<SolanaRpcClient>, E: AsEntityKey>(
client: &C,
sub_dao: SubDao,
keypair: &Keypair,
amount: u64,
router_key: Pubkey,
router_key: &E,
opts: &TransactionOpts,
) -> Result<(VersionedTransaction, u64), Error> {
let (msg, block_height) =
Expand Down
6 changes: 6 additions & 0 deletions helium-lib/src/entity_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ impl AsEntityKey for helium_crypto::PublicKey {
}
}

impl AsEntityKey for helium_crypto::PublicKeyBinary {
fn as_entity_key(&self) -> Vec<u8> {
self.to_string().as_entity_key()
}
}

pub use helium_anchor_gen::helium_entity_manager::KeySerialization;

pub fn from_str(str: &str, encoding: KeySerialization) -> Result<Vec<u8>, DecodeError> {
Expand Down
6 changes: 6 additions & 0 deletions helium-lib/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ impl TryFrom<&[u8; 64]> for Keypair {
}
}

impl From<solana_sdk::signer::keypair::Keypair> for Keypair {
fn from(value: solana_sdk::signer::keypair::Keypair) -> Self {
Self(value)
}
}

impl Keypair {
pub fn generate() -> Self {
Keypair(solana_sdk::signer::keypair::Keypair::new())
Expand Down

0 comments on commit f7e0651

Please sign in to comment.