Skip to content

Commit 1c3dd78

Browse files
committed
optional account number
1 parent 7b69020 commit 1c3dd78

11 files changed

Lines changed: 55 additions & 27 deletions

File tree

crates/breez-sdk/core/src/bindings.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ impl SdkBuilder {
3232
/// Arguments:
3333
/// - `key_set_type`: The key set type which determines the derivation path.
3434
/// - `use_address_index`: Controls the structure of the BIP derivation path.
35-
pub async fn with_key_set(&self, key_set_type: KeySetType, use_address_index: bool) {
35+
pub async fn with_key_set(
36+
&self,
37+
key_set_type: KeySetType,
38+
use_address_index: bool,
39+
account_number: Option<u32>,
40+
) {
3641
let mut builder = self.inner.lock().await;
3742
*builder = builder
3843
.clone()
39-
.with_key_set(key_set_type, use_address_index);
44+
.with_key_set(key_set_type, use_address_index, account_number);
4045
}
4146

4247
/// Sets the chain service to be used by the SDK.

crates/breez-sdk/core/src/sdk_builder.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct SdkBuilder {
5353
lnurl_server_client: Option<Arc<dyn LnurlServerClient>>,
5454
key_set_type: KeySetType,
5555
use_address_index: bool,
56+
account_number: Option<u32>,
5657
}
5758

5859
impl SdkBuilder {
@@ -72,6 +73,7 @@ impl SdkBuilder {
7273
lnurl_server_client: None,
7374
key_set_type: KeySetType::Default,
7475
use_address_index: false,
76+
account_number: None,
7577
}
7678
}
7779

@@ -80,9 +82,15 @@ impl SdkBuilder {
8082
/// - `key_set_type`: The key set type which determines the derivation path.
8183
/// - `use_address_index`: Controls the structure of the BIP derivation path.
8284
#[must_use]
83-
pub fn with_key_set(mut self, key_set_type: KeySetType, use_address_index: bool) -> Self {
85+
pub fn with_key_set(
86+
mut self,
87+
key_set_type: KeySetType,
88+
use_address_index: bool,
89+
account_number: Option<u32>,
90+
) -> Self {
8491
self.key_set_type = key_set_type;
8592
self.use_address_index = use_address_index;
93+
self.account_number = account_number;
8694
self
8795
}
8896

@@ -163,6 +171,7 @@ impl SdkBuilder {
163171
self.config.network.into(),
164172
self.key_set_type.into(),
165173
self.use_address_index,
174+
self.account_number,
166175
)
167176
.map_err(|e| SdkError::Generic(e.to_string()))?;
168177
let chain_service = if let Some(service) = self.chain_service {

crates/breez-sdk/wasm/src/sdk_builder.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ impl SdkBuilder {
3939
}
4040

4141
#[wasm_bindgen(js_name = "withKeySet")]
42-
pub fn with_key_set(mut self, key_set_type: KeySetType, use_address_index: bool) -> Self {
43-
self.builder = self
44-
.builder
45-
.with_key_set(key_set_type.into(), use_address_index);
42+
pub fn with_key_set(
43+
mut self,
44+
key_set_type: KeySetType,
45+
use_address_index: bool,
46+
account_number: Option<u32>,
47+
) -> Self {
48+
self.builder =
49+
self.builder
50+
.with_key_set(key_set_type.into(), use_address_index, account_number);
4651
self
4752
}
4853

crates/spark/src/signer/default_signer.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,11 @@ pub struct KeySet {
145145
}
146146

147147
impl KeySet {
148-
fn default_keys(seed: &[u8], network: Network) -> Result<Self, DefaultSignerError> {
149-
let account_number = account_number(network);
148+
fn default_keys(
149+
seed: &[u8],
150+
network: Network,
151+
account_number: u32,
152+
) -> Result<Self, DefaultSignerError> {
150153
let derivation_path = format!("m/8797555'/{account_number}'").parse()?;
151154
let derived_key_set = DerivedKeySet::new(seed, network, derivation_path)?;
152155
derived_key_set.to_key_set(ChildNumber::from_hardened_idx(0).ok())
@@ -156,8 +159,8 @@ impl KeySet {
156159
seed: &[u8],
157160
network: Network,
158161
use_address_index: bool,
162+
account_number: u32,
159163
) -> Result<Self, DefaultSignerError> {
160-
let account_number = account_number(network);
161164
let derivation_path = if use_address_index {
162165
format!("m/86'/0'/0'/0/{account_number}")
163166
} else {
@@ -190,8 +193,8 @@ impl KeySet {
190193
seed: &[u8],
191194
network: Network,
192195
use_address_index: bool,
196+
account_number: u32,
193197
) -> Result<Self, DefaultSignerError> {
194-
let account_number = account_number(network);
195198
let derivation_path = if use_address_index {
196199
format!("m/84'/0'/0'/0/{account_number}")
197200
} else {
@@ -206,8 +209,8 @@ impl KeySet {
206209
seed: &[u8],
207210
network: Network,
208211
use_address_index: bool,
212+
account_number: u32,
209213
) -> Result<Self, DefaultSignerError> {
210-
let account_number = account_number(network);
211214
let derivation_path = if use_address_index {
212215
format!("m/49'/0'/0'/0/{account_number}")
213216
} else {
@@ -222,8 +225,8 @@ impl KeySet {
222225
seed: &[u8],
223226
network: Network,
224227
use_address_index: bool,
228+
account_number: u32,
225229
) -> Result<Self, DefaultSignerError> {
226-
let account_number = account_number(network);
227230
let derivation_path = if use_address_index {
228231
format!("m/44'/0'/0'/0/{account_number}")
229232
} else {
@@ -264,25 +267,31 @@ impl From<bitcoin::bip32::Error> for DefaultSignerError {
264267

265268
impl DefaultSigner {
266269
pub fn new(seed: &[u8], network: Network) -> Result<Self, DefaultSignerError> {
267-
Self::with_keyset_type(seed, network, KeySetType::Default, false)
270+
Self::with_keyset_type(seed, network, KeySetType::Default, false, None)
268271
}
269272

270273
pub fn with_keyset_type(
271274
seed: &[u8],
272275
network: Network,
273276
key_type: KeySetType,
274277
use_address_index: bool,
278+
account_no: Option<u32>,
275279
) -> Result<Self, DefaultSignerError> {
280+
let account_number = account_no.unwrap_or_else(|| account_number(network));
276281
let key_set = match key_type {
277-
KeySetType::Default => KeySet::default_keys(seed, network),
278-
KeySetType::Taproot => KeySet::taproot_keys(seed, network, use_address_index),
282+
KeySetType::Default => KeySet::default_keys(seed, network, account_number),
283+
KeySetType::Taproot => {
284+
KeySet::taproot_keys(seed, network, use_address_index, account_number)
285+
}
279286
KeySetType::NativeSegwit => {
280-
KeySet::native_segwit_keys(seed, network, use_address_index)
287+
KeySet::native_segwit_keys(seed, network, use_address_index, account_number)
281288
}
282289
KeySetType::WrappedSegwit => {
283-
KeySet::wrapped_segwit_keys(seed, network, use_address_index)
290+
KeySet::wrapped_segwit_keys(seed, network, use_address_index, account_number)
291+
}
292+
KeySetType::Legacy => {
293+
KeySet::legacy_bitcoin_keys(seed, network, use_address_index, account_number)
284294
}
285-
KeySetType::Legacy => KeySet::legacy_bitcoin_keys(seed, network, use_address_index),
286295
}?;
287296
Ok(Self::from_key_set(key_set))
288297
}

docs/breez-sdk/snippets/flutter/lib/getting_started.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Future<void> initSdkAdvanced() async {
4545
// url: "https://custom.chain.service",
4646
// credentials: Credentials(
4747
// username: "service-username", password: "service-password"));
48-
// builder.withKeySet(keySetType: <your key set type>, useAddressIndex: <use address index>);
48+
// builder.withKeySet(keySetType: <your key set type>, useAddressIndex: <use address index>, accountNumber: <account number>);
4949
final sdk = await builder.build();
5050
// ANCHOR_END: init-sdk-advanced
5151
print(sdk);

docs/breez-sdk/snippets/go/getting_started.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func InitSdkAdvanced() (*breez_sdk_spark.BreezSdk, error) {
5656
// You can also pass your custom implementations:
5757
// builder.WithChainService(<your chain service implementation>)
5858
// builder.WithRestClient(<your rest client implementation>)
59-
// builder.WithKeySet(<your key set type>, <use address index>)
59+
// builder.WithKeySet(<your key set type>, <use address index>, <account number>)
6060
sdk, err := builder.Build()
6161

6262
return sdk, err

docs/breez-sdk/snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/GettingStarted.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class GettingStarted {
4343
// You can also pass your custom implementations:
4444
// builder.withChainService(<your chain service implementation>)
4545
// builder.withRestClient(<your rest client implementation>)
46-
// builder.withKeySet(<your key set type>, <use address index>)
46+
// builder.withKeySet(<your key set type>, <use address index>, <account number>)
4747
val sdk = builder.build()
4848
} catch (e: Exception) {
4949
// handle error

docs/breez-sdk/snippets/python/src/getting_started.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async def init_sdk_advanced():
5353
# You can also pass your custom implementations:
5454
# builder.with_chain_service(<your chain service implementation>)
5555
# builder.with_rest_client(<your rest client implementation>)
56-
# builder.with_key_set(<your key set type>, <use address index>)
56+
# builder.with_key_set(<your key set type>, <use address index>, <account number>)
5757
sdk = await builder.build()
5858
return sdk
5959
except Exception as error:

docs/breez-sdk/snippets/rust/src/getting_started.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub(crate) async fn init_sdk_advanced() -> Result<BreezSdk> {
5252
// You can also pass your custom implementations:
5353
// let builder = builder.with_chain_service(<your chain service implementation>)
5454
// let builder = builder.with_rest_client(<your rest client implementation>)
55-
// let builder = builder.with_key_set(<your key set type>, <use address index>)
55+
// let builder = builder.with_key_set(<your key set type>, <use address index>, <account number>)
5656
let sdk = builder.build().await?;
5757

5858
// ANCHOR_END: init-sdk-advanced

docs/breez-sdk/snippets/wasm/getting_started.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const exampleGettingStartedAdvanced = async () => {
6161
// You can also pass your custom implementations:
6262
// builder = builder.withChainService(<your chain service implementation>)
6363
// builder = builder.withRestClient(<your rest client implementation>)
64-
// builder = builder.withKeySet(<your key set type>, <use address index>)
64+
// builder = builder.withKeySet(<your key set type>, <use address index>, <account number>)
6565
const sdk = await builder.build()
6666
// ANCHOR_END: init-sdk-advanced
6767
}

0 commit comments

Comments
 (0)