Skip to content

Commit 2f1123e

Browse files
Add wallet DB generation access and example polling
1 parent fe0ff18 commit 2f1123e

12 files changed

Lines changed: 94 additions & 12 deletions

File tree

bark-cpp/Cargo.lock

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bark-cpp/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ strip = true # Strip symbols from binary
5252
[dev-dependencies]
5353
tempfile = "3.23.0"
5454
serde_json = "1.0"
55+
56+
[patch."https://gitlab.com/ark-bitcoin/bark.git"]
57+
bark-wallet = { path = "../../bark/bark" }
58+
bark-bitcoin-ext = { path = "../../bark/bitcoin-ext" }

bark-cpp/src/cxx.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ pub(crate) mod ffi {
344344
fn init_logger();
345345
fn create_mnemonic() -> Result<String>;
346346
fn is_wallet_loaded() -> bool;
347+
fn get_wallet_db_generation() -> u64;
347348
fn close_wallet() -> Result<()>;
348349
fn get_ark_info() -> Result<CxxArkInfo>;
349350
fn offchain_balance() -> Result<OffchainBalance>;
@@ -496,6 +497,10 @@ pub(crate) fn is_wallet_loaded() -> bool {
496497
crate::TOKIO_RUNTIME.block_on(crate::is_wallet_loaded())
497498
}
498499

500+
pub(crate) fn get_wallet_db_generation() -> u64 {
501+
crate::get_wallet_db_generation()
502+
}
503+
499504
pub(crate) fn close_wallet() -> anyhow::Result<()> {
500505
ffi_boundary("close_wallet", || {
501506
crate::TOKIO_RUNTIME.block_on(crate::close_wallet())

bark-cpp/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ pub async fn is_wallet_loaded() -> bool {
302302
manager.is_loaded()
303303
}
304304

305+
pub fn get_wallet_db_generation() -> u64 {
306+
bark::persist::sqlite::wallet_db_generation()
307+
}
308+
305309
pub async fn balance() -> anyhow::Result<bark::Balance> {
306310
let mut manager = GLOBAL_WALLET_MANAGER.lock().await;
307311
manager

react-native-nitro-ark/cpp/NitroArk.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ class NitroArk : public HybridNitroArkSpec {
400400
return Promise<bool>::async([]() { return bark_cxx::is_wallet_loaded(); });
401401
}
402402

403+
std::shared_ptr<Promise<double>> getWalletDbGeneration() override {
404+
return Promise<double>::async([]() { return static_cast<double>(bark_cxx::get_wallet_db_generation()); });
405+
}
406+
403407
std::shared_ptr<Promise<void>> syncPendingBoards() override {
404408
return Promise<void>::async([]() {
405409
try {

react-native-nitro-ark/cpp/generated/ark_cxx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,8 @@ ::rust::String create_mnemonic();
14961496

14971497
bool is_wallet_loaded() noexcept;
14981498

1499+
::std::uint64_t get_wallet_db_generation() noexcept;
1500+
14991501
void close_wallet();
15001502

15011503
::bark_cxx::CxxArkInfo get_ark_info();

react-native-nitro-ark/example/src/App.tsx

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useCallback } from 'react';
1+
import { useState, useEffect, useCallback, useRef } from 'react';
22
import {
33
View,
44
Text,
@@ -10,10 +10,11 @@ import {
1010
import RNFSTurbo from 'react-native-fs-turbo';
1111
import AsyncStorage from '@react-native-async-storage/async-storage';
1212

13-
import type {
14-
BarkArkInfo,
15-
OnchainBalanceResult,
16-
OffchainBalanceResult,
13+
import {
14+
getWalletDbGeneration,
15+
type BarkArkInfo,
16+
type OnchainBalanceResult,
17+
type OffchainBalanceResult,
1718
} from 'react-native-nitro-ark';
1819

1920
import { WalletTab } from './tabs/WalletTab';
@@ -40,6 +41,10 @@ export default function App() {
4041

4142
// Wallet loaded state
4243
const [isWalletLoaded, setIsWalletLoaded] = useState(false);
44+
const [walletDbGeneration, setWalletDbGeneration] = useState<
45+
number | undefined
46+
>();
47+
const lastLoggedWalletDbGenerationRef = useRef<number | undefined>(undefined);
4348

4449
// UI state
4550
const [results, setResults] = useState<{ [key: string]: string }>({});
@@ -80,6 +85,46 @@ export default function App() {
8085
loadSavedMnemonic();
8186
}, []);
8287

88+
// Poll the native wallet DB generation counter so the example app logs
89+
// whenever Bark commits wallet SQLite changes.
90+
useEffect(() => {
91+
let isActive = true;
92+
93+
const pollWalletDbGeneration = async () => {
94+
try {
95+
const generation = await getWalletDbGeneration();
96+
if (!isActive) {
97+
return;
98+
}
99+
100+
setWalletDbGeneration(generation);
101+
102+
const previous = lastLoggedWalletDbGenerationRef.current;
103+
if (previous === undefined) {
104+
console.log('[wallet-db] initial generation:', generation);
105+
} else if (generation !== previous) {
106+
console.log('[wallet-db] generation changed:', {
107+
previous,
108+
generation,
109+
});
110+
}
111+
lastLoggedWalletDbGenerationRef.current = generation;
112+
} catch (err) {
113+
console.error('Error polling wallet DB generation:', err);
114+
}
115+
};
116+
117+
void pollWalletDbGeneration();
118+
const interval = setInterval(() => {
119+
void pollWalletDbGeneration();
120+
}, 1000);
121+
122+
return () => {
123+
isActive = false;
124+
clearInterval(interval);
125+
};
126+
}, []);
127+
83128
// Generic operation runner
84129
const runOperation = useCallback(
85130
async (
@@ -171,6 +216,19 @@ export default function App() {
171216
{isWalletLoaded ? 'Wallet Loaded' : 'Not Loaded'}
172217
</Text>
173218
</View>
219+
<View style={styles.headerStatusRow}>
220+
<View
221+
style={[
222+
styles.statusDot,
223+
walletDbGeneration !== undefined
224+
? styles.statusActive
225+
: styles.statusInactive,
226+
]}
227+
/>
228+
<Text style={styles.statusLabel}>
229+
DB Gen {walletDbGeneration ?? '-'}
230+
</Text>
231+
</View>
174232
</View>
175233
</View>
176234

react-native-nitro-ark/nitrogen/generated/shared/c++/HybridNitroArkSpec.cpp

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-native-nitro-ark/nitrogen/generated/shared/c++/HybridNitroArkSpec.hpp

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-native-nitro-ark/src/NitroArk.nitro.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ export interface NitroArk extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
293293
createWallet(datadir: string, opts: BarkCreateOpts): Promise<void>;
294294
loadWallet(datadir: string, config: BarkCreateOpts): Promise<void>;
295295
isWalletLoaded(): Promise<boolean>;
296+
getWalletDbGeneration(): Promise<number>;
296297
closeWallet(): Promise<void>;
297298
refreshServer(): Promise<void>;
298299
syncPendingBoards(): Promise<void>;

0 commit comments

Comments
 (0)