Skip to content

Commit 078d1e5

Browse files
committed
fix!: replace usize with u32 or smaller
BREAKING CHANGE.
1 parent 492ae4c commit 078d1e5

7 files changed

Lines changed: 44 additions & 44 deletions

File tree

cktap-swift/Sources/CKTap/cktap_ffi.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,8 +2680,8 @@ extension DumpError: Foundation.LocalizedError {
26802680

26812681
public enum KeyError: Swift.Error {
26822682

2683-
2684-
2683+
2684+
26852685
case Secp256k1(msg: String
26862686
)
26872687
case KeyFromSlice(msg: String

cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ async fn main() -> Result<(), CliError> {
228228
dbg!(response);
229229
}
230230
TapSignerCommand::Derive { path } => {
231-
// let test_path:Vec<usize> = ts.path.clone().unwrap().iter().map(|p| p ^ (1 << 31)).collect();
231+
// let test_path:Vec<u32> = ts.path.clone().unwrap().iter().map(|p| p ^ (1 << 31)).collect();
232232
// dbg!(test_path);
233233
dbg!(&ts.derive(path.unwrap_or_default(), &cvc()).await);
234234
}

lib/src/apdu.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ impl CommandApdu for StatusCommand {
9797
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
9898
pub struct StatusResponse {
9999
/// Version of CBOR protocol in use.
100-
pub proto: usize,
101-
/// Firmware version of card itself.
100+
pub proto: u32,
101+
/// Firmware version of the card itself.
102102
pub ver: String,
103103
/// Card birth block height (int) (fixed after production).
104-
pub birth: usize,
104+
pub birth: u32,
105105
/// SATSCARD Only: Tuple of (active_slot, num_slots).
106106
pub slots: Option<(u8, u8)>,
107107
/// SATSCARD Only: Payment address, middle chars blanked out with 3 underscores.
@@ -114,9 +114,9 @@ pub struct StatusResponse {
114114
/// effect. It encodes a BIP-32 derivation path, like m/84h/0h/0h, which is a typical value for
115115
/// segwit usage, although the value is controlled by the wallet application. The field is only
116116
/// present if a master key has been picked (i.e., setup is complete).
117-
pub path: Option<Vec<usize>>,
117+
pub path: Option<Vec<u32>>,
118118
/// Counts up, when backup command is used.
119-
pub num_backups: Option<usize>,
119+
pub num_backups: Option<u32>,
120120
/// Public key unique to this card (fixed for card life) aka: card_pubkey.
121121
#[serde(with = "serde_bytes")]
122122
pub pubkey: Vec<u8>,
@@ -836,7 +836,7 @@ impl CommandApdu for DumpCommand {
836836
pub struct DumpResponse {
837837
/// slot just made
838838
#[allow(unused)] // TODO verify this is correct slot
839-
pub slot: usize,
839+
pub slot: u8,
840840
/// private key for spending (for addr), 32 bytes
841841
/// The private keys are encrypted, XORed with the session key
842842
#[serde(with = "serde_bytes")]

lib/src/error.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ pub enum ChangeError {
131131
#[error(transparent)]
132132
CkTap(#[from] CkTapError),
133133
#[error("new cvc is too short, must be at least 6 bytes, was only {0} bytes")]
134-
TooShort(usize),
134+
TooShort(u8),
135135
#[error("new cvc is too long, must be at most 32 bytes, was {0} bytes")]
136-
TooLong(usize),
136+
TooLong(u8),
137137
#[error("new cvc is the same as the old one")]
138138
SameAsOld,
139139
}
@@ -228,15 +228,15 @@ pub enum DumpError {
228228
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
229229
pub enum SignPsbtError {
230230
#[error("Invalid path at index: {0}")]
231-
InvalidPath(usize),
231+
InvalidPath(u32),
232232
#[error("Invalid script at index: {0}")]
233-
InvalidScript(usize),
233+
InvalidScript(u32),
234234
#[error("Missing pubkey at index: {0}")]
235-
MissingPubkey(usize),
235+
MissingPubkey(u32),
236236
#[error("Missing UTXO at index: {0}")]
237-
MissingUtxo(usize),
237+
MissingUtxo(u32),
238238
#[error("Pubkey mismatch at index: {0}")]
239-
PubkeyMismatch(usize),
239+
PubkeyMismatch(u32),
240240
#[error("Sighash error: {0}")]
241241
SighashError(String),
242242
#[error("Signature error: {0}")]

lib/src/sats_card.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ use std::sync::Arc;
2525
pub struct SatsCard {
2626
pub transport: Arc<dyn CkTransport>,
2727
pub secp: Secp256k1<All>,
28-
pub proto: usize,
28+
pub proto: u32,
2929
pub ver: String,
30-
pub birth: usize,
30+
pub birth: u32,
3131
pub slots: (u8, u8),
3232
pub addr: Option<String>,
3333
pub pubkey: PublicKey,
@@ -372,25 +372,25 @@ impl SatsCard {
372372
let witness_utxo = input
373373
.witness_utxo
374374
.as_ref()
375-
.ok_or(Error::MissingUtxo(input_index))?;
375+
.ok_or(Error::MissingUtxo(input_index as u32))?;
376376

377377
let amount = witness_utxo.value;
378378

379379
// extract the P2WPKH script from PSBT
380380
let script_pubkey = &witness_utxo.script_pubkey;
381381
if !script_pubkey.is_p2wpkh() {
382-
return Err(Error::InvalidScript(input_index));
382+
return Err(Error::InvalidScript(input_index as u32));
383383
}
384384

385385
// get the public key from the PSBT
386386
let key_pairs = &input.bip32_derivation;
387387
let (psbt_pubkey, (_fingerprint, path)) = key_pairs
388388
.iter()
389389
.next()
390-
.ok_or(Error::MissingPubkey(input_index))?;
390+
.ok_or(Error::MissingPubkey(input_index as u32))?;
391391

392392
if !path.is_empty() {
393-
return Err(Error::InvalidPath(input_index));
393+
return Err(Error::InvalidPath(input_index as u32));
394394
}
395395

396396
// calculate sighash
@@ -408,7 +408,7 @@ impl SatsCard {
408408

409409
// verify that SATSCARD used the same public key as the PSBT
410410
if sign_response.pubkey != psbt_pubkey.serialize() {
411-
return Err(Error::PubkeyMismatch(input_index));
411+
return Err(Error::PubkeyMismatch(input_index as u32));
412412
}
413413

414414
// update the PSBT input with the signature

lib/src/sats_chip.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ use crate::tap_signer::TapSignerShared;
2020
pub struct SatsChip {
2121
pub transport: Arc<dyn CkTransport>,
2222
pub secp: Secp256k1<All>,
23-
pub proto: usize,
23+
pub proto: u32,
2424
pub ver: String,
25-
pub birth: usize,
26-
pub path: Option<Vec<usize>>,
25+
pub birth: u32,
26+
pub path: Option<Vec<u32>>,
2727
// [(1<<31)+84, (1<<31), (1<<31)], user-defined, will be omitted if not yet setup
28-
pub num_backups: Option<usize>,
28+
pub num_backups: Option<u32>,
2929
pub pubkey: PublicKey,
3030
pub card_nonce: [u8; 16],
3131
pub auth_delay: Option<u8>,

lib/src/tap_signer.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ use bitcoin::secp256k1::{self, All, Message, Secp256k1, ecdsa::Signature};
2020
use bitcoin_hashes::sha256;
2121
use std::sync::Arc;
2222

23-
const BIP84_PATH_LEN: usize = 5;
23+
const BIP84_PATH_LEN: u32 = 5;
2424

2525
// BIP84 derivation path structure, m / 84' / 0' / account' / change / address_index
2626

2727
// Derivation sub-path indexes that must be hardened
28-
const BIP84_HARDENED_SUBPATH: [usize; 3] = [0, 1, 2];
28+
const BIP84_HARDENED_SUBPATH: [u32; 3] = [0, 1, 2];
2929

3030
pub struct TapSigner {
3131
pub transport: Arc<dyn CkTransport>,
3232
pub secp: Secp256k1<All>,
33-
pub proto: usize,
33+
pub proto: u32,
3434
pub ver: String,
35-
pub birth: usize,
36-
pub path: Option<Vec<usize>>,
35+
pub birth: u32,
36+
pub path: Option<Vec<u32>>,
3737
// [(1<<31)+84, (1<<31), (1<<31)], user-defined, will be omitted if not yet setup
38-
pub num_backups: Option<usize>,
38+
pub num_backups: Option<u32>,
3939
pub pubkey: PublicKey,
4040
pub card_nonce: [u8; 16],
4141
pub auth_delay: Option<u8>,
@@ -162,32 +162,32 @@ pub trait TapSignerShared: Authentication {
162162
let witness_utxo = input
163163
.witness_utxo
164164
.as_ref()
165-
.ok_or(SignPsbtError::MissingUtxo(input_index))?;
165+
.ok_or(SignPsbtError::MissingUtxo(input_index as u32))?;
166166

167167
let amount = witness_utxo.value;
168168

169169
// extract the P2WPKH script from PSBT
170170
let script_pubkey = &witness_utxo.script_pubkey;
171171
if !script_pubkey.is_p2wpkh() {
172-
return Err(SignPsbtError::InvalidScript(input_index));
172+
return Err(SignPsbtError::InvalidScript(input_index as u32));
173173
}
174174

175175
// get the public key from the PSBT
176176
let key_pairs = &input.bip32_derivation;
177177
let (psbt_pubkey, (_fingerprint, path)) = key_pairs
178178
.iter()
179179
.next()
180-
.ok_or(SignPsbtError::MissingPubkey(input_index))?;
180+
.ok_or(SignPsbtError::MissingPubkey(input_index as u32))?;
181181

182182
let path = path.to_u32_vec();
183183

184-
if path.len() != BIP84_PATH_LEN {
185-
return Err(SignPsbtError::InvalidPath(input_index));
184+
if path.len() as u32 != BIP84_PATH_LEN {
185+
return Err(SignPsbtError::InvalidPath(input_index as u32));
186186
}
187187

188-
let sub_path = BIP84_HARDENED_SUBPATH.map(|i| path[i]);
188+
let sub_path = BIP84_HARDENED_SUBPATH.map(|i| path[i as usize]);
189189
if sub_path.iter().any(|p| *p > BIP32_HARDENED_MASK) {
190-
return Err(SignPsbtError::InvalidPath(input_index));
190+
return Err(SignPsbtError::InvalidPath(input_index as u32));
191191
}
192192

193193
// calculate sighash
@@ -214,7 +214,7 @@ pub trait TapSignerShared: Authentication {
214214
.collect();
215215
let derive_response = self.derive(path, cvc).await;
216216
if derive_response.is_err() {
217-
return Err(SignPsbtError::PubkeyMismatch(input_index));
217+
return Err(SignPsbtError::PubkeyMismatch(input_index as u32));
218218
}
219219

220220
// update signature to the new one we just derived
@@ -223,7 +223,7 @@ pub trait TapSignerShared: Authentication {
223223

224224
// if still not matching, return error
225225
if sign_response.pubkey != psbt_pubkey.serialize() {
226-
return Err(SignPsbtError::PubkeyMismatch(input_index));
226+
return Err(SignPsbtError::PubkeyMismatch(input_index as u32));
227227
}
228228
}
229229

@@ -286,11 +286,11 @@ pub trait TapSignerShared: Authentication {
286286
/// Change the CVC used for card authentication to a new user provided one
287287
async fn change(&mut self, new_cvc: &str, cvc: &str) -> Result<(), ChangeError> {
288288
if new_cvc.len() < 6 {
289-
return Err(ChangeError::TooShort(new_cvc.len()));
289+
return Err(ChangeError::TooShort(new_cvc.len() as u8));
290290
}
291291

292292
if new_cvc.len() > 32 {
293-
return Err(ChangeError::TooLong(new_cvc.len()));
293+
return Err(ChangeError::TooLong(new_cvc.len() as u8));
294294
}
295295

296296
if new_cvc == cvc {

0 commit comments

Comments
 (0)