Skip to content

Commit

Permalink
Merge pull request #11 from DIG-Network/height-stuff
Browse files Browse the repository at this point in the history
Stuff
  • Loading branch information
Yakuhito authored Sep 23, 2024
2 parents 6285a57 + 19a79bd commit ff61f73
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,15 @@ export declare class Peer {
* @returns {Promise<SyncStoreResponse>} The sync store response.
*/
syncStoreFromLauncherId(launcherId: Buffer, lastHeight: number | undefined | null, lastHeaderHash: Buffer, withHistory: boolean): Promise<SyncStoreResponse>
/**
* Fetch a store's creation height.
*
* @param {Buffer} launcherId - The store's launcher/singleton ID.
* @param {Option<u32>} lastHeight - Min. height to search records from. If null, sync will be done from the genesis block.
* @param {Buffer} lastHeaderHash - Header hash corresponding to `lastHeight`. If null, this should be the genesis challenge of the current chain.
* @returns {Promise<BigInt>} The store's creation height.
*/
getStoreCreationHeight(launcherId: Buffer, lastHeight: number | undefined | null, lastHeaderHash: Buffer): Promise<bigint>
/**
* Broadcasts a spend bundle to the mempool.
*
Expand Down
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,31 @@ impl Peer {
res.to_js()
}

#[napi]
/// Fetch a store's creation height.
///
/// @param {Buffer} launcherId - The store's launcher/singleton ID.
/// @param {Option<u32>} lastHeight - Min. height to search records from. If null, sync will be done from the genesis block.
/// @param {Buffer} lastHeaderHash - Header hash corresponding to `lastHeight`. If null, this should be the genesis challenge of the current chain.
/// @returns {Promise<BigInt>} The store's creation height.
pub async fn get_store_creation_height(
&self,
launcher_id: Buffer,
last_height: Option<u32>,
last_header_hash: Buffer,
) -> napi::Result<BigInt> {
let res = wallet::get_store_creation_height(
&self.inner.clone(),
RustBytes32::from_js(launcher_id)?,
last_height,
RustBytes32::from_js(last_header_hash)?,
)
.await
.map_err(js::err)?;

(res as u64).to_js()
}

#[napi]
/// Broadcasts a spend bundle to the mempool.
///
Expand Down
22 changes: 22 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,28 @@ pub async fn sync_store_using_launcher_id(
})
}

pub async fn get_store_creation_height(
peer: &Peer,
launcher_id: Bytes32,
last_height: Option<u32>,
last_header_hash: Bytes32,
) -> Result<u32, WalletError> {
let response = peer
.request_coin_state(vec![launcher_id], last_height, last_header_hash, false)
.await
.map_err(WalletError::Client)?
.map_err(|_| WalletError::RejectCoinState)?;
let last_coin_record = response
.coin_states
.into_iter()
.next()
.ok_or(WalletError::UnknownCoin)?;

last_coin_record
.created_height
.ok_or(WalletError::UnknownCoin)
}

pub enum DataStoreInnerSpend {
Owner(PublicKey),
Admin(PublicKey),
Expand Down

0 comments on commit ff61f73

Please sign in to comment.