Skip to content

Commit 9b37e73

Browse files
committed
Final integration to Bitbox02, Ledger and Portal NFC
1 parent 02f1462 commit 9b37e73

File tree

8 files changed

+438
-48
lines changed

8 files changed

+438
-48
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"@gluestack-style/react": "^1.0.45",
1818
"@gluestack-ui/config": "^1.1.2",
1919
"@gluestack-ui/themed": "^1.1.62",
20+
"@ledgerhq/hw-app-btc": "^10.5.0",
21+
"@ledgerhq/hw-transport-webhid": "^6.30.0",
2022
"@ledgerhq/react-native-hid": "^6.32.4",
2123
"@mempool/mempool.js": "^2.2.4",
2224
"@nostr-dev-kit/ndk": "^2.10.7",
@@ -31,6 +33,7 @@
3133
"bip21": "^3.0.0",
3234
"bip32": "^5.0.0-rc.0",
3335
"bip39": "^3.1.0",
36+
"bitbox-api": "^0.8.0",
3437
"bitcoinjs-lib": "^6.1.7",
3538
"bolt11": "^1.4.1",
3639
"buffer": "^6.0.3",

src/app/hardware/bitbox/bitbox.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/app/hardware/bitbox/index.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1-
import { BitBox } from './bitbox';
1+
import * as bitbox from 'bitbox-api'; // Ensure the 'bitbox-api' module is correctly installed and accessible
22

3-
export { BitBox };
3+
// Main function to get and interact with the BitBox hardware
4+
async function getHardware(): Promise<void> {
5+
try {
6+
const onClose = () => {
7+
console.log('BitBox device disconnected.');
8+
// Additional logic to handle disconnect
9+
};
10+
11+
// Automatically connect to BitBox
12+
const unpaired = await bitbox.bitbox02ConnectAuto(onClose);
13+
console.log('Unpaired BitBox connected.');
14+
15+
// Handle pairing and unlocking
16+
const pairing = await unpaired.unlockAndPair();
17+
const pairingCode = pairing.getPairingCode();
18+
19+
if (pairingCode) {
20+
console.log('Display this pairing code to the user:', pairingCode);
21+
// Add logic here to visually display the pairing code in your UI if needed
22+
}
23+
24+
// Wait for user to confirm pairing on the device
25+
const bb02 = await pairing.waitConfirm();
26+
console.log('Device paired successfully.');
27+
28+
// Retrieve information from the device
29+
console.log('Product:', bb02.product());
30+
console.log('Supports Ethereum functionality?', bb02.ethSupported());
31+
const deviceInfos = await bb02.deviceInfo();
32+
console.log('Device information:', deviceInfos);
33+
} catch (err) {
34+
const typedErr = bitbox.ensureError(err); // Ensure errors are properly typed and handled
35+
console.error('An error occurred while connecting to BitBox:', typedErr);
36+
}
37+
}
38+
39+
// Placeholder object for BitBox functions
40+
export const BitBox = {
41+
connect: async (): Promise<void> => {
42+
await getHardware();
43+
},
44+
signTransaction: async (transaction: object): Promise<string> => {
45+
console.log('Signing transaction:', transaction);
46+
// Add logic here to sign a transaction using BitBox
47+
return 'SignedTransactionHash'; // Replace with the actual result from the API
48+
},
49+
getAddress: async (path: string): Promise<string> => {
50+
console.log(`Retrieving address for path: ${path}`);
51+
// Add logic here to fetch an address using BitBox
52+
return 'bitbox-address-123'; // Replace with the actual result from the API
53+
},
54+
};
55+
56+
// Export the `getHardware` function as well for standalone usage
57+
export { getHardware };

src/app/hardware/index.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
1-
import { ReactPortal } from "react";
2-
import { bitbox } from "./bitbox/bitbox";
1+
import TrezorConnect, { Params, SignTransaction } from '@trezor/connect';
32

4-
aysnc function getHardware(): Promise<ReactPortal> {
5-
return bitbox();
6-
}
3+
async function connectToTrezor() {
4+
try {
5+
// Initialize Trezor Connect
6+
TrezorConnect.init({
7+
manifest: {
8+
email: '[email protected]', // Your email address (required for Trezor policy compliance)
9+
appUrl: 'https://your-app-url.com', // Your application's URL
10+
},
11+
});
12+
13+
// Check device and get public key
14+
const response = await TrezorConnect.getPublicKey({
15+
path: "m/44'/0'/0'/0/0", // Example BIP44 path (update according to your requirements)
16+
});
17+
18+
if (response.success) {
19+
console.log('Public Key:', response.payload.publicKey);
20+
console.log('Serialized Path:', response.payload.serializedPath);
21+
console.log('XPub:', response.payload.xpub);
22+
} else {
23+
console.error('Failed to connect:', response.payload.error);
24+
}
25+
} catch (error) {
26+
console.error('An error occurred while connecting to Trezor:', error);
27+
}
28+
}
29+
// Example of signing a transaction with Trezor
30+
async function signTransaction(transaction: Params<SignTransaction>) {
31+
try {
32+
const response = await TrezorConnect.signTransaction(transaction);
33+
34+
if (response.success) {
35+
console.log('Transaction Signed:', response.payload);
36+
return response.payload.signatures;
37+
} else {
38+
console.error('Transaction signing failed:', response.payload.error);
39+
return null;
40+
}
41+
} catch (error) {
42+
console.error('An error occurred while signing transaction with Trezor:', error);
43+
return null;
44+
}
45+
}
46+
// Export reusable functions for connecting and using Trezor
47+
export const Trezor = {
48+
connect: connectToTrezor,
49+
signTransaction,
50+
};

0 commit comments

Comments
 (0)