Skip to content

Commit

Permalink
Final integration to Bitbox02, Ledger and Portal NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
22388o committed Dec 18, 2024
1 parent 02f1462 commit 9b37e73
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 48 deletions.
182 changes: 182 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@gluestack-style/react": "^1.0.45",
"@gluestack-ui/config": "^1.1.2",
"@gluestack-ui/themed": "^1.1.62",
"@ledgerhq/hw-app-btc": "^10.5.0",
"@ledgerhq/hw-transport-webhid": "^6.30.0",
"@ledgerhq/react-native-hid": "^6.32.4",
"@mempool/mempool.js": "^2.2.4",
"@nostr-dev-kit/ndk": "^2.10.7",
Expand All @@ -31,6 +33,7 @@
"bip21": "^3.0.0",
"bip32": "^5.0.0-rc.0",
"bip39": "^3.1.0",
"bitbox-api": "^0.8.0",
"bitcoinjs-lib": "^6.1.7",
"bolt11": "^1.4.1",
"buffer": "^6.0.3",
Expand Down
24 changes: 0 additions & 24 deletions src/app/hardware/bitbox/bitbox.ts

This file was deleted.

58 changes: 56 additions & 2 deletions src/app/hardware/bitbox/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
import { BitBox } from './bitbox';
import * as bitbox from 'bitbox-api'; // Ensure the 'bitbox-api' module is correctly installed and accessible

export { BitBox };
// Main function to get and interact with the BitBox hardware
async function getHardware(): Promise<void> {
try {
const onClose = () => {
console.log('BitBox device disconnected.');
// Additional logic to handle disconnect
};

// Automatically connect to BitBox
const unpaired = await bitbox.bitbox02ConnectAuto(onClose);
console.log('Unpaired BitBox connected.');

// Handle pairing and unlocking
const pairing = await unpaired.unlockAndPair();
const pairingCode = pairing.getPairingCode();

if (pairingCode) {
console.log('Display this pairing code to the user:', pairingCode);
// Add logic here to visually display the pairing code in your UI if needed
}

// Wait for user to confirm pairing on the device
const bb02 = await pairing.waitConfirm();
console.log('Device paired successfully.');

// Retrieve information from the device
console.log('Product:', bb02.product());
console.log('Supports Ethereum functionality?', bb02.ethSupported());
const deviceInfos = await bb02.deviceInfo();
console.log('Device information:', deviceInfos);
} catch (err) {
const typedErr = bitbox.ensureError(err); // Ensure errors are properly typed and handled
console.error('An error occurred while connecting to BitBox:', typedErr);
}
}

// Placeholder object for BitBox functions
export const BitBox = {
connect: async (): Promise<void> => {
await getHardware();
},
signTransaction: async (transaction: object): Promise<string> => {
console.log('Signing transaction:', transaction);
// Add logic here to sign a transaction using BitBox
return 'SignedTransactionHash'; // Replace with the actual result from the API
},
getAddress: async (path: string): Promise<string> => {
console.log(`Retrieving address for path: ${path}`);
// Add logic here to fetch an address using BitBox
return 'bitbox-address-123'; // Replace with the actual result from the API
},
};

// Export the `getHardware` function as well for standalone usage
export { getHardware };
54 changes: 49 additions & 5 deletions src/app/hardware/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import { ReactPortal } from "react";
import { bitbox } from "./bitbox/bitbox";
import TrezorConnect, { Params, SignTransaction } from '@trezor/connect';

aysnc function getHardware(): Promise<ReactPortal> {
return bitbox();
}
async function connectToTrezor() {
try {
// Initialize Trezor Connect
TrezorConnect.init({
manifest: {
email: 'your-email@example.com', // Your email address (required for Trezor policy compliance)
appUrl: 'https://your-app-url.com', // Your application's URL
},
});

// Check device and get public key
const response = await TrezorConnect.getPublicKey({
path: "m/44'/0'/0'/0/0", // Example BIP44 path (update according to your requirements)
});

if (response.success) {
console.log('Public Key:', response.payload.publicKey);
console.log('Serialized Path:', response.payload.serializedPath);
console.log('XPub:', response.payload.xpub);
} else {
console.error('Failed to connect:', response.payload.error);
}
} catch (error) {
console.error('An error occurred while connecting to Trezor:', error);
}
}
// Example of signing a transaction with Trezor
async function signTransaction(transaction: Params<SignTransaction>) {
try {
const response = await TrezorConnect.signTransaction(transaction);

if (response.success) {
console.log('Transaction Signed:', response.payload);
return response.payload.signatures;
} else {
console.error('Transaction signing failed:', response.payload.error);
return null;
}
} catch (error) {
console.error('An error occurred while signing transaction with Trezor:', error);
return null;
}
}
// Export reusable functions for connecting and using Trezor
export const Trezor = {
connect: connectToTrezor,
signTransaction,
};
Loading

0 comments on commit 9b37e73

Please sign in to comment.